driftsql 1.0.9 → 1.0.11
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/dist/index.d.mts +19 -13
- package/dist/index.d.ts +19 -13
- package/dist/index.mjs +92 -41
- package/package.json +3 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
|
-
import { Config } from '@libsql/client';
|
|
2
|
-
import { ConnectionOptions } from 'mysql2/promise';
|
|
3
|
-
|
|
4
1
|
interface PostgresConfig {
|
|
5
2
|
connectionString: string;
|
|
6
3
|
}
|
|
7
4
|
|
|
5
|
+
interface LibSQLConfig {
|
|
6
|
+
url: string;
|
|
7
|
+
authToken?: string;
|
|
8
|
+
options?: {
|
|
9
|
+
experimental?: {
|
|
10
|
+
useTursoServerlessDriver?: boolean;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface MySQLConfig {
|
|
16
|
+
connectionString: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
8
19
|
type Drivers = ClientOptions['drivers'];
|
|
9
20
|
declare const inspectDB: (drivers: Drivers) => Promise<never>;
|
|
10
21
|
|
|
@@ -18,23 +29,18 @@ type UnifiedQueryResult<T extends Record<string, any>> = {
|
|
|
18
29
|
}>;
|
|
19
30
|
};
|
|
20
31
|
interface ClientOptions {
|
|
21
|
-
drivers
|
|
22
|
-
libsql?:
|
|
32
|
+
drivers: {
|
|
33
|
+
libsql?: LibSQLConfig;
|
|
23
34
|
postgres?: PostgresConfig;
|
|
24
|
-
mysql?:
|
|
25
|
-
};
|
|
26
|
-
options?: {
|
|
27
|
-
defaultTimeout?: number;
|
|
35
|
+
mysql?: MySQLConfig;
|
|
28
36
|
};
|
|
29
37
|
}
|
|
30
38
|
declare class DriftSQLClient<DT> {
|
|
31
39
|
private postgres?;
|
|
32
|
-
private
|
|
33
|
-
private
|
|
34
|
-
private postgresClient?;
|
|
40
|
+
private libsql?;
|
|
41
|
+
private mysql?;
|
|
35
42
|
private drivers;
|
|
36
43
|
constructor(options: ClientOptions);
|
|
37
|
-
private convertLibsqlResult;
|
|
38
44
|
readonly inspect: () => Promise<void>;
|
|
39
45
|
query<T extends Record<string, any>>(query: string, args?: (string | number | boolean | null)[]): Promise<UnifiedQueryResult<T>>;
|
|
40
46
|
findFirst<K extends keyof DT>(table: K, where?: Partial<DT[K]>): Promise<DT[K] | null>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
|
-
import { Config } from '@libsql/client';
|
|
2
|
-
import { ConnectionOptions } from 'mysql2/promise';
|
|
3
|
-
|
|
4
1
|
interface PostgresConfig {
|
|
5
2
|
connectionString: string;
|
|
6
3
|
}
|
|
7
4
|
|
|
5
|
+
interface LibSQLConfig {
|
|
6
|
+
url: string;
|
|
7
|
+
authToken?: string;
|
|
8
|
+
options?: {
|
|
9
|
+
experimental?: {
|
|
10
|
+
useTursoServerlessDriver?: boolean;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface MySQLConfig {
|
|
16
|
+
connectionString: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
8
19
|
type Drivers = ClientOptions['drivers'];
|
|
9
20
|
declare const inspectDB: (drivers: Drivers) => Promise<never>;
|
|
10
21
|
|
|
@@ -18,23 +29,18 @@ type UnifiedQueryResult<T extends Record<string, any>> = {
|
|
|
18
29
|
}>;
|
|
19
30
|
};
|
|
20
31
|
interface ClientOptions {
|
|
21
|
-
drivers
|
|
22
|
-
libsql?:
|
|
32
|
+
drivers: {
|
|
33
|
+
libsql?: LibSQLConfig;
|
|
23
34
|
postgres?: PostgresConfig;
|
|
24
|
-
mysql?:
|
|
25
|
-
};
|
|
26
|
-
options?: {
|
|
27
|
-
defaultTimeout?: number;
|
|
35
|
+
mysql?: MySQLConfig;
|
|
28
36
|
};
|
|
29
37
|
}
|
|
30
38
|
declare class DriftSQLClient<DT> {
|
|
31
39
|
private postgres?;
|
|
32
|
-
private
|
|
33
|
-
private
|
|
34
|
-
private postgresClient?;
|
|
40
|
+
private libsql?;
|
|
41
|
+
private mysql?;
|
|
35
42
|
private drivers;
|
|
36
43
|
constructor(options: ClientOptions);
|
|
37
|
-
private convertLibsqlResult;
|
|
38
44
|
readonly inspect: () => Promise<void>;
|
|
39
45
|
query<T extends Record<string, any>>(query: string, args?: (string | number | boolean | null)[]): Promise<UnifiedQueryResult<T>>;
|
|
40
46
|
findFirst<K extends keyof DT>(table: K, where?: Partial<DT[K]>): Promise<DT[K] | null>;
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import consola from 'consola';
|
|
2
|
-
import { createClient } from '@libsql/client';
|
|
3
|
-
import mysql from 'mysql2/promise';
|
|
4
2
|
import postgres from 'postgres';
|
|
3
|
+
import { createClient as createClient$1 } from '@libsql/client';
|
|
4
|
+
import { createClient } from '@tursodatabase/serverless/compat';
|
|
5
|
+
import * as mysql from 'mysql2/promise';
|
|
5
6
|
import fs from 'node:fs/promises';
|
|
6
7
|
|
|
7
8
|
const supportedDrivers = ["postgres", "postgresHTTP", "mysql", "libsql"];
|
|
@@ -283,18 +284,18 @@ class PostgresDriver {
|
|
|
283
284
|
}
|
|
284
285
|
}
|
|
285
286
|
|
|
286
|
-
class
|
|
287
|
-
postgres;
|
|
288
|
-
mysqlClient;
|
|
289
|
-
libsqlClient;
|
|
290
|
-
postgresClient;
|
|
291
|
-
drivers;
|
|
287
|
+
class LibSQLDriver {
|
|
292
288
|
constructor(options) {
|
|
293
|
-
this.
|
|
294
|
-
this.
|
|
295
|
-
|
|
296
|
-
|
|
289
|
+
this.options = options;
|
|
290
|
+
this.client = this.options?.options?.experimental?.useTursoServerlessDriver ? createClient({
|
|
291
|
+
url: this.options.url,
|
|
292
|
+
...this.options.authToken ? { authToken: this.options.authToken } : {}
|
|
293
|
+
}) : createClient$1({
|
|
294
|
+
url: this.options.url,
|
|
295
|
+
...this.options.authToken ? { authToken: this.options.authToken } : {}
|
|
296
|
+
});
|
|
297
297
|
}
|
|
298
|
+
client;
|
|
298
299
|
convertLibsqlResult(result) {
|
|
299
300
|
const rows = result.rows.map((row) => {
|
|
300
301
|
const obj = {};
|
|
@@ -310,16 +311,70 @@ class DriftSQLClient {
|
|
|
310
311
|
fields: result.columns.map((col) => ({ name: col, dataTypeID: 0 }))
|
|
311
312
|
};
|
|
312
313
|
}
|
|
314
|
+
async query(query, params) {
|
|
315
|
+
try {
|
|
316
|
+
const result = await this.client.execute(query, params);
|
|
317
|
+
return this.convertLibsqlResult(result);
|
|
318
|
+
} catch (error) {
|
|
319
|
+
console.error("LibSQL query error:", error);
|
|
320
|
+
throw error;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
async close() {
|
|
324
|
+
try {
|
|
325
|
+
this.client.close();
|
|
326
|
+
} catch (error) {
|
|
327
|
+
console.error("Error closing LibSQL client:", error);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
class MySQLDriver {
|
|
333
|
+
constructor(options) {
|
|
334
|
+
this.options = options;
|
|
335
|
+
consola.warn("MySQL client is experimental and may not be compatible with the helper functions, since they originally designed for PostgreSQL and libsql. But .query() method should work.");
|
|
336
|
+
this.client = mysql.createConnection(this.options.connectionString);
|
|
337
|
+
}
|
|
338
|
+
client;
|
|
339
|
+
async query(query, params) {
|
|
340
|
+
try {
|
|
341
|
+
const [rows, fields] = await (await this.client).execute(query, params || []);
|
|
342
|
+
const rowCount = Array.isArray(rows) ? rows.length : 0;
|
|
343
|
+
return {
|
|
344
|
+
rows,
|
|
345
|
+
rowCount,
|
|
346
|
+
command: void 0,
|
|
347
|
+
fields: fields.map((field) => ({ name: field.name, dataTypeID: field.columnType }))
|
|
348
|
+
};
|
|
349
|
+
} catch (error) {
|
|
350
|
+
consola.error("MySQL query error:", error);
|
|
351
|
+
throw error;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
async close() {
|
|
355
|
+
try {
|
|
356
|
+
await (await this.client).end();
|
|
357
|
+
} catch (error) {
|
|
358
|
+
consola.error("Error closing MySQL client:", error);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
class DriftSQLClient {
|
|
364
|
+
postgres;
|
|
365
|
+
libsql;
|
|
366
|
+
mysql;
|
|
367
|
+
drivers;
|
|
368
|
+
constructor(options) {
|
|
369
|
+
this.postgres = options.drivers?.postgres ? new PostgresDriver({ connectionString: options.drivers.postgres.connectionString }) : void 0;
|
|
370
|
+
this.libsql = options.drivers?.libsql ? new LibSQLDriver(options.drivers.libsql) : void 0;
|
|
371
|
+
this.mysql = options.drivers?.mysql ? new MySQLDriver(options.drivers.mysql) : void 0;
|
|
372
|
+
this.drivers = options.drivers || {};
|
|
373
|
+
}
|
|
313
374
|
inspect = async () => {
|
|
314
375
|
return inspectDB(this.drivers);
|
|
315
376
|
};
|
|
316
377
|
async query(query, args) {
|
|
317
|
-
const driversCount = Object.keys(this.drivers).filter((key) => this.drivers[key] !== void 0).length;
|
|
318
|
-
if (driversCount > 1) {
|
|
319
|
-
const error2 = new Error("Multiple drivers are configured. Please use only one driver at a time.");
|
|
320
|
-
consola.error(error2);
|
|
321
|
-
throw error2;
|
|
322
|
-
}
|
|
323
378
|
if (this.postgres) {
|
|
324
379
|
try {
|
|
325
380
|
const result = await this.postgres.query(query, args || []);
|
|
@@ -334,32 +389,31 @@ class DriftSQLClient {
|
|
|
334
389
|
throw error2;
|
|
335
390
|
}
|
|
336
391
|
}
|
|
337
|
-
if (this.
|
|
392
|
+
if (this.mysql) {
|
|
338
393
|
try {
|
|
339
|
-
|
|
340
|
-
const filteredArgs = (args || []).map((arg) => arg === void 0 ? null : arg);
|
|
341
|
-
const [rows, fields] = await (await this.mysqlClient).execute(query, filteredArgs);
|
|
394
|
+
const result = await this.mysql.query(query, args || []);
|
|
342
395
|
return {
|
|
343
|
-
rows,
|
|
344
|
-
rowCount:
|
|
345
|
-
command:
|
|
346
|
-
|
|
347
|
-
fields: fields.map((field) => ({ name: field.name, dataTypeID: field.columnType }))
|
|
396
|
+
rows: result.rows,
|
|
397
|
+
rowCount: result.rowCount || 0,
|
|
398
|
+
command: result.command,
|
|
399
|
+
fields: result.fields?.map((field) => ({ name: field.name, dataTypeID: field.dataTypeID })) || []
|
|
348
400
|
};
|
|
349
401
|
} catch (error2) {
|
|
350
402
|
consola.error("Failed to execute query with MySQL:", error2);
|
|
351
403
|
throw error2;
|
|
352
404
|
}
|
|
353
405
|
}
|
|
354
|
-
if (this.
|
|
406
|
+
if (this.libsql) {
|
|
355
407
|
try {
|
|
356
|
-
const result = await this.
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
408
|
+
const result = await this.libsql.query(query, args || []);
|
|
409
|
+
return {
|
|
410
|
+
rows: result.rows,
|
|
411
|
+
rowCount: result.rowCount || 0,
|
|
412
|
+
command: result.command,
|
|
413
|
+
fields: void 0
|
|
414
|
+
};
|
|
361
415
|
} catch (error2) {
|
|
362
|
-
consola.error("Failed to execute query with
|
|
416
|
+
consola.error("Failed to execute query with LibSQL:", error2);
|
|
363
417
|
throw error2;
|
|
364
418
|
}
|
|
365
419
|
}
|
|
@@ -450,14 +504,11 @@ class DriftSQLClient {
|
|
|
450
504
|
if (this.postgres) {
|
|
451
505
|
await this.postgres.close();
|
|
452
506
|
}
|
|
453
|
-
if (this.
|
|
454
|
-
this.
|
|
455
|
-
}
|
|
456
|
-
if (this.mysqlClient) {
|
|
457
|
-
await (await this.mysqlClient).end();
|
|
507
|
+
if (this.libsql) {
|
|
508
|
+
this.libsql.close();
|
|
458
509
|
}
|
|
459
|
-
if (this.
|
|
460
|
-
await this.
|
|
510
|
+
if (this.mysql) {
|
|
511
|
+
await this.mysql.close();
|
|
461
512
|
}
|
|
462
513
|
}
|
|
463
514
|
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "driftsql",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"author": "lasse vestergaard",
|
|
5
|
-
"description": "A lightweight SQL client for TypeScript
|
|
5
|
+
"description": "A lightweight SQL client for TypeScript",
|
|
6
6
|
"repository": "lassejlv/driftsql",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"sideEffects": false,
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@libsql/client": "^0.15.9",
|
|
44
44
|
"@neondatabase/serverless": "^1.0.1",
|
|
45
|
+
"@tursodatabase/serverless": "^0.1.2",
|
|
45
46
|
"@types/pg": "^8.15.4",
|
|
46
47
|
"consola": "^3.4.2",
|
|
47
48
|
"driftsql": "^0.0.1",
|