driftsql 1.0.9 → 1.0.10

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 CHANGED
@@ -1,10 +1,16 @@
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
+ }
9
+
10
+ interface MySQLConfig {
11
+ connectionString: string;
12
+ }
13
+
8
14
  type Drivers = ClientOptions['drivers'];
9
15
  declare const inspectDB: (drivers: Drivers) => Promise<never>;
10
16
 
@@ -18,23 +24,18 @@ type UnifiedQueryResult<T extends Record<string, any>> = {
18
24
  }>;
19
25
  };
20
26
  interface ClientOptions {
21
- drivers?: {
22
- libsql?: Config;
27
+ drivers: {
28
+ libsql?: LibSQLConfig;
23
29
  postgres?: PostgresConfig;
24
- mysql?: ConnectionOptions;
25
- };
26
- options?: {
27
- defaultTimeout?: number;
30
+ mysql?: MySQLConfig;
28
31
  };
29
32
  }
30
33
  declare class DriftSQLClient<DT> {
31
34
  private postgres?;
32
- private mysqlClient?;
33
- private libsqlClient?;
34
- private postgresClient?;
35
+ private libsql?;
36
+ private mysql?;
35
37
  private drivers;
36
38
  constructor(options: ClientOptions);
37
- private convertLibsqlResult;
38
39
  readonly inspect: () => Promise<void>;
39
40
  query<T extends Record<string, any>>(query: string, args?: (string | number | boolean | null)[]): Promise<UnifiedQueryResult<T>>;
40
41
  findFirst<K extends keyof DT>(table: K, where?: Partial<DT[K]>): Promise<DT[K] | null>;
package/dist/index.d.ts CHANGED
@@ -1,10 +1,16 @@
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
+ }
9
+
10
+ interface MySQLConfig {
11
+ connectionString: string;
12
+ }
13
+
8
14
  type Drivers = ClientOptions['drivers'];
9
15
  declare const inspectDB: (drivers: Drivers) => Promise<never>;
10
16
 
@@ -18,23 +24,18 @@ type UnifiedQueryResult<T extends Record<string, any>> = {
18
24
  }>;
19
25
  };
20
26
  interface ClientOptions {
21
- drivers?: {
22
- libsql?: Config;
27
+ drivers: {
28
+ libsql?: LibSQLConfig;
23
29
  postgres?: PostgresConfig;
24
- mysql?: ConnectionOptions;
25
- };
26
- options?: {
27
- defaultTimeout?: number;
30
+ mysql?: MySQLConfig;
28
31
  };
29
32
  }
30
33
  declare class DriftSQLClient<DT> {
31
34
  private postgres?;
32
- private mysqlClient?;
33
- private libsqlClient?;
34
- private postgresClient?;
35
+ private libsql?;
36
+ private mysql?;
35
37
  private drivers;
36
38
  constructor(options: ClientOptions);
37
- private convertLibsqlResult;
38
39
  readonly inspect: () => Promise<void>;
39
40
  query<T extends Record<string, any>>(query: string, args?: (string | number | boolean | null)[]): Promise<UnifiedQueryResult<T>>;
40
41
  findFirst<K extends keyof DT>(table: K, where?: Partial<DT[K]>): Promise<DT[K] | null>;
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
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 } from '@libsql/client';
4
+ import * as mysql from 'mysql2/promise';
5
5
  import fs from 'node:fs/promises';
6
6
 
7
7
  const supportedDrivers = ["postgres", "postgresHTTP", "mysql", "libsql"];
@@ -283,18 +283,15 @@ class PostgresDriver {
283
283
  }
284
284
  }
285
285
 
