@celljs/typeorm 4.0.0 → 4.1.0

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/README.md CHANGED
@@ -1 +1,11 @@
1
1
  # Cell - typeorm Component
2
+
3
+ [TypeORM](https://typeorm.io) data sources, repositories and `@Transactional()` for Cell backends.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @celljs/typeorm mysql2
9
+ ```
10
+
11
+ Database drivers are not dependencies of this package: install the one your data source uses (`mysql2`, `pg`, `better-sqlite3`, …). `mysql2` is an optional peer dependency; when the driver of a configured data source is missing, the application fails at startup with `CELL_TYPEORM_DRIVER_MISSING` and the command that installs it.
@@ -1,3 +1,9 @@
1
1
  import { DataSource, DataSourceOptions } from 'typeorm';
2
2
  export declare function getDataSource(name?: string): Promise<DataSource>;
3
3
  export declare function createDataSources(options: DataSourceOptions[]): Promise<DataSource[]>;
4
+ /**
5
+ * Database drivers are peer dependencies the project installs (mysql2 for `type: mysql`). When one
6
+ * is missing, typeorm throws a DriverPackageNotInstalledError; this turns it into a CellError that
7
+ * names the package to install.
8
+ */
9
+ export declare function toDriverMissingError(error: unknown): Promise<Error | undefined>;
@@ -9,12 +9,37 @@ export async function getDataSource(name) {
9
9
  return dataSource;
10
10
  }
11
11
  export async function createDataSources(options) {
12
+ var _a;
12
13
  const dataSourceManager = ContainerUtil.get(DataSourceManager);
13
- const dataSources = options.map(option => dataSourceManager.create(option));
14
- // Do not use Promise.all or test 8522 will produce a dangling sqlite connection
15
- for (const dataSource of dataSources) {
16
- await dataSource.initialize();
14
+ try {
15
+ const dataSources = options.map(option => dataSourceManager.create(option));
16
+ // Do not use Promise.all or test 8522 will produce a dangling sqlite connection
17
+ for (const dataSource of dataSources) {
18
+ await dataSource.initialize();
19
+ }
20
+ return dataSources;
21
+ }
22
+ catch (error) {
23
+ throw (_a = (await toDriverMissingError(error))) !== null && _a !== void 0 ? _a : error;
24
+ }
25
+ }
26
+ /**
27
+ * Database drivers are peer dependencies the project installs (mysql2 for `type: mysql`). When one
28
+ * is missing, typeorm throws a DriverPackageNotInstalledError; this turns it into a CellError that
29
+ * names the package to install.
30
+ */
31
+ export async function toDriverMissingError(error) {
32
+ var _a;
33
+ if (!(error instanceof Error) || error.name !== 'DriverPackageNotInstalledError') {
34
+ return undefined;
17
35
  }
18
- return dataSources;
36
+ const driver = (_a = /npm install ([^\s"'`]+)/.exec(error.message)) === null || _a === void 0 ? void 0 : _a[1];
37
+ const { CellError } = await import('@celljs/kit');
38
+ return new CellError({
39
+ code: 'CELL_TYPEORM_DRIVER_MISSING',
40
+ message: `The database driver${driver ? ` ${driver}` : ''} is not installed: ${error.message}`,
41
+ hint: driver ? `Add it to the dependencies of the project: npm install ${driver}` : 'Install the driver package of the database type in cell.typeorm.ormConfig.',
42
+ cause: error,
43
+ });
19
44
  }
