Skip to content

Commit

Permalink
autoconvert null to options
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed May 1, 2024
1 parent 10b268b commit 7597211
Show file tree
Hide file tree
Showing 14 changed files with 420 additions and 131 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# main

- 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.

# 2.4.0

Expand Down
8 changes: 3 additions & 5 deletions packages/cli/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export async function queryToTypeDeclarations(
(addNullability || nullable || nullable == null) &&
!removeNullability
) {
tsTypeName = 'Null.t<' + tsTypeName + '>';
tsTypeName = 'option<' + tsTypeName + '>';
}

if (addNullability || removeNullability) {
Expand Down Expand Up @@ -176,7 +176,7 @@ export async function queryToTypeDeclarations(
let tsTypeName = types.use(pgTypeName, TypeScope.Parameter);

if (!param.required) {
tsTypeName = 'Null.t<' + tsTypeName + '>';
tsTypeName = tsTypeName;
}

// Allow optional scalar parameters to be missing from parameters object
Expand Down Expand Up @@ -289,9 +289,7 @@ async function generateTypedecsFromFile(
const typeSource: TypeSource = (query) => getTypes(query, connection);

const { queries, events } =
mode === 'res'
? parseRescriptFile(contents, fileName)
: parseSQLFile(contents);
mode === 'res' ? parseRescriptFile(contents) : parseSQLFile(contents);
if (events.length > 0) {
prettyPrintEvents(contents, events);
if (events.find((e) => 'critical' in e)) {
Expand Down
8 changes: 4 additions & 4 deletions packages/example/src/__snapshots__/rescript.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`insert query with an inline sql comment 1`] = `
Object {
"body": "Just a comment",
"book_id": null,
"book_id": undefined,
"id": Any<Number>,
"user_id": 1,
}
Expand Down Expand Up @@ -51,10 +51,10 @@ exports[`select query with join and a parameter override 1`] = `
Array [
Object {
"author_id": 2,
"big_int": null,
"categories": null,
"big_int": undefined,
"categories": undefined,
"id": 2,
"meta": null,
"meta": undefined,
"name": "The Dragons Of Eden",
"rank": 4,
},
Expand Down
120 changes: 120 additions & 0 deletions packages/example/src/books/BookService__sql.gen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/* TypeScript file generated from BookService__sql.res by genType. */

/* eslint-disable */
/* tslint:disable */

const BookService__sqlJS = require('./BookService__sql.js');

import type {Pg_Client_t as PgTyped_Pg_Client_t} from 'pgtyped-rescript/src/res/PgTyped.gen';

import type {t as JSON_t} from './JSON.gen';

export type category = "novel" | "science-fiction" | "thriller";

export type arrayJSON_t = JSON_t[];

export type categoryArray = category[];

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

/** 'FindBookById' return type */
export type findBookByIdResult = {
readonly author_id: (undefined | number);
readonly big_int: (undefined | bigint);
readonly categories: (undefined | categoryArray);
readonly id: number;
readonly meta: (undefined | arrayJSON_t);
readonly name: (undefined | string);
readonly rank: (undefined | number)
};

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

/** 'BooksByAuthor' parameters type */
export type booksByAuthorParams = { readonly authorName: string };

/** 'BooksByAuthor' return type */
export type booksByAuthorResult = {
readonly author_id: (undefined | number);
readonly big_int: (undefined | bigint);
readonly categories: (undefined | categoryArray);
readonly id: number;
readonly meta: (undefined | arrayJSON_t);
readonly name: (undefined | string);
readonly rank: (undefined | number)
};

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

/** 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;

/** 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;

/** 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;

/** 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 findBookById: (params:findBookByIdParams, client:PgTyped_Pg_Client_t) => Promise<findBookByIdResult[]> = BookService__sqlJS.findBookById 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;

/** 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;

/** 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;

/** 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 booksByAuthor: (params:booksByAuthorParams, client:PgTyped_Pg_Client_t) => Promise<booksByAuthorResult[]> = BookService__sqlJS.booksByAuthor 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. */
expectOne: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams, errorMessage:(undefined | string)) => Promise<
{
TAG: "Ok";
_0: findBookByIdResult
}
| {
TAG: "Error";
_0: string
}>;
/** Returns exactly 1 result. Returns `None` if more or less than exactly 1 result is returned. */
one: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<(undefined | findBookByIdResult)>;
/** Returns an array of all matched results. */
many: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<findBookByIdResult[]>;
/** Executes the query, but ignores whatever is returned by it. */
execute: (_1:PgTyped_Pg_Client_t, _2:findBookByIdParams) => Promise<void>
} = BookService__sqlJS.FindBookById as any;

export const BooksByAuthor: {
/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
expectOne: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams, errorMessage:(undefined | string)) => Promise<
{
TAG: "Ok";
_0: booksByAuthorResult
}
| {
TAG: "Error";
_0: string
}>;
/** Returns exactly 1 result. Returns `None` if more or less than exactly 1 result is returned. */
one: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<(undefined | booksByAuthorResult)>;
/** Returns an array of all matched results. */
many: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<booksByAuthorResult[]>;
/** Executes the query, but ignores whatever is returned by it. */
execute: (_1:PgTyped_Pg_Client_t, _2:booksByAuthorParams) => Promise<void>
} = BookService__sqlJS.BooksByAuthor as any;
26 changes: 13 additions & 13 deletions packages/example/src/books/BookService__sql.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ type categoryArray = array<category>
/** 'FindBookById' parameters type */
@gentype
type findBookByIdParams = {
id?: Null.t<int>,
id?: int,
}

/** 'FindBookById' return type */
@gentype
type findBookByIdResult = {
author_id: Null.t<int>,
big_int: Null.t<bigint>,
categories: Null.t<categoryArray>,
author_id: option<int>,
big_int: option<bigint>,
categories: option<categoryArray>,
id: int,
meta: Null.t<arrayJSON_t>,
name: Null.t<string>,
rank: Null.t<int>,
meta: option<arrayJSON_t>,
name: option<string>,
rank: option<int>,
}

/** 'FindBookById' query type */
Expand Down Expand Up @@ -104,13 +104,13 @@ type booksByAuthorParams = {
/** 'BooksByAuthor' return type */
@gentype
type booksByAuthorResult = {
author_id: Null.t<int>,
big_int: Null.t<bigint>,
categories: Null.t<categoryArray>,
author_id: option<int>,
big_int: option<bigint>,
categories: option<categoryArray>,
id: int,
meta: Null.t<arrayJSON_t>,
name: Null.t<string>,
rank: Null.t<int>,
meta: option<arrayJSON_t>,
name: option<string>,
rank: option<int>,
}

/** 'BooksByAuthor' query type */
Expand Down
6 changes: 6 additions & 0 deletions packages/example/src/books/books.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ SELECT * FROM books WHERE name = 'שקל';
INSERT INTO books (rank, name, author_id, categories)
VALUES :books RETURNING id as book_id;

/*
@name InsertBook
*/
INSERT INTO books (rank, name, author_id, categories)
VALUES (:rank!, :name!, :author_id!, :categories) RETURNING id as book_id;

/*
@name UpdateBooksCustom
*/
Expand Down
Loading

0 comments on commit 7597211

Please sign in to comment.