pqb 0.28.0 → 0.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +83 -99
- package/dist/index.js +134 -131
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +134 -131
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -3363,13 +3363,13 @@ const pushInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
3363
3363
|
}
|
|
3364
3364
|
if (query.onConflict) {
|
|
3365
3365
|
ctx.sql.push("ON CONFLICT");
|
|
3366
|
-
const {
|
|
3367
|
-
if (
|
|
3368
|
-
if (typeof
|
|
3369
|
-
ctx.sql.push(`("${((_b = shape[
|
|
3370
|
-
} else if (Array.isArray(
|
|
3366
|
+
const { target } = query.onConflict;
|
|
3367
|
+
if (target) {
|
|
3368
|
+
if (typeof target === "string") {
|
|
3369
|
+
ctx.sql.push(`("${((_b = shape[target]) == null ? void 0 : _b.data.name) || target}")`);
|
|
3370
|
+
} else if (Array.isArray(target)) {
|
|
3371
3371
|
ctx.sql.push(
|
|
3372
|
-
`(${
|
|
3372
|
+
`(${target.reduce(
|
|
3373
3373
|
(sql, item, i) => {
|
|
3374
3374
|
var _a2;
|
|
3375
3375
|
return sql + (i ? ", " : "") + `"${((_a2 = shape[item]) == null ? void 0 : _a2.data.name) || item}"`;
|
|
@@ -3377,50 +3377,67 @@ const pushInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
3377
3377
|
""
|
|
3378
3378
|
)})`
|
|
3379
3379
|
);
|
|
3380
|
-
} else if ("toSQL" in
|
|
3381
|
-
ctx.sql.push(
|
|
3380
|
+
} else if ("toSQL" in target) {
|
|
3381
|
+
ctx.sql.push(target.toSQL(ctx, quotedAs));
|
|
3382
3382
|
} else {
|
|
3383
|
-
ctx.sql.push(`ON CONSTRAINT "${
|
|
3383
|
+
ctx.sql.push(`ON CONSTRAINT "${target.constraint}"`);
|
|
3384
3384
|
}
|
|
3385
3385
|
}
|
|
3386
|
-
if (
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3386
|
+
if ("merge" in query.onConflict) {
|
|
3387
|
+
let sql;
|
|
3388
|
+
const { merge } = query.onConflict;
|
|
3389
|
+
if (merge) {
|
|
3390
|
+
if (typeof merge === "string") {
|
|
3391
|
+
const name = ((_c = shape[merge]) == null ? void 0 : _c.data.name) || merge;
|
|
3392
|
+
sql = `"${name}" = excluded."${name}"`;
|
|
3393
|
+
} else if ("except" in merge) {
|
|
3394
|
+
const notExcluded = [];
|
|
3395
|
+
const except = toArray(merge.except);
|
|
3396
|
+
for (let i = 0; i < columns.length; i++) {
|
|
3397
|
+
if (!except.includes(columns[i])) {
|
|
3398
|
+
notExcluded.push(quotedColumns[i]);
|
|
3399
|
+
}
|
|
3400
|
+
}
|
|
3401
|
+
sql = mergeColumnsSql(notExcluded);
|
|
3402
|
+
} else {
|
|
3403
|
+
sql = merge.reduce((sql2, item, i) => {
|
|
3397
3404
|
var _a2;
|
|
3398
3405
|
const name = ((_a2 = shape[item]) == null ? void 0 : _a2.data.name) || item;
|
|
3399
|
-
return
|
|
3406
|
+
return sql2 + (i ? ", " : "") + `"${name}" = excluded."${name}"`;
|
|
3400
3407
|
}, "");
|
|
3401
|
-
} else if (isExpression(update)) {
|
|
3402
|
-
set = update.toSQL(ctx, quotedAs);
|
|
3403
|
-
} else {
|
|
3404
|
-
const arr = [];
|
|
3405
|
-
for (const key in update) {
|
|
3406
|
-
arr.push(
|
|
3407
|
-
`"${((_d = shape[key]) == null ? void 0 : _d.data.name) || key}" = ${addValue(
|
|
3408
|
-
ctx.values,
|
|
3409
|
-
update[key]
|
|
3410
|
-
)}`
|
|
3411
|
-
);
|
|
3412
|
-
}
|
|
3413
|
-
set = arr.join(", ");
|
|
3414
3408
|
}
|
|
3415
3409
|
} else {
|
|
3416
|
-
|
|
3410
|
+
sql = mergeColumnsSql(quotedColumns);
|
|
3411
|
+
}
|
|
3412
|
+
ctx.sql.push("DO UPDATE SET", sql);
|
|
3413
|
+
} else if (query.onConflict.set) {
|
|
3414
|
+
let sql;
|
|
3415
|
+
const { set } = query.onConflict;
|
|
3416
|
+
if (isExpression(set)) {
|
|
3417
|
+
sql = set.toSQL(ctx, quotedAs);
|
|
3418
|
+
} else {
|
|
3419
|
+
const arr = [];
|
|
3420
|
+
for (const key in set) {
|
|
3421
|
+
arr.push(
|
|
3422
|
+
`"${((_d = shape[key]) == null ? void 0 : _d.data.name) || key}" = ${addValue(
|
|
3423
|
+
ctx.values,
|
|
3424
|
+
set[key]
|
|
3425
|
+
)}`
|
|
3426
|
+
);
|
|
3427
|
+
}
|
|
3428
|
+
sql = arr.join(", ");
|
|
3417
3429
|
}
|
|
3418
|
-
ctx.sql.push("DO UPDATE SET",
|
|
3430
|
+
ctx.sql.push("DO UPDATE SET", sql);
|
|
3431
|
+
} else {
|
|
3432
|
+
ctx.sql.push("DO NOTHING");
|
|
3419
3433
|
}
|
|
3420
3434
|
}
|
|
3421
3435
|
pushWhereStatementSql(ctx, q, query, quotedAs);
|
|
3422
3436
|
return pushReturningSql(ctx, q, query, quotedAs, query.afterCreateSelect);
|
|
3423
3437
|
};
|
|
3438
|
+
const mergeColumnsSql = (quotedColumns2) => {
|
|
3439
|
+
return quotedColumns2.map((column) => `${column} = excluded.${column}`).join(", ");
|
|
3440
|
+
};
|
|
3424
3441
|
const encodeRow = (ctx, q, QueryClass, row, runtimeDefaults, quotedAs) => {
|
|
3425
3442
|
const arr = row.map((value) => {
|
|
3426
3443
|
if (typeof value === "function") {
|
|
@@ -6237,9 +6254,9 @@ class Create {
|
|
|
6237
6254
|
* password: '1234',
|
|
6238
6255
|
* });
|
|
6239
6256
|
*
|
|
6240
|
-
* // When using `.
|
|
6257
|
+
* // When using `.onConflictDoNothing()`,
|
|
6241
6258
|
* // the record may be not created and the `createdCount` will be 0.
|
|
6242
|
-
* const createdCount = await db.table.insert(data).
|
|
6259
|
+
* const createdCount = await db.table.insert(data).onConflictDoNothing();
|
|
6243
6260
|
*
|
|
6244
6261
|
* await db.table.create({
|
|
6245
6262
|
* // raw SQL
|
|
@@ -6432,7 +6449,7 @@ class Create {
|
|
|
6432
6449
|
*
|
|
6433
6450
|
* Columns provided in `defaults` are marked as optional in the following `create`.
|
|
6434
6451
|
*
|
|
6435
|
-
* Default data is the same as in
|
|
6452
|
+
* Default data is the same as in {@link create} and {@link createMany},
|
|
6436
6453
|
* so you can provide a raw SQL, or a query with a query.
|
|
6437
6454
|
*
|
|
6438
6455
|
* ```ts
|
|
@@ -6460,8 +6477,9 @@ class Create {
|
|
|
6460
6477
|
* or a composite primary key unique index on a set of columns,
|
|
6461
6478
|
* and a row being created has the same value as a row that already exists in the table in this column(s).
|
|
6462
6479
|
*
|
|
6463
|
-
* Use
|
|
6464
|
-
* or `
|
|
6480
|
+
* Use {@link onConflictDoNothing} to suppress the error and continue without updating the record,
|
|
6481
|
+
* or the `merge` to update the record with new values automatically,
|
|
6482
|
+
* or the `set` to specify own values for the update.
|
|
6465
6483
|
*
|
|
6466
6484
|
* `onConflict` only accepts column names that are defined in `primaryKey` or `unique` in the table definition.
|
|
6467
6485
|
* To specify a constraint, its name also must be explicitly set in `primaryKey` or `unique` in the table code.
|
|
@@ -6470,11 +6488,11 @@ class Create {
|
|
|
6470
6488
|
* for updating the record.
|
|
6471
6489
|
*
|
|
6472
6490
|
* If your table has multiple potential reasons for unique constraint violation, such as username and email columns in a user table,
|
|
6473
|
-
* consider using
|
|
6491
|
+
* consider using `upsert` instead.
|
|
6474
6492
|
*
|
|
6475
6493
|
* ```ts
|
|
6476
6494
|
* // leave `onConflict` without argument to ignore or merge on any conflict
|
|
6477
|
-
* db.table.create(data).
|
|
6495
|
+
* db.table.create(data).onConflictDoNothing();
|
|
6478
6496
|
*
|
|
6479
6497
|
* // single column:
|
|
6480
6498
|
* db.table.create(data).onConfict('email').merge();
|
|
@@ -6507,13 +6525,28 @@ class Create {
|
|
|
6507
6525
|
* .ignore();
|
|
6508
6526
|
* ```
|
|
6509
6527
|
*
|
|
6528
|
+
* For `merge` and `set`, you can append `where` to update data only for the matching rows:
|
|
6529
|
+
*
|
|
6530
|
+
* ```ts
|
|
6531
|
+
* const timestamp = Date.now();
|
|
6532
|
+
*
|
|
6533
|
+
* db.table
|
|
6534
|
+
* .create(data)
|
|
6535
|
+
* .onConflict('email')
|
|
6536
|
+
* .set({
|
|
6537
|
+
* name: 'John Doe',
|
|
6538
|
+
* updatedAt: timestamp,
|
|
6539
|
+
* })
|
|
6540
|
+
* .where({ updatedAt: { lt: timestamp } });
|
|
6541
|
+
* ```
|
|
6542
|
+
*
|
|
6510
6543
|
* @param arg - optionally provide an array of columns
|
|
6511
6544
|
*/
|
|
6512
6545
|
onConflict(arg) {
|
|
6513
6546
|
return new OnConflictQueryBuilder(this, arg);
|
|
6514
6547
|
}
|
|
6515
6548
|
/**
|
|
6516
|
-
* Use `
|
|
6549
|
+
* Use `onConflictDoNothing` to suppress unique constraint violation error when creating a record.
|
|
6517
6550
|
*
|
|
6518
6551
|
* Adds `ON CONFLICT (columns) DO NOTHING` clause to the insert statement, columns are optional.
|
|
6519
6552
|
*
|
|
@@ -6526,38 +6559,37 @@ class Create {
|
|
|
6526
6559
|
* name: 'John Doe',
|
|
6527
6560
|
* })
|
|
6528
6561
|
* // on any conflict:
|
|
6529
|
-
* .
|
|
6562
|
+
* .onConflictDoNothing()
|
|
6530
6563
|
* // or, for a specific column:
|
|
6531
|
-
* .
|
|
6564
|
+
* .onConflictDoNothing('email')
|
|
6532
6565
|
* // or, for a specific constraint:
|
|
6533
|
-
* .
|
|
6566
|
+
* .onConflictDoNothing({ constraint: 'unique_index_name' });
|
|
6534
6567
|
* ```
|
|
6535
6568
|
*
|
|
6536
|
-
* When there is a conflict, nothing can be returned from the database, so `
|
|
6569
|
+
* When there is a conflict, nothing can be returned from the database, so `onConflictDoNothing` adds `| undefined` part to the response type.
|
|
6537
6570
|
*
|
|
6538
6571
|
* ```ts
|
|
6539
6572
|
* const maybeRecord: RecordType | undefined = await db.table
|
|
6540
6573
|
* .create(data)
|
|
6541
|
-
* .
|
|
6574
|
+
* .onConflictDoNothing();
|
|
6542
6575
|
*
|
|
6543
6576
|
* const maybeId: number | undefined = await db.table
|
|
6544
6577
|
* .get('id')
|
|
6545
6578
|
* .create(data)
|
|
6546
|
-
* .
|
|
6579
|
+
* .onConflictDoNothing();
|
|
6547
6580
|
* ```
|
|
6548
6581
|
*
|
|
6549
6582
|
* When creating multiple records, only created records will be returned. If no records were created, array will be empty:
|
|
6550
6583
|
*
|
|
6551
6584
|
* ```ts
|
|
6552
6585
|
* // array can be empty
|
|
6553
|
-
* const arr = await db.table.createMany([data, data, data]).
|
|
6586
|
+
* const arr = await db.table.createMany([data, data, data]).onConflictDoNothing();
|
|
6554
6587
|
* ```
|
|
6555
6588
|
*/
|
|
6556
|
-
|
|
6589
|
+
onConflictDoNothing(arg) {
|
|
6557
6590
|
const q = this.clone();
|
|
6558
6591
|
q.q.onConflict = {
|
|
6559
|
-
|
|
6560
|
-
expr: arg
|
|
6592
|
+
target: arg
|
|
6561
6593
|
};
|
|
6562
6594
|
if (q.q.returnType === "oneOrThrow") {
|
|
6563
6595
|
q.q.returnType = "one";
|
|
@@ -6573,112 +6605,83 @@ class OnConflictQueryBuilder {
|
|
|
6573
6605
|
this.onConflict = onConflict;
|
|
6574
6606
|
}
|
|
6575
6607
|
/**
|
|
6576
|
-
* Available only after
|
|
6608
|
+
* Available only after `onConflict`.
|
|
6577
6609
|
*
|
|
6578
|
-
*
|
|
6610
|
+
* Updates the record with a given data when conflict occurs.
|
|
6579
6611
|
*
|
|
6580
6612
|
* ```ts
|
|
6581
|
-
* db.table
|
|
6582
|
-
*
|
|
6583
|
-
*
|
|
6584
|
-
* name: 'John Doe',
|
|
6585
|
-
* })
|
|
6586
|
-
* // for a specific column:
|
|
6587
|
-
* .onConflict('email')
|
|
6588
|
-
* // or, for a specific constraint:
|
|
6589
|
-
* .onConflict({ constraint: 'unique_constraint_name' })
|
|
6590
|
-
* .merge();
|
|
6613
|
+
* db.table.create(data).onConflict('column').set({
|
|
6614
|
+
* description: 'setting different data on conflict',
|
|
6615
|
+
* });
|
|
6591
6616
|
* ```
|
|
6592
6617
|
*
|
|
6593
|
-
*
|
|
6618
|
+
* The `set` can take a raw SQL expression:
|
|
6594
6619
|
*
|
|
6595
6620
|
* ```ts
|
|
6596
6621
|
* db.table
|
|
6597
|
-
* .
|
|
6598
|
-
*
|
|
6599
|
-
*
|
|
6600
|
-
* { email: 'alex@example.com', name: 'Alex Doe' },
|
|
6601
|
-
* ])
|
|
6602
|
-
* .onConflict('email')
|
|
6603
|
-
* .merge();
|
|
6604
|
-
* ```
|
|
6605
|
-
*
|
|
6606
|
-
* It is also possible to specify a subset of the columns to merge when a conflict occurs.
|
|
6607
|
-
* For example, you may want to set a `createdAt` column when creating but would prefer not to update it if the row already exists:
|
|
6608
|
-
*
|
|
6609
|
-
* ```ts
|
|
6610
|
-
* const timestamp = Date.now();
|
|
6622
|
+
* .create(data)
|
|
6623
|
+
* .onConflict()
|
|
6624
|
+
* .set(db.table.sql`raw SQL expression`);
|
|
6611
6625
|
*
|
|
6626
|
+
* // update records only on certain conditions
|
|
6612
6627
|
* db.table
|
|
6613
|
-
* .create(
|
|
6614
|
-
* email: 'ignore@example.com',
|
|
6615
|
-
* name: 'John Doe',
|
|
6616
|
-
* createdAt: timestamp,
|
|
6617
|
-
* updatedAt: timestamp,
|
|
6618
|
-
* })
|
|
6628
|
+
* .create(data)
|
|
6619
6629
|
* .onConflict('email')
|
|
6620
|
-
*
|
|
6621
|
-
* .
|
|
6622
|
-
* // or, update multiple columns
|
|
6623
|
-
* .merge(['email', 'name', 'updatedAt']);
|
|
6630
|
+
* .set({ key: 'value' })
|
|
6631
|
+
* .where({ ...certainConditions });
|
|
6624
6632
|
* ```
|
|
6625
6633
|
*
|
|
6626
|
-
*
|
|
6627
|
-
|
|
6628
|
-
|
|
6634
|
+
* @param set - object containing new column values, or raw SQL
|
|
6635
|
+
*/
|
|
6636
|
+
set(set) {
|
|
6637
|
+
this.query.q.onConflict = {
|
|
6638
|
+
target: this.onConflict,
|
|
6639
|
+
set
|
|
6640
|
+
};
|
|
6641
|
+
return this.query;
|
|
6642
|
+
}
|
|
6643
|
+
/**
|
|
6644
|
+
* Available only after `onConflict`.
|
|
6629
6645
|
*
|
|
6630
|
-
*
|
|
6631
|
-
* const timestamp = Date.now();
|
|
6646
|
+
* Use this method to merge all the data you have passed into `create` to update the existing record on conflict.
|
|
6632
6647
|
*
|
|
6633
|
-
*
|
|
6634
|
-
* .create({
|
|
6635
|
-
* email: 'ignore@example.com',
|
|
6636
|
-
* name: 'John Doe',
|
|
6637
|
-
* createdAt: timestamp,
|
|
6638
|
-
* updatedAt: timestamp,
|
|
6639
|
-
* })
|
|
6640
|
-
* .onConflict('email')
|
|
6641
|
-
* .merge({
|
|
6642
|
-
* name: 'John Doe The Second',
|
|
6643
|
-
* });
|
|
6644
|
-
* ```
|
|
6648
|
+
* If the table has columns with **dynamic** default values, such values will be applied as well.
|
|
6645
6649
|
*
|
|
6646
|
-
* You can
|
|
6650
|
+
* You can exclude certain columns from being merged by passing the `exclude` option.
|
|
6647
6651
|
*
|
|
6648
6652
|
* ```ts
|
|
6649
|
-
*
|
|
6653
|
+
* // merge the full data
|
|
6654
|
+
* db.table.create(data).onConflict('email').merge();
|
|
6650
6655
|
*
|
|
6656
|
+
* // merge only a single column
|
|
6657
|
+
* db.table.create(data).onConflict('email').merge('name');
|
|
6658
|
+
*
|
|
6659
|
+
* // merge multiple columns
|
|
6660
|
+
* db.table.create(data).onConflict('email').merge(['name', 'quantity']);
|
|
6661
|
+
*
|
|
6662
|
+
* // merge all columns except some
|
|
6651
6663
|
* db.table
|
|
6652
|
-
* .create(
|
|
6653
|
-
* email: 'ignore@example.com',
|
|
6654
|
-
* name: 'John Doe',
|
|
6655
|
-
* createdAt: timestamp,
|
|
6656
|
-
* updatedAt: timestamp,
|
|
6657
|
-
* })
|
|
6664
|
+
* .create(data)
|
|
6658
6665
|
* .onConflict('email')
|
|
6659
|
-
* .merge({
|
|
6660
|
-
* name: 'John Doe',
|
|
6661
|
-
* updatedAt: timestamp,
|
|
6662
|
-
* })
|
|
6663
|
-
* .where({ updatedAt: { lt: timestamp } });
|
|
6664
|
-
* ```
|
|
6666
|
+
* .merge({ except: ['name', 'quantity'] });
|
|
6665
6667
|
*
|
|
6666
|
-
*
|
|
6668
|
+
* // merge can be applied also for batch creates
|
|
6669
|
+
* db.table.createMany([data1, data2, data2]).onConflict('email').merge();
|
|
6667
6670
|
*
|
|
6668
|
-
*
|
|
6671
|
+
* // update records only on certain conditions
|
|
6669
6672
|
* db.table
|
|
6670
6673
|
* .create(data)
|
|
6671
|
-
* .onConflict()
|
|
6672
|
-
* .merge(
|
|
6674
|
+
* .onConflict('email')
|
|
6675
|
+
* .merge()
|
|
6676
|
+
* .where({ ...certainConditions });
|
|
6673
6677
|
* ```
|
|
6674
6678
|
*
|
|
6675
|
-
* @param
|
|
6679
|
+
* @param merge - no argument will merge all data, or provide a column(s) to merge, or provide `except` to update all except some.
|
|
6676
6680
|
*/
|
|
6677
|
-
merge(
|
|
6681
|
+
merge(merge) {
|
|
6678
6682
|
this.query.q.onConflict = {
|
|
6679
|
-
|
|
6680
|
-
|
|
6681
|
-
update
|
|
6683
|
+
target: this.onConflict,
|
|
6684
|
+
merge
|
|
6682
6685
|
};
|
|
6683
6686
|
return this.query;
|
|
6684
6687
|
}
|