@nauth-toolkit/database-typeorm-mysql 0.1.25 → 0.1.27

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.
@@ -1,10 +1,16 @@
1
- import type { DataSource } from 'typeorm';
1
+ import { DataSource } from 'typeorm';
2
2
  import type { NAuthConfig, NAuthLogger } from '@nauth-toolkit/core';
3
3
  /**
4
4
  * Run nauth-toolkit migrations for MySQL.
5
5
  *
6
6
  * @remarks
7
- * This is invoked automatically by the core/nestjs packages at startup.
7
+ * This creates a completely isolated DataSource instance for nauth migrations,
8
+ * ensuring zero interference with consumer migrations. The consumer's DataSource
9
+ * is never modified or accessed for migration purposes.
10
+ *
11
+ * @param dataSource - Consumer's DataSource (only used to extract connection config)
12
+ * @param logger - NAuth logger instance
13
+ * @param config - NAuth configuration
8
14
  */
9
15
  export declare function runNAuthMigrations(dataSource: DataSource, logger: NAuthLogger, config: NAuthConfig): Promise<void>;
10
16
  //# sourceMappingURL=run-migrations.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"run-migrations.d.ts","sourceRoot":"","sources":["../../src/utils/run-migrations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAUpE;;;;;GAKG;AACH,wBAAsB,kBAAkB,CACtC,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,WAAW,EACnB,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,IAAI,CAAC,CA8Bf"}
