Skip to content

Commit

Permalink
relax requirement on providing name of query
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed May 2, 2024
1 parent ac96717 commit d50bc95
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Autoinsert trailing commas in embedded SQL blocks.
- BREAKING CHANGE: `Null.t` is no longer emitted, all `null` values are autoconverted to `option`. This gives a much more idiomatic ReScript experience.
- Emit actually runnable query in module comment for each query, instead of the original non-valid SQL query.
- Relax requirement on providing query via `@name` comment.

# 2.4.0

Expand Down
153 changes: 153 additions & 0 deletions packages/cli/src/__snapshots__/rescript.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,95 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`inserts @name when needed 1`] = `
Object {
"events": Array [],
"queries": Array [
Object {
"name": "FindBookById",
"params": Array [
Object {
"codeRefs": Object {
"used": Array [
Object {
"a": 61,
"b": 62,
"col": 36,
"line": 2,
},
],
},
"name": "id",
"required": false,
"transform": Object {
"type": "scalar",
},
},
],
"statement": Object {
"body": "SELECT * FROM books WHERE id = :id",
"loc": Object {
"a": 29,
"b": 62,
"col": 4,
"line": 2,
},
},
"usedParamSet": Object {
"id": true,
},
},
Object {
"name": "Query2",
"params": Array [],
"statement": Object {
"body": "SELECT * FROM books",
"loc": Object {
"a": 85,
"b": 103,
"col": 0,
"line": 5,
},
},
"usedParamSet": Object {},
},
Object {
"name": "Query3",
"params": Array [
Object {
"codeRefs": Object {
"used": Array [
Object {
"a": 158,
"b": 159,
"col": 32,
"line": 8,
},
],
},
"name": "id",
"required": false,
"transform": Object {
"type": "scalar",
},
},
],
"statement": Object {
"body": "SELECT * FROM books WHERE id = :id",
"loc": Object {
"a": 126,
"b": 159,
"col": 0,
"line": 8,
},
},
"usedParamSet": Object {
"id": true,
},
},
],
}
`;

exports[`inserts trailing semicolon when needed 1`] = `
Object {
"events": Array [],
Expand Down Expand Up @@ -97,3 +187,66 @@ Object {
],
}
`;

exports[`preserves @param when inserting @name 1`] = `
Object {
"events": Array [],
"queries": Array [
Object {
"name": "Query1",
"params": Array [
Object {
"codeRefs": Object {
"defined": Object {
"a": 24,
"b": 35,
"col": 7,
"line": 3,
},
"used": Array [
Object {
"a": 196,
"b": 207,
"col": 63,
"line": 9,
},
],
},
"name": "notification",
"required": false,
"transform": Object {
"keys": Array [
Object {
"name": "payload",
"required": false,
},
Object {
"name": "user_id",
"required": false,
},
Object {
"name": "type",
"required": false,
},
],
"type": "pick_tuple",
},
},
],
"statement": Object {
"body": "
INSERT INTO notifications (payload, user_id, type) VALUES :notification",
"loc": Object {
"a": 69,
"b": 207,
"col": 0,
"line": 6,
},
},
"usedParamSet": Object {
"notification": true,
},
},
],
}
`;
20 changes: 20 additions & 0 deletions packages/cli/src/parseRescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ export function parseCode(fileContent: string): SQLParseResult {
if (!query.endsWith(';')) {
query += ';';
}

if (!query.includes('@name')) {
// Handle potentially existing doc comment
if (query.trim().startsWith('/*')) {
const lines = query.split('\n');

let comment = `/* @name Query${queries.length + 1}\n`;
for (let i = 0; i <= lines.length - 1; i += 1) {
const line = lines[i].trim().replace('/*', '');
comment += line + '\n';
if (line.endsWith('*/')) {
break;
}
}
query = `${comment}\n${query}`;
} else {
query = `/* @name Query${queries.length + 1} */\n${query}`;
}
}

queries.push(query);
}

Expand Down
34 changes: 34 additions & 0 deletions packages/cli/src/rescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,37 @@ test('inserts trailing semicolon when needed', () => {
const result = parseCode(fileContent);
expect(result).toMatchSnapshot();
});

test('inserts @name when needed', () => {
const fileContent = `
let query = %sql.one(\`
/* @name FindBookById */
SELECT * FROM books WHERE id = :id
\`);
let queryMany = %sql.many(\`
SELECT * FROM books
\`);
let queryExpect = %sql.expectOne(\`
SELECT * FROM books WHERE id = :id
\`);
`;

const result = parseCode(fileContent);
expect(result).toMatchSnapshot();
});

test('preserves @param when inserting @name', () => {
const fileContent = `
let query = %sql.one(\`
/*
@param notification -> (payload, user_id, type)
*/
INSERT INTO notifications (payload, user_id, type) VALUES :notification
\`);
`;

const result = parseCode(fileContent);
expect(result).toMatchSnapshot();
});
1 change: 0 additions & 1 deletion packages/example/src/books/BookService.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ let findBookById = (client, ~id) => {

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

0 comments on commit d50bc95

Please sign in to comment.