Skip to content

Commit 32827fb

Browse files
committed
feat: wrap queries in workspaces
1 parent ebd63e3 commit 32827fb

File tree

6 files changed

+256
-21
lines changed

6 files changed

+256
-21
lines changed

examples/authors/postgresql/query.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ RETURNING *;
1717
-- name: DeleteAuthor :exec
1818
DELETE FROM authors
1919
WHERE id = $1;
20+
21+
-- name: Authors_Get :one
22+
SELECT * FROM authors
23+
WHERE id = $1 LIMIT 1;
24+
25+
-- name: Authors_List :many
26+
SELECT * FROM authors
27+
ORDER BY name;

examples/bun-pg/src/db/query_sql.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,58 @@ export async function deleteAuthor(client: Client, args: DeleteAuthorArgs): Prom
114114
});
115115
}
116116

117+
export module Authors {
118+
const getQuery = `-- name: Authors_Get :one
119+
SELECT id, name, bio FROM authors
120+
WHERE id = $1 LIMIT 1`;
121+
export interface Authors_GetArgs {
122+
id: string;
123+
}
124+
export interface Authors_GetRow {
125+
id: string;
126+
name: string;
127+
bio: string | null;
128+
}
129+
export async function get(client: Client, args: Authors_GetArgs): Promise<Authors_GetRow | null> {
130+
const result = await client.query({
131+
text: getQuery,
132+
values: [args.id],
133+
rowMode: "array"
134+
});
135+
if (result.rows.length !== 1) {
136+
return null;
137+
}
138+
const row = result.rows[0];
139+
return {
140+
id: row[0],
141+
name: row[1],
142+
bio: row[2]
143+
};
144+
}
145+
}
146+
147+
export module Authors {
148+
const listQuery = `-- name: Authors_List :many
149+
SELECT id, name, bio FROM authors
150+
ORDER BY name`;
151+
export interface Authors_ListRow {
152+
id: string;
153+
name: string;
154+
bio: string | null;
155+
}
156+
export async function list(client: Client): Promise<Authors_ListRow[]> {
157+
const result = await client.query({
158+
text: listQuery,
159+
values: [],
160+
rowMode: "array"
161+
});
162+
return result.rows.map(row => {
163+
return {
164+
id: row[0],
165+
name: row[1],
166+
bio: row[2]
167+
};
168+
});
169+
}
170+
}
171+

examples/bun-postgres/src/db/query_sql.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,50 @@ export async function deleteAuthor(sql: Sql, args: DeleteAuthorArgs): Promise<vo
9797
await sql.unsafe(deleteAuthorQuery, [args.id]);
9898
}
9999

100+
export module Authors {
101+
const getQuery = `-- name: Authors_Get :one
102+
SELECT id, name, bio FROM authors
103+
WHERE id = $1 LIMIT 1`;
104+
export interface Authors_GetArgs {
105+
id: string;
106+
}
107+
export interface Authors_GetRow {
108+
id: string;
109+
name: string;
110+
bio: string | null;
111+
}
112+
export async function get(sql: Sql, args: Authors_GetArgs): Promise<Authors_GetRow | null> {
113+
const rows = await sql.unsafe(getQuery, [args.id]).values();
114+
if (rows.length !== 1) {
115+
return null;
116+
}
117+
const row = rows[0];
118+
if (!row) {
119+
return null;
120+
}
121+
return {
122+
id: row[0],
123+
name: row[1],
124+
bio: row[2]
125+
};
126+
}
127+
}
128+
129+
export module Authors {
130+
const listQuery = `-- name: Authors_List :many
131+
SELECT id, name, bio FROM authors
132+
ORDER BY name`;
133+
export interface Authors_ListRow {
134+
id: string;
135+
name: string;
136+
bio: string | null;
137+
}
138+
export async function list(sql: Sql): Promise<Authors_ListRow[]> {
139+
return (await sql.unsafe(listQuery, []).values()).map(row => ({
140+
id: row[0],
141+
name: row[1],
142+
bio: row[2]
143+
}));
144+
}
145+
}
146+

