@graffy/pg 0.15.23-alpha.1 → 0.15.24

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 CHANGED
@@ -27,7 +27,8 @@ var __objRest = (source, exclude) => {
27
27
  }
28
28
  return target;
29
29
  };
30
- Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports[Symbol.toStringTag] = "Module";
31
32
  var common = require("@graffy/common");
32
33
  var pg$1 = require("pg");
33
34
  var sql = require("sql-template-tag");
@@ -174,12 +175,16 @@ const lookup = (prop) => {
174
175
  const [prefix, ...suffix] = common.encodePath(prop);
175
176
  return suffix.length ? sql__default["default"]`"${sql.raw(prefix)}" #> ${suffix}` : sql__default["default"]`"${sql.raw(prefix)}"`;
176
177
  };
178
+ const lookupNumeric = (prop) => {
179
+ const [prefix, ...suffix] = common.encodePath(prop);
180
+ return suffix.length ? sql__default["default"]`CASE WHEN "${sql.raw(prefix)}" #> ${suffix} = 'null'::jsonb THEN 0 ELSE ("${sql.raw(prefix)}" #> ${suffix})::numeric END` : sql__default["default"]`"${sql.raw(prefix)}"`;
181
+ };
177
182
  const aggSql = {
178
- $sum: (prop) => sql__default["default"]`sum((${lookup(prop)})::numeric)`,
183
+ $sum: (prop) => sql__default["default"]`sum((${lookupNumeric(prop)})::numeric)`,
179
184
  $card: (prop) => sql__default["default"]`count(distinct(${lookup(prop)}))`,
180
- $avg: (prop) => sql__default["default"]`sum((${lookup(prop)})::numeric)`,
181
- $max: (prop) => sql__default["default"]`sum((${lookup(prop)})::numeric)`,
182
- $min: (prop) => sql__default["default"]`sum((${lookup(prop)})::numeric)`
185
+ $avg: (prop) => sql__default["default"]`avg((${lookupNumeric(prop)})::numeric)`,
186
+ $max: (prop) => sql__default["default"]`max((${lookupNumeric(prop)})::numeric)`,
187
+ $min: (prop) => sql__default["default"]`min((${lookupNumeric(prop)})::numeric)`
183
188
  };