20
45
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/common/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAE9C,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,IAAa;IAC7C,MAAM,iBAAiB,GAAG,aAAa,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;IAC/D,MAAM,UAAU,GAAG,iBAAiB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC/C,IAAI,CAAC,UAAU,CAAC,aAAa,EAAE,CAAC;QAC5B,MAAM,UAAU,CAAC,UAAU,EAAE,CAAC;IAClC,CAAC;IACD,OAAO,UAAU,CAAC;AACtB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,OAA4B;IAChE,MAAM,iBAAiB,GAAG,aAAa,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;IAC/D,MAAM,WAAW,GAAG,OAAO,C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K,CAAC,CAAC,mCAAI,KAAK,CAAC;IACvD,CAAC;AACL,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,KAAc;;IACrD,IAAI,CAAC,CAAC,KAAK,YAAY,KAAK,CAAC,IAAI,KAAK,CAAC,IAAI,KAAK,gCAAgC,EAAE,CAAC;QAC/E,OAAO,SAAS,CAAC;IACrB,CAAC;IACD,MAAM,MAAM,GAAG,MAAA,yBAAyB,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,0CAAG,CAAC,CAAC,CAAC;IAClE,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,CAAC;IAClD,OAAO,IAAI,SAAS,CAAC;QACjB,IAAI,EAAE,6BAA6B;QACnC,OAAO,EAAE,sBAAsB,MAAM,CAAC,CAAC,CAAC,IAAI,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,sBAAsB,KAAK,CAAC,OAAO,EAAE;QAC9F,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,0DAA0D,MAAM,EAAE,CAAC,CAAC,CAAC,4EAA4E;QAChK,KAAK,EAAE,KAAK;KACf,CAAC,CAAC;AACP,CAAC"}
@@ -1,4 +1,10 @@
1
1
  import { DataSource, DataSourceOptions } from 'typeorm';
2
2
  export declare function getDataSource(name?: string): Promise<DataSource>;
3
3
  export declare function createDataSources(options: DataSourceOptions[]): Promise<DataSource[]>;
4
+ /**
5
+ * Database drivers are peer dependencies the project installs (mysql2 for `type: mysql`). When one
6
+ * is missing, typeorm throws a DriverPackageNotInstalledError; this turns it into a CellError that
7
+ * names the package to install.
8
+ */
9
+ export declare function toDriverMissingError(error: unknown): Promise<Error | undefined>;
4
10
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/common/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAGxD,wBAAsB,aAAa,CAAC,IAAI,CAAC,EAAE,MAAM,uBAOhD;AAED,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAQ3F"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/common/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAGxD,wBAAsB,aAAa,CAAC,IAAI,CAAC,EAAE,MAAM,uBAOhD;AAED,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAY3F;AAED;;;;GAIG;AACH,wBAAsB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,KAAK,GAAG,SAAS,CAAC,CAYrF"}
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getDataSource = getDataSource;
4
4
  exports.createDataSources = createDataSources;
5
+ exports.toDriverMissingError = toDriverMissingError;
5
6
  const core_1 = require("@celljs/core");
6
7
  const common_1 = require("../common");
