@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
@@ -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.2",
5
+ "version": "0.0.3",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/sentzunhat/zacatl.git"
@@ -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 TDocument<T> = Document<Types.ObjectId, {}, T> &
18
- T & {
19
- id: string;
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<TDocument<T> | null>;
30
- create(entity: T): Promise<TDocument<T>>;
31
- update(id: string, entity: Partial<T>): Promise<TDocument<T> | null>;
32
- delete(id: string): Promise<TDocument<T> | null>;
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
- * Transforms a Mongoose document into a plain entity object.
58
- */
59
- protected toEntity(doc: TDocument<T>): TRequiredId<T> {
60
- // We call the built-in toObject method and cast to T.
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 (!foundEntity) {
57
+ if (!entity) {
68
58
  return null;
69
59
  }
70
60
 
71
- return foundEntity.toObject<TDocument<T>>({
72
- virtuals: true,
73
- });
61
+ return entity;
74
62
  }
75
63
 
76
- async create(entity: T): Promise<TDocument<T>> {
77
- const createdEntity = await this.model.create<TDocument<T>>(entity);
64
+ async create(entity: T): Promise<LeanDocument<T>> {
65
+ const doc = await this.model.create(entity);
78
66
 
79
- return createdEntity.toObject<TDocument<T>>({
80
- virtuals: true,
81
- });
67
+ return doc.toObject<LeanDocument<T>>({ virtuals: true });
82
68
  }
83
69
 
84
- async update(id: string, entity: Partial<T>): Promise<TDocument<T> | null> {
85
- const updatedEntity = await this.model
86
- .findByIdAndUpdate<TDocument<T>>(id, entity, { new: true })
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 (!updatedEntity) {
79
+ if (!entity) {
90
80
  return null;
91
81
  }
92
82
 
93
- return updatedEntity.toObject<TDocument<T>>({
94
- virtuals: true,
95
- });
83
+ return entity;
96
84
  }
97
85
 
98
- async delete(id: string): Promise<TDocument<T> | null> {
99
- const deletedEntity = await this.model
100
- .findByIdAndDelete<TDocument<T>>(id)
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 (!deletedEntity) {
92
+ if (!entity) {
104
93
  return null;
105
94
  }
106
95
 
107
- return deletedEntity.toObject<TDocument<T>>({
108
- virtuals: true,
109
- });
96
+ return entity;
110
97
  }
111
98
  }