184
189
  const getSelectCols = (table, projection = null) => {
185
190
  if (!projection)
@@ -299,6 +304,9 @@ function getBinarySql(lhs, type, op, value, textLhs) {
299
304
  return sql__default["default"]`${lhs} ${sqlOp} ${String(value)}`;
300
305
  }
301
306
  if (type === "jsonb") {
307
+ if (typeof value === "string") {
308
+ return sql__default["default"]`${textLhs} ${sqlOp} ${value}`;
309
+ }
302
310
  return sql__default["default"]`${lhs} ${sqlOp} ${JSON.stringify(value)}::jsonb`;
303
311
  }
304
312
  if (type === "cube")
@@ -342,40 +350,40 @@ const getArgMeta = (key, { prefix, idCol, verDefault }) => getJsonBuildTrusted({
342
350
  $ref: sql__default["default"]`jsonb_build_array(${sql.join(prefix.map((k) => sql__default["default"]`${k}::text`))}, "${sql.raw(idCol)}")`,
343
351
  $ver: sql.raw(verDefault)
344
352
  });
345
- const getAggMeta = (key, $group, { verDefault }) => getJsonBuildTrusted({
346
- $key: sql.join([key, getJsonBuildTrusted({ $group })].filter(Boolean), " || "),
353
+ const getAggMeta = (key, { verDefault }) => getJsonBuildTrusted({
354
+ $key: key,
347
355
  $ver: sql.raw(verDefault)
348
356
  });
349
357
  function getArgSql(_a, options) {
350
358
  var _b = _a, { $first, $last, $after, $before, $since, $until, $all, $cursor: _ } = _b, rest = __objRest(_b, ["$first", "$last", "$after", "$before", "$since", "$until", "$all", "$cursor"]);
351
359
  const _a2 = rest, { $order, $group } = _a2, filter = __objRest(_a2, ["$order", "$group"]);
352
360
  const { prefix, idCol } = options;
361
+ const meta = (key2) => $group ? getAggMeta(key2, options) : getArgMeta(key2, options);
362
+ const hasRangeArg = $before || $after || $since || $until || $first || $last || $all;
353
363
  if ($order && $group) {
354
364
  throw Error("pg_arg.order_and_group_unsupported in " + prefix);
355
365
  }
356
- const meta = (key2) => $group ? getAggMeta(key2, $group, options) : getArgMeta(key2, options);
357
- const groupCols = Array.isArray($group) && $group.length && $group.map(lookup);
358
- const group = groupCols ? sql.join(groupCols, ", ") : void 0;
359
- const hasRangeArg = $before || $after || $since || $until || $first || $last || $all || $order;
360
- let key;
366
+ if (($order || $group && $group !== true) && !hasRangeArg) {
367
+ throw Error("pg_arg.range_arg_expected in " + prefix);
368
+ }
369
+ const baseKey = sql__default["default"]`${JSON.stringify(rest)}::jsonb`;
361
370
  const where = [];
362
- if (!common.isEmpty(filter)) {
371
+ if (!common.isEmpty(filter))
363
372
  where.push(getSql(filter, options));
364
- key = sql__default["default"]`${JSON.stringify(filter)}::jsonb`;
365
- }
366
373
  if (!hasRangeArg)
367
- return { meta: meta(key), where, group, limit: 1 };
374
+ return { meta: meta(baseKey), where, limit: 1 };
375
+ const groupCols = Array.isArray($group) && $group.length && $group.map(lookup);
376
+ const group = groupCols ? sql.join(groupCols, ", ") : void 0;
368
377
  const orderCols = ($order || [idCol]).map((orderItem) => orderItem[0] === "!" ? sql__default["default"]`-(${lookup(orderItem.slice(1))})::float8` : lookup(orderItem));
369
378
  Object.entries({ $after, $before, $since, $until }).forEach(([name, value]) => {
370
379
  if (value)
371
380
  where.push(getBoundCond(orderCols, value, name));
372
381
  });
373
382
  const order = !$group && sql.join(($order || [idCol]).map((orderItem) => orderItem[0] === "!" ? sql__default["default"]`${lookup(orderItem.slice(1))} ${$last ? sql__default["default"]`ASC` : sql__default["default"]`DESC`}` : sql__default["default"]`${lookup(orderItem)} ${$last ? sql__default["default"]`DESC` : sql__default["default"]`ASC`}`), `, `);
374
- const orderKey = $order && getJsonBuildTrusted({ $order: sql__default["default"]`${JSON.stringify($order)}::jsonb` });
375
383
  const cursorKey = getJsonBuildTrusted({
376
- $cursor: sql__default["default"]`jsonb_build_array(${sql.join(groupCols || orderCols)})`
384
+ $cursor: $group === true ? sql__default["default"]`''` : sql__default["default"]`jsonb_build_array(${sql.join(groupCols || orderCols)})`
377
385
  });
378
- key = sql__default["default"]`(${sql.join([key, orderKey, cursorKey].filter(Boolean), ` || `)})`;
386
+ const key = sql__default["default"]`(${baseKey} || ${cursorKey})`;
379
387
  return {
380
388
  meta: meta(key),
381
389
  where,
package/index.mjs CHANGED
@@ -167,12 +167,16 @@ const lookup = (prop) => {
167
167
  const [prefix, ...suffix] = encodePath(prop);
168
168
  return suffix.length ? sql`"${raw(prefix)}" #> ${suffix}` : sql`"${raw(prefix)}"`;
169
169
  };
170
+ const lookupNumeric = (prop) => {
171
+ const [prefix, ...suffix] = encodePath(prop);
172
+ return suffix.length ? sql`CASE WHEN "${raw(prefix)}" #> ${suffix} = 'null'::jsonb THEN 0 ELSE ("${raw(prefix)}" #> ${suffix})::numeric END` : sql`"${raw(prefix)}"`;
173
+ };
170
174
  const aggSql = {
171
- $sum: (prop) => sql`sum((${lookup(prop)})::numeric)`,
175
+ $sum: (prop) => sql`sum((${lookupNumeric(prop)})::numeric)`,
172
176
  $card: (prop) => sql`count(distinct(${lookup(prop)}))`,
173
- $avg: (prop) => sql`sum((${lookup(prop)})::numeric)`,
174
- $max: (prop) => sql`sum((${lookup(prop)})::numeric)`,
175
- $min: (prop) => sql`sum((${lookup(prop)})::numeric)`
177
+ $avg: (prop) => sql`avg((${lookupNumeric(prop)})::numeric)`,
178
+ $max: (prop) => sql`max((${lookupNumeric(prop)})::numeric)`,
179
+ $min: (prop) => sql`min((${lookupNumeric(prop)})::numeric)`
176
180
  };
177
181
  const getSelectCols = (table, projection = null) => {
178
182
  if (!projection)
@@ -292,6 +296,9 @@ function getBinarySql(lhs, type, op, value, textLhs) {
292
296
  return sql`${lhs} ${sqlOp} ${String(value)}`;
293
297
  }
294
298
  if (type === "jsonb") {
299
+ if (typeof value === "string") {
300
+ return sql`${textLhs} ${sqlOp} ${value}`;
301
+ }
295
302
  return sql`${lhs} ${sqlOp} ${JSON.stringify(value)}::jsonb`;
296
303
  }
297
304
  if (type === "cube")
@@ -335,40 +342,40 @@ const getArgMeta = (key, { prefix, idCol, verDefault }) => getJsonBuildTrusted({
335
342
  $ref: sql`jsonb_build_array(${join(prefix.map((k) => sql`${k}::text`))}, "${raw(idCol)}")`,
336
343
  $ver: raw(verDefault)
337
344
  });
338
- const getAggMeta = (key, $group, { verDefault }) => getJsonBuildTrusted({
339
- $key: join([key, getJsonBuildTrusted({ $group })].filter(Boolean), " || "),
345
+ const getAggMeta = (key, { verDefault }) => getJsonBuildTrusted({
346
+ $key: key,
340
347
  $ver: raw(verDefault)
341
348
  });
342
349
  function getArgSql(_a, options) {
343
350
  var _b = _a, { $first, $last, $after, $before, $since, $until, $all, $cursor: _ } = _b, rest = __objRest(_b, ["$first", "$last", "$after", "$before", "$since", "$until", "$all", "$cursor"]);
344
351
  const _a2 = rest, { $order, $group } = _a2, filter = __objRest(_a2, ["$order", "$group"]);
345
352
  const { prefix, idCol } = options;
353
+ const meta = (key2) => $group ? getAggMeta(key2, options) : getArgMeta(key2, options);
354
+ const hasRangeArg = $before || $after || $since || $until || $first || $last || $all;
346
355
  if ($order && $group) {
347
356
  throw Error("pg_arg.order_and_group_unsupported in " + prefix);
348
357
  }
349
- const meta = (key2) => $group ? getAggMeta(key2, $group, options) : getArgMeta(key2, options);
350
- const groupCols = Array.isArray($group) && $group.length && $group.map(lookup);
351
- const group = groupCols ? join(groupCols, ", ") : void 0;
352
- const hasRangeArg = $before || $after || $since || $until || $first || $last || $all || $order;
353
- let key;
358
+ if (($order || $group && $group !== true) && !hasRangeArg) {
359
+ throw Error("pg_arg.range_arg_expected in " + prefix);
360
+ }
361
+ const baseKey = sql`${JSON.stringify(rest)}::jsonb`;
354
362
  const where = [];
355
- if (!isEmpty(filter)) {
363
+ if (!isEmpty(filter))
356
364
  where.push(getSql(filter, options));
357
- key = sql`${JSON.stringify(filter)}::jsonb`;
358
- }
359
365
  if (!hasRangeArg)
360
- return { meta: meta(key), where, group, limit: 1 };
366
+ return { meta: meta(baseKey), where, limit: 1 };
367
+ const groupCols = Array.isArray($group) && $group.length && $group.map(lookup);
368
+ const group = groupCols ? join(groupCols, ", ") : void 0;
361
369
  const orderCols = ($order || [idCol]).map((orderItem) => orderItem[0] === "!" ? sql`-(${lookup(orderItem.slice(1))})::float8` : lookup(orderItem));
362
370
  Object.entries({ $after, $before, $since, $until }).forEach(([name, value]) => {
363
371
  if (value)
364
372
  where.push(getBoundCond(orderCols, value, name));
365
373
  });
366
374
  const order = !$group && join(($order || [idCol]).map((orderItem) => orderItem[0] === "!" ? sql`${lookup(orderItem.slice(1))} ${$last ? sql`ASC` : sql`DESC`}` : sql`${lookup(orderItem)} ${$last ? sql`DESC` : sql`ASC`}`), `, `);
367
- const orderKey = $order && getJsonBuildTrusted({ $order: sql`${JSON.stringify($order)}::jsonb` });
368
375
  const cursorKey = getJsonBuildTrusted({
369
- $cursor: sql`jsonb_build_array(${join(groupCols || orderCols)})`
376
+ $cursor: $group === true ? sql`''` : sql`jsonb_build_array(${join(groupCols || orderCols)})`
370
377
  });
371
- key = sql`(${join([key, orderKey, cursorKey].filter(Boolean), ` || `)})`;
378
+ const key = sql`(${baseKey} || ${cursorKey})`;
372
379
  return {
373
380
  meta: meta(key),
374
381
  where,
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.15.23-alpha.1",
5
+ "version": "0.15.24",
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.15.23-alpha.1",
19
+ "@graffy/common": "0.15.24",
20
20
  "sql-template-tag": "^4.1.0",
21
21
  "debug": "^4.3.3"
22
22
  },
@@ -1,6 +1,7 @@
1
1
  export function cubeLiteralSql(value: any): Sql;
2
2
  export function getJsonBuildTrusted(variadic: any): Sql;
3
3
  export function lookup(prop: any): Sql;
4
+ export function lookupNumeric(prop: any): Sql;
4
5
  export function getSelectCols(table: any, projection?: any): Sql;
5
6
  export function getInsert(row: any, options: any): {
6
7
  cols: Sql;
@@ -2,7 +2,7 @@
2
2
  Uses the args object (typically passed in the $key attribute)
3
3
 
4
4
  @param {object} args
5
- @param {{prefix: string, idCol: string}} options
5
+ @param {{prefix: string, idCol: string, verDefault: string}} options
6
6
 
7
7
  @typedef { import('sql-template-tag').Sql } Sql
8
8
  @return {{ meta: Sql, where: Sql[], order?: Sql, group?: Sql, limit: number }}
@@ -10,6 +10,7 @@
10
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
+ verDefault: string;
13
14
  }): {
14
15
  meta: Sql;
15
16
  where: Sql[];
@@ -7,6 +7,6 @@ export function getArgMeta(key: any, { prefix, idCol, verDefault }: {
7
7
  idCol: any;
8
8
  verDefault: any;
9
9
  }): import("sql-template-tag").Sql;
10
- export function getAggMeta(key: any, $group: any, { verDefault }: {
10
+ export function getAggMeta(key: any, { verDefault }: {
11
11
  verDefault: any;
12
12
  }): import("sql-template-tag").Sql;