@utaba/deep-memory-storage-sqlserver 0.1.0 → 0.2.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/dist/index.cjs CHANGED
@@ -462,7 +462,51 @@ var SqlServerStorageProvider = class {
462
462
  }
463
463
  this.pool = null;
464
464
  }
465
+ /**
466
+ * Ensure the target database exists. If constructed with a sql.config and the
467
+ * database does not yet exist, creates it via a temporary connection to master,
468
+ * then (re)connects the main pool to the newly created database.
469
+ *
470
+ * @returns true if the database was created, false if it already existed.
471
+ */
472
+ async ensureDatabase() {
473
+ if (this.config.connection instanceof import_mssql.default.ConnectionPool) {
474
+ return false;
475
+ }
476
+ const cfg = this.config.connection;
477
+ const dbName = cfg.database;
478
+ if (!dbName) {
479
+ return false;
480
+ }
481
+ let created = false;
482
+ const masterPool = new import_mssql.default.ConnectionPool({ ...cfg, database: "master" });
483
+ try {
484
+ await masterPool.connect();
485
+ const result = await masterPool.request().input("dbName", import_mssql.default.NVarChar, dbName).query(
486
+ `SELECT COUNT(*) AS cnt FROM sys.databases WHERE name = @dbName`
487
+ );
488
+ const exists = (result.recordset[0]?.cnt ?? 0) > 0;
489
+ if (!exists) {
490
+ if (!/^[A-Za-z0-9_-]+$/.test(dbName)) {
491
+ throw new import_deep_memory.ProviderError(
492
+ `Invalid database name '${dbName}'. Only alphanumeric characters, hyphens, and underscores are allowed.`
493
+ );
494
+ }
495
+ await masterPool.request().query(`CREATE DATABASE [${dbName}]`);
496
+ created = true;
497
+ }
498
+ } finally {
499
+ await masterPool.close();
500
+ }
501
+ if (!this.pool || !this.pool.connected) {
502
+ this.pool = new import_mssql.default.ConnectionPool(cfg);
503
+ this.ownsPool = true;
504
+ await this.pool.connect();
505
+ }
506
+ return created;
507
+ }
465
508
  async ensureSchema() {
509
+ const databaseCreated = await this.ensureDatabase();
466
510
  const pool = this.getPool();
467
511
  const metaCheck = await pool.request().query(
468
512
  `SELECT COUNT(*) AS cnt FROM sys.tables WHERE name = 'dm_meta' AND schema_id = SCHEMA_ID('${this.schema}')`
@@ -479,7 +523,12 @@ var SqlServerStorageProvider = class {
479
523
  );
480
524
  }
481
525
  if (currentVersion === SCHEMA_VERSION) {
482
- return;
526
+ return {
527
+ databaseCreated,
528
+ schemaCreated: false,
529
+ alreadyUpToDate: !databaseCreated,
530
+ schemaVersion: SCHEMA_VERSION
531
+ };
483
532
  }
484
533
  }
485
534
  const ddl = getSchemaSQL(this.schema);
@@ -494,6 +543,12 @@ var SqlServerStorageProvider = class {
494
543
  }
495
544
  }
496
545
  }
546
+ return {
547
+ databaseCreated,
548
+ schemaCreated: true,
549
+ alreadyUpToDate: false,
550
+ schemaVersion: SCHEMA_VERSION
551
+ };
497
552
  }
498
553
  // ─── Repository ──────────────────────────────────────────────────
499
554
  async createRepository(config) {