@sentzunhat/zacatl 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@sentzunhat/zacatl",
|
|
3
3
|
"main": "src/index.ts",
|
|
4
4
|
"module": "src/index.ts",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.5",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "https://github.com/sentzunhat/zacatl.git"
|
|
@@ -45,18 +45,18 @@
|
|
|
45
45
|
},
|
|
46
46
|
"license": "MIT",
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@fastify/http-proxy": "^
|
|
49
|
-
"config": "^
|
|
50
|
-
"fastify": "^5.
|
|
48
|
+
"@fastify/http-proxy": "^11.1.2",
|
|
49
|
+
"config": "^4.0.0",
|
|
50
|
+
"fastify": "^5.3.3",
|
|
51
51
|
"fastify-type-provider-zod": "^4.0.2",
|
|
52
52
|
"i18n": "^0.15.1",
|
|
53
53
|
"mongodb-memory-server": "^10.1.4",
|
|
54
|
-
"mongoose": "^8.
|
|
55
|
-
"pino": "^9.
|
|
56
|
-
"pino-pretty": "^
|
|
54
|
+
"mongoose": "^8.15.0",
|
|
55
|
+
"pino": "^9.7.0",
|
|
56
|
+
"pino-pretty": "^13.0.0",
|
|
57
57
|
"reflect-metadata": "^0.2.2",
|
|
58
|
-
"tsyringe": "^4.
|
|
59
|
-
"uuid": "^
|
|
60
|
-
"zod": "^3.
|
|
58
|
+
"tsyringe": "^4.10.0",
|
|
59
|
+
"uuid": "^11.1.0",
|
|
60
|
+
"zod": "^3.25.28"
|
|
61
61
|
}
|
|
62
62
|
}
|
|
@@ -7,9 +7,9 @@ import importedMongoose, {
|
|
|
7
7
|
import { v4 as uuidv4 } from "uuid";
|
|
8
8
|
import { container } from "tsyringe";
|
|
9
9
|
|
|
10
|
-
export type BaseRepositoryConfig<
|
|
10
|
+
export type BaseRepositoryConfig<D> = {
|
|
11
11
|
name?: string;
|
|
12
|
-
schema: Schema<
|
|
12
|
+
schema: Schema<D>;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
export type LeanDocument<T> = T & {
|
|
@@ -18,19 +18,21 @@ export type LeanDocument<T> = T & {
|
|
|
18
18
|
updatedAt: Date;
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
// D - Document, meant for Database representation
|
|
22
|
+
// T - Type, meant for TypeScript representation
|
|
23
|
+
export type Repository<D, T> = {
|
|
24
|
+
model: Model<D>;
|
|
23
25
|
|
|
24
26
|
findById(id: string): Promise<LeanDocument<T> | null>;
|
|
25
|
-
create(entity:
|
|
26
|
-
update(id: string, update: Partial<
|
|
27
|
+
create(entity: D): Promise<LeanDocument<T>>;
|
|
28
|
+
update(id: string, update: Partial<D>): Promise<LeanDocument<T> | null>;
|
|
27
29
|
delete(id: string): Promise<LeanDocument<T> | null>;
|
|
28
30
|
};
|
|
29
31
|
|
|
30
|
-
export abstract class BaseRepository<T> implements Repository<T> {
|
|
31
|
-
public readonly model: Model<
|
|
32
|
+
export abstract class BaseRepository<D, T> implements Repository<D, T> {
|
|
33
|
+
public readonly model: Model<D>;
|
|
32
34
|
|
|
33
|
-
constructor(config: BaseRepositoryConfig<
|
|
35
|
+
constructor(config: BaseRepositoryConfig<D>) {
|
|
34
36
|
const mongoose = connection.db?.databaseName
|
|
35
37
|
? importedMongoose
|
|
36
38
|
: container.resolve<Mongoose>(Mongoose);
|
|
@@ -38,9 +40,9 @@ export abstract class BaseRepository<T> implements Repository<T> {
|
|
|
38
40
|
const { name, schema } = config;
|
|
39
41
|
|
|
40
42
|
if (name) {
|
|
41
|
-
this.model = mongoose.model<
|
|
43
|
+
this.model = mongoose.model<D>(name, schema);
|
|
42
44
|
} else {
|
|
43
|
-
this.model = mongoose.model<
|
|
45
|
+
this.model = mongoose.model<D>(uuidv4(), schema);
|
|
44
46
|
}
|
|
45
47
|
|
|
46
48
|
this.model.createCollection();
|
|
@@ -61,7 +63,7 @@ export abstract class BaseRepository<T> implements Repository<T> {
|
|
|
61
63
|
return entity;
|
|
62
64
|
}
|
|
63
65
|
|
|
64
|
-
async create(entity:
|
|
66
|
+
async create(entity: D): Promise<LeanDocument<T>> {
|
|
65
67
|
const doc = await this.model.create(entity);
|
|
66
68
|
|
|
67
69
|
return doc.toObject<LeanDocument<T>>({ virtuals: true });
|
|
@@ -69,7 +71,7 @@ export abstract class BaseRepository<T> implements Repository<T> {
|
|
|
69
71
|
|
|
70
72
|
async update(
|
|
71
73
|
id: string,
|
|
72
|
-
update: Partial<
|
|
74
|
+
update: Partial<D>
|
|
73
75
|
): Promise<LeanDocument<T> | null> {
|
|
74
76
|
const entity = await this.model
|
|
75
77
|
.findByIdAndUpdate(id, update, { new: true })
|