Skip to content

Commit 85f512f

Browse files
author
DylanBulmer
committed
document Base and User entities
1 parent 7e745cf commit 85f512f

19 files changed

Lines changed: 123 additions & 83 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import { User } from "jsr:@codr/models@^1";
4949

5050
```bash
5151
# Clone the repo
52-
git clone git@github.com:CodrJS/models.git
52+
git clone git@github.com:CodrJS/jsr-models.git
5353

5454
# Cache deno dependencies
5555
deno cache ./mod.ts

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"singleQuote": false
1111
},
1212
"publish": {
13-
"exclude": [".github/*", "tests/*.test.ts", ".gitignore"]
13+
"exclude": [".github/*", "tests/*.test.ts", ".gitignore", "coverage/*"]
1414
},
1515
"imports": {
1616
"@std/assert": "jsr:@std/assert@^1.0.0",

deno.lock

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mod.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1+
/**
2+
* @module
3+
*
4+
* This module consists of class entities used to collect, transform, and
5+
* process data in Codr.
6+
*
7+
* @example
8+
* ```ts
9+
* import { Organization } from "@codr/models";
10+
* import { ObjectId } from "bson";
11+
*
12+
* const org = new Organization ({
13+
* name: "Demo Organization",
14+
* domains: ["localhost:3000"],
15+
* flags: {
16+
* isActive: true,
17+
* isDeleted: false,
18+
* isDemo: true,
19+
* },
20+
* slug: "demo",
21+
* createdBy: new ObjectId(),
22+
* })
23+
* ```
24+
*/
25+
126
export * from "./src/mod.ts";

src/mod.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,2 @@
1-
/**
2-
* @module
3-
*
4-
* This module consists of class entities used to collect, transform, and
5-
* process data in Codr.
6-
*
7-
* @example
8-
* ```ts
9-
* import { Organization } from "@codr/models";
10-
* import { ObjectId } from "bson";
11-
*
12-
* const org = new Organization ({
13-
* name: "Demo Organization",
14-
* domains: ["localhost:3000"],
15-
* flags: {
16-
* isActive: true,
17-
* isDeleted: false,
18-
* isDemo: true,
19-
* },
20-
* slug: "demo",
21-
* createdBy: new ObjectId(),
22-
* })
23-
* ```
24-
*/
25-
261
// export * as Types from "./types/mod.ts";
272
export * from "./models/mod.ts";

src/models/Annotation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class Annotation extends Base<"Annotation"> {
2424
value,
2525
annotatedBy,
2626
_id,
27-
__v,
27+
_version,
2828
createdAt,
2929
updatedAt,
3030
createdBy,
@@ -38,7 +38,7 @@ export class Annotation extends Base<"Annotation"> {
3838
| "sampleId"
3939
| "value"
4040
>) {
41-
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
41+
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
4242
this.projectId = projectId;
4343
this.datasetId = datasetId;
4444
this.value = value;

src/models/Authorization.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ export class Authorization extends Base<"Authorization"> {
2121
userId,
2222
roleId = [],
2323
_id,
24-
__v,
24+
_version,
2525
createdAt,
2626
updatedAt,
2727
createdBy,
2828
updatedBy,
2929
}: AtLeast<IAuthorization, "createdBy" | "userId">) {
30-
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
30+
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
3131
this.userId = userId;
3232
this.roleId = roleId;
3333
}

src/models/Base.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,60 @@
11
import { ObjectId } from "bson";
22
import type { AtLeast } from "../types/mod.ts";
33

4+
/**
5+
* Base entity parameters for all database entities.
6+
*/
47
export interface IBase<Kind extends string> {
8+
/** Typescript annotation for permissioning/authorization purposes. */
59
readonly kind: Kind;
6-
__v?: number;
10+
/** Entity version stored in the database. */
11+
_version?: number;
12+
/** Entity identifier stored in the database. */
713
_id: ObjectId;
14+
/** Entity created date. */
815
createdAt: Date;
16+
/** Entity unpdated date. */
917
updatedAt: Date;
18+
/** Entity created by {@link User} id. */
1019
createdBy: ObjectId;
20+
/** Entity updated by {@link User} id. */
1121
updatedBy: ObjectId;
1222
}
1323

1424
export class Base<K extends string> {
15-
readonly __v: IBase<K>["__v"];
25+
readonly _version: IBase<K>["_version"];
1626
readonly _id: IBase<K>["_id"];
17-
readonly createdAt: Date;
18-
readonly updatedAt: Date;
19-
readonly createdBy: ObjectId;
20-
readonly updatedBy: ObjectId;
27+
readonly createdAt: IBase<K>["createdAt"];
28+
readonly updatedAt: IBase<K>["updatedAt"];
29+
readonly createdBy: IBase<K>["createdBy"];
30+
readonly updatedBy: IBase<K>["updatedBy"];
2131

2232
constructor({
2333
createdAt,
2434
updatedAt,
2535
createdBy,
2636
updatedBy,
2737
_id,
28-
__v,
38+
_version = 0,
2939
}: AtLeast<IBase<K>, "createdBy">) {
30-
this.__v = __v;
40+
this._version = _version;
3141
this._id = _id || new ObjectId();
3242

33-
const now = new Date(Date.now());
43+
const now = new Date();
3444
this.createdAt = createdAt || now;
3545
this.updatedAt = updatedAt || now;
3646

3747
this.createdBy = createdBy;
3848
this.updatedBy = updatedBy || createdBy;
3949
}
4050

51+
/**
52+
* Transforms the base class object into a json object. Useful for saving the entity to the database.
53+
* @returns a json representation of the base entity.
54+
*/
4155
toJSON(): Omit<IBase<K>, "kind"> {
4256
return {
43-
__v: this.__v,
57+
_version: this._version,
4458
_id: this._id,
4559
createdAt: this.createdAt,
4660
updatedAt: this.updatedAt,

src/models/Dataset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class Dataset extends Base<"Dataset"> {
1717
flags,
1818
projectId,
1919
_id,
20-
__v,
20+
_version,
2121
createdAt,
2222
updatedAt,
2323
name,
@@ -26,7 +26,7 @@ export class Dataset extends Base<"Dataset"> {
2626
}: AtLeast<IDataset, "createdBy" | "name" | "flags" | "projectId">) {
2727
super({
2828
_id,
29-
__v,
29+
_version,
3030
createdAt,
3131
updatedAt,
3232
createdBy,

src/models/Group.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ export class Group<
3434
members,
3535
flags,
3636
_id,
37-
__v,
37+
_version,
3838
createdAt,
3939
updatedAt,
4040
createdBy,
4141
updatedBy,
4242
}: AtLeast<IGroup<K, F>, "createdBy" | "name" | "members" | "flags">) {
43-
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
43+
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
4444
this.name = name;
4545
this.members = members;
4646
this.flags = flags;

0 commit comments

Comments
 (0)