@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
@@ -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.4",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/sentzunhat/zacatl.git"
@@ -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<T> = {
10
+ export type BaseRepositoryConfig<D> = {
13
11
  name?: string;
14
- schema: Schema<T>;
12
+ schema: Schema<D>;
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
- export type Repository<T> = {
27
- model: Model<T>;
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<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>;
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<T>;
32
+ export abstract class BaseRepository<D, T> implements Repository<D, T> {
33
+ public readonly model: Model<D>;
37
34
 
38
- constructor(config: BaseRepositoryConfig<T>) {
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<T>(name, schema);
43
+ this.model = mongoose.model<D>(name, schema);
47
44
  } else {
48
- this.model = mongoose.model<T>(uuidv4(), schema);
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
- * 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();
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 (!foundEntity) {
59
+ if (!entity) {
68
60
  return null;
69
61
  }
70
62
 
71
- return foundEntity.toObject<TDocument<T>>({
72
- virtuals: true,
73
- });
63
+ return entity;
74
64
  }
75
65
 
76
- async create(entity: T): Promise<TDocument<T>> {
77
- const createdEntity = await this.model.create<TDocument<T>>(entity);
66
+ async create(entity: D): Promise<LeanDocument<T>> {
67
+ const doc = await this.model.create(entity);
78
68
 
79
- return createdEntity.toObject<TDocument<T>>({
80
- virtuals: true,
81
- });
69
+ return doc.toObject<LeanDocument<T>>({ virtuals: true });
82
70
  }
83
71
 
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 })
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 (!updatedEntity) {
81
+ if (!entity) {
90
82
  return null;
91
83
  }
92
84
 
93
- return updatedEntity.toObject<TDocument<T>>({
94
- virtuals: true,
95
- });
85
+ return entity;
96
86
  }
97
87
 
98
- async delete(id: string): Promise<TDocument<T> | null> {
99
- const deletedEntity = await this.model
100
- .findByIdAndDelete<TDocument<T>>(id)
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 (!deletedEntity) {
94
+ if (!entity) {
104
95
  return null;
105
96
  }
106
97
 
107
- return deletedEntity.toObject<TDocument<T>>({
108
- virtuals: true,
109
- });
98
+ return entity;
110
99
  }
111
100
  }