@sentzunhat/zacatl 0.0.2 → 0.0.3
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,10 +1,8 @@
|
|
|
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";
|
|
@@ -14,26 +12,23 @@ export type BaseRepositoryConfig<T> = {
|
|
|
14
12
|
schema: Schema<T>;
|
|
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
21
|
export type Repository<T> = {
|
|
27
22
|
model: Model<T>;
|
|
28
23
|
|
|
29
|
-
findById(id: string): Promise<
|
|
30
|
-
create(entity: T): Promise<
|
|
31
|
-
update(id: string,
|
|
32
|
-
delete(id: string): Promise<
|
|
24
|
+
findById(id: string): Promise<LeanDocument<T> | null>;
|
|
25
|
+
create(entity: T): Promise<LeanDocument<T>>;
|
|
26
|
+
update(id: string, update: Partial<T>): Promise<LeanDocument<T> | null>;
|
|
27
|
+
delete(id: string): Promise<LeanDocument<T> | null>;
|
|
33
28
|
};
|
|
34
29
|
|
|
35
30
|
export abstract class BaseRepository<T> implements Repository<T> {
|
|
36
|
-
public model: Model<T>;
|
|
31
|
+
public readonly model: Model<T>;
|
|
37
32
|
|
|
38
33
|
constructor(config: BaseRepositoryConfig<T>) {
|
|
39
34
|
const mongoose = connection.db?.databaseName
|
|
@@ -53,59 +48,51 @@ export abstract class BaseRepository<T> implements Repository<T> {
|
|
|
53
48
|
this.model.init();
|
|
54
49
|
}
|
|
55
50
|
|
|
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();
|
|
51
|
+
async findById(id: string): Promise<LeanDocument<T> | null> {
|
|
52
|
+
const entity = await this.model
|
|
53
|
+
.findById(id)
|
|
54
|
+
.lean<LeanDocument<T>>({ virtuals: true })
|
|
55
|
+
.exec();
|
|
66
56
|
|
|
67
|
-
if (!
|
|
57
|
+
if (!entity) {
|
|
68
58
|
return null;
|
|
69
59
|
}
|
|
70
60
|
|
|
71
|
-
return
|
|
72
|
-
virtuals: true,
|
|
73
|
-
});
|
|
61
|
+
return entity;
|
|
74
62
|
}
|
|
75
63
|
|
|
76
|
-
async create(entity: T): Promise<
|
|
77
|
-
const
|
|
64
|
+
async create(entity: T): Promise<LeanDocument<T>> {
|
|
65
|
+
const doc = await this.model.create(entity);
|
|
78
66
|
|
|
79
|
-
return
|
|
80
|
-
virtuals: true,
|
|
81
|
-
});
|
|
67
|
+
return doc.toObject<LeanDocument<T>>({ virtuals: true });
|
|
82
68
|
}
|
|
83
69
|
|
|
84
|
-
async update(
|
|
85
|
-
|
|
86
|
-
|
|
70
|
+
async update(
|
|
71
|
+
id: string,
|
|
72
|
+
update: Partial<T>
|
|
73
|
+
): Promise<LeanDocument<T> | null> {
|
|
74
|
+
const entity = await this.model
|
|
75
|
+
.findByIdAndUpdate(id, update, { new: true })
|
|
76
|
+
.lean<LeanDocument<T>>({ virtuals: true })
|
|
87
77
|
.exec();
|
|
88
78
|
|
|
89
|
-
if (!
|
|
79
|
+
if (!entity) {
|
|
90
80
|
return null;
|
|
91
81
|
}
|
|
92
82
|
|
|
93
|
-
return
|
|
94
|
-
virtuals: true,
|
|
95
|
-
});
|
|
83
|
+
return entity;
|
|
96
84
|
}
|
|
97
85
|
|
|
98
|
-
async delete(id: string): Promise<
|
|
99
|
-
const
|
|
100
|
-
.findByIdAndDelete
|
|
86
|
+
async delete(id: string): Promise<LeanDocument<T> | null> {
|
|
87
|
+
const entity = await this.model
|
|
88
|
+
.findByIdAndDelete(id)
|
|
89
|
+
.lean<LeanDocument<T>>({ virtuals: true })
|
|
101
90
|
.exec();
|
|
102
91
|
|
|
103
|
-
if (!
|
|
92
|
+
if (!entity) {
|
|
104
93
|
return null;
|
|
105
94
|
}
|
|
106
95
|
|
|
107
|
-
return
|
|
108
|
-
virtuals: true,
|
|
109
|
-
});
|
|
96
|
+
return entity;
|
|
110
97
|
}
|
|
111
98
|
}
|