@graffy/pg 0.16.0 → 0.16.2-alpha.1
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/index.cjs +35 -35
- package/index.mjs +32 -29
- package/package.json +2 -2
- package/types/sql/getArgSql.d.ts +1 -1
package/index.cjs
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const common = require("@graffy/common");
|
|
4
4
|
const pg$1 = require("pg");
|
|
5
5
|
const debug = require("debug");
|
|
6
|
-
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
|
|
7
|
-
const pg__default = /* @__PURE__ */ _interopDefaultLegacy(pg$1);
|
|
8
|
-
const debug__default = /* @__PURE__ */ _interopDefaultLegacy(debug);
|
|
9
6
|
class Sql {
|
|
10
7
|
constructor(rawStrings, rawValues) {
|
|
11
8
|
if (rawStrings.length - 1 !== rawValues.length) {
|
|
@@ -105,7 +102,7 @@ function construct(node, prop, op) {
|
|
|
105
102
|
return [op, prop, node];
|
|
106
103
|
if (prop)
|
|
107
104
|
return ["$eq", prop, node];
|
|
108
|
-
throw Error(
|
|
105
|
+
throw Error(`pgast.expected_prop_before:${JSON.stringify(node)}`);
|
|
109
106
|
}
|
|
110
107
|
if (Array.isArray(node)) {
|
|
111
108
|
return ["$or", node.map((item) => construct(item, prop, op))];
|
|
@@ -121,17 +118,17 @@ function construct(node, prop, op) {
|
|
|
121
118
|
}
|
|
122
119
|
if (key[0] === "$") {
|
|
123
120
|
if (!valid[key])
|
|
124
|
-
throw Error(
|
|
121
|
+
throw Error(`pgast.invalid_op:${key}`);
|
|
125
122
|
if (op)
|
|
126
|
-
throw Error(
|
|
123
|
+
throw Error(`pgast.unexpected_op:${op} before:${key}`);
|
|
127
124
|
if (!prop)
|
|
128
|
-
throw Error(
|
|
125
|
+
throw Error(`pgast.expected_prop_before:${key}`);
|
|
129
126
|
return construct(val, prop, key);
|
|
130
127
|
}
|
|
131
128
|
if (prop) {
|
|
132
129
|
if (key[0] === ".")
|
|
133
130
|
return construct(val, prop + key);
|
|
134
|
-
throw Error(
|
|
131
|
+
throw Error(`pgast.unexpected_prop: ${key}`);
|
|
135
132
|
}
|
|
136
133
|
return construct(val, key);
|
|
137
134
|
})
|
|
@@ -253,8 +250,8 @@ function vertexSql(array, nullValue) {
|
|
|
253
250
|
)}]::float8[]`;
|
|
254
251
|
}
|
|
255
252
|
function cubeLiteralSql(value) {
|
|
256
|
-
if (!Array.isArray(value)
|
|
257
|
-
throw Error(
|
|
253
|
+
if (!(Array.isArray(value) && value.length) || Array.isArray(value[0]) && value.length !== 2) {
|
|
254
|
+
throw Error(`pg.castValue_bad_cube${JSON.stringify(value)}`);
|
|
258
255
|
}
|
|
259
256
|
return Array.isArray(value[0]) ? sql`cube(${vertexSql(value[0], sql`'-Infinity'`)}, ${vertexSql(
|
|
260
257
|
value[1],
|
|
@@ -263,13 +260,13 @@ function cubeLiteralSql(value) {
|
|
|
263
260
|
}
|
|
264
261
|
function castValue(value, type, name, isPut) {
|
|
265
262
|
if (!type)
|
|
266
|
-
throw Error(
|
|
263
|
+
throw Error(`pg.write_no_column ${name}`);
|
|
267
264
|
if (value instanceof Sql)
|
|
268
265
|
return value;
|
|
269
266
|
if (value === null)
|
|
270
267
|
return sql`NULL`;
|
|
271
268
|
if (type === "jsonb") {
|
|
272
|
-
return isPut ? JSON.stringify(stripAttributes(value)) :
|
|
269
|
+
return isPut ? JSON.stringify(stripAttributes(value)) : getJsonUpdate(value, name, []);
|
|
273
270
|
}
|
|
274
271
|
if (type === "cube")
|
|
275
272
|
return cubeLiteralSql(value);
|
|
@@ -304,7 +301,7 @@ function getJsonUpdate(object, col, path) {
|
|
|
304
301
|
const curr = sql`"${raw(col)}"${path.length ? sql`#>${path}` : empty}`;
|
|
305
302
|
if (common.isEmpty(object))
|
|
306
303
|
return curr;
|
|
307
|
-
return sql`(case jsonb_typeof(${curr})
|
|
304
|
+
return sql`nullif(jsonb_strip_nulls((case jsonb_typeof(${curr})
|
|
308
305
|
when 'object' then ${curr}
|
|
309
306
|
else '{}'::jsonb
|
|
310
307
|
end) || jsonb_build_object(${join(
|
|
@@ -312,7 +309,7 @@ function getJsonUpdate(object, col, path) {
|
|
|
312
309
|
([key, value]) => sql`${key}::text, ${getJsonUpdate(value, col, path.concat(key))}`
|
|
313
310
|
),
|
|
314
311
|
", "
|
|
315
|
-
)})`;
|
|
312
|
+
)})), '{}'::jsonb)`;
|
|
316
313
|
}
|
|
317
314
|
function stripAttributes(object) {
|
|
318
315
|
if (typeof object !== "object" || !object)
|
|
@@ -321,15 +318,17 @@ function stripAttributes(object) {
|
|
|
321
318
|
return object.map((item) => stripAttributes(item));
|
|
322
319
|
}
|
|
323
320
|
return Object.entries(object).reduce((out, [key, val]) => {
|
|
324
|
-
if (key === "$put")
|
|
321
|
+
if (key === "$put" || val === null)
|
|
325
322
|
return out;
|
|
323
|
+
if (out === null)
|
|
324
|
+
out = {};
|
|
326
325
|
out[key] = stripAttributes(val);
|
|
327
326
|
return out;
|
|
328
|
-
},
|
|
327
|
+
}, null);
|
|
329
328
|
}
|
|
330
329
|
const opSql = {
|
|
331
|
-
$and:
|
|
332
|
-
$or:
|
|
330
|
+
$and: "AND",
|
|
331
|
+
$or: "OR",
|
|
333
332
|
$not: sql`NOT`,
|
|
334
333
|
$eq: sql`=`,
|
|
335
334
|
$neq: sql`<>`,
|
|
@@ -351,7 +350,7 @@ function getBinarySql(lhs, type, op, value, textLhs) {
|
|
|
351
350
|
return sql`${lhs} IS NOT NULL`;
|
|
352
351
|
const sqlOp = opSql[op];
|
|
353
352
|
if (!sqlOp)
|
|
354
|
-
throw Error(
|
|
353
|
+
throw Error(`pg.getSql_unknown_operator ${op}`);
|
|
355
354
|
if (op === "$in" || op === "$nin") {
|
|
356
355
|
if (type === "jsonb" && typeof value[0] === "string")
|
|
357
356
|
lhs = textLhs;
|
|
@@ -391,7 +390,7 @@ function getSql(filter, options) {
|
|
|
391
390
|
const [prefix, ...suffix] = ast[1].split(".");
|
|
392
391
|
const { types: types2 } = options.schema;
|
|
393
392
|
if (!types2[prefix])
|
|
394
|
-
throw Error(
|
|
393
|
+
throw Error(`pg.no_column ${prefix}`);
|
|
395
394
|
if (types2[prefix] === "jsonb") {
|
|
396
395
|
const [lhs, textLhs] = suffix.length ? [
|
|
397
396
|
sql`"${raw(prefix)}" #> ${suffix}`,
|
|
@@ -400,7 +399,7 @@ function getSql(filter, options) {
|
|
|
400
399
|
return getBinarySql(lhs, "jsonb", op, ast[2], textLhs);
|
|
401
400
|
} else {
|
|
402
401
|
if (suffix.length)
|
|
403
|
-
throw Error(
|
|
402
|
+
throw Error(`pg.lookup_not_jsonb ${prefix}`);
|
|
404
403
|
return getBinarySql(sql`"${raw(prefix)}"`, types2[prefix], op, ast[2]);
|
|
405
404
|
}
|
|
406
405
|
}
|
|
@@ -422,10 +421,10 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
|
|
|
422
421
|
const meta = (key2) => $group ? getAggMeta(key2, options) : getArgMeta(key2, options);
|
|
423
422
|
const hasRangeArg = $before || $after || $since || $until || $first || $last || $all;
|
|
424
423
|
if ($order && $group) {
|
|
425
|
-
throw Error(
|
|
424
|
+
throw Error(`pg_arg.order_and_group_unsupported in ${prefix}`);
|
|
426
425
|
}
|
|
427
426
|
if (($order || $group && $group !== true) && !hasRangeArg) {
|
|
428
|
-
throw Error(
|
|
427
|
+
throw Error(`pg_arg.range_arg_expected in ${prefix}`);
|
|
429
428
|
}
|
|
430
429
|
const baseKey = sql`${JSON.stringify(rest)}::jsonb`;
|
|
431
430
|
const where = [];
|
|
@@ -448,7 +447,7 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
|
|
|
448
447
|
($order || [idCol]).map(
|
|
449
448
|
(orderItem) => orderItem[0] === "!" ? sql`${lookup(orderItem.slice(1))} ${$last ? sql`ASC` : sql`DESC`}` : sql`${lookup(orderItem)} ${$last ? sql`DESC` : sql`ASC`}`
|
|
450
449
|
),
|
|
451
|
-
|
|
450
|
+
", "
|
|
452
451
|
);
|
|
453
452
|
const cursorKey = getJsonBuildTrusted({
|
|
454
453
|
$cursor: $group === true ? sql`''` : sql`jsonb_build_array(${join(groupCols || orderCols)})`
|
|
@@ -464,7 +463,7 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
|
|
|
464
463
|
}
|
|
465
464
|
function getBoundCond(orderCols, bound, kind) {
|
|
466
465
|
if (!Array.isArray(bound)) {
|
|
467
|
-
throw Error(
|
|
466
|
+
throw Error(`pg_arg.bad_query bound : ${JSON.stringify(bound)}`);
|
|
468
467
|
}
|
|
469
468
|
const lhs = orderCols[0];
|
|
470
469
|
const rhs = bound[0];
|
|
@@ -500,7 +499,7 @@ function selectByArgs(args, projection, options) {
|
|
|
500
499
|
SELECT
|
|
501
500
|
${getSelectCols(table, projection)}, ${meta}
|
|
502
501
|
FROM "${raw(table)}"
|
|
503
|
-
${where.length ? sql`WHERE ${join(where,
|
|
502
|
+
${where.length ? sql`WHERE ${join(where, " AND ")}` : empty}
|
|
504
503
|
${group ? sql`GROUP BY ${group}` : empty}
|
|
505
504
|
${order ? sql`ORDER BY ${order}` : empty}
|
|
506
505
|
LIMIT ${clampedLimit}
|
|
@@ -524,13 +523,13 @@ function getSingleSql(arg, options) {
|
|
|
524
523
|
};
|
|
525
524
|
}
|
|
526
525
|
const { where, meta } = getArgSql(arg, options);
|
|
527
|
-
if (!where
|
|
526
|
+
if (!(where == null ? void 0 : where.length))
|
|
528
527
|
throw Error("pg_write.no_condition");
|
|
529
528
|
return {
|
|
530
529
|
where: sql`"${raw(idCol)}" = (
|
|
531
530
|
SELECT "${raw(idCol)}"
|
|
532
531
|
FROM "${raw(table)}"
|
|
533
|
-
WHERE ${join(where,
|
|
532
|
+
WHERE ${join(where, " AND ")}
|
|
534
533
|
LIMIT 1
|
|
535
534
|
)`,
|
|
536
535
|
meta
|
|
@@ -548,7 +547,8 @@ function patch(object, arg, options) {
|
|
|
548
547
|
function put(object, arg, options) {
|
|
549
548
|
const { idCol, table } = options;
|
|
550
549
|
const row = object;
|
|
551
|
-
let meta
|
|
550
|
+
let meta;
|
|
551
|
+
let conflictTarget;
|
|
552
552
|
if (common.isPlainObject(arg)) {
|
|
553
553
|
({ meta } = getArgSql(arg, options));
|
|
554
554
|
conflictTarget = join(Object.keys(arg).map((col) => sql`"${raw(col)}"`));
|
|
@@ -570,8 +570,8 @@ function del(arg, options) {
|
|
|
570
570
|
WHERE ${where}
|
|
571
571
|
RETURNING ${arg} "$key"`;
|
|
572
572
|
}
|
|
573
|
-
const log =
|
|
574
|
-
const { Pool, Client, types } =
|
|
573
|
+
const log = debug("graffy:pg:db");
|
|
574
|
+
const { Pool, Client, types } = pg$1;
|
|
575
575
|
class Db {
|
|
576
576
|
constructor(connection) {
|
|
577
577
|
if (typeof connection === "object" && connection && (connection instanceof Pool || connection instanceof Client)) {
|
|
@@ -581,7 +581,7 @@ class Db {
|
|
|
581
581
|
}
|
|
582
582
|
}
|
|
583
583
|
async query(sql2) {
|
|
584
|
-
log(
|
|
584
|
+
log(`Making SQL query: ${sql2.text}`, sql2.values);
|
|
585
585
|
try {
|
|
586
586
|
sql2.types = {
|
|
587
587
|
getTypeParser: (oid, format) => {
|
|
@@ -601,7 +601,7 @@ class Db {
|
|
|
601
601
|
sql2.text,
|
|
602
602
|
JSON.stringify(sql2.values)
|
|
603
603
|
].filter(Boolean).join("; ");
|
|
604
|
-
throw Error(
|
|
604
|
+
throw Error(`pg.sql_error ${message}`);
|
|
605
605
|
}
|
|
606
606
|
}
|
|
607
607
|
async readSql(sql2) {
|
|
@@ -613,7 +613,7 @@ class Db {
|
|
|
613
613
|
const res = await this.query(sql2);
|
|
614
614
|
log("Rows written", res.rowCount);
|
|
615
615
|
if (!res.rowCount) {
|
|
616
|
-
throw Error(
|
|
616
|
+
throw Error(`pg.nothing_written ${sql2.text} with ${sql2.values}`);
|
|
617
617
|
}
|
|
618
618
|
return res.rows[0];
|
|
619
619
|
}
|
package/index.mjs
CHANGED
|
@@ -100,7 +100,7 @@ function construct(node, prop, op) {
|
|
|
100
100
|
return [op, prop, node];
|
|
101
101
|
if (prop)
|
|
102
102
|
return ["$eq", prop, node];
|
|
103
|
-
throw Error(
|
|
103
|
+
throw Error(`pgast.expected_prop_before:${JSON.stringify(node)}`);
|
|
104
104
|
}
|
|
105
105
|
if (Array.isArray(node)) {
|
|
106
106
|
return ["$or", node.map((item) => construct(item, prop, op))];
|
|
@@ -116,17 +116,17 @@ function construct(node, prop, op) {
|
|
|
116
116
|
}
|
|
117
117
|
if (key[0] === "$") {
|
|
118
118
|
if (!valid[key])
|
|
119
|
-
throw Error(
|
|
119
|
+
throw Error(`pgast.invalid_op:${key}`);
|
|
120
120
|
if (op)
|
|
121
|
-
throw Error(
|
|
121
|
+
throw Error(`pgast.unexpected_op:${op} before:${key}`);
|
|
122
122
|
if (!prop)
|
|
123
|
-
throw Error(
|
|
123
|
+
throw Error(`pgast.expected_prop_before:${key}`);
|
|
124
124
|
return construct(val, prop, key);
|
|
125
125
|
}
|
|
126
126
|
if (prop) {
|
|
127
127
|
if (key[0] === ".")
|
|
128
128
|
return construct(val, prop + key);
|
|
129
|
-
throw Error(
|
|
129
|
+
throw Error(`pgast.unexpected_prop: ${key}`);
|
|
130
130
|
}
|
|
131
131
|
return construct(val, key);
|
|
132
132
|
})
|
|
@@ -248,8 +248,8 @@ function vertexSql(array, nullValue) {
|
|
|
248
248
|
)}]::float8[]`;
|
|
249
249
|
}
|
|
250
250
|
function cubeLiteralSql(value) {
|
|
251
|
-
if (!Array.isArray(value)
|
|
252
|
-
throw Error(
|
|
251
|
+
if (!(Array.isArray(value) && value.length) || Array.isArray(value[0]) && value.length !== 2) {
|
|
252
|
+
throw Error(`pg.castValue_bad_cube${JSON.stringify(value)}`);
|
|
253
253
|
}
|
|
254
254
|
return Array.isArray(value[0]) ? sql`cube(${vertexSql(value[0], sql`'-Infinity'`)}, ${vertexSql(
|
|
255
255
|
value[1],
|
|
@@ -258,13 +258,13 @@ function cubeLiteralSql(value) {
|
|
|
258
258
|
}
|
|
259
259
|
function castValue(value, type, name, isPut) {
|
|
260
260
|
if (!type)
|
|
261
|
-
throw Error(
|
|
261
|
+
throw Error(`pg.write_no_column ${name}`);
|
|
262
262
|
if (value instanceof Sql)
|
|
263
263
|
return value;
|
|
264
264
|
if (value === null)
|
|
265
265
|
return sql`NULL`;
|
|
266
266
|
if (type === "jsonb") {
|
|
267
|
-
return isPut ? JSON.stringify(stripAttributes(value)) :
|
|
267
|
+
return isPut ? JSON.stringify(stripAttributes(value)) : getJsonUpdate(value, name, []);
|
|
268
268
|
}
|
|
269
269
|
if (type === "cube")
|
|
270
270
|
return cubeLiteralSql(value);
|
|
@@ -299,7 +299,7 @@ function getJsonUpdate(object, col, path) {
|
|
|
299
299
|
const curr = sql`"${raw(col)}"${path.length ? sql`#>${path}` : empty}`;
|
|
300
300
|
if (isEmpty(object))
|
|
301
301
|
return curr;
|
|
302
|
-
return sql`(case jsonb_typeof(${curr})
|
|
302
|
+
return sql`nullif(jsonb_strip_nulls((case jsonb_typeof(${curr})
|
|
303
303
|
when 'object' then ${curr}
|
|
304
304
|
else '{}'::jsonb
|
|
305
305
|
end) || jsonb_build_object(${join(
|
|
@@ -307,7 +307,7 @@ function getJsonUpdate(object, col, path) {
|
|
|
307
307
|
([key, value]) => sql`${key}::text, ${getJsonUpdate(value, col, path.concat(key))}`
|
|
308
308
|
),
|
|
309
309
|
", "
|
|
310
|
-
)})`;
|
|
310
|
+
)})), '{}'::jsonb)`;
|
|
311
311
|
}
|
|
312
312
|
function stripAttributes(object) {
|
|
313
313
|
if (typeof object !== "object" || !object)
|
|
@@ -316,15 +316,17 @@ function stripAttributes(object) {
|
|
|
316
316
|
return object.map((item) => stripAttributes(item));
|
|
317
317
|
}
|
|
318
318
|
return Object.entries(object).reduce((out, [key, val]) => {
|
|
319
|
-
if (key === "$put")
|
|
319
|
+
if (key === "$put" || val === null)
|
|
320
320
|
return out;
|
|
321
|
+
if (out === null)
|
|
322
|
+
out = {};
|
|
321
323
|
out[key] = stripAttributes(val);
|
|
322
324
|
return out;
|
|
323
|
-
},
|
|
325
|
+
}, null);
|
|
324
326
|
}
|
|
325
327
|
const opSql = {
|
|
326
|
-
$and:
|
|
327
|
-
$or:
|
|
328
|
+
$and: "AND",
|
|
329
|
+
$or: "OR",
|
|
328
330
|
$not: sql`NOT`,
|
|
329
331
|
$eq: sql`=`,
|
|
330
332
|
$neq: sql`<>`,
|
|
@@ -346,7 +348,7 @@ function getBinarySql(lhs, type, op, value, textLhs) {
|
|
|
346
348
|
return sql`${lhs} IS NOT NULL`;
|
|
347
349
|
const sqlOp = opSql[op];
|
|
348
350
|
if (!sqlOp)
|
|
349
|
-
throw Error(
|
|
351
|
+
throw Error(`pg.getSql_unknown_operator ${op}`);
|
|
350
352
|
if (op === "$in" || op === "$nin") {
|
|
351
353
|
if (type === "jsonb" && typeof value[0] === "string")
|
|
352
354
|
lhs = textLhs;
|
|
@@ -386,7 +388,7 @@ function getSql(filter, options) {
|
|
|
386
388
|
const [prefix, ...suffix] = ast[1].split(".");
|
|
387
389
|
const { types: types2 } = options.schema;
|
|
388
390
|
if (!types2[prefix])
|
|
389
|
-
throw Error(
|
|
391
|
+
throw Error(`pg.no_column ${prefix}`);
|
|
390
392
|
if (types2[prefix] === "jsonb") {
|
|
391
393
|
const [lhs, textLhs] = suffix.length ? [
|
|
392
394
|
sql`"${raw(prefix)}" #> ${suffix}`,
|
|
@@ -395,7 +397,7 @@ function getSql(filter, options) {
|
|
|
395
397
|
return getBinarySql(lhs, "jsonb", op, ast[2], textLhs);
|
|
396
398
|
} else {
|
|
397
399
|
if (suffix.length)
|
|
398
|
-
throw Error(
|
|
400
|
+
throw Error(`pg.lookup_not_jsonb ${prefix}`);
|
|
399
401
|
return getBinarySql(sql`"${raw(prefix)}"`, types2[prefix], op, ast[2]);
|
|
400
402
|
}
|
|
401
403
|
}
|
|
@@ -417,10 +419,10 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
|
|
|
417
419
|
const meta = (key2) => $group ? getAggMeta(key2, options) : getArgMeta(key2, options);
|
|
418
420
|
const hasRangeArg = $before || $after || $since || $until || $first || $last || $all;
|
|
419
421
|
if ($order && $group) {
|
|
420
|
-
throw Error(
|
|
422
|
+
throw Error(`pg_arg.order_and_group_unsupported in ${prefix}`);
|
|
421
423
|
}
|
|
422
424
|
if (($order || $group && $group !== true) && !hasRangeArg) {
|
|
423
|
-
throw Error(
|
|
425
|
+
throw Error(`pg_arg.range_arg_expected in ${prefix}`);
|
|
424
426
|
}
|
|
425
427
|
const baseKey = sql`${JSON.stringify(rest)}::jsonb`;
|
|
426
428
|
const where = [];
|
|
@@ -443,7 +445,7 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
|
|
|
443
445
|
($order || [idCol]).map(
|
|
444
446
|
(orderItem) => orderItem[0] === "!" ? sql`${lookup(orderItem.slice(1))} ${$last ? sql`ASC` : sql`DESC`}` : sql`${lookup(orderItem)} ${$last ? sql`DESC` : sql`ASC`}`
|
|
445
447
|
),
|
|
446
|
-
|
|
448
|
+
", "
|
|
447
449
|
);
|
|
448
450
|
const cursorKey = getJsonBuildTrusted({
|
|
449
451
|
$cursor: $group === true ? sql`''` : sql`jsonb_build_array(${join(groupCols || orderCols)})`
|
|
@@ -459,7 +461,7 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
|
|
|
459
461
|
}
|
|
460
462
|
function getBoundCond(orderCols, bound, kind) {
|
|
461
463
|
if (!Array.isArray(bound)) {
|
|
462
|
-
throw Error(
|
|
464
|
+
throw Error(`pg_arg.bad_query bound : ${JSON.stringify(bound)}`);
|
|
463
465
|
}
|
|
464
466
|
const lhs = orderCols[0];
|
|
465
467
|
const rhs = bound[0];
|
|
@@ -495,7 +497,7 @@ function selectByArgs(args, projection, options) {
|
|
|
495
497
|
SELECT
|
|
496
498
|
${getSelectCols(table, projection)}, ${meta}
|
|
497
499
|
FROM "${raw(table)}"
|
|
498
|
-
${where.length ? sql`WHERE ${join(where,
|
|
500
|
+
${where.length ? sql`WHERE ${join(where, " AND ")}` : empty}
|
|
499
501
|
${group ? sql`GROUP BY ${group}` : empty}
|
|
500
502
|
${order ? sql`ORDER BY ${order}` : empty}
|
|
501
503
|
LIMIT ${clampedLimit}
|
|
@@ -519,13 +521,13 @@ function getSingleSql(arg, options) {
|
|
|
519
521
|
};
|
|
520
522
|
}
|
|
521
523
|
const { where, meta } = getArgSql(arg, options);
|
|
522
|
-
if (!where
|
|
524
|
+
if (!(where == null ? void 0 : where.length))
|
|
523
525
|
throw Error("pg_write.no_condition");
|
|
524
526
|
return {
|
|
525
527
|
where: sql`"${raw(idCol)}" = (
|
|
526
528
|
SELECT "${raw(idCol)}"
|
|
527
529
|
FROM "${raw(table)}"
|
|
528
|
-
WHERE ${join(where,
|
|
530
|
+
WHERE ${join(where, " AND ")}
|
|
529
531
|
LIMIT 1
|
|
530
532
|
)`,
|
|
531
533
|
meta
|
|
@@ -543,7 +545,8 @@ function patch(object, arg, options) {
|
|
|
543
545
|
function put(object, arg, options) {
|
|
544
546
|
const { idCol, table } = options;
|
|
545
547
|
const row = object;
|
|
546
|
-
let meta
|
|
548
|
+
let meta;
|
|
549
|
+
let conflictTarget;
|
|
547
550
|
if (isPlainObject(arg)) {
|
|
548
551
|
({ meta } = getArgSql(arg, options));
|
|
549
552
|
conflictTarget = join(Object.keys(arg).map((col) => sql`"${raw(col)}"`));
|
|
@@ -576,7 +579,7 @@ class Db {
|
|
|
576
579
|
}
|
|
577
580
|
}
|
|
578
581
|
async query(sql2) {
|
|
579
|
-
log(
|
|
582
|
+
log(`Making SQL query: ${sql2.text}`, sql2.values);
|
|
580
583
|
try {
|
|
581
584
|
sql2.types = {
|
|
582
585
|
getTypeParser: (oid, format) => {
|
|
@@ -596,7 +599,7 @@ class Db {
|
|
|
596
599
|
sql2.text,
|
|
597
600
|
JSON.stringify(sql2.values)
|
|
598
601
|
].filter(Boolean).join("; ");
|
|
599
|
-
throw Error(
|
|
602
|
+
throw Error(`pg.sql_error ${message}`);
|
|
600
603
|
}
|
|
601
604
|
}
|
|
602
605
|
async readSql(sql2) {
|
|
@@ -608,7 +611,7 @@ class Db {
|
|
|
608
611
|
const res = await this.query(sql2);
|
|
609
612
|
log("Rows written", res.rowCount);
|
|
610
613
|
if (!res.rowCount) {
|
|
611
|
-
throw Error(
|
|
614
|
+
throw Error(`pg.nothing_written ${sql2.text} with ${sql2.values}`);
|
|
612
615
|
}
|
|
613
616
|
return res.rows[0];
|
|
614
617
|
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@graffy/pg",
|
|
3
3
|
"description": "The standard Postgres module for Graffy. Each instance this module mounts a Postgres table as a Graffy subtree.",
|
|
4
4
|
"author": "aravind (https://github.com/aravindet)",
|
|
5
|
-
"version": "0.16.
|
|
5
|
+
"version": "0.16.2-alpha.1",
|
|
6
6
|
"main": "./index.cjs",
|
|
7
7
|
"exports": {
|
|
8
8
|
"import": "./index.mjs",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"license": "Apache-2.0",
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@graffy/common": "0.16.
|
|
19
|
+
"@graffy/common": "0.16.2-alpha.1",
|
|
20
20
|
"debug": "^4.3.3"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
package/types/sql/getArgSql.d.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
@typedef { import('sql-template-tag').Sql } Sql
|
|
8
8
|
@return {{ meta: Sql, where: Sql[], order?: Sql, group?: Sql, limit: number }}
|
|
9
9
|
*/
|
|
10
|
-
export default function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $cursor, ...rest }: object, options: {
|
|
10
|
+
export default function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $cursor: _, ...rest }: object, options: {
|
|
11
11
|
prefix: string;
|
|
12
12
|
idCol: string;
|
|
13
13
|
verDefault: string;
|