@companix/xeo-server 0.0.2
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/.eslintrc +54 -0
- package/dist/common/decorators.d.ts +3 -0
- package/dist/common/decorators.js +14 -0
- package/dist/common/decorators.js.map +1 -0
- package/dist/common/index.d.ts +3 -0
- package/dist/common/index.js +20 -0
- package/dist/common/index.js.map +1 -0
- package/dist/common/tokens.d.ts +3 -0
- package/dist/common/tokens.js +14 -0
- package/dist/common/tokens.js.map +1 -0
- package/dist/common/utils.d.ts +2 -0
- package/dist/common/utils.js +19 -0
- package/dist/common/utils.js.map +1 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +8 -0
- package/dist/constants.js.map +1 -0
- package/dist/driver.module.d.ts +7 -0
- package/dist/driver.module.js +41 -0
- package/dist/driver.module.js.map +1 -0
- package/dist/drivers/collection.driver.d.ts +21 -0
- package/dist/drivers/collection.driver.js +101 -0
- package/dist/drivers/collection.driver.js.map +1 -0
- package/dist/drivers/table.driver.d.ts +13 -0
- package/dist/drivers/table.driver.js +79 -0
- package/dist/drivers/table.driver.js.map +1 -0
- package/dist/factories/definitions.factory.d.ts +11 -0
- package/dist/factories/definitions.factory.js +116 -0
- package/dist/factories/definitions.factory.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/mongoose-options.interface.d.ts +12 -0
- package/dist/mongoose-options.interface.js +3 -0
- package/dist/mongoose-options.interface.js.map +1 -0
- package/dist/mongoose.module.d.ts +11 -0
- package/dist/mongoose.module.js +108 -0
- package/dist/mongoose.module.js.map +1 -0
- package/dist/storages/data-source.d.ts +10 -0
- package/dist/storages/data-source.js +34 -0
- package/dist/storages/data-source.js.map +1 -0
- package/jest.cases.config.cjs +14 -0
- package/lib/common/decorators.ts +17 -0
- package/lib/common/index.ts +3 -0
- package/lib/common/tokens.ts +17 -0
- package/lib/common/utils.ts +29 -0
- package/lib/constants.ts +4 -0
- package/lib/driver.module.ts +37 -0
- package/lib/drivers/collection.driver.ts +157 -0
- package/lib/drivers/table.driver.ts +109 -0
- package/lib/factories/definitions.factory.ts +129 -0
- package/lib/index.ts +3 -0
- package/lib/mongoose-options.interface.ts +19 -0
- package/lib/mongoose.module.ts +95 -0
- package/lib/storages/data-source.ts +37 -0
- package/package.json +42 -0
- package/tests/app/bootstrap.ts +16 -0
- package/tests/app/db.ts +22 -0
- package/tests/app/filters.ts +25 -0
- package/tests/app/helpers/is-one-of.ts +58 -0
- package/tests/app/main.ts +3 -0
- package/tests/app/module/app.controller.ts +67 -0
- package/tests/app/module/app.dto.ts +394 -0
- package/tests/app/module/app.module.ts +12 -0
- package/tests/app/module/app.service.ts +76 -0
- package/tests/app/root.module.ts +12 -0
- package/tests/globals.d.ts +6 -0
- package/tests/integrations/cases.test.ts +154 -0
- package/tests/integrations/custom.test.ts +69 -0
- package/tests/unit/definitions.test.ts +31 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +17 -0
- package/tsconfig.test-app.json +10 -0
package/.eslintrc
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"env": {
|
|
3
|
+
"node": true,
|
|
4
|
+
"browser": true,
|
|
5
|
+
"es6": true
|
|
6
|
+
},
|
|
7
|
+
"extends": [
|
|
8
|
+
"plugin:react/recommended",
|
|
9
|
+
"plugin:react-hooks/recommended",
|
|
10
|
+
"react-app"
|
|
11
|
+
],
|
|
12
|
+
"parser": "@typescript-eslint/parser",
|
|
13
|
+
"plugins": ["react"],
|
|
14
|
+
"parserOptions": {
|
|
15
|
+
"sourceType": "module",
|
|
16
|
+
"requireConfigFile": false,
|
|
17
|
+
"ecmaFeatures": {
|
|
18
|
+
"jsx": true
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"settings": {
|
|
22
|
+
"react": {
|
|
23
|
+
"version": "detect"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"rules": {
|
|
27
|
+
"no-var": "error",
|
|
28
|
+
"typescript-eslint/typescript-estree": "off",
|
|
29
|
+
"react-hooks/exhaustive-deps": "off",
|
|
30
|
+
"react/react-in-jsx-scope": "off",
|
|
31
|
+
"react/no-children-prop": "off",
|
|
32
|
+
"react/display-name": "off",
|
|
33
|
+
"jsx-a11y/aria-role": "off",
|
|
34
|
+
"react/prop-types": "off",
|
|
35
|
+
"react/jsx-no-target-blank": "off",
|
|
36
|
+
"import/no-anonymous-default-export": "off",
|
|
37
|
+
"import/no-extraneous-dependencies": [
|
|
38
|
+
"error",
|
|
39
|
+
{
|
|
40
|
+
"devDependencies": true,
|
|
41
|
+
"peerDependencies": false
|
|
42
|
+
}
|
|
43
|
+
],
|
|
44
|
+
"no-throw-literal": "off",
|
|
45
|
+
"react/no-multi-comp": [2, { "ignoreStateless": true }],
|
|
46
|
+
"no-restricted-syntax": [
|
|
47
|
+
"error",
|
|
48
|
+
{
|
|
49
|
+
"selector": "CallExpression[arguments.length=1][callee.property.name='reduce']",
|
|
50
|
+
"message": "Provide initialValue to .reduce()."
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { CollectionScheme, DataScheme } from '@companix/xeo-scheme';
|
|
2
|
+
export declare const InjectDataSource: (dataSource: DataScheme<CollectionScheme>) => PropertyDecorator & ParameterDecorator;
|
|
3
|
+
export declare const InjectConnection: () => PropertyDecorator & ParameterDecorator;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InjectConnection = exports.InjectDataSource = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const tokens_1 = require("./tokens");
|
|
6
|
+
const InjectDataSource = (dataSource) => {
|
|
7
|
+
return (0, common_1.Inject)((0, tokens_1.getDataSourceToken)(dataSource));
|
|
8
|
+
};
|
|
9
|
+
exports.InjectDataSource = InjectDataSource;
|
|
10
|
+
const InjectConnection = () => {
|
|
11
|
+
return (0, common_1.Inject)((0, tokens_1.getConnectionToken)());
|
|
12
|
+
};
|
|
13
|
+
exports.InjectConnection = InjectConnection;
|
|
14
|
+
//# sourceMappingURL=decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../lib/common/decorators.ts"],"names":[],"mappings":";;;AAAA,2CAAuC;AACvC,qCAAiE;AAM1D,MAAM,gBAAgB,GAAG,CAAC,UAAwC,EAAE,EAAE;IAC3E,OAAO,IAAA,eAAM,EAAC,IAAA,2BAAkB,EAAC,UAAU,CAAC,CAAC,CAAA;AAC/C,CAAC,CAAA;AAFY,QAAA,gBAAgB,oBAE5B;AAKM,MAAM,gBAAgB,GAAG,GAAG,EAAE;IACnC,OAAO,IAAA,eAAM,EAAC,IAAA,2BAAkB,GAAE,CAAC,CAAA;AACrC,CAAC,CAAA;AAFY,QAAA,gBAAgB,oBAE5B"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./decorators"), exports);
|
|
18
|
+
__exportStar(require("./tokens"), exports);
|
|
19
|
+
__exportStar(require("./utils"), exports);
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/common/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA4B;AAC5B,2CAAwB;AACxB,0CAAuB"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDataSourceToken = exports.getConnectionToken = void 0;
|
|
4
|
+
const constants_1 = require("../constants");
|
|
5
|
+
const data_source_1 = require("../storages/data-source");
|
|
6
|
+
const getConnectionToken = (name) => {
|
|
7
|
+
return name && name !== constants_1.DEFAULT_DB_CONNECTION ? `${name}Connection` : constants_1.DEFAULT_DB_CONNECTION;
|
|
8
|
+
};
|
|
9
|
+
exports.getConnectionToken = getConnectionToken;
|
|
10
|
+
const getDataSourceToken = (dataScheme) => {
|
|
11
|
+
return data_source_1.DataSourceStorage.getProviderToken(dataScheme);
|
|
12
|
+
};
|
|
13
|
+
exports.getDataSourceToken = getDataSourceToken;
|
|
14
|
+
//# sourceMappingURL=tokens.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.js","sourceRoot":"","sources":["../../lib/common/tokens.ts"],"names":[],"mappings":";;;AAAA,4CAAoD;AAEpD,yDAA2D;AAKpD,MAAM,kBAAkB,GAAG,CAAC,IAAa,EAAE,EAAE;IAClD,OAAO,IAAI,IAAI,IAAI,KAAK,iCAAqB,CAAC,CAAC,CAAC,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,iCAAqB,CAAA;AAC7F,CAAC,CAAA;AAFY,QAAA,kBAAkB,sBAE9B;AAKM,MAAM,kBAAkB,GAAG,CAAC,UAAwC,EAAE,EAAE;IAC7E,OAAO,+BAAiB,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAA;AACvD,CAAC,CAAA;AAFY,QAAA,kBAAkB,sBAE9B"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.handleRetry = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const operators_1 = require("rxjs/operators");
|
|
6
|
+
const handleRetry = (retryAttempts = 9, retryDelay = 3000, verboseRetryLog = false) => {
|
|
7
|
+
const logger = new common_1.Logger('MongooseModule');
|
|
8
|
+
return (source) => source.pipe((0, operators_1.retryWhen)((e) => e.pipe((0, operators_1.scan)((errorCount, error) => {
|
|
9
|
+
const verboseMessage = verboseRetryLog ? ` Message: ${error.message}.` : '';
|
|
10
|
+
const retryMessage = retryAttempts > 0 ? ` Retrying (${errorCount + 1})...` : '';
|
|
11
|
+
logger.error(['Unable to connect to the database.', verboseMessage, retryMessage].join(''), error.stack);
|
|
12
|
+
if (errorCount + 1 >= retryAttempts) {
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
return errorCount + 1;
|
|
16
|
+
}, 0), (0, operators_1.delay)(retryDelay))));
|
|
17
|
+
};
|
|
18
|
+
exports.handleRetry = handleRetry;
|
|
19
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../lib/common/utils.ts"],"names":[],"mappings":";;;AAAA,2CAAuC;AAEvC,8CAAuD;AAEhD,MAAM,WAAW,GAAG,CAAC,aAAa,GAAG,CAAC,EAAE,UAAU,GAAG,IAAI,EAAE,eAAe,GAAG,KAAK,EAAE,EAAE;IAC3F,MAAM,MAAM,GAAG,IAAI,eAAM,CAAC,gBAAgB,CAAC,CAAA;IAE3C,OAAO,CAAI,MAAqB,EAAE,EAAE,CAClC,MAAM,CAAC,IAAI,CACT,IAAA,qBAAS,EAAC,CAAC,CAAC,EAAE,EAAE,CACd,CAAC,CAAC,IAAI,CACJ,IAAA,gBAAI,EAAC,CAAC,UAAU,EAAE,KAAK,EAAE,EAAE;QACzB,MAAM,cAAc,GAAG,eAAe,CAAC,CAAC,CAAC,aAAa,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC,EAAE,CAAA;QAC3E,MAAM,YAAY,GAAG,aAAa,GAAG,CAAC,CAAC,CAAC,CAAC,cAAc,UAAU,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAA;QAEhF,MAAM,CAAC,KAAK,CACV,CAAC,oCAAoC,EAAE,cAAc,EAAE,YAAY,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,EAC7E,KAAK,CAAC,KAAK,CACZ,CAAA;QACD,IAAI,UAAU,GAAG,CAAC,IAAI,aAAa,EAAE,CAAC;YACpC,MAAM,KAAK,CAAA;QACb,CAAC;QACD,OAAO,UAAU,GAAG,CAAC,CAAA;IACvB,CAAC,EAAE,CAAC,CAAC,EACL,IAAA,iBAAK,EAAC,UAAU,CAAC,CAClB,CACF,CACF,CAAA;AACL,CAAC,CAAA;AAxBY,QAAA,WAAW,eAwBvB"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const DEFAULT_DB_CONNECTION = "XeoDatabaseConnection";
|
|
2
|
+
export declare const MONGOOSE_MODULE_OPTIONS = "XeoMongooseModuleOptions";
|
|
3
|
+
export declare const MONGOOSE_CONNECTION_NAME = "XeoMongooseConnectionName";
|
|
4
|
+
export declare const DATA_SOURCE_TOKEN = "XEO_DATA_SOURCE";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DATA_SOURCE_TOKEN = exports.MONGOOSE_CONNECTION_NAME = exports.MONGOOSE_MODULE_OPTIONS = exports.DEFAULT_DB_CONNECTION = void 0;
|
|
4
|
+
exports.DEFAULT_DB_CONNECTION = 'XeoDatabaseConnection';
|
|
5
|
+
exports.MONGOOSE_MODULE_OPTIONS = 'XeoMongooseModuleOptions';
|
|
6
|
+
exports.MONGOOSE_CONNECTION_NAME = 'XeoMongooseConnectionName';
|
|
7
|
+
exports.DATA_SOURCE_TOKEN = `XEO_DATA_SOURCE`;
|
|
8
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../lib/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,qBAAqB,GAAG,uBAAuB,CAAA;AAC/C,QAAA,uBAAuB,GAAG,0BAA0B,CAAA;AACpD,QAAA,wBAAwB,GAAG,2BAA2B,CAAA;AACtD,QAAA,iBAAiB,GAAG,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { CollectionScheme, DataScheme } from '@companix/xeo-scheme';
|
|
3
|
+
import { MongooseModuleOptions } from './mongoose-options.interface';
|
|
4
|
+
export declare class MongooseDriverModule {
|
|
5
|
+
static forRoot(uri: string, options?: MongooseModuleOptions): DynamicModule;
|
|
6
|
+
static forFeature(dataSource: DataScheme<CollectionScheme>, connectionName?: string): DynamicModule;
|
|
7
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var MongooseDriverModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.MongooseDriverModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const tokens_1 = require("./common/tokens");
|
|
13
|
+
const mongoose_module_1 = require("./mongoose.module");
|
|
14
|
+
const data_source_1 = require("./storages/data-source");
|
|
15
|
+
let MongooseDriverModule = MongooseDriverModule_1 = class MongooseDriverModule {
|
|
16
|
+
static forRoot(uri, options = {}) {
|
|
17
|
+
return {
|
|
18
|
+
module: MongooseDriverModule_1,
|
|
19
|
+
imports: [mongoose_module_1.MongooseCoreModule.forRoot(uri, options)]
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
static forFeature(dataSource, connectionName) {
|
|
23
|
+
const provider = {
|
|
24
|
+
provide: (0, tokens_1.getDataSourceToken)(dataSource),
|
|
25
|
+
useFactory: (connection) => {
|
|
26
|
+
return data_source_1.DataSourceStorage.getSource(dataSource, connection);
|
|
27
|
+
},
|
|
28
|
+
inject: [(0, tokens_1.getConnectionToken)(connectionName)]
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
module: MongooseDriverModule_1,
|
|
32
|
+
providers: [provider],
|
|
33
|
+
exports: [provider]
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
exports.MongooseDriverModule = MongooseDriverModule;
|
|
38
|
+
exports.MongooseDriverModule = MongooseDriverModule = MongooseDriverModule_1 = __decorate([
|
|
39
|
+
(0, common_1.Module)({})
|
|
40
|
+
], MongooseDriverModule);
|
|
41
|
+
//# sourceMappingURL=driver.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"driver.module.js","sourceRoot":"","sources":["../lib/driver.module.ts"],"names":[],"mappings":";;;;;;;;;;AACA,2CAAsD;AACtD,4CAAwE;AAGxE,uDAAsD;AAEtD,wDAA0D;AAMnD,IAAM,oBAAoB,4BAA1B,MAAM,oBAAoB;IAC/B,MAAM,CAAC,OAAO,CAAC,GAAW,EAAE,UAAiC,EAAE;QAC7D,OAAO;YACL,MAAM,EAAE,sBAAoB;YAC5B,OAAO,EAAE,CAAC,oCAAkB,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;SACpD,CAAA;IACH,CAAC;IAED,MAAM,CAAC,UAAU,CAAC,UAAwC,EAAE,cAAuB;QACjF,MAAM,QAAQ,GAAa;YACzB,OAAO,EAAE,IAAA,2BAAkB,EAAC,UAAU,CAAC;YACvC,UAAU,EAAE,CAAC,UAAsB,EAAgC,EAAE;gBACnE,OAAO,+BAAiB,CAAC,SAAS,CAAC,UAAU,EAAE,UAAU,CAAC,CAAA;YAC5D,CAAC;YACD,MAAM,EAAE,CAAC,IAAA,2BAAkB,EAAC,cAAc,CAAC,CAAC;SAC7C,CAAA;QAED,OAAO;YACL,MAAM,EAAE,sBAAoB;YAC5B,SAAS,EAAE,CAAC,QAAQ,CAAC;YACrB,OAAO,EAAE,CAAC,QAAQ,CAAC;SACpB,CAAA;IACH,CAAC;CACF,CAAA;AAvBY,oDAAoB;+BAApB,oBAAoB;IADhC,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,oBAAoB,CAuBhC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { CollectionDriver, CollectionDriverParams, CollectionScheme, DataScheme } from '@companix/xeo-scheme';
|
|
2
|
+
import { MongoRelationsTable } from './table.driver';
|
|
3
|
+
import { Connection } from 'mongoose';
|
|
4
|
+
export declare class MongoCollectionDriver<T extends CollectionScheme> implements CollectionDriver {
|
|
5
|
+
private dataScheme;
|
|
6
|
+
private connection;
|
|
7
|
+
private collections;
|
|
8
|
+
private discriminatedModels;
|
|
9
|
+
tables: MongoRelationsTable<T>;
|
|
10
|
+
constructor(dataScheme: DataScheme<T>, connection: Connection);
|
|
11
|
+
getAll({ model }: CollectionDriverParams.Model): Promise<any[]>;
|
|
12
|
+
get({ model, id }: CollectionDriverParams.Record): Promise<any>;
|
|
13
|
+
create({ model, data }: CollectionDriverParams.Create): Promise<void>;
|
|
14
|
+
remove({ model, id }: CollectionDriverParams.Record): Promise<void>;
|
|
15
|
+
update({ model, id, patches }: CollectionDriverParams.Update): Promise<void>;
|
|
16
|
+
private getCollection;
|
|
17
|
+
exists({ model, id }: CollectionDriverParams.Record): Promise<boolean>;
|
|
18
|
+
private getIdentifierKey;
|
|
19
|
+
private useModel;
|
|
20
|
+
}
|
|
21
|
+
export declare const createMongoDriver: (connection: Connection) => <T extends CollectionScheme>(dataScheme: DataScheme<T>) => MongoCollectionDriver<T>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createMongoDriver = exports.MongoCollectionDriver = void 0;
|
|
4
|
+
const table_driver_1 = require("./table.driver");
|
|
5
|
+
const mongoose_1 = require("mongoose");
|
|
6
|
+
const definitions_factory_1 = require("../factories/definitions.factory");
|
|
7
|
+
const common_1 = require("@nestjs/common");
|
|
8
|
+
class MongoCollectionDriver {
|
|
9
|
+
constructor(dataScheme, connection) {
|
|
10
|
+
this.dataScheme = dataScheme;
|
|
11
|
+
this.connection = connection;
|
|
12
|
+
this.collections = {};
|
|
13
|
+
this.discriminatedModels = {};
|
|
14
|
+
common_1.Logger.log('Driver bootstrap', 'MongoDriver');
|
|
15
|
+
this.tables = new table_driver_1.MongoRelationsTable(dataScheme, connection);
|
|
16
|
+
const factory = new definitions_factory_1.DefinitionsFactory(dataScheme);
|
|
17
|
+
for (const name in dataScheme.collections) {
|
|
18
|
+
const model = dataScheme.collections[name].name;
|
|
19
|
+
const scheme = dataScheme.models[model].scheme;
|
|
20
|
+
const baseDefinition = factory.createForScheme(scheme);
|
|
21
|
+
if (scheme.type === 'base') {
|
|
22
|
+
this.collections[model] = this.useModel(model, new mongoose_1.Schema(baseDefinition));
|
|
23
|
+
}
|
|
24
|
+
if (scheme.type === 'discriminated') {
|
|
25
|
+
this.collections[model] = this.useModel(model, new mongoose_1.Schema(baseDefinition, {
|
|
26
|
+
discriminatorKey: scheme.discriminatorKey
|
|
27
|
+
}));
|
|
28
|
+
this.discriminatedModels[model] = {
|
|
29
|
+
discriminatorKey: scheme.discriminatorKey,
|
|
30
|
+
models: {}
|
|
31
|
+
};
|
|
32
|
+
for (const discriminator of scheme.discriminators) {
|
|
33
|
+
const discriminatedModel = this.collections[model].discriminator(discriminator.name, new mongoose_1.Schema(factory.createDefinitionScheme(discriminator)), discriminator.value);
|
|
34
|
+
this.discriminatedModels[model].models[discriminator.value] = discriminatedModel;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async getAll({ model }) {
|
|
40
|
+
return this.collections[model].find().lean().exec();
|
|
41
|
+
}
|
|
42
|
+
async get({ model, id }) {
|
|
43
|
+
return this.collections[model].findOne({ [this.getIdentifierKey(model)]: id }).lean();
|
|
44
|
+
}
|
|
45
|
+
async create({ model, data }) {
|
|
46
|
+
await new this.collections[model](data).save();
|
|
47
|
+
}
|
|
48
|
+
async remove({ model, id }) {
|
|
49
|
+
await this.collections[model].deleteOne({ [this.getIdentifierKey(model)]: id }).exec();
|
|
50
|
+
}
|
|
51
|
+
async update({ model, id, patches }) {
|
|
52
|
+
const identifierKey = this.getIdentifierKey(model);
|
|
53
|
+
const collection = await this.getCollection({ model, id });
|
|
54
|
+
const responses = await Promise.all(patches.map(async (patch) => {
|
|
55
|
+
switch (patch.type) {
|
|
56
|
+
case 'set': {
|
|
57
|
+
return collection.updateOne({ [identifierKey]: id }, { $set: { [patch.address]: patch.value } });
|
|
58
|
+
}
|
|
59
|
+
case 'push': {
|
|
60
|
+
return collection.updateOne({ [identifierKey]: id }, { $push: { [patch.address]: { $each: patch.items } } });
|
|
61
|
+
}
|
|
62
|
+
case 'pull': {
|
|
63
|
+
return collection.updateOne({ [identifierKey]: id }, { $pull: { [patch.address]: { $in: patch.items } } });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}));
|
|
67
|
+
for (const response of responses) {
|
|
68
|
+
if (!response.acknowledged) {
|
|
69
|
+
console.log('MongoDB Write Warning', { model, id, patches }, responses);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async getCollection({ model, id }) {
|
|
74
|
+
if (this.discriminatedModels[model]) {
|
|
75
|
+
const target = await this.get({ model, id });
|
|
76
|
+
const discriminatorValue = target[this.discriminatedModels[model].discriminatorKey];
|
|
77
|
+
return this.discriminatedModels[model].models[discriminatorValue];
|
|
78
|
+
}
|
|
79
|
+
return this.collections[model];
|
|
80
|
+
}
|
|
81
|
+
async exists({ model, id }) {
|
|
82
|
+
const result = await this.collections[model].exists({
|
|
83
|
+
[this.getIdentifierKey(model)]: id
|
|
84
|
+
});
|
|
85
|
+
return result !== null;
|
|
86
|
+
}
|
|
87
|
+
getIdentifierKey(model) {
|
|
88
|
+
return this.dataScheme.models[model].scheme.identifier.propertyKey;
|
|
89
|
+
}
|
|
90
|
+
useModel(model, schema) {
|
|
91
|
+
return this.connection.models[model] ?? this.connection.model(model, schema);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.MongoCollectionDriver = MongoCollectionDriver;
|
|
95
|
+
const createMongoDriver = (connection) => {
|
|
96
|
+
return (dataScheme) => {
|
|
97
|
+
return new MongoCollectionDriver(dataScheme, connection);
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
exports.createMongoDriver = createMongoDriver;
|
|
101
|
+
//# sourceMappingURL=collection.driver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection.driver.js","sourceRoot":"","sources":["../../lib/drivers/collection.driver.ts"],"names":[],"mappings":";;;AAMA,iDAAoD;AACpD,uCAAoD;AACpD,0EAAqE;AACrE,2CAAuC;AAWvC,MAAa,qBAAqB;IAM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U,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;IAC9E,CAAC;CACF;AAlID,sDAkIC;AAEM,MAAM,iBAAiB,GAAG,CAAC,UAAsB,EAAE,EAAE;IAC1D,OAAO,CAA6B,UAAyB,EAAE,EAAE;QAC/D,OAAO,IAAI,qBAAqB,CAAC,UAAU,EAAE,UAAU,CAAC,CAAA;IAC1D,CAAC,CAAA;AACH,CAAC,CAAA;AAJY,QAAA,iBAAiB,qBAI7B"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CollectionScheme, DataScheme, IType, RelationRecord, TableDriver, TableRow, TableRelationSlice } from '@companix/xeo-scheme';
|
|
2
|
+
import { Connection } from 'mongoose';
|
|
3
|
+
export declare class MongoRelationsTable<T extends CollectionScheme> implements TableDriver {
|
|
4
|
+
private readonly tables;
|
|
5
|
+
constructor(dataScheme: DataScheme<T>, connection: Connection);
|
|
6
|
+
createRecord(row: RelationRecord): Promise<void>;
|
|
7
|
+
removeRecord(row: RelationRecord): Promise<void>;
|
|
8
|
+
removeRecordsByModel(row: TableRelationSlice): Promise<void>;
|
|
9
|
+
getRecords({ tableName, modelSide, modelId }: TableRelationSlice): Promise<IType[]>;
|
|
10
|
+
getTables(): Promise<{
|
|
11
|
+
[name: string]: TableRow[];
|
|
12
|
+
}>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MongoRelationsTable = void 0;
|
|
4
|
+
const mongoose_1 = require("mongoose");
|
|
5
|
+
class MongoTableStore {
|
|
6
|
+
constructor({ rules, tableName }, connection) {
|
|
7
|
+
this.connection = connection;
|
|
8
|
+
this.modelKey = {};
|
|
9
|
+
this.keyMirror = { m1: 'm2', m2: 'm1' };
|
|
10
|
+
this.modelKey[rules.m1] = 'm1';
|
|
11
|
+
this.modelKey[rules.m2] = 'm2';
|
|
12
|
+
this.model = this.useModel(tableName, new mongoose_1.Schema(this.getSchemeDefinition()));
|
|
13
|
+
}
|
|
14
|
+
getSchemeDefinition() {
|
|
15
|
+
return {
|
|
16
|
+
m1: { type: mongoose_1.Schema.Types.Mixed, index: true, required: true },
|
|
17
|
+
m2: { type: mongoose_1.Schema.Types.Mixed, index: true, required: true }
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
getRow({ modelId, modelSide, oppositeId }) {
|
|
21
|
+
const key = this.modelKey[modelSide];
|
|
22
|
+
const row = { [key]: modelId, [this.keyMirror[key]]: oppositeId };
|
|
23
|
+
return row;
|
|
24
|
+
}
|
|
25
|
+
async addRow(row) {
|
|
26
|
+
await this.model.create(this.getRow(row));
|
|
27
|
+
}
|
|
28
|
+
async removeRow(row) {
|
|
29
|
+
await this.model.deleteOne(this.getRow(row)).exec();
|
|
30
|
+
}
|
|
31
|
+
async removeModel(model, modelId) {
|
|
32
|
+
await this.model.deleteMany({ [this.modelKey[model]]: modelId }).exec();
|
|
33
|
+
}
|
|
34
|
+
async getRelations(model, modelId) {
|
|
35
|
+
const key = this.modelKey[model];
|
|
36
|
+
const oppositeKey = this.keyMirror[key];
|
|
37
|
+
const rows = await this.model
|
|
38
|
+
.find({ [key]: modelId })
|
|
39
|
+
.select({ [oppositeKey]: 1, _id: 0 })
|
|
40
|
+
.lean()
|
|
41
|
+
.exec();
|
|
42
|
+
return rows.map((row) => row[oppositeKey]);
|
|
43
|
+
}
|
|
44
|
+
useModel(model, schema) {
|
|
45
|
+
return this.connection.models[model] ?? this.connection.model(model, schema);
|
|
46
|
+
}
|
|
47
|
+
getStore() {
|
|
48
|
+
return this.model.find().lean().exec();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
class MongoRelationsTable {
|
|
52
|
+
constructor(dataScheme, connection) {
|
|
53
|
+
this.tables = {};
|
|
54
|
+
for (const table of dataScheme.tables) {
|
|
55
|
+
this.tables[table.tableName] = new MongoTableStore(table, connection);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async createRecord(row) {
|
|
59
|
+
await this.tables[row.tableName].addRow(row);
|
|
60
|
+
}
|
|
61
|
+
async removeRecord(row) {
|
|
62
|
+
await this.tables[row.tableName].removeRow(row);
|
|
63
|
+
}
|
|
64
|
+
async removeRecordsByModel(row) {
|
|
65
|
+
await this.tables[row.tableName].removeModel(row.modelSide, row.modelId);
|
|
66
|
+
}
|
|
67
|
+
async getRecords({ tableName, modelSide, modelId }) {
|
|
68
|
+
return this.tables[tableName].getRelations(modelSide, modelId);
|
|
69
|
+
}
|
|
70
|
+
async getTables() {
|
|
71
|
+
const state = {};
|
|
72
|
+
for (const tableName in this.tables) {
|
|
73
|
+
state[tableName] = await this.tables[tableName].getStore();
|
|
74
|
+
}
|
|
75
|
+
return state;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
exports.MongoRelationsTable = MongoRelationsTable;
|
|
79
|
+
//# sourceMappingURL=table.driver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"table.driver.js","sourceRoot":"","sources":["../../lib/drivers/table.driver.ts"],"names":[],"mappings":";;;AAUA,uCAAsE;AAEtE,MAAM,eAAe;IAMnB,YAAY,EAAE,KAAK,EAAE,SAAS,EAAsB,EAAU,UAAsB;QAAtB,eAAU,GAAV,UAAU,CAAY;QAL5E,aAAQ,GAAqC,EAAE,CAAA;QAC/C,cAAS,GAAG,EAAE,EAAE,EAAE,IAAY,EAAE,EAAE,EAAE,IAAY,EAAE,CAAA;QAKxD,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI,CAAA;QAC9B,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI,CAAA;QAC9B,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAW,SAAS,EAAE,IAAI,iBAAM,CAAC,IAAI,CAAC,mBAAmB,EAAE,CAAC,CAAC,CAAA;IACzF,CAAC;IAE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,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,CAAA;IACjD,CAAC;IAED,KAAK,CAAC,oBAAoB,CAAC,GAAuB;QAChD,MAAM,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,SAAS,EAAE,GAAG,CAAC,OAAO,CAAC,CAAA;IAC1E,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,OAAO,EAAsB;QACpE,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,YAAY,CAAC,SAAS,EAAE,OAAO,CAAC,CAAA;IAChE,CAAC;IAID,KAAK,CAAC,SAAS;QACb,MAAM,KAAK,GAAmC,EAAE,CAAA;QAEhD,KAAK,MAAM,SAAS,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YACpC,KAAK,CAAC,SAAS,CAAC,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE,CAAA;QAC5D,CAAC;QAED,OAAO,KAAK,CAAA;IACd,CAAC;CACF;AApCD,kDAoCC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CollectionScheme, DataScheme, ModelProperties, ModelScheme } from '@companix/xeo-scheme';
|
|
2
|
+
import type { SchemaDefinition } from 'mongoose';
|
|
3
|
+
export declare class DefinitionsFactory<T extends CollectionScheme> {
|
|
4
|
+
private dataScheme;
|
|
5
|
+
constructor(dataScheme: DataScheme<T>);
|
|
6
|
+
createForCollection(name: keyof T): SchemaDefinition;
|
|
7
|
+
createForScheme(scheme: ModelScheme): SchemaDefinition;
|
|
8
|
+
createDefinitionScheme({ properties, references }: ModelProperties): SchemaDefinition;
|
|
9
|
+
private getPropertyDefinition;
|
|
10
|
+
private getRelationDefinition;
|
|
11
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DefinitionsFactory = void 0;
|
|
4
|
+
const mongoose_1 = require("mongoose");
|
|
5
|
+
const TypesTranslator = {
|
|
6
|
+
string: String,
|
|
7
|
+
number: Number,
|
|
8
|
+
json: Object,
|
|
9
|
+
boolean: Boolean
|
|
10
|
+
};
|
|
11
|
+
class DefinitionsFactory {
|
|
12
|
+
constructor(dataScheme) {
|
|
13
|
+
this.dataScheme = dataScheme;
|
|
14
|
+
}
|
|
15
|
+
createForCollection(name) {
|
|
16
|
+
return this.createForScheme(this.dataScheme.models[this.dataScheme.collections[name].name].scheme);
|
|
17
|
+
}
|
|
18
|
+
createForScheme(scheme) {
|
|
19
|
+
return {
|
|
20
|
+
[scheme.identifier.propertyKey]: {
|
|
21
|
+
index: true,
|
|
22
|
+
type: TypesTranslator[scheme.identifier.options.type],
|
|
23
|
+
unique: true
|
|
24
|
+
},
|
|
25
|
+
...this.createDefinitionScheme(scheme)
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
createDefinitionScheme({ properties, references }) {
|
|
29
|
+
const map = {};
|
|
30
|
+
for (const property of properties) {
|
|
31
|
+
map[property.propertyKey] = this.getPropertyDefinition(property);
|
|
32
|
+
}
|
|
33
|
+
for (const reference of references) {
|
|
34
|
+
map[reference.propertyKey] = this.getRelationDefinition(reference);
|
|
35
|
+
}
|
|
36
|
+
return map;
|
|
37
|
+
}
|
|
38
|
+
getPropertyDefinition({ primitive }) {
|
|
39
|
+
switch (primitive.type) {
|
|
40
|
+
case 'embedded': {
|
|
41
|
+
return {
|
|
42
|
+
_id: false,
|
|
43
|
+
type: this.createDefinitionScheme(primitive),
|
|
44
|
+
required: true
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
case 'array': {
|
|
48
|
+
return {
|
|
49
|
+
type: [TypesTranslator[primitive.itemType]],
|
|
50
|
+
required: true
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
case 'literal': {
|
|
54
|
+
return {
|
|
55
|
+
type: mongoose_1.Schema.Types.Mixed,
|
|
56
|
+
enum: primitive.values,
|
|
57
|
+
required: !primitive.nullable
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
case 'enum': {
|
|
61
|
+
return {
|
|
62
|
+
type: [String],
|
|
63
|
+
enum: primitive.values,
|
|
64
|
+
required: !primitive.nullable
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
default: {
|
|
68
|
+
return {
|
|
69
|
+
type: TypesTranslator[primitive.type]
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
getRelationDefinition(reference) {
|
|
75
|
+
const refModel = this.dataScheme.metadata.getModelSchemaByTarget(reference.referenceModel);
|
|
76
|
+
switch (reference.refType) {
|
|
77
|
+
case 'reference-to': {
|
|
78
|
+
return {
|
|
79
|
+
type: TypesTranslator[refModel.identifier.options.type],
|
|
80
|
+
required: (reference.options?.onRefDeleting ?? 'restrict') === 'restrict'
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
case 'reference-set': {
|
|
84
|
+
return {
|
|
85
|
+
type: [TypesTranslator[refModel.identifier.options.type]]
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
case 'belongs-to': {
|
|
89
|
+
return {
|
|
90
|
+
type: TypesTranslator[refModel.identifier.options.type],
|
|
91
|
+
required: true
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
case 'has-many': {
|
|
95
|
+
return {
|
|
96
|
+
type: [TypesTranslator[refModel.identifier.options.type]],
|
|
97
|
+
required: true
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
case 'owner': {
|
|
101
|
+
return {
|
|
102
|
+
type: TypesTranslator[refModel.identifier.options.type],
|
|
103
|
+
required: true
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
case 'owner-fallback': {
|
|
107
|
+
return {
|
|
108
|
+
type: TypesTranslator[refModel.identifier.options.type],
|
|
109
|
+
refType: 'owner-fallback'
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.DefinitionsFactory = DefinitionsFactory;
|
|
116
|
+
//# sourceMappingURL=definitions.factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"definitions.factory.js","sourceRoot":"","sources":["../../lib/factories/definitions.factory.ts"],"names":[],"mappings":";;;AASA,uCAAiC;AAEjC,MAAM,eAAe,GAAG;IACtB,MAAM,EAAE,MAAM;IACd,MAAM,EAAE,MAAM;IACd,IAAI,EAAE,MAAM;IACZ,OAAO,EAAE,OAAO;CACjB,CAAA;AAED,MAAa,kBAAkB;IAC7B,YAAoB,UAAyB;QAAzB,eAAU,GAAV,UAAU,CAAe;IAAG,CAAC;IAEjD,mBAAmB,CAAC,IAAa;QAC/B,OAAO,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAA;IACpG,CAAC;IAED,eAAe,CAAC,MAAmB;QACjC,OAAO;YACL,CAAC,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,EAAE;gBAC/B,KAAK,EAAE,IAAI;gBACX,IAAI,EAAE,eAAe,CAAC,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC;gBACrD,MAAM,EAAE,IAAI;aACb;YACD,GAAG,IAAI,CAAC,sBAAsB,CAAC,MAAM,CAAC;SACvC,CAAA;IACH,CAAC;IAED,sBAAsB,CAAC,EAAE,UAAU,EAAE,UAAU,EAAmB;QAChE,MAAM,GAAG,GAAqB,EAAE,CAAA;QAEhC,KAAK,MAAM,QAAQ,IAAI,UAAU,EAAE,CAAC;YAClC,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC,qBAAqB,CAAC,QAAQ,CAAC,CAAA;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YACH,CAAC;QACH,CAAC;IACH,CAAC;CACF;AA9GD,gDA8GC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./common"), exports);
|
|
18
|
+
__exportStar(require("./drivers/table.driver"), exports);
|
|
19
|
+
__exportStar(require("./driver.module"), exports);
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAAwB;AACxB,yDAAsC;AACtC,kDAA+B"}
|