Skip to content

Commit 861bb98

Browse files
author
DylanBulmer
committed
Document User, Profile, Role, and Permission; update naming convention from IEntity to EntityParamaters; update JWT token type
1 parent e236740 commit 861bb98

19 files changed

Lines changed: 317 additions & 183 deletions

deno.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "@codr/models",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"exports": "./mod.ts",
55
"fmt": {
66
"lineWidth": 80,
77
"indentWidth": 2,
88
"useTabs": false,
99
"semiColons": true,
10-
"singleQuote": false
10+
"singleQuote": false,
11+
"proseWrap": "always"
1112
},
1213
"publish": {
1314
"exclude": [".github/*", "tests/*.test.ts", ".gitignore", "coverage/*"]

src/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
// export * as Types from "./types/mod.ts";
22
export * from "./models/mod.ts";
3+
export type { JwtPayload } from "./types/mod.ts";

src/models/Annotation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ObjectId } from "bson";
2-
import { Base, type IBase } from "./Base.ts";
2+
import { Base, type BaseParameters } from "./Base.ts";
33
import type { AtLeast } from "../types/mod.ts";
44

5-
export interface IAnnotation extends IBase<"Annotation"> {
5+
export interface AnnotationParameters extends BaseParameters<"Annotation"> {
66
projectId: ObjectId;
77
datasetId: ObjectId;
88
sampleId: ObjectId;
@@ -30,7 +30,7 @@ export class Annotation extends Base<"Annotation"> {
3030
createdBy,
3131
updatedBy,
3232
}: AtLeast<
33-
IAnnotation,
33+
AnnotationParameters,
3434
| "createdBy"
3535
| "projectId"
3636
| "datasetId"
@@ -46,7 +46,7 @@ export class Annotation extends Base<"Annotation"> {
4646
this.annotatedBy = annotatedBy;
4747
}
4848

49-
toJSON(): Omit<IAnnotation, "kind"> {
49+
toJSON(): Omit<AnnotationParameters, "kind"> {
5050
const json = super.toJSON();
5151
return {
5252
...json,

src/models/Audit.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { ObjectId } from "bson";
2-
import type { IBase } from "./Base.ts";
2+
import type { BaseParameters } from "./Base.ts";
33
import type { ActionCode, ResourceCode } from "../types/mod.ts";
44

5-
export interface IAudit<T> extends Omit<IBase<"Audit">, "createdBy"> {
5+
export interface AuditParameters<T>
6+
extends Omit<BaseParameters<"Audit">, "createdBy"> {
67
type: ResourceCode; // Where: what system processed this event.
78
action: ActionCode; // How: what action was taken.
89
userId: ObjectId; // Who: which user made the change.

src/models/Authorization.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/models/Base.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { AtLeast } from "../types/mod.ts";
44
/**
55
* Base entity parameters for all database entities.
66
*/
7-
export interface IBase<Kind extends string> {
7+
export interface BaseParameters<Kind extends string> {
88
/** Typescript annotation for permissioning/authorization purposes. */
99
readonly kind: Kind;
1010
/** Entity version stored in the database. */
@@ -22,12 +22,12 @@ export interface IBase<Kind extends string> {
2222
}
2323

2424
export class Base<K extends string> {
25-
readonly _version: IBase<K>["_version"];
26-
readonly _id: IBase<K>["_id"];
27-
readonly createdAt: IBase<K>["createdAt"];
28-
readonly updatedAt: IBase<K>["updatedAt"];
29-
readonly createdBy: IBase<K>["createdBy"];
30-
readonly updatedBy: IBase<K>["updatedBy"];
25+
readonly _version: BaseParameters<K>["_version"];
26+
readonly _id: BaseParameters<K>["_id"];
27+
readonly createdAt: BaseParameters<K>["createdAt"];
28+
readonly updatedAt: BaseParameters<K>["updatedAt"];
29+
readonly createdBy: BaseParameters<K>["createdBy"];
30+
readonly updatedBy: BaseParameters<K>["updatedBy"];
3131

3232
constructor({
3333
createdAt,
@@ -36,7 +36,7 @@ export class Base<K extends string> {
3636
updatedBy,
3737
_id,
3838
_version = 0,
39-
}: AtLeast<IBase<K>, "createdBy">) {
39+
}: AtLeast<BaseParameters<K>, "createdBy">) {
4040
this._version = _version;
4141
this._id = _id || new ObjectId();
4242

@@ -52,7 +52,7 @@ export class Base<K extends string> {
5252
* Transforms the base class object into a json object. Useful for saving the entity to the database.
5353
* @returns a json representation of the base entity.
5454
*/
55-
toJSON(): Omit<IBase<K>, "kind"> {
55+
toJSON(): Omit<BaseParameters<K>, "kind"> {
5656
return {
5757
_version: this._version,
5858
_id: this._id,

src/models/Dataset.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ObjectId } from "bson";
22
import type { AtLeast, Flags } from "../types/mod.ts";
3-
import { Base, type IBase } from "./Base.ts";
3+
import { Base, type BaseParameters } from "./Base.ts";
44

5-
export interface IDataset extends IBase<"Dataset"> {
5+
export interface DatasetParameters extends BaseParameters<"Dataset"> {
66
projectId: ObjectId;
77
flags: Flags;
88
name: string;
@@ -23,7 +23,7 @@ export class Dataset extends Base<"Dataset"> {
2323
name,
2424
createdBy,
2525
updatedBy,
26-
}: AtLeast<IDataset, "createdBy" | "name" | "flags" | "projectId">) {
26+
}: AtLeast<DatasetParameters, "createdBy" | "name" | "flags" | "projectId">) {
2727
super({
2828
_id,
2929
_version,
@@ -37,7 +37,7 @@ export class Dataset extends Base<"Dataset"> {
3737
this.name = name;
3838
}
3939

40-
toJSON(): Omit<IDataset, "kind"> {
40+
toJSON(): Omit<DatasetParameters, "kind"> {
4141
const json = super.toJSON();
4242
return {
4343
...json,

src/models/Group.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
11
import type { ObjectId } from "bson";
2-
import { Base, type IBase } from "./Base.ts";
2+
import { Base, type BaseParameters } from "./Base.ts";
33
import type { AtLeast, Flags } from "../types/mod.ts";
44

5-
export interface IGroup<
5+
/**
6+
* Parameters for creating a {@link Group} entity.
7+
*/
8+
export interface GroupParameters<
69
Kind extends string = "Group",
710
F extends object = Flags,
8-
> extends IBase<Kind> {
9-
members: IGroupMember[];
11+
> extends BaseParameters<Kind> {
1012
name: string;
13+
members: GroupMemberParameters[];
1114
flags: F;
1215
}
1316

14-
export interface IGroupMember {
15-
type: GroupMemberEnum;
17+
/**
18+
* Parameters for adding Group members to the {@link GroupParameters}.
19+
*/
20+
export interface GroupMemberParameters {
21+
type: GroupMemberType;
1622
_id: ObjectId;
1723
}
1824

19-
export enum GroupMemberEnum {
20-
"USER" = "USER",
21-
"TEAM" = "TEAM",
25+
/**
26+
* Type codes for the {@link GroupMemberParameters} object.
27+
*/
28+
export enum GroupMemberType {
29+
"User" = "USER",
30+
"Group" = "GROUP",
2231
}
2332

2433
export class Group<
2534
K extends string = "Group",
2635
F extends object = Flags,
2736
> extends Base<K> {
28-
members: IGroupMember[];
37+
members: GroupMemberParameters[];
2938
name: string;
3039
flags: F;
3140

@@ -39,14 +48,35 @@ export class Group<
3948
updatedAt,
4049
createdBy,
4150
updatedBy,
42-
}: AtLeast<IGroup<K, F>, "createdBy" | "name" | "members" | "flags">) {
51+
}: AtLeast<
52+
GroupParameters<K, F>,
53+
"createdBy" | "name" | "members" | "flags"
54+
>) {
4355
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
4456
this.name = name;
4557
this.members = members;
4658
this.flags = flags;
4759
}
4860

49-
toJSON(): Omit<IGroup<K, F>, "kind"> {
61+
addUser(id: ObjectId): GroupMemberParameters {
62+
const member = {
63+
type: GroupMemberType.User,
64+
_id: id,
65+
};
66+
this.members.push(member);
67+
return member;
68+
}
69+
70+
addGroup(id: ObjectId): GroupMemberParameters {
71+
const member = {
72+
type: GroupMemberType.Group,
73+
_id: id,
74+
};
75+
this.members.push(member);
76+
return member;
77+
}
78+
79+
toJSON(): Omit<GroupParameters<K, F>, "kind"> {
5080
const json = super.toJSON();
5181
return {
5282
...json,

src/models/Message.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ObjectId } from "bson";
22
import type { AtLeast, MessageType } from "../types/mod.ts";
3-
import { Base, type IBase } from "./Base.ts";
3+
import { Base, type BaseParameters } from "./Base.ts";
44

5-
export interface IMessage extends IBase<"Message"> {
5+
export interface MessageParameters extends BaseParameters<"Message"> {
66
type: MessageType;
77
subject: string;
88
body: string;
@@ -26,15 +26,18 @@ export class Message extends Base<"Message"> {
2626
updatedAt,
2727
createdBy,
2828
updatedBy,
29-
}: AtLeast<IMessage, "createdBy" | "body" | "subject" | "to" | "type">) {
29+
}: AtLeast<
30+
MessageParameters,
31+
"createdBy" | "body" | "subject" | "to" | "type"
32+
>) {
3033
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
3134
this.body = body;
3235
this.subject = subject;
3336
this.to = to;
3437
this.type = type;
3538
}
3639

37-
toJSON(): Omit<IMessage, "kind"> {
40+
toJSON(): Omit<MessageParameters, "kind"> {
3841
const json = super.toJSON();
3942
return {
4043
...json,

src/models/Organization.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
import type { AtLeast } from "../types/mod.ts";
2-
import { Base, type IBase } from "./Base.ts";
3-
4-
/**
5-
* Flags used in the {@link IOrganization} interface.
6-
*/
7-
interface OrganizationFlags {
8-
/** Whether or not the organization is active */
9-
isActive: boolean;
10-
/** Whether or not the organization is soft deleted */
11-
isDeleted: boolean;
12-
/** Whether or not the organization is a demo organization */
13-
isDemo: boolean;
14-
}
2+
import { Base, type BaseParameters } from "./Base.ts";
153

164
/**
175
* Parameters for building an {@link Organization} entity.
186
*/
19-
export interface IOrganization extends IBase<"Organization"> {
7+
export interface OrganizationParameters extends BaseParameters<"Organization"> {
208
/** A list of domains (and ports) linked to the Organziation */
219
domains: string[];
2210
/** Flags options for the organization */
23-
flags: OrganizationFlags;
11+
flags: {
12+
/** Whether or not the organization is active */
13+
isActive: boolean;
14+
/** Whether or not the organization is soft deleted */
15+
isDeleted: boolean;
16+
/** Whether or not the organization is a demo organization */
17+
isDemo: boolean;
18+
};
2419
/** Name of the organization */
2520
name: string;
2621
/** Slug for generating subdomains for the organization */
@@ -31,14 +26,14 @@ export interface IOrganization extends IBase<"Organization"> {
3126
* A class the represents an organization.
3227
*/
3328
export class Organization extends Base<"Organization"> {
34-
readonly domains: string[];
35-
readonly flags: OrganizationFlags;
36-
readonly name: string;
37-
readonly slug: string;
29+
readonly domains: OrganizationParameters["domains"];
30+
readonly flags: OrganizationParameters["flags"];
31+
readonly name: OrganizationParameters["name"];
32+
readonly slug: OrganizationParameters["slug"];
3833

3934
/**
4035
* Create an Organization entity.
41-
* @param params An object of required and optional parameters referenced from the {@link IOrganization} interface.
36+
* @param params An object of required and optional parameters referenced from the {@link OrganizationParameters} interface.
4237
*/
4338
constructor({
4439
flags = {
@@ -55,7 +50,10 @@ export class Organization extends Base<"Organization"> {
5550
name,
5651
slug,
5752
domains,
58-
}: AtLeast<IOrganization, "createdBy" | "domains" | "name" | "slug">) {
53+
}: AtLeast<
54+
OrganizationParameters,
55+
"createdBy" | "domains" | "name" | "slug"
56+
>) {
5957
super({
6058
_id,
6159
_version,
@@ -74,7 +72,7 @@ export class Organization extends Base<"Organization"> {
7472
* Transforms the organization class object to a json object. Useful for saving the entity to the database.
7573
* @returns a json representation of the organization.
7674
*/
77-
toJSON(): Omit<IOrganization, "kind"> {
75+
toJSON(): Omit<OrganizationParameters, "kind"> {
7876
const json = super.toJSON();
7977
return {
8078
...json,

0 commit comments

Comments
 (0)