@memberjunction/ai-vectors-sqlserver 6.2.0-edge.0 → 6.2.0-edge.1
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.
|
@@ -30,28 +30,28 @@ export declare class SQLServerVectorDatabase extends VectorDBBase {
|
|
|
30
30
|
constructor(apiKey: string);
|
|
31
31
|
get SupportsColocatedQuery(): boolean;
|
|
32
32
|
get SupportsHybridSearch(): boolean;
|
|
33
|
-
private get
|
|
34
|
-
private get
|
|
35
|
-
private
|
|
36
|
-
private
|
|
37
|
-
private
|
|
38
|
-
private
|
|
39
|
-
private
|
|
33
|
+
private get host();
|
|
34
|
+
private get schema();
|
|
35
|
+
private qualify;
|
|
36
|
+
private run;
|
|
37
|
+
private assertVectorCapable;
|
|
38
|
+
private ensureInfrastructure;
|
|
39
|
+
private getResolvedIndex;
|
|
40
40
|
/** Validate & narrow the untyped `CreateIndexParams.additionalParams` bag into a typed config,
|
|
41
41
|
* throwing on malformed shapes rather than asserting (`as`) a shape that may not hold. */
|
|
42
|
-
private
|
|
43
|
-
private
|
|
42
|
+
private normalizeIndexConfig;
|
|
43
|
+
private parseSelectColumns;
|
|
44
44
|
/** Resolve the schema-qualified table string for an entityColumn source spec, validating each
|
|
45
45
|
* identifier segment (accepts `[schema].[table]`, `schema.table`, or bare `table`). */
|
|
46
|
-
private
|
|
46
|
+
private qualifyEntityTable;
|
|
47
47
|
ListIndexes(): Promise<IndexList>;
|
|
48
48
|
GetIndex(params: BaseRequestParams): Promise<BaseResponse>;
|
|
49
49
|
CreateIndex(params: CreateIndexParams): Promise<BaseResponse>;
|
|
50
|
-
private
|
|
51
|
-
private
|
|
50
|
+
private createSiblingIndex;
|
|
51
|
+
private registerEntityColumnIndex;
|
|
52
52
|
/** Attempt to create a DiskANN vector index; tolerate failure (preview surface / pre-existing). */
|
|
53
|
-
private
|
|
54
|
-
private
|
|
53
|
+
private tryCreateVectorIndex;
|
|
54
|
+
private upsertRegistry;
|
|
55
55
|
DeleteIndex(params: BaseRequestParams): Promise<BaseResponse>;
|
|
56
56
|
EditIndex(_params: EditIndexParams): Promise<BaseResponse>;
|
|
57
57
|
/**
|
|
@@ -64,7 +64,7 @@ export declare class SQLServerVectorDatabase extends VectorDBBase {
|
|
|
64
64
|
* Servers (parallel client-to-many-servers, or mixed 2022/2025 backends), and capability differs
|
|
65
65
|
* per server — a process-global flag would let one server's probe poison another's path.
|
|
66
66
|
*/
|
|
67
|
-
private static
|
|
67
|
+
private static approximateSearchSupportedByHost;
|
|
68
68
|
ColocatedQuery(params: ColocatedQueryOptions, _contextUser?: UserInfo): Promise<ColocatedQueryResult>;
|
|
69
69
|
/**
|
|
70
70
|
* Execute the vector search, choosing the exact vs. approximate path and transparently falling
|
|
@@ -77,33 +77,33 @@ export declare class SQLServerVectorDatabase extends VectorDBBase {
|
|
|
77
77
|
* - Else attempt approximate; if it errors with a "feature not supported" signature, cache that
|
|
78
78
|
* fact and retry exact.
|
|
79
79
|
*/
|
|
80
|
-
private
|
|
80
|
+
private runVectorQuery;
|
|
81
81
|
/** Best-effort heuristic: does this error indicate the VECTOR_SEARCH/DiskANN surface is absent on
|
|
82
82
|
* this server? Matches on message text (brittle across CUs/locales), but only gates a fallback to
|
|
83
83
|
* an equally-correct exact query, so a false negative just rethrows and a false positive is slow,
|
|
84
84
|
* not wrong. */
|
|
85
|
-
private
|
|
85
|
+
private isApproximateUnsupportedError;
|
|
86
86
|
/** Map a query result row to a ColocatedMatch, synthesizing metadata per storage mode. */
|
|
87
|
-
private
|
|
88
|
-
private
|
|
87
|
+
private rowToMatch;
|
|
88
|
+
private toMetadataValue;
|
|
89
89
|
QueryIndex(params: QueryOptions, contextUser?: UserInfo): Promise<BaseResponse>;
|
|
90
90
|
CreateRecord(record: VectorRecord, indexName?: string): Promise<BaseResponse>;
|
|
91
91
|
CreateRecords(records: VectorRecord[], indexName?: string): Promise<BaseResponse>;
|
|
92
|
-
private
|
|
92
|
+
private upsertRecords;
|
|
93
93
|
/** Extract the scalar key value from a vector record id (CompositeKey URL `Field|Value`). */
|
|
94
|
-
private
|
|
94
|
+
private extractKeyValue;
|
|
95
95
|
UpdateRecord(record: UpdateOptions): Promise<BaseResponse>;
|
|
96
96
|
UpdateRecords(records: UpdateOptions): Promise<BaseResponse>;
|
|
97
97
|
DeleteRecord(record: VectorRecord, indexName?: string): Promise<BaseResponse>;
|
|
98
98
|
DeleteRecords(records: VectorRecord[], indexName?: string): Promise<BaseResponse>;
|
|
99
|
-
private
|
|
99
|
+
private deleteByKeys;
|
|
100
100
|
DeleteAllRecords(indexName: string, _namespace?: string): Promise<BaseResponse>;
|
|
101
101
|
GetRecord(params: BaseRequestParams): Promise<BaseResponse>;
|
|
102
102
|
GetRecords(params: BaseRequestParams): Promise<BaseResponse>;
|
|
103
103
|
ListVectorIDs(params: ListVectorIDsParams): Promise<ListVectorIDsResult>;
|
|
104
104
|
BuildMetadataFilter(options: SharedIndexFilterOptions): object | undefined;
|
|
105
|
-
private
|
|
106
|
-
private
|
|
107
|
-
private
|
|
105
|
+
private parseMetadata;
|
|
106
|
+
private success;
|
|
107
|
+
private failure;
|
|
108
108
|
}
|
|
109
109
|
//# sourceMappingURL=SQLServerVectorDatabase.d.ts.map
|
|
@@ -55,26 +55,26 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
55
55
|
// ----------------------------------------------------------------
|
|
56
56
|
// Host connection access
|
|
57
57
|
// ----------------------------------------------------------------
|
|
58
|
-
get
|
|
58
|
+
get host() {
|
|
59
59
|
if (!this.ColocatedHost) {
|
|
60
60
|
throw new Error('SQLServerVectorDatabase requires a host connection. Call TryWireColocatedHost()/SetColocatedHost() with the active data provider before use.');
|
|
61
61
|
}
|
|
62
62
|
return this.ColocatedHost;
|
|
63
63
|
}
|
|
64
|
-
get
|
|
65
|
-
return this.
|
|
64
|
+
get schema() {
|
|
65
|
+
return this.host.ColocatedSchema;
|
|
66
66
|
}
|
|
67
|
-
|
|
68
|
-
return `[${ValidateSqlIdentifier(this.
|
|
67
|
+
qualify(tableName) {
|
|
68
|
+
return `[${ValidateSqlIdentifier(this.schema, 'schema')}].[${ValidateSqlIdentifier(tableName, 'table')}]`;
|
|
69
69
|
}
|
|
70
|
-
|
|
71
|
-
return this.
|
|
70
|
+
run(sql, params) {
|
|
71
|
+
return this.host.RunColocatedSQL(sql, params);
|
|
72
72
|
}
|
|
73
73
|
// ----------------------------------------------------------------
|
|
74
74
|
// Capability detection (fail loud)
|
|
75
75
|
// ----------------------------------------------------------------
|
|
76
|
-
async
|
|
77
|
-
const rows = await this.
|
|
76
|
+
async assertVectorCapable() {
|
|
77
|
+
const rows = await this.run(`SELECT CAST(SERVERPROPERTY('ProductMajorVersion') AS INT) AS v`);
|
|
78
78
|
const major = rows[0]?.v != null ? Number(rows[0].v) : 0;
|
|
79
79
|
if (!(major >= SQLSERVER_2025_MAJOR_VERSION)) {
|
|
80
80
|
throw new Error(`Colocated SQL Server vectors are unavailable: the host SQL Server major version is ${major}, ` +
|
|
@@ -82,10 +82,10 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
82
82
|
`Upgrade the server or configure this index to use an external vector database instead.`);
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
-
async
|
|
86
|
-
await this.
|
|
87
|
-
await this.
|
|
88
|
-
CREATE TABLE ${this.
|
|
85
|
+
async ensureInfrastructure() {
|
|
86
|
+
await this.assertVectorCapable();
|
|
87
|
+
await this.run(`IF OBJECT_ID('${this.schema}.${INDEX_REGISTRY_TABLE}') IS NULL
|
|
88
|
+
CREATE TABLE ${this.qualify(INDEX_REGISTRY_TABLE)} (
|
|
89
89
|
name NVARCHAR(450) NOT NULL PRIMARY KEY,
|
|
90
90
|
dimension INT NOT NULL,
|
|
91
91
|
metric NVARCHAR(50) NOT NULL CONSTRAINT DF_${INDEX_REGISTRY_TABLE}_metric DEFAULT 'cosine',
|
|
@@ -102,19 +102,19 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
102
102
|
// ----------------------------------------------------------------
|
|
103
103
|
// Registry resolution
|
|
104
104
|
// ----------------------------------------------------------------
|
|
105
|
-
async
|
|
106
|
-
const rows = await this.
|
|
105
|
+
async getResolvedIndex(indexName) {
|
|
106
|
+
const rows = await this.run(`SELECT dimension, metric, storage_mode, source_table, vector_column, key_column,
|
|
107
107
|
entity_name, select_columns, iterative_threshold
|
|
108
|
-
FROM ${this.
|
|
108
|
+
FROM ${this.qualify(INDEX_REGISTRY_TABLE)} WHERE name = @p0`, [indexName]);
|
|
109
109
|
if (rows.length === 0) {
|
|
110
110
|
return null;
|
|
111
111
|
}
|
|
112
112
|
const row = rows[0];
|
|
113
113
|
const storageMode = row.storage_mode === 'entityColumn' ? 'entityColumn' : 'sibling';
|
|
114
|
-
const selectColumns = this.
|
|
114
|
+
const selectColumns = this.parseSelectColumns(row.select_columns, storageMode);
|
|
115
115
|
const qualifiedTable = row.source_table && row.source_table.trim().length > 0
|
|
116
116
|
? row.source_table
|
|
117
|
-
: this.
|
|
117
|
+
: this.qualify(indexName);
|
|
118
118
|
return {
|
|
119
119
|
dimension: Number(row.dimension),
|
|
120
120
|
metric: row.metric,
|
|
@@ -133,7 +133,7 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
133
133
|
}
|
|
134
134
|
/** Validate & narrow the untyped `CreateIndexParams.additionalParams` bag into a typed config,
|
|
135
135
|
* throwing on malformed shapes rather than asserting (`as`) a shape that may not hold. */
|
|
136
|
-
|
|
136
|
+
normalizeIndexConfig(raw) {
|
|
137
137
|
const obj = raw && typeof raw === 'object' ? raw : {};
|
|
138
138
|
const cfg = {};
|
|
139
139
|
if (obj['storageMode'] === 'entityColumn' || obj['storageMode'] === 'sibling') {
|
|
@@ -164,7 +164,7 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
164
164
|
cfg.createVectorIndex = obj['createVectorIndex'];
|
|
165
165
|
return cfg;
|
|
166
166
|
}
|
|
167
|
-
|
|
167
|
+
parseSelectColumns(raw, storageMode) {
|
|
168
168
|
if (storageMode === 'sibling') {
|
|
169
169
|
return ['metadata'];
|
|
170
170
|
}
|
|
@@ -181,9 +181,9 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
181
181
|
}
|
|
182
182
|
/** Resolve the schema-qualified table string for an entityColumn source spec, validating each
|
|
183
183
|
* identifier segment (accepts `[schema].[table]`, `schema.table`, or bare `table`). */
|
|
184
|
-
|
|
184
|
+
qualifyEntityTable(cfg) {
|
|
185
185
|
const raw = (cfg.sourceTable ?? '').trim().replace(/[[\]]/g, '');
|
|
186
|
-
const parts = raw.includes('.') ? raw.split('.') : [cfg.schema ?? this.
|
|
186
|
+
const parts = raw.includes('.') ? raw.split('.') : [cfg.schema ?? this.schema, raw];
|
|
187
187
|
if (parts.length !== 2 || !parts[0] || !parts[1]) {
|
|
188
188
|
throw new Error(`Invalid sourceTable ${JSON.stringify(cfg.sourceTable)} — expected "schema.table" or "table".`);
|
|
189
189
|
}
|
|
@@ -194,13 +194,13 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
194
194
|
// ----------------------------------------------------------------
|
|
195
195
|
async ListIndexes() {
|
|
196
196
|
try {
|
|
197
|
-
await this.
|
|
198
|
-
const rows = await this.
|
|
197
|
+
await this.ensureInfrastructure();
|
|
198
|
+
const rows = await this.run(`SELECT name, dimension, metric FROM ${this.qualify(INDEX_REGISTRY_TABLE)} ORDER BY name`);
|
|
199
199
|
const indexes = rows.map(row => ({
|
|
200
200
|
name: row.name,
|
|
201
201
|
dimension: Number(row.dimension),
|
|
202
202
|
metric: row.metric,
|
|
203
|
-
host: this.
|
|
203
|
+
host: this.schema,
|
|
204
204
|
}));
|
|
205
205
|
return { indexes };
|
|
206
206
|
}
|
|
@@ -211,92 +211,92 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
211
211
|
}
|
|
212
212
|
async GetIndex(params) {
|
|
213
213
|
try {
|
|
214
|
-
await this.
|
|
215
|
-
const resolved = await this.
|
|
214
|
+
await this.ensureInfrastructure();
|
|
215
|
+
const resolved = await this.getResolvedIndex(params.id);
|
|
216
216
|
if (!resolved) {
|
|
217
|
-
return this.
|
|
217
|
+
return this.failure(`Index "${params.id}" not found`);
|
|
218
218
|
}
|
|
219
219
|
const desc = {
|
|
220
220
|
name: params.id,
|
|
221
221
|
dimension: resolved.dimension,
|
|
222
222
|
metric: resolved.metric,
|
|
223
|
-
host: this.
|
|
223
|
+
host: this.schema,
|
|
224
224
|
};
|
|
225
|
-
return this.
|
|
225
|
+
return this.success(desc);
|
|
226
226
|
}
|
|
227
227
|
catch (ex) {
|
|
228
228
|
LogError('SQLServerVectorDatabase.GetIndex error', undefined, ex);
|
|
229
|
-
return this.
|
|
229
|
+
return this.failure('Error getting index');
|
|
230
230
|
}
|
|
231
231
|
}
|
|
232
232
|
async CreateIndex(params) {
|
|
233
233
|
try {
|
|
234
|
-
await this.
|
|
234
|
+
await this.ensureInfrastructure();
|
|
235
235
|
ValidateSqlIdentifier(params.id, 'index name');
|
|
236
|
-
const cfg = this.
|
|
236
|
+
const cfg = this.normalizeIndexConfig(params.additionalParams);
|
|
237
237
|
const storageMode = cfg.storageMode === 'entityColumn' ? 'entityColumn' : 'sibling';
|
|
238
238
|
const dimension = params.dimension;
|
|
239
239
|
const metric = params.metric || 'cosine';
|
|
240
240
|
if (storageMode === 'entityColumn') {
|
|
241
|
-
return await this.
|
|
241
|
+
return await this.registerEntityColumnIndex(params.id, dimension, metric, cfg);
|
|
242
242
|
}
|
|
243
|
-
return await this.
|
|
243
|
+
return await this.createSiblingIndex(params.id, dimension, metric, cfg);
|
|
244
244
|
}
|
|
245
245
|
catch (ex) {
|
|
246
246
|
LogError('SQLServerVectorDatabase.CreateIndex error', undefined, ex);
|
|
247
|
-
return this.
|
|
247
|
+
return this.failure(ex instanceof Error ? ex.message : 'Error creating index');
|
|
248
248
|
}
|
|
249
249
|
}
|
|
250
|
-
async
|
|
251
|
-
await this.
|
|
252
|
-
CREATE TABLE ${this.
|
|
250
|
+
async createSiblingIndex(name, dimension, metric, cfg) {
|
|
251
|
+
await this.run(`IF OBJECT_ID('${this.schema}.${name}') IS NULL
|
|
252
|
+
CREATE TABLE ${this.qualify(name)} (
|
|
253
253
|
id NVARCHAR(450) NOT NULL PRIMARY KEY,
|
|
254
254
|
embedding VECTOR(${dimension}) NULL,
|
|
255
255
|
metadata NVARCHAR(MAX) NULL,
|
|
256
256
|
content NVARCHAR(MAX) NULL
|
|
257
257
|
)`);
|
|
258
258
|
if (cfg.createVectorIndex !== false) {
|
|
259
|
-
await this.
|
|
259
|
+
await this.tryCreateVectorIndex(name, this.qualify(name), 'embedding', metric);
|
|
260
260
|
}
|
|
261
|
-
await this.
|
|
261
|
+
await this.upsertRegistry(name, {
|
|
262
262
|
dimension, metric, storageMode: 'sibling',
|
|
263
|
-
sourceTable: this.
|
|
263
|
+
sourceTable: this.qualify(name), vectorColumn: 'embedding', keyColumn: 'id',
|
|
264
264
|
entityName: null, selectColumns: ['metadata'], threshold: cfg.iterativeFilterThreshold ?? null,
|
|
265
265
|
});
|
|
266
266
|
LogStatus(`SQLServerVectorDatabase: Created sibling index "${name}" (dim=${dimension}, metric=${metric})`);
|
|
267
|
-
return this.
|
|
267
|
+
return this.success({ name, dimension, metric, storageMode: 'sibling' });
|
|
268
268
|
}
|
|
269
|
-
async
|
|
269
|
+
async registerEntityColumnIndex(name, dimension, metric, cfg) {
|
|
270
270
|
if (!cfg.sourceTable) {
|
|
271
|
-
return this.
|
|
271
|
+
return this.failure('entityColumn storage mode requires additionalParams.sourceTable');
|
|
272
272
|
}
|
|
273
|
-
const qualified = this.
|
|
273
|
+
const qualified = this.qualifyEntityTable(cfg);
|
|
274
274
|
const vectorColumn = ValidateSqlIdentifier(cfg.vectorColumn ?? 'Embedding', 'vectorColumn');
|
|
275
275
|
const keyColumn = ValidateSqlIdentifier(cfg.keyColumn ?? 'ID', 'keyColumn');
|
|
276
276
|
if (cfg.createVectorIndex !== false) {
|
|
277
|
-
await this.
|
|
277
|
+
await this.tryCreateVectorIndex(name, qualified, vectorColumn, metric);
|
|
278
278
|
}
|
|
279
|
-
await this.
|
|
279
|
+
await this.upsertRegistry(name, {
|
|
280
280
|
dimension, metric, storageMode: 'entityColumn',
|
|
281
281
|
sourceTable: qualified, vectorColumn, keyColumn,
|
|
282
282
|
entityName: cfg.entityName ?? null, selectColumns: cfg.selectColumns ?? [],
|
|
283
283
|
threshold: cfg.iterativeFilterThreshold ?? null,
|
|
284
284
|
});
|
|
285
285
|
LogStatus(`SQLServerVectorDatabase: Registered entityColumn index "${name}" → ${qualified}.${vectorColumn} (key=${keyColumn})`);
|
|
286
|
-
return this.
|
|
286
|
+
return this.success({ name, dimension, metric, storageMode: 'entityColumn', sourceTable: qualified });
|
|
287
287
|
}
|
|
288
288
|
/** Attempt to create a DiskANN vector index; tolerate failure (preview surface / pre-existing). */
|
|
289
|
-
async
|
|
289
|
+
async tryCreateVectorIndex(indexName, qualifiedTable, vectorColumn, metric) {
|
|
290
290
|
try {
|
|
291
|
-
await this.
|
|
291
|
+
await this.run(`CREATE VECTOR INDEX [VIX_${indexName}_embedding] ON ${qualifiedTable}([${vectorColumn}])
|
|
292
292
|
WITH (METRIC = '${metric}', TYPE = 'DiskANN')`);
|
|
293
293
|
}
|
|
294
294
|
catch (ex) {
|
|
295
295
|
LogStatus(`SQLServerVectorDatabase: vector index not created for "${indexName}" (continuing): ${ex instanceof Error ? ex.message : String(ex)}`);
|
|
296
296
|
}
|
|
297
297
|
}
|
|
298
|
-
async
|
|
299
|
-
await this.
|
|
298
|
+
async upsertRegistry(name, r) {
|
|
299
|
+
await this.run(`MERGE ${this.qualify(INDEX_REGISTRY_TABLE)} AS t
|
|
300
300
|
USING (SELECT @p0 AS name) AS s ON t.name = s.name
|
|
301
301
|
WHEN MATCHED THEN UPDATE SET dimension=@p1, metric=@p2, storage_mode=@p3, source_table=@p4,
|
|
302
302
|
vector_column=@p5, key_column=@p6, entity_name=@p7, select_columns=@p8, iterative_threshold=@p9
|
|
@@ -306,22 +306,22 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
306
306
|
}
|
|
307
307
|
async DeleteIndex(params) {
|
|
308
308
|
try {
|
|
309
|
-
await this.
|
|
310
|
-
const resolved = await this.
|
|
309
|
+
await this.ensureInfrastructure();
|
|
310
|
+
const resolved = await this.getResolvedIndex(params.id);
|
|
311
311
|
// Only drop a table we own (sibling mode). entityColumn tables belong to the app.
|
|
312
312
|
if (resolved?.storageMode === 'sibling') {
|
|
313
|
-
await this.
|
|
313
|
+
await this.run(`IF OBJECT_ID('${this.schema}.${params.id}') IS NOT NULL DROP TABLE ${this.qualify(params.id)}`);
|
|
314
314
|
}
|
|
315
|
-
await this.
|
|
316
|
-
return this.
|
|
315
|
+
await this.run(`DELETE FROM ${this.qualify(INDEX_REGISTRY_TABLE)} WHERE name = @p0`, [params.id]);
|
|
316
|
+
return this.success(null);
|
|
317
317
|
}
|
|
318
318
|
catch (ex) {
|
|
319
319
|
LogError('SQLServerVectorDatabase.DeleteIndex error', undefined, ex);
|
|
320
|
-
return this.
|
|
320
|
+
return this.failure('Error deleting index');
|
|
321
321
|
}
|
|
322
322
|
}
|
|
323
323
|
async EditIndex(_params) {
|
|
324
|
-
return this.
|
|
324
|
+
return this.failure('EditIndex is not currently supported for colocated SQL Server vectors');
|
|
325
325
|
}
|
|
326
326
|
// ----------------------------------------------------------------
|
|
327
327
|
// Query — VECTOR_SEARCH TVF with cardinality-based exact/approximate dispatch
|
|
@@ -336,9 +336,9 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
336
336
|
* Servers (parallel client-to-many-servers, or mixed 2022/2025 backends), and capability differs
|
|
337
337
|
* per server — a process-global flag would let one server's probe poison another's path.
|
|
338
338
|
*/
|
|
339
|
-
static { this.
|
|
339
|
+
static { this.approximateSearchSupportedByHost = new WeakMap(); }
|
|
340
340
|
async ColocatedQuery(params, _contextUser) {
|
|
341
|
-
const resolved = await this.
|
|
341
|
+
const resolved = await this.getResolvedIndex(params.indexName);
|
|
342
342
|
if (!resolved) {
|
|
343
343
|
throw new Error(`SQLServerVectorDatabase: index "${params.indexName}" is not registered`);
|
|
344
344
|
}
|
|
@@ -354,9 +354,9 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
354
354
|
topK: SafeTopK(params.topK || 10),
|
|
355
355
|
filter: params.filter,
|
|
356
356
|
};
|
|
357
|
-
const rows = await this.
|
|
357
|
+
const rows = await this.runVectorQuery(build, resolved, !!params.filter);
|
|
358
358
|
const includeMetadata = params.includeMetadata !== false;
|
|
359
|
-
return { matches: rows.map(row => this.
|
|
359
|
+
return { matches: rows.map(row => this.rowToMatch(row, resolved, includeMetadata)) };
|
|
360
360
|
}
|
|
361
361
|
/**
|
|
362
362
|
* Execute the vector search, choosing the exact vs. approximate path and transparently falling
|
|
@@ -369,51 +369,51 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
369
369
|
* - Else attempt approximate; if it errors with a "feature not supported" signature, cache that
|
|
370
370
|
* fact and retry exact.
|
|
371
371
|
*/
|
|
372
|
-
async
|
|
373
|
-
const cache = SQLServerVectorDatabase_1.
|
|
374
|
-
let useExact = cache.get(this.
|
|
372
|
+
async runVectorQuery(build, resolved, hasFilter) {
|
|
373
|
+
const cache = SQLServerVectorDatabase_1.approximateSearchSupportedByHost;
|
|
374
|
+
let useExact = cache.get(this.host) === false;
|
|
375
375
|
if (!useExact && hasFilter) {
|
|
376
376
|
const countQ = BuildFilterCountQuery(resolved.target, build.filter);
|
|
377
|
-
const countRows = await this.
|
|
377
|
+
const countRows = await this.run(countQ.sql, countQ.params);
|
|
378
378
|
const n = countRows[0]?.n != null ? Number(countRows[0].n) : Number.MAX_SAFE_INTEGER;
|
|
379
379
|
useExact = n <= resolved.threshold;
|
|
380
380
|
}
|
|
381
381
|
if (useExact) {
|
|
382
382
|
const exact = BuildExactVectorQuery(build);
|
|
383
|
-
return this.
|
|
383
|
+
return this.run(exact.sql, exact.params);
|
|
384
384
|
}
|
|
385
385
|
const approx = BuildApproximateVectorQuery(build);
|
|
386
386
|
try {
|
|
387
|
-
const rows = await this.
|
|
388
|
-
cache.set(this.
|
|
387
|
+
const rows = await this.run(approx.sql, approx.params);
|
|
388
|
+
cache.set(this.host, true);
|
|
389
389
|
return rows;
|
|
390
390
|
}
|
|
391
391
|
catch (ex) {
|
|
392
|
-
if (!this.
|
|
392
|
+
if (!this.isApproximateUnsupportedError(ex)) {
|
|
393
393
|
throw ex;
|
|
394
394
|
}
|
|
395
|
-
cache.set(this.
|
|
395
|
+
cache.set(this.host, false);
|
|
396
396
|
LogStatus('SQLServerVectorDatabase: VECTOR_SEARCH/WITH APPROXIMATE is unavailable on this server; falling back to exact VECTOR_DISTANCE search.');
|
|
397
397
|
const exact = BuildExactVectorQuery(build);
|
|
398
|
-
return this.
|
|
398
|
+
return this.run(exact.sql, exact.params);
|
|
399
399
|
}
|
|
400
400
|
}
|
|
401
401
|
/** Best-effort heuristic: does this error indicate the VECTOR_SEARCH/DiskANN surface is absent on
|
|
402
402
|
* this server? Matches on message text (brittle across CUs/locales), but only gates a fallback to
|
|
403
403
|
* an equally-correct exact query, so a false negative just rethrows and a false positive is slow,
|
|
404
404
|
* not wrong. */
|
|
405
|
-
|
|
405
|
+
isApproximateUnsupportedError(ex) {
|
|
406
406
|
const msg = ex instanceof Error ? ex.message : String(ex);
|
|
407
407
|
return /APPROXIMATE|VECTOR_SEARCH|Unknown object type 'VECTOR'/i.test(msg);
|
|
408
408
|
}
|
|
409
409
|
/** Map a query result row to a ColocatedMatch, synthesizing metadata per storage mode. */
|
|
410
|
-
|
|
410
|
+
rowToMatch(row, resolved, includeMetadata) {
|
|
411
411
|
const distance = Number(row['distance'] ?? 0);
|
|
412
412
|
const score = DistanceToScore(distance, resolved.metric);
|
|
413
413
|
if (resolved.storageMode === 'sibling') {
|
|
414
414
|
const match = { id: String(row['id']), score };
|
|
415
415
|
if (includeMetadata && row['metadata']) {
|
|
416
|
-
match.metadata = this.
|
|
416
|
+
match.metadata = this.parseMetadata(row['metadata']);
|
|
417
417
|
}
|
|
418
418
|
return match;
|
|
419
419
|
}
|
|
@@ -432,14 +432,14 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
432
432
|
for (const col of resolved.target.selectColumns) {
|
|
433
433
|
const value = row[col];
|
|
434
434
|
if (value != null) {
|
|
435
|
-
metadata[col] = this.
|
|
435
|
+
metadata[col] = this.toMetadataValue(value);
|
|
436
436
|
}
|
|
437
437
|
}
|
|
438
438
|
match.metadata = metadata;
|
|
439
439
|
}
|
|
440
440
|
return match;
|
|
441
441
|
}
|
|
442
|
-
|
|
442
|
+
toMetadataValue(value) {
|
|
443
443
|
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
444
444
|
return value;
|
|
445
445
|
}
|
|
@@ -453,10 +453,10 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
453
453
|
const vector = 'vector' in params ? params.vector : undefined;
|
|
454
454
|
const indexName = 'id' in params ? params.id : undefined;
|
|
455
455
|
if (!vector) {
|
|
456
|
-
return this.
|
|
456
|
+
return this.failure('QueryIndex requires a vector in the params');
|
|
457
457
|
}
|
|
458
458
|
if (!indexName) {
|
|
459
|
-
return this.
|
|
459
|
+
return this.failure('QueryIndex requires an index name (params.id)');
|
|
460
460
|
}
|
|
461
461
|
const result = await this.ColocatedQuery({
|
|
462
462
|
indexName,
|
|
@@ -471,12 +471,12 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
471
471
|
score: m.score,
|
|
472
472
|
...(m.metadata ? { metadata: m.metadata } : {}),
|
|
473
473
|
}));
|
|
474
|
-
const response = { matches, namespace: this.
|
|
475
|
-
return this.
|
|
474
|
+
const response = { matches, namespace: this.schema };
|
|
475
|
+
return this.success(response);
|
|
476
476
|
}
|
|
477
477
|
catch (ex) {
|
|
478
478
|
LogError('SQLServerVectorDatabase.QueryIndex error', undefined, ex);
|
|
479
|
-
return this.
|
|
479
|
+
return this.failure('Error querying index');
|
|
480
480
|
}
|
|
481
481
|
}
|
|
482
482
|
// ----------------------------------------------------------------
|
|
@@ -484,61 +484,61 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
484
484
|
// ----------------------------------------------------------------
|
|
485
485
|
async CreateRecord(record, indexName) {
|
|
486
486
|
if (!indexName) {
|
|
487
|
-
return this.
|
|
487
|
+
return this.failure('indexName is required for CreateRecord');
|
|
488
488
|
}
|
|
489
|
-
return this.
|
|
489
|
+
return this.upsertRecords([record], indexName);
|
|
490
490
|
}
|
|
491
491
|
async CreateRecords(records, indexName) {
|
|
492
492
|
if (!indexName) {
|
|
493
|
-
return this.
|
|
493
|
+
return this.failure('indexName is required for CreateRecords');
|
|
494
494
|
}
|
|
495
|
-
return this.
|
|
495
|
+
return this.upsertRecords(records, indexName);
|
|
496
496
|
}
|
|
497
|
-
async
|
|
497
|
+
async upsertRecords(records, indexName) {
|
|
498
498
|
try {
|
|
499
|
-
const resolved = await this.
|
|
499
|
+
const resolved = await this.getResolvedIndex(indexName);
|
|
500
500
|
if (!resolved) {
|
|
501
|
-
return this.
|
|
501
|
+
return this.failure(`index "${indexName}" is not registered`);
|
|
502
502
|
}
|
|
503
503
|
for (const record of records) {
|
|
504
504
|
const dim = resolved.dimension || record.values.length;
|
|
505
505
|
if (resolved.storageMode === 'entityColumn') {
|
|
506
506
|
// The entity row already exists and is app-owned; only set its vector column.
|
|
507
|
-
const keyValue = this.
|
|
508
|
-
await this.
|
|
507
|
+
const keyValue = this.extractKeyValue(record.id);
|
|
508
|
+
await this.run(`UPDATE ${resolved.target.qualifiedTable}
|
|
509
509
|
SET [${resolved.target.vectorColumn}] = CAST(@p1 AS VECTOR(${dim}))
|
|
510
510
|
WHERE [${resolved.keyColumn}] = @p0`, [keyValue, VectorJson(record.values)]);
|
|
511
511
|
}
|
|
512
512
|
else {
|
|
513
513
|
const content = DeriveContent(record.metadata);
|
|
514
|
-
await this.
|
|
514
|
+
await this.run(`MERGE ${this.qualify(indexName)} AS t
|
|
515
515
|
USING (SELECT @p0 AS id) AS s ON t.id = s.id
|
|
516
516
|
WHEN MATCHED THEN UPDATE SET embedding = CAST(@p1 AS VECTOR(${dim})), metadata = @p2, content = @p3
|
|
517
517
|
WHEN NOT MATCHED THEN INSERT (id, embedding, metadata, content)
|
|
518
518
|
VALUES (@p0, CAST(@p1 AS VECTOR(${dim})), @p2, @p3);`, [record.id, VectorJson(record.values), JSON.stringify(record.metadata ?? {}), content]);
|
|
519
519
|
}
|
|
520
520
|
}
|
|
521
|
-
return this.
|
|
521
|
+
return this.success({ upsertedCount: records.length });
|
|
522
522
|
}
|
|
523
523
|
catch (ex) {
|
|
524
524
|
LogError('SQLServerVectorDatabase.UpsertRecords error', undefined, ex);
|
|
525
|
-
return this.
|
|
525
|
+
return this.failure('Error upserting records');
|
|
526
526
|
}
|
|
527
527
|
}
|
|
528
528
|
/** Extract the scalar key value from a vector record id (CompositeKey URL `Field|Value`). */
|
|
529
|
-
|
|
529
|
+
extractKeyValue(recordId) {
|
|
530
530
|
const parts = recordId.split('|');
|
|
531
531
|
return parts.length >= 2 ? parts[1] : recordId;
|
|
532
532
|
}
|
|
533
533
|
async UpdateRecord(record) {
|
|
534
534
|
const indexName = record['indexName'];
|
|
535
535
|
if (!indexName) {
|
|
536
|
-
return this.
|
|
536
|
+
return this.failure('indexName property is required on the UpdateOptions object');
|
|
537
537
|
}
|
|
538
538
|
if (!record.values) {
|
|
539
|
-
return this.
|
|
539
|
+
return this.success(null);
|
|
540
540
|
}
|
|
541
|
-
return this.
|
|
541
|
+
return this.upsertRecords([{ id: record.id, values: record.values, metadata: record.metadata }], indexName);
|
|
542
542
|
}
|
|
543
543
|
async UpdateRecords(records) {
|
|
544
544
|
return this.UpdateRecord(records);
|
|
@@ -548,52 +548,52 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
548
548
|
// ----------------------------------------------------------------
|
|
549
549
|
async DeleteRecord(record, indexName) {
|
|
550
550
|
if (!indexName) {
|
|
551
|
-
return this.
|
|
551
|
+
return this.failure('indexName is required for DeleteRecord');
|
|
552
552
|
}
|
|
553
|
-
return this.
|
|
553
|
+
return this.deleteByKeys([record.id], indexName);
|
|
554
554
|
}
|
|
555
555
|
async DeleteRecords(records, indexName) {
|
|
556
556
|
if (!indexName) {
|
|
557
|
-
return this.
|
|
557
|
+
return this.failure('indexName is required for DeleteRecords');
|
|
558
558
|
}
|
|
559
|
-
return this.
|
|
559
|
+
return this.deleteByKeys(records.map(r => r.id), indexName);
|
|
560
560
|
}
|
|
561
|
-
async
|
|
561
|
+
async deleteByKeys(ids, indexName) {
|
|
562
562
|
try {
|
|
563
|
-
const resolved = await this.
|
|
563
|
+
const resolved = await this.getResolvedIndex(indexName);
|
|
564
564
|
if (!resolved) {
|
|
565
|
-
return this.
|
|
565
|
+
return this.failure(`index "${indexName}" is not registered`);
|
|
566
566
|
}
|
|
567
567
|
for (const id of ids) {
|
|
568
568
|
if (resolved.storageMode === 'entityColumn') {
|
|
569
569
|
// Don't delete app-owned rows — null out the vector instead.
|
|
570
|
-
await this.
|
|
570
|
+
await this.run(`UPDATE ${resolved.target.qualifiedTable} SET [${resolved.target.vectorColumn}] = NULL WHERE [${resolved.keyColumn}] = @p0`, [this.extractKeyValue(id)]);
|
|
571
571
|
}
|
|
572
572
|
else {
|
|
573
|
-
await this.
|
|
573
|
+
await this.run(`DELETE FROM ${this.qualify(indexName)} WHERE id = @p0`, [id]);
|
|
574
574
|
}
|
|
575
575
|
}
|
|
576
|
-
return this.
|
|
576
|
+
return this.success(null);
|
|
577
577
|
}
|
|
578
578
|
catch (ex) {
|
|
579
579
|
LogError('SQLServerVectorDatabase.DeleteByKeys error', undefined, ex);
|
|
580
|
-
return this.
|
|
580
|
+
return this.failure('Error deleting records');
|
|
581
581
|
}
|
|
582
582
|
}
|
|
583
583
|
async DeleteAllRecords(indexName, _namespace) {
|
|
584
584
|
try {
|
|
585
|
-
const resolved = await this.
|
|
585
|
+
const resolved = await this.getResolvedIndex(indexName);
|
|
586
586
|
if (resolved?.storageMode === 'entityColumn') {
|
|
587
|
-
await this.
|
|
587
|
+
await this.run(`UPDATE ${resolved.target.qualifiedTable} SET [${resolved.target.vectorColumn}] = NULL`);
|
|
588
588
|
}
|
|
589
589
|
else {
|
|
590
|
-
await this.
|
|
590
|
+
await this.run(`DELETE FROM ${this.qualify(indexName)}`);
|
|
591
591
|
}
|
|
592
|
-
return this.
|
|
592
|
+
return this.success(null);
|
|
593
593
|
}
|
|
594
594
|
catch (ex) {
|
|
595
595
|
LogError('SQLServerVectorDatabase.DeleteAllRecords error', undefined, ex);
|
|
596
|
-
return this.
|
|
596
|
+
return this.failure('Error deleting all records');
|
|
597
597
|
}
|
|
598
598
|
}
|
|
599
599
|
// ----------------------------------------------------------------
|
|
@@ -603,23 +603,23 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
603
603
|
try {
|
|
604
604
|
const indexName = typeof params.data?.indexName === 'string' ? params.data.indexName : undefined;
|
|
605
605
|
if (!indexName) {
|
|
606
|
-
return this.
|
|
606
|
+
return this.failure('params.data.indexName is required');
|
|
607
607
|
}
|
|
608
|
-
const resolved = await this.
|
|
608
|
+
const resolved = await this.getResolvedIndex(indexName);
|
|
609
609
|
if (!resolved) {
|
|
610
|
-
return this.
|
|
610
|
+
return this.failure(`index "${indexName}" is not registered`);
|
|
611
611
|
}
|
|
612
612
|
const t = resolved.target;
|
|
613
|
-
const rows = await this.
|
|
614
|
-
FROM ${t.qualifiedTable} c WHERE c.[${t.keyColumn}] = @p0`, [this.
|
|
613
|
+
const rows = await this.run(`SELECT c.[${t.keyColumn}] AS id, CAST(c.[${t.vectorColumn}] AS NVARCHAR(MAX)) AS embedding_text
|
|
614
|
+
FROM ${t.qualifiedTable} c WHERE c.[${t.keyColumn}] = @p0`, [this.extractKeyValue(params.id)]);
|
|
615
615
|
if (rows.length === 0) {
|
|
616
|
-
return this.
|
|
616
|
+
return this.failure(`Record "${params.id}" not found`);
|
|
617
617
|
}
|
|
618
|
-
return this.
|
|
618
|
+
return this.success({ id: String(rows[0].id), values: ParseVectorJson(rows[0].embedding_text) });
|
|
619
619
|
}
|
|
620
620
|
catch (ex) {
|
|
621
621
|
LogError('SQLServerVectorDatabase.GetRecord error', undefined, ex);
|
|
622
|
-
return this.
|
|
622
|
+
return this.failure('Error getting record');
|
|
623
623
|
}
|
|
624
624
|
}
|
|
625
625
|
async GetRecords(params) {
|
|
@@ -627,37 +627,37 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
627
627
|
const indexName = typeof params.data?.indexName === 'string' ? params.data.indexName : undefined;
|
|
628
628
|
const ids = Array.isArray(params.data?.ids) ? params.data.ids : undefined;
|
|
629
629
|
if (!indexName) {
|
|
630
|
-
return this.
|
|
630
|
+
return this.failure('params.data.indexName is required');
|
|
631
631
|
}
|
|
632
632
|
if (!ids || ids.length === 0) {
|
|
633
|
-
return this.
|
|
633
|
+
return this.failure('params.data.ids is required');
|
|
634
634
|
}
|
|
635
|
-
const resolved = await this.
|
|
635
|
+
const resolved = await this.getResolvedIndex(indexName);
|
|
636
636
|
if (!resolved) {
|
|
637
|
-
return this.
|
|
637
|
+
return this.failure(`index "${indexName}" is not registered`);
|
|
638
638
|
}
|
|
639
639
|
const t = resolved.target;
|
|
640
|
-
const keyValues = ids.map(id => this.
|
|
640
|
+
const keyValues = ids.map(id => this.extractKeyValue(id));
|
|
641
641
|
const placeholders = keyValues.map((_v, i) => `@p${i}`).join(', ');
|
|
642
|
-
const rows = await this.
|
|
642
|
+
const rows = await this.run(`SELECT c.[${t.keyColumn}] AS id, CAST(c.[${t.vectorColumn}] AS NVARCHAR(MAX)) AS embedding_text
|
|
643
643
|
FROM ${t.qualifiedTable} c WHERE c.[${t.keyColumn}] IN (${placeholders})`, keyValues);
|
|
644
|
-
return this.
|
|
644
|
+
return this.success(rows.map(r => ({ id: String(r.id), values: ParseVectorJson(r.embedding_text) })));
|
|
645
645
|
}
|
|
646
646
|
catch (ex) {
|
|
647
647
|
LogError('SQLServerVectorDatabase.GetRecords error', undefined, ex);
|
|
648
|
-
return this.
|
|
648
|
+
return this.failure('Error getting records');
|
|
649
649
|
}
|
|
650
650
|
}
|
|
651
651
|
async ListVectorIDs(params) {
|
|
652
652
|
try {
|
|
653
|
-
const resolved = await this.
|
|
653
|
+
const resolved = await this.getResolvedIndex(params.IndexName);
|
|
654
654
|
if (!resolved) {
|
|
655
655
|
return { IDs: [] };
|
|
656
656
|
}
|
|
657
657
|
const t = resolved.target;
|
|
658
658
|
const limit = params.Limit ?? 100;
|
|
659
659
|
const offset = params.PaginationToken ? parseInt(params.PaginationToken, 10) : 0;
|
|
660
|
-
const rows = await this.
|
|
660
|
+
const rows = await this.run(`SELECT c.[${t.keyColumn}] AS id FROM ${t.qualifiedTable} c
|
|
661
661
|
ORDER BY c.[${t.keyColumn}] OFFSET @p0 ROWS FETCH NEXT @p1 ROWS ONLY`, [offset, limit]);
|
|
662
662
|
const ids = rows.map(r => String(r.id));
|
|
663
663
|
const nextToken = ids.length < limit ? undefined : String(offset + ids.length);
|
|
@@ -675,7 +675,7 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
675
675
|
// ----------------------------------------------------------------
|
|
676
676
|
// Helpers
|
|
677
677
|
// ----------------------------------------------------------------
|
|
678
|
-
|
|
678
|
+
parseMetadata(raw) {
|
|
679
679
|
if (raw == null) {
|
|
680
680
|
return {};
|
|
681
681
|
}
|
|
@@ -689,10 +689,10 @@ let SQLServerVectorDatabase = class SQLServerVectorDatabase extends VectorDBBase
|
|
|
689
689
|
return {};
|
|
690
690
|
}
|
|
691
691
|
}
|
|
692
|
-
|
|
692
|
+
success(data) {
|
|
693
693
|
return { success: true, message: '', data };
|
|
694
694
|
}
|
|
695
|
-
|
|
695
|
+
failure(message) {
|
|
696
696
|
return { success: false, message: message || 'An error occurred', data: null };
|
|
697
697
|
}
|
|
698
698
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai-vectors-sqlserver",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "6.2.0-edge.
|
|
4
|
+
"version": "6.2.0-edge.1",
|
|
5
5
|
"description": "SQL Server 2025 native VECTOR implementation for AI Vector Database (colocated)",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
"author": "MemberJunction.com",
|
|
12
12
|
"license": "BUSL-1.1",
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@memberjunction/ai-vectordb": "6.2.0-edge.
|
|
15
|
-
"@memberjunction/core": "6.2.0-edge.
|
|
16
|
-
"@memberjunction/global": "6.2.0-edge.
|
|
14
|
+
"@memberjunction/ai-vectordb": "6.2.0-edge.1",
|
|
15
|
+
"@memberjunction/core": "6.2.0-edge.1",
|
|
16
|
+
"@memberjunction/global": "6.2.0-edge.1",
|
|
17
17
|
"dotenv": "^17.2.4"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|