@sentzunhat/zacatl 0.0.13 → 0.0.14

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.13",
5
+ "version": "0.0.14",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/sentzunhat/zacatl.git"
@@ -1,15 +1,11 @@
1
1
  import i18n from "i18n";
2
2
  import path from "path";
3
3
 
4
- import { AbstractArchitecture } from "../architecture";
4
+ import { AbstractArchitecture, Constructor } from "../architecture";
5
5
  import { HookHandler, RouteHandler } from "./entry-points/rest";
6
6
 
7
- export type ApplicationHookHandlers = Array<
8
- new (...args: unknown[]) => HookHandler
9
- >;
10
- export type ApplicationRouteHandlers = Array<
11
- new (...args: unknown[]) => RouteHandler
12
- >;
7
+ export type ApplicationHookHandlers = Array<Constructor<HookHandler>>;
8
+ export type ApplicationRouteHandlers = Array<Constructor<RouteHandler>>;
13
9
 
14
10
  export type ApplicationEntryPoints = {
15
11
  rest: {
@@ -6,10 +6,11 @@ export type RouteHandler<
6
6
  TBody = void,
7
7
  TQuerystring = void,
8
8
  TParams = void,
9
- THeaders = void
9
+ THeaders = void,
10
+ TReply = unknown
10
11
  > = {
11
12
  url: string;
12
13
  method: HTTPMethods;
13
14
  schema: FastifySchema;
14
- execute: Handler<TBody, TQuerystring, TParams, THeaders>;
15
+ execute: Handler<TBody, TQuerystring, TParams, THeaders, TReply>;
15
16
  };
@@ -1,5 +1,7 @@
1
1
  import { container } from "tsyringe";
2
2
 
3
+ export type Constructor<T = unknown> = new (...args: unknown[]) => T;
4
+
3
5
  type Architecture = {
4
6
  start: () => void;
5
7
  };
@@ -9,7 +11,7 @@ export abstract class AbstractArchitecture implements Architecture {
9
11
  * A generic helper method to register an array of handler classes.
10
12
  * @param handlers - An array of class constructors that implement the handler functionality.
11
13
  */
12
- protected registerDependencies<T>(dependencies: Array<new () => T>): void {
14
+ protected registerDependencies<T>(dependencies: Array<Constructor<T>>): void {
13
15
  for (const dependency of dependencies) {
14
16
  const instance = new dependency();
15
17
 
@@ -1,7 +1,7 @@
1
- import { AbstractArchitecture } from "../architecture";
1
+ import { AbstractArchitecture, Constructor } from "../architecture";
2
2
 
3
3
  export type ConfigDomain = {
4
- providers: Array<new (...args: unknown[]) => unknown>;
4
+ providers: Array<Constructor>;
5
5
  };
6
6
 
7
7
  export class Domain extends AbstractArchitecture {
@@ -1,7 +1,7 @@
1
- import { AbstractArchitecture } from "../architecture";
1
+ import { AbstractArchitecture, Constructor } from "../architecture";
2
2
 
3
3
  export type ConfigInfrastructure = {
4
- repositories: Array<new (...args: unknown[]) => unknown>;
4
+ repositories: Array<Constructor>;
5
5
  };
6
6
 
7
7
  export class Infrastructure extends AbstractArchitecture {
@@ -43,6 +43,28 @@ export abstract class BaseRepository<D, T> implements Repository<D, T> {
43
43
  return this.implementation.model;
44
44
  }
45
45
 
46
+ public isMongoose(): boolean {
47
+ return this.implementation instanceof MongooseRepository;
48
+ }
49
+
50
+ public isSequelize(): boolean {
51
+ return this.implementation instanceof SequelizeRepository;
52
+ }
53
+
54
+ public getMongooseModel(): MongooseModel<D> {
55
+ if (!this.isMongoose()) {
56
+ throw new Error("Repository is not using Mongoose");
57
+ }
58
+ return this.implementation.model as MongooseModel<D>;
59
+ }
60
+
61
+ public getSequelizeModel(): ModelCtor<any> {
62
+ if (!this.isSequelize()) {
63
+ throw new Error("Repository is not using Sequelize");
64
+ }
65
+ return this.implementation.model as ModelCtor<any>;
66
+ }
67
+
46
68
  public toLean(input: ToLeanInput<D, T>): LeanDocument<T> | null {
47
69
  return this.implementation.toLean(input);
48
70
  }
@@ -29,14 +29,14 @@ describe("BaseRepository", () => {
29
29
  });
30
30
 
31
31
  it("should create a model using the provided name and schema", async () => {
32
- expect((repository.model as any).modelName).toBe("User");
33
- expect((repository.model as any).schema).toBe(schemaUserTest);
32
+ expect(repository.getMongooseModel().modelName).toBe("User");
33
+ expect(repository.getMongooseModel().schema).toBe(schemaUserTest);
34
34
  });
35
35
 
36
36
  it("should call create() and return the created user document", async () => {
37
37
  const user = { name: "Alice" };
38
38
 
39
- const spyFunction = vi.spyOn(repository.model as any, "create");
39
+ const spyFunction = vi.spyOn(repository.getMongooseModel(), "create");
40
40
 
41
41
  const result = await repository.create(user);
42
42
 
@@ -46,7 +46,7 @@ describe("BaseRepository", () => {
46
46
 
47
47
  it("should call findById() and return the user document", async () => {
48
48
  const user = await repository.create({ name: "Alice" });
49
- const spyFunction = vi.spyOn(repository.model as any, "findById");
49
+ const spyFunction = vi.spyOn(repository.getMongooseModel(), "findById");
50
50
  const result = await repository.findById(user.id);
51
51
  expect(spyFunction).toHaveBeenNthCalledWith(1, user.id);
52
52
  expect(result).toMatchObject({ name: user.name });
@@ -58,7 +58,10 @@ describe("BaseRepository", () => {
58
58
 
59
59
  const update = { name: "Alice Updated" };
60
60
 
61
- const spyFunction = vi.spyOn(repository.model as any, "findByIdAndUpdate");
61
+ const spyFunction = vi.spyOn(
62
+ repository.getMongooseModel(),
63
+ "findByIdAndUpdate"
64
+ );
62
65
 
63
66
  const result = await repository.update(user.id, update);
64
67
 
@@ -70,7 +73,10 @@ describe("BaseRepository", () => {
70
73
 
71
74
  it("should call delete() and return the deleted user document", async () => {
72
75
  const user = await repository.create({ name: "Alice" });
73
- const spyFunction = vi.spyOn(repository.model as any, "findByIdAndDelete");
76
+ const spyFunction = vi.spyOn(
77
+ repository.getMongooseModel(),
78
+ "findByIdAndDelete"
79
+ );
74
80
  const result = await repository.delete(user.id);
75
81
  expect(spyFunction).toHaveBeenNthCalledWith(1, user.id);
76
82
  // Patch: allow for id only (ignore _id)