Skip to content

Commit

Permalink
switch to rescript tools for extracting embedded tags
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed May 2, 2024
1 parent d50bc95 commit d0acb5c
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 87 deletions.
40 changes: 32 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"dependencies": {
"@pgtyped/parser": "^2.1.0",
"@pgtyped/wire": "^2.2.0",
"@rescript/tools": "^0.6.2",
"camel-case": "^4.1.1",
"chalk": "^4.0.0",
"chokidar": "^3.3.1",
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ async function generateTypedecsFromFile(
const typeSource: TypeSource = (query) => getTypes(query, connection);

const { queries, events } =
mode === 'res' ? parseRescriptFile(contents) : parseSQLFile(contents);
mode === 'res'
? parseRescriptFile(contents, fileName)
: parseSQLFile(contents);
if (events.length > 0) {
prettyPrintEvents(contents, events);
if (events.find((e) => 'critical' in e)) {
Expand Down
30 changes: 23 additions & 7 deletions packages/cli/src/parseRescript.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { parseSQLFile } from '@pgtyped/parser';
import { SQLParseResult } from '@pgtyped/parser/lib/loader/sql';
import cp from 'child_process';
// @ts-ignore
import { getBinaryPath } from '@rescript/tools/npm/getBinaryPath.js';

export function parseCode(fileContent: string): SQLParseResult {
export function parseCode(
fileContent: string,
fileName: string,
): SQLParseResult {
if (!fileContent.includes('%sql')) {
return {
queries: [],
Expand All @@ -10,12 +16,22 @@ export function parseCode(fileContent: string): SQLParseResult {
}

// Replace with more robust @rescript/tools CLI usage when that package ships linuxarm64 binary.
const regex = /%sql(?:\.\w+)?\(`([^`]*)`\)/g;
let match;
const queries = [];
const content: Array<{ contents: string }> = JSON.parse(
cp
.execFileSync(getBinaryPath(), [
'extract-embedded',
['sql', 'sql.one', 'sql.expectOne', 'sql.many', 'sql.execute'].join(
',',
),
fileName,
])
.toString(),
);

while ((match = regex.exec(fileContent)) !== null) {
let query = match[1].trim();
const queries: Array<string> = [];

content.forEach((v) => {
let query = v.contents.trim();
if (!query.endsWith(';')) {
query += ';';
}
Expand All @@ -40,7 +56,7 @@ export function parseCode(fileContent: string): SQLParseResult {
}

queries.push(query);
}
});

const asSql = queries.join('\n\n');

Expand Down
2 changes: 1 addition & 1 deletion packages/example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"pgtyped-rescript": "^2.4.0",
"pgtyped-rescript-query": "^2.3.0",
"rescript": "11.1.0",
"rescript-embed-lang": "^0.5.0",
"rescript-embed-lang": "^0.5.1",
"typescript": "4.9.4"
},
"devDependencies": {
Expand Down
52 changes: 26 additions & 26 deletions packages/example/src/books/BookService__sql.gen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export type arrayJSON_t = JSON_t[];

export type categoryArray = category[];

/** 'FindBookById' parameters type */
export type findBookByIdParams = { readonly id?: number };
/** 'BooksByAuthor' parameters type */
export type booksByAuthorParams = { readonly authorName: string };

/** 'FindBookById' return type */
export type findBookByIdResult = {
/** 'BooksByAuthor' return type */
export type booksByAuthorResult = {
readonly author_id: (undefined | number);
readonly big_int: (undefined | bigint);
readonly categories: (undefined | categoryArray);
Expand All @@ -29,14 +29,14 @@ export type findBookByIdResult = {
readonly rank: (undefined | number)
};

/** 'FindBookById' query type */
export type findBookByIdQuery = { readonly params: findBookByIdParams; readonly result: findBookByIdResult };
/** 'BooksByAuthor' query type */
export type booksByAuthorQuery = { readonly params: booksByAuthorParams; readonly result: booksByAuthorResult };

/** 'BooksByAuthor' parameters type */
export type booksByAuthorParams = { readonly authorName: string };
/** 'FindBookById' parameters type */
export type findBookByIdParams = { readonly id?: number };

/** 'BooksByAuthor' return type */
export type booksByAuthorResult = {
/** 'FindBookById' return type */
export type findBookByIdResult = {
readonly author_id: (undefined | number);
readonly big_int: (undefined | bigint);
readonly categories: (undefined | categoryArray);
Expand All @@ -46,40 +46,40 @@ export type booksByAuthorResult = {
readonly rank: (undefined | number)
};

/** 'BooksByAuthor' query type */
export type booksByAuthorQuery = { readonly params: booksByAuthorParams; readonly result: booksByAuthorResult };
/** 'FindBookById' query type */
export type findBookByIdQuery = { readonly params: findBookByIdParams; readonly result: findBookByIdResult };

/** Returns an array of all matched results. */
export const FindBookById_many: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<findBookByIdResult[]> = BookService__sqlJS.FindBookById.many as any;
export const BooksByAuthor_many: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<booksByAuthorResult[]> = BookService__sqlJS.BooksByAuthor.many as any;

/** Returns exactly 1 result. Returns `None` if more or less than exactly 1 result is returned. */
export const FindBookById_one: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<(undefined | findBookByIdResult)> = BookService__sqlJS.FindBookById.one as any;
export const BooksByAuthor_one: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<(undefined | booksByAuthorResult)> = BookService__sqlJS.BooksByAuthor.one as any;

/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
export const FindBookById_expectOne: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams, errorMessage:(undefined | string)) => Promise<
{ TAG: "Ok"; _0: findBookByIdResult }
| { TAG: "Error"; _0: string }> = BookService__sqlJS.FindBookById.expectOne as any;
export const BooksByAuthor_expectOne: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams, errorMessage:(undefined | string)) => Promise<
{ TAG: "Ok"; _0: booksByAuthorResult }
| { TAG: "Error"; _0: string }> = BookService__sqlJS.BooksByAuthor.expectOne as any;

/** Executes the query, but ignores whatever is returned by it. */
export const FindBookById_execute: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<void> = BookService__sqlJS.FindBookById.execute as any;
export const BooksByAuthor_execute: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<void> = BookService__sqlJS.BooksByAuthor.execute as any;

export const findBookById: (params:findBookByIdParams, client:PgTyped_Pg_Client_t) => Promise<findBookByIdResult[]> = BookService__sqlJS.findBookById as any;
export const booksByAuthor: (params:booksByAuthorParams, client:PgTyped_Pg_Client_t) => Promise<booksByAuthorResult[]> = BookService__sqlJS.booksByAuthor as any;

/** Returns an array of all matched results. */
export const BooksByAuthor_many: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<booksByAuthorResult[]> = BookService__sqlJS.BooksByAuthor.many as any;
export const FindBookById_many: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<findBookByIdResult[]> = BookService__sqlJS.FindBookById.many as any;

/** Returns exactly 1 result. Returns `None` if more or less than exactly 1 result is returned. */
export const BooksByAuthor_one: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<(undefined | booksByAuthorResult)> = BookService__sqlJS.BooksByAuthor.one as any;
export const FindBookById_one: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<(undefined | findBookByIdResult)> = BookService__sqlJS.FindBookById.one as any;

/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
export const BooksByAuthor_expectOne: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams, errorMessage:(undefined | string)) => Promise<
{ TAG: "Ok"; _0: booksByAuthorResult }
| { TAG: "Error"; _0: string }> = BookService__sqlJS.BooksByAuthor.expectOne as any;
export const FindBookById_expectOne: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams, errorMessage:(undefined | string)) => Promise<
{ TAG: "Ok"; _0: findBookByIdResult }
| { TAG: "Error"; _0: string }> = BookService__sqlJS.FindBookById.expectOne as any;

/** Executes the query, but ignores whatever is returned by it. */
export const BooksByAuthor_execute: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<void> = BookService__sqlJS.BooksByAuthor.execute as any;
export const FindBookById_execute: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<void> = BookService__sqlJS.FindBookById.execute as any;

export const booksByAuthor: (params:booksByAuthorParams, client:PgTyped_Pg_Client_t) => Promise<booksByAuthorResult[]> = BookService__sqlJS.booksByAuthor as any;
export const findBookById: (params:findBookByIdParams, client:PgTyped_Pg_Client_t) => Promise<findBookByIdResult[]> = BookService__sqlJS.findBookById as any;

export const FindBookById: {
/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
Expand Down
Loading

0 comments on commit d0acb5c

Please sign in to comment.