@flusys/nestjs-core 7.1.0 → 8.0.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.
Files changed (69) hide show
  1. package/README.md +2 -2
  2. package/config/index.d.ts +1 -1
  3. package/constants/index.d.ts +1 -1
  4. package/docs/docs.config.d.ts +1 -1
  5. package/docs/index.d.ts +1 -1
  6. package/fesm/229.js +729 -0
  7. package/fesm/config/index.js +126 -1
  8. package/fesm/constants/index.js +29 -1
  9. package/fesm/docs/index.js +288 -1
  10. package/fesm/index.js +124 -5
  11. package/fesm/interfaces/index.js +18 -4
  12. package/fesm/migration/cli.js +19 -2
  13. package/fesm/migration/index.js +45 -3
  14. package/fesm/runtime.js +85 -0
  15. package/fesm/utils/index.js +131 -1
  16. package/index.d.ts +5 -5
  17. package/interfaces/app-config.interfaces.d.ts +1 -1
  18. package/interfaces/index.d.ts +4 -4
  19. package/interfaces/migration.interface.d.ts +2 -2
  20. package/migration/cli.d.ts +0 -1
  21. package/migration/datasource.factory.d.ts +1 -1
  22. package/migration/index.d.ts +3 -3
  23. package/migration/migration.cli.d.ts +1 -1
  24. package/migration/migration.runner.d.ts +1 -1
  25. package/package.json +24 -32
  26. package/utils/datasource-config.builder.d.ts +2 -2
  27. package/utils/index.d.ts +1 -1
  28. package/cjs/config/env-config.service.js +0 -162
  29. package/cjs/config/env-config.service.spec.js +0 -325
  30. package/cjs/config/index.js +0 -18
  31. package/cjs/constants/database.constants.js +0 -20
  32. package/cjs/constants/index.js +0 -18
  33. package/cjs/docs/docs.config.js +0 -276
  34. package/cjs/docs/docs.config.spec.js +0 -923
  35. package/cjs/docs/index.js +0 -18
  36. package/cjs/index.js +0 -23
  37. package/cjs/interfaces/app-config.interfaces.js +0 -4
  38. package/cjs/interfaces/base-entity.interface.js +0 -9
  39. package/cjs/interfaces/database.interface.js +0 -4
  40. package/cjs/interfaces/index.js +0 -21
  41. package/cjs/interfaces/migration.interface.js +0 -4
  42. package/cjs/migration/cli.js +0 -10
  43. package/cjs/migration/datasource.factory.js +0 -43
  44. package/cjs/migration/datasource.factory.spec.js +0 -113
  45. package/cjs/migration/index.js +0 -45
  46. package/cjs/migration/migration.cli.js +0 -231
  47. package/cjs/migration/migration.cli.spec.js +0 -293
  48. package/cjs/migration/migration.runner.js +0 -276
  49. package/cjs/migration/migration.runner.spec.js +0 -268
  50. package/cjs/utils/datasource-config.builder.js +0 -103
  51. package/cjs/utils/datasource-config.builder.spec.js +0 -232
  52. package/cjs/utils/index.js +0 -18
  53. package/fesm/config/env-config.service.js +0 -112
  54. package/fesm/config/env-config.service.spec.js +0 -321
  55. package/fesm/constants/database.constants.js +0 -2
  56. package/fesm/docs/docs.config.js +0 -258
  57. package/fesm/docs/docs.config.spec.js +0 -919
  58. package/fesm/interfaces/app-config.interfaces.js +0 -1
  59. package/fesm/interfaces/base-entity.interface.js +0 -6
  60. package/fesm/interfaces/database.interface.js +0 -1
  61. package/fesm/interfaces/migration.interface.js +0 -1
  62. package/fesm/migration/datasource.factory.js +0 -25
  63. package/fesm/migration/datasource.factory.spec.js +0 -109
  64. package/fesm/migration/migration.cli.js +0 -180
  65. package/fesm/migration/migration.cli.spec.js +0 -289
  66. package/fesm/migration/migration.runner.js +0 -202
  67. package/fesm/migration/migration.runner.spec.js +0 -264
  68. package/fesm/utils/datasource-config.builder.js +0 -84
  69. package/fesm/utils/datasource-config.builder.spec.js +0 -228