1
+ {"version":3,"file":"run-migrations.d.ts","sourceRoot":"","sources":["../../src/utils/run-migrations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AA+DpE;;;;;;;;;;;GAWG;AACH,wBAAsB,kBAAkB,CACtC,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,WAAW,EACnB,MAAM,EAAE,WAAW,GAClB,OAAO,CAAC,IAAI,CAAC,CA8Cf"}
@@ -1,36 +1,94 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runNAuthMigrations = runNAuthMigrations;
4
+ const typeorm_1 = require("typeorm");
4
5
  const migrations_1 = require("../migrations");
6
+ const entities_1 = require("../entities");
5
7
  function getMigrationsTableName(config) {
6
8
  const tablePrefix = config.tablePrefix ?? 'nauth_';
7
9
  return `${tablePrefix}migrations`;
8
10
  }
11
+ /**
12
+ * Extracts connection configuration from consumer DataSource without touching it.
13
+ *
14
+ * @remarks
15
+ * This function only reads connection options and never modifies the consumer's DataSource.
16
+ * It supports both connection URL and individual connection parameters.
17
+ *
18
+ * @param dataSource - Consumer's DataSource instance
19
+ * @returns Connection configuration object
20
+ */
21
+ function extractConnectionConfig(dataSource) {
22
+ const options = dataSource.options;
23
+ const opts = options;
24
+ // If connection URL is provided, use it (takes precedence)
25
+ if (opts.url) {
26
+ return {
27
+ type: 'mysql',
28
+ url: opts.url,
29
+ extra: opts.extra,
30
+ };
31
+ }
32
+ // Otherwise, use individual connection parameters
33
+ return {
34
+ type: 'mysql',
35
+ host: opts.host,
36
+ port: opts.port,
37
+ username: opts.username,
38
+ password: opts.password,
39
+ database: opts.database,
40
+ extra: opts.extra,
41
+ };
42
+ }
9
43
  /**
10
44
  * Run nauth-toolkit migrations for MySQL.
11
45
  *
12
46
  * @remarks
13
- * This is invoked automatically by the core/nestjs packages at startup.
47
+ * This creates a completely isolated DataSource instance for nauth migrations,
48
+ * ensuring zero interference with consumer migrations. The consumer's DataSource
49
+ * is never modified or accessed for migration purposes.
50
+ *
51
+ * @param dataSource - Consumer's DataSource (only used to extract connection config)
52
+ * @param logger - NAuth logger instance
53
+ * @param config - NAuth configuration
14
54
  */
15
55
  async function runNAuthMigrations(dataSource, logger, config) {
16
56
  const migrationsTableName = getMigrationsTableName(config);
17
57
  logger.log(`[nauth-toolkit] Ensuring database schema via migrations (@nauth-toolkit/database-typeorm-mysql)...`);
18
- const existing = Array.isArray(dataSource.options.migrations) ? dataSource.options.migrations : [];
19
- const merged = [...existing, ...migrations_1.migrations];
20
- // Inject our migrations into the DataSource options, then rebuild metadatas so TypeORM creates Migration instances.
21
- // (Setting options alone is not enough after initialization; TypeORM uses `dataSource.migrations` built in buildMetadatas()).
22
- dataSource.options.migrations = merged;
23
- dataSource.options.migrationsTableName = migrationsTableName;
24
- await dataSource.buildMetadatas();
25
- logger.log(`[nauth-toolkit] Injecting ${migrations_1.migrations.length} NAuth migration(s) into DataSource (existing runtime: ${existing.length})`);
26
- logger.log('[nauth-toolkit] Checking for pending migrations...');
27
- const executed = (await dataSource.runMigrations({ transaction: 'all' }));
28
- if (!executed.length) {
29
- logger.log('[nauth-toolkit] No pending migrations.');
30
- return;
58
+ // Extract connection config from consumer DataSource (read-only, no modifications)
59
+ const connectionConfig = extractConnectionConfig(dataSource);
60
+ // Create a completely separate DataSource instance for nauth migrations only
61
+ // This ensures hard separation - consumer migrations are never touched
62
+ const nauthDataSource = new typeorm_1.DataSource({
63
+ ...connectionConfig,
64
+ entities: [...(0, entities_1.getNAuthEntities)(), ...(0, entities_1.getNAuthTransientStorageEntities)()],
65
+ migrations: migrations_1.migrations,
66
+ migrationsTableName,
67
+ synchronize: false,
68
+ logging: false,
69
+ });
70
+ try {
71
+ // Initialize the isolated nauth DataSource
72
+ await nauthDataSource.initialize();
73
+ logger.log(`[nauth-toolkit] Running ${migrations_1.migrations.length} NAuth migration(s) using isolated DataSource (table: ${migrationsTableName})`);
74
+ logger.log('[nauth-toolkit] Checking for pending migrations...');
75
+ // Run migrations on the isolated DataSource
76
+ const executed = (await nauthDataSource.runMigrations({
77
+ transaction: 'all',
78
+ }));
79
+ if (!executed.length) {
80
+ logger.log('[nauth-toolkit] No pending migrations.');
81
+ return;
82
+ }
83
+ logger.log(`[nauth-toolkit] Executed ${executed.length} migration(s):`);
84
+ for (const m of executed)
85
+ logger.log(` ${m.name}`);
86
+ }
87
+ finally {
88
+ // Always destroy the isolated DataSource to clean up connections
89
+ if (nauthDataSource.isInitialized) {
90
+ await nauthDataSource.destroy();
91
+ }
31
92
  }
32
- logger.log(`[nauth-toolkit] Executed ${executed.length} migration(s):`);
33
- for (const m of executed)
34
- logger.log(` ${m.name}`);
35
93
  }
36
94
  //# sourceMappingURL=run-migrations.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"run-migrations.js","sourceRoot":"","sources":["../../src/utils/run-migrations.ts"],"names":[],"mappings":";;AAiBA,gDAkCC;AAjDD,8CAA2C;AAI3C,SAAS,sBAAsB,CAAC,MAAmB;IACjD,MAAM,WAAW,GAAG,MAAM,CAAC,WAAW,IAAI,QAAQ,CAAC;IACnD,OAAO,GAAG,WAAW,YAAY,CAAC;AACpC,CAAC;AAED;;;;;GAKG;AACI,KAAK,UAAU,kBAAkB,CACtC,UAAsB,EACtB,MAAmB,EACnB,MAAmB;IAEnB,MAAM,mBAAmB,GAAG,sBAAsB,CAAC,MAAM,CAAC,CAAC;IAE3D,MAAM,CAAC,GAAG,CAAC,oGAAoG,CAAC,CAAC;IAEjH,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC;IACnG,MAAM,MAAM,GAAG,CAAC,GAAG,QAAQ,EAAE,GAAG,uBAAU,CAAC,CAAC;IAE5C,oHAAoH;IACpH,8HAA8H;IAC7H,UAAU,CAAC,OAAsC,CAAC,UAAU,GAAG,MAAmB,CAAC;IACnF,UAAU,CAAC,OAA4C,CAAC,mBAAmB,GAAG,mBAAmB,CAAC;IACnG,MAAO,UAAiE,CAAC,cAAc,EAAE,CAAC;IAE1F,MAAM,CAAC,GAAG,CACR,6BAA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AC;YACrD,OAAO;QACT,CAAC;QAED,MAAM,CAAC,GAAG,CAAC,4BAA4B,QAAQ,CAAC,MAAM,gBAAgB,CAAC,CAAC;QACxE,KAAK,MAAM,CAAC,IAAI,QAAQ;YAAE,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;IACtD,CAAC;YAAS,CAAC;QACT,iEAAiE;QACjE,IAAI,eAAe,CAAC,aAAa,EAAE,CAAC;YAClC,MAAM,eAAe,CAAC,OAAO,EAAE,CAAC;QAClC,CAAC;IACH,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nauth-toolkit/database-typeorm-mysql",
3
- "version": "0.1.25",
3
+ "version": "0.1.27",
4
4
  "description": "MySQL TypeORM adapter for nauth-toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -14,7 +14,7 @@
14
14
  "format:check": "prettier --check \"src/**/*.ts\""
15
15
  },
16
16
  "peerDependencies": {
17
- "@nauth-toolkit/core": "^0.1.25",
17
+ "@nauth-toolkit/core": "^0.1.27",
18
18
  "typeorm": "^0.3.20",
19
19
  "mysql2": "^2.3.0 || ^3.0.0"
20
20
  },