oracledb 7.0.0 → 26.0.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/README.md +16 -6
- package/build/Release/oracledb-26.0.0-darwin-arm64.node +0 -0
- package/build/Release/oracledb-26.0.0-darwin-arm64.node-buildinfo.txt +1 -0
- package/build/Release/oracledb-26.0.0-js-buildinfo.txt +1 -0
- package/build/Release/oracledb-26.0.0-linux-arm64.node +0 -0
- package/build/Release/oracledb-26.0.0-linux-arm64.node-buildinfo.txt +1 -0
- package/build/Release/oracledb-26.0.0-linux-x64.node +0 -0
- package/build/Release/oracledb-26.0.0-linux-x64.node-buildinfo.txt +1 -0
- package/build/Release/oracledb-26.0.0-win32-x64.node +0 -0
- package/build/Release/oracledb-26.0.0-win32-x64.node-buildinfo.txt +1 -0
- package/lib/aqDeqOptions.js +4 -4
- package/lib/aqEnqOptions.js +2 -2
- package/lib/aqQueue.js +11 -6
- package/lib/connection.js +73 -18
- package/lib/constants.js +4 -0
- package/lib/endUserSecurityContext.js +30 -25
- package/lib/errors.js +92 -5
- package/lib/impl/connection.js +36 -0
- package/lib/impl/datahandlers/buffer.js +91 -18
- package/lib/impl/datahandlers/oson.js +3 -2
- package/lib/impl/parserHelpers.js +6 -2
- package/lib/impl/resultset.js +32 -11
- package/lib/oracledb.js +34 -0
- package/lib/pool.js +6 -2
- package/lib/queryStream.js +13 -5
- package/lib/resultset.js +10 -3
- package/lib/securityContextProvider.js +206 -0
- package/lib/thin/aq.js +2 -3
- package/lib/thin/connection.js +380 -24
- package/lib/thin/dbObject.js +20 -11
- package/lib/thin/pool.js +4 -3
- package/lib/thin/protocol/capabilities.js +37 -4
- package/lib/thin/protocol/constants.js +49 -1
- package/lib/thin/protocol/messages/aqArray.js +2 -2
- package/lib/thin/protocol/messages/aqBase.js +2 -2
- package/lib/thin/protocol/messages/auth.js +17 -6
- package/lib/thin/protocol/messages/base.js +75 -17
- package/lib/thin/protocol/messages/dataType.js +46 -0
- package/lib/thin/protocol/messages/directPathLoadStream.js +3 -2
- package/lib/thin/protocol/messages/execute.js +10 -4
- package/lib/thin/protocol/messages/index.js +4 -0
- package/lib/thin/protocol/messages/notification.js +281 -0
- package/lib/thin/protocol/messages/subscribe.js +181 -0
- package/lib/thin/protocol/messages/withData.js +23 -10
- package/lib/thin/protocol/packet.js +2 -4
- package/lib/thin/protocol/protocol.js +7 -4
- package/lib/thin/resultSet.js +40 -20
- package/lib/thin/sqlnet/ezConnectResolver.js +9 -2
- package/lib/thin/sqlnet/navNodes.js +7 -5
- package/lib/thin/sqlnet/networkSession.js +17 -11
- package/lib/thin/sqlnet/ntTcp.js +24 -8
- package/lib/thin/sqlnet/packet.js +24 -4
- package/lib/thin/sqlnet/sessionAtts.js +2 -2
- package/lib/thin/statement.js +6 -1
- package/lib/thin/statementCache.js +34 -6
- package/lib/thin/subscr.js +363 -0
- package/lib/transformer.js +60 -17
- package/lib/util.js +46 -8
- package/lib/version.js +1 -1
- package/package.json +1 -1
- package/plugins/configProviders/gcpstorage/index.js +121 -0
- package/plugins/configProviders/ociobject/index.js +1 -1
- package/plugins/token/cache.js +154 -0
- package/plugins/token/endUserSecurityProvider/index.js +377 -0
- package/plugins/token/extensionAzure/index.js +160 -28
- package/plugins/token/extensionOci/index.js +221 -16
- package/plugins/vectorsdk/OracleVecDB.js +346 -0
- package/plugins/vectorsdk/index.js +49 -0
- package/plugins/vectorsdk/utils.js +370 -0
- package/plugins/vectorsdk/vectorEmbedding.js +223 -0
- package/plugins/vectorsdk/vectorIndex.js +177 -0
- package/plugins/vectorsdk/vectorRecords.js +406 -0
- package/plugins/vectorsdk/vectorSchema.js +408 -0
- package/plugins/vectorsdk/vectorSearch.js +538 -0
- package/build/Release/oracledb-7.0.0-darwin-arm64.node +0 -0
- package/build/Release/oracledb-7.0.0-darwin-arm64.node-buildinfo.txt +0 -1
- package/build/Release/oracledb-7.0.0-js-buildinfo.txt +0 -1
- package/build/Release/oracledb-7.0.0-linux-arm64.node +0 -0
- package/build/Release/oracledb-7.0.0-linux-arm64.node-buildinfo.txt +0 -1
- package/build/Release/oracledb-7.0.0-linux-x64.node +0 -0
- package/build/Release/oracledb-7.0.0-linux-x64.node-buildinfo.txt +0 -1
- package/build/Release/oracledb-7.0.0-win32-x64.node +0 -0
- package/build/Release/oracledb-7.0.0-win32-x64.node-buildinfo.txt +0 -1
- package/eslint.config.js +0 -39
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// Copyright (c) 2026, Oracle and/or its affiliates.
|
|
2
|
+
|
|
3
|
+
//-----------------------------------------------------------------------------
|
|
4
|
+
//
|
|
5
|
+
// This software is dual-licensed to you under the Universal Permissive License
|
|
6
|
+
// (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
|
|
7
|
+
// 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
|
|
8
|
+
// either license.
|
|
9
|
+
//
|
|
10
|
+
// If you elect to accept the software under the Apache License, Version 2.0,
|
|
11
|
+
// the following applies:
|
|
12
|
+
//
|
|
13
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
// you may not use this file except in compliance with the License.
|
|
15
|
+
// You may obtain a copy of the License at
|
|
16
|
+
//
|
|
17
|
+
// https://www.apache.org/licenses/LICENSE-2.0
|
|
18
|
+
//
|
|
19
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
20
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
21
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
22
|
+
// See the License for the specific language governing permissions and
|
|
23
|
+
// limitations under the License.
|
|
24
|
+
//
|
|
25
|
+
//-----------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
'use strict';
|
|
28
|
+
|
|
29
|
+
const oracledb = require('oracledb');
|
|
30
|
+
const {
|
|
31
|
+
assertIntegerInRange,
|
|
32
|
+
assertNonEmptyString,
|
|
33
|
+
assertOptionalArray,
|
|
34
|
+
assertPlainObject,
|
|
35
|
+
assertPositiveInteger,
|
|
36
|
+
normalizeVectorDistanceType,
|
|
37
|
+
readLobAsString,
|
|
38
|
+
assertDatabaseVersion
|
|
39
|
+
} = require('./utils.js');
|
|
40
|
+
const {
|
|
41
|
+
renderIdentifier,
|
|
42
|
+
renderQualifiedName
|
|
43
|
+
} = require('./vectorSchema.js');
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
//-----------------------------------------------------------------------------
|
|
47
|
+
// createVectorIndex()
|
|
48
|
+
|
|
49
|
+
// Create the vector index on a database table.
|
|
50
|
+
//-----------------------------------------------------------------------------
|
|
51
|
+
async function createVectorIndex(connection, tableDef, options) {
|
|
52
|
+
assertPlainObject(options, 'options');
|
|
53
|
+
|
|
54
|
+
const {
|
|
55
|
+
tableName,
|
|
56
|
+
vector,
|
|
57
|
+
quoteIdentifiers,
|
|
58
|
+
vectorDistanceType
|
|
59
|
+
} = tableDef;
|
|
60
|
+
|
|
61
|
+
const vectorColumn = vector.column;
|
|
62
|
+
|
|
63
|
+
if (vector.storageFormat === 'BINARY' &&
|
|
64
|
+
vectorDistanceType !== 'HAMMING' && vectorDistanceType !== 'JACCARD') {
|
|
65
|
+
throw new TypeError(
|
|
66
|
+
'BINARY vector indexes require HAMMING or JACCARD distance.'
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const {
|
|
71
|
+
indexName,
|
|
72
|
+
includeColumns = null,
|
|
73
|
+
partitioningScheme = 'GLOBAL',
|
|
74
|
+
type = 'HNSW',
|
|
75
|
+
accuracy = 90,
|
|
76
|
+
parameters,
|
|
77
|
+
parallel = 1,
|
|
78
|
+
} = options;
|
|
79
|
+
|
|
80
|
+
assertNonEmptyString(partitioningScheme, 'options.partitioningScheme');
|
|
81
|
+
assertNonEmptyString(type, 'options.type');
|
|
82
|
+
assertIntegerInRange(accuracy, 'options.accuracy', 1, 100);
|
|
83
|
+
assertPositiveInteger(parallel, 'options.parallel');
|
|
84
|
+
assertOptionalArray(includeColumns, 'options.includeColumns');
|
|
85
|
+
assertPlainObject(parameters, 'options.parameters');
|
|
86
|
+
|
|
87
|
+
const indexType = type.trim().toUpperCase();
|
|
88
|
+
if (!['HNSW', 'IVF'].includes(indexType)) {
|
|
89
|
+
throw new TypeError('options.type must be either HNSW or IVF.');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const organization = indexType === "HNSW"
|
|
93
|
+
? "INMEMORY NEIGHBOR GRAPH"
|
|
94
|
+
: "NEIGHBOR PARTITIONS";
|
|
95
|
+
|
|
96
|
+
const dbParameters = {...parameters, type: indexType};
|
|
97
|
+
const includeColumnsSql = includeColumns == null
|
|
98
|
+
? null
|
|
99
|
+
: includeColumns.map((name, index) =>
|
|
100
|
+
renderIdentifier(
|
|
101
|
+
name,
|
|
102
|
+
`includeColumns[${index}]`,
|
|
103
|
+
quoteIdentifiers
|
|
104
|
+
)
|
|
105
|
+
).join(',');
|
|
106
|
+
const binds = {
|
|
107
|
+
index_name: renderIdentifier(
|
|
108
|
+
indexName,
|
|
109
|
+
'indexName',
|
|
110
|
+
quoteIdentifiers
|
|
111
|
+
),
|
|
112
|
+
table_name: renderQualifiedName(
|
|
113
|
+
tableName,
|
|
114
|
+
'tableName',
|
|
115
|
+
quoteIdentifiers
|
|
116
|
+
),
|
|
117
|
+
vector_column: renderIdentifier(
|
|
118
|
+
vectorColumn,
|
|
119
|
+
'vectorColumn',
|
|
120
|
+
quoteIdentifiers
|
|
121
|
+
),
|
|
122
|
+
include_columns: includeColumnsSql,
|
|
123
|
+
partitioning_scheme: partitioningScheme,
|
|
124
|
+
organization,
|
|
125
|
+
distance_metric: normalizeVectorDistanceType(vectorDistanceType),
|
|
126
|
+
accuracy,
|
|
127
|
+
parallel,
|
|
128
|
+
parameters: {
|
|
129
|
+
val: JSON.stringify(dbParameters),
|
|
130
|
+
type: oracledb.DB_TYPE_CLOB
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const plsql = `
|
|
135
|
+
BEGIN
|
|
136
|
+
DBMS_VECTOR.CREATE_INDEX(
|
|
137
|
+
idx_name => :index_name,
|
|
138
|
+
table_name => :table_name,
|
|
139
|
+
idx_vector_col => :vector_column,
|
|
140
|
+
idx_include_cols => :include_columns,
|
|
141
|
+
idx_partitioning_scheme => :partitioning_scheme,
|
|
142
|
+
idx_organization => :organization,
|
|
143
|
+
idx_distance_metric => :distance_metric,
|
|
144
|
+
idx_accuracy => :accuracy,
|
|
145
|
+
idx_parameters => :parameters,
|
|
146
|
+
idx_parallel_creation => :parallel
|
|
147
|
+
);
|
|
148
|
+
END;
|
|
149
|
+
`;
|
|
150
|
+
|
|
151
|
+
await connection.execute(plsql, binds);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Return the build status of the vector index associated with this table.
|
|
155
|
+
async function getIndexBuildStatus(connection, tableName) {
|
|
156
|
+
assertDatabaseVersion(connection, 'getIndexBuildStatus()');
|
|
157
|
+
const binds = {
|
|
158
|
+
table_name: tableName,
|
|
159
|
+
result: {
|
|
160
|
+
dir: oracledb.BIND_OUT,
|
|
161
|
+
type: oracledb.DB_TYPE_CLOB
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const plsql = `
|
|
166
|
+
BEGIN
|
|
167
|
+
:result := DBMS_VECTOR_DATABASE.INDEX_BUILD_STATUS(
|
|
168
|
+
TABLE_NAME => :table_name
|
|
169
|
+
);
|
|
170
|
+
END;`;
|
|
171
|
+
|
|
172
|
+
const res = await connection.execute(plsql, binds);
|
|
173
|
+
const output = await readLobAsString(res.outBinds.result);
|
|
174
|
+
return JSON.parse(output);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
module.exports = {createVectorIndex, getIndexBuildStatus};
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
// Copyright (c) 2026, Oracle and/or its affiliates.
|
|
2
|
+
|
|
3
|
+
//-----------------------------------------------------------------------------
|
|
4
|
+
//
|
|
5
|
+
// This software is dual-licensed to you under the Universal Permissive License
|
|
6
|
+
// (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
|
|
7
|
+
// 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
|
|
8
|
+
// either license.
|
|
9
|
+
//
|
|
10
|
+
// If you elect to accept the software under the Apache License, Version 2.0,
|
|
11
|
+
// the following applies:
|
|
12
|
+
//
|
|
13
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
// you may not use this file except in compliance with the License.
|
|
15
|
+
// You may obtain a copy of the License at
|
|
16
|
+
//
|
|
17
|
+
// https://www.apache.org/licenses/LICENSE-2.0
|
|
18
|
+
//
|
|
19
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
20
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
21
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
22
|
+
// See the License for the specific language governing permissions and
|
|
23
|
+
// limitations under the License.
|
|
24
|
+
//
|
|
25
|
+
//-----------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
'use strict';
|
|
28
|
+
|
|
29
|
+
const crypto = require('crypto');
|
|
30
|
+
const oracledb = require('oracledb');
|
|
31
|
+
const {
|
|
32
|
+
assertOptionalArray,
|
|
33
|
+
assertOptionalBoolean,
|
|
34
|
+
assertPlainObject,
|
|
35
|
+
assertNonEmptyString,
|
|
36
|
+
prepareVector,
|
|
37
|
+
assertNonEmptyArray
|
|
38
|
+
} = require('./utils.js');
|
|
39
|
+
const {
|
|
40
|
+
renderIdentifier,
|
|
41
|
+
renderQualifiedName
|
|
42
|
+
} = require('./vectorSchema.js');
|
|
43
|
+
const {generateEmbeddings} = require('./vectorEmbedding.js');
|
|
44
|
+
|
|
45
|
+
function validateAddOptions(options) {
|
|
46
|
+
assertPlainObject(options, 'options');
|
|
47
|
+
assertOptionalBoolean(options.upsert, 'options.upsert');
|
|
48
|
+
assertOptionalBoolean(options.autoCommit, 'options.autoCommit');
|
|
49
|
+
|
|
50
|
+
// Ensure returned IDs refer to committed records.
|
|
51
|
+
if (options.autoCommit != true) {
|
|
52
|
+
throw new TypeError(
|
|
53
|
+
'autoCommit: false is not supported by OracleVecDB operations.'
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function validateIds(ids, name = 'options.ids') {
|
|
59
|
+
assertOptionalArray(ids, name);
|
|
60
|
+
|
|
61
|
+
if (ids != null) {
|
|
62
|
+
ids.forEach((id, index) => {
|
|
63
|
+
resolveVectorId(id, `${name}[${index}]`);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function resolveVectorId(id, idPath) {
|
|
69
|
+
assertNonEmptyString(id, idPath);
|
|
70
|
+
|
|
71
|
+
if (Buffer.byteLength(id) > 255) {
|
|
72
|
+
throw new TypeError(`${idPath} exceeds 255 bytes.`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return id;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function prepareInputs(documents, suppliedIds, vectors, vectorDef) {
|
|
79
|
+
assertOptionalArray(suppliedIds, 'options.ids');
|
|
80
|
+
assertNonEmptyArray(documents, 'documents');
|
|
81
|
+
|
|
82
|
+
if (vectors != null && vectors.length !== documents.length) {
|
|
83
|
+
throw new TypeError(
|
|
84
|
+
'The number of documents must match the number of vectors.'
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (suppliedIds != null && suppliedIds.length !== documents.length) {
|
|
89
|
+
throw new TypeError(
|
|
90
|
+
`The number of options.ids must match the number of ${vectors ? 'vectors' : 'documents'}.`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const ids = new Array(documents.length);
|
|
95
|
+
const texts = vectors == null ? new Array(documents.length) : null;
|
|
96
|
+
const bindRows = new Array(documents.length);
|
|
97
|
+
|
|
98
|
+
for (let i = 0; i < documents.length; i++) {
|
|
99
|
+
const document = documents[i];
|
|
100
|
+
const path = `documents[${i}]`;
|
|
101
|
+
|
|
102
|
+
assertPlainObject(document, path);
|
|
103
|
+
assertNonEmptyString(document.content, `${path}.content`);
|
|
104
|
+
assertPlainObject(document.metadata, `${path}.metadata`);
|
|
105
|
+
|
|
106
|
+
const id = suppliedIds == null
|
|
107
|
+
? crypto.randomUUID()
|
|
108
|
+
: resolveVectorId(suppliedIds[i], `options.ids[${i}]`);
|
|
109
|
+
|
|
110
|
+
ids[i] = id;
|
|
111
|
+
|
|
112
|
+
bindRows[i] = {
|
|
113
|
+
id,
|
|
114
|
+
content: document.content,
|
|
115
|
+
metadata: document.metadata
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
if (vectors == null) {
|
|
119
|
+
texts[i] = document.content;
|
|
120
|
+
} else {
|
|
121
|
+
bindRows[i].vector = prepareVector(vectors[i], vectorDef);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return {ids, texts, bindRows};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function renderVectorTableIdentifiers(tableDef) {
|
|
129
|
+
return {
|
|
130
|
+
table: renderQualifiedName(
|
|
131
|
+
tableDef.tableName,
|
|
132
|
+
'tableName',
|
|
133
|
+
tableDef.quoteIdentifiers
|
|
134
|
+
),
|
|
135
|
+
id: renderIdentifier(
|
|
136
|
+
tableDef.columns.id,
|
|
137
|
+
'columns.id',
|
|
138
|
+
tableDef.quoteIdentifiers
|
|
139
|
+
),
|
|
140
|
+
vector: renderIdentifier(
|
|
141
|
+
tableDef.vector.column,
|
|
142
|
+
'vector.column',
|
|
143
|
+
tableDef.quoteIdentifiers
|
|
144
|
+
),
|
|
145
|
+
content: renderIdentifier(
|
|
146
|
+
tableDef.columns.content,
|
|
147
|
+
'columns.content',
|
|
148
|
+
tableDef.quoteIdentifiers
|
|
149
|
+
),
|
|
150
|
+
metadata: renderIdentifier(
|
|
151
|
+
tableDef.columns.metadata,
|
|
152
|
+
'columns.metadata',
|
|
153
|
+
tableDef.quoteIdentifiers
|
|
154
|
+
)
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function insertSql(tableIdentifiers) {
|
|
159
|
+
const { table, id, vector, content, metadata } = tableIdentifiers;
|
|
160
|
+
return `
|
|
161
|
+
INSERT INTO ${table} (${id}, ${vector}, ${content}, ${metadata})
|
|
162
|
+
VALUES (:id, :vector, :content, :metadata)
|
|
163
|
+
`;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function mergeSql(tableIdentifiers) {
|
|
167
|
+
const { table, id, vector, content, metadata } = tableIdentifiers;
|
|
168
|
+
return `
|
|
169
|
+
MERGE INTO ${table} target
|
|
170
|
+
USING (
|
|
171
|
+
SELECT :id AS source_id, :vector AS source_vector,
|
|
172
|
+
:content AS source_content, :metadata AS source_metadata
|
|
173
|
+
FROM DUAL
|
|
174
|
+
) source
|
|
175
|
+
ON (target.${id} = source.source_id)
|
|
176
|
+
WHEN MATCHED THEN
|
|
177
|
+
UPDATE SET
|
|
178
|
+
target.${vector} = source.source_vector,
|
|
179
|
+
target.${content} = source.source_content,
|
|
180
|
+
target.${metadata} = source.source_metadata
|
|
181
|
+
WHEN NOT MATCHED THEN
|
|
182
|
+
INSERT (${id}, ${vector}, ${content}, ${metadata})
|
|
183
|
+
VALUES (
|
|
184
|
+
source.source_id,
|
|
185
|
+
source.source_vector,
|
|
186
|
+
source.source_content,
|
|
187
|
+
source.source_metadata
|
|
188
|
+
)
|
|
189
|
+
`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function storeVectors(
|
|
193
|
+
connection,
|
|
194
|
+
tableDef,
|
|
195
|
+
{ids, bindRows},
|
|
196
|
+
options
|
|
197
|
+
) {
|
|
198
|
+
const identifiers = renderVectorTableIdentifiers(tableDef);
|
|
199
|
+
const sql = options.upsert
|
|
200
|
+
? mergeSql(identifiers)
|
|
201
|
+
: insertSql(identifiers);
|
|
202
|
+
|
|
203
|
+
await connection.executeMany(sql, bindRows, {
|
|
204
|
+
autoCommit: options.autoCommit,
|
|
205
|
+
bindDefs: {
|
|
206
|
+
id: {type: oracledb.DB_TYPE_VARCHAR, maxSize: 255},
|
|
207
|
+
vector: {type: oracledb.DB_TYPE_VECTOR},
|
|
208
|
+
content: {type: oracledb.DB_TYPE_CLOB},
|
|
209
|
+
metadata: {type: oracledb.DB_TYPE_JSON}
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
return ids;
|
|
214
|
+
}
|
|
215
|
+
//-----------------------------------------------------------------------------
|
|
216
|
+
// addVectors()
|
|
217
|
+
|
|
218
|
+
// Insert supplied vectors or update matching rows in the target table
|
|
219
|
+
//-----------------------------------------------------------------------------
|
|
220
|
+
|
|
221
|
+
function addVectors(
|
|
222
|
+
connection,
|
|
223
|
+
tableDef,
|
|
224
|
+
vectors,
|
|
225
|
+
documents,
|
|
226
|
+
options = {}
|
|
227
|
+
) {
|
|
228
|
+
validateAddOptions(options);
|
|
229
|
+
assertNonEmptyArray(vectors, 'vectors');
|
|
230
|
+
|
|
231
|
+
const prepared = prepareInputs(
|
|
232
|
+
documents,
|
|
233
|
+
options.ids,
|
|
234
|
+
vectors,
|
|
235
|
+
tableDef.vector
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
return storeVectors(
|
|
239
|
+
connection,
|
|
240
|
+
tableDef,
|
|
241
|
+
prepared,
|
|
242
|
+
options
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
//-----------------------------------------------------------------------------
|
|
247
|
+
// addDocuments()
|
|
248
|
+
|
|
249
|
+
// Generate embeddings for documents, then insert or update their rows
|
|
250
|
+
//-----------------------------------------------------------------------------
|
|
251
|
+
|
|
252
|
+
async function addDocuments(
|
|
253
|
+
connection,
|
|
254
|
+
tableDef,
|
|
255
|
+
documents,
|
|
256
|
+
options = {}
|
|
257
|
+
) {
|
|
258
|
+
validateAddOptions(options);
|
|
259
|
+
const prepared = prepareInputs(
|
|
260
|
+
documents,
|
|
261
|
+
options.ids,
|
|
262
|
+
null,
|
|
263
|
+
tableDef.vector
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
const vectors = await generateEmbeddings(
|
|
267
|
+
connection,
|
|
268
|
+
tableDef.modelParams,
|
|
269
|
+
prepared.texts
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
for (let i = 0; i < vectors.length; i++) {
|
|
273
|
+
prepared.bindRows[i].vector =
|
|
274
|
+
prepareVector(vectors[i], tableDef.vector);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return storeVectors(
|
|
278
|
+
connection,
|
|
279
|
+
tableDef,
|
|
280
|
+
prepared,
|
|
281
|
+
options
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
//-----------------------------------------------------------------------------
|
|
286
|
+
// insertWithEmbedding()
|
|
287
|
+
|
|
288
|
+
// Insert rows with embeddings generated in the database from source table text
|
|
289
|
+
//-----------------------------------------------------------------------------
|
|
290
|
+
|
|
291
|
+
async function insertWithEmbedding(connection, tableDef, options) {
|
|
292
|
+
assertPlainObject(options, 'options');
|
|
293
|
+
assertOptionalBoolean(options.autoCommit, 'options.autoCommit');
|
|
294
|
+
assertOptionalBoolean(options.quoteSource, 'options.quoteSource');
|
|
295
|
+
|
|
296
|
+
const quoteSource = options.quoteSource ?? false;
|
|
297
|
+
const { table, id, content, metadata, vector } = renderVectorTableIdentifiers(tableDef);
|
|
298
|
+
const modelParams = tableDef.modelParams;
|
|
299
|
+
assertPlainObject(modelParams, 'modelParams');
|
|
300
|
+
|
|
301
|
+
const srcTable = renderQualifiedName(
|
|
302
|
+
options.sourceTable,
|
|
303
|
+
'options.sourceTable',
|
|
304
|
+
quoteSource
|
|
305
|
+
);
|
|
306
|
+
const sourceContentIdentifier = renderIdentifier(
|
|
307
|
+
options.contentColumn,
|
|
308
|
+
'options.contentColumn',
|
|
309
|
+
quoteSource
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
const sourceId = renderIdentifier(options.idColumn, 'options.idColumn', quoteSource);
|
|
313
|
+
const sourceMetadata = options.metadataColumn == null
|
|
314
|
+
? null
|
|
315
|
+
: renderIdentifier(options.metadataColumn, 'options.metadataColumn', quoteSource);
|
|
316
|
+
|
|
317
|
+
const insertColumns = [
|
|
318
|
+
id,
|
|
319
|
+
content,
|
|
320
|
+
sourceMetadata && metadata,
|
|
321
|
+
vector
|
|
322
|
+
].filter(Boolean).join(', ');
|
|
323
|
+
|
|
324
|
+
const selectExpressions = [
|
|
325
|
+
sourceId,
|
|
326
|
+
sourceContentIdentifier,
|
|
327
|
+
sourceMetadata,
|
|
328
|
+
`DBMS_VECTOR.UTL_TO_EMBEDDING(
|
|
329
|
+
${sourceContentIdentifier},
|
|
330
|
+
:model_params
|
|
331
|
+
)`
|
|
332
|
+
].filter(Boolean).join(', ');
|
|
333
|
+
|
|
334
|
+
const sql = `
|
|
335
|
+
INSERT INTO ${table} (${insertColumns})
|
|
336
|
+
SELECT ${selectExpressions}
|
|
337
|
+
FROM ${srcTable}
|
|
338
|
+
`;
|
|
339
|
+
|
|
340
|
+
const result = await connection.execute(
|
|
341
|
+
sql,
|
|
342
|
+
{
|
|
343
|
+
model_params: {
|
|
344
|
+
val: modelParams,
|
|
345
|
+
type: oracledb.DB_TYPE_JSON
|
|
346
|
+
}
|
|
347
|
+
},
|
|
348
|
+
{ autoCommit: options.autoCommit }
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
return result.rowsAffected;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
//-----------------------------------------------------------------------------
|
|
355
|
+
// deleteByIds()
|
|
356
|
+
|
|
357
|
+
// delete Records by external_id
|
|
358
|
+
//-----------------------------------------------------------------------------
|
|
359
|
+
|
|
360
|
+
async function deleteByIds(connection, tableDef, options) {
|
|
361
|
+
assertPlainObject(options, 'options');
|
|
362
|
+
validateIds(options.ids);
|
|
363
|
+
assertOptionalBoolean(options.deleteAll, 'options.deleteAll');
|
|
364
|
+
assertOptionalBoolean(options.autoCommit, 'options.autoCommit');
|
|
365
|
+
|
|
366
|
+
if (options.ids?.length > 0 && options.deleteAll === true) {
|
|
367
|
+
throw new TypeError(
|
|
368
|
+
'options.ids and options.deleteAll cannot be used together.'
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const {table, id} = renderVectorTableIdentifiers(tableDef);
|
|
373
|
+
const deleteAll = options.deleteAll ?? false;
|
|
374
|
+
|
|
375
|
+
if (options.ids && options.ids.length > 0) {
|
|
376
|
+
|
|
377
|
+
const query = `DELETE FROM ${table} WHERE ${id} = :id`;
|
|
378
|
+
const binds = options.ids.map((value) => ({id: value}));
|
|
379
|
+
|
|
380
|
+
const result = await connection.executeMany(query, binds, {
|
|
381
|
+
autoCommit: options.autoCommit,
|
|
382
|
+
bindDefs: {
|
|
383
|
+
id: {
|
|
384
|
+
type: oracledb.DB_TYPE_VARCHAR,
|
|
385
|
+
maxSize: 255
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
return {
|
|
391
|
+
message: `Deleted ${result.rowsAffected} vector records`
|
|
392
|
+
};
|
|
393
|
+
// deleteAll cannot be rolled back
|
|
394
|
+
} else if (deleteAll) {
|
|
395
|
+
await connection.execute(`TRUNCATE TABLE ${table}`, []);
|
|
396
|
+
|
|
397
|
+
return { message: 'Deleted all vector records' };
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
module.exports = {
|
|
402
|
+
addDocuments,
|
|
403
|
+
addVectors,
|
|
404
|
+
insertWithEmbedding,
|
|
405
|
+
deleteByIds
|
|
406
|
+
};
|