Skip to content

Commit caef334

Browse files
author
DylanBulmer
committed
init jsr models
1 parent 81e7f99 commit caef334

29 files changed

Lines changed: 958 additions & 17 deletions

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024-present Dylan Bulmer
3+
Copyright (c) 2023-present Dylan Bulmer
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

README.md

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,61 @@
1-
# @codr/ts-jsr-template
1+
# @codr/models
22

33
[![JSR Scope](https://jsr.io/badges/@codr)](https://jsr.io/@codr)
4-
[![JSR](https://jsr.io/badges/@codr/<package>)](https://jsr.io/@codr/<package>)
5-
[![JSR Score](https://jsr.io/badges/@codr/<package>/score)](https://jsr.io/@codr/<package>)
6-
[![CodeQL](https://github.com/CodrJS/ts-jsr-template/actions/workflows/codeql.yml/badge.svg)](https://github.com/CodrJS/ts-jsr-template/actions/workflows/codeql.yml)
4+
[![JSR](https://jsr.io/badges/@codr/models)](https://jsr.io/@codr/models)
5+
[![JSR Score](https://jsr.io/badges/@codr/models/score)](https://jsr.io/@codr/models)
6+
[![CodeQL](https://github.com/CodrJS/models/actions/workflows/codeql.yml/badge.svg)](https://github.com/CodrJS/models/actions/workflows/codeql.yml)
77

88
## Purpose
99

10-
Codr has the `@codr` namespace for the JavaScript Resistry (jsr). Modules
11-
published to JSR should by compatible for at least Node and Deno.
10+
This module is for defining all reusable models for Codr.
1211

1312
## Getting Started
1413

15-
Click "Use this template" in Github, then "Create a new repository."
14+
In order to use the `@jsr` scope in npm, the `.npmrc` file needs to be updated
15+
with the following:
16+
17+
```ini
18+
@jsr:registry=https://npm.jsr.io
19+
```
20+
21+
Installing via `npm`
22+
23+
```bash
24+
npx jsr add @codr/models@^1
25+
# or
26+
npm install @jsr/codr___models@^1
27+
```
28+
29+
Installing via `yarn`
30+
31+
```bash
32+
yarn dlx jsr add @codr/models@^1
33+
# or
34+
yarn add @jsr/codr___models@^1
35+
```
36+
37+
Installing via `deno`
38+
39+
```bash
40+
deno add @codr/models@^1
41+
```
42+
43+
```ts
44+
/* or import directly */
45+
import { User } from "jsr:@codr/models@^1";
46+
```
1647

1748
## Contributing
1849

19-
```shell
50+
```bash
2051
# Clone the repo
21-
git clone git@github.com:CodrJS/ts-jsr-template.git
52+
git clone git@github.com:CodrJS/models.git
2253

2354
# Cache deno dependencies
2455
deno cache
2556

26-
# Lint, format, and test the code
27-
deno lint
57+
# Format, lint, and test the code
2858
deno fmt
59+
deno lint
2960
deno test
3061
```

deno.lock

Lines changed: 84 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/add.ts

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

src/mod.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
// export files from here.
2-
export { add } from "./add.ts";
2+
3+
// export * as Types from "./types/mod.ts";
4+
export * from "./models/mod.ts";

src/models/Annotation.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import type { ObjectId } from "npm:mongodb";
2+
import { Base, type IBase } from "./Base.ts";
3+
import type { AtLeast } from "../types/mod.ts";
4+
5+
export interface IAnnotation extends IBase<"Annotation"> {
6+
projectId: ObjectId;
7+
datasetId: ObjectId;
8+
sampleId: ObjectId;
9+
annotatedBy: ObjectId;
10+
value: object;
11+
}
12+
13+
export class Annotation extends Base<"Annotation"> {
14+
projectId: ObjectId;
15+
datasetId: ObjectId;
16+
sampleId: ObjectId;
17+
annotatedBy: ObjectId;
18+
value: object;
19+
20+
constructor({
21+
projectId,
22+
datasetId,
23+
sampleId,
24+
value,
25+
annotatedBy,
26+
_id,
27+
__v,
28+
createdAt,
29+
updatedAt,
30+
createdBy,
31+
updatedBy,
32+
}: AtLeast<
33+
IAnnotation,
34+
| "createdBy"
35+
| "projectId"
36+
| "datasetId"
37+
| "annotatedBy"
38+
| "sampleId"
39+
| "value"
40+
>) {
41+
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
42+
this.projectId = projectId;
43+
this.datasetId = datasetId;
44+
this.value = value;
45+
this.sampleId = sampleId;
46+
this.annotatedBy = annotatedBy;
47+
}
48+
49+
toJSON(): Omit<IAnnotation, "kind"> {
50+
const json = super.toJSON();
51+
return {
52+
...json,
53+
projectId: this.projectId,
54+
annotatedBy: this.annotatedBy,
55+
sampleId: this.sampleId,
56+
datasetId: this.datasetId,
57+
value: this.value,
58+
};
59+
}
60+
}

src/models/Audit.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { ObjectId } from "npm:mongodb";
2+
import type { IBase } from "./Base.ts";
3+
import type { ActionCode, ResourceCode } from "../types/mod.ts";
4+
5+
export interface IAudit<T> extends Omit<IBase<"Audit">, "createdBy"> {
6+
type: ResourceCode; // Where: what system processed this event.
7+
action: ActionCode; // How: what action was taken.
8+
userId: ObjectId; // Who: which user made the change.
9+
payload: Partial<T>; // What: what data got modified (should be a diff of before/after. New entities should be complete)
10+
processedAt: Date; // When: what date/time did this occur.
11+
}

src/models/Authorization.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { ObjectId } from "npm:mongodb";
2+
import { Base, type IBase } from "./Base.ts";
3+
import type { ActionCode, AtLeast, ResourceCode } from "../types/mod.ts";
4+
5+
export interface IAuthorization extends IBase<"Authorization"> {
6+
userId: ObjectId;
7+
roleId: ObjectId[];
8+
}
9+
10+
export interface IAuthorizationResponse {
11+
userId: ObjectId;
12+
roleCodes: string[];
13+
grants: Partial<Record<ResourceCode, Partial<Record<ActionCode, boolean>>>>;
14+
}
15+
16+
export class Authorization extends Base<"Authorization"> {
17+
userId: ObjectId;
18+
roleId: ObjectId[];
19+
20+
constructor({
21+
userId,
22+
roleId = [],
23+
_id,
24+
__v,
25+
createdAt,
26+
updatedAt,
27+
createdBy,
28+
updatedBy,
29+
}: AtLeast<IAuthorization, "createdBy" | "userId">) {
30+
super({ _id, __v, createdAt, updatedAt, createdBy, updatedBy });
31+
this.userId = userId;
32+
this.roleId = roleId;
33+
}
34+
35+
toJSON(): Omit<IAuthorization, "kind"> {
36+
const json = super.toJSON();
37+
return {
38+
userId: this.userId,
39+
roleId: this.roleId,
40+
...json,
41+
};
42+
}
43+
}

src/models/Base.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { ObjectId } from "npm:mongodb";
2+
import type { AtLeast } from "../types/mod.ts";
3+
4+
export interface IBase<Kind extends string> {
5+
readonly kind: Kind;
6+
__v?: number;
7+
_id: ObjectId;
8+
createdAt: Date;
9+
updatedAt: Date;
10+
createdBy: ObjectId;
11+
updatedBy: ObjectId;
12+
}
13+
14+
export class Base<K extends string> {
15+
readonly __v: IBase<K>["__v"];
16+
readonly _id: IBase<K>["_id"];
17+
readonly createdAt: Date;
18+
readonly updatedAt: Date;
19+
readonly createdBy: ObjectId;
20+
readonly updatedBy: ObjectId;
21+
22+
constructor({
23+
createdAt,
24+
updatedAt,
25+
createdBy,
26+
updatedBy,
27+
_id,
28+
__v,
29+
}: AtLeast<IBase<K>, "createdBy">) {
30+
this.__v = __v;
31+
this._id = _id || new ObjectId();
32+
33+
const now = new Date(Date.now());
34+
this.createdAt = createdAt || now;
35+
this.updatedAt = updatedAt || now;
36+
37+
this.createdBy = createdBy;
38+
this.updatedBy = updatedBy || createdBy;
39+
}
40+
41+
toJSON(): Omit<IBase<K>, "kind"> {
42+
return {
43+
__v: this.__v,
44+
_id: this._id,
45+
createdAt: this.createdAt,
46+
updatedAt: this.updatedAt,
47+
createdBy: this.createdBy,
48+
updatedBy: this.updatedBy,
49+
};
50+
}
51+
}

0 commit comments

Comments
 (0)