Skip to content

Commit d50bc95

Browse files
committed
relax requirement on providing name of query
1 parent ac96717 commit d50bc95

File tree

5 files changed

+208
-1
lines changed

5 files changed

+208
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Autoinsert trailing commas in embedded SQL blocks.
44
- BREAKING CHANGE: `Null.t` is no longer emitted, all `null` values are autoconverted to `option`. This gives a much more idiomatic ReScript experience.
55
- Emit actually runnable query in module comment for each query, instead of the original non-valid SQL query.
6+
- Relax requirement on providing query via `@name` comment.
67

78
# 2.4.0
89

packages/cli/src/__snapshots__/rescript.test.ts.snap

+153
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,95 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`inserts @name when needed 1`] = `
4+
Object {
5+
"events": Array [],
6+
"queries": Array [
7+
Object {
8+
"name": "FindBookById",
9+
"params": Array [
10+
Object {
11+
"codeRefs": Object {
12+
"used": Array [
13+
Object {
14+
"a": 61,
15+
"b": 62,
16+
"col": 36,
17+
"line": 2,
18+
},
19+
],
20+
},
21+
"name": "id",
22+
"required": false,
23+
"transform": Object {
24+
"type": "scalar",
25+
},
26+
},
27+
],
28+
"statement": Object {
29+
"body": "SELECT * FROM books WHERE id = :id",
30+
"loc": Object {
31+
"a": 29,
32+
"b": 62,
33+
"col": 4,
34+
"line": 2,
35+
},
36+
},
37+
"usedParamSet": Object {
38+
"id": true,
39+
},
40+
},
41+
Object {
42+
"name": "Query2",
43+
"params": Array [],
44+
"statement": Object {
45+
"body": "SELECT * FROM books",
46+
"loc": Object {
47+
"a": 85,
48+
"b": 103,
49+
"col": 0,
50+
"line": 5,
51+
},
52+
},
53+
"usedParamSet": Object {},
54+
},
55+
Object {
56+
"name": "Query3",
57+
"params": Array [
58+
Object {
59+
"codeRefs": Object {
60+
"used": Array [
61+
Object {
62+
"a": 158,
63+
"b": 159,
64+
"col": 32,
65+
"line": 8,
66+
},
67+
],
68+
},
69+
"name": "id",
70+
"required": false,
71+
"transform": Object {
72+
"type": "scalar",
73+
},
74+
},
75+
],
76+
"statement": Object {
77+
"body": "SELECT * FROM books WHERE id = :id",
78+
"loc": Object {
79+
"a": 126,
80+
"b": 159,
81+
"col": 0,
82+
"line": 8,
83+
},
84+
},
85+
"usedParamSet": Object {
86+
"id": true,
87+
},
88+
},
89+
],
90+
}
91+
`;
92+
393
exports[`inserts trailing semicolon when needed 1`] = `
494
Object {
595
"events": Array [],
@@ -97,3 +187,66 @@ Object {
97187
],
98188
}
99189
`;
190+
191+
exports[`preserves @param when inserting @name 1`] = `
192+
Object {
193+
"events": Array [],
194+
"queries": Array [
195+
Object {
196+
"name": "Query1",
197+
"params": Array [
198+
Object {
199+
"codeRefs": Object {
200+
"defined": Object {
201+
"a": 24,
202+
"b": 35,
203+
"col": 7,
204+
"line": 3,
205+
},
206+
"used": Array [
207+
Object {
208+
"a": 196,
209+
"b": 207,
210+
"col": 63,
211+
"line": 9,
212+
},
213+
],
214+
},
215+
"name": "notification",
216+
"required": false,
217+
"transform": Object {
218+
"keys": Array [
219+
Object {
220+
"name": "payload",
221+
"required": false,
222+
},
223+
Object {
224+
"name": "user_id",
225+
"required": false,
226+
},
227+
Object {
228+
"name": "type",
229+
"required": false,
230+
},
231+
],
232+
"type": "pick_tuple",
233+
},
234+
},
235+
],
236+
"statement": Object {
237+
"body": "
238+
INSERT INTO notifications (payload, user_id, type) VALUES :notification",
239+
"loc": Object {
240+
"a": 69,
241+
"b": 207,
242+
"col": 0,
243+
"line": 6,
244+
},
245+
},
246+
"usedParamSet": Object {
247+
"notification": true,
248+
},
249+
},
250+
],
251+
}
252+
`;

packages/cli/src/parseRescript.ts

+20
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@ export function parseCode(fileContent: string): SQLParseResult {
1919
if (!query.endsWith(';')) {
2020
query += ';';
2121
}
22+
23+
if (!query.includes('@name')) {
24+
// Handle potentially existing doc comment
25+
if (query.trim().startsWith('/*')) {
26+
const lines = query.split('\n');
27+
28+
let comment = `/* @name Query${queries.length + 1}\n`;
29+
for (let i = 0; i <= lines.length - 1; i += 1) {
30+
const line = lines[i].trim().replace('/*', '');
31+
comment += line + '\n';
32+
if (line.endsWith('*/')) {
33+
break;
34+
}
35+
}
36+
query = `${comment}\n${query}`;
37+
} else {
38+
query = `/* @name Query${queries.length + 1} */\n${query}`;
39+
}
40+
}
41+
2242
queries.push(query);
2343
}
2444

packages/cli/src/rescript.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,37 @@ test('inserts trailing semicolon when needed', () => {
2828
const result = parseCode(fileContent);
2929
expect(result).toMatchSnapshot();
3030
});
31+
32+
test('inserts @name when needed', () => {
33+
const fileContent = `
34+
let query = %sql.one(\`
35+
/* @name FindBookById */
36+
SELECT * FROM books WHERE id = :id
37+
\`);
38+
39+
let queryMany = %sql.many(\`
40+
SELECT * FROM books
41+
\`);
42+
43+
let queryExpect = %sql.expectOne(\`
44+
SELECT * FROM books WHERE id = :id
45+
\`);
46+
`;
47+
48+
const result = parseCode(fileContent);
49+
expect(result).toMatchSnapshot();
50+
});
51+
52+
test('preserves @param when inserting @name', () => {
53+
const fileContent = `
54+
let query = %sql.one(\`
55+
/*
56+
@param notification -> (payload, user_id, type)
57+
*/
58+
INSERT INTO notifications (payload, user_id, type) VALUES :notification
59+
\`);
60+
`;
61+
62+
const result = parseCode(fileContent);
63+
expect(result).toMatchSnapshot();
64+
});

packages/example/src/books/BookService.res

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ let findBookById = (client, ~id) => {
99

1010
let booksByAuthor = (client, ~authorName) => {
1111
let query = %sql.many(`
12-
/* @name BooksByAuthor */
1312
SELECT b.* FROM books b
1413
INNER JOIN authors a ON a.id = b.author_id
1514
WHERE a.first_name || ' ' || a.last_name = :authorName!;

0 commit comments

Comments
 (0)