7
8
  async function getDataSource(name) {
@@ -13,12 +14,37 @@ async function getDataSource(name) {
13
14
  return dataSource;
14
15
  }
15
16
  async function createDataSources(options) {
17
+ var _a;
16
18
  const dataSourceManager = core_1.ContainerUtil.get(common_1.DataSourceManager);
17
- const dataSources = options.map(option => dataSourceManager.create(option));
18
- // Do not use Promise.all or test 8522 will produce a dangling sqlite connection
19
- for (const dataSource of dataSources) {
20
- await dataSource.initialize();
19
+ try {
20
+ const dataSources = options.map(option => dataSourceManager.create(option));
21
+ // Do not use Promise.all or test 8522 will produce a dangling sqlite connection
22
+ for (const dataSource of dataSources) {
23
+ await dataSource.initialize();
24
+ }
25
+ return dataSources;
26
+ }
27
+ catch (error) {
28
+ throw (_a = (await toDriverMissingError(error))) !== null && _a !== void 0 ? _a : error;
29
+ }
30
+ }
31
+ /**
32
+ * Database drivers are peer dependencies the project installs (mysql2 for `type: mysql`). When one
33
+ * is missing, typeorm throws a DriverPackageNotInstalledError; this turns it into a CellError that
34
+ * names the package to install.
35
+ */
36
+ async function toDriverMissingError(error) {
37
+ var _a;
38
+ if (!(error instanceof Error) || error.name !== 'DriverPackageNotInstalledError') {
39
+ return undefined;
21
40
  }
22
- return dataSources;
41
+ const driver = (_a = /npm install ([^\s"'`]+)/.exec(error.message)) === null || _a === void 0 ? void 0 : _a[1];
42
+ const { CellError } = await import('@celljs/kit');
43
+ return new CellError({
44
+ code: 'CELL_TYPEORM_DRIVER_MISSING',
45
+ message: `The database driver${driver ? ` ${driver}` : ''} is not installed: ${error.message}`,
46
+ hint: driver ? `Add it to the dependencies of the project: npm install ${driver}` : 'Install the driver package of the database type in cell.typeorm.ormConfig.',
47
+ cause: error,
48
+ });
23
49
  }
24
50
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/common/utils.ts"],"names":[],"mappings":";;;;AAAA,uCAA6C;AAE7C,sCAA8C;AAEvC,KAAK,wBAAwB,IAAa;IAC7C,MAAM,iBAAiB,GAAG,oBAAa,CAAC,GAAG,CAAC,0BAAiB,CAAC,CAAC;IAC/D,MAAM,UAAU,GAAG,iBAAiB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC/C,IAAI,CAAC,UAAU,CAAC,aAAa,EAAE,CAAC;QAC5B,MAAM,UAAU,CAAC,UAAU,EAAE,CAAC;IAClC,CAAC;IACD,OAAO,UAAU,CAAC;AACtB,CAAC;AAEM,KAAK,4BAA4B,OAA4B;IAChE,MAAM,iBAAiB,GAAG,oBAAa,CAAC,GAAG,CAAC,0BAAiB,CAAC,CAAC;IAC/D,MAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,iBAAiB,CAAC,MAAM,CAAC,M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gCAAgC,EAAE,CAAC;QAC/E,OAAO,SAAS,CAAC;IACrB,CAAC;IACD,MAAM,MAAM,GAAG,MAAA,yBAAyB,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,0CAAG,CAAC,CAAC,CAAC;IAClE,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,CAAC;IAClD,OAAO,IAAI,SAAS,CAAC;QACjB,IAAI,EAAE,6BAA6B;QACnC,OAAO,EAAE,sBAAsB,MAAM,CAAC,CAAC,CAAC,IAAI,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,sBAAsB,KAAK,CAAC,OAAO,EAAE;QAC9F,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,0DAA0D,MAAM,EAAE,CAAC,CAAC,CAAC,4EAA4E;QAChK,KAAK,EAAE,KAAK;KACf,CAAC,CAAC;AACP,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=utils.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.spec.d.ts","sourceRoot":"","sources":["../../src/common/utils.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const bun_test_1 = require("bun:test");
4
+ const chai_1 = require("chai");
5
+ const typeorm_1 = require("typeorm");
6
+ const DriverPackageNotInstalledError_1 = require("typeorm/error/DriverPackageNotInstalledError");
7
+ const utils_1 = require("./utils");
8
+ (0, bun_test_1.describe)('toDriverMissingError', () => {
9
+ (0, bun_test_1.it)('turns a missing driver into a CellError naming the package', async () => {
10
+ const error = await (0, utils_1.toDriverMissingError)(new DriverPackageNotInstalledError_1.DriverPackageNotInstalledError('Mysql', 'mysql2'));
11
+ (0, chai_1.expect)(error === null || error === void 0 ? void 0 : error.name).to.equal('CellError');
12
+ (0, chai_1.expect)(error.code).to.equal('CELL_TYPEORM_DRIVER_MISSING');
13
+ (0, chai_1.expect)(error.hint).to.equal('Add it to the dependencies of the project: npm install mysql2');
14
+ });
15
+ (0, bun_test_1.it)('leaves other errors alone', async () => {
16
+ (0, chai_1.expect)(await (0, utils_1.toDriverMissingError)(new Error('connect ECONNREFUSED'))).to.equal(undefined);
17
+ (0, chai_1.expect)(await (0, utils_1.toDriverMissingError)('text')).to.equal(undefined);
18
+ });
19
+ (0, bun_test_1.it)('recognises what typeorm throws for a driver that is not installed', async () => {
20
+ let thrown;
21
+ try {
22
+ new typeorm_1.DataSource({ type: 'mssql', host: 'localhost' });
23
+ }
24
+ catch (error) {
25
+ thrown = error;
26
+ }
27
+ const error = await (0, utils_1.toDriverMissingError)(thrown);
28
+ (0, chai_1.expect)(error.hint).to.equal('Add it to the dependencies of the project: npm install mssql');
29
+ });
30
+ });
31
+ //# sourceMappingURL=utils.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.spec.js","sourceRoot":"","sources":["../../src/common/utils.spec.ts"],"names":[],"mappings":";;AAAA,uCAAwC;AACxC,+BAA8B;AAC9B,qCAAqC;AACrC,iGAA8F;AAC9F,mCAA+C;AAE/C,IAAA,mBAAQ,EAAC,sBAAsB,EAAE,GAAG,EAAE;IAClC,IAAA,aAAE,EAAC,4DAA4D,EAAE,KAAK,IAAI,EAAE;QACxE,MAAM,KAAK,GAAG,MAAM,IAAA,4BAAoB,EAAC,IAAI,+DAA8B,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC;QAChG,IAAA,aAAM,EAAC,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,IAAI,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;QAC1C,IAAA,aAAM,EAAE,KAAqC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAAC;QAC5F,IAAA,aAAM,EAAE,KAAqC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,+DAA+D,CAAC,CAAC;IAClI,CAAC,CAAC,CAAC;IAEH,IAAA,aAAE,EAAC,2BAA2B,EAAE,KAAK,IAAI,EAAE;QACvC,IAAA,aAAM,EAAC,MAAM,IAAA,4BAAoB,EAAC,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;QAC1F,IAAA,aAAM,EAAC,MAAM,IAAA,4BAAoB,EAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;IACnE,CAAC,CAAC,CAAC;IAEH,IAAA,aAAE,EAAC,mEAAmE,EAAE,KAAK,IAAI,EAAE;QAC/E,IAAI,MAAe,CAAC;QACpB,IAAI,CAAC;YACD,IAAI,oBAAU,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAS,CAAC,CAAC;QAChE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,MAAM,GAAG,KAAK,CAAC;QACnB,CAAC;QACD,MAAM,KAAK,GAAG,MAAM,IAAA,4BAAoB,EAAC,MAAM,CAAC,CAAC;QACjD,IAAA,aAAM,EAAE,KAAqC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,8DAA8D,CAAC,CAAC;IACjI,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/typeorm",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "cell-component"
@@ -29,14 +29,21 @@
29
29
  "watch": "tsc -b -w --preserveWatchOutput"
30
30
  },
31
31
  "dependencies": {
32
- "@celljs/core": "4.0.0",
33
- "mysql2": "^2.3.3",
34
- "tslib": "^2.8.0",
35
- "typeorm": "^0.3.7"
32
+ "@celljs/core": "4.1.0",
33
+ "@celljs/kit": "4.1.0",
34
+ "typeorm": "^0.3.31"
35
+ },
36
+ "peerDependencies": {
37
+ "mysql2": "^3.24.6"
38
+ },
39
+ "peerDependenciesMeta": {
40
+ "mysql2": {
41
+ "optional": true
42
+ }
36
43
  },
37
44
  "devDependencies": {
38
45
  "@types/js-yaml": "^4.0.9",
39
- "js-yaml": "^4.1.0"
46
+ "js-yaml": "^4.1.1"
40
47
  },
41
48
  "publishConfig": {
42
49
  "access": "public"
@@ -0,0 +1,30 @@
1
+ import { describe, it } from 'bun:test';
2
+ import { expect } from 'chai';
3
+ import { DataSource } from 'typeorm';
4
+ import { DriverPackageNotInstalledError } from 'typeorm/error/DriverPackageNotInstalledError';
5
+ import { toDriverMissingError } from './utils';
6
+
7
+ describe('toDriverMissingError', () => {
8
+ it('turns a missing driver into a CellError naming the package', async () => {
9
+ const error = await toDriverMissingError(new DriverPackageNotInstalledError('Mysql', 'mysql2'));
10
+ expect(error?.name).to.equal('CellError');
11
+ expect((error as unknown as { code: string }).code).to.equal('CELL_TYPEORM_DRIVER_MISSING');
12
+ expect((error as unknown as { hint: string }).hint).to.equal('Add it to the dependencies of the project: npm install mysql2');
13
+ });
14
+
15
+ it('leaves other errors alone', async () => {
16
+ expect(await toDriverMissingError(new Error('connect ECONNREFUSED'))).to.equal(undefined);
17
+ expect(await toDriverMissingError('text')).to.equal(undefined);
18
+ });
19
+
20
+ it('recognises what typeorm throws for a driver that is not installed', async () => {
21
+ let thrown: unknown;
22
+ try {
23
+ new DataSource({ type: 'mssql', host: 'localhost' } as any);
24
+ } catch (error) {
25
+ thrown = error;
26
+ }
27
+ const error = await toDriverMissingError(thrown);
28
+ expect((error as unknown as { hint: string }).hint).to.equal('Add it to the dependencies of the project: npm install mssql');
29
+ });
30
+ });
@@ -13,10 +13,33 @@ export async function getDataSource(name?: string) {
13
13
 
14
14
  export async function createDataSources(options: DataSourceOptions[]): Promise<DataSource[]> {
15
15
  const dataSourceManager = ContainerUtil.get(DataSourceManager);
16
- const dataSources = options.map(option => dataSourceManager.create(option));
17
- // Do not use Promise.all or test 8522 will produce a dangling sqlite connection
18
- for (const dataSource of dataSources) {
19
- await dataSource.initialize();
16
+ try {
17
+ const dataSources = options.map(option => dataSourceManager.create(option));
18
+ // Do not use Promise.all or test 8522 will produce a dangling sqlite connection
19
+ for (const dataSource of dataSources) {
20
+ await dataSource.initialize();
21
+ }
22
+ return dataSources;
23
+ } catch (error) {
24
+ throw (await toDriverMissingError(error)) ?? error;
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Database drivers are peer dependencies the project installs (mysql2 for `type: mysql`). When one
30
+ * is missing, typeorm throws a DriverPackageNotInstalledError; this turns it into a CellError that
31
+ * names the package to install.
32
+ */
33
+ export async function toDriverMissingError(error: unknown): Promise<Error | undefined> {
34
+ if (!(error instanceof Error) || error.name !== 'DriverPackageNotInstalledError') {
35
+ return undefined;
20
36
  }
21
- return dataSources;
37
+ const driver = /npm install ([^\s"'`]+)/.exec(error.message)?.[1];
38
+ const { CellError } = await import('@celljs/kit');
39
+ return new CellError({
40
+ code: 'CELL_TYPEORM_DRIVER_MISSING',
41
+ message: `The database driver${driver ? ` ${driver}` : ''} is not installed: ${error.message}`,
42
+ hint: driver ? `Add it to the dependencies of the project: npm install ${driver}` : 'Install the driver package of the database type in cell.typeorm.ormConfig.',
43
+ cause: error,
44
+ });
22
45
  }