@utaba/deep-memory-storage-sqlserver 0.1.0 → 0.3.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) {
@@ -643,13 +698,23 @@ var SqlServerStorageProvider = class {
643
698
  if (!updated) throw new import_deep_memory.RepositoryNotFoundError(repositoryId);
644
699
  return updated;
645
700
  }
646
- async deleteRepository(repositoryId) {
701
+ async deleteRepository(repositoryId, _onProgress) {
647
702
  const pool = this.getPool();
648
703
  const result = await pool.request().input("id", import_mssql.default.UniqueIdentifier, repositoryId).query(`DELETE FROM ${this.t("dm_repositories")} WHERE [repository_id] = @id`);
649
704
  if (result.rowsAffected[0] === 0) {
650
705
  throw new import_deep_memory.RepositoryNotFoundError(repositoryId);
651
706
  }
652
707
  }
708
+ async deleteAllContents(repositoryId, _onProgress) {
709
+ await this.assertRepository(repositoryId);
710
+ const pool = this.getPool();
711
+ const relResult = await pool.request().input("id", import_mssql.default.UniqueIdentifier, repositoryId).query(`DELETE FROM ${this.t("dm_relationships")} WHERE [repository_id] = @id`);
712
+ const entityResult = await pool.request().input("id", import_mssql.default.UniqueIdentifier, repositoryId).query(`DELETE FROM ${this.t("dm_entities")} WHERE [repository_id] = @id`);
713
+ return {
714
+ deletedEntities: entityResult.rowsAffected[0] ?? 0,
715
+ deletedRelationships: relResult.rowsAffected[0] ?? 0
716
+ };
717
+ }
653
718
  async getRepositoryStats(repositoryId) {
654
719
  await this.assertRepository(repositoryId);
655
720
  const pool = this.getPool();
@@ -1339,7 +1404,7 @@ var SqlServerStorageProvider = class {
1339
1404
  }
1340
1405
  }
1341
1406
  }
1342
- async importBulk(repositoryId, data) {
1407
+ async importBulk(repositoryId, data, _options) {
1343
1408
  await this.assertRepository(repositoryId);
1344
1409
  const pool = this.getPool();
1345
1410
  let entitiesImported = 0;