Skip to content

Commit

Permalink
document project domain entities
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanBulmer committed Jul 26, 2024
1 parent 07ab24f commit 65fbf2f
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 92 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codr/models",
"version": "0.1.6",
"version": "0.1.7",
"exports": "./mod.ts",
"fmt": {
"lineWidth": 80,
Expand Down
2 changes: 1 addition & 1 deletion src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// export * as Types from "./types/mod.ts";
export * from "./models/mod.ts";
export { type JwtPayload, UserTypeCode } from "./types/mod.ts";
export { type JwtPayload } from "./types/mod.ts";
33 changes: 19 additions & 14 deletions src/models/Annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@ import type { ObjectId } from "bson";
import { Base, type BaseParameters } from "./Base.ts";
import type { AtLeast } from "../types/mod.ts";

/**
* Parameters for creating an {@link Annotation} entity.
*/
export interface AnnotationParameters extends BaseParameters<"Annotation"> {
projectId: ObjectId;
/** The dataset this annotation comes from. */
datasetId: ObjectId;
/** The sample this annotation is for. */
sampleId: ObjectId;
/** What user annotated the annotation. */
annotatedBy: ObjectId;
/** The annotated value/output */
value: object;
}

/**
* Annotation entity class for representing a dataset sample annotation.
*/
export class Annotation extends Base<"Annotation"> {
projectId: ObjectId;
datasetId: ObjectId;
sampleId: ObjectId;
annotatedBy: ObjectId;
value: object;

constructor({
projectId,
datasetId,
sampleId,
value,
Expand All @@ -31,29 +38,27 @@ export class Annotation extends Base<"Annotation"> {
updatedBy,
}: AtLeast<
AnnotationParameters,
| "createdBy"
| "projectId"
| "datasetId"
| "annotatedBy"
| "sampleId"
| "value"
"createdBy" | "datasetId" | "annotatedBy" | "sampleId" | "value"
>) {
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
this.projectId = projectId;
this.datasetId = datasetId;
this.value = value;
this.sampleId = sampleId;
this.annotatedBy = annotatedBy;
this.value = value;
}

/**
* Transforms the {@link Annotation} class object into a {@link AnnotationParameters}-like
* json object. Useful for saving the entity to the database.
* @returns a json representation of the annotation entity.
*/
toJSON(): Omit<AnnotationParameters, "kind"> {
const json = super.toJSON();
return {
...json,
projectId: this.projectId,
annotatedBy: this.annotatedBy,
sampleId: this.sampleId,
datasetId: this.datasetId,
sampleId: this.sampleId,
annotatedBy: this.annotatedBy,
value: this.value,
};
}
Expand Down
59 changes: 48 additions & 11 deletions src/models/Dataset.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,57 @@
import type { ObjectId } from "bson";
import type { AtLeast, Flags } from "../types/mod.ts";
import type { AtLeast } from "../types/mod.ts";
import { Base, type BaseParameters } from "./Base.ts";

/**
* Parameters for creating a {@link Dataset} entity.
*/
export interface DatasetParameters extends BaseParameters<"Dataset"> {
/** The project id that this dataset is for. */
projectId: ObjectId;
flags: Flags;
name: string;
/** The task id(s) that this dataset is for. */
taskId: ObjectId[];
/**
* The sub-dataset id(s) that for this dataset. This is for dividing out
* samples for annotation.
*/
subDatasetId: ObjectId[];
/** The title/name of the dataset */
title: string;
/** The dataset flags. */
flags: {
/** Is the dataset active/archived? */
isActive: boolean;
/** Is the dateset soft deleted? */
isDeleted: boolean;
};
}

/**
* Dataset entity class for representing a set of samples for a project and task.
*/
export class Dataset extends Base<"Dataset"> {
projectId: ObjectId;
flags: Flags;
name: string;
projectId: DatasetParameters["projectId"];
taskId: DatasetParameters["taskId"];
subDatasetId: DatasetParameters["subDatasetId"];
title: DatasetParameters["title"];
flags: DatasetParameters["flags"];

constructor({
flags,
projectId,
taskId,
subDatasetId,
title,
flags,
_id,
_version,
createdAt,
updatedAt,
name,
createdBy,
updatedBy,
}: AtLeast<DatasetParameters, "createdBy" | "name" | "flags" | "projectId">) {
}: AtLeast<
DatasetParameters,
"createdBy" | "title" | "flags" | "projectId" | "taskId" | "subDatasetId"
>) {
super({
_id,
_version,
Expand All @@ -33,16 +61,25 @@ export class Dataset extends Base<"Dataset"> {
updatedBy,
});
this.projectId = projectId;
this.taskId = taskId;
this.subDatasetId = subDatasetId;
this.title = title;
this.flags = flags;
this.name = name;
}

/**
* Transforms the {@link Dataset} class object into a {@link DatasetParameters}-like
* json object. Useful for saving the entity to the database.
* @returns a json representation of the dataset entity.
*/
toJSON(): Omit<DatasetParameters, "kind"> {
const json = super.toJSON();
return {
...json,
projectId: this.projectId,
name: this.name,
taskId: this.taskId,
subDatasetId: this.subDatasetId,
title: this.title,
flags: this.flags,
};
}
Expand Down
1 change: 1 addition & 0 deletions src/models/Permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { AtLeast } from "../types/mod.ts";
* Parameters for creating a {@link Permission} entity.
*/
export interface PermissionParameters extends BaseParameters<"Permission"> {
/** The title/name of the role. */
title: string;
/**
* Permission code.
Expand Down
65 changes: 36 additions & 29 deletions src/models/Project.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
import type { ObjectId } from "bson";
import type { AtLeast, Flags, TaskType } from "../types/mod.ts";
import type { AtLeast } from "../types/mod.ts";
import { Base, type BaseParameters } from "./Base.ts";

/**
* Parameters for creating a {@link Project} entity.
*/
export interface ProjectParameters extends BaseParameters<"Project"> {
name: string;
type: TaskType;
/** The organization id the project belongs to. */
readonly organizationId: ObjectId;
/** The title of the project. */
title: string;
/** The URL slug of the project (unique to the organization) */
slug: string;
guidelines?: string;
/** The color of the project shown in the UI. */
bgColorClass: string;
config: ObjectId;
flags: Flags & {
isAnonymized: boolean;
/** Project flags. */
flags: {
/** Is the project active/archived? */
isActive: boolean;
/** Is the project soft deleted? */
isDeleted: boolean;
};
}

/**
* Poject entity class for representing a reseach project.
*/
export class Project extends Base<"Project"> {
name: string;
slug: string;
type: TaskType;
guidelines?: string;
bgColorClass: string;
config: ObjectId;
flags: Flags & {
isAnonymized: boolean;
};
readonly organizationId: ProjectParameters["organizationId"];
title: ProjectParameters["title"];
slug: ProjectParameters["slug"];
bgColorClass: ProjectParameters["bgColorClass"];
flags: ProjectParameters["flags"];

constructor({
organizationId,
bgColorClass,
config,
flags,
guidelines,
slug,
name,
type,
title,
_id,
_version,
createdAt,
Expand All @@ -41,31 +47,32 @@ export class Project extends Base<"Project"> {
updatedBy,
}: AtLeast<
ProjectParameters,
"createdBy" | "bgColorClass" | "config" | "flags" | "slug" | "name" | "type"
"createdBy" | "bgColorClass" | "flags" | "slug" | "title" | "organizationId"
>) {
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });

this.bgColorClass = bgColorClass;
this.config = config;
this.flags = flags;
this.guidelines = guidelines;
this.slug = slug;
this.name = name;
this.type = type;
this.title = title;
this.organizationId = organizationId;
}

/**
* Transforms the {@link Project} class object into a {@link ProjectParameters}-like
* json object. Useful for saving the entity to the database.
* @returns a json representation of the project entity.
*/
toJSON(): Omit<ProjectParameters, "kind"> {
const json = super.toJSON();
return {
...json,
bgColorClass: this.bgColorClass,
config: this.config,
flags: this.flags,
guidelines: this.guidelines,
slug: this.slug,
name: this.name,
type: this.type,
title: this.title,
createdBy: this.createdBy,
organizationId: this.organizationId,
};
}
}
27 changes: 19 additions & 8 deletions src/models/Sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ import { Base, type BaseParameters } from "./Base.ts";
import type { AtLeast } from "../types/mod.ts";

export interface SampleParameters extends BaseParameters<"Sample"> {
projectId: ObjectId;
/** Dataset this sample is a part of */
datasetId: ObjectId;
/** What sub-dataset/partition this sample belongs to. */
subDatasetId: ObjectId;
/** The payload of the sample. */
payload: object;
}

/**
* Parameters for creating a {@link Sample} entity.
*/
export class Sample extends Base<"Smaple"> {
projectId: ObjectId;
datasetId: ObjectId;
payload: object;
datasetId: SampleParameters["datasetId"];
subDatasetId: SampleParameters["subDatasetId"];
payload: SampleParameters["payload"];

constructor({
projectId,
subDatasetId,
datasetId,
payload,
_id,
Expand All @@ -25,20 +31,25 @@ export class Sample extends Base<"Smaple"> {
updatedBy,
}: AtLeast<
SampleParameters,
"createdBy" | "datasetId" | "projectId" | "payload"
"createdBy" | "datasetId" | "subDatasetId" | "payload"
>) {
super({ _id, _version, createdAt, updatedAt, createdBy, updatedBy });
this.projectId = projectId;
this.datasetId = datasetId;
this.subDatasetId = subDatasetId;
this.payload = payload;
}

/**
* Transforms the {@link Sample} class object into a {@link SampleParameters}-like
* json object. Useful for saving the entity to the database.
* @returns a json representation of a dataset sample entity.
*/
toJSON(): Omit<SampleParameters, "kind"> {
const json = super.toJSON();
return {
...json,
projectId: this.projectId,
datasetId: this.datasetId,
subDatasetId: this.subDatasetId,
payload: this.payload,
};
}
Expand Down
Loading

0 comments on commit 65fbf2f

Please sign in to comment.