Skip to content

Commit

Permalink
change expectOne to raise if not exactly one item is encountered
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed May 2, 2024
1 parent 9aef10a commit 351f08f
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 104 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- 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.
- Change `expectOne` to panic if not finding a single item.

# 2.4.0

Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,13 @@ module ${
typeDec.query.name
}Params) => promise<option<${typeDec.query.name}Result>>
/** Returns exactly 1 result. Returns \`Error\` (with an optionally provided \`errorMessage\`) if more or less than exactly 1 result is returned. */
/** Returns exactly 1 result. Raises \`Exn.t\` (with an optionally provided \`errorMessage\`) if more or less than exactly 1 result is returned. */
@gentype
let expectOne: (
PgTyped.Pg.Client.t,
${typeDec.query.name}Params,
~errorMessage: string=?
) => promise<result<${typeDec.query.name}Result, string>>
) => promise<${typeDec.query.name}Result>
/** Executes the query, but ignores whatever is returned by it. */
@gentype
Expand All @@ -430,8 +430,8 @@ module ${
@gentype
let expectOne = async (client, params, ~errorMessage=?) => switch await query(params, ~client) {
| [item] => Ok(item)
| _ => Error(errorMessage->Option.getOr("More or less than one item was returned"))
| [item] => item
| _ => panic(errorMessage->Option.getOr("More or less than one item was returned"))
}
@gentype
Expand Down
32 changes: 16 additions & 16 deletions packages/example/src/books/BookService__sql.res
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ module FindBookById: {
@gentype
let one: (PgTyped.Pg.Client.t, findBookByIdParams) => promise<option<findBookByIdResult>>

/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
/** Returns exactly 1 result. Raises `Exn.t` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
@gentype
let expectOne: (
PgTyped.Pg.Client.t,
findBookByIdParams,
~errorMessage: string=?
) => promise<result<findBookByIdResult, string>>
) => promise<findBookByIdResult>

/** Executes the query, but ignores whatever is returned by it. */
@gentype
Expand All @@ -84,8 +84,8 @@ module FindBookById: {

@gentype
let expectOne = async (client, params, ~errorMessage=?) => switch await query(params, ~client) {
| [item] => Ok(item)
| _ => Error(errorMessage->Option.getOr("More or less than one item was returned"))
| [item] => item
| _ => panic(errorMessage->Option.getOr("More or less than one item was returned"))
}

@gentype
Expand Down Expand Up @@ -144,13 +144,13 @@ module Query1: {
@gentype
let one: (PgTyped.Pg.Client.t, query1Params) => promise<option<query1Result>>

/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
/** Returns exactly 1 result. Raises `Exn.t` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
@gentype
let expectOne: (
PgTyped.Pg.Client.t,
query1Params,
~errorMessage: string=?
) => promise<result<query1Result, string>>
) => promise<query1Result>

/** Executes the query, but ignores whatever is returned by it. */
@gentype
Expand All @@ -171,8 +171,8 @@ module Query1: {

@gentype
let expectOne = async (client, params, ~errorMessage=?) => switch await query(params, ~client) {
| [item] => Ok(item)
| _ => Error(errorMessage->Option.getOr("More or less than one item was returned"))
| [item] => item
| _ => panic(errorMessage->Option.getOr("More or less than one item was returned"))
}

@gentype
Expand Down Expand Up @@ -227,13 +227,13 @@ module Query2: {
@gentype
let one: (PgTyped.Pg.Client.t, query2Params) => promise<option<query2Result>>

/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
/** Returns exactly 1 result. Raises `Exn.t` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
@gentype
let expectOne: (
PgTyped.Pg.Client.t,
query2Params,
~errorMessage: string=?
) => promise<result<query2Result, string>>
) => promise<query2Result>

/** Executes the query, but ignores whatever is returned by it. */
@gentype
Expand All @@ -254,8 +254,8 @@ module Query2: {

@gentype
let expectOne = async (client, params, ~errorMessage=?) => switch await query(params, ~client) {
| [item] => Ok(item)
| _ => Error(errorMessage->Option.getOr("More or less than one item was returned"))
| [item] => item
| _ => panic(errorMessage->Option.getOr("More or less than one item was returned"))
}

@gentype
Expand Down Expand Up @@ -310,13 +310,13 @@ module Query3: {
@gentype
let one: (PgTyped.Pg.Client.t, query3Params) => promise<option<query3Result>>

/** Returns exactly 1 result. Returns `Error` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
/** Returns exactly 1 result. Raises `Exn.t` (with an optionally provided `errorMessage`) if more or less than exactly 1 result is returned. */
@gentype
let expectOne: (
PgTyped.Pg.Client.t,
query3Params,
~errorMessage: string=?
) => promise<result<query3Result, string>>
) => promise<query3Result>

/** Executes the query, but ignores whatever is returned by it. */
@gentype
Expand All @@ -337,8 +337,8 @@ module Query3: {

@gentype
let expectOne = async (client, params, ~errorMessage=?) => switch await query(params, ~client) {
| [item] => Ok(item)
| _ => Error(errorMessage->Option.getOr("More or less than one item was returned"))
| [item] => item
| _ => panic(errorMessage->Option.getOr("More or less than one item was returned"))
}

@gentype
Expand Down
Loading

0 comments on commit 351f08f

Please sign in to comment.