@sqg/sqg 0.6.0 → 0.7.0

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/sqg.mjs CHANGED
@@ -55,6 +55,22 @@ const GENERATORS = {
55
55
  extension: ".ts",
56
56
  template: "node-sqlite.hbs"
57
57
  },
58
+ "typescript/sqlite/libsql": {
59
+ language: "typescript",
60
+ engine: "sqlite",
61
+ driver: "libsql",
62
+ description: "TypeScript with @libsql/client (Turso)",
63
+ extension: ".ts",
64
+ template: "libsql.hbs"
65
+ },
66
+ "typescript/sqlite/turso": {
67
+ language: "typescript",
68
+ engine: "sqlite",
69
+ driver: "turso",
70
+ description: "TypeScript with Turso (limbo) native driver",
71
+ extension: ".ts",
72
+ template: "turso.hbs"
73
+ },
58
74
  "typescript/duckdb/node-api": {
59
75
  language: "typescript",
60
76
  engine: "duckdb",
@@ -2090,7 +2106,9 @@ function getGenerator(generator) {
2090
2106
  const key = `${info.language}/${info.driver}`;
2091
2107
  switch (key) {
2092
2108
  case "typescript/better-sqlite3":
2093
- case "typescript/node": return new TsGenerator(`templates/${info.template}`);
2109
+ case "typescript/node":
2110
+ case "typescript/libsql":
2111
+ case "typescript/turso": return new TsGenerator(`templates/${info.template}`);
2094
2112
  case "typescript/node-api": return new TsDuckDBGenerator(`templates/${info.template}`);
2095
2113
  case "java/jdbc": return new JavaGenerator(`templates/${info.template}`);
2096
2114
  case "java/arrow": return new JavaDuckDBArrowGenerator(`templates/${info.template}`);
@@ -2550,7 +2568,7 @@ async function processProject(projectPath) {
2550
2568
  //#region src/mcp-server.ts
2551
2569
  const server = new Server({
2552
2570
  name: "sqg-mcp",
2553
- version: process.env.npm_package_version ?? "0.6.0"
2571
+ version: process.env.npm_package_version ?? "0.7.0"
2554
2572
  }, { capabilities: {
2555
2573
  tools: {},
2556
2574
  resources: {}
@@ -2879,7 +2897,7 @@ async function startMcpServer() {
2879
2897
 
2880
2898
  //#endregion
2881
2899
  //#region src/sqg.ts
2882
- const version = process.env.npm_package_version ?? "0.6.0";
2900
+ const version = process.env.npm_package_version ?? "0.7.0";
2883
2901
  const description = process.env.npm_package_description ?? "SQG - SQL Query Generator - Type-safe code generation from SQL (https://sqg.dev)";
2884
2902
  consola.level = LogLevels.info;
2885
2903
  const program = new Command().name("sqg").description(`${description}
@@ -0,0 +1,101 @@
1
+ // {{generatedComment}}
2
+ import type { Client, InArgs } from '@libsql/client';
3
+
4
+ interface RunResult {
5
+ changes: number;
6
+ lastInsertRowid: bigint;
7
+ }
8
+
9
+ export class {{className}} {
10
+ constructor(private client: Client) {}
11
+
12
+ static getMigrations(): string[] {
13
+ return [
14
+ {{#each migrations}}
15
+ {{{quote sqlQuery}}},
16
+ {{/each}}
17
+ ];
18
+ }
19
+
20
+ static getQueryNames(): Map<string, keyof {{className}}> {
21
+ return new Map([
22
+ {{#each queries}} {{#unless skipGenerateFunction}}
23
+ ["{{id}}", "{{functionName}}"],{{/unless}}{{/each}}]
24
+ );
25
+ }
26
+
27
+ {{#each queries}}
28
+ {{#unless skipGenerateFunction}}
29
+ async {{functionName}}({{#each variables}}{{name}}: {{type}}{{#unless @last}}, {{/unless}}{{/each}}): Promise<{{> returnType }}> {
30
+ const result = await this.client.execute({
31
+ sql: {{{quote sqlQuery}}},
32
+ args: [{{> params}}] as InArgs,
33
+ });
34
+ {{> execute}}
35
+ }
36
+ {{/unless}}
37
+ {{/each}}
38
+ }
39
+
40
+ {{#*inline "params"}}{{#each parameterNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}{{/inline}}
41
+
42
+ {{#*inline "paramTypes"}}{{#each parameters}}{{type}} {{#unless @last}}, {{/unless}}{{/each}}{{/inline}}
43
+
44
+ {{#*inline "columnTypes"}}{{#each columns}}{{name}}: {{mapType .}}{{#unless @last}}, {{/unless}}{{/each}}{{/inline}}
45
+
46
+ {{#*inline "rowType"}}
47
+ {{#if isQuery~}}
48
+ {{#if isPluck~}}
49
+ {{#if columns.length~}} {{mapType (lookup columns 0)}} {{else}}unknown{{/if~}}
50
+ {{~else~}}
51
+ {{#if columns.length}}{ {{> columnTypes}} }{{else}}unknown{{/if~}}
52
+ {{/if~}}
53
+ {{~else~}}
54
+ unknown
55
+ {{~/if~}}
56
+ {{/inline~}}
57
+
58
+ {{#*inline "resultType"}}
59
+ {{#if isQuery~}}
60
+ {{> rowType}}
61
+ {{~else~}}
62
+ unknown
63
+ {{~/if~}}
64
+ {{/inline~}}
65
+
66
+
67
+ {{#*inline "returnType"}}
68
+ {{#if isQuery~}}
69
+ {{#if isOne~}}
70
+ {{> rowType}} | undefined
71
+ {{~else~}}
72
+ {{> rowType}}[]
73
+ {{/if~}}
74
+ {{~else~}}
75
+ RunResult
76
+ {{~/if~}}
77
+ {{/inline~}}
78
+
79
+ {{#*inline "execute"}}
80
+ {{#if isQuery}}
81
+ {{#if isOne}}
82
+ {{#if isPluck}}
83
+ const row = result.rows[0];
84
+ return row ? Object.values(row)[0] as {{> rowType}} : undefined;
85
+ {{else}}
86
+ return (result.rows as unknown[])[0] as {{> rowType}} | undefined;
87
+ {{/if}}
88
+ {{else}}
89
+ {{#if isPluck}}
90
+ return result.rows.map(row => Object.values(row)[0] as {{> rowType}});
91
+ {{else}}
92
+ return (result.rows as unknown[]) as {{> rowType}}[];
93
+ {{/if}}
94
+ {{/if}}
95
+ {{else}}
96
+ return {
97
+ changes: result.rowsAffected,
98
+ lastInsertRowid: result.lastInsertRowid ?? 0n,
99
+ };
100
+ {{/if}}
101
+ {{/inline}}
@@ -0,0 +1,112 @@
1
+ // {{generatedComment}}
2
+ import { connect, type Database } from '@tursodatabase/database';
3
+
4
+ interface RunResult {
5
+ changes: number;
6
+ lastInsertRowid: number;
7
+ }
8
+
9
+ // Statement type inferred from Database.prepare()
10
+ type Statement = Awaited<ReturnType<Database['prepare']>>;
11
+
12
+ export class {{className}} {
13
+ private statements = new Map<string, Statement>();
14
+
15
+ constructor(private db: Database) {}
16
+
17
+ private async prepare(id: string, query: string): Promise<Statement> {
18
+ let stmt = this.statements.get(id);
19
+ if (!stmt) {
20
+ stmt = await this.db.prepare(query);
21
+ this.statements.set(id, stmt);
22
+ }
23
+ return stmt;
24
+ }
25
+
26
+ static getMigrations(): string[] {
27
+ return [
28
+ {{#each migrations}}
29
+ {{{quote sqlQuery}}},
30
+ {{/each}}
31
+ ];
32
+ }
33
+
34
+ static getQueryNames(): Map<string, keyof {{className}}> {
35
+ return new Map([
36
+ {{#each queries}} {{#unless skipGenerateFunction}}
37
+ ["{{id}}", "{{functionName}}"],{{/unless}}{{/each}}]
38
+ );
39
+ }
40
+
41
+ {{#each queries}}
42
+ {{#unless skipGenerateFunction}}
43
+ async {{functionName}}({{#each variables}}{{name}}: {{type}}{{#unless @last}}, {{/unless}}{{/each}}): Promise<{{> returnType }}> {
44
+ const stmt = await this.prepare('{{id}}',
45
+ {{{quote sqlQuery}}});
46
+ {{> execute}}
47
+ }
48
+ {{/unless}}
49
+ {{/each}}
50
+ }
51
+
52
+ {{#*inline "params"}}{{#each parameterNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}{{/inline}}
53
+
54
+ {{#*inline "paramTypes"}}{{#each parameters}}{{type}} {{#unless @last}}, {{/unless}}{{/each}}{{/inline}}
55
+
56
+ {{#*inline "columnTypes"}}{{#each columns}}{{name}}: {{mapType .}}{{#unless @last}}, {{/unless}}{{/each}}{{/inline}}
57
+
58
+ {{#*inline "rowType"}}
59
+ {{#if isQuery~}}
60
+ {{#if isPluck~}}
61
+ {{#if columns.length~}} {{mapType (lookup columns 0)}} {{else}}unknown{{/if~}}
62
+ {{~else~}}
63
+ {{#if columns.length}}{ {{> columnTypes}} }{{else}}unknown{{/if~}}
64
+ {{/if~}}
65
+ {{~else~}}
66
+ unknown
67
+ {{~/if~}}
68
+ {{/inline~}}
69
+
70
+ {{#*inline "resultType"}}
71
+ {{#if isQuery~}}
72
+ {{> rowType}}
73
+ {{~else~}}
74
+ unknown
75
+ {{~/if~}}
76
+ {{/inline~}}
77
+
78
+
79
+ {{#*inline "returnType"}}
80
+ {{#if isQuery~}}
81
+ {{#if isOne~}}
82
+ {{> rowType}} | undefined
83
+ {{~else~}}
84
+ {{> rowType}}[]
85
+ {{/if~}}
86
+ {{~else~}}
87
+ RunResult
88
+ {{~/if~}}
89
+ {{/inline~}}
90
+
91
+ {{#*inline "execute"}}
92
+ {{#if isQuery}}
93
+ {{#if isOne}}
94
+ {{#if isPluck}}
95
+ const row = await stmt.get({{> params}}) as Record<string, {{> rowType}}> | undefined;
96
+ return row ? Object.values(row)[0] : undefined;
97
+ {{else}}
98
+ return await stmt.get({{> params}}) as {{> rowType}} | undefined;
99
+ {{/if}}
100
+ {{else}}
101
+ {{#if isPluck}}
102
+ const rows = await stmt.all({{> params}}) as Record<string, {{> rowType}}>[];
103
+ return rows.map(row => Object.values(row)[0]);
104
+ {{else}}
105
+ return await stmt.all({{> params}}) as {{> rowType}}[];
106
+ {{/if}}
107
+ {{/if}}
108
+ {{else}}
109
+ const result = await stmt.run({{> params}});
110
+ return result as RunResult;
111
+ {{/if}}
112
+ {{/inline}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sqg/sqg",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "SQG - SQL Query Generator - Type-safe code generation from SQL (https://sqg.dev)",
5
5
  "type": "module",
6
6
  "bin": {
@@ -60,6 +60,8 @@
60
60
  "@modelcontextprotocol/sdk": "^1.0.4"
61
61
  },
62
62
  "devDependencies": {
63
+ "@libsql/client": "^0.17.0",
64
+ "@tursodatabase/database": "^0.4.3",
63
65
  "@types/better-sqlite3": "^7.6.13",
64
66
  "@types/node": "^25.0.3",
65
67
  "@types/pg": "^8.16.0",