@graffy/pg 0.16.2 → 0.16.3-alpha.2
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 +33 -22
- package/index.mjs +33 -22
- package/package.json +2 -2
- package/types/sql/clauses.d.ts +2 -2
package/index.cjs
CHANGED
|
@@ -206,24 +206,32 @@ const getJsonBuildValue = (value) => {
|
|
|
206
206
|
return sql`${value}::text`;
|
|
207
207
|
return sql`${JSON.stringify(stripAttributes(value))}::jsonb`;
|
|
208
208
|
};
|
|
209
|
-
const lookup = (prop) => {
|
|
209
|
+
const lookup = (prop, options) => {
|
|
210
210
|
const [prefix, ...suffix] = prop.split(".");
|
|
211
|
-
|
|
211
|
+
if (!suffix.length)
|
|
212
|
+
return sql`"${raw(prefix)}"`;
|
|
213
|
+
const { types: types2 } = options.schema;
|
|
214
|
+
if (types2[prefix] === "jsonb") {
|
|
215
|
+
return sql`"${raw(prefix)}" #> ${suffix}`;
|
|
216
|
+
} else if (types2[prefix] === "cube" && suffix.length === 1) {
|
|
217
|
+
return sql`"${raw(prefix)}" ~> ${parseInt(suffix[0])}`;
|
|
218
|
+
} else {
|
|
219
|
+
throw Error(`pg.cannot_lookup ${prop}`);
|
|
220
|
+
}
|
|
212
221
|
};
|
|
213
222
|
const lookupNumeric = (prop) => {
|
|
214
223
|
const [prefix, ...suffix] = prop.split(".");
|
|
215
|
-
return suffix.length ? sql`CASE WHEN "${raw(prefix)}" #> ${suffix} = 'null'::jsonb
|
|
216
|
-
|
|
217
|
-
)}" #> ${suffix})::numeric END` : sql`"${raw(prefix)}"`;
|
|
224
|
+
return suffix.length ? sql`CASE WHEN "${raw(prefix)}" #> ${suffix} = 'null'::jsonb
|
|
225
|
+
THEN 0 ELSE ("${raw(prefix)}" #> ${suffix})::numeric END` : sql`"${raw(prefix)}"`;
|
|
218
226
|
};
|
|
219
227
|
const aggSql = {
|
|
220
228
|
$sum: (prop) => sql`sum((${lookupNumeric(prop)})::numeric)`,
|
|
221
|
-
$card: (prop) => sql`count(distinct(${lookup(prop)}))`,
|
|
229
|
+
$card: (prop, options) => sql`count(distinct(${lookup(prop, options)}))`,
|
|
222
230
|
$avg: (prop) => sql`avg((${lookupNumeric(prop)})::numeric)`,
|
|
223
231
|
$max: (prop) => sql`max((${lookupNumeric(prop)})::numeric)`,
|
|
224
232
|
$min: (prop) => sql`min((${lookupNumeric(prop)})::numeric)`
|
|
225
233
|
};
|
|
226
|
-
const getSelectCols = (
|
|
234
|
+
const getSelectCols = (options, projection = null) => {
|
|
227
235
|
if (!projection)
|
|
228
236
|
return sql`*`;
|
|
229
237
|
const sqls = [];
|
|
@@ -233,7 +241,7 @@ const getSelectCols = (table, projection = null) => {
|
|
|
233
241
|
} else if (aggSql[key]) {
|
|
234
242
|
const subSqls = [];
|
|
235
243
|
for (const prop in projection[key]) {
|
|
236
|
-
subSqls.push(sql`${prop}::text, ${aggSql[key](prop)}`);
|
|
244
|
+
subSqls.push(sql`${prop}::text, ${aggSql[key](prop, options)}`);
|
|
237
245
|
}
|
|
238
246
|
sqls.push(
|
|
239
247
|
sql`jsonb_build_object(${join(subSqls, ", ")}) AS "${raw(key)}"`
|
|
@@ -317,14 +325,17 @@ function stripAttributes(object) {
|
|
|
317
325
|
if (Array.isArray(object)) {
|
|
318
326
|
return object.map((item) => stripAttributes(item));
|
|
319
327
|
}
|
|
320
|
-
return Object.entries(object).reduce(
|
|
321
|
-
|
|
328
|
+
return Object.entries(object).reduce(
|
|
329
|
+
(out, [key, val]) => {
|
|
330
|
+
if (key === "$put" || val === null)
|
|
331
|
+
return out;
|
|
332
|
+
if (out === null)
|
|
333
|
+
out = {};
|
|
334
|
+
out[key] = stripAttributes(val);
|
|
322
335
|
return out;
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
return out;
|
|
327
|
-
}, null);
|
|
336
|
+
},
|
|
337
|
+
null
|
|
338
|
+
);
|
|
328
339
|
}
|
|
329
340
|
const opSql = {
|
|
330
341
|
$and: "AND",
|
|
@@ -432,10 +443,10 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
|
|
|
432
443
|
where.push(getSql(filter, options));
|
|
433
444
|
if (!hasRangeArg)
|
|
434
445
|
return { meta: meta(baseKey), where, limit: 1 };
|
|
435
|
-
const groupCols = Array.isArray($group) && $group.length && $group.map(lookup);
|
|
446
|
+
const groupCols = Array.isArray($group) && $group.length && $group.map((prop) => lookup(prop, options));
|
|
436
447
|
const group = groupCols ? join(groupCols, ", ") : void 0;
|
|
437
448
|
const orderCols = ($order || [idCol]).map(
|
|
438
|
-
(orderItem) => orderItem[0] === "!" ? sql`-(${lookup(orderItem.slice(1))})::float8` : lookup(orderItem)
|
|
449
|
+
(orderItem) => orderItem[0] === "!" ? sql`-(${lookup(orderItem.slice(1), options)})::float8` : lookup(orderItem, options)
|
|
439
450
|
);
|
|
440
451
|
Object.entries({ $after, $before, $since, $until }).forEach(
|
|
441
452
|
([name, value]) => {
|
|
@@ -445,7 +456,7 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
|
|
|
445
456
|
);
|
|
446
457
|
const order = !$group && join(
|
|
447
458
|
($order || [idCol]).map(
|
|
448
|
-
(orderItem) => orderItem[0] === "!" ? sql`${lookup(orderItem.slice(1))} ${$last ? sql`ASC` : sql`DESC`}` : sql`${lookup(orderItem)} ${$last ? sql`DESC` : sql`ASC`}`
|
|
459
|
+
(orderItem) => orderItem[0] === "!" ? sql`${lookup(orderItem.slice(1), options)} ${$last ? sql`ASC` : sql`DESC`}` : sql`${lookup(orderItem, options)} ${$last ? sql`DESC` : sql`ASC`}`
|
|
449
460
|
),
|
|
450
461
|
", "
|
|
451
462
|
);
|
|
@@ -497,7 +508,7 @@ function selectByArgs(args, projection, options) {
|
|
|
497
508
|
const clampedLimit = Math.min(MAX_LIMIT, limit || MAX_LIMIT);
|
|
498
509
|
return sql`
|
|
499
510
|
SELECT
|
|
500
|
-
${getSelectCols(
|
|
511
|
+
${getSelectCols(options, projection)}, ${meta}
|
|
501
512
|
FROM "${raw(table)}"
|
|
502
513
|
${where.length ? sql`WHERE ${join(where, " AND ")}` : empty}
|
|
503
514
|
${group ? sql`GROUP BY ${group}` : empty}
|
|
@@ -509,7 +520,7 @@ function selectByIds(ids, projection, options) {
|
|
|
509
520
|
const { table, idCol } = options;
|
|
510
521
|
return sql`
|
|
511
522
|
SELECT
|
|
512
|
-
${getSelectCols(
|
|
523
|
+
${getSelectCols(options, projection)}, ${getIdMeta(options)}
|
|
513
524
|
FROM "${raw(table)}"
|
|
514
525
|
WHERE "${raw(idCol)}" IN (${join(ids)})
|
|
515
526
|
`;
|
|
@@ -542,7 +553,7 @@ function patch(object, arg, options) {
|
|
|
542
553
|
return sql`
|
|
543
554
|
UPDATE "${raw(table)}" SET ${getUpdates(row, options)}
|
|
544
555
|
WHERE ${where}
|
|
545
|
-
RETURNING ${getSelectCols()}, ${meta}`;
|
|
556
|
+
RETURNING ${getSelectCols(options)}, ${meta}`;
|
|
546
557
|
}
|
|
547
558
|
function put(object, arg, options) {
|
|
548
559
|
const { idCol, table } = options;
|
|
@@ -560,7 +571,7 @@ function put(object, arg, options) {
|
|
|
560
571
|
return sql`
|
|
561
572
|
INSERT INTO "${raw(table)}" (${cols}) VALUES (${vals})
|
|
562
573
|
ON CONFLICT (${conflictTarget}) DO UPDATE SET (${cols}) = (${vals})
|
|
563
|
-
RETURNING ${getSelectCols()}, ${meta}`;
|
|
574
|
+
RETURNING ${getSelectCols(options)}, ${meta}`;
|
|
564
575
|
}
|
|
565
576
|
function del(arg, options) {
|
|
566
577
|
const { table } = options;
|
package/index.mjs
CHANGED
|
@@ -204,24 +204,32 @@ const getJsonBuildValue = (value) => {
|
|
|
204
204
|
return sql`${value}::text`;
|
|
205
205
|
return sql`${JSON.stringify(stripAttributes(value))}::jsonb`;
|
|
206
206
|
};
|
|
207
|
-
const lookup = (prop) => {
|
|
207
|
+
const lookup = (prop, options) => {
|
|
208
208
|
const [prefix, ...suffix] = prop.split(".");
|
|
209
|
-
|
|
209
|
+
if (!suffix.length)
|
|
210
|
+
return sql`"${raw(prefix)}"`;
|
|
211
|
+
const { types: types2 } = options.schema;
|
|
212
|
+
if (types2[prefix] === "jsonb") {
|
|
213
|
+
return sql`"${raw(prefix)}" #> ${suffix}`;
|
|
214
|
+
} else if (types2[prefix] === "cube" && suffix.length === 1) {
|
|
215
|
+
return sql`"${raw(prefix)}" ~> ${parseInt(suffix[0])}`;
|
|
216
|
+
} else {
|
|
217
|
+
throw Error(`pg.cannot_lookup ${prop}`);
|
|
218
|
+
}
|
|
210
219
|
};
|
|
211
220
|
const lookupNumeric = (prop) => {
|
|
212
221
|
const [prefix, ...suffix] = prop.split(".");
|
|
213
|
-
return suffix.length ? sql`CASE WHEN "${raw(prefix)}" #> ${suffix} = 'null'::jsonb
|
|
214
|
-
|
|
215
|
-
)}" #> ${suffix})::numeric END` : sql`"${raw(prefix)}"`;
|
|
222
|
+
return suffix.length ? sql`CASE WHEN "${raw(prefix)}" #> ${suffix} = 'null'::jsonb
|
|
223
|
+
THEN 0 ELSE ("${raw(prefix)}" #> ${suffix})::numeric END` : sql`"${raw(prefix)}"`;
|
|
216
224
|
};
|
|
217
225
|
const aggSql = {
|
|
218
226
|
$sum: (prop) => sql`sum((${lookupNumeric(prop)})::numeric)`,
|
|
219
|
-
$card: (prop) => sql`count(distinct(${lookup(prop)}))`,
|
|
227
|
+
$card: (prop, options) => sql`count(distinct(${lookup(prop, options)}))`,
|
|
220
228
|
$avg: (prop) => sql`avg((${lookupNumeric(prop)})::numeric)`,
|
|
221
229
|
$max: (prop) => sql`max((${lookupNumeric(prop)})::numeric)`,
|
|
222
230
|
$min: (prop) => sql`min((${lookupNumeric(prop)})::numeric)`
|
|
223
231
|
};
|
|
224
|
-
const getSelectCols = (
|
|
232
|
+
const getSelectCols = (options, projection = null) => {
|
|
225
233
|
if (!projection)
|
|
226
234
|
return sql`*`;
|
|
227
235
|
const sqls = [];
|
|
@@ -231,7 +239,7 @@ const getSelectCols = (table, projection = null) => {
|
|
|
231
239
|
} else if (aggSql[key]) {
|
|
232
240
|
const subSqls = [];
|
|
233
241
|
for (const prop in projection[key]) {
|
|
234
|
-
subSqls.push(sql`${prop}::text, ${aggSql[key](prop)}`);
|
|
242
|
+
subSqls.push(sql`${prop}::text, ${aggSql[key](prop, options)}`);
|
|
235
243
|
}
|
|
236
244
|
sqls.push(
|
|
237
245
|
sql`jsonb_build_object(${join(subSqls, ", ")}) AS "${raw(key)}"`
|
|
@@ -315,14 +323,17 @@ function stripAttributes(object) {
|
|
|
315
323
|
if (Array.isArray(object)) {
|
|
316
324
|
return object.map((item) => stripAttributes(item));
|
|
317
325
|
}
|
|
318
|
-
return Object.entries(object).reduce(
|
|
319
|
-
|
|
326
|
+
return Object.entries(object).reduce(
|
|
327
|
+
(out, [key, val]) => {
|
|
328
|
+
if (key === "$put" || val === null)
|
|
329
|
+
return out;
|
|
330
|
+
if (out === null)
|
|
331
|
+
out = {};
|
|
332
|
+
out[key] = stripAttributes(val);
|
|
320
333
|
return out;
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
return out;
|
|
325
|
-
}, null);
|
|
334
|
+
},
|
|
335
|
+
null
|
|
336
|
+
);
|
|
326
337
|
}
|
|
327
338
|
const opSql = {
|
|
328
339
|
$and: "AND",
|
|
@@ -430,10 +441,10 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
|
|
|
430
441
|
where.push(getSql(filter, options));
|
|
431
442
|
if (!hasRangeArg)
|
|
432
443
|
return { meta: meta(baseKey), where, limit: 1 };
|
|
433
|
-
const groupCols = Array.isArray($group) && $group.length && $group.map(lookup);
|
|
444
|
+
const groupCols = Array.isArray($group) && $group.length && $group.map((prop) => lookup(prop, options));
|
|
434
445
|
const group = groupCols ? join(groupCols, ", ") : void 0;
|
|
435
446
|
const orderCols = ($order || [idCol]).map(
|
|
436
|
-
(orderItem) => orderItem[0] === "!" ? sql`-(${lookup(orderItem.slice(1))})::float8` : lookup(orderItem)
|
|
447
|
+
(orderItem) => orderItem[0] === "!" ? sql`-(${lookup(orderItem.slice(1), options)})::float8` : lookup(orderItem, options)
|
|
437
448
|
);
|
|
438
449
|
Object.entries({ $after, $before, $since, $until }).forEach(
|
|
439
450
|
([name, value]) => {
|
|
@@ -443,7 +454,7 @@ function getArgSql({ $first, $last, $after, $before, $since, $until, $all, $curs
|
|
|
443
454
|
);
|
|
444
455
|
const order = !$group && join(
|
|
445
456
|
($order || [idCol]).map(
|
|
446
|
-
(orderItem) => orderItem[0] === "!" ? sql`${lookup(orderItem.slice(1))} ${$last ? sql`ASC` : sql`DESC`}` : sql`${lookup(orderItem)} ${$last ? sql`DESC` : sql`ASC`}`
|
|
457
|
+
(orderItem) => orderItem[0] === "!" ? sql`${lookup(orderItem.slice(1), options)} ${$last ? sql`ASC` : sql`DESC`}` : sql`${lookup(orderItem, options)} ${$last ? sql`DESC` : sql`ASC`}`
|
|
447
458
|
),
|
|
448
459
|
", "
|
|
449
460
|
);
|
|
@@ -495,7 +506,7 @@ function selectByArgs(args, projection, options) {
|
|
|
495
506
|
const clampedLimit = Math.min(MAX_LIMIT, limit || MAX_LIMIT);
|
|
496
507
|
return sql`
|
|
497
508
|
SELECT
|
|
498
|
-
${getSelectCols(
|
|
509
|
+
${getSelectCols(options, projection)}, ${meta}
|
|
499
510
|
FROM "${raw(table)}"
|
|
500
511
|
${where.length ? sql`WHERE ${join(where, " AND ")}` : empty}
|
|
501
512
|
${group ? sql`GROUP BY ${group}` : empty}
|
|
@@ -507,7 +518,7 @@ function selectByIds(ids, projection, options) {
|
|
|
507
518
|
const { table, idCol } = options;
|
|
508
519
|
return sql`
|
|
509
520
|
SELECT
|
|
510
|
-
${getSelectCols(
|
|
521
|
+
${getSelectCols(options, projection)}, ${getIdMeta(options)}
|
|
511
522
|
FROM "${raw(table)}"
|
|
512
523
|
WHERE "${raw(idCol)}" IN (${join(ids)})
|
|
513
524
|
`;
|
|
@@ -540,7 +551,7 @@ function patch(object, arg, options) {
|
|
|
540
551
|
return sql`
|
|
541
552
|
UPDATE "${raw(table)}" SET ${getUpdates(row, options)}
|
|
542
553
|
WHERE ${where}
|
|
543
|
-
RETURNING ${getSelectCols()}, ${meta}`;
|
|
554
|
+
RETURNING ${getSelectCols(options)}, ${meta}`;
|
|
544
555
|
}
|
|
545
556
|
function put(object, arg, options) {
|
|
546
557
|
const { idCol, table } = options;
|
|
@@ -558,7 +569,7 @@ function put(object, arg, options) {
|
|
|
558
569
|
return sql`
|
|
559
570
|
INSERT INTO "${raw(table)}" (${cols}) VALUES (${vals})
|
|
560
571
|
ON CONFLICT (${conflictTarget}) DO UPDATE SET (${cols}) = (${vals})
|
|
561
|
-
RETURNING ${getSelectCols()}, ${meta}`;
|
|
572
|
+
RETURNING ${getSelectCols(options)}, ${meta}`;
|
|
562
573
|
}
|
|
563
574
|
function del(arg, options) {
|
|
564
575
|
const { table } = options;
|
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.2",
|
|
5
|
+
"version": "0.16.3-alpha.2",
|
|
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.2",
|
|
19
|
+
"@graffy/common": "0.16.3-alpha.2",
|
|
20
20
|
"debug": "^4.3.3"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
package/types/sql/clauses.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export function cubeLiteralSql(value: any): Sql;
|
|
2
2
|
export function getJsonBuildTrusted(variadic: any): Sql;
|
|
3
|
-
export function lookup(prop: any): Sql;
|
|
3
|
+
export function lookup(prop: any, options: any): Sql;
|
|
4
4
|
export function lookupNumeric(prop: any): Sql;
|
|
5
|
-
export function getSelectCols(
|
|
5
|
+
export function getSelectCols(options: any, projection?: any): Sql;
|
|
6
6
|
export function getInsert(row: any, options: any): {
|
|
7
7
|
cols: Sql;
|
|
8
8
|
vals: Sql;
|