package/cjs/docs/index.js DELETED
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- _export_star(require("./docs.config"), exports);
6
- function _export_star(from, to) {
7
- Object.keys(from).forEach(function(k) {
8
- if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
9
- Object.defineProperty(to, k, {
10
- enumerable: true,
11
- get: function() {
12
- return from[k];
13
- }
14
- });
15
- }
16
- });
17
- return from;
18
- }
package/cjs/index.js DELETED
@@ -1,23 +0,0 @@
1
- // Interfaces
2
- "use strict";
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- _export_star(require("./interfaces"), exports);
7
- _export_star(require("./constants"), exports);
8
- _export_star(require("./utils"), exports);
9
- _export_star(require("./migration"), exports);
10
- _export_star(require("./config"), exports);
11
- function _export_star(from, to) {
12
- Object.keys(from).forEach(function(k) {
13
- if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
14
- Object.defineProperty(to, k, {
15
- enumerable: true,
16
- get: function() {
17
- return from[k];
18
- }
19
- });
20
- }
21
- });
22
- return from;
23
- }
@@ -1,4 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
@@ -1,9 +0,0 @@
1
- /**
2
- * Entity Contract Interfaces
3
- *
4
- * These interfaces define the standard fields that entities should have.
5
- * Use them as contracts when implementing new entities.
6
- */ /** Base entity with UUID, timestamps, and soft delete */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
@@ -1,4 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- _export_star(require("./app-config.interfaces"), exports);
6
- _export_star(require("./base-entity.interface"), exports);
7
- _export_star(require("./database.interface"), exports);
8
- _export_star(require("./migration.interface"), exports);
9
- function _export_star(from, to) {
10
- Object.keys(from).forEach(function(k) {
11
- if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
12
- Object.defineProperty(to, k, {
13
- enumerable: true,
14
- get: function() {
15
- return from[k];
16
- }
17
- });
18
- }
19
- });
20
- return from;
21
- }
@@ -1,4 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- const _migrationcli = require("./migration.cli");
7
- (0, _migrationcli.runMigrationCli)().catch((err)=>{
8
- console.error('CLI Error:', err.message);
9
- process.exit(1);
10
- });
@@ -1,43 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: Object.getOwnPropertyDescriptor(all, name).get
9
- });
10
- }
11
- _export(exports, {
12
- get createMigrationDataSource () {
13
- return createMigrationDataSource;
14
- },
15
- get initializeDataSource () {
16
- return initializeDataSource;
17
- }
18
- });
19
- const _typeorm = require("typeorm");
20
- const _typeormnamingstrategies = require("typeorm-naming-strategies");
21
- const _datasourceconfigbuilder = require("../utils/datasource-config.builder");
22
- function createMigrationDataSource(options) {
23
- const { config, tenantId } = options;
24
- const { database, tenant, tenantConfig } = (0, _datasourceconfigbuilder.getDatabaseForTenant)(config, tenantId);
25
- console.log(`\n📦 Database: ${database}${tenant ? ` [${tenant.name}]` : ' [default]'}`);
26
- console.log(`📁 Migrations: migrations/${tenantId || 'default'}/`);
27
- if (config.bootstrapAppConfig?.databaseMode === 'multi-tenant' && !tenantId && config.tenants && config.tenants.length > 0) {
28
- console.log(`\n💡 Multi-tenant mode. Available tenants:`);
29
- config.tenants.forEach((t)=>console.log(` - TENANT_ID=${t.id} → ${t.database}`));
30
- }
31
- const dbConfig = (0, _datasourceconfigbuilder.resolveDatabaseConnection)(config, tenantId);
32
- const entities = (0, _datasourceconfigbuilder.resolveEntities)(config, tenantConfig);
33
- const dsOptions = (0, _datasourceconfigbuilder.buildDataSourceOptions)(dbConfig, entities, (0, _datasourceconfigbuilder.getMigrationsGlobPattern)(config.migrationsPath, tenantId), config.migrationsTableName);
34
- return new _typeorm.DataSource({
35
- ...dsOptions,
36
- namingStrategy: new _typeormnamingstrategies.SnakeNamingStrategy()
37
- });
38
- }
39
- async function initializeDataSource(options) {
40
- const ds = createMigrationDataSource(options);
41
- await ds.initialize();
42
- return ds;
43
- }
@@ -1,113 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- const _datasourcefactory = require("./datasource.factory");
6
- const mockInitialize = jest.fn();
7
- const dataSourceCtor = jest.fn().mockImplementation((options)=>({
8
- options,
9
- initialize: mockInitialize
10
- }));
11
- jest.mock('typeorm', ()=>({
12
- DataSource: jest.fn().mockImplementation((options)=>dataSourceCtor(options))
13
- }));
14
- jest.mock('typeorm-naming-strategies', ()=>({
15
- SnakeNamingStrategy: jest.fn().mockImplementation(()=>({
16
- __brand: 'SnakeNamingStrategy'
17
- }))
18
- }));
19
- describe('datasource.factory', ()=>{
20
- const baseConfig = {
21
- defaultDatabaseConfig: {
22
- type: 'postgres',
23
- host: 'localhost',
24
- port: 5432,
25
- username: 'root',
26
- password: 'pw',
27
- database: 'flusys_default'
28
- },
29
- migrationsPath: 'migrations',
30
- entities: [
31
- Object
32
- ]
33
- };
34
- beforeEach(()=>{
35
- jest.clearAllMocks();
36
- mockInitialize.mockResolvedValue(undefined);
37
- });
38
- describe('createMigrationDataSource', ()=>{
39
- it('should build a DataSource with the resolved connection and a snake naming strategy', ()=>{
40
- const ds = (0, _datasourcefactory.createMigrationDataSource)({
41
- config: baseConfig
42
- });
43
- expect(dataSourceCtor).toHaveBeenCalledWith(expect.objectContaining({
44
- type: 'postgres',
45
- host: 'localhost',
46
- database: 'flusys_default',
47
- synchronize: false,
48
- migrations: [
49
- 'migrations/default/*{.ts,.js}'
50
- ]
51
- }));
52
- expect(ds.options.namingStrategy).toEqual({
53
- __brand: 'SnakeNamingStrategy'
54
- });
55
- });
56
- it('should resolve tenant-specific database and migrations path', ()=>{
57
- const config = {
58
- ...baseConfig,
59
- tenants: [
60
- {
61
- id: 'tenant-a',
62
- database: 'db_a'
63
- }
64
- ]
65
- };
66
- (0, _datasourcefactory.createMigrationDataSource)({
67
- config,
68
- tenantId: 'tenant-a'
69
- });
70
- expect(dataSourceCtor).toHaveBeenCalledWith(expect.objectContaining({
71
- database: 'db_a',
72
- migrations: [
73
- 'migrations/tenant-a/*{.ts,.js}'
74
- ]
75
- }));
76
- });
77
- it('should resolve entities via an entity resolver function when provided', ()=>{
78
- const resolver = jest.fn().mockReturnValue([
79
- Number,
80
- String
81
- ]);
82
- const config = {
83
- ...baseConfig,
84
- entities: resolver
85
- };
86
- (0, _datasourcefactory.createMigrationDataSource)({
87
- config
88
- });
89
- expect(resolver).toHaveBeenCalled();
90
- expect(dataSourceCtor).toHaveBeenCalledWith(expect.objectContaining({
91
- entities: [
92
- Number,
93
- String
94
- ]
95
- }));
96
- });
97
- });
98
- describe('initializeDataSource', ()=>{
99
- it('should create and initialize the DataSource', async ()=>{
100
- const ds = await (0, _datasourcefactory.initializeDataSource)({
101
- config: baseConfig
102
- });
103
- expect(mockInitialize).toHaveBeenCalledTimes(1);
104
- expect(ds).toBeDefined();
105
- });
106
- it('should propagate initialization errors', async ()=>{
107
- mockInitialize.mockRejectedValue(new Error('connection refused'));
108
- await expect((0, _datasourcefactory.initializeDataSource)({
109
- config: baseConfig
110
- })).rejects.toThrow('connection refused');
111
- });
112
- });
113
- });
@@ -1,45 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: Object.getOwnPropertyDescriptor(all, name).get
9
- });
10
- }
11
- _export(exports, {
12
- get createMigrationDataSource () {
13
- return _datasourcefactory.createMigrationDataSource;
14
- },
15
- get ensureMigrationsFolder () {
16
- return _migrationrunner.ensureMigrationsFolder;
17
- },
18
- get generateMigration () {
19
- return _migrationrunner.generateMigration;
20
- },
21
- get initializeDataSource () {
22
- return _datasourcefactory.initializeDataSource;
23
- },
24
- get migrationStatus () {
25
- return _migrationrunner.migrationStatus;
26
- },
27
- get resetDatabase () {
28
- return _migrationrunner.resetDatabase;
29
- },
30
- get revertMigration () {
31
- return _migrationrunner.revertMigration;
32
- },
33
- get runForAllTenants () {
34
- return _migrationrunner.runForAllTenants;
35
- },
36
- get runMigrationCli () {
37
- return _migrationcli.runMigrationCli;
38
- },
39
- get runMigrations () {
40
- return _migrationrunner.runMigrations;
41
- }
42
- });
43
- const _datasourcefactory = require("./datasource.factory");
44
- const _migrationcli = require("./migration.cli");
45
- const _migrationrunner = require("./migration.runner");
@@ -1,231 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "runMigrationCli", {
6
- enumerable: true,
7
- get: function() {
8
- return runMigrationCli;
9
- }
10
- });
11
- const _path = /*#__PURE__*/ _interop_require_wildcard(require("path"));
12
- const _config = require("../config");
13
- const _utils = require("../utils");
14
- const _migrationrunner = require("./migration.runner");
15
- function _getRequireWildcardCache(nodeInterop) {
16
- if (typeof WeakMap !== "function") return null;
17
- var cacheBabelInterop = new WeakMap();
18
- var cacheNodeInterop = new WeakMap();
19
- return (_getRequireWildcardCache = function(nodeInterop) {
20
- return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
21
- })(nodeInterop);
22
- }
23
- function _interop_require_wildcard(obj, nodeInterop) {
24
- if (!nodeInterop && obj && obj.__esModule) {
25
- return obj;
26
- }
27
- if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
28
- return {
29
- default: obj
30
- };
31
- }
32
- var cache = _getRequireWildcardCache(nodeInterop);
33
- if (cache && cache.has(obj)) {
34
- return cache.get(obj);
35
- }
36
- var newObj = {
37
- __proto__: null
38
- };
39
- var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
40
- for(var key in obj){
41
- if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
42
- var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
43
- if (desc && (desc.get || desc.set)) {
44
- Object.defineProperty(newObj, key, desc);
45
- } else {
46
- newObj[key] = obj[key];
47
- }
48
- }
49
- }
50
- newObj.default = obj;
51
- if (cache) {
52
- cache.set(obj, newObj);
53
- }
54
- return newObj;
55
- }
56
- function showHelp() {
57
- console.log(`
58
- Migration CLI
59
-
60
- Usage:
61
- npm run migration <command> [options]
62
-
63
- Commands:
64
- generate --name=<Name> Generate migration
65
- run Run pending migrations
66
- revert Revert last migration
67
- reset Drop db, wipe migration files, recreate db, generate + run initial migration (dev only)
68
- status Show migration status
69
- run:all Run for all tenants
70
- revert:all Revert for all tenants
71
- status:all Status for all tenants
72
-
73
- Options:
74
- --config=<path> Migration config file (default: src/persistence/migration.config.ts)
75
- --name=<Name> Migration name (for generate command)
76
- --force Required to run 'reset' when MODE is not 'dev'
77
-
78
- Environment:
79
- TENANT_ID Target tenant (default: uses default database)
80
- MIGRATION_CONFIG Default config path (overrides auto-detection)
81
-
82
- Examples:
83
- npm run migration generate --name=CreateUsers
84
- npm run migration -- generate --name=CreateUsers --config=src/persistence/migration.config.ts
85
- TENANT_ID=tenant1 npm run migration run
86
- npm run migration run:all
87
- npm run migration reset
88
- `);
89
- }
90
- function parseArgs() {
91
- const args = process.argv.slice(2);
92
- let command;
93
- let name;
94
- let configPath;
95
- let datasourcePath;
96
- let force = false;
97
- for(let i = 0; i < args.length; i++){
98
- const arg = args[i];
99
- if (arg.startsWith('--name=')) {
100
- name = arg.split('=')[1];
101
- } else if (arg.startsWith('--config=')) {
102
- configPath = arg.split('=')[1];
103
- } else if (arg.startsWith('--datasource=')) {
104
- datasourcePath = arg.split('=')[1];
105
- } else if (arg === '--force') {
106
- force = true;
107
- } else if (!arg.startsWith('-') && !command) {
108
- command = arg;
109
- }
110
- }
111
- return {
112
- command,
113
- name,
114
- configPath,
115
- datasourcePath,
116
- force
117
- };
118
- }
119
- async function loadConfig(configPath) {
120
- const absolutePath = _path.isAbsolute(configPath) ? configPath : _path.resolve(process.cwd(), configPath);
121
- // Use require() for CommonJS compatibility with ts-node
122
- // eslint-disable-next-line @typescript-eslint/no-require-imports
123
- const module = require(absolutePath);
124
- return module.migrationConfig || module.default?.migrationConfig || module;
125
- }
126
- async function runMigrationCli(configOrPath) {
127
- const { command, name, configPath: argConfigPath, datasourcePath, force } = parseArgs();
128
- const tenantId = _config.envConfig.getTenantId() ?? undefined;
129
- // npm passes --name=X as npm_config_name
130
- const migrationName = _config.envConfig.tryGetValue('npm_config_name') || _config.envConfig.tryGetValue('MIGRATION_NAME') || name;
131
- // Resolve config
132
- let config;
133
- let dsPath = datasourcePath;
134
- if (typeof configOrPath === 'string') {
135
- config = await loadConfig(configOrPath);
136
- dsPath = dsPath || configOrPath;
137
- } else if (configOrPath) {
138
- config = configOrPath;
139
- } else if (argConfigPath) {
140
- config = await loadConfig(argConfigPath);
141
- dsPath = dsPath || argConfigPath;
142
- } else {
143
- // Try paths in order: env var, then common defaults
144
- const envConfigPath = _config.envConfig.tryGetValue('MIGRATION_CONFIG');
145
- const defaultPaths = [
146
- envConfigPath,
147
- 'src/persistence/migration.config.ts',
148
- 'src/persistence/migration.config.js',
149
- 'persistence/migration.config.ts',
150
- 'persistence/migration.config.js'
151
- ].filter(Boolean);
152
- let loaded = false;
153
- for (const tryPath of defaultPaths){
154
- try {
155
- config = await loadConfig(tryPath);
156
- dsPath = dsPath || tryPath;
157
- loaded = true;
158
- break;
159
- } catch {
160
- // Try next path
161
- }
162
- }
163
- if (!loaded) {
164
- console.error('❌ Config not found. Use --config=<path> or set MIGRATION_CONFIG env var');
165
- console.error(' Tried paths:', defaultPaths.join(', '));
166
- showHelp();
167
- process.exit(1);
168
- }
169
- }
170
- if (!command) {
171
- showHelp();
172
- return;
173
- }
174
- // Multi-tenant batch commands
175
- if (command.endsWith(':all')) {
176
- if (config.bootstrapAppConfig?.databaseMode === 'single') {
177
- console.error('❌ Multi-tenant mode not enabled');
178
- process.exit(1);
179
- }
180
- const action = command.split(':')[0];
181
- const results = await (0, _migrationrunner.runForAllTenants)(config, action);
182
- if (results.some((r)=>!r.success)) process.exit(1);
183
- return;
184
- }
185
- // Show target info
186
- const { database } = (0, _utils.getDatabaseForTenant)(config, tenantId);
187
- const folder = (0, _utils.getMigrationsFolderPath)(config.migrationsPath, tenantId);
188
- console.log(`\n📦 Database: ${database}`);
189
- console.log(`📁 Migrations: ${_path.relative(process.cwd(), folder)}`);
190
- switch(command){
191
- case 'generate':
192
- if (!migrationName) {
193
- console.error('❌ Migration name required. Use --name=<Name>');
194
- process.exit(1);
195
- }
196
- await (0, _migrationrunner.generateMigration)(config, migrationName, tenantId, dsPath);
197
- break;
198
- case 'run':
199
- {
200
- const result = await (0, _migrationrunner.runMigrations)(config, tenantId);
201
- if (!result.success) process.exit(1);
202
- break;
203
- }
204
- case 'revert':
205
- {
206
- const result = await (0, _migrationrunner.revertMigration)(config, tenantId);
207
- if (!result.success) process.exit(1);
208
- break;
209
- }
210
- case 'reset':
211
- {
212
- if (_config.envConfig.isProduction() && !force) {
213
- console.error('❌ Refusing to reset database outside dev mode. Pass --force to override.');
214
- process.exit(1);
215
- }
216
- const result = await (0, _migrationrunner.resetDatabase)(config, tenantId);
217
- if (!result.success) process.exit(1);
218
- await (0, _migrationrunner.generateMigration)(config, migrationName || 'Init', tenantId, dsPath);
219
- const runResult = await (0, _migrationrunner.runMigrations)(config, tenantId);
220
- if (!runResult.success) process.exit(1);
221
- break;
222
- }
223
- case 'status':
224
- await (0, _migrationrunner.migrationStatus)(config, tenantId);
225
- break;
226
- default:
227
- console.error(`❌ Unknown command: ${command}`);
228
- showHelp();
229
- process.exit(1);
230
- }
231
- }