driftsql 1.0.1 → 1.0.3

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
@@ -2,6 +2,9 @@ import { PoolConfig } from 'pg';
2
2
  import { Config } from '@libsql/client';
3
3
  import mysql from 'mysql2/promise';
4
4
 
5
+ type Drivers = ClientOptions['drivers'];
6
+ declare const inspectDB: (drivers: Drivers) => Promise<never>;
7
+
5
8
  type UnifiedQueryResult<T extends Record<string, any>> = {
6
9
  rows: T[];
7
10
  rowCount: number;
@@ -30,8 +33,10 @@ declare class DriftSQLClient<DT> {
30
33
  private libsqlClient?;
31
34
  private neonClient?;
32
35
  private postgresClient?;
36
+ private drivers;
33
37
  constructor(options: ClientOptions);
34
38
  private convertLibsqlResult;
39
+ readonly inspect: () => Promise<void>;
35
40
  query<T extends Record<string, any>>(query: string, args?: (string | number | boolean | null)[]): Promise<UnifiedQueryResult<T>>;
36
41
  status(): Promise<{
37
42
  ok: boolean;
@@ -46,5 +51,5 @@ declare class DriftSQLClient<DT> {
46
51
  close(): Promise<void>;
47
52
  }
48
53
 
49
- export { DriftSQLClient };
54
+ export { DriftSQLClient, inspectDB };
50
55
  export type { ClientOptions };
package/dist/index.d.ts CHANGED
@@ -2,6 +2,9 @@ import { PoolConfig } from 'pg';
2
2
  import { Config } from '@libsql/client';
3
3
  import mysql from 'mysql2/promise';
4
4
 
5
+ type Drivers = ClientOptions['drivers'];
6
+ declare const inspectDB: (drivers: Drivers) => Promise<never>;
7
+
5
8
  type UnifiedQueryResult<T extends Record<string, any>> = {
6
9
  rows: T[];
7
10
  rowCount: number;
@@ -30,8 +33,10 @@ declare class DriftSQLClient<DT> {
30
33
  private libsqlClient?;
31
34
  private neonClient?;
32
35
  private postgresClient?;
36
+ private drivers;
33
37
  constructor(options: ClientOptions);
34
38
  private convertLibsqlResult;
39
+ readonly inspect: () => Promise<void>;
35
40
  query<T extends Record<string, any>>(query: string, args?: (string | number | boolean | null)[]): Promise<UnifiedQueryResult<T>>;
36
41
  status(): Promise<{
37
42
  ok: boolean;
@@ -46,5 +51,5 @@ declare class DriftSQLClient<DT> {
46
51
  close(): Promise<void>;
47
52
  }
48
53
 
49
- export { DriftSQLClient };
54
+ export { DriftSQLClient, inspectDB };
50
55
  export type { ClientOptions };
package/dist/index.mjs CHANGED
@@ -3,6 +3,140 @@ import ky from 'ky';
3
3
  import { Pool } from 'pg';
4
4
  import { createClient } from '@libsql/client';
5
5
  import mysql from 'mysql2/promise';
6
+ import fs from 'node:fs/promises';
7
+
8
+ const supportedDrivers = ["postgres"];
9
+ const mapPostgresToTypeScript = (postgresType, isNullable = false) => {
10
+ const nullable = isNullable ? " | null" : "";
11
+ switch (postgresType.toLowerCase()) {
12
+ case "uuid": {
13
+ return `string${nullable}`;
14
+ }
15
+ case "character varying":
16
+ case "varchar":
17
+ case "text":
18
+ case "char":
19
+ case "character": {
20
+ return `string${nullable}`;
21
+ }
22
+ case "integer":
23
+ case "int":
24
+ case "int4":
25
+ case "smallint":
26
+ case "int2":
27
+ case "bigint":
28
+ case "int8":
29
+ case "serial":
30
+ case "bigserial":
31
+ case "numeric":
32
+ case "decimal":
33
+ case "real":
34
+ case "float4":
35
+ case "double precision":
36
+ case "float8": {
37
+ return `number${nullable}`;
38
+ }
39
+ case "boolean":
40
+ case "bool": {
41
+ return `boolean${nullable}`;
42
+ }
43
+ case "timestamp":
44
+ case "timestamp with time zone":
45
+ case "timestamp without time zone":
46
+ case "timestamptz":
47
+ case "date":
48
+ case "time":
49
+ case "time with time zone":
50
+ case "time without time zone":
51
+ case "timetz":
52
+ case "interval": {
53
+ return `Date${nullable}`;
54
+ }
55
+ case "json":
56
+ case "jsonb": {
57
+ return `any${nullable}`;
58
+ }
59
+ case "array": {
60
+ return `any[]${nullable}`;
61
+ }
62
+ case "bytea": {
63
+ return `Buffer${nullable}`;
64
+ }
65
+ default: {
66
+ console.warn(`Unknown PostgreSQL type: ${postgresType}, defaulting to 'any'`);
67
+ return `any${nullable}`;
68
+ }
69
+ }
70
+ };
71
+ const inspectDB = async (drivers) => {
72
+ if (!drivers) throw new Error("No drivers provided for inspection");
73
+ const configuredDrivers = Object.keys(drivers).filter((key) => drivers[key] !== void 0);
74
+ if (configuredDrivers.length === 0) {
75
+ throw new Error("No drivers are configured");
76
+ }
77
+ const supportedConfiguredDrivers = configuredDrivers.filter((driver) => supportedDrivers.includes(driver));
78
+ if (supportedConfiguredDrivers.length === 0) {
79
+ throw new Error(`No supported drivers found. Configured: ${configuredDrivers.join(", ")}. Supported: ${supportedDrivers.join(", ")}`);
80
+ }
81
+ console.log(`Found supported drivers: ${supportedConfiguredDrivers.join(", ")}`);
82
+ let generatedTypes = "";
83
+ const client = new DriftSQLClient({ drivers });
84
+ const tables = await client.query(
85
+ `SELECT table_name
86
+ FROM information_schema.tables
87
+ WHERE table_schema = 'public'
88
+ AND table_type = 'BASE TABLE'
89
+ ORDER BY table_name`
90
+ );
91
+ console.log("Tables in the database:", tables.rows.map((t) => t.table_name).join(", "));
92
+ for (const table of tables.rows) {
93
+ const tableName = table.table_name;
94
+ console.log(`Inspecting table: ${tableName}`);
95
+ const columns = await client.query(
96
+ `
97
+ SELECT
98
+ column_name,
99
+ data_type,
100
+ is_nullable,
101
+ column_default
102
+ FROM information_schema.columns
103
+ WHERE table_name = $1
104
+ AND table_schema = 'public'
105
+ ORDER BY ordinal_position
106
+ `,
107
+ [tableName]
108
+ );
109
+ if (columns.rows.length === 0) {
110
+ console.log(`No columns found for table: ${tableName}`);
111
+ continue;
112
+ }
113
+ console.log(`Columns in ${tableName}:`, columns.rows.map((c) => `${c.column_name} (${c.data_type}${c.is_nullable === "YES" ? ", nullable" : ""})`).join(", "));
114
+ const uniqueColumns = /* @__PURE__ */ new Map();
115
+ columns.rows.forEach((col) => {
116
+ if (!uniqueColumns.has(col.column_name)) {
117
+ uniqueColumns.set(col.column_name, col);
118
+ }
119
+ });
120
+ generatedTypes += `export interface ${tableName.charAt(0).toUpperCase() + tableName.slice(1)} {
121
+ `;
122
+ for (const col of uniqueColumns.values()) {
123
+ const tsType = mapPostgresToTypeScript(col.data_type, col.is_nullable === "YES");
124
+ generatedTypes += ` ${col.column_name}: ${tsType};
125
+ `;
126
+ }
127
+ generatedTypes += "}\n\n";
128
+ }
129
+ generatedTypes += "export interface Database {\n";
130
+ for (const table of tables.rows) {
131
+ const tableName = table.table_name.charAt(0).toUpperCase() + table.table_name.slice(1);
132
+ generatedTypes += ` ${tableName}: ${tableName};
133
+ `;
134
+ }
135
+ generatedTypes += "}\n\n";
136
+ await fs.writeFile("db-types.ts", generatedTypes, "utf8");
137
+ console.log("TypeScript types written to db-types.ts");
138
+ process.exit(0);
139
+ };
6
140
 
7
141
  class DriftSQLClient {
8
142
  client;
@@ -11,6 +145,7 @@ class DriftSQLClient {
11
145
  libsqlClient;
12
146
  neonClient;
13
147
  postgresClient;
148
+ drivers;
14
149
  constructor(options) {
15
150
  this.client = ky.create({
16
151
  prefixUrl: options.url,
@@ -33,6 +168,7 @@ class DriftSQLClient {
33
168
  this.pool = options.drivers?.postgres ? new Pool(options.drivers.postgres) : void 0;
34
169
  this.libsqlClient = options.drivers?.libsql ? createClient(options.drivers.libsql) : void 0;
35
170
  this.mysqlClient = options.drivers?.mysql ? mysql.createConnection(options.drivers.mysql) : void 0;
171
+ this.drivers = options.drivers || {};
36
172
  }
37
173
  convertLibsqlResult(result) {
38
174
  const rows = result.rows.map((row) => {
@@ -49,6 +185,9 @@ class DriftSQLClient {
49
185
  fields: result.columns.map((col) => ({ name: col, dataTypeID: 0 }))
50
186
  };
51
187
  }
188
+ inspect = async () => {
189
+ return inspectDB(this.drivers);
190
+ };
52
191
  async query(query, args) {
53
192
  if (this.pool) {
54
193
  try {
@@ -222,7 +361,13 @@ class DriftSQLClient {
222
361
  if (this.libsqlClient) {
223
362
  this.libsqlClient.close();
224
363
  }
364
+ if (this.mysqlClient) {
365
+ await (await this.mysqlClient).end();
366
+ }
367
+ if (this.postgresClient) {
368
+ await this.postgresClient.end();
369
+ }
225
370
  }
226
371
  }
227
372
 
228
- export { DriftSQLClient };
373
+ export { DriftSQLClient, inspectDB };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "driftsql",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
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",