pqb 0.2.6 → 0.2.7
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.esm.js +3 -3
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/queryMethods/update.test.ts +25 -0
- package/src/sql/update.ts +3 -5
package/package.json
CHANGED
|
@@ -413,4 +413,29 @@ describe('update', () => {
|
|
|
413
413
|
expect(eq).toBe(true);
|
|
414
414
|
});
|
|
415
415
|
});
|
|
416
|
+
|
|
417
|
+
describe('chaining', () => {
|
|
418
|
+
it('should handle multiple updates with increment and decrement', () => {
|
|
419
|
+
const query = User.select('id')
|
|
420
|
+
.find(1)
|
|
421
|
+
.update({ name: 'name' })
|
|
422
|
+
.increment('id')
|
|
423
|
+
.update({ password: 'password' })
|
|
424
|
+
.decrement('age');
|
|
425
|
+
|
|
426
|
+
expectSql(
|
|
427
|
+
query.toSql(),
|
|
428
|
+
`
|
|
429
|
+
UPDATE "user"
|
|
430
|
+
SET "name" = $1,
|
|
431
|
+
"id" = "id" + $2,
|
|
432
|
+
"password" = $3,
|
|
433
|
+
"age" = "age" - $4
|
|
434
|
+
WHERE "user"."id" = $5
|
|
435
|
+
RETURNING "user"."id"
|
|
436
|
+
`,
|
|
437
|
+
['name', 1, 'password', 1, 1],
|
|
438
|
+
);
|
|
439
|
+
});
|
|
440
|
+
});
|
|
416
441
|
});
|
package/src/sql/update.ts
CHANGED
|
@@ -21,22 +21,20 @@ export const pushUpdateSql = (
|
|
|
21
21
|
|
|
22
22
|
ctx.sql.push('SET');
|
|
23
23
|
|
|
24
|
+
const set: string[] = [];
|
|
24
25
|
query.data.forEach((item) => {
|
|
25
26
|
if (isRaw(item)) {
|
|
26
|
-
|
|
27
|
+
set.push(getRaw(item, ctx.values));
|
|
27
28
|
} else {
|
|
28
|
-
const set: string[] = [];
|
|
29
|
-
|
|
30
29
|
for (const key in item) {
|
|
31
30
|
const value = item[key];
|
|
32
31
|
if (value !== undefined) {
|
|
33
32
|
set.push(`${q(key)} = ${processValue(ctx.values, key, value)}`);
|
|
34
33
|
}
|
|
35
34
|
}
|
|
36
|
-
|
|
37
|
-
ctx.sql.push(set.join(', '));
|
|
38
35
|
}
|
|
39
36
|
});
|
|
37
|
+
ctx.sql.push(set.join(', '));
|
|
40
38
|
|
|
41
39
|
pushWhereStatementSql(ctx, model, query, quotedAs);
|
|
42
40
|
pushReturningSql(ctx, model, query, quotedAs);
|