Skip to content

Commit

Permalink
document Base and User entities
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanBulmer committed Jul 21, 2024
1 parent 7e745cf commit 85f512f
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { User } from "jsr:@codr/models@^1";

```bash
# Clone the repo
git clone [email protected]:CodrJS/models.git
git clone [email protected]:CodrJS/jsr-models.git

# Cache deno dependencies
deno cache ./mod.ts
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"singleQuote": false
},
"publish": {
"exclude": [".github/*", "tests/*.test.ts", ".gitignore"]
"exclude": [".github/*", "tests/*.test.ts", ".gitignore", "coverage/*"]
},
"imports": {
"@std/assert": "jsr:@std/assert@^1.0.0",
Expand Down
15 changes: 11 additions & 4 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
/**
* @module
*
* This module consists of class entities used to collect, transform, and
* process data in Codr.
*
* @example
* ```ts
* import { Organization } from "@codr/models";
* import { ObjectId } from "bson";
*
* const org = new Organization ({
* name: "Demo Organization",
* domains: ["localhost:3000"],
* flags: {
* isActive: true,
* isDeleted: false,
* isDemo: true,
* },
* slug: "demo",
* createdBy: new ObjectId(),
* })
* ```
*/

export * from "./src/mod.ts";
25 changes: 0 additions & 25 deletions src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,2 @@
/**
* @module
*
* This module consists of class entities used to collect, transform, and
* process data in Codr.
*
* @example
* ```ts
* import { Organization } from "@codr/models";
* import { ObjectId } from "bson";
*
* const org = new Organization ({
* name: "Demo Organization",
* domains: ["localhost:3000"],
* flags: {
* isActive: true,
* isDeleted: false,
* isDemo: true,
* },
* slug: "demo",
* createdBy: new ObjectId(),
* })
* ```
*/

// export * as Types from "./types/mod.ts";
export * from "./models/mod.ts";
4 changes: 2 additions & 2 deletions src/models/Annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class Annotation extends Base<"Annotation"> {
value,
annotatedBy,
_id,
__v,
_version,
createdAt,
updatedAt,
createdBy,
Expand All @@ -38,7 +38,7 @@ export class Annotation extends Base<"Annotation"> {
| "sampleId"
| "value"
>) {
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
this.projectId = projectId;
this.datasetId = datasetId;
this.value = value;
Expand Down
4 changes: 2 additions & 2 deletions src/models/Authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export class Authorization extends Base<"Authorization"> {
userId,
roleId = [],
_id,
__v,
_version,
createdAt,
updatedAt,
createdBy,
updatedBy,
}: AtLeast<IAuthorization, "createdBy" | "userId">) {
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
this.userId = userId;
this.roleId = roleId;
}
Expand Down
34 changes: 24 additions & 10 deletions src/models/Base.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,60 @@
import { ObjectId } from "bson";
import type { AtLeast } from "../types/mod.ts";

/**
* Base entity parameters for all database entities.
*/
export interface IBase<Kind extends string> {
/** Typescript annotation for permissioning/authorization purposes. */
readonly kind: Kind;
__v?: number;
/** Entity version stored in the database. */
_version?: number;
/** Entity identifier stored in the database. */
_id: ObjectId;
/** Entity created date. */
createdAt: Date;
/** Entity unpdated date. */
updatedAt: Date;
/** Entity created by {@link User} id. */
createdBy: ObjectId;
/** Entity updated by {@link User} id. */
updatedBy: ObjectId;
}

export class Base<K extends string> {
readonly __v: IBase<K>["__v"];
readonly _version: IBase<K>["_version"];
readonly _id: IBase<K>["_id"];
readonly createdAt: Date;
readonly updatedAt: Date;
readonly createdBy: ObjectId;
readonly updatedBy: ObjectId;
readonly createdAt: IBase<K>["createdAt"];
readonly updatedAt: IBase<K>["updatedAt"];
readonly createdBy: IBase<K>["createdBy"];
readonly updatedBy: IBase<K>["updatedBy"];

constructor({
createdAt,
updatedAt,
createdBy,
updatedBy,
_id,
__v,
_version = 0,
}: AtLeast<IBase<K>, "createdBy">) {
this.__v = __v;
this._version = _version;
this._id = _id || new ObjectId();

const now = new Date(Date.now());
const now = new Date();
this.createdAt = createdAt || now;
this.updatedAt = updatedAt || now;

this.createdBy = createdBy;
this.updatedBy = updatedBy || createdBy;
}

/**
* Transforms the base class object into a json object. Useful for saving the entity to the database.
* @returns a json representation of the base entity.
*/
toJSON(): Omit<IBase<K>, "kind"> {
return {
__v: this.__v,
_version: this._version,
_id: this._id,
createdAt: this.createdAt,
updatedAt: this.updatedAt,
Expand Down
4 changes: 2 additions & 2 deletions src/models/Dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Dataset extends Base<"Dataset"> {
flags,
projectId,
_id,
__v,
_version,
createdAt,
updatedAt,
name,
Expand All @@ -26,7 +26,7 @@ export class Dataset extends Base<"Dataset"> {
}: AtLeast<IDataset, "createdBy" | "name" | "flags" | "projectId">) {
super({
_id,
__v,
_version,
createdAt,
updatedAt,
createdBy,
Expand Down
4 changes: 2 additions & 2 deletions src/models/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ export class Group<
members,
flags,
_id,
__v,
_version,
createdAt,
updatedAt,
createdBy,
updatedBy,
}: AtLeast<IGroup<K, F>, "createdBy" | "name" | "members" | "flags">) {
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
this.name = name;
this.members = members;
this.flags = flags;
Expand Down
4 changes: 2 additions & 2 deletions src/models/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export class Message extends Base<"Message"> {
body,
to,
_id,
__v,
_version,
createdAt,
updatedAt,
createdBy,
updatedBy,
}: AtLeast<IMessage, "createdBy" | "body" | "subject" | "to" | "type">) {
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
this.body = body;
this.subject = subject;
this.to = to;
Expand Down
4 changes: 2 additions & 2 deletions src/models/Organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Organization extends Base<"Organization"> {
isDemo: false,
},
_id,
__v,
_version,
createdAt,
updatedAt,
createdBy,
Expand All @@ -58,7 +58,7 @@ export class Organization extends Base<"Organization"> {
}: AtLeast<IOrganization, "createdBy" | "domains" | "name" | "slug">) {
super({
_id,
__v,
_version,
createdAt,
updatedAt,
createdBy,
Expand Down
4 changes: 2 additions & 2 deletions src/models/Profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Profile extends Base<"Profile"> {
userId,
phone,
_id,
__v,
_version,
user,
createdAt,
updatedAt,
Expand All @@ -44,7 +44,7 @@ export class Profile extends Base<"Profile"> {
IProfile & { user: User },
"createdBy" | "name" | "userId" | "username"
>) {
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
this.name = name;
this.avatarUrl = avatarUrl;
this.userId = userId;
Expand Down
4 changes: 2 additions & 2 deletions src/models/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Project extends Base<"Project"> {
name,
type,
_id,
__v,
_version,
createdAt,
updatedAt,
createdBy,
Expand All @@ -43,7 +43,7 @@ export class Project extends Base<"Project"> {
IProject,
"createdBy" | "bgColorClass" | "config" | "flags" | "slug" | "name" | "type"
>) {
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });

this.bgColorClass = bgColorClass;
this.config = config;
Expand Down
4 changes: 2 additions & 2 deletions src/models/Role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Role extends Base<"Role"> {
description,
grants,
_id,
__v,
_version,
createdAt,
updatedAt,
createdBy,
Expand All @@ -42,7 +42,7 @@ export class Role extends Base<"Role"> {
IRole & { user: User },
"createdBy" | "name" | "code" | "description" | "grants"
>) {
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
this.name = name;
this.code = code;
this.description = description;
Expand Down
4 changes: 2 additions & 2 deletions src/models/Sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export class Sample extends Base<"Smaple"> {
datasetId,
payload,
_id,
__v,
_version,
createdAt,
updatedAt,
createdBy,
updatedBy,
}: AtLeast<ISample, "createdBy" | "datasetId" | "projectId" | "payload">) {
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
this.projectId = projectId;
this.datasetId = datasetId;
this.payload = payload;
Expand Down
Loading

0 comments on commit 85f512f

Please sign in to comment.