examples/node-pg/src/db/query_sql.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,58 @@ export async function deleteAuthor(client: Client, args: DeleteAuthorArgs): Prom
114114
});
115115
}
116116

117+
export module Authors {
118+
const getQuery = `-- name: Authors_Get :one
119+
SELECT id, name, bio FROM authors
120+
WHERE id = $1 LIMIT 1`;
121+
export interface Authors_GetArgs {
122+
id: string;
123+
}
124+
export interface Authors_GetRow {
125+
id: string;
126+
name: string;
127+
bio: string | null;
128+
}
129+
export async function get(client: Client, args: Authors_GetArgs): Promise<Authors_GetRow | null> {
130+
const result = await client.query({
131+
text: getQuery,
132+
values: [args.id],
133+
rowMode: "array"
134+
});
135+
if (result.rows.length !== 1) {
136+
return null;
137+
}
138+
const row = result.rows[0];
139+
return {
140+
id: row[0],
141+
name: row[1],
142+
bio: row[2]
143+
};
144+
}
145+
}
146+
147+
export module Authors {
148+
const listQuery = `-- name: Authors_List :many
149+
SELECT id, name, bio FROM authors
150+
ORDER BY name`;
151+
export interface Authors_ListRow {
152+
id: string;
153+
name: string;
154+
bio: string | null;
155+
}
156+
export async function list(client: Client): Promise<Authors_ListRow[]> {
157+
const result = await client.query({
158+
text: listQuery,
159+
values: [],
160+
rowMode: "array"
161+
});
162+
return result.rows.map(row => {
163+
return {
164+
id: row[0],
165+
name: row[1],
166+
bio: row[2]
167+
};
168+
});
169+
}
170+
}
171+

examples/node-postgres/src/db/query_sql.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,50 @@ export async function deleteAuthor(sql: Sql, args: DeleteAuthorArgs): Promise<vo
9797
await sql.unsafe(deleteAuthorQuery, [args.id]);
9898
}
9999

100+
export module Authors {
101+
const getQuery = `-- name: Authors_Get :one
102+
SELECT id, name, bio FROM authors
103+
WHERE id = $1 LIMIT 1`;
104+
export interface Authors_GetArgs {
105+
id: string;
106+
}
107+
export interface Authors_GetRow {
108+
id: string;
109+
name: string;
110+
bio: string | null;
111+
}
112+
export async function get(sql: Sql, args: Authors_GetArgs): Promise<Authors_GetRow | null> {
113+
const rows = await sql.unsafe(getQuery, [args.id]).values();
114+
if (rows.length !== 1) {
115+
return null;
116+
}
117+
const row = rows[0];
118+
if (!row) {
119+
return null;
120+
}
121+
return {
122+
id: row[0],
123+
name: row[1],
124+
bio: row[2]
125+
};
126+
}
127+
}
128+
129+
export module Authors {
130+
const listQuery = `-- name: Authors_List :many
131+
SELECT id, name, bio FROM authors
132+
ORDER BY name`;
133+
export interface Authors_ListRow {
134+
id: string;
135+
name: string;
136+
bio: string | null;
137+
}
138+
export async function list(sql: Sql): Promise<Authors_ListRow[]> {
139+
return (await sql.unsafe(listQuery, []).values()).map(row => ({
140+
id: row[0],
141+
name: row[1],
142+
bio: row[2]
143+
}));
144+
}
145+
}
146+

src/app.ts

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
createPrinter,
1616
createSourceFile,
1717
factory,
18+
Statement,
1819
} from "typescript";
1920

