@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.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import sql from 'mssql';
2
- import { StorageProvider, SearchProvider, SearchableEntity } from '@utaba/deep-memory/providers';
2
+ import { StorageProvider, EnsureSchemaResult, SearchProvider, SearchableEntity } from '@utaba/deep-memory/providers';
3
3
  import { StorageRepositoryConfig, StoredRepository, RepositoryFilter, PaginatedResult, StoredRepositorySummary, RepositoryUpdate, RepositoryStats, MemoryVocabulary, PaginationOptions, VocabularyChangeRecord, StoredEntity, StoredEntityUpdate, StorageFindQuery, StoredRelationship, RelationshipQueryOptions, StorageExploreOptions, StorageNeighbourhood, StoragePathOptions, StoragePathResult, StorageTimelineOptions, StorageTimelineResult, ExportChunk, ImportChunk, BulkImportResult, SearchOptions, SearchHit } from '@utaba/deep-memory/types';
4
4
 
5
5
  /** Configuration for SqlServerStorageProvider */
@@ -19,7 +19,15 @@ declare class SqlServerStorageProvider implements StorageProvider {
19
19
  private getPool;
20
20
  initialise(): Promise<void>;
21
21
  dispose(): Promise<void>;
22
- ensureSchema(): Promise<void>;
22
+ /**
23
+ * Ensure the target database exists. If constructed with a sql.config and the
24
+ * database does not yet exist, creates it via a temporary connection to master,
25
+ * then (re)connects the main pool to the newly created database.
26
+ *
27
+ * @returns true if the database was created, false if it already existed.
28
+ */
29
+ private ensureDatabase;
30
+ ensureSchema(): Promise<EnsureSchemaResult>;
23
31
  createRepository(config: StorageRepositoryConfig): Promise<StoredRepository>;
24
32
  getRepository(repositoryId: string): Promise<StoredRepository | null>;
25
33
  listRepositories(filter?: RepositoryFilter): Promise<PaginatedResult<StoredRepositorySummary>>;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import sql from 'mssql';
2
- import { StorageProvider, SearchProvider, SearchableEntity } from '@utaba/deep-memory/providers';
2
+ import { StorageProvider, EnsureSchemaResult, SearchProvider, SearchableEntity } from '@utaba/deep-memory/providers';
3
3
  import { StorageRepositoryConfig, StoredRepository, RepositoryFilter, PaginatedResult, StoredRepositorySummary, RepositoryUpdate, RepositoryStats, MemoryVocabulary, PaginationOptions, VocabularyChangeRecord, StoredEntity, StoredEntityUpdate, StorageFindQuery, StoredRelationship, RelationshipQueryOptions, StorageExploreOptions, StorageNeighbourhood, StoragePathOptions, StoragePathResult, StorageTimelineOptions, StorageTimelineResult, ExportChunk, ImportChunk, BulkImportResult, SearchOptions, SearchHit } from '@utaba/deep-memory/types';
4
4
 
5
5
  /** Configuration for SqlServerStorageProvider */
@@ -19,7 +19,15 @@ declare class SqlServerStorageProvider implements StorageProvider {
19
19
  private getPool;
20
20
  initialise(): Promise<void>;
21
21
  dispose(): Promise<void>;
22
- ensureSchema(): Promise<void>;
22
+ /**
23
+ * Ensure the target database exists. If constructed with a sql.config and the
24
+ * database does not yet exist, creates it via a temporary connection to master,
25
+ * then (re)connects the main pool to the newly created database.
26
+ *
27
+ * @returns true if the database was created, false if it already existed.
28
+ */
29
+ private ensureDatabase;
30
+ ensureSchema(): Promise<EnsureSchemaResult>;
23
31
  createRepository(config: StorageRepositoryConfig): Promise<StoredRepository>;
24
32
  getRepository(repositoryId: string): Promise<StoredRepository | null>;
25
33
  listRepositories(filter?: RepositoryFilter): Promise<PaginatedResult<StoredRepositorySummary>>;
package/dist/index.js CHANGED
@@ -431,7 +431,51 @@ var SqlServerStorageProvider = class {
431
431
  }
432
432
  this.pool = null;
433
433
  }
434
+ /**
435
+ * Ensure the target database exists. If constructed with a sql.config and the
436
+ * database does not yet exist, creates it via a temporary connection to master,
437
+ * then (re)connects the main pool to the newly created database.
438
+ *
439
+ * @returns true if the database was created, false if it already existed.
440
+ */
441
+ async ensureDatabase() {
442
+ if (this.config.connection instanceof sql.ConnectionPool) {
443
+ return false;
444
+ }
445
+ const cfg = this.config.connection;
446
+ const dbName = cfg.database;
447
+ if (!dbName) {
448
+ return false;
449
+ }
450
+ let created = false;
451
+ const masterPool = new sql.ConnectionPool({ ...cfg, database: "master" });
452
+ try {
453
+ await masterPool.connect();
454
+ const result = await masterPool.request().input("dbName", sql.NVarChar, dbName).query(
455
+ `SELECT COUNT(*) AS cnt FROM sys.databases WHERE name = @dbName`
456
+ );
457
+ const exists = (result.recordset[0]?.cnt ?? 0) > 0;
458
+ if (!exists) {
459
+ if (!/^[A-Za-z0-9_-]+$/.test(dbName)) {
460
+ throw new ProviderError(
461
+ `Invalid database name '${dbName}'. Only alphanumeric characters, hyphens, and underscores are allowed.`
462
+ );
463
+ }
464
+ await masterPool.request().query(`CREATE DATABASE [${dbName}]`);
465
+ created = true;
466
+ }
467
+ } finally {
468
+ await masterPool.close();
469
+ }
470
+ if (!this.pool || !this.pool.connected) {
471
+ this.pool = new sql.ConnectionPool(cfg);
472
+ this.ownsPool = true;
473
+ await this.pool.connect();
474
+ }
475
+ return created;
476
+ }
434
477
  async ensureSchema() {
478
+ const databaseCreated = await this.ensureDatabase();
435
479
  const pool = this.getPool();
436
480
  const metaCheck = await pool.request().query(
437
481
  `SELECT COUNT(*) AS cnt FROM sys.tables WHERE name = 'dm_meta' AND schema_id = SCHEMA_ID('${this.schema}')`
@@ -448,7 +492,12 @@ var SqlServerStorageProvider = class {
448
492
  );
449
493
  }
450
494
  if (currentVersion === SCHEMA_VERSION) {
451
- return;
495
+ return {
496
+ databaseCreated,
497
+ schemaCreated: false,
498
+ alreadyUpToDate: !databaseCreated,
499
+ schemaVersion: SCHEMA_VERSION
500
+ };
452
501
  }
453
502
  }
454
503
  const ddl = getSchemaSQL(this.schema);
@@ -463,6 +512,12 @@ var SqlServerStorageProvider = class {
463
512
  }
464
513
  }
465
514
  }
515
+ return {
516
+ databaseCreated,
517
+ schemaCreated: true,
518
+ alreadyUpToDate: false,
519
+ schemaVersion: SCHEMA_VERSION
520
+ };
466
521
  }
467
522
  // ─── Repository ──────────────────────────────────────────────────
468
523
  async createRepository(config) {