@sentzunhat/zacatl 0.0.2 → 0.0.4
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
|
@@ -1,41 +1,38 @@
|
|
|
1
1
|
import importedMongoose, {
|
|
2
2
|
connection,
|
|
3
3
|
Model,
|
|
4
|
-
Document,
|
|
5
4
|
Mongoose,
|
|
6
5
|
Schema,
|
|
7
|
-
Types,
|
|
8
6
|
} from "mongoose";
|
|
9
7
|
import { v4 as uuidv4 } from "uuid";
|
|
10
8
|
import { container } from "tsyringe";
|
|
11
9
|
|
|
12
|
-
export type BaseRepositoryConfig<
|
|
10
|
+
export type BaseRepositoryConfig<D> = {
|
|
13
11
|
name?: string;
|
|
14
|
-
schema: Schema<
|
|
12
|
+
schema: Schema<D>;
|
|
15
13
|
};
|
|
16
14
|
|
|
17
|
-
export type
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export type TRequiredId<T> = T & {
|
|
23
|
-
_id: Types.ObjectId;
|
|
15
|
+
export type LeanDocument<T> = T & {
|
|
16
|
+
id: string;
|
|
17
|
+
createdAt: Date;
|
|
18
|
+
updatedAt: Date;
|
|
24
19
|
};
|
|
25
20
|
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
// D - Document, meant for Database representation
|
|
22
|
+
// T - Type, meant for TypeScript representation
|
|
23
|
+
export type Repository<D, T> = {
|
|
24
|
+
model: Model<D>;
|
|
28
25
|
|
|
29
|
-
findById(id: string): Promise<
|
|
30
|
-
create(entity:
|
|
31
|
-
update(id: string,
|
|
32
|
-
delete(id: string): Promise<
|
|
26
|
+
findById(id: string): Promise<LeanDocument<T> | null>;
|
|
27
|
+
create(entity: D): Promise<LeanDocument<T>>;
|
|
28
|
+
update(id: string, update: Partial<D>): Promise<LeanDocument<T> | null>;
|
|
29
|
+
delete(id: string): Promise<LeanDocument<T> | null>;
|
|
33
30
|
};
|
|
34
31
|
|
|
35
|
-
export abstract class BaseRepository<T> implements Repository<T> {
|
|
36
|
-
public model: Model<
|
|
32
|
+
export abstract class BaseRepository<D, T> implements Repository<D, T> {
|
|
33
|
+
public readonly model: Model<D>;
|
|
37
34
|
|
|
38
|
-
constructor(config: BaseRepositoryConfig<
|
|
35
|
+
constructor(config: BaseRepositoryConfig<D>) {
|
|
39
36
|
const mongoose = connection.db?.databaseName
|
|
40
37
|
? importedMongoose
|
|
41
38
|
: container.resolve<Mongoose>(Mongoose);
|
|
@@ -43,9 +40,9 @@ export abstract class BaseRepository<T> implements Repository<T> {
|
|
|
43
40
|
const { name, schema } = config;
|
|
44
41
|
|
|
45
42
|
if (name) {
|
|
46
|
-
this.model = mongoose.model<
|
|
43
|
+
this.model = mongoose.model<D>(name, schema);
|
|
47
44
|
} else {
|
|
48
|
-
this.model = mongoose.model<
|
|
45
|
+
this.model = mongoose.model<D>(uuidv4(), schema);
|
|
49
46
|
}
|
|
50
47
|
|
|
51
48
|
this.model.createCollection();
|
|
@@ -53,59 +50,51 @@ export abstract class BaseRepository<T> implements Repository<T> {
|
|
|
53
50
|
this.model.init();
|
|
54
51
|
}
|
|
55
52
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
return doc.toObject<TDocument<T>>({ virtuals: true }) as TRequiredId<T>;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async findById(id: string): Promise<TDocument<T> | null> {
|
|
65
|
-
const foundEntity = await this.model.findById<TDocument<T>>(id).exec();
|
|
53
|
+
async findById(id: string): Promise<LeanDocument<T> | null> {
|
|
54
|
+
const entity = await this.model
|
|
55
|
+
.findById(id)
|
|
56
|
+
.lean<LeanDocument<T>>({ virtuals: true })
|
|
57
|
+
.exec();
|
|
66
58
|
|
|
67
|
-
if (!
|
|
59
|
+
if (!entity) {
|
|
68
60
|
return null;
|
|
69
61
|
}
|
|
70
62
|
|
|
71
|
-
return
|
|
72
|
-
virtuals: true,
|
|
73
|
-
});
|
|
63
|
+
return entity;
|
|
74
64
|
}
|
|
75
65
|
|
|
76
|
-
async create(entity:
|
|
77
|
-
const
|
|
66
|
+
async create(entity: D): Promise<LeanDocument<T>> {
|
|
67
|
+
const doc = await this.model.create(entity);
|
|
78
68
|
|
|
79
|
-
return
|
|
80
|
-
virtuals: true,
|
|
81
|
-
});
|
|
69
|
+
return doc.toObject<LeanDocument<T>>({ virtuals: true });
|
|
82
70
|
}
|
|
83
71
|
|
|
84
|
-
async update(
|
|
85
|
-
|
|
86
|
-
|
|
72
|
+
async update(
|
|
73
|
+
id: string,
|
|
74
|
+
update: Partial<D>
|
|
75
|
+
): Promise<LeanDocument<T> | null> {
|
|
76
|
+
const entity = await this.model
|
|
77
|
+
.findByIdAndUpdate(id, update, { new: true })
|
|
78
|
+
.lean<LeanDocument<T>>({ virtuals: true })
|
|
87
79
|
.exec();
|
|
88
80
|
|
|
89
|
-
if (!
|
|
81
|
+
if (!entity) {
|
|
90
82
|
return null;
|
|
91
83
|
}
|
|
92
84
|
|
|
93
|
-
return
|
|
94
|
-
virtuals: true,
|
|
95
|
-
});
|
|
85
|
+
return entity;
|
|
96
86
|
}
|
|
97
87
|
|
|
98
|
-
async delete(id: string): Promise<
|
|
99
|
-
const
|
|
100
|
-
.findByIdAndDelete
|
|
88
|
+
async delete(id: string): Promise<LeanDocument<T> | null> {
|
|
89
|
+
const entity = await this.model
|
|
90
|
+
.findByIdAndDelete(id)
|
|
91
|
+
.lean<LeanDocument<T>>({ virtuals: true })
|
|
101
92
|
.exec();
|
|
102
93
|
|
|
103
|
-
if (!
|
|
94
|
+
if (!entity) {
|
|
104
95
|
return null;
|
|
105
96
|
}
|
|
106
97
|
|
|
107
|
-
return
|
|
108
|
-
virtuals: true,
|
|
109
|
-
});
|
|
98
|
+
return entity;
|
|
110
99
|
}
|
|
111
100
|
}
|