2021
import {
@@ -52,30 +53,34 @@ interface Driver {
5253
name: string,
5354
text: string,
5455
iface: string | undefined,
55-
params: Parameter[]
56-
) => Node;
56+
params: Parameter[],
57+
namespace?: string,
58+
) => Statement;
5759
execlastidDecl: (
5860
name: string,
5961
text: string,
6062
iface: string | undefined,
61-
params: Parameter[]
62-
) => Node;
63+
params: Parameter[],
64+
namespace?: string,
65+
) => Statement;
6366
manyDecl: (
6467
name: string,
6568
text: string,
6669
argIface: string | undefined,
6770
returnIface: string,
6871
params: Parameter[],
69-
columns: Column[]
70-
) => Node;
72+
columns: Column[],
73+
namespace?: string,
74+
) => Statement;
7175
oneDecl: (
7276
name: string,
7377
text: string,
7478
argIface: string | undefined,
7579
returnIface: string,
7680
params: Parameter[],
77-
columns: Column[]
78-
) => Node;
81+
columns: Column[],
82+
namespace?: string,
83+
) => Statement;
7984
}
8085

8186
function createNodeGenerator(options: Options): Driver {
@@ -135,10 +140,15 @@ function codegen(input: GenerateRequest): GenerateResponse {
135140
colmap.set(column.name, count + 1);
136141
}
137142

138-
const lowerName = query.name[0].toLowerCase() + query.name.slice(1);
143+
const [namespace, name] = query.name.indexOf('_') > -1
144+
? query.name.split("_")
145+
: [undefined, query.name];
146+
const lowerName = name[0].toLowerCase() + name.slice(1);
139147
const textName = `${lowerName}Query`;
140148

141-
nodes.push(
149+
const nodesToPush: Statement[] = [];
150+
151+
nodesToPush.push(
142152
queryDecl(
143153
textName,
144154
`-- name: ${query.name} ${query.cmd}
@@ -150,54 +160,67 @@ ${query.text}`
150160
let returnIface = undefined;
151161
if (query.params.length > 0) {
152162
argIface = `${query.name}Args`;
153-
nodes.push(argsDecl(argIface, driver, query.params));
163+
nodesToPush.push(argsDecl(argIface, driver, query.params));
154164
}
155165
if (query.columns.length > 0) {
156166
returnIface = `${query.name}Row`;
157-
nodes.push(rowDecl(returnIface, driver, query.columns));
167+
nodesToPush.push(rowDecl(returnIface, driver, query.columns));
158168
}
159169

160170
switch (query.cmd) {
161171
case ":exec": {
162-
nodes.push(
163-
driver.execDecl(lowerName, textName, argIface, query.params)
172+
nodesToPush.push(
173+
driver.execDecl(lowerName, textName, argIface, query.params, namespace)
164174
);
165175
break;
166176
}
167177
case ":execlastid": {
168-
nodes.push(
169-
driver.execlastidDecl(lowerName, textName, argIface, query.params)
178+
nodesToPush.push(
179+
driver.execlastidDecl(lowerName, textName, argIface, query.params, namespace)
170180
);
171181
break;
172182
}
173183
case ":one": {
174-
nodes.push(
184+
nodesToPush.push(
175185
driver.oneDecl(
176186
lowerName,
177187
textName,
178188
argIface,
179189
returnIface ?? "void",
180190
query.params,
181-
query.columns
191+
query.columns,
192+
namespace
182193
)
183194
);
184195
break;
185196
}
186197
case ":many": {
187-
nodes.push(
198+
nodesToPush.push(
188199
driver.manyDecl(
189200
lowerName,
190201
textName,
191202
argIface,
192203
returnIface ?? "void",
193204
query.params,
194-
query.columns
205+
query.columns,
206+
namespace
195207
)
196208
);
197209
break;
198210
}
199211
}
200-
if (nodes) {
212+
if (nodesToPush) {
213+
if (namespace) {
214+
nodes.push(
215+
factory.createModuleDeclaration(
216+
[factory.createToken(SyntaxKind.ExportKeyword),],
217+
factory.createIdentifier(namespace),
218+
factory.createModuleBlock(nodesToPush)
219+
))
220+
} else {
221+
nodes.push(...nodesToPush);
222+
}
223+
201224
files.push(
202225
new File({
203226
name: `${filename.replace(".", "_")}.ts`,

0 commit comments

Comments
 (0)