Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7597211

Browse files
committed
autoconvert null to options
1 parent10b268b commit7597211

File tree

14 files changed

+420
-131
lines changed

14 files changed

+420
-131
lines changed

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#main
22

33
- Autoinsert trailing commas in embedded SQL blocks.
4+
- BREAKING CHANGE:`Null.t` is no longer emitted, all`null` values are autoconverted to`option`. This gives a much more idiomatic ReScript experience.
45

56
#2.4.0
67

‎packages/cli/src/generator.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export async function queryToTypeDeclarations(
145145
(addNullability||nullable||nullable==null)&&
146146
!removeNullability
147147
){
148-
tsTypeName='Null.t<'+tsTypeName+'>';
148+
tsTypeName='option<'+tsTypeName+'>';
149149
}
150150

151151
if(addNullability||removeNullability){
@@ -176,7 +176,7 @@ export async function queryToTypeDeclarations(
176176
lettsTypeName=types.use(pgTypeName,TypeScope.Parameter);
177177

178178
if(!param.required){
179-
tsTypeName='Null.t<'+tsTypeName+'>';
179+
tsTypeName=tsTypeName;
180180
}
181181

182182
// Allow optional scalar parameters to be missing from parameters object
@@ -289,9 +289,7 @@ async function generateTypedecsFromFile(
289289
consttypeSource:TypeSource=(query)=>getTypes(query,connection);
290290

291291
const{ queries, events}=
292-
mode==='res'
293-
?parseRescriptFile(contents,fileName)
294-
:parseSQLFile(contents);
292+
mode==='res' ?parseRescriptFile(contents) :parseSQLFile(contents);
295293
if(events.length>0){
296294
prettyPrintEvents(contents,events);
297295
if(events.find((e)=>'critical'ine)){

‎packages/example/src/__snapshots__/rescript.test.js.snap‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`insert query with an inline sql comment 1`]=`
44
Object{
55
"body":"Just a comment",
6-
"book_id":null,
6+
"book_id":undefined,
77
"id":Any<Number>,
88
"user_id": 1,
99
}
@@ -51,10 +51,10 @@ exports[`select query with join and a parameter override 1`] = `
5151
Array [
5252
Object{
5353
"author_id":2,
54-
"big_int":null,
55-
"categories":null,
54+
"big_int":undefined,
55+
"categories":undefined,
5656
"id":2,
57-
"meta":null,
57+
"meta":undefined,
5858
"name":"The Dragons Of Eden",
5959
"rank":4,
6060
},
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/* TypeScript file generated from BookService__sql.res by genType. */
2+
3+
/* eslint-disable */
4+
/* tslint:disable */
5+
6+
constBookService__sqlJS=require('./BookService__sql.js');
7+
8+
importtype{Pg_Client_tasPgTyped_Pg_Client_t}from'pgtyped-rescript/src/res/PgTyped.gen';
9+
10+
importtype{tasJSON_t}from'./JSON.gen';
11+
12+
exporttypecategory="novel"|"science-fiction"|"thriller";
13+
14+
exporttypearrayJSON_t=JSON_t[];
15+
16+
exporttypecategoryArray=category[];
17+
18+
/** 'FindBookById' parameters type */
19+
exporttypefindBookByIdParams={readonlyid?:number};
20+
21+
/** 'FindBookById' return type */
22+
exporttypefindBookByIdResult={
23+
readonlyauthor_id:(undefined|number);
24+
readonlybig_int:(undefined|bigint);
25+
readonlycategories:(undefined|categoryArray);
26+
readonlyid:number;
27+
readonlymeta:(undefined|arrayJSON_t);
28+
readonlyname:(undefined|string);
29+
readonlyrank:(undefined|number)
30+
};
31+
32+
/** 'FindBookById' query type */
33+
exporttypefindBookByIdQuery={readonlyparams:findBookByIdParams;readonlyresult:findBookByIdResult};
34+
35+
/** 'BooksByAuthor' parameters type */
36+
exporttypebooksByAuthorParams={readonlyauthorName:string};
37+
38+
/** 'BooksByAuthor' return type */
39+
exporttypebooksByAuthorResult={
40+
readonlyauthor_id:(undefined|number);
41+
readonlybig_int:(undefined|bigint);
42+
readonlycategories:(undefined|categoryArray);
43+
readonlyid:number;
44+
readonlymeta:(undefined|arrayJSON_t);
45+
readonlyname:(undefined|string);
46+
readonlyrank:(undefined|number)
47+
};
48+
49+
/** 'BooksByAuthor' query type */
50+
exporttypebooksByAuthorQuery={readonlyparams:booksByAuthorParams;readonlyresult:booksByAuthorResult};
51+
52+
/** Returns an array of all matched results. */
53+
exportconstFindBookById_many:(_1:PgTyped_Pg_Client_t,_2:findBookByIdParams)=>Promise<findBookByIdResult[]>=BookService__sqlJS.FindBookById.manyasany;
54+
55+
/** Returns exactly 1 result. Returns `None` if more or less than exactly 1 result is returned. */
56+
exportconstFindBookById_one:(_1:PgTyped_Pg_Client_t,_2:findBookByIdParams)=>Promise<(undefined|findBookByIdResult)>=BookService__sqlJS.FindBookById.oneasany;
57+
58+
/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
59+
exportconstFindBookById_expectOne:(_1:PgTyped_Pg_Client_t,_2:findBookByIdParams,errorMessage:(undefined|string))=>Promise<
60+
{TAG:"Ok";_0:findBookByIdResult}
61+
|{TAG:"Error";_0:string}>=BookService__sqlJS.FindBookById.expectOneasany;
62+
63+
/** Executes the query, but ignores whatever is returned by it. */
64+
exportconstFindBookById_execute:(_1:PgTyped_Pg_Client_t,_2:findBookByIdParams)=>Promise<void>=BookService__sqlJS.FindBookById.executeasany;
65+
66+
exportconstfindBookById:(params:findBookByIdParams,client:PgTyped_Pg_Client_t)=>Promise<findBookByIdResult[]>=BookService__sqlJS.findBookByIdasany;
67+
68+
/** Returns an array of all matched results. */
69+
exportconstBooksByAuthor_many:(_1:PgTyped_Pg_Client_t,_2:booksByAuthorParams)=>Promise<booksByAuthorResult[]>=BookService__sqlJS.BooksByAuthor.manyasany;
70+
71+
/** Returns exactly 1 result. Returns `None` if more or less than exactly 1 result is returned. */
72+
exportconstBooksByAuthor_one:(_1:PgTyped_Pg_Client_t,_2:booksByAuthorParams)=>Promise<(undefined|booksByAuthorResult)>=BookService__sqlJS.BooksByAuthor.oneasany;
73+
74+
/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
75+
exportconstBooksByAuthor_expectOne:(_1:PgTyped_Pg_Client_t,_2:booksByAuthorParams,errorMessage:(undefined|string))=>Promise<
76+
{TAG:"Ok";_0:booksByAuthorResult}
77+
|{TAG:"Error";_0:string}>=BookService__sqlJS.BooksByAuthor.expectOneasany;
78+
79+
/** Executes the query, but ignores whatever is returned by it. */
80+
exportconstBooksByAuthor_execute:(_1:PgTyped_Pg_Client_t,_2:booksByAuthorParams)=>Promise<void>=BookService__sqlJS.BooksByAuthor.executeasany;
81+
82+
exportconstbooksByAuthor:(params:booksByAuthorParams,client:PgTyped_Pg_Client_t)=>Promise<booksByAuthorResult[]>=BookService__sqlJS.booksByAuthorasany;
83+
84+
exportconstFindBookById:{
85+
/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
86+
expectOne:(_1:PgTyped_Pg_Client_t,_2:findBookByIdParams,errorMessage:(undefined|string))=>Promise<
87+
{
88+
TAG:"Ok";
89+
_0:findBookByIdResult
90+
}
91+
|{
92+
TAG:"Error";
93+
_0:string
94+
}>;
95+
/** Returns exactly 1 result. Returns `None` if more or less than exactly 1 result is returned. */
96+
one:(_1:PgTyped_Pg_Client_t,_2:findBookByIdParams)=>Promise<(undefined|findBookByIdResult)>;
97+
/** Returns an array of all matched results. */
98+
many:(_1:PgTyped_Pg_Client_t,_2:findBookByIdParams)=>Promise<findBookByIdResult[]>;
99+
/** Executes the query, but ignores whatever is returned by it. */
100+
execute:(_1:PgTyped_Pg_Client_t,_2:findBookByIdParams)=>Promise<void>
101+
}=BookService__sqlJS.FindBookByIdasany;
102+
103+
exportconstBooksByAuthor:{
104+
/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
105+
expectOne:(_1:PgTyped_Pg_Client_t,_2:booksByAuthorParams,errorMessage:(undefined|string))=>Promise<
106+
{
107+
TAG:"Ok";
108+
_0:booksByAuthorResult
109+
}
110+
|{
111+
TAG:"Error";
112+
_0:string
113+
}>;
114+
/** Returns exactly 1 result. Returns `None` if more or less than exactly 1 result is returned. */
115+
one:(_1:PgTyped_Pg_Client_t,_2:booksByAuthorParams)=>Promise<(undefined|booksByAuthorResult)>;
116+
/** Returns an array of all matched results. */
117+
many:(_1:PgTyped_Pg_Client_t,_2:booksByAuthorParams)=>Promise<booksByAuthorResult[]>;
118+
/** Executes the query, but ignores whatever is returned by it. */
119+
execute:(_1:PgTyped_Pg_Client_t,_2:booksByAuthorParams)=>Promise<void>
120+
}=BookService__sqlJS.BooksByAuthorasany;

‎packages/example/src/books/BookService__sql.res‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ type categoryArray = array<category>
1414
/** 'FindBookById' parameters type */
1515
@gentype
1616
typefindBookByIdParams= {
17-
id?:Null.t<int>,
17+
id?:int,
1818
}
1919

2020
/** 'FindBookById' return type */
2121
@gentype
2222
typefindBookByIdResult= {
23-
author_id:Null.t<int>,
24-
big_int:Null.t<bigint>,
25-
categories:Null.t<categoryArray>,
23+
author_id:option<int>,
24+
big_int:option<bigint>,
25+
categories:option<categoryArray>,
2626
id:int,
27-
meta:Null.t<arrayJSON_t>,
28-
name:Null.t<string>,
29-
rank:Null.t<int>,
27+
meta:option<arrayJSON_t>,
28+
name:option<string>,
29+
rank:option<int>,
3030
}
3131

3232
/** 'FindBookById' query type */
@@ -104,13 +104,13 @@ type booksByAuthorParams = {
104104
/** 'BooksByAuthor' return type */
105105
@gentype
106106
typebooksByAuthorResult= {
107-
author_id:Null.t<int>,
108-
big_int:Null.t<bigint>,
109-
categories:Null.t<categoryArray>,
107+
author_id:option<int>,
108+
big_int:option<bigint>,
109+
categories:option<categoryArray>,
110110
id:int,
111-
meta:Null.t<arrayJSON_t>,
112-
name:Null.t<string>,
113-
rank:Null.t<int>,
111+
meta:option<arrayJSON_t>,
112+
name:option<string>,
113+
rank:option<int>,
114114
}
115115

116116
/** 'BooksByAuthor' query type */

‎packages/example/src/books/books.sql‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ SELECT * FROM books WHERE name = 'שקל';
1919
INSERT INTO books (rank, name, author_id, categories)
2020
VALUES :books RETURNING idas book_id;
2121

22+
/*
23+
@name InsertBook
24+
*/
25+
INSERT INTO books (rank, name, author_id, categories)
26+
VALUES (:rank!, :name!, :author_id!, :categories) RETURNING idas book_id;
27+
2228
/*
2329
@name UpdateBooksCustom
2430
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp