@hazeljs/typeorm 0.2.0-beta.57 → 0.2.0-beta.59

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.
@@ -12,9 +12,33 @@ export interface TypeOrmServiceOptions {
12
12
  }
13
13
  export declare class TypeOrmService {
14
14
  private _dataSource;
15
+ /**
16
+ * Resolves when the DataSource is fully initialised. Started eagerly in the
17
+ * constructor so that by the time the first HTTP request arrives the
18
+ * connection is already established — no manual `onModuleInit` call needed.
19
+ */
20
+ private _ready;
15
21
  constructor(options?: TypeOrmServiceOptions);
16
22
  get dataSource(): DataSource;
17
23
  getRepository<T extends ObjectLiteral>(entity: EntityTarget<T>): Repository<T>;
24
+ /**
25
+ * Returns a promise that resolves once the DataSource is initialised.
26
+ * Useful in `main.ts` when you want to block the server start until the
27
+ * database is ready (optional but recommended for production).
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * // Optional — guarantees DB is up before first request
32
+ * await app.getContainer().resolve(TypeOrmService).ready();
33
+ * await app.listen(3000);
34
+ * ```
35
+ */
36
+ ready(): Promise<void>;
37
+ /**
38
+ * Awaits the DataSource initialisation. Idempotent — safe to call multiple
39
+ * times. Kept for compatibility with frameworks that call `onModuleInit`
40
+ * on lifecycle-aware services.
41
+ */
18
42
  onModuleInit(): Promise<void>;
19
43
  onModuleDestroy(): Promise<void>;
20
44
  }
@@ -1 +1 @@
1
- {"version":3,"file":"typeorm.service.d.ts","sourceRoot":"","sources":["../src/typeorm.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEjG,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB;;;OAGG;IACH,OAAO,CAAC,EAAE,iBAAiB,CAAC;CAC7B;AAED,qBACa,cAAc;IACzB,OAAO,CAAC,WAAW,CAAa;gBAEpB,OAAO,CAAC,EAAE,qBAAqB;IAqB3C,IAAI,UAAU,IAAI,UAAU,CAE3B;IAED,aAAa,CAAC,CAAC,SAAS,aAAa,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;IAIxE,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IAY7B,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;CAWvC"}
1
+ {"version":3,"file":"typeorm.service.d.ts","sourceRoot":"","sources":["../src/typeorm.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEjG,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB;;;OAGG;IACH,OAAO,CAAC,EAAE,iBAAiB,CAAC;CAC7B;AAED,qBACa,cAAc;IACzB,OAAO,CAAC,WAAW,CAAa;IAChC;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAgB;gBAElB,OAAO,CAAC,EAAE,qBAAqB;IAmC3C,IAAI,UAAU,IAAI,UAAU,CAE3B;IAED,aAAa,CAAC,CAAC,SAAS,aAAa,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;IAI9E;;;;;;;;;;;OAWG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAItB;;;;OAIG;IACG,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IAI7B,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;CAWvC"}
@@ -36,6 +36,19 @@ let TypeOrmService = class TypeOrmService {
36
36
  logging: false,
37
37
  });
38
38
  }
39
+ // Kick off the connection immediately. Errors are surfaced via ready() /
40
+ // onModuleInit() and on the first query attempt.
41
+ this._ready = this._dataSource.isInitialized
42
+ ? Promise.resolve()
43
+ : this._dataSource
44
+ .initialize()
45
+ .then(() => {
46
+ core_2.default.info('TypeORM connected to database');
47
+ })
48
+ .catch((err) => {
49
+ core_2.default.error('Failed to connect to database:', err);
50
+ throw err;
51
+ });
39
52
  }
40
53
  get dataSource() {
41
54
  return this._dataSource;
@@ -43,17 +56,28 @@ let TypeOrmService = class TypeOrmService {
43
56
  getRepository(entity) {
44
57
  return this._dataSource.getRepository(entity);
45
58
  }
59
+ /**
60
+ * Returns a promise that resolves once the DataSource is initialised.
61
+ * Useful in `main.ts` when you want to block the server start until the
62
+ * database is ready (optional but recommended for production).
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * // Optional — guarantees DB is up before first request
67
+ * await app.getContainer().resolve(TypeOrmService).ready();
68
+ * await app.listen(3000);
69
+ * ```
70
+ */
71
+ ready() {
72
+ return this._ready;
73
+ }
74
+ /**
75
+ * Awaits the DataSource initialisation. Idempotent — safe to call multiple
76
+ * times. Kept for compatibility with frameworks that call `onModuleInit`
77
+ * on lifecycle-aware services.
78
+ */
46
79
  async onModuleInit() {
47
- try {
48
- if (!this._dataSource.isInitialized) {
49
- await this._dataSource.initialize();
50
- }
51
- core_2.default.info('TypeORM connected to database');
52
- }
53
- catch (error) {
54
- core_2.default.error('Failed to connect to database:', error);
55
- throw error;
56
- }
80
+ await this._ready;
57
81
  }
58
82
  async onModuleDestroy() {
59
83
  try {
@@ -3,35 +3,66 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const typeorm_service_1 = require("./typeorm.service");
4
4
  describe('TypeOrmService', () => {
5
5
  describe('with provided DataSource', () => {
6
- it('should use provided DataSource and initialize/destroy', async () => {
6
+ it('auto-initialises the DataSource on construction', async () => {
7
7
  const mockDs = {
8
8
  isInitialized: false,
9
- initialize: async () => {
10
- mockDs.isInitialized = true;
11
- },
12
- destroy: async () => {
13
- mockDs.isInitialized = false;
14
- },
9
+ initialize: jest.fn(async function () {
10
+ this.isInitialized = true;
11
+ }),
12
+ destroy: jest.fn(async function () {
13
+ this.isInitialized = false;
14
+ }),
15
15
  getRepository: jest.fn(() => ({ find: jest.fn() })),
16
16
  };
17
17
  const dataSource = mockDs;
18
18
  const service = new typeorm_service_1.TypeOrmService({ dataSource });
19
- expect(service.dataSource).toBe(dataSource);
20
- await service.onModuleInit();
19
+ // ready() / onModuleInit() both resolve once initialization completes
20
+ await service.ready();
21
+ expect(mockDs.initialize).toHaveBeenCalledTimes(1);
21
22
  expect(dataSource.isInitialized).toBe(true);
23
+ // onModuleInit() is idempotent — does not re-initialize
24
+ await service.onModuleInit();
25
+ expect(mockDs.initialize).toHaveBeenCalledTimes(1);
26
+ });
27
+ it('skips re-initialization when DataSource is already initialized', async () => {
28
+ const mockDs = {
29
+ isInitialized: true,
30
+ initialize: jest.fn(),
31
+ destroy: jest.fn(async function () {
32
+ this.isInitialized = false;
33
+ }),
34
+ getRepository: jest.fn(() => ({})),
35
+ };
36
+ const dataSource = mockDs;
37
+ const service = new typeorm_service_1.TypeOrmService({ dataSource });
38
+ await service.ready();
39
+ expect(mockDs.initialize).not.toHaveBeenCalled();
40
+ });
41
+ it('destroys the DataSource on onModuleDestroy', async () => {
42
+ const mockDs = {
43
+ isInitialized: true,
44
+ initialize: jest.fn(),
45
+ destroy: jest.fn(async function () {
46
+ this.isInitialized = false;
47
+ }),
48
+ getRepository: jest.fn(() => ({})),
49
+ };
50
+ const dataSource = mockDs;
51
+ const service = new typeorm_service_1.TypeOrmService({ dataSource });
22
52
  await service.onModuleDestroy();
53
+ expect(mockDs.destroy).toHaveBeenCalledTimes(1);
23
54
  expect(dataSource.isInitialized).toBe(false);
24
55
  });
25
- it('should expose getRepository', () => {
56
+ it('exposes getRepository', async () => {
26
57
  const mockRepo = { find: jest.fn(), findOne: jest.fn() };
27
58
  const dataSource = {
28
59
  isInitialized: true,
29
60
  getRepository: jest.fn(() => mockRepo),
30
61
  };
31
62
  const service = new typeorm_service_1.TypeOrmService({ dataSource });
63
+ await service.ready();
32
64
  const repo = service.getRepository('dummy');
33
65
  expect(repo).toBe(mockRepo);
34
- expect(typeof repo.find).toBe('function');
35
66
  });
36
67
  });
37
68
  describe('without options or dataSource', () => {
@@ -39,7 +70,7 @@ describe('TypeOrmService', () => {
39
70
  afterEach(() => {
40
71
  process.env.DATABASE_URL = origEnv;
41
72
  });
42
- it('should throw when DATABASE_URL is not set', () => {
73
+ it('throws when DATABASE_URL is not set', () => {
43
74
  delete process.env.DATABASE_URL;
44
75
  expect(() => new typeorm_service_1.TypeOrmService()).toThrow(/DATABASE_URL|options|dataSource/);
45
76
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hazeljs/typeorm",
3
- "version": "0.2.0-beta.57",
3
+ "version": "0.2.0-beta.59",
4
4
  "description": "TypeORM integration for HazelJS framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -49,5 +49,5 @@
49
49
  "peerDependencies": {
50
50
  "@hazeljs/core": ">=0.2.0-beta.0"
51
51
  },
52
- "gitHead": "98c84683e8f487c660e344c2beda3a8dcaaff07f"
52
+ "gitHead": "f6d8ee8162a40e2298ccce46d843269838bbe6ff"
53
53
  }