@prisma/adapter-libsql 6.6.0-integration-push-spwsqpvywlkl.10 → 6.6.0-integration-push-ytsqyomrunkq.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/README.md CHANGED
@@ -40,6 +40,9 @@ 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
+ // 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'
43
46
 
44
47
  // Setup
45
48
  const connectionString = `${process.env.TURSO_DATABASE_URL}`
@@ -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 { }
@@ -17,15 +17,17 @@ var __copyProps = (to, from, except, desc) => {
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
20
+ // src/index-node.ts
21
+ var index_node_exports = {};
22
+ __export(index_node_exports, {
23
23
  PrismaLibSQL: () => PrismaLibSQLAdapterFactory
24
24
  });
25
- module.exports = __toCommonJS(index_exports);
25
+ module.exports = __toCommonJS(index_node_exports);
26
26
 
27
- // src/libsql.ts
27
+ // src/libsql-node.ts
28
28
  var import_client = require("@libsql/client");
29
+
30
+ // src/libsql.ts
29
31
  var import_driver_adapter_utils2 = require("@prisma/driver-adapter-utils");
30
32
  var import_async_mutex = require("async-mutex");
31
33
 
@@ -297,22 +299,26 @@ var PrismaLibSQLAdapter = class extends LibSqlQueryable {
297
299
  return Promise.resolve();
298
300
  }
299
301
  };
300
- var PrismaLibSQLAdapterFactory = class {
302
+ var PrismaLibSQLAdapterFactoryBase = class {
301
303
  constructor(config) {
302
304
  this.config = config;
303
305
  }
304
306
  provider = "sqlite";
305
307
  adapterName = name;
306
308
  connect() {
307
- return Promise.resolve(new PrismaLibSQLAdapter(createLibSQLClient(this.config)));
309
+ return Promise.resolve(new PrismaLibSQLAdapter(this.createClient(this.config)));
308
310
  }
309
311
  connectToShadowDb() {
310
- return Promise.resolve(new PrismaLibSQLAdapter(createLibSQLClient({ ...this.config, url: ":memory:" })));
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);
311
320
  }
312
321
  };
313
- function createLibSQLClient(config) {
314
- return (0, import_client.createClient)(config);
315
- }
316
322
  // Annotate the CommonJS export names for ESM import in node:
317
323
  0 && (module.exports = {
318
324
  PrismaLibSQL
@@ -1,5 +1,7 @@
1
- // src/libsql.ts
1
+ // src/libsql-node.ts
2
2
  import { createClient } from "@libsql/client";
3
+
4
+ // src/libsql.ts
3
5
  import { Debug as Debug2, DriverAdapterError } from "@prisma/driver-adapter-utils";
4
6
  import { Mutex } from "async-mutex";
5
7
 
@@ -271,22 +273,26 @@ var PrismaLibSQLAdapter = class extends LibSqlQueryable {
271
273
  return Promise.resolve();
272
274
  }
273
275
  };
274
- var PrismaLibSQLAdapterFactory = class {
276
+ var PrismaLibSQLAdapterFactoryBase = class {
275
277
  constructor(config) {
276
278
  this.config = config;
277
279
  }
278
280
  provider = "sqlite";
279
281
  adapterName = name;
280
282
  connect() {
281
- return Promise.resolve(new PrismaLibSQLAdapter(createLibSQLClient(this.config)));
283
+ return Promise.resolve(new PrismaLibSQLAdapter(this.createClient(this.config)));
282
284
  }
283
285
  connectToShadowDb() {
284
- return Promise.resolve(new PrismaLibSQLAdapter(createLibSQLClient({ ...this.config, url: ":memory:" })));
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);
285
294
  }
286
295
  };
287
- function createLibSQLClient(config) {
288
- return createClient(config);
289
- }
290
296
  export {
291
297
  PrismaLibSQLAdapterFactory as PrismaLibSQL
292
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 { }
@@ -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-web.ts
21
+ var index_web_exports = {};
22
+ __export(index_web_exports, {
23
+ PrismaLibSQL: () => PrismaLibSQLWebAdapterFactory
24
+ });
25
+ module.exports = __toCommonJS(index_web_exports);
26
+
27
+ // src/libsql-web.ts
28
+ var import_web = require("@libsql/client/web");
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-web.ts
317
+ var PrismaLibSQLWebAdapterFactory = class extends PrismaLibSQLAdapterFactoryBase {
318
+ createClient(config) {
319
+ return (0, import_web.createClient)(config);
320
+ }
321
+ };
322
+ // Annotate the CommonJS export names for ESM import in node:
323
+ 0 && (module.exports = {
324
+ PrismaLibSQL
325
+ });
@@ -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-integration-push-spwsqpvywlkl.10",
3
+ "version": "6.6.0-integration-push-ytsqyomrunkq.1",
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-integration-push-spwsqpvywlkl.10"
46
+ "@prisma/driver-adapter-utils": "6.6.0-integration-push-ytsqyomrunkq.1"
36
47
  },
37
48
  "devDependencies": {
38
- "@libsql/client": "0.8.0"
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,14 +0,0 @@
1
- import type { Config } from '@libsql/client';
2
- import type { SqlDriverAdapter } from '@prisma/driver-adapter-utils';
3
- import type { SqlMigrationAwareDriverAdapterFactory } from '@prisma/driver-adapter-utils';
4
-
5
- export declare class PrismaLibSQL implements SqlMigrationAwareDriverAdapterFactory {
6
- private readonly config;
7
- readonly provider = "sqlite";
8
- readonly adapterName: string;
9
- constructor(config: Config);
10
- connect(): Promise<SqlDriverAdapter>;
11
- connectToShadowDb(): Promise<SqlDriverAdapter>;
12
- }
13
-
14
- export { }
package/dist/index.d.ts DELETED
@@ -1,14 +0,0 @@
1
- import type { Config } from '@libsql/client';
2
- import type { SqlDriverAdapter } from '@prisma/driver-adapter-utils';
3
- import type { SqlMigrationAwareDriverAdapterFactory } from '@prisma/driver-adapter-utils';
4
-
5
- export declare class PrismaLibSQL implements SqlMigrationAwareDriverAdapterFactory {
6
- private readonly config;
7
- readonly provider = "sqlite";
8
- readonly adapterName: string;
9
- constructor(config: Config);
10
- connect(): Promise<SqlDriverAdapter>;
11
- connectToShadowDb(): Promise<SqlDriverAdapter>;
12
- }
13
-
14
- export { }