Skip to content

Commit

Permalink
add more exports; implement ts magic; code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanBulmer committed Jun 11, 2023
1 parent 1bb9e9f commit cdb5ac1
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 289 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codrjs/mongo",
"version": "1.0.1",
"version": "1.0.2",
"description": "",
"main": "./cjs/index.js",
"module": "./esm/index.js",
Expand All @@ -21,7 +21,7 @@
"test": "jest --config jest.config.json --passWithNoTests --coverage",
"build:esm": "tsc --project tsconfig.esm.json && tsc-alias -p tsconfig.esm.json",
"build:cjs": "tsc --project tsconfig.cjs.json && tsc-alias -p tsconfig.cjs.json",
"build": "yarn clean && yarn build:cjs && yarn build:esm && ./bin/post-build.sh",
"build": "yarn build:cjs && yarn build:esm && ./bin/post-build.sh",
"clean": "rm -rf ./dist",
"format": "prettier --write \"src/**/*.(ts|js)\"",
"lint": "eslint -c .eslintrc.json --ignore-path .eslintignore --ext .ts src",
Expand Down
4 changes: 4 additions & 0 deletions src/abilities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { default as ProfileAbility } from "./Profile.ability";
export { default as SessionAbility } from "./Session.ability";
export { default as UserAbility } from "./User.ability";
export { default as UserGroupAbility } from "./UserGroup.ability";
4 changes: 4 additions & 0 deletions src/schemas/Documents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { ProfileDocument } from "./Profile";
export { SessionDocument } from "./Session";
export { UserDocument } from "./User";
export { UserGroupDocument } from "./UserGroup";
76 changes: 40 additions & 36 deletions src/schemas/Profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,49 @@ import { UserDocument } from "./User";

export type ProfileDocument = IProfile & AccessibleFieldsModel<IProfile>;

export function createProfileModel(userModel: AccessibleModel<UserDocument>) {
const ProfileSchema = new Schema<IProfile>(
{
avatarUrl: String,
userId: {
type: SchemaTypes.ObjectId,
required: true,
unique: true,
index: true,
ref: "User",
},
username: {
type: String,
required: true,
unique: true,
index: true,
},
name: {
type: {
first: String,
last: String,
preferred: String,
export function createProfileModel(userModel?: AccessibleModel<UserDocument>) {
if (userModel) {
const ProfileSchema = new Schema<IProfile>(
{
avatarUrl: String,
userId: {
type: SchemaTypes.ObjectId,
required: true,
unique: true,
index: true,
ref: "User",
},
username: {
type: String,
required: true,
unique: true,
index: true,
},
required: true,
name: {
type: {
first: String,
last: String,
preferred: String,
},
required: true,
},
},
{
timestamps: true,
},
},
{
timestamps: true,
},
);
);

ProfileSchema.virtual("user", {
ref: userModel,
localField: "userId",
foreignField: "_id",
});
ProfileSchema.virtual("user", {
ref: userModel,
localField: "userId",
foreignField: "_id",
});

ProfileSchema.plugin(accessibleFieldsPlugin);
ProfileSchema.plugin(accessibleRecordsPlugin);
ProfileSchema.plugin(accessibleFieldsPlugin);
ProfileSchema.plugin(accessibleRecordsPlugin);

return ProfileSchema;
return ProfileSchema;
} else {
throw "User model is not defined.";
}
}
70 changes: 37 additions & 33 deletions src/schemas/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,46 @@ import { UserDocument } from "./User";

export type SessionDocument = ISession & AccessibleFieldsModel<ISession>;

export function createSessionModel(userModel: AccessibleModel<UserDocument>) {
const SessionSchema = new Schema<ISession>(
{
status: {
type: String,
enum: ["INITIATING", "ESTABLISHED", "CLOSED"],
required: true,
default: "INITIATING",
export function createSessionModel(userModel?: AccessibleModel<UserDocument>) {
if (userModel) {
const SessionSchema = new Schema<ISession>(
{
status: {
type: String,
enum: ["INITIATING", "ESTABLISHED", "CLOSED"],
required: true,
default: "INITIATING",
},
userId: {
type: SchemaTypes.ObjectId,
required: true,
unique: false,
index: true,
ref: "User",
},
os: { type: String },
browser: { type: String },
ipAddress: { type: String },
createdAt: String,
updatedAt: String,
},
userId: {
type: SchemaTypes.ObjectId,
required: true,
unique: false,
index: true,
ref: "User",
{
timestamps: true,
},
os: { type: String },
browser: { type: String },
ipAddress: { type: String },
createdAt: String,
updatedAt: String,
},
{
timestamps: true,
},
);
);

SessionSchema.virtual("user", {
ref: userModel,
localField: "userId",
foreignField: "_id",
});
SessionSchema.virtual("user", {
ref: userModel,
localField: "userId",
foreignField: "_id",
});

// exports Session model.
SessionSchema.plugin(accessibleFieldsPlugin);
SessionSchema.plugin(accessibleRecordsPlugin);
// exports Session model.
SessionSchema.plugin(accessibleFieldsPlugin);
SessionSchema.plugin(accessibleRecordsPlugin);

return SessionSchema;
return SessionSchema;
} else {
throw "User model is not defined.";
}
}
106 changes: 55 additions & 51 deletions src/schemas/UserGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,65 +10,69 @@ import { UserDocument } from "./User";

export type UserGroupDocument = IUserGroup & AccessibleFieldsModel<IUserGroup>;

export function createUserGroupModel(userModel: AccessibleModel<UserDocument>) {
const UserGroupSchema = new Schema<UserGroupDocument>(
{
createdBy: {
required: true,
index: true,
type: SchemaTypes.ObjectId,
ref: "User",
},
members: {
items: {
export function createUserGroupModel(userModel?: AccessibleModel<UserDocument>) {
if (userModel) {
const UserGroupSchema = new Schema<UserGroupDocument>(
{
createdBy: {
required: true,
index: true,
type: SchemaTypes.ObjectId,
ref: "User",
},
},
teams: {
items: {
type: SchemaTypes.ObjectId,
ref: "UserGroup",
members: {
items: {
type: SchemaTypes.ObjectId,
ref: "User",
},
},
},
name: {
type: "String",
required: true,
index: true,
default: "Unnamed Group",
},
flags: {
type: {
isAnonymous: Boolean,
isDeleted: Boolean,
isJoinable: Boolean,
isPrivate: Boolean,
teams: {
items: {
type: SchemaTypes.ObjectId,
ref: "UserGroup",
},
},
name: {
type: "String",
required: true,
index: true,
default: "Unnamed Group",
},
required: true,
default: {
isAnonymous: false,
isDeleted: false,
isJoinable: false,
isPrivate: false,
flags: {
type: {
isAnonymous: Boolean,
isDeleted: Boolean,
isJoinable: Boolean,
isPrivate: Boolean,
},
required: true,
default: {
isAnonymous: false,
isDeleted: false,
isJoinable: false,
isPrivate: false,
},
},
createdAt: { type: String },
updatedAt: { type: String },
},
{
timestamps: true,
},
createdAt: { type: String },
updatedAt: { type: String },
},
{
timestamps: true,
},
);
);

UserGroupSchema.virtual("user", {
ref: userModel,
localField: "userId",
foreignField: "_id",
});
UserGroupSchema.virtual("user", {
ref: userModel,
localField: "userId",
foreignField: "_id",
});

// exports UserGroup model.
UserGroupSchema.plugin(accessibleFieldsPlugin);
UserGroupSchema.plugin(accessibleRecordsPlugin);
// exports UserGroup model.
UserGroupSchema.plugin(accessibleFieldsPlugin);
UserGroupSchema.plugin(accessibleRecordsPlugin);

return UserGroupSchema;
return UserGroupSchema;
} else {
throw "User model is not defined.";
}
}
1 change: 1 addition & 0 deletions src/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as Documents from "./Documents";
24 changes: 23 additions & 1 deletion src/types/UserDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { AccessibleModel } from "@casl/mongoose";
import { DatabaseEnum } from "./Database";
import {
ProfileDocument,
SessionDocument,
UserDocument,
UserGroupDocument,
} from "@/schemas/Documents";

export type UserModelType = "PROFILE" | "SESSION" | "USERGROUP" | "USER";
export enum UserModelEnum {
Expand All @@ -10,5 +17,20 @@ export enum UserModelEnum {

export interface DatabaseUserConfig {
name: DatabaseEnum.USER;
models?: UserModelEnum[];
models: UserModelEnum[];
}

export interface ILoadedUserModels<T extends UserModelEnum | undefined> {
[UserModelEnum.PROFILE]: T extends UserModelEnum.PROFILE
? AccessibleModel<ProfileDocument>
: undefined;
[UserModelEnum.SESSION]: T extends UserModelEnum.SESSION
? AccessibleModel<SessionDocument>
: undefined;
[UserModelEnum.USERGROUP]: T extends UserModelEnum.USERGROUP
? AccessibleModel<UserGroupDocument>
: undefined;
[UserModelEnum.USER]: T extends UserModelEnum.USER
? AccessibleModel<UserDocument>
: undefined;
}
Loading

0 comments on commit cdb5ac1

Please sign in to comment.