-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from G4brym/add-upsert
Add upsert support
- Loading branch information
Showing
16 changed files
with
472 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
The Upsert feature in the SQL Builder Library streamlines database operations by combining insert and update actions | ||
into a single operation. It automatically determines whether a record exists based on a specified key and updates or | ||
inserts data accordingly. This simplifies coding, enhances data integrity, and boosts performance. | ||
|
||
## Simple Upsert | ||
|
||
`new Raw(...)` is used here to let `workers-qb` know that it is not a parameter. | ||
|
||
```ts | ||
const qb = new D1QB(env.DB) | ||
|
||
const upserted = await qb | ||
.insert({ | ||
tableName: 'phonebook2', | ||
data: { | ||
name: 'Alice', | ||
phonenumber: '704-555-1212', | ||
validDate: '2018-05-08', | ||
}, | ||
onConflict: { | ||
column: 'name', | ||
data: { | ||
phonenumber: new Raw('excluded.phonenumber'), | ||
validDate: new Raw('excluded.validDate'), | ||
}, | ||
}, | ||
}) | ||
.execute() | ||
``` | ||
|
||
This will generate this query | ||
|
||
```sql | ||
INSERT INTO phonebook2 (name, phonenumber, validDate) | ||
VALUES (?1, ?2, ?3) | ||
ON CONFLICT (name) DO UPDATE SET phonenumber = excluded.phonenumber, | ||
validDate = excluded.validDate | ||
``` | ||
|
||
## Upsert with where | ||
|
||
```ts | ||
const qb = new D1QB(env.DB) | ||
|
||
const upserted = await qb | ||
.insert({ | ||
tableName: 'phonebook2', | ||
data: { | ||
name: 'Alice', | ||
phonenumber: '704-555-1212', | ||
validDate: '2018-05-08', | ||
}, | ||
onConflict: { | ||
column: 'name', | ||
data: { | ||
phonenumber: new Raw('excluded.phonenumber'), | ||
validDate: new Raw('excluded.validDate'), | ||
}, | ||
where: { | ||
conditions: 'excluded.validDate > phonebook2.validDate', | ||
}, | ||
}, | ||
}) | ||
.execute() | ||
``` | ||
|
||
This will generate this query | ||
|
||
```sql | ||
INSERT INTO phonebook2 (name, phonenumber, validDate) | ||
VALUES (?1, ?2, ?3) | ||
ON CONFLICT (name) DO UPDATE SET phonenumber = excluded.phonenumber, | ||
validDate = excluded.validDate | ||
WHERE excluded.validDate > phonebook2.validDate | ||
``` | ||
|
||
## Upsert with multiple columns | ||
|
||
```ts | ||
const qb = new D1QB(env.DB) | ||
|
||
const upserted = await qb | ||
.insert({ | ||
tableName: 'phonebook2', | ||
data: { | ||
name: 'Alice', | ||
phonenumber: '704-555-1212', | ||
validDate: '2018-05-08', | ||
}, | ||
onConflict: { | ||
column: ['name', 'phonenumber'], | ||
data: { | ||
validDate: new Raw('excluded.validDate'), | ||
}, | ||
where: { | ||
conditions: 'excluded.validDate > phonebook2.validDate', | ||
}, | ||
}, | ||
}) | ||
.execute() | ||
``` | ||
|
||
This will generate this query | ||
|
||
```sql | ||
INSERT INTO phonebook2 (name, phonenumber, validDate) | ||
VALUES (?1, ?2, ?3) | ||
ON CONFLICT (name, phonenumber) DO UPDATE SET validDate = excluded.validDate | ||
WHERE excluded.validDate > phonebook2.validDate | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,6 @@ | |
}, | ||
"dependencies": { | ||
"pg": "^8.11.0", | ||
"workers-qb": "^1.0.0" | ||
"workers-qb": "^1.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.