Skip to content

Commit 8141b49

Browse files
author
DylanBulmer
committed
Fix typescript inference bugs
1 parent cdb5ac1 commit 8141b49

13 files changed

Lines changed: 560 additions & 566 deletions

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
[![CodeQL](https://github.com/CodrJS/mongo/actions/workflows/codeql.yml/badge.svg?branch=main)](https://github.com/CodrJS/mongo/actions/workflows/codeql.yml)
55

66
## Purpose
7-
This module is for defining and linking all entities stoared in Mongo.
7+
8+
This module is for defining and linking all entities stored in Mongo.
89

910
## Getting started
1011

@@ -16,7 +17,14 @@ git clone git@github.com:CodrJS/mongo.git
1617
yarn install
1718
```
1819

20+
## Environment
21+
22+
Necessary variables needed to run:
23+
24+
| Env var | Location | Required | Description |
25+
| ----------- | ----------- | -------- | ------------------------------------------------------------------------- |
26+
| `MONGO_URI` | `mongo.uri` | `true` | MongoDB - server URL, please include username and password to this string |
27+
1928
## TODO
2029

2130
- [ ] .
22-

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@codrjs/mongo",
3-
"version": "1.0.2",
4-
"description": "",
3+
"version": "1.0.3",
4+
"description": "A utility module to manage all mongodb connections.",
55
"main": "./cjs/index.js",
66
"module": "./esm/index.js",
77
"types": "./types/index.d.ts",
@@ -27,8 +27,7 @@
2727
"lint": "eslint -c .eslintrc.json --ignore-path .eslintignore --ext .ts src",
2828
"preversion": "yarn lint",
2929
"version": "yarn format && git add -A src",
30-
"postversion": "git push && git push --tags",
31-
"convert": "taplo get -f src/examples/project.toml -o json > src/examples/project.json"
30+
"postversion": "git push && git push --tags"
3231
},
3332
"devDependencies": {
3433
"@swc/cli": "^0.1.57",
@@ -46,10 +45,14 @@
4645
"typescript": "^4.9.4"
4746
},
4847
"dependencies": {
48+
"@casl/ability": "^6.5.0",
4949
"@casl/mongoose": "^7.1.3",
5050
"@codrjs/config": "^1.0.7",
5151
"@codrjs/models": "^1.0.11",
52-
"mongoose": "^7.2.2",
52+
"mongoose": "^7.3.0",
5353
"tsc-alias": "^1.8.6"
54+
},
55+
"peerDependencies": {
56+
"mongoose": "^7.3.0"
5457
}
5558
}

src/schemas/Profile.ts

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,54 @@
1-
import { IProfile } from "@codrjs/models";
1+
import { IProfile, IUser } from "@codrjs/models";
22
import { Schema, SchemaTypes } from "mongoose";
33
import {
44
AccessibleFieldsModel,
55
AccessibleModel,
66
accessibleFieldsPlugin,
77
accessibleRecordsPlugin,
88
} from "@casl/mongoose";
9-
import { UserDocument } from "./User";
109

1110
export type ProfileDocument = IProfile & AccessibleFieldsModel<IProfile>;
1211

13-
export function createProfileModel(userModel?: AccessibleModel<UserDocument>) {
14-
if (userModel) {
15-
const ProfileSchema = new Schema<IProfile>(
16-
{
17-
avatarUrl: String,
18-
userId: {
19-
type: SchemaTypes.ObjectId,
20-
required: true,
21-
unique: true,
22-
index: true,
23-
ref: "User",
24-
},
25-
username: {
26-
type: String,
27-
required: true,
28-
unique: true,
29-
index: true,
30-
},
31-
name: {
32-
type: {
33-
first: String,
34-
last: String,
35-
preferred: String,
36-
},
37-
required: true,
38-
},
12+
export function createProfileModel(userModel: AccessibleModel<IUser>) {
13+
const ProfileSchema = new Schema<IProfile>(
14+
{
15+
avatarUrl: String,
16+
userId: {
17+
type: SchemaTypes.ObjectId,
18+
required: true,
19+
unique: true,
20+
index: true,
21+
ref: "User",
3922
},
40-
{
41-
timestamps: true,
23+
username: {
24+
type: String,
25+
required: true,
26+
unique: true,
27+
index: true,
28+
},
29+
name: {
30+
type: {
31+
first: String,
32+
last: String,
33+
preferred: String,
34+
},
35+
required: true,
4236
},
43-
);
37+
},
38+
{
39+
timestamps: true,
40+
},
41+
);
4442

45-
ProfileSchema.virtual("user", {
46-
ref: userModel,
47-
localField: "userId",
48-
foreignField: "_id",
49-
});
43+
ProfileSchema.virtual("user", {
44+
ref: userModel,
45+
localField: "userId",
46+
foreignField: "_id",
47+
});
5048

51-
ProfileSchema.plugin(accessibleFieldsPlugin);
52-
ProfileSchema.plugin(accessibleRecordsPlugin);
49+
// exports Profile model.
50+
ProfileSchema.plugin(accessibleFieldsPlugin);
51+
ProfileSchema.plugin(accessibleRecordsPlugin);
5352

54-
return ProfileSchema;
55-
} else {
56-
throw "User model is not defined.";
57-
}
53+
return ProfileSchema;
5854
}

src/schemas/Session.ts

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,50 @@
1-
import { ISession } from "@codrjs/models";
1+
import { ISession, IUser } from "@codrjs/models";
22
import { Schema, SchemaTypes } from "mongoose";
33
import {
44
AccessibleFieldsModel,
55
AccessibleModel,
66
accessibleFieldsPlugin,
77
accessibleRecordsPlugin,
88
} from "@casl/mongoose";
9-
import { UserDocument } from "./User";
109

1110
export type SessionDocument = ISession & AccessibleFieldsModel<ISession>;
1211

13-
export function createSessionModel(userModel?: AccessibleModel<UserDocument>) {
14-
if (userModel) {
15-
const SessionSchema = new Schema<ISession>(
16-
{
17-
status: {
18-
type: String,
19-
enum: ["INITIATING", "ESTABLISHED", "CLOSED"],
20-
required: true,
21-
default: "INITIATING",
22-
},
23-
userId: {
24-
type: SchemaTypes.ObjectId,
25-
required: true,
26-
unique: false,
27-
index: true,
28-
ref: "User",
29-
},
30-
os: { type: String },
31-
browser: { type: String },
32-
ipAddress: { type: String },
33-
createdAt: String,
34-
updatedAt: String,
12+
export function createSessionModel(userModel: AccessibleModel<IUser>) {
13+
const SessionSchema = new Schema<ISession>(
14+
{
15+
status: {
16+
type: String,
17+
enum: ["INITIATING", "ESTABLISHED", "CLOSED"],
18+
required: true,
19+
default: "INITIATING",
3520
},
36-
{
37-
timestamps: true,
21+
userId: {
22+
type: SchemaTypes.ObjectId,
23+
required: true,
24+
unique: false,
25+
index: true,
26+
ref: "User",
3827
},
39-
);
28+
os: { type: String },
29+
browser: { type: String },
30+
ipAddress: { type: String },
31+
createdAt: String,
32+
updatedAt: String,
33+
},
34+
{
35+
timestamps: true,
36+
},
37+
);
4038

41-
SessionSchema.virtual("user", {
42-
ref: userModel,
43-
localField: "userId",
44-
foreignField: "_id",
45-
});
39+
SessionSchema.virtual("user", {
40+
ref: userModel,
41+
localField: "userId",
42+
foreignField: "_id",
43+
});
4644

47-
// exports Session model.
48-
SessionSchema.plugin(accessibleFieldsPlugin);
49-
SessionSchema.plugin(accessibleRecordsPlugin);
45+
// exports Session model.
46+
SessionSchema.plugin(accessibleFieldsPlugin);
47+
SessionSchema.plugin(accessibleRecordsPlugin);
5048

51-
return SessionSchema;
52-
} else {
53-
throw "User model is not defined.";
54-
}
49+
return SessionSchema;
5550
}

src/schemas/UserGroup.ts

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,73 @@
1-
import { IUserGroup } from "@codrjs/models";
1+
import { IUser, IUserGroup } from "@codrjs/models";
22
import { Schema, SchemaTypes } from "mongoose";
33
import {
44
AccessibleFieldsModel,
55
AccessibleModel,
66
accessibleFieldsPlugin,
77
accessibleRecordsPlugin,
88
} from "@casl/mongoose";
9-
import { UserDocument } from "./User";
109

1110
export type UserGroupDocument = IUserGroup & AccessibleFieldsModel<IUserGroup>;
1211

13-
export function createUserGroupModel(userModel?: AccessibleModel<UserDocument>) {
14-
if (userModel) {
15-
const UserGroupSchema = new Schema<UserGroupDocument>(
16-
{
17-
createdBy: {
18-
required: true,
19-
index: true,
12+
export function createUserGroupModel(userModel: AccessibleModel<IUser>) {
13+
const UserGroupSchema = new Schema<UserGroupDocument>(
14+
{
15+
createdBy: {
16+
required: true,
17+
index: true,
18+
type: SchemaTypes.ObjectId,
19+
ref: "User",
20+
},
21+
members: {
22+
items: {
2023
type: SchemaTypes.ObjectId,
2124
ref: "User",
2225
},
23-
members: {
24-
items: {
25-
type: SchemaTypes.ObjectId,
26-
ref: "User",
27-
},
28-
},
29-
teams: {
30-
items: {
31-
type: SchemaTypes.ObjectId,
32-
ref: "UserGroup",
33-
},
26+
},
27+
teams: {
28+
items: {
29+
type: SchemaTypes.ObjectId,
30+
ref: "UserGroup",
3431
},
35-
name: {
36-
type: "String",
37-
required: true,
38-
index: true,
39-
default: "Unnamed Group",
32+
},
33+
name: {
34+
type: "String",
35+
required: true,
36+
index: true,
37+
default: "Unnamed Group",
38+
},
39+
flags: {
40+
type: {
41+
isAnonymous: Boolean,
42+
isDeleted: Boolean,
43+
isJoinable: Boolean,
44+
isPrivate: Boolean,
4045
},
41-
flags: {
42-
type: {
43-
isAnonymous: Boolean,
44-
isDeleted: Boolean,
45-
isJoinable: Boolean,
46-
isPrivate: Boolean,
47-
},
48-
required: true,
49-
default: {
50-
isAnonymous: false,
51-
isDeleted: false,
52-
isJoinable: false,
53-
isPrivate: false,
54-
},
46+
required: true,
47+
default: {
48+
isAnonymous: false,
49+
isDeleted: false,
50+
isJoinable: false,
51+
isPrivate: false,
5552
},
56-
createdAt: { type: String },
57-
updatedAt: { type: String },
58-
},
59-
{
60-
timestamps: true,
6153
},
62-
);
54+
createdAt: { type: String },
55+
updatedAt: { type: String },
56+
},
57+
{
58+
timestamps: true,
59+
},
60+
);
6361

64-
UserGroupSchema.virtual("user", {
65-
ref: userModel,
66-
localField: "userId",
67-
foreignField: "_id",
68-
});
62+
UserGroupSchema.virtual("user", {
63+
ref: userModel,
64+
localField: "userId",
65+
foreignField: "_id",
66+
});
6967

70-
// exports UserGroup model.
71-
UserGroupSchema.plugin(accessibleFieldsPlugin);
72-
UserGroupSchema.plugin(accessibleRecordsPlugin);
68+
// exports UserGroup model.
69+
UserGroupSchema.plugin(accessibleFieldsPlugin);
70+
UserGroupSchema.plugin(accessibleRecordsPlugin);
7371

74-
return UserGroupSchema;
75-
} else {
76-
throw "User model is not defined.";
77-
}
72+
return UserGroupSchema;
7873
}

src/types/CoreDatabase.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { DatabaseEnum } from "./Database";
22

3-
export type CoreModelType = "CONFIG" | "AUDIT";
43
export enum CoreModelEnum {
54
CONFIG = "Config",
65
AUDIT = "Audit",
76
}
7+
export type CoreModelType = keyof typeof CoreModelEnum;
8+
export type CoreModels = `${CoreModelEnum}`;
89

910
export interface DatabaseCoreConfig {
1011
name: DatabaseEnum.CORE;
11-
models?: CoreModelEnum[];
12+
models: CoreModelEnum[];
1213
}

0 commit comments

Comments
 (0)