@sqg/sqg 0.5.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.
@@ -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,108 @@
1
+ // {{generatedComment}}
2
+ import { DatabaseSync, StatementSync } from 'node:sqlite';
3
+
4
+ interface RunResult {
5
+ changes: number;
6
+ lastInsertRowid: number;
7
+ }
8
+
9
+ export class {{className}} {
10
+ private statements = new Map<string, StatementSync>();
11
+
12
+ constructor(private db: DatabaseSync) {}
13
+
14
+ private prepare(id: string, query: string) {
15
+ let stmt = this.statements.get(id);
16
+ if (!stmt) {
17
+ stmt = this.db.prepare(query);
18
+ this.statements.set(id, stmt);
19
+ }
20
+ return stmt;
21
+ }
22
+
23
+ static getMigrations(): string[] {
24
+ return [
25
+ {{#each migrations}}
26
+ {{{quote sqlQuery}}},
27
+ {{/each}}
28
+ ];
29
+ }
30
+
31
+ static getQueryNames(): Map<string, keyof {{className}}> {
32
+ return new Map([
33
+ {{#each queries}} {{#unless skipGenerateFunction}}
34
+ ["{{id}}", "{{functionName}}"],{{/unless}}{{/each}}]
35
+ );
36
+ }
37
+
38
+ {{#each queries}}
39
+ {{#unless skipGenerateFunction}}
40
+ {{functionName}}({{#each variables}}{{name}}: {{type}}{{#unless @last}}, {{/unless}}{{/each}}): {{> returnType }} {
41
+ const stmt = this.prepare('{{id}}',
42
+ {{{quote sqlQuery}}});
43
+ {{> execute}}
44
+ }
45
+ {{/unless}}
46
+ {{/each}}
47
+ }
48
+
49
+ {{#*inline "params"}}{{#each parameterNames}}{{this}}{{#unless @last}}, {{/unless}}{{/each}}{{/inline}}
50
+
51
+ {{#*inline "paramTypes"}}{{#each parameters}}{{type}} {{#unless @last}}, {{/unless}}{{/each}}{{/inline}}
52
+
53
+ {{#*inline "columnTypes"}}{{#each columns}}{{name}}: {{mapType .}}{{#unless @last}}, {{/unless}}{{/each}}{{/inline}}
54
+
55
+ {{#*inline "rowType"}}
56
+ {{#if isQuery~}}
57
+ {{#if isPluck~}}
58
+ {{#if columns.length~}} {{mapType (lookup columns 0)}} {{else}}any{{/if~}}
59
+ {{~else~}}
60
+ {{#if columns.length}}{ {{> columnTypes}} }{{else}}any{{/if~}}
61
+ {{/if~}}
62
+ {{~else~}}
63
+ any
64
+ {{~/if~}}
65
+ {{/inline~}}
66
+
67
+ {{#*inline "resultType"}}
68
+ {{#if isQuery~}}
69
+ {{> rowType}}
70
+ {{~else~}}
71
+ any
72
+ {{~/if~}}
73
+ {{/inline~}}
74
+
75
+
76
+ {{#*inline "returnType"}}
77
+ {{#if isQuery~}}
78
+ {{#if isOne~}}
79
+ {{> rowType}} | undefined
80
+ {{~else~}}
81
+ {{> rowType}}[]
82
+ {{/if~}}
83
+ {{~else~}}
84
+ RunResult
85
+ {{~/if~}}
86
+ {{/inline~}}
87
+
88
+ {{#*inline "execute"}}
89
+ {{#if isQuery}}
90
+ {{#if isOne}}
91
+ {{#if isPluck}}
92
+ const row = stmt.get({{> params}}) as Record<string, {{> rowType}}> | undefined;
93
+ return row ? Object.values(row)[0] : undefined;
94
+ {{else}}
95
+ return stmt.get({{> params}}) as {{> rowType}} | undefined;
96
+ {{/if}}
97
+ {{else}}
98
+ {{#if isPluck}}
99
+ const rows = stmt.all({{> params}}) as Record<string, {{> rowType}}>[];
100
+ return rows.map(row => Object.values(row)[0]);
101
+ {{else}}
102
+ return stmt.all({{> params}}) as {{> rowType}}[];
103
+ {{/if}}
104
+ {{/if}}
105
+ {{else}}
106
+ return stmt.run({{> params}}) as RunResult;
107
+ {{/if}}
108
+ {{/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.5.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": {
@@ -56,9 +56,12 @@
56
56
  "prettier": "^3.7.4",
57
57
  "prettier-plugin-java": "^2.7.7",
58
58
  "yaml": "^2.8.2",
59
- "zod": "^4.3.5"
59
+ "zod": "^4.3.5",
60
+ "@modelcontextprotocol/sdk": "^1.0.4"
60
61
  },
61
62
  "devDependencies": {
63
+ "@libsql/client": "^0.17.0",
64
+ "@tursodatabase/database": "^0.4.3",
62
65
  "@types/better-sqlite3": "^7.6.13",
63
66
  "@types/node": "^25.0.3",
64
67
  "@types/pg": "^8.16.0",