@prisma/adapter-libsql 6.6.0-dev.1 → 6.6.0-dev.100
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 +4 -3
- package/dist/index-node.d.mts +22 -0
- package/dist/index-node.d.ts +22 -0
- package/dist/index-node.js +325 -0
- package/dist/{index.mjs → index-node.mjs} +47 -34
- package/dist/index-web.d.mts +24 -0
- package/dist/index-web.d.ts +24 -0
- package/dist/{index.js → index-web.js} +52 -37
- package/dist/index-web.mjs +298 -0
- package/package.json +24 -14
- package/dist/index.d.mts +0 -48
- package/dist/index.d.ts +0 -48
package/README.md
CHANGED
|
@@ -40,18 +40,19 @@ Update your Prisma Client instance to use the libSQL database Client:
|
|
|
40
40
|
// Import needed packages
|
|
41
41
|
import { PrismaClient } from '@prisma/client'
|
|
42
42
|
import { PrismaLibSQL } from '@prisma/adapter-libsql'
|
|
43
|
-
|
|
43
|
+
// You can alternatively use the web version of the client if you're running in
|
|
44
|
+
// a constrained environment where the standard libsql client doesn't work:
|
|
45
|
+
// import { PrismaLibSQL } from '@prisma/adapter-libsql/web'
|
|
44
46
|
|
|
45
47
|
// Setup
|
|
46
48
|
const connectionString = `${process.env.TURSO_DATABASE_URL}`
|
|
47
49
|
const authToken = `${process.env.TURSO_AUTH_TOKEN}`
|
|
48
50
|
|
|
49
51
|
// Init prisma client
|
|
50
|
-
const
|
|
52
|
+
const adapter = new PrismaLibSQL({
|
|
51
53
|
url: connectionString,
|
|
52
54
|
authToken,
|
|
53
55
|
})
|
|
54
|
-
const adapter = new PrismaLibSQL(libsql)
|
|
55
56
|
const prisma = new PrismaClient({ adapter })
|
|
56
57
|
```
|
|
57
58
|
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Client } from '@libsql/client';
|
|
2
|
+
import { Config } from '@libsql/client';
|
|
3
|
+
import type { SqlDriverAdapter } from '@prisma/driver-adapter-utils';
|
|
4
|
+
import type { SqlMigrationAwareDriverAdapterFactory } from '@prisma/driver-adapter-utils';
|
|
5
|
+
|
|
6
|
+
export declare class PrismaLibSQL extends PrismaLibSQLAdapterFactoryBase {
|
|
7
|
+
createClient(config: Config): Client;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
declare abstract class PrismaLibSQLAdapterFactoryBase implements SqlMigrationAwareDriverAdapterFactory {
|
|
11
|
+
private readonly config;
|
|
12
|
+
readonly provider = "sqlite";
|
|
13
|
+
readonly adapterName: string;
|
|
14
|
+
constructor(config: Config);
|
|
15
|
+
connect(): Promise<SqlDriverAdapter>;
|
|
16
|
+
connectToShadowDb(): Promise<SqlDriverAdapter>;
|
|
17
|
+
abstract createClient(config: Config): StdClient;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare type StdClient = Client;
|
|
21
|
+
|
|
22
|
+
export { }
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Client } from '@libsql/client';
|
|
2
|
+
import { Config } from '@libsql/client';
|
|
3
|
+
import type { SqlDriverAdapter } from '@prisma/driver-adapter-utils';
|
|
4
|
+
import type { SqlMigrationAwareDriverAdapterFactory } from '@prisma/driver-adapter-utils';
|
|
5
|
+
|
|
6
|
+
export declare class PrismaLibSQL extends PrismaLibSQLAdapterFactoryBase {
|
|
7
|
+
createClient(config: Config): Client;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
declare abstract class PrismaLibSQLAdapterFactoryBase implements SqlMigrationAwareDriverAdapterFactory {
|
|
11
|
+
private readonly config;
|
|
12
|
+
readonly provider = "sqlite";
|
|
13
|
+
readonly adapterName: string;
|
|
14
|
+
constructor(config: Config);
|
|
15
|
+
connect(): Promise<SqlDriverAdapter>;
|
|
16
|
+
connectToShadowDb(): Promise<SqlDriverAdapter>;
|
|
17
|
+
abstract createClient(config: Config): StdClient;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
declare type StdClient = Client;
|
|
21
|
+
|
|
22
|
+
export { }
|
|
@@ -0,0 +1,325 @@
|
|
|
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 name2 in all)
|
|
8
|
+
__defProp(target, name2, { get: all[name2], 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/index-node.ts
|
|
21
|
+
var index_node_exports = {};
|
|
22
|
+
__export(index_node_exports, {
|
|
23
|
+
PrismaLibSQL: () => PrismaLibSQLAdapterFactory
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_node_exports);
|
|
26
|
+
|
|
27
|
+
// src/libsql-node.ts
|
|
28
|
+
var import_client = require("@libsql/client");
|
|
29
|
+
|
|
30
|
+
// src/libsql.ts
|
|
31
|
+
var import_driver_adapter_utils2 = require("@prisma/driver-adapter-utils");
|
|
32
|
+
var import_async_mutex = require("async-mutex");
|
|
33
|
+
|
|
34
|
+
// package.json
|
|
35
|
+
var name = "@prisma/adapter-libsql";
|
|
36
|
+
|
|
37
|
+
// src/conversion.ts
|
|
38
|
+
var import_driver_adapter_utils = require("@prisma/driver-adapter-utils");
|
|
39
|
+
var debug = (0, import_driver_adapter_utils.Debug)("prisma:driver-adapter:libsql:conversion");
|
|
40
|
+
function mapDeclType(declType) {
|
|
41
|
+
switch (declType.toUpperCase()) {
|
|
42
|
+
case "":
|
|
43
|
+
return null;
|
|
44
|
+
case "DECIMAL":
|
|
45
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Numeric;
|
|
46
|
+
case "FLOAT":
|
|
47
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Float;
|
|
48
|
+
case "DOUBLE":
|
|
49
|
+
case "DOUBLE PRECISION":
|
|
50
|
+
case "NUMERIC":
|
|
51
|
+
case "REAL":
|
|
52
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Double;
|
|
53
|
+
case "TINYINT":
|
|
54
|
+
case "SMALLINT":
|
|
55
|
+
case "MEDIUMINT":
|
|
56
|
+
case "INT":
|
|
57
|
+
case "INTEGER":
|
|
58
|
+
case "SERIAL":
|
|
59
|
+
case "INT2":
|
|
60
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Int32;
|
|
61
|
+
case "BIGINT":
|
|
62
|
+
case "UNSIGNED BIG INT":
|
|
63
|
+
case "INT8":
|
|
64
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Int64;
|
|
65
|
+
case "DATETIME":
|
|
66
|
+
case "TIMESTAMP":
|
|
67
|
+
return import_driver_adapter_utils.ColumnTypeEnum.DateTime;
|
|
68
|
+
case "TIME":
|
|
69
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Time;
|
|
70
|
+
case "DATE":
|
|
71
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Date;
|
|
72
|
+
case "TEXT":
|
|
73
|
+
case "CLOB":
|
|
74
|
+
case "CHARACTER":
|
|
75
|
+
case "VARCHAR":
|
|
76
|
+
case "VARYING CHARACTER":
|
|
77
|
+
case "NCHAR":
|
|
78
|
+
case "NATIVE CHARACTER":
|
|
79
|
+
case "NVARCHAR":
|
|
80
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Text;
|
|
81
|
+
case "BLOB":
|
|
82
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Bytes;
|
|
83
|
+
case "BOOLEAN":
|
|
84
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Boolean;
|
|
85
|
+
case "JSONB":
|
|
86
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Json;
|
|
87
|
+
default:
|
|
88
|
+
debug("unknown decltype:", declType);
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function mapDeclaredColumnTypes(columnTypes) {
|
|
93
|
+
const emptyIndices = /* @__PURE__ */ new Set();
|
|
94
|
+
const result = columnTypes.map((typeName, index) => {
|
|
95
|
+
const mappedType = mapDeclType(typeName);
|
|
96
|
+
if (mappedType === null) {
|
|
97
|
+
emptyIndices.add(index);
|
|
98
|
+
}
|
|
99
|
+
return mappedType;
|
|
100
|
+
});
|
|
101
|
+
return [result, emptyIndices];
|
|
102
|
+
}
|
|
103
|
+
function getColumnTypes(declaredTypes, rows) {
|
|
104
|
+
const [columnTypes, emptyIndices] = mapDeclaredColumnTypes(declaredTypes);
|
|
105
|
+
if (emptyIndices.size === 0) {
|
|
106
|
+
return columnTypes;
|
|
107
|
+
}
|
|
108
|
+
columnLoop: for (const columnIndex of emptyIndices) {
|
|
109
|
+
for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
|
|
110
|
+
const candidateValue = rows[rowIndex][columnIndex];
|
|
111
|
+
if (candidateValue !== null) {
|
|
112
|
+
columnTypes[columnIndex] = inferColumnType(candidateValue);
|
|
113
|
+
continue columnLoop;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
columnTypes[columnIndex] = import_driver_adapter_utils.ColumnTypeEnum.Int32;
|
|
117
|
+
}
|
|
118
|
+
return columnTypes;
|
|
119
|
+
}
|
|
120
|
+
function inferColumnType(value) {
|
|
121
|
+
switch (typeof value) {
|
|
122
|
+
case "string":
|
|
123
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Text;
|
|
124
|
+
case "bigint":
|
|
125
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Int64;
|
|
126
|
+
case "boolean":
|
|
127
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Boolean;
|
|
128
|
+
case "number":
|
|
129
|
+
return import_driver_adapter_utils.ColumnTypeEnum.UnknownNumber;
|
|
130
|
+
case "object":
|
|
131
|
+
return inferObjectType(value);
|
|
132
|
+
default:
|
|
133
|
+
throw new UnexpectedTypeError(value);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function inferObjectType(value) {
|
|
137
|
+
if (value instanceof ArrayBuffer) {
|
|
138
|
+
return import_driver_adapter_utils.ColumnTypeEnum.Bytes;
|
|
139
|
+
}
|
|
140
|
+
throw new UnexpectedTypeError(value);
|
|
141
|
+
}
|
|
142
|
+
var UnexpectedTypeError = class extends Error {
|
|
143
|
+
name = "UnexpectedTypeError";
|
|
144
|
+
constructor(value) {
|
|
145
|
+
const type = typeof value;
|
|
146
|
+
const repr = type === "object" ? JSON.stringify(value) : String(value);
|
|
147
|
+
super(`unexpected value of type ${type}: ${repr}`);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
function mapRow(row, columnTypes) {
|
|
151
|
+
const result = Array.from(row);
|
|
152
|
+
for (let i = 0; i < result.length; i++) {
|
|
153
|
+
const value = result[i];
|
|
154
|
+
if (value instanceof ArrayBuffer) {
|
|
155
|
+
result[i] = Array.from(new Uint8Array(value));
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (typeof value === "number" && (columnTypes[i] === import_driver_adapter_utils.ColumnTypeEnum.Int32 || columnTypes[i] === import_driver_adapter_utils.ColumnTypeEnum.Int64) && !Number.isInteger(value)) {
|
|
159
|
+
result[i] = Math.trunc(value);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (["number", "bigint"].includes(typeof value) && columnTypes[i] === import_driver_adapter_utils.ColumnTypeEnum.DateTime) {
|
|
163
|
+
result[i] = new Date(Number(value)).toISOString();
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (typeof value === "bigint") {
|
|
167
|
+
result[i] = value.toString();
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// src/libsql.ts
|
|
175
|
+
var debug2 = (0, import_driver_adapter_utils2.Debug)("prisma:driver-adapter:libsql");
|
|
176
|
+
var LOCK_TAG = Symbol();
|
|
177
|
+
var LibSqlQueryable = class {
|
|
178
|
+
constructor(client) {
|
|
179
|
+
this.client = client;
|
|
180
|
+
}
|
|
181
|
+
provider = "sqlite";
|
|
182
|
+
adapterName = name;
|
|
183
|
+
[LOCK_TAG] = new import_async_mutex.Mutex();
|
|
184
|
+
/**
|
|
185
|
+
* Execute a query given as SQL, interpolating the given parameters.
|
|
186
|
+
*/
|
|
187
|
+
async queryRaw(query) {
|
|
188
|
+
const tag = "[js::query_raw]";
|
|
189
|
+
debug2(`${tag} %O`, query);
|
|
190
|
+
const { columns, rows, columnTypes: declaredColumnTypes } = await this.performIO(query);
|
|
191
|
+
const columnTypes = getColumnTypes(declaredColumnTypes, rows);
|
|
192
|
+
return {
|
|
193
|
+
columnNames: columns,
|
|
194
|
+
columnTypes,
|
|
195
|
+
rows: rows.map((row) => mapRow(row, columnTypes))
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Execute a query given as SQL, interpolating the given parameters and
|
|
200
|
+
* returning the number of affected rows.
|
|
201
|
+
* Note: Queryable expects a u64, but napi.rs only supports u32.
|
|
202
|
+
*/
|
|
203
|
+
async executeRaw(query) {
|
|
204
|
+
const tag = "[js::execute_raw]";
|
|
205
|
+
debug2(`${tag} %O`, query);
|
|
206
|
+
return (await this.performIO(query)).rowsAffected ?? 0;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Run a query against the database, returning the result set.
|
|
210
|
+
* Should the query fail due to a connection error, the connection is
|
|
211
|
+
* marked as unhealthy.
|
|
212
|
+
*/
|
|
213
|
+
async performIO(query) {
|
|
214
|
+
const release = await this[LOCK_TAG].acquire();
|
|
215
|
+
try {
|
|
216
|
+
const result = await this.client.execute(query);
|
|
217
|
+
return result;
|
|
218
|
+
} catch (e) {
|
|
219
|
+
this.onError(e);
|
|
220
|
+
} finally {
|
|
221
|
+
release();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
onError(error) {
|
|
225
|
+
debug2("Error in performIO: %O", error);
|
|
226
|
+
const rawCode = error["rawCode"] ?? error.cause?.["rawCode"];
|
|
227
|
+
if (typeof rawCode === "number") {
|
|
228
|
+
throw new import_driver_adapter_utils2.DriverAdapterError({
|
|
229
|
+
kind: "sqlite",
|
|
230
|
+
extendedCode: rawCode,
|
|
231
|
+
message: error.message
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
throw error;
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
var LibSqlTransaction = class extends LibSqlQueryable {
|
|
238
|
+
constructor(client, options, unlockParent) {
|
|
239
|
+
super(client);
|
|
240
|
+
this.options = options;
|
|
241
|
+
this.unlockParent = unlockParent;
|
|
242
|
+
}
|
|
243
|
+
async commit() {
|
|
244
|
+
debug2(`[js::commit]`);
|
|
245
|
+
try {
|
|
246
|
+
await this.client.commit();
|
|
247
|
+
} finally {
|
|
248
|
+
this.unlockParent();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
async rollback() {
|
|
252
|
+
debug2(`[js::rollback]`);
|
|
253
|
+
try {
|
|
254
|
+
await this.client.rollback();
|
|
255
|
+
} catch (error) {
|
|
256
|
+
debug2("error in rollback:", error);
|
|
257
|
+
} finally {
|
|
258
|
+
this.unlockParent();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
var PrismaLibSQLAdapter = class extends LibSqlQueryable {
|
|
263
|
+
constructor(client) {
|
|
264
|
+
super(client);
|
|
265
|
+
}
|
|
266
|
+
async executeScript(script) {
|
|
267
|
+
const release = await this[LOCK_TAG].acquire();
|
|
268
|
+
try {
|
|
269
|
+
await this.client.executeMultiple(script);
|
|
270
|
+
} catch (e) {
|
|
271
|
+
this.onError(e);
|
|
272
|
+
} finally {
|
|
273
|
+
release();
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
async startTransaction(isolationLevel) {
|
|
277
|
+
if (isolationLevel && isolationLevel !== "SERIALIZABLE") {
|
|
278
|
+
throw new import_driver_adapter_utils2.DriverAdapterError({
|
|
279
|
+
kind: "InvalidIsolationLevel",
|
|
280
|
+
level: isolationLevel
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
const options = {
|
|
284
|
+
usePhantomQuery: true
|
|
285
|
+
};
|
|
286
|
+
const tag = "[js::startTransaction]";
|
|
287
|
+
debug2("%s options: %O", tag, options);
|
|
288
|
+
const release = await this[LOCK_TAG].acquire();
|
|
289
|
+
try {
|
|
290
|
+
const tx = await this.client.transaction("deferred");
|
|
291
|
+
return new LibSqlTransaction(tx, options, release);
|
|
292
|
+
} catch (e) {
|
|
293
|
+
release();
|
|
294
|
+
throw e;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
dispose() {
|
|
298
|
+
this.client.close();
|
|
299
|
+
return Promise.resolve();
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
var PrismaLibSQLAdapterFactoryBase = class {
|
|
303
|
+
constructor(config) {
|
|
304
|
+
this.config = config;
|
|
305
|
+
}
|
|
306
|
+
provider = "sqlite";
|
|
307
|
+
adapterName = name;
|
|
308
|
+
connect() {
|
|
309
|
+
return Promise.resolve(new PrismaLibSQLAdapter(this.createClient(this.config)));
|
|
310
|
+
}
|
|
311
|
+
connectToShadowDb() {
|
|
312
|
+
return Promise.resolve(new PrismaLibSQLAdapter(this.createClient({ ...this.config, url: ":memory:" })));
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
// src/libsql-node.ts
|
|
317
|
+
var PrismaLibSQLAdapterFactory = class extends PrismaLibSQLAdapterFactoryBase {
|
|
318
|
+
createClient(config) {
|
|
319
|
+
return (0, import_client.createClient)(config);
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
323
|
+
0 && (module.exports = {
|
|
324
|
+
PrismaLibSQL
|
|
325
|
+
});
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
1
|
+
// src/libsql-node.ts
|
|
2
|
+
import { createClient } from "@libsql/client";
|
|
4
3
|
|
|
5
4
|
// src/libsql.ts
|
|
6
5
|
import { Debug as Debug2, DriverAdapterError } from "@prisma/driver-adapter-utils";
|
|
@@ -115,11 +114,11 @@ function inferObjectType(value) {
|
|
|
115
114
|
throw new UnexpectedTypeError(value);
|
|
116
115
|
}
|
|
117
116
|
var UnexpectedTypeError = class extends Error {
|
|
117
|
+
name = "UnexpectedTypeError";
|
|
118
118
|
constructor(value) {
|
|
119
119
|
const type = typeof value;
|
|
120
120
|
const repr = type === "object" ? JSON.stringify(value) : String(value);
|
|
121
121
|
super(`unexpected value of type ${type}: ${repr}`);
|
|
122
|
-
__publicField(this, "name", "UnexpectedTypeError");
|
|
123
122
|
}
|
|
124
123
|
};
|
|
125
124
|
function mapRow(row, columnTypes) {
|
|
@@ -149,15 +148,13 @@ function mapRow(row, columnTypes) {
|
|
|
149
148
|
// src/libsql.ts
|
|
150
149
|
var debug2 = Debug2("prisma:driver-adapter:libsql");
|
|
151
150
|
var LOCK_TAG = Symbol();
|
|
152
|
-
var _a;
|
|
153
|
-
_a = LOCK_TAG;
|
|
154
151
|
var LibSqlQueryable = class {
|
|
155
152
|
constructor(client) {
|
|
156
153
|
this.client = client;
|
|
157
|
-
__publicField(this, "provider", "sqlite");
|
|
158
|
-
__publicField(this, "adapterName", name);
|
|
159
|
-
__publicField(this, _a, new Mutex());
|
|
160
154
|
}
|
|
155
|
+
provider = "sqlite";
|
|
156
|
+
adapterName = name;
|
|
157
|
+
[LOCK_TAG] = new Mutex();
|
|
161
158
|
/**
|
|
162
159
|
* Execute a query given as SQL, interpolating the given parameters.
|
|
163
160
|
*/
|
|
@@ -236,28 +233,7 @@ var LibSqlTransaction = class extends LibSqlQueryable {
|
|
|
236
233
|
}
|
|
237
234
|
}
|
|
238
235
|
};
|
|
239
|
-
var
|
|
240
|
-
constructor(client, release) {
|
|
241
|
-
super(client);
|
|
242
|
-
this.client = client;
|
|
243
|
-
this.release = release;
|
|
244
|
-
}
|
|
245
|
-
async startTransaction() {
|
|
246
|
-
const options = {
|
|
247
|
-
usePhantomQuery: true
|
|
248
|
-
};
|
|
249
|
-
const tag = "[js::startTransaction]";
|
|
250
|
-
debug2("%s options: %O", tag, options);
|
|
251
|
-
try {
|
|
252
|
-
const tx = await this.client.transaction("deferred");
|
|
253
|
-
return new LibSqlTransaction(tx, options, this.release);
|
|
254
|
-
} catch (e) {
|
|
255
|
-
this.release();
|
|
256
|
-
throw e;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
};
|
|
260
|
-
var PrismaLibSQL = class extends LibSqlQueryable {
|
|
236
|
+
var PrismaLibSQLAdapter = class extends LibSqlQueryable {
|
|
261
237
|
constructor(client) {
|
|
262
238
|
super(client);
|
|
263
239
|
}
|
|
@@ -271,15 +247,52 @@ var PrismaLibSQL = class extends LibSqlQueryable {
|
|
|
271
247
|
release();
|
|
272
248
|
}
|
|
273
249
|
}
|
|
274
|
-
async
|
|
250
|
+
async startTransaction(isolationLevel) {
|
|
251
|
+
if (isolationLevel && isolationLevel !== "SERIALIZABLE") {
|
|
252
|
+
throw new DriverAdapterError({
|
|
253
|
+
kind: "InvalidIsolationLevel",
|
|
254
|
+
level: isolationLevel
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
const options = {
|
|
258
|
+
usePhantomQuery: true
|
|
259
|
+
};
|
|
260
|
+
const tag = "[js::startTransaction]";
|
|
261
|
+
debug2("%s options: %O", tag, options);
|
|
275
262
|
const release = await this[LOCK_TAG].acquire();
|
|
276
|
-
|
|
263
|
+
try {
|
|
264
|
+
const tx = await this.client.transaction("deferred");
|
|
265
|
+
return new LibSqlTransaction(tx, options, release);
|
|
266
|
+
} catch (e) {
|
|
267
|
+
release();
|
|
268
|
+
throw e;
|
|
269
|
+
}
|
|
277
270
|
}
|
|
278
271
|
dispose() {
|
|
279
272
|
this.client.close();
|
|
280
273
|
return Promise.resolve();
|
|
281
274
|
}
|
|
282
275
|
};
|
|
276
|
+
var PrismaLibSQLAdapterFactoryBase = class {
|
|
277
|
+
constructor(config) {
|
|
278
|
+
this.config = config;
|
|
279
|
+
}
|
|
280
|
+
provider = "sqlite";
|
|
281
|
+
adapterName = name;
|
|
282
|
+
connect() {
|
|
283
|
+
return Promise.resolve(new PrismaLibSQLAdapter(this.createClient(this.config)));
|
|
284
|
+
}
|
|
285
|
+
connectToShadowDb() {
|
|
286
|
+
return Promise.resolve(new PrismaLibSQLAdapter(this.createClient({ ...this.config, url: ":memory:" })));
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// src/libsql-node.ts
|
|
291
|
+
var PrismaLibSQLAdapterFactory = class extends PrismaLibSQLAdapterFactoryBase {
|
|
292
|
+
createClient(config) {
|
|
293
|
+
return createClient(config);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
283
296
|
export {
|
|
284
|
-
PrismaLibSQL
|
|
297
|
+
PrismaLibSQLAdapterFactory as PrismaLibSQL
|
|
285
298
|
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Client } from '@libsql/client';
|
|
2
|
+
import { Client as Client_2 } from '@libsql/client/web';
|
|
3
|
+
import type { Config } from '@libsql/client';
|
|
4
|
+
import { Config as Config_2 } from '@libsql/client/web';
|
|
5
|
+
import type { SqlDriverAdapter } from '@prisma/driver-adapter-utils';
|
|
6
|
+
import type { SqlMigrationAwareDriverAdapterFactory } from '@prisma/driver-adapter-utils';
|
|
7
|
+
|
|
8
|
+
export declare class PrismaLibSQL extends PrismaLibSQLAdapterFactoryBase {
|
|
9
|
+
createClient(config: Config_2): Client_2;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare abstract class PrismaLibSQLAdapterFactoryBase implements SqlMigrationAwareDriverAdapterFactory {
|
|
13
|
+
private readonly config;
|
|
14
|
+
readonly provider = "sqlite";
|
|
15
|
+
readonly adapterName: string;
|
|
16
|
+
constructor(config: Config);
|
|
17
|
+
connect(): Promise<SqlDriverAdapter>;
|
|
18
|
+
connectToShadowDb(): Promise<SqlDriverAdapter>;
|
|
19
|
+
abstract createClient(config: Config): StdClient;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare type StdClient = Client;
|
|
23
|
+
|
|
24
|
+
export { }
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Client } from '@libsql/client';
|
|
2
|
+
import { Client as Client_2 } from '@libsql/client/web';
|
|
3
|
+
import type { Config } from '@libsql/client';
|
|
4
|
+
import { Config as Config_2 } from '@libsql/client/web';
|
|
5
|
+
import type { SqlDriverAdapter } from '@prisma/driver-adapter-utils';
|
|
6
|
+
import type { SqlMigrationAwareDriverAdapterFactory } from '@prisma/driver-adapter-utils';
|
|
7
|
+
|
|
8
|
+
export declare class PrismaLibSQL extends PrismaLibSQLAdapterFactoryBase {
|
|
9
|
+
createClient(config: Config_2): Client_2;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
declare abstract class PrismaLibSQLAdapterFactoryBase implements SqlMigrationAwareDriverAdapterFactory {
|
|
13
|
+
private readonly config;
|
|
14
|
+
readonly provider = "sqlite";
|
|
15
|
+
readonly adapterName: string;
|
|
16
|
+
constructor(config: Config);
|
|
17
|
+
connect(): Promise<SqlDriverAdapter>;
|
|
18
|
+
connectToShadowDb(): Promise<SqlDriverAdapter>;
|
|
19
|
+
abstract createClient(config: Config): StdClient;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare type StdClient = Client;
|
|
23
|
+
|
|
24
|
+
export { }
|
|
@@ -3,7 +3,6 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
7
6
|
var __export = (target, all) => {
|
|
8
7
|
for (var name2 in all)
|
|
9
8
|
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
@@ -17,14 +16,16 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
16
|
return to;
|
|
18
17
|
};
|
|
19
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
21
19
|
|
|
22
|
-
// src/index.ts
|
|
23
|
-
var
|
|
24
|
-
__export(
|
|
25
|
-
PrismaLibSQL: () =>
|
|
20
|
+
// src/index-web.ts
|
|
21
|
+
var index_web_exports = {};
|
|
22
|
+
__export(index_web_exports, {
|
|
23
|
+
PrismaLibSQL: () => PrismaLibSQLWebAdapterFactory
|
|
26
24
|
});
|
|
27
|
-
module.exports = __toCommonJS(
|
|
25
|
+
module.exports = __toCommonJS(index_web_exports);
|
|
26
|
+
|
|
27
|
+
// src/libsql-web.ts
|
|
28
|
+
var import_web = require("@libsql/client/web");
|
|
28
29
|
|
|
29
30
|
// src/libsql.ts
|
|
30
31
|
var import_driver_adapter_utils2 = require("@prisma/driver-adapter-utils");
|
|
@@ -139,11 +140,11 @@ function inferObjectType(value) {
|
|
|
139
140
|
throw new UnexpectedTypeError(value);
|
|
140
141
|
}
|
|
141
142
|
var UnexpectedTypeError = class extends Error {
|
|
143
|
+
name = "UnexpectedTypeError";
|
|
142
144
|
constructor(value) {
|
|
143
145
|
const type = typeof value;
|
|
144
146
|
const repr = type === "object" ? JSON.stringify(value) : String(value);
|
|
145
147
|
super(`unexpected value of type ${type}: ${repr}`);
|
|
146
|
-
__publicField(this, "name", "UnexpectedTypeError");
|
|
147
148
|
}
|
|
148
149
|
};
|
|
149
150
|
function mapRow(row, columnTypes) {
|
|
@@ -173,15 +174,13 @@ function mapRow(row, columnTypes) {
|
|
|
173
174
|
// src/libsql.ts
|
|
174
175
|
var debug2 = (0, import_driver_adapter_utils2.Debug)("prisma:driver-adapter:libsql");
|
|
175
176
|
var LOCK_TAG = Symbol();
|
|
176
|
-
var _a;
|
|
177
|
-
_a = LOCK_TAG;
|
|
178
177
|
var LibSqlQueryable = class {
|
|
179
178
|
constructor(client) {
|
|
180
179
|
this.client = client;
|
|
181
|
-
__publicField(this, "provider", "sqlite");
|
|
182
|
-
__publicField(this, "adapterName", name);
|
|
183
|
-
__publicField(this, _a, new import_async_mutex.Mutex());
|
|
184
180
|
}
|
|
181
|
+
provider = "sqlite";
|
|
182
|
+
adapterName = name;
|
|
183
|
+
[LOCK_TAG] = new import_async_mutex.Mutex();
|
|
185
184
|
/**
|
|
186
185
|
* Execute a query given as SQL, interpolating the given parameters.
|
|
187
186
|
*/
|
|
@@ -260,28 +259,7 @@ var LibSqlTransaction = class extends LibSqlQueryable {
|
|
|
260
259
|
}
|
|
261
260
|
}
|
|
262
261
|
};
|
|
263
|
-
var
|
|
264
|
-
constructor(client, release) {
|
|
265
|
-
super(client);
|
|
266
|
-
this.client = client;
|
|
267
|
-
this.release = release;
|
|
268
|
-
}
|
|
269
|
-
async startTransaction() {
|
|
270
|
-
const options = {
|
|
271
|
-
usePhantomQuery: true
|
|
272
|
-
};
|
|
273
|
-
const tag = "[js::startTransaction]";
|
|
274
|
-
debug2("%s options: %O", tag, options);
|
|
275
|
-
try {
|
|
276
|
-
const tx = await this.client.transaction("deferred");
|
|
277
|
-
return new LibSqlTransaction(tx, options, this.release);
|
|
278
|
-
} catch (e) {
|
|
279
|
-
this.release();
|
|
280
|
-
throw e;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
};
|
|
284
|
-
var PrismaLibSQL = class extends LibSqlQueryable {
|
|
262
|
+
var PrismaLibSQLAdapter = class extends LibSqlQueryable {
|
|
285
263
|
constructor(client) {
|
|
286
264
|
super(client);
|
|
287
265
|
}
|
|
@@ -295,15 +273,52 @@ var PrismaLibSQL = class extends LibSqlQueryable {
|
|
|
295
273
|
release();
|
|
296
274
|
}
|
|
297
275
|
}
|
|
298
|
-
async
|
|
276
|
+
async startTransaction(isolationLevel) {
|
|
277
|
+
if (isolationLevel && isolationLevel !== "SERIALIZABLE") {
|
|
278
|
+
throw new import_driver_adapter_utils2.DriverAdapterError({
|
|
279
|
+
kind: "InvalidIsolationLevel",
|
|
280
|
+
level: isolationLevel
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
const options = {
|
|
284
|
+
usePhantomQuery: true
|
|
285
|
+
};
|
|
286
|
+
const tag = "[js::startTransaction]";
|
|
287
|
+
debug2("%s options: %O", tag, options);
|
|
299
288
|
const release = await this[LOCK_TAG].acquire();
|
|
300
|
-
|
|
289
|
+
try {
|
|
290
|
+
const tx = await this.client.transaction("deferred");
|
|
291
|
+
return new LibSqlTransaction(tx, options, release);
|
|
292
|
+
} catch (e) {
|
|
293
|
+
release();
|
|
294
|
+
throw e;
|
|
295
|
+
}
|
|
301
296
|
}
|
|
302
297
|
dispose() {
|
|
303
298
|
this.client.close();
|
|
304
299
|
return Promise.resolve();
|
|
305
300
|
}
|
|
306
301
|
};
|
|
302
|
+
var PrismaLibSQLAdapterFactoryBase = class {
|
|
303
|
+
constructor(config) {
|
|
304
|
+
this.config = config;
|
|
305
|
+
}
|
|
306
|
+
provider = "sqlite";
|
|
307
|
+
adapterName = name;
|
|
308
|
+
connect() {
|
|
309
|
+
return Promise.resolve(new PrismaLibSQLAdapter(this.createClient(this.config)));
|
|
310
|
+
}
|
|
311
|
+
connectToShadowDb() {
|
|
312
|
+
return Promise.resolve(new PrismaLibSQLAdapter(this.createClient({ ...this.config, url: ":memory:" })));
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
// src/libsql-web.ts
|
|
317
|
+
var PrismaLibSQLWebAdapterFactory = class extends PrismaLibSQLAdapterFactoryBase {
|
|
318
|
+
createClient(config) {
|
|
319
|
+
return (0, import_web.createClient)(config);
|
|
320
|
+
}
|
|
321
|
+
};
|
|
307
322
|
// Annotate the CommonJS export names for ESM import in node:
|
|
308
323
|
0 && (module.exports = {
|
|
309
324
|
PrismaLibSQL
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
// src/libsql-web.ts
|
|
2
|
+
import { createClient } from "@libsql/client/web";
|
|
3
|
+
|
|
4
|
+
// src/libsql.ts
|
|
5
|
+
import { Debug as Debug2, DriverAdapterError } from "@prisma/driver-adapter-utils";
|
|
6
|
+
import { Mutex } from "async-mutex";
|
|
7
|
+
|
|
8
|
+
// package.json
|
|
9
|
+
var name = "@prisma/adapter-libsql";
|
|
10
|
+
|
|
11
|
+
// src/conversion.ts
|
|
12
|
+
import { ColumnTypeEnum, Debug } from "@prisma/driver-adapter-utils";
|
|
13
|
+
var debug = Debug("prisma:driver-adapter:libsql:conversion");
|
|
14
|
+
function mapDeclType(declType) {
|
|
15
|
+
switch (declType.toUpperCase()) {
|
|
16
|
+
case "":
|
|
17
|
+
return null;
|
|
18
|
+
case "DECIMAL":
|
|
19
|
+
return ColumnTypeEnum.Numeric;
|
|
20
|
+
case "FLOAT":
|
|
21
|
+
return ColumnTypeEnum.Float;
|
|
22
|
+
case "DOUBLE":
|
|
23
|
+
case "DOUBLE PRECISION":
|
|
24
|
+
case "NUMERIC":
|
|
25
|
+
case "REAL":
|
|
26
|
+
return ColumnTypeEnum.Double;
|
|
27
|
+
case "TINYINT":
|
|
28
|
+
case "SMALLINT":
|
|
29
|
+
case "MEDIUMINT":
|
|
30
|
+
case "INT":
|
|
31
|
+
case "INTEGER":
|
|
32
|
+
case "SERIAL":
|
|
33
|
+
case "INT2":
|
|
34
|
+
return ColumnTypeEnum.Int32;
|
|
35
|
+
case "BIGINT":
|
|
36
|
+
case "UNSIGNED BIG INT":
|
|
37
|
+
case "INT8":
|
|
38
|
+
return ColumnTypeEnum.Int64;
|
|
39
|
+
case "DATETIME":
|
|
40
|
+
case "TIMESTAMP":
|
|
41
|
+
return ColumnTypeEnum.DateTime;
|
|
42
|
+
case "TIME":
|
|
43
|
+
return ColumnTypeEnum.Time;
|
|
44
|
+
case "DATE":
|
|
45
|
+
return ColumnTypeEnum.Date;
|
|
46
|
+
case "TEXT":
|
|
47
|
+
case "CLOB":
|
|
48
|
+
case "CHARACTER":
|
|
49
|
+
case "VARCHAR":
|
|
50
|
+
case "VARYING CHARACTER":
|
|
51
|
+
case "NCHAR":
|
|
52
|
+
case "NATIVE CHARACTER":
|
|
53
|
+
case "NVARCHAR":
|
|
54
|
+
return ColumnTypeEnum.Text;
|
|
55
|
+
case "BLOB":
|
|
56
|
+
return ColumnTypeEnum.Bytes;
|
|
57
|
+
case "BOOLEAN":
|
|
58
|
+
return ColumnTypeEnum.Boolean;
|
|
59
|
+
case "JSONB":
|
|
60
|
+
return ColumnTypeEnum.Json;
|
|
61
|
+
default:
|
|
62
|
+
debug("unknown decltype:", declType);
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function mapDeclaredColumnTypes(columnTypes) {
|
|
67
|
+
const emptyIndices = /* @__PURE__ */ new Set();
|
|
68
|
+
const result = columnTypes.map((typeName, index) => {
|
|
69
|
+
const mappedType = mapDeclType(typeName);
|
|
70
|
+
if (mappedType === null) {
|
|
71
|
+
emptyIndices.add(index);
|
|
72
|
+
}
|
|
73
|
+
return mappedType;
|
|
74
|
+
});
|
|
75
|
+
return [result, emptyIndices];
|
|
76
|
+
}
|
|
77
|
+
function getColumnTypes(declaredTypes, rows) {
|
|
78
|
+
const [columnTypes, emptyIndices] = mapDeclaredColumnTypes(declaredTypes);
|
|
79
|
+
if (emptyIndices.size === 0) {
|
|
80
|
+
return columnTypes;
|
|
81
|
+
}
|
|
82
|
+
columnLoop: for (const columnIndex of emptyIndices) {
|
|
83
|
+
for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
|
|
84
|
+
const candidateValue = rows[rowIndex][columnIndex];
|
|
85
|
+
if (candidateValue !== null) {
|
|
86
|
+
columnTypes[columnIndex] = inferColumnType(candidateValue);
|
|
87
|
+
continue columnLoop;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
columnTypes[columnIndex] = ColumnTypeEnum.Int32;
|
|
91
|
+
}
|
|
92
|
+
return columnTypes;
|
|
93
|
+
}
|
|
94
|
+
function inferColumnType(value) {
|
|
95
|
+
switch (typeof value) {
|
|
96
|
+
case "string":
|
|
97
|
+
return ColumnTypeEnum.Text;
|
|
98
|
+
case "bigint":
|
|
99
|
+
return ColumnTypeEnum.Int64;
|
|
100
|
+
case "boolean":
|
|
101
|
+
return ColumnTypeEnum.Boolean;
|
|
102
|
+
case "number":
|
|
103
|
+
return ColumnTypeEnum.UnknownNumber;
|
|
104
|
+
case "object":
|
|
105
|
+
return inferObjectType(value);
|
|
106
|
+
default:
|
|
107
|
+
throw new UnexpectedTypeError(value);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function inferObjectType(value) {
|
|
111
|
+
if (value instanceof ArrayBuffer) {
|
|
112
|
+
return ColumnTypeEnum.Bytes;
|
|
113
|
+
}
|
|
114
|
+
throw new UnexpectedTypeError(value);
|
|
115
|
+
}
|
|
116
|
+
var UnexpectedTypeError = class extends Error {
|
|
117
|
+
name = "UnexpectedTypeError";
|
|
118
|
+
constructor(value) {
|
|
119
|
+
const type = typeof value;
|
|
120
|
+
const repr = type === "object" ? JSON.stringify(value) : String(value);
|
|
121
|
+
super(`unexpected value of type ${type}: ${repr}`);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
function mapRow(row, columnTypes) {
|
|
125
|
+
const result = Array.from(row);
|
|
126
|
+
for (let i = 0; i < result.length; i++) {
|
|
127
|
+
const value = result[i];
|
|
128
|
+
if (value instanceof ArrayBuffer) {
|
|
129
|
+
result[i] = Array.from(new Uint8Array(value));
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (typeof value === "number" && (columnTypes[i] === ColumnTypeEnum.Int32 || columnTypes[i] === ColumnTypeEnum.Int64) && !Number.isInteger(value)) {
|
|
133
|
+
result[i] = Math.trunc(value);
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (["number", "bigint"].includes(typeof value) && columnTypes[i] === ColumnTypeEnum.DateTime) {
|
|
137
|
+
result[i] = new Date(Number(value)).toISOString();
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (typeof value === "bigint") {
|
|
141
|
+
result[i] = value.toString();
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/libsql.ts
|
|
149
|
+
var debug2 = Debug2("prisma:driver-adapter:libsql");
|
|
150
|
+
var LOCK_TAG = Symbol();
|
|
151
|
+
var LibSqlQueryable = class {
|
|
152
|
+
constructor(client) {
|
|
153
|
+
this.client = client;
|
|
154
|
+
}
|
|
155
|
+
provider = "sqlite";
|
|
156
|
+
adapterName = name;
|
|
157
|
+
[LOCK_TAG] = new Mutex();
|
|
158
|
+
/**
|
|
159
|
+
* Execute a query given as SQL, interpolating the given parameters.
|
|
160
|
+
*/
|
|
161
|
+
async queryRaw(query) {
|
|
162
|
+
const tag = "[js::query_raw]";
|
|
163
|
+
debug2(`${tag} %O`, query);
|
|
164
|
+
const { columns, rows, columnTypes: declaredColumnTypes } = await this.performIO(query);
|
|
165
|
+
const columnTypes = getColumnTypes(declaredColumnTypes, rows);
|
|
166
|
+
return {
|
|
167
|
+
columnNames: columns,
|
|
168
|
+
columnTypes,
|
|
169
|
+
rows: rows.map((row) => mapRow(row, columnTypes))
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Execute a query given as SQL, interpolating the given parameters and
|
|
174
|
+
* returning the number of affected rows.
|
|
175
|
+
* Note: Queryable expects a u64, but napi.rs only supports u32.
|
|
176
|
+
*/
|
|
177
|
+
async executeRaw(query) {
|
|
178
|
+
const tag = "[js::execute_raw]";
|
|
179
|
+
debug2(`${tag} %O`, query);
|
|
180
|
+
return (await this.performIO(query)).rowsAffected ?? 0;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Run a query against the database, returning the result set.
|
|
184
|
+
* Should the query fail due to a connection error, the connection is
|
|
185
|
+
* marked as unhealthy.
|
|
186
|
+
*/
|
|
187
|
+
async performIO(query) {
|
|
188
|
+
const release = await this[LOCK_TAG].acquire();
|
|
189
|
+
try {
|
|
190
|
+
const result = await this.client.execute(query);
|
|
191
|
+
return result;
|
|
192
|
+
} catch (e) {
|
|
193
|
+
this.onError(e);
|
|
194
|
+
} finally {
|
|
195
|
+
release();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
onError(error) {
|
|
199
|
+
debug2("Error in performIO: %O", error);
|
|
200
|
+
const rawCode = error["rawCode"] ?? error.cause?.["rawCode"];
|
|
201
|
+
if (typeof rawCode === "number") {
|
|
202
|
+
throw new DriverAdapterError({
|
|
203
|
+
kind: "sqlite",
|
|
204
|
+
extendedCode: rawCode,
|
|
205
|
+
message: error.message
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
throw error;
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
var LibSqlTransaction = class extends LibSqlQueryable {
|
|
212
|
+
constructor(client, options, unlockParent) {
|
|
213
|
+
super(client);
|
|
214
|
+
this.options = options;
|
|
215
|
+
this.unlockParent = unlockParent;
|
|
216
|
+
}
|
|
217
|
+
async commit() {
|
|
218
|
+
debug2(`[js::commit]`);
|
|
219
|
+
try {
|
|
220
|
+
await this.client.commit();
|
|
221
|
+
} finally {
|
|
222
|
+
this.unlockParent();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
async rollback() {
|
|
226
|
+
debug2(`[js::rollback]`);
|
|
227
|
+
try {
|
|
228
|
+
await this.client.rollback();
|
|
229
|
+
} catch (error) {
|
|
230
|
+
debug2("error in rollback:", error);
|
|
231
|
+
} finally {
|
|
232
|
+
this.unlockParent();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
var PrismaLibSQLAdapter = class extends LibSqlQueryable {
|
|
237
|
+
constructor(client) {
|
|
238
|
+
super(client);
|
|
239
|
+
}
|
|
240
|
+
async executeScript(script) {
|
|
241
|
+
const release = await this[LOCK_TAG].acquire();
|
|
242
|
+
try {
|
|
243
|
+
await this.client.executeMultiple(script);
|
|
244
|
+
} catch (e) {
|
|
245
|
+
this.onError(e);
|
|
246
|
+
} finally {
|
|
247
|
+
release();
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
async startTransaction(isolationLevel) {
|
|
251
|
+
if (isolationLevel && isolationLevel !== "SERIALIZABLE") {
|
|
252
|
+
throw new DriverAdapterError({
|
|
253
|
+
kind: "InvalidIsolationLevel",
|
|
254
|
+
level: isolationLevel
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
const options = {
|
|
258
|
+
usePhantomQuery: true
|
|
259
|
+
};
|
|
260
|
+
const tag = "[js::startTransaction]";
|
|
261
|
+
debug2("%s options: %O", tag, options);
|
|
262
|
+
const release = await this[LOCK_TAG].acquire();
|
|
263
|
+
try {
|
|
264
|
+
const tx = await this.client.transaction("deferred");
|
|
265
|
+
return new LibSqlTransaction(tx, options, release);
|
|
266
|
+
} catch (e) {
|
|
267
|
+
release();
|
|
268
|
+
throw e;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
dispose() {
|
|
272
|
+
this.client.close();
|
|
273
|
+
return Promise.resolve();
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
var PrismaLibSQLAdapterFactoryBase = class {
|
|
277
|
+
constructor(config) {
|
|
278
|
+
this.config = config;
|
|
279
|
+
}
|
|
280
|
+
provider = "sqlite";
|
|
281
|
+
adapterName = name;
|
|
282
|
+
connect() {
|
|
283
|
+
return Promise.resolve(new PrismaLibSQLAdapter(this.createClient(this.config)));
|
|
284
|
+
}
|
|
285
|
+
connectToShadowDb() {
|
|
286
|
+
return Promise.resolve(new PrismaLibSQLAdapter(this.createClient({ ...this.config, url: ":memory:" })));
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// src/libsql-web.ts
|
|
291
|
+
var PrismaLibSQLWebAdapterFactory = class extends PrismaLibSQLAdapterFactoryBase {
|
|
292
|
+
createClient(config) {
|
|
293
|
+
return createClient(config);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
export {
|
|
297
|
+
PrismaLibSQLWebAdapterFactory as PrismaLibSQL
|
|
298
|
+
};
|
package/package.json
CHANGED
|
@@ -1,19 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma/adapter-libsql",
|
|
3
|
-
"version": "6.6.0-dev.
|
|
3
|
+
"version": "6.6.0-dev.100",
|
|
4
4
|
"description": "Prisma's driver adapter for libSQL and Turso",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"module": "dist/index.mjs",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
5
|
+
"main": "dist/index-node.js",
|
|
6
|
+
"module": "dist/index-node.mjs",
|
|
7
|
+
"types": "dist/index-node.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
10
|
"require": {
|
|
11
|
-
"types": "./dist/index.d.ts",
|
|
12
|
-
"default": "./dist/index.js"
|
|
11
|
+
"types": "./dist/index-node.d.ts",
|
|
12
|
+
"default": "./dist/index-node.js"
|
|
13
13
|
},
|
|
14
14
|
"import": {
|
|
15
|
-
"types": "./dist/index.d.mts",
|
|
16
|
-
"default": "./dist/index.mjs"
|
|
15
|
+
"types": "./dist/index-node.d.mts",
|
|
16
|
+
"default": "./dist/index-node.mjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"./web": {
|
|
20
|
+
"require": {
|
|
21
|
+
"types": "./dist/index-web.d.ts",
|
|
22
|
+
"default": "./dist/index-web.js"
|
|
23
|
+
},
|
|
24
|
+
"import": {
|
|
25
|
+
"types": "./dist/index-web.d.mts",
|
|
26
|
+
"default": "./dist/index-web.mjs"
|
|
17
27
|
}
|
|
18
28
|
}
|
|
19
29
|
},
|
|
@@ -31,17 +41,17 @@
|
|
|
31
41
|
"license": "Apache-2.0",
|
|
32
42
|
"sideEffects": false,
|
|
33
43
|
"dependencies": {
|
|
44
|
+
"@libsql/client": "^0.3.5 || ^0.4.0 || ^0.5.0 || ^0.6.0 || ^0.7.0 || ^0.8.0",
|
|
34
45
|
"async-mutex": "0.5.0",
|
|
35
|
-
"@prisma/driver-adapter-utils": "6.6.0-dev.
|
|
46
|
+
"@prisma/driver-adapter-utils": "6.6.0-dev.100"
|
|
36
47
|
},
|
|
37
48
|
"devDependencies": {
|
|
38
|
-
"
|
|
39
|
-
|
|
40
|
-
"peerDependencies": {
|
|
41
|
-
"@libsql/client": "^0.3.5 || ^0.4.0 || ^0.5.0 || ^0.6.0 || ^0.7.0 || ^0.8.0"
|
|
49
|
+
"jest": "29.7.0",
|
|
50
|
+
"jest-junit": "16.0.0"
|
|
42
51
|
},
|
|
43
52
|
"scripts": {
|
|
44
53
|
"dev": "DEV=true tsx helpers/build.ts",
|
|
45
|
-
"build": "tsx helpers/build.ts"
|
|
54
|
+
"build": "tsx helpers/build.ts",
|
|
55
|
+
"test": "jest"
|
|
46
56
|
}
|
|
47
57
|
}
|
package/dist/index.d.mts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import type { Client } from '@libsql/client';
|
|
2
|
-
import { Mutex } from 'async-mutex';
|
|
3
|
-
import type { SqlConnection } from '@prisma/driver-adapter-utils';
|
|
4
|
-
import type { SqlQuery } from '@prisma/driver-adapter-utils';
|
|
5
|
-
import type { SqlQueryable } from '@prisma/driver-adapter-utils';
|
|
6
|
-
import type { SqlResultSet } from '@prisma/driver-adapter-utils';
|
|
7
|
-
import type { Transaction } from '@libsql/client';
|
|
8
|
-
import type { TransactionContext } from '@prisma/driver-adapter-utils';
|
|
9
|
-
|
|
10
|
-
declare class LibSqlQueryable<ClientT extends StdClient | TransactionClient> implements SqlQueryable {
|
|
11
|
-
protected readonly client: ClientT;
|
|
12
|
-
readonly provider = "sqlite";
|
|
13
|
-
readonly adapterName: string;
|
|
14
|
-
[LOCK_TAG]: Mutex;
|
|
15
|
-
constructor(client: ClientT);
|
|
16
|
-
/**
|
|
17
|
-
* Execute a query given as SQL, interpolating the given parameters.
|
|
18
|
-
*/
|
|
19
|
-
queryRaw(query: SqlQuery): Promise<SqlResultSet>;
|
|
20
|
-
/**
|
|
21
|
-
* Execute a query given as SQL, interpolating the given parameters and
|
|
22
|
-
* returning the number of affected rows.
|
|
23
|
-
* Note: Queryable expects a u64, but napi.rs only supports u32.
|
|
24
|
-
*/
|
|
25
|
-
executeRaw(query: SqlQuery): Promise<number>;
|
|
26
|
-
/**
|
|
27
|
-
* Run a query against the database, returning the result set.
|
|
28
|
-
* Should the query fail due to a connection error, the connection is
|
|
29
|
-
* marked as unhealthy.
|
|
30
|
-
*/
|
|
31
|
-
private performIO;
|
|
32
|
-
protected onError(error: any): never;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
declare const LOCK_TAG: unique symbol;
|
|
36
|
-
|
|
37
|
-
export declare class PrismaLibSQL extends LibSqlQueryable<StdClient> implements SqlConnection {
|
|
38
|
-
constructor(client: StdClient);
|
|
39
|
-
executeScript(script: string): Promise<void>;
|
|
40
|
-
transactionContext(): Promise<TransactionContext>;
|
|
41
|
-
dispose(): Promise<void>;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
declare type StdClient = Client;
|
|
45
|
-
|
|
46
|
-
declare type TransactionClient = Transaction;
|
|
47
|
-
|
|
48
|
-
export { }
|
package/dist/index.d.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import type { Client } from '@libsql/client';
|
|
2
|
-
import { Mutex } from 'async-mutex';
|
|
3
|
-
import type { SqlConnection } from '@prisma/driver-adapter-utils';
|
|
4
|
-
import type { SqlQuery } from '@prisma/driver-adapter-utils';
|
|
5
|
-
import type { SqlQueryable } from '@prisma/driver-adapter-utils';
|
|
6
|
-
import type { SqlResultSet } from '@prisma/driver-adapter-utils';
|
|
7
|
-
import type { Transaction } from '@libsql/client';
|
|
8
|
-
import type { TransactionContext } from '@prisma/driver-adapter-utils';
|
|
9
|
-
|
|
10
|
-
declare class LibSqlQueryable<ClientT extends StdClient | TransactionClient> implements SqlQueryable {
|
|
11
|
-
protected readonly client: ClientT;
|
|
12
|
-
readonly provider = "sqlite";
|
|
13
|
-
readonly adapterName: string;
|
|
14
|
-
[LOCK_TAG]: Mutex;
|
|
15
|
-
constructor(client: ClientT);
|
|
16
|
-
/**
|
|
17
|
-
* Execute a query given as SQL, interpolating the given parameters.
|
|
18
|
-
*/
|
|
19
|
-
queryRaw(query: SqlQuery): Promise<SqlResultSet>;
|
|
20
|
-
/**
|
|
21
|
-
* Execute a query given as SQL, interpolating the given parameters and
|
|
22
|
-
* returning the number of affected rows.
|
|
23
|
-
* Note: Queryable expects a u64, but napi.rs only supports u32.
|
|
24
|
-
*/
|
|
25
|
-
executeRaw(query: SqlQuery): Promise<number>;
|
|
26
|
-
/**
|
|
27
|
-
* Run a query against the database, returning the result set.
|
|
28
|
-
* Should the query fail due to a connection error, the connection is
|
|
29
|
-
* marked as unhealthy.
|
|
30
|
-
*/
|
|
31
|
-
private performIO;
|
|
32
|
-
protected onError(error: any): never;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
declare const LOCK_TAG: unique symbol;
|
|
36
|
-
|
|
37
|
-
export declare class PrismaLibSQL extends LibSqlQueryable<StdClient> implements SqlConnection {
|
|
38
|
-
constructor(client: StdClient);
|
|
39
|
-
executeScript(script: string): Promise<void>;
|
|
40
|
-
transactionContext(): Promise<TransactionContext>;
|
|
41
|
-
dispose(): Promise<void>;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
declare type StdClient = Client;
|
|
45
|
-
|
|
46
|
-
declare type TransactionClient = Transaction;
|
|
47
|
-
|
|
48
|
-
export { }
|