Skip to content

Commit

Permalink
support partial objects in sql helpers (#3460)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart authored Aug 15, 2024
1 parent a2aea87 commit 4fabf75
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-hounds-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/sql": patch
---

support partial objects in sql helpers
17 changes: 10 additions & 7 deletions packages/sql/src/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ export interface ArrayHelper {
*/
export interface RecordInsertHelper {
readonly _tag: "RecordInsertHelper"
readonly value: ReadonlyArray<Record<string, Primitive | Fragment>>
readonly value: ReadonlyArray<Record<string, Primitive | Fragment | undefined>>
/** @internal */
readonly returningIdentifier: string | Fragment | undefined
readonly returning: (sql: string | Identifier | Fragment) => RecordInsertHelper
}
Expand All @@ -188,8 +189,9 @@ export interface RecordInsertHelper {
*/
export interface RecordUpdateHelper {
readonly _tag: "RecordUpdateHelper"
readonly value: ReadonlyArray<Record<string, Primitive | Fragment>>
readonly value: ReadonlyArray<Record<string, Primitive | Fragment | undefined>>
readonly alias: string
/** @internal */
readonly returningIdentifier: string | Fragment | undefined
readonly returning: (sql: string | Identifier | Fragment) => RecordUpdateHelper
}
Expand All @@ -200,8 +202,9 @@ export interface RecordUpdateHelper {
*/
export interface RecordUpdateHelperSingle {
readonly _tag: "RecordUpdateHelperSingle"
readonly value: Record<string, Primitive | Fragment>
readonly value: Record<string, Primitive | Fragment | undefined>
readonly omit: ReadonlyArray<string>
/** @internal */
readonly returningIdentifier: string | Fragment | undefined
readonly returning: (sql: string | Identifier | Fragment) => RecordUpdateHelperSingle
}
Expand Down Expand Up @@ -306,13 +309,13 @@ export interface Constructor {

readonly insert: {
(
value: ReadonlyArray<Record<string, Primitive | Fragment>>
value: ReadonlyArray<Record<string, Primitive | Fragment | undefined>>
): RecordInsertHelper
(value: Record<string, Primitive | Fragment>): RecordInsertHelper
(value: Record<string, Primitive | Fragment | undefined>): RecordInsertHelper
}

/** Update a single row */
readonly update: <A extends Record<string, Primitive | Fragment>>(
readonly update: <A extends Record<string, Primitive | Fragment | undefined>>(
value: A,
omit?: ReadonlyArray<keyof A>
) => RecordUpdateHelperSingle
Expand All @@ -323,7 +326,7 @@ export interface Constructor {
* **Note:** Not supported in sqlite
*/
readonly updateValues: (
value: ReadonlyArray<Record<string, Primitive | Fragment>>,
value: ReadonlyArray<Record<string, Primitive | Fragment | undefined>>,
alias: string
) => RecordUpdateHelper

Expand Down
6 changes: 4 additions & 2 deletions packages/sql/src/internal/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ export const primitiveKind = (value: Statement.Primitive): Statement.PrimitiveKi
}

const extractPrimitive = (
value: Statement.Primitive | Statement.Fragment,
value: Statement.Primitive | Statement.Fragment | undefined,
onCustom: (
type: Statement.Custom<string, unknown, unknown>,
placeholder: () => string,
Expand All @@ -857,7 +857,9 @@ const extractPrimitive = (
placeholder: () => string,
withoutTransform: boolean
): Statement.Primitive => {
if (isFragment(value)) {
if (value === undefined) {
return null
} else if (isFragment(value)) {
const head = value.segments[0]
if (head._tag === "Custom") {
const compiled = onCustom(head, placeholder, withoutTransform)
Expand Down

0 comments on commit 4fabf75

Please sign in to comment.