Skip to content

Commit dd6b3bb

Browse files
author
DylanBulmer
committed
improve model usablity
1 parent dc9c6bd commit dd6b3bb

21 files changed

Lines changed: 195 additions & 87 deletions

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codrjs/models",
3-
"version": "1.0.11",
3+
"version": "1.0.12",
44
"description": "",
55
"main": "./cjs/index.js",
66
"module": "./esm/index.js",
@@ -19,8 +19,8 @@
1919
},
2020
"scripts": {
2121
"test": "jest --config jest.config.json --passWithNoTests --coverage",
22-
"build:esm": "tsc --project tsconfig.esm.json",
23-
"build:cjs": "tsc --project tsconfig.cjs.json",
22+
"build:esm": "tsc --project tsconfig.esm.json && tsc-alias -p tsconfig.esm.json",
23+
"build:cjs": "tsc --project tsconfig.cjs.json && tsc-alias -p tsconfig.cjs.json",
2424
"build": "yarn build:cjs && yarn build:esm && ./bin/post-build.sh",
2525
"clean": "rm -rf ./dist/*",
2626
"format": "prettier --write \"src/**/*.(ts|js)\"",
@@ -47,11 +47,13 @@
4747
"jest": "^29.0.3",
4848
"mongoose": "^6.10.0",
4949
"prettier": "^2.7.1",
50+
"tsc-alias": "^1.8.6",
5051
"typescript": "^4.9.4"
5152
},
5253
"dependencies": {
5354
"@casl/ability": "^6.3.3",
5455
"@codrjs/config": "^1.0.7",
56+
"@ucast/mongo2js": "^1.3.4",
5557
"uuid": "^9.0.0"
5658
},
5759
"peerDependencies": {

src/entities/Annotation.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Types } from "mongoose";
2-
import { Base, IBaseMinimal } from "./Base";
2+
import { Base, IBase } from "./Base";
3+
import { AtLeast } from "@/types";
34

4-
export interface IAnnotation extends IBaseMinimal {
5-
readonly kind: "Annotation";
5+
export interface IAnnotation extends IBase<"Annotation"> {
66
projectId: Types.ObjectId;
77
datasetId: Types.ObjectId;
88
sampleId: Types.ObjectId;
@@ -29,7 +29,10 @@ export class Annotation extends Base {
2929
updatedAt,
3030
createdBy,
3131
updatedBy,
32-
}: IAnnotation) {
32+
}: AtLeast<
33+
IAnnotation,
34+
"createdBy" | "projectId" | "datasetId" | "annotatedBy" | "sampleId"
35+
>) {
3336
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
3437
this.projectId = projectId;
3538
this.datasetId = datasetId;

src/entities/Audit.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { Types } from "mongoose";
2-
import type { IBaseMinimal } from "./Base";
2+
import type { IBase } from "./Base";
33
import type { ActionType, EntityType } from "../types";
44

5-
export interface IAudit extends Omit<IBaseMinimal, "createdBy"> {
6-
readonly kind: "Audit";
5+
export interface IAudit extends Omit<IBase<"Audit">, "createdBy"> {
76
entityType: EntityType; // (where) what entity got modified
87
action: ActionType; // action taken
98
userId: Types.ObjectId; // who

src/entities/Base.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Types } from "mongoose";
2-
import { AtLeast } from "../types";
2+
import { AtLeast, IModelKind } from "../types";
33

4-
export interface IBase {
5-
readonly kind: string;
4+
export interface IBase<K extends string = string> extends IModelKind<K> {
5+
readonly kind: K;
66
__v?: number;
77
_id: Types.ObjectId;
88
createdAt: Date;
@@ -11,8 +11,6 @@ export interface IBase {
1111
updatedBy: Types.ObjectId;
1212
}
1313

14-
export type IBaseMinimal = AtLeast<IBase, "createdBy">;
15-
1614
export class Base {
1715
readonly __v: IBase["__v"];
1816
readonly _id: IBase["_id"];
@@ -28,7 +26,7 @@ export class Base {
2826
updatedBy,
2927
_id,
3028
__v,
31-
}: Partial<IBase> & { createdBy: Types.ObjectId }) {
29+
}: AtLeast<IBase, "createdBy">) {
3230
this.__v = __v;
3331
this._id = _id || new Types.ObjectId();
3432

src/entities/Config/BaseConfig.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Base, IBaseMinimal } from "../Base";
1+
import { AtLeast } from "@/types";
2+
import { Base, IBase } from "../Base";
23

3-
export interface IBaseConfig extends IBaseMinimal {
4-
readonly kind: "Config";
4+
export interface IBaseConfig<Kind extends string = "Config">
5+
extends IBase<Kind> {
56
verison?: string;
67
flags?: { isDeleted: boolean };
78
}
@@ -20,7 +21,7 @@ export default abstract class BaseConfig extends Base {
2021
updatedAt,
2122
createdBy,
2223
updatedBy,
23-
}: IBaseConfig) {
24+
}: AtLeast<IBaseConfig, "createdBy">) {
2425
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
2526
this.verison = verison;
2627
this.flags = flags || { isDeleted: false };

src/entities/Config/Project/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { AtLeast } from "@/types";
12
import BaseConfig, { IBaseConfig } from "../BaseConfig";
23
import { DisplayConfig } from "./types/Display";
34
import { SampleConfig } from "./types/Sample";
45

5-
export interface IProjectConfig extends Omit<IBaseConfig, "kind"> {
6+
export interface IProjectConfig extends IBaseConfig {
67
// $schema: string;
78
display: DisplayConfig;
89
sample: SampleConfig;
@@ -24,9 +25,8 @@ export default class ProjectConfig extends BaseConfig {
2425
flags,
2526
display,
2627
sample,
27-
}: IProjectConfig) {
28+
}: AtLeast<IProjectConfig, "createdBy" | "display" | "sample">) {
2829
super({
29-
kind: 'Config',
3030
verison,
3131
flags,
3232
_id,

src/entities/Dataset.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Types } from "mongoose";
22
import { Group, IGroup } from "./Group";
3+
import { AtLeast } from "@/types";
34

45
// eslint-disable-next-line @typescript-eslint/no-empty-interface
5-
export interface IDataset extends IGroup {
6-
readonly kind: "Dataset";
6+
export interface IDataset extends IGroup<"Dataset"> {
77
projectId: Types.ObjectId;
88
}
99

@@ -22,7 +22,10 @@ export class Dataset extends Group {
2222
teams,
2323
createdBy,
2424
updatedBy,
25-
}: IDataset) {
25+
}: AtLeast<
26+
IDataset,
27+
"createdBy" | "name" | "members" | "teams" | "flags" | "projectId"
28+
>) {
2629
super({
2730
_id,
2831
__v,

src/entities/Group.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { Types } from "mongoose";
2-
import { Base, IBaseMinimal } from "./Base";
2+
import { Base, IBase } from "./Base";
33
import { Flags } from "../types/Flags";
4+
import { AtLeast } from "@/types";
45

5-
export interface IGroup<F = object> extends IBaseMinimal {
6+
export interface IGroup<Kind extends string = "Group", F = object>
7+
extends IBase<Kind> {
68
createdBy: Types.ObjectId;
79
members: Types.ObjectId[];
810
name: string;
@@ -27,7 +29,7 @@ export class Group extends Base {
2729
updatedAt,
2830
createdBy,
2931
updatedBy,
30-
}: IGroup) {
32+
}: AtLeast<IGroup, "createdBy" | "name" | "members" | "teams" | "flags">) {
3133
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
3234
this.name = name;
3335
this.members = members;

src/entities/Message.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { Types } from "mongoose";
2-
import type { MessageType } from "../types";
3-
import { Base, IBaseMinimal } from "./Base";
2+
import type { AtLeast, MessageType } from "../types";
3+
import { Base, IBase } from "./Base";
44

5-
export interface IMessage extends IBaseMinimal {
6-
readonly kind: "Message";
5+
export interface IMessage extends IBase<"Message"> {
76
type: MessageType;
87
subject: string;
98
body: string;
@@ -27,7 +26,7 @@ export class Message extends Base {
2726
updatedAt,
2827
createdBy,
2928
updatedBy,
30-
}: IMessage) {
29+
}: AtLeast<IMessage, "createdBy" | "body" | "subject" | "to" | "type">) {
3130
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
3231
this.body = body;
3332
this.subject = subject;

src/entities/Profile.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Types } from "mongoose";
2-
import { Base, IBaseMinimal } from "./Base";
2+
import { Base, IBase } from "./Base";
33
import { User } from "./User";
4+
import { AtLeast } from "@/types";
45

5-
export interface IProfile extends IBaseMinimal {
6-
readonly kind: "Profile";
6+
export interface IProfile extends IBase<"Profile"> {
77
name: {
88
first: string;
99
last: string;
@@ -40,7 +40,10 @@ export class Profile extends Base {
4040
updatedAt,
4141
createdBy,
4242
updatedBy,
43-
}: IProfile & { user?: User }) {
43+
}: AtLeast<
44+
IProfile & { user: User },
45+
"createdBy" | "name" | "userId" | "username"
46+
>) {
4447
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
4548
this.name = name;
4649
this.avatarUrl = avatarUrl;

0 commit comments

Comments
 (0)