-
Notifications
You must be signed in to change notification settings - Fork 0
Insert fluent API #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -40,7 +40,28 @@ export class Insert< | |||||
| R extends Types.RowLike = I, | ||||||
| V extends TableSchemaToRowLikeStrict<TS> = TableSchemaToRowLikeStrict<TS>, | ||||||
| > { | ||||||
| constructor(public clause: Parameters<typeof insert<I, TS, K, R, V>>) {} | ||||||
| public clause: Parameters<typeof insert<I, TS, K, R, V>>; | ||||||
|
|
||||||
| constructor(clause: Parameters<typeof insert<I, TS, K, R, V>>) { | ||||||
| this.clause = [...clause]; | ||||||
| if (!this.clause[1]) { | ||||||
| // insertion values are required, but we allow omitting them to make the | ||||||
| // query builder more ergonomic: | ||||||
| this.clause[1] = "defaultValues"; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| values(values: "defaultValues" | Values<V> | Select<V, any, any>) { | ||||||
| return new Insert<I, TS, K, R, V>([this.clause[0], values, this.clause[2]]); | ||||||
| } | ||||||
|
|
||||||
| onConflict(onConflict: OnConflictInput<I>) { | ||||||
| return new Insert<I, TS, K, R, V>([this.clause[0], this.clause[1], { ...this.clause[2], onConflict }]); | ||||||
| } | ||||||
|
|
||||||
| returning<R2 extends Types.RowLike = I>(fn?: (insertRow: I) => R2): Insert<I, TS, K, R2, V> { | ||||||
| return new Insert<I, TS, K, R2, V>([this.clause[0], this.clause[1], { ...this.clause[2], returning: fn }]); | ||||||
|
||||||
| return new Insert<I, TS, K, R2, V>([this.clause[0], this.clause[1], { ...this.clause[2], returning: fn }]); | |
| return new Insert<I, TS, K, R2, V>([this.clause[0], this.clause[1], { ...(this.clause[2] || {}), returning: fn }]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] When
this.clause[2]is undefined, spreading it with{ ...this.clause[2], onConflict }will result in{ onConflict }only. However, if other properties were set previously (likereturning), they will be preserved. But ifthis.clause[2]is undefined initially, this works correctly. Consider being more explicit:{ ...(this.clause[2] || {}), onConflict }to make the intent clearer and avoid potential confusion.