@antfly/embedded 0.0.0-stage → 0.3.0-rc.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.
- package/LICENSE +202 -0
- package/README.md +454 -2
- package/dist/chunk-ELM44XQ4.js +168 -0
- package/dist/database-BY3WSLgt.d.cts +558 -0
- package/dist/database-BY3WSLgt.d.ts +558 -0
- package/dist/index.cjs +2302 -0
- package/dist/index.d.cts +322 -0
- package/dist/index.d.ts +322 -0
- package/dist/index.js +2057 -0
- package/dist/kysely.cjs +346 -0
- package/dist/kysely.d.cts +28 -0
- package/dist/kysely.d.ts +28 -0
- package/dist/kysely.js +169 -0
- package/package.json +87 -4
package/dist/kysely.cjs
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/kysely.ts
|
|
21
|
+
var kysely_exports = {};
|
|
22
|
+
__export(kysely_exports, {
|
|
23
|
+
AntflyDialect: () => AntflyDialect
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(kysely_exports);
|
|
26
|
+
var import_kysely = require("kysely");
|
|
27
|
+
|
|
28
|
+
// src/sql-types.ts
|
|
29
|
+
var SqlError = class extends Error {
|
|
30
|
+
constructor(diagnostic, transactionId) {
|
|
31
|
+
super(`${diagnostic.code}: ${diagnostic.message}`);
|
|
32
|
+
this.diagnostic = diagnostic;
|
|
33
|
+
this.transactionId = transactionId;
|
|
34
|
+
this.name = "SqlError";
|
|
35
|
+
this.sqlstate = diagnostic.code;
|
|
36
|
+
}
|
|
37
|
+
diagnostic;
|
|
38
|
+
transactionId;
|
|
39
|
+
sqlstate;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// src/sql.ts
|
|
43
|
+
function request(statement, parameters, session, streaming = false) {
|
|
44
|
+
return JSON.stringify(
|
|
45
|
+
{ statement, parameters, session_id: session, limit: streaming ? void 0 : 4096 },
|
|
46
|
+
(_key, value) => {
|
|
47
|
+
if (typeof value === "bigint") {
|
|
48
|
+
if (value < -(1n << 63n) || value >= 1n << 63n)
|
|
49
|
+
throw new RangeError("integer parameter is outside the signed 64-bit range");
|
|
50
|
+
return value.toString();
|
|
51
|
+
}
|
|
52
|
+
if (typeof value === "number" && (!Number.isFinite(value) || Number.isInteger(value) && !Number.isSafeInteger(value)))
|
|
53
|
+
throw new RangeError("use bigint for integer parameters outside JavaScript's safe range");
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
function result(wire) {
|
|
59
|
+
return {
|
|
60
|
+
columns: wire.columns,
|
|
61
|
+
commandTag: wire.command_tag,
|
|
62
|
+
rowsAffected: BigInt(wire.rows_affected),
|
|
63
|
+
rows: wire.rows.map(
|
|
64
|
+
(row, rowIndex) => Object.fromEntries(
|
|
65
|
+
wire.columns.map((column, index) => [
|
|
66
|
+
column.name,
|
|
67
|
+
wire.sql_nulls?.[rowIndex]?.[index] ? null : column.type === "integer" && row[index] !== null ? BigInt(row[index]) : row[index]
|
|
68
|
+
])
|
|
69
|
+
)
|
|
70
|
+
)
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
var Connection = class _Connection {
|
|
74
|
+
constructor(database, session, direct = false) {
|
|
75
|
+
this.database = database;
|
|
76
|
+
this.session = session;
|
|
77
|
+
this.direct = direct;
|
|
78
|
+
}
|
|
79
|
+
database;
|
|
80
|
+
session;
|
|
81
|
+
direct;
|
|
82
|
+
#pending = Promise.resolve();
|
|
83
|
+
#closed = false;
|
|
84
|
+
static async open(database) {
|
|
85
|
+
return new _Connection(database, await database.openSqlSession());
|
|
86
|
+
}
|
|
87
|
+
#run(operation) {
|
|
88
|
+
if (this.#closed) return Promise.reject(new Error("SQL connection is closed"));
|
|
89
|
+
if (this.direct) return operation();
|
|
90
|
+
const work = this.#pending.then(operation);
|
|
91
|
+
this.#pending = work.catch(() => void 0);
|
|
92
|
+
return work;
|
|
93
|
+
}
|
|
94
|
+
execute(statement, parameters = []) {
|
|
95
|
+
return this.#run(
|
|
96
|
+
async () => result(
|
|
97
|
+
await this.database.sqlJson(request(statement, parameters, this.session))
|
|
98
|
+
)
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
query(statement, parameters = []) {
|
|
102
|
+
return this.#run(async () => {
|
|
103
|
+
let cursor;
|
|
104
|
+
try {
|
|
105
|
+
cursor = await this.database.openSqlCursor(
|
|
106
|
+
request(statement, parameters, this.session, true)
|
|
107
|
+
);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
if (!(error instanceof SqlError) || error.sqlstate !== "0A000") throw error;
|
|
110
|
+
return result(
|
|
111
|
+
await this.database.sqlJson(request(statement, parameters, this.session))
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
let output;
|
|
115
|
+
try {
|
|
116
|
+
while (true) {
|
|
117
|
+
const page = await this.database.fetchSqlCursor(cursor);
|
|
118
|
+
const decoded = result(page.result);
|
|
119
|
+
if (output) output.rows.push(...decoded.rows);
|
|
120
|
+
else output = decoded;
|
|
121
|
+
if (page.exhausted) return output;
|
|
122
|
+
}
|
|
123
|
+
} finally {
|
|
124
|
+
await this.database.closeSqlCursor(cursor);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
async *stream(statement, parameters = [], chunkSize = 128) {
|
|
129
|
+
if (!Number.isInteger(chunkSize) || chunkSize < 1 || chunkSize > 4096)
|
|
130
|
+
throw new RangeError("chunkSize must be 1..4096");
|
|
131
|
+
if (this.#closed) throw new Error("SQL connection is closed");
|
|
132
|
+
const previous = this.#pending;
|
|
133
|
+
let release = () => {
|
|
134
|
+
};
|
|
135
|
+
if (!this.direct)
|
|
136
|
+
this.#pending = new Promise((resolve) => {
|
|
137
|
+
release = resolve;
|
|
138
|
+
});
|
|
139
|
+
await previous;
|
|
140
|
+
let cursor;
|
|
141
|
+
try {
|
|
142
|
+
cursor = await this.database.openSqlCursor(
|
|
143
|
+
request(statement, parameters, this.session, true)
|
|
144
|
+
);
|
|
145
|
+
while (true) {
|
|
146
|
+
const page = await this.database.fetchSqlCursor(cursor, chunkSize);
|
|
147
|
+
yield result(page.result);
|
|
148
|
+
if (page.exhausted) break;
|
|
149
|
+
}
|
|
150
|
+
} finally {
|
|
151
|
+
try {
|
|
152
|
+
if (cursor !== void 0) await this.database.closeSqlCursor(cursor);
|
|
153
|
+
} finally {
|
|
154
|
+
release();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
transaction(operation) {
|
|
159
|
+
return this.#run(async () => {
|
|
160
|
+
const transaction = new _Connection(this.database, this.session, true);
|
|
161
|
+
await transaction.execute("BEGIN ISOLATION LEVEL READ COMMITTED");
|
|
162
|
+
try {
|
|
163
|
+
const value = await operation(transaction);
|
|
164
|
+
await transaction.execute("COMMIT");
|
|
165
|
+
return value;
|
|
166
|
+
} catch (error) {
|
|
167
|
+
try {
|
|
168
|
+
await transaction.execute("ROLLBACK");
|
|
169
|
+
} catch {
|
|
170
|
+
}
|
|
171
|
+
throw error;
|
|
172
|
+
} finally {
|
|
173
|
+
transaction.#closed = true;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
async close() {
|
|
178
|
+
if (this.#closed) return;
|
|
179
|
+
this.#closed = true;
|
|
180
|
+
await this.#pending;
|
|
181
|
+
await this.database.closeSqlSession(this.session);
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// src/kysely.ts
|
|
186
|
+
var KyselyConnection = class {
|
|
187
|
+
constructor(sql) {
|
|
188
|
+
this.sql = sql;
|
|
189
|
+
}
|
|
190
|
+
sql;
|
|
191
|
+
async executeQuery(query, options) {
|
|
192
|
+
options?.signal?.throwIfAborted();
|
|
193
|
+
const result2 = await this.sql.query(query.sql, query.parameters);
|
|
194
|
+
return { rows: result2.rows, numAffectedRows: result2.rowsAffected };
|
|
195
|
+
}
|
|
196
|
+
async *streamQuery(query, chunkSize, options) {
|
|
197
|
+
options?.signal?.throwIfAborted();
|
|
198
|
+
for await (const page of this.sql.stream(query.sql, query.parameters, chunkSize)) {
|
|
199
|
+
options?.signal?.throwIfAborted();
|
|
200
|
+
yield { rows: page.rows, numAffectedRows: page.rowsAffected };
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
var AntflyDriver = class {
|
|
205
|
+
constructor(database) {
|
|
206
|
+
this.database = database;
|
|
207
|
+
}
|
|
208
|
+
database;
|
|
209
|
+
#connections = /* @__PURE__ */ new Set();
|
|
210
|
+
#destroyed = false;
|
|
211
|
+
async init() {
|
|
212
|
+
}
|
|
213
|
+
async acquireConnection(options) {
|
|
214
|
+
options?.signal?.throwIfAborted();
|
|
215
|
+
if (this.#destroyed) throw new Error("Antfly driver is destroyed");
|
|
216
|
+
const connection = new KyselyConnection(await Connection.open(this.database));
|
|
217
|
+
this.#connections.add(connection);
|
|
218
|
+
return connection;
|
|
219
|
+
}
|
|
220
|
+
async beginTransaction(connection, settings) {
|
|
221
|
+
if (settings.isolationLevel && settings.isolationLevel !== "read committed")
|
|
222
|
+
throw new Error("Antfly supports READ COMMITTED isolation");
|
|
223
|
+
await connection.sql.execute(
|
|
224
|
+
`BEGIN ISOLATION LEVEL READ COMMITTED${settings.accessMode === "read only" ? " READ ONLY" : ""}`
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
async commitTransaction(connection) {
|
|
228
|
+
await connection.sql.execute("COMMIT");
|
|
229
|
+
}
|
|
230
|
+
async rollbackTransaction(connection) {
|
|
231
|
+
await connection.sql.execute("ROLLBACK");
|
|
232
|
+
}
|
|
233
|
+
async savepoint(connection, name) {
|
|
234
|
+
await connection.sql.execute(`SAVEPOINT "${name.replaceAll('"', '""')}"`);
|
|
235
|
+
}
|
|
236
|
+
async rollbackToSavepoint(connection, name) {
|
|
237
|
+
await connection.sql.execute(
|
|
238
|
+
`ROLLBACK TO SAVEPOINT "${name.replaceAll('"', '""')}"`
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
async releaseSavepoint(connection, name) {
|
|
242
|
+
await connection.sql.execute(
|
|
243
|
+
`RELEASE SAVEPOINT "${name.replaceAll('"', '""')}"`
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
async releaseConnection(connection) {
|
|
247
|
+
const c = connection;
|
|
248
|
+
this.#connections.delete(c);
|
|
249
|
+
await c.sql.close();
|
|
250
|
+
}
|
|
251
|
+
async destroy() {
|
|
252
|
+
this.#destroyed = true;
|
|
253
|
+
await Promise.all([...this.#connections].map((c) => this.releaseConnection(c)));
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
var migrationLocks = /* @__PURE__ */ new WeakMap();
|
|
257
|
+
var AntflyAdapter = class extends import_kysely.PostgresAdapter {
|
|
258
|
+
constructor(database) {
|
|
259
|
+
super();
|
|
260
|
+
this.database = database;
|
|
261
|
+
}
|
|
262
|
+
database;
|
|
263
|
+
#release;
|
|
264
|
+
async acquireMigrationLock(_db, _options) {
|
|
265
|
+
const prior = migrationLocks.get(this.database) ?? Promise.resolve();
|
|
266
|
+
let release = () => {
|
|
267
|
+
};
|
|
268
|
+
const mine = new Promise((resolve) => {
|
|
269
|
+
release = resolve;
|
|
270
|
+
});
|
|
271
|
+
migrationLocks.set(
|
|
272
|
+
this.database,
|
|
273
|
+
prior.then(() => mine)
|
|
274
|
+
);
|
|
275
|
+
await prior;
|
|
276
|
+
this.#release = release;
|
|
277
|
+
}
|
|
278
|
+
async releaseMigrationLock(_db, _options) {
|
|
279
|
+
this.#release?.();
|
|
280
|
+
this.#release = void 0;
|
|
281
|
+
}
|
|
282
|
+
get supportsTransactionalDdl() {
|
|
283
|
+
return false;
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
var AntflyDialect = class {
|
|
287
|
+
constructor(config) {
|
|
288
|
+
this.config = config;
|
|
289
|
+
}
|
|
290
|
+
config;
|
|
291
|
+
createDriver() {
|
|
292
|
+
return new AntflyDriver(this.config.database);
|
|
293
|
+
}
|
|
294
|
+
createQueryCompiler() {
|
|
295
|
+
return new import_kysely.PostgresQueryCompiler();
|
|
296
|
+
}
|
|
297
|
+
createAdapter() {
|
|
298
|
+
return new AntflyAdapter(this.config.database);
|
|
299
|
+
}
|
|
300
|
+
createIntrospector() {
|
|
301
|
+
const database = this.config.database;
|
|
302
|
+
return {
|
|
303
|
+
async getSchemas() {
|
|
304
|
+
return [{ name: "public" }];
|
|
305
|
+
},
|
|
306
|
+
async getTables(options) {
|
|
307
|
+
const names = (await database.listTables()).filter(
|
|
308
|
+
(name) => name !== "default" && (options?.withInternalKyselyTables || !name.startsWith("kysely_"))
|
|
309
|
+
);
|
|
310
|
+
return Promise.all(
|
|
311
|
+
names.map(async (name) => {
|
|
312
|
+
const table = `"${name.replaceAll('"', '""')}"`;
|
|
313
|
+
const described = await database.sqlJson({
|
|
314
|
+
statement: `SELECT * FROM ${table} LIMIT 0`
|
|
315
|
+
});
|
|
316
|
+
const handle = await database.openTable(name);
|
|
317
|
+
let schema;
|
|
318
|
+
try {
|
|
319
|
+
schema = await handle.getSchema();
|
|
320
|
+
} finally {
|
|
321
|
+
await handle.close();
|
|
322
|
+
}
|
|
323
|
+
const document = schema.document_schemas?.[schema.default_type ?? "row"]?.schema;
|
|
324
|
+
return {
|
|
325
|
+
name,
|
|
326
|
+
schema: "public",
|
|
327
|
+
isView: false,
|
|
328
|
+
isForeign: false,
|
|
329
|
+
columns: described.columns.map((column) => ({
|
|
330
|
+
name: column.name,
|
|
331
|
+
dataType: column.type,
|
|
332
|
+
isNullable: column.name !== "_id" && !document?.required?.includes(column.name) && document?.properties?.[column.name]?.nullable !== false,
|
|
333
|
+
isAutoIncrementing: false,
|
|
334
|
+
hasDefaultValue: schema.column_defaults?.some((entry) => entry.column === column.name) ?? false
|
|
335
|
+
}))
|
|
336
|
+
};
|
|
337
|
+
})
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
};
|
|
343
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
344
|
+
0 && (module.exports = {
|
|
345
|
+
AntflyDialect
|
|
346
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Dialect, Driver, PostgresQueryCompiler, PostgresAdapter, Kysely, MigrationLockOptions, DatabaseIntrospector } from 'kysely';
|
|
2
|
+
import { D as Database } from './database-BY3WSLgt.cjs';
|
|
3
|
+
import 'koffi';
|
|
4
|
+
|
|
5
|
+
declare class AntflyAdapter extends PostgresAdapter {
|
|
6
|
+
#private;
|
|
7
|
+
readonly database: Database;
|
|
8
|
+
constructor(database: Database);
|
|
9
|
+
acquireMigrationLock(_db: Kysely<unknown>, _options: MigrationLockOptions): Promise<void>;
|
|
10
|
+
releaseMigrationLock(_db: Kysely<unknown>, _options: MigrationLockOptions): Promise<void>;
|
|
11
|
+
get supportsTransactionalDdl(): boolean;
|
|
12
|
+
}
|
|
13
|
+
/** PostgreSQL-style SQL compilation with an in-process Antfly session driver.
|
|
14
|
+
* The caller retains ownership of the supplied Database. */
|
|
15
|
+
declare class AntflyDialect implements Dialect {
|
|
16
|
+
readonly config: {
|
|
17
|
+
database: Database;
|
|
18
|
+
};
|
|
19
|
+
constructor(config: {
|
|
20
|
+
database: Database;
|
|
21
|
+
});
|
|
22
|
+
createDriver(): Driver;
|
|
23
|
+
createQueryCompiler(): PostgresQueryCompiler;
|
|
24
|
+
createAdapter(): AntflyAdapter;
|
|
25
|
+
createIntrospector(): DatabaseIntrospector;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { AntflyDialect };
|
package/dist/kysely.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Dialect, Driver, PostgresQueryCompiler, PostgresAdapter, Kysely, MigrationLockOptions, DatabaseIntrospector } from 'kysely';
|
|
2
|
+
import { D as Database } from './database-BY3WSLgt.js';
|
|
3
|
+
import 'koffi';
|
|
4
|
+
|
|
5
|
+
declare class AntflyAdapter extends PostgresAdapter {
|
|
6
|
+
#private;
|
|
7
|
+
readonly database: Database;
|
|
8
|
+
constructor(database: Database);
|
|
9
|
+
acquireMigrationLock(_db: Kysely<unknown>, _options: MigrationLockOptions): Promise<void>;
|
|
10
|
+
releaseMigrationLock(_db: Kysely<unknown>, _options: MigrationLockOptions): Promise<void>;
|
|
11
|
+
get supportsTransactionalDdl(): boolean;
|
|
12
|
+
}
|
|
13
|
+
/** PostgreSQL-style SQL compilation with an in-process Antfly session driver.
|
|
14
|
+
* The caller retains ownership of the supplied Database. */
|
|
15
|
+
declare class AntflyDialect implements Dialect {
|
|
16
|
+
readonly config: {
|
|
17
|
+
database: Database;
|
|
18
|
+
};
|
|
19
|
+
constructor(config: {
|
|
20
|
+
database: Database;
|
|
21
|
+
});
|
|
22
|
+
createDriver(): Driver;
|
|
23
|
+
createQueryCompiler(): PostgresQueryCompiler;
|
|
24
|
+
createAdapter(): AntflyAdapter;
|
|
25
|
+
createIntrospector(): DatabaseIntrospector;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export { AntflyDialect };
|
package/dist/kysely.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Connection
|
|
3
|
+
} from "./chunk-ELM44XQ4.js";
|
|
4
|
+
|
|
5
|
+
// src/kysely.ts
|
|
6
|
+
import {
|
|
7
|
+
PostgresAdapter,
|
|
8
|
+
PostgresQueryCompiler
|
|
9
|
+
} from "kysely";
|
|
10
|
+
var KyselyConnection = class {
|
|
11
|
+
constructor(sql) {
|
|
12
|
+
this.sql = sql;
|
|
13
|
+
}
|
|
14
|
+
sql;
|
|
15
|
+
async executeQuery(query, options) {
|
|
16
|
+
options?.signal?.throwIfAborted();
|
|
17
|
+
const result = await this.sql.query(query.sql, query.parameters);
|
|
18
|
+
return { rows: result.rows, numAffectedRows: result.rowsAffected };
|
|
19
|
+
}
|
|
20
|
+
async *streamQuery(query, chunkSize, options) {
|
|
21
|
+
options?.signal?.throwIfAborted();
|
|
22
|
+
for await (const page of this.sql.stream(query.sql, query.parameters, chunkSize)) {
|
|
23
|
+
options?.signal?.throwIfAborted();
|
|
24
|
+
yield { rows: page.rows, numAffectedRows: page.rowsAffected };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var AntflyDriver = class {
|
|
29
|
+
constructor(database) {
|
|
30
|
+
this.database = database;
|
|
31
|
+
}
|
|
32
|
+
database;
|
|
33
|
+
#connections = /* @__PURE__ */ new Set();
|
|
34
|
+
#destroyed = false;
|
|
35
|
+
async init() {
|
|
36
|
+
}
|
|
37
|
+
async acquireConnection(options) {
|
|
38
|
+
options?.signal?.throwIfAborted();
|
|
39
|
+
if (this.#destroyed) throw new Error("Antfly driver is destroyed");
|
|
40
|
+
const connection = new KyselyConnection(await Connection.open(this.database));
|
|
41
|
+
this.#connections.add(connection);
|
|
42
|
+
return connection;
|
|
43
|
+
}
|
|
44
|
+
async beginTransaction(connection, settings) {
|
|
45
|
+
if (settings.isolationLevel && settings.isolationLevel !== "read committed")
|
|
46
|
+
throw new Error("Antfly supports READ COMMITTED isolation");
|
|
47
|
+
await connection.sql.execute(
|
|
48
|
+
`BEGIN ISOLATION LEVEL READ COMMITTED${settings.accessMode === "read only" ? " READ ONLY" : ""}`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
async commitTransaction(connection) {
|
|
52
|
+
await connection.sql.execute("COMMIT");
|
|
53
|
+
}
|
|
54
|
+
async rollbackTransaction(connection) {
|
|
55
|
+
await connection.sql.execute("ROLLBACK");
|
|
56
|
+
}
|
|
57
|
+
async savepoint(connection, name) {
|
|
58
|
+
await connection.sql.execute(`SAVEPOINT "${name.replaceAll('"', '""')}"`);
|
|
59
|
+
}
|
|
60
|
+
async rollbackToSavepoint(connection, name) {
|
|
61
|
+
await connection.sql.execute(
|
|
62
|
+
`ROLLBACK TO SAVEPOINT "${name.replaceAll('"', '""')}"`
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
async releaseSavepoint(connection, name) {
|
|
66
|
+
await connection.sql.execute(
|
|
67
|
+
`RELEASE SAVEPOINT "${name.replaceAll('"', '""')}"`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
async releaseConnection(connection) {
|
|
71
|
+
const c = connection;
|
|
72
|
+
this.#connections.delete(c);
|
|
73
|
+
await c.sql.close();
|
|
74
|
+
}
|
|
75
|
+
async destroy() {
|
|
76
|
+
this.#destroyed = true;
|
|
77
|
+
await Promise.all([...this.#connections].map((c) => this.releaseConnection(c)));
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var migrationLocks = /* @__PURE__ */ new WeakMap();
|
|
81
|
+
var AntflyAdapter = class extends PostgresAdapter {
|
|
82
|
+
constructor(database) {
|
|
83
|
+
super();
|
|
84
|
+
this.database = database;
|
|
85
|
+
}
|
|
86
|
+
database;
|
|
87
|
+
#release;
|
|
88
|
+
async acquireMigrationLock(_db, _options) {
|
|
89
|
+
const prior = migrationLocks.get(this.database) ?? Promise.resolve();
|
|
90
|
+
let release = () => {
|
|
91
|
+
};
|
|
92
|
+
const mine = new Promise((resolve) => {
|
|
93
|
+
release = resolve;
|
|
94
|
+
});
|
|
95
|
+
migrationLocks.set(
|
|
96
|
+
this.database,
|
|
97
|
+
prior.then(() => mine)
|
|
98
|
+
);
|
|
99
|
+
await prior;
|
|
100
|
+
this.#release = release;
|
|
101
|
+
}
|
|
102
|
+
async releaseMigrationLock(_db, _options) {
|
|
103
|
+
this.#release?.();
|
|
104
|
+
this.#release = void 0;
|
|
105
|
+
}
|
|
106
|
+
get supportsTransactionalDdl() {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var AntflyDialect = class {
|
|
111
|
+
constructor(config) {
|
|
112
|
+
this.config = config;
|
|
113
|
+
}
|
|
114
|
+
config;
|
|
115
|
+
createDriver() {
|
|
116
|
+
return new AntflyDriver(this.config.database);
|
|
117
|
+
}
|
|
118
|
+
createQueryCompiler() {
|
|
119
|
+
return new PostgresQueryCompiler();
|
|
120
|
+
}
|
|
121
|
+
createAdapter() {
|
|
122
|
+
return new AntflyAdapter(this.config.database);
|
|
123
|
+
}
|
|
124
|
+
createIntrospector() {
|
|
125
|
+
const database = this.config.database;
|
|
126
|
+
return {
|
|
127
|
+
async getSchemas() {
|
|
128
|
+
return [{ name: "public" }];
|
|
129
|
+
},
|
|
130
|
+
async getTables(options) {
|
|
131
|
+
const names = (await database.listTables()).filter(
|
|
132
|
+
(name) => name !== "default" && (options?.withInternalKyselyTables || !name.startsWith("kysely_"))
|
|
133
|
+
);
|
|
134
|
+
return Promise.all(
|
|
135
|
+
names.map(async (name) => {
|
|
136
|
+
const table = `"${name.replaceAll('"', '""')}"`;
|
|
137
|
+
const described = await database.sqlJson({
|
|
138
|
+
statement: `SELECT * FROM ${table} LIMIT 0`
|
|
139
|
+
});
|
|
140
|
+
const handle = await database.openTable(name);
|
|
141
|
+
let schema;
|
|
142
|
+
try {
|
|
143
|
+
schema = await handle.getSchema();
|
|
144
|
+
} finally {
|
|
145
|
+
await handle.close();
|
|
146
|
+
}
|
|
147
|
+
const document = schema.document_schemas?.[schema.default_type ?? "row"]?.schema;
|
|
148
|
+
return {
|
|
149
|
+
name,
|
|
150
|
+
schema: "public",
|
|
151
|
+
isView: false,
|
|
152
|
+
isForeign: false,
|
|
153
|
+
columns: described.columns.map((column) => ({
|
|
154
|
+
name: column.name,
|
|
155
|
+
dataType: column.type,
|
|
156
|
+
isNullable: column.name !== "_id" && !document?.required?.includes(column.name) && document?.properties?.[column.name]?.nullable !== false,
|
|
157
|
+
isAutoIncrementing: false,
|
|
158
|
+
hasDefaultValue: schema.column_defaults?.some((entry) => entry.column === column.name) ?? false
|
|
159
|
+
}))
|
|
160
|
+
};
|
|
161
|
+
})
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
export {
|
|
168
|
+
AntflyDialect
|
|
169
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,89 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antfly/embedded",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0-rc.1",
|
|
4
|
+
"description": "TypeScript/Node binding for local Antfly databases and inference (libantfly C ABI)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./kysely": {
|
|
16
|
+
"types": "./dist/kysely.d.ts",
|
|
17
|
+
"import": "./dist/kysely.js",
|
|
18
|
+
"require": "./dist/kysely.cjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup",
|
|
28
|
+
"dev": "tsup --watch",
|
|
29
|
+
"test": "vitest run --maxWorkers=2",
|
|
30
|
+
"test:watch": "vitest",
|
|
31
|
+
"typecheck": "tsc -p tsconfig.typecheck.json",
|
|
32
|
+
"lint": "biome check .",
|
|
33
|
+
"clean": "rm -rf dist",
|
|
34
|
+
"prepare": "pnpm run build",
|
|
35
|
+
"prepublishOnly": "pnpm run typecheck && pnpm run lint && pnpm run build && pnpm run test"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"antfly",
|
|
39
|
+
"lite",
|
|
40
|
+
"embedded",
|
|
41
|
+
"database",
|
|
42
|
+
"vector",
|
|
43
|
+
"search",
|
|
44
|
+
"koffi",
|
|
45
|
+
"ffi"
|
|
46
|
+
],
|
|
47
|
+
"author": "Antfly Contributors",
|
|
48
|
+
"license": "Apache-2.0",
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/antflydb/antfly.git",
|
|
52
|
+
"directory": "ts/packages/embedded"
|
|
53
|
+
},
|
|
54
|
+
"bugs": {
|
|
55
|
+
"url": "https://github.com/antflydb/antfly/issues"
|
|
56
|
+
},
|
|
57
|
+
"homepage": "https://github.com/antflydb/antfly/tree/main/ts/packages/embedded#readme",
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@types/node": "^25.9.3",
|
|
60
|
+
"kysely": "^0.29.6",
|
|
61
|
+
"tsup": "^8.5.1",
|
|
62
|
+
"typescript": "~5.9.3",
|
|
63
|
+
"vitest": "^4.1.9"
|
|
64
|
+
},
|
|
65
|
+
"dependencies": {
|
|
66
|
+
"koffi": "^3.3.1"
|
|
67
|
+
},
|
|
68
|
+
"engines": {
|
|
69
|
+
"node": ">=24.0"
|
|
70
|
+
},
|
|
71
|
+
"os": [
|
|
72
|
+
"darwin",
|
|
73
|
+
"linux",
|
|
74
|
+
"win32"
|
|
75
|
+
],
|
|
76
|
+
"optionalDependencies": {
|
|
77
|
+
"@antfly/embedded-darwin-arm64": "0.3.0-rc.1",
|
|
78
|
+
"@antfly/embedded-linux-arm64": "0.3.0-rc.1",
|
|
79
|
+
"@antfly/embedded-linux-x64": "0.3.0-rc.1"
|
|
80
|
+
},
|
|
81
|
+
"peerDependencies": {
|
|
82
|
+
"kysely": "^0.29.6"
|
|
83
|
+
},
|
|
84
|
+
"peerDependenciesMeta": {
|
|
85
|
+
"kysely": {
|
|
86
|
+
"optional": true
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|