286
- class DriftSQLClient {
287
- postgres;
288
- mysqlClient;
289
- libsqlClient;
290
- postgresClient;
291
- drivers;
286
+ class LibSQLDriver {
292
287
  constructor(options) {
293
- this.postgres = options.drivers?.postgres ? new PostgresDriver({ connectionString: options.drivers.postgres.connectionString }) : void 0;
294
- this.libsqlClient = options.drivers?.libsql ? createClient(options.drivers.libsql) : void 0;
295
- this.mysqlClient = options.drivers?.mysql ? mysql.createConnection(options.drivers.mysql) : void 0;
296
- this.drivers = options.drivers || {};
288
+ this.options = options;
289
+ this.client = createClient({
290
+ url: this.options.url,
291
+ ...this.options.authToken ? { authToken: this.options.authToken } : {}
292
+ });
297
293
  }
294
+ client;
298
295
  convertLibsqlResult(result) {
299
296
  const rows = result.rows.map((row) => {
300
297
  const obj = {};
@@ -310,16 +307,70 @@ class DriftSQLClient {
310
307
  fields: result.columns.map((col) => ({ name: col, dataTypeID: 0 }))
311
308
  };
312
309
  }
310
+ async query(query, params) {
311
+ try {
312
+ const result = await this.client.execute(query, params);
313
+ return this.convertLibsqlResult(result);
314
+ } catch (error) {
315
+ console.error("LibSQL query error:", error);
316
+ throw error;
317
+ }
318
+ }
319
+ async close() {
320
+ try {
321
+ this.client.close();
322
+ } catch (error) {
323
+ console.error("Error closing LibSQL client:", error);
324
+ }
325
+ }
326
+ }
327
+
328
+ class MySQLDriver {
329
+ constructor(options) {
330
+ this.options = options;
331
+ 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.");
332
+ this.client = mysql.createConnection(this.options.connectionString);
333
+ }
334
+ client;
335
+ async query(query, params) {
336
+ try {
337
+ const [rows, fields] = await (await this.client).execute(query, params || []);
338
+ const rowCount = Array.isArray(rows) ? rows.length : 0;
339
+ return {
340
+ rows,
341
+ rowCount,
342
+ command: void 0,
343
+ fields: fields.map((field) => ({ name: field.name, dataTypeID: field.columnType }))
344
+ };
345
+ } catch (error) {
346
+ consola.error("MySQL query error:", error);
347
+ throw error;
348
+ }
349
+ }
350
+ async close() {
351
+ try {
352
+ await (await this.client).end();
353
+ } catch (error) {
354
+ consola.error("Error closing MySQL client:", error);
355
+ }
356
+ }
357
+ }
358
+
359
+ class DriftSQLClient {
360
+ postgres;
361
+ libsql;
362
+ mysql;
363
+ drivers;
364
+ constructor(options) {
365
+ this.postgres = options.drivers?.postgres ? new PostgresDriver({ connectionString: options.drivers.postgres.connectionString }) : void 0;
366
+ this.libsql = options.drivers?.libsql ? new LibSQLDriver(options.drivers.libsql) : void 0;
367
+ this.mysql = options.drivers?.mysql ? new MySQLDriver(options.drivers.mysql) : void 0;
368
+ this.drivers = options.drivers || {};
369
+ }
313
370
  inspect = async () => {
314
371
  return inspectDB(this.drivers);
315
372
  };
316
373
  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
374
  if (this.postgres) {
324
375
  try {
325
376
  const result = await this.postgres.query(query, args || []);
@@ -334,32 +385,31 @@ class DriftSQLClient {
334
385
  throw error2;
335
386
  }
336
387
  }
337
- if (this.mysqlClient) {
388
+ if (this.mysql) {
338
389
  try {
339
- 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.");
340
- const filteredArgs = (args || []).map((arg) => arg === void 0 ? null : arg);
341
- const [rows, fields] = await (await this.mysqlClient).execute(query, filteredArgs);
390
+ const result = await this.mysql.query(query, args || []);
342
391
  return {
343
- rows,
344
- rowCount: Array.isArray(rows) ? rows.length : 0,
345
- command: void 0,
346
- // MySQL does not return command info
347
- fields: fields.map((field) => ({ name: field.name, dataTypeID: field.columnType }))
392
+ rows: result.rows,
393
+ rowCount: result.rowCount || 0,
394
+ command: result.command,
395
+ fields: result.fields?.map((field) => ({ name: field.name, dataTypeID: field.dataTypeID })) || []
348
396
  };
349
397
  } catch (error2) {
350
398
  consola.error("Failed to execute query with MySQL:", error2);
351
399
  throw error2;
352
400
  }
353
401
  }
354
- if (this.libsqlClient) {
402
+ if (this.libsql) {
355
403
  try {
356
- const result = await this.libsqlClient.execute({
357
- sql: query,
358
- args: args || []
359
- });
360
- return this.convertLibsqlResult(result);
404
+ const result = await this.libsql.query(query, args || []);
405
+ return {
406
+ rows: result.rows,
407
+ rowCount: result.rowCount || 0,
408
+ command: result.command,
409
+ fields: void 0
410
+ };
361
411
  } catch (error2) {
362
- consola.error("Failed to execute query with libsql:", error2);
412
+ consola.error("Failed to execute query with LibSQL:", error2);
363
413
  throw error2;
364
414
  }
365
415
  }
@@ -450,14 +500,11 @@ class DriftSQLClient {
450
500
  if (this.postgres) {
451
501
  await this.postgres.close();
452
502
  }
453
- if (this.libsqlClient) {
454
- this.libsqlClient.close();
455
- }
456
- if (this.mysqlClient) {
457
- await (await this.mysqlClient).end();
503
+ if (this.libsql) {
504
+ this.libsql.close();
458
505
  }
459
- if (this.postgresClient) {
460
- await this.postgresClient.end();
506
+ if (this.mysql) {
507
+ await this.mysql.close();
461
508
  }
462
509
  }
463
510
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "driftsql",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "author": "lasse vestergaard",
5
5
  "description": "A lightweight SQL client for TypeScript, supporting multiple databases like PostgreSQL, MySQL, and LibSQL.",
6
6
  "repository": "lassejlv/driftsql",