@graffy/pg 0.15.14 → 0.15.15-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 CHANGED
@@ -159,7 +159,7 @@ function simplify(node) {
159
159
  return node;
160
160
  }
161
161
  const nowTimestamp = sql__default["default"]`cast(extract(epoch from now()) as integer)`;
162
- const getJsonBuildObject = (variadic) => {
162
+ const getJsonBuildTrusted = (variadic) => {
163
163
  const args = sql.join(Object.entries(variadic).map(([name, value]) => {
164
164
  return sql__default["default"]`'${sql.raw(name)}', ${getJsonBuildValue(value)}`;
165
165
  }));
@@ -205,7 +205,7 @@ const getSelectCols = (table, projection = null) => {
205
205
  function vertexSql(array) {
206
206
  return sql__default["default"]`array[${sql.join(array.map((num) => num === Infinity ? sql__default["default"]`'Infinity'` : num === -Infinity ? sql__default["default"]`'-Infinity'` : num))}]::float8[]`;
207
207
  }
208
- function castValue$1(value, type, name, isPut) {
208
+ function castValue(value, type, name, isPut) {
209
209
  if (!type)
210
210
  throw Error("pg.write_no_column " + name);
211
211
  if (value instanceof sql.Sql)
@@ -228,24 +228,24 @@ const getInsert = (row, options) => {
228
228
  const vals = [];
229
229
  Object.entries(row).filter(([col]) => col !== options.verCol && col[0] !== "$").concat([[options.verCol, nowTimestamp]]).forEach(([col, val]) => {
230
230
  cols.push(sql__default["default"]`"${sql.raw(col)}"`);
231
- vals.push(castValue$1(val, options.schema.types[col], col, row.$put));
231
+ vals.push(castValue(val, options.schema.types[col], col, row.$put));
232
232
  });
233
233
  return { cols: sql.join(cols, ", "), vals: sql.join(vals, ", ") };
234
234
  };
235
235
  const getUpdates = (row, options) => {
236
- return sql.join(Object.entries(row).filter(([col]) => col !== options.idCol && col[0] !== "$").map(([col, val]) => sql__default["default"]`"${sql.raw(col)}" = ${castValue$1(val, options.schema.types[col], col, row.$put)}`).concat(sql__default["default"]`"${sql.raw(options.verCol)}" = ${nowTimestamp}`), ", ");
236
+ return sql.join(Object.entries(row).filter(([col]) => col !== options.idCol && col[0] !== "$").map(([col, val]) => sql__default["default"]`"${sql.raw(col)}" = ${castValue(val, options.schema.types[col], col, row.$put)}`).concat(sql__default["default"]`"${sql.raw(options.verCol)}" = ${nowTimestamp}`), ", ");
237
237
  };
238
- function getJsonUpdate(_a, col, path) {
239
- var _b = _a, { $put } = _b, object = __objRest(_b, ["$put"]);
240
- if ($put)
241
- return JSON.stringify(object);
238
+ function getJsonUpdate(object, col, path) {
239
+ if (!object || typeof object !== "object" || Array.isArray(object) || object.$put) {
240
+ return getJsonBuildValue(object);
241
+ }
242
242
  const curr = sql__default["default"]`"${sql.raw(col)}"${path.length ? sql__default["default"]`#>${path}` : sql.empty}`;
243
243
  if (common.isEmpty(object))
244
244
  return curr;
245
245
  return sql__default["default"]`(case jsonb_typeof(${curr})
246
246
  when 'object' then ${curr}
247
247
  else '{}'::jsonb
248
- end) || jsonb_build_object(${sql.join(Object.entries(object).map(([key, value]) => sql__default["default"]`${key}::text, ${typeof value === "object" && value && !Array.isArray(value) ? getJsonUpdate(value, col, path.concat(key)) : sql__default["default"]`${getJsonBuildValue(value)}`}`), ", ")})`;
248
+ end) || jsonb_build_object(${sql.join(Object.entries(object).map(([key, value]) => sql__default["default"]`${key}::text, ${getJsonUpdate(value, col, path.concat(key))}`), ", ")})`;
249
249
  }
250
250
  function stripAttributes(object) {
251
251
  if (typeof object !== "object" || !object)
@@ -277,27 +277,31 @@ const opSql = {
277
277
  $cts: sql__default["default"]`@>`,
278
278
  $ctd: sql__default["default"]`<@`
279
279
  };
280
- function castValue(value, type, op) {
280
+ function getBinarySql(lhs, type, op, value) {
281
281
  if (value === null && op === "$eq")
282
- return sql__default["default"]`IS NULL`;
282
+ return sql__default["default"]`${lhs} IS NULL`;
283
283
  if (value === null && op === "$neq")
284
- return sql__default["default"]`IS NOT NULL`;
284
+ return sql__default["default"]`${lhs} IS NOT NULL`;
285
285
  const sqlOp = opSql[op];
286
286
  if (!sqlOp)
287
287
  throw Error("pg.getSql_unknown_operator " + op);
288
288
  if (op === "$in" || op === "$nin") {
289
- return sql__default["default"]`${sqlOp} (${sql.join(value)})`;
289
+ return sql__default["default"]`${lhs} ${sqlOp} (${sql.join(value)})`;
290
+ }
291
+ if (op === "$re" || op === "$ire") {
292
+ const castLhs = type === "text" ? lhs : sql__default["default"]`(${lhs})::text`;
293
+ return sql__default["default"]`${castLhs} ${sqlOp} ${String(value)}`;
290
294
  }
291
295
  if (type === "jsonb") {
292
- return sql__default["default"]`${sqlOp} ${JSON.stringify(value)}::jsonb`;
296
+ return sql__default["default"]`${lhs} ${sqlOp} ${JSON.stringify(value)}::jsonb`;
293
297
  }
294
298
  if (type === "cube") {
295
299
  if (!Array.isArray(value) || !value.length || Array.isArray(value[0]) && value.length !== 2) {
296
- throw Error("pg.castValue_bad_cube" + JSON.stringify(value));
300
+ throw Error("pg.getBinarySql_bad_cube" + JSON.stringify(value));
297
301
  }
298
- return Array.isArray(value[0]) ? sql__default["default"]`${sqlOp} cube(${vertexSql(value[0])}, ${vertexSql(value[1])})` : sql__default["default"]`${sqlOp} cube(${vertexSql(value)})`;
302
+ return Array.isArray(value[0]) ? sql__default["default"]`${lhs} ${sqlOp} cube(${vertexSql(value[0])}, ${vertexSql(value[1])})` : sql__default["default"]`${lhs} ${sqlOp} cube(${vertexSql(value)})`;
299
303
  }
300
- return sql__default["default"]`${sqlOp} ${value}`;
304
+ return sql__default["default"]`${lhs} ${sqlOp} ${value}`;
301
305
  }
302
306
  function getSql(filter, options) {
303
307
  function getNodeSql(ast) {
@@ -317,27 +321,26 @@ function getSql(filter, options) {
317
321
  throw Error("pg.lookup_not_jsonb " + prefix);
318
322
  }
319
323
  const [lhs, type] = suffix.length ? [sql__default["default"]`"${sql.raw(prefix)}" #> ${suffix}`, "jsonb"] : [sql__default["default"]`"${sql.raw(prefix)}"`, types[prefix]];
320
- const rhs = castValue(ast[2], type, op);
321
- return sql__default["default"]`${lhs} ${rhs}`;
324
+ return getBinarySql(lhs, type, op, ast[2]);
322
325
  }
323
326
  return getNodeSql(getAst(filter));
324
327
  }
325
- const getIdMeta = ({ idCol }) => getJsonBuildObject({
328
+ const getIdMeta = ({ idCol }) => getJsonBuildTrusted({
326
329
  $key: sql__default["default"]`"${sql.raw(idCol)}"`,
327
330
  $ver: nowTimestamp
328
331
  });
329
- const getArgMeta = (key, prefix, idCol) => getJsonBuildObject({
332
+ const getArgMeta = (key, prefix, idCol) => getJsonBuildTrusted({
330
333
  $key: key,
331
334
  $ref: sql__default["default"]`jsonb_build_array(${sql.join(prefix.map((k) => sql__default["default"]`${k}::text`))}, "${sql.raw(idCol)}")`,
332
335
  $ver: nowTimestamp
333
336
  });
334
- const getAggMeta = (key, $group) => getJsonBuildObject({
335
- $key: sql.join([key, getJsonBuildObject({ $group })].filter(Boolean), " || "),
337
+ const getAggMeta = (key, $group) => getJsonBuildTrusted({
338
+ $key: sql.join([key, getJsonBuildTrusted({ $group })].filter(Boolean), " || "),
336
339
  $ver: nowTimestamp
337
340
  });
338
- function getArgSql(_c, options) {
339
- var _d = _c, { $first, $last, $after, $before, $since, $until, $all, $cursor: _ } = _d, rest = __objRest(_d, ["$first", "$last", "$after", "$before", "$since", "$until", "$all", "$cursor"]);
340
- const _a = rest, { $order, $group } = _a, filter = __objRest(_a, ["$order", "$group"]);
341
+ function getArgSql(_a, options) {
342
+ var _b = _a, { $first, $last, $after, $before, $since, $until, $all, $cursor: _ } = _b, rest = __objRest(_b, ["$first", "$last", "$after", "$before", "$since", "$until", "$all", "$cursor"]);
343
+ const _a2 = rest, { $order, $group } = _a2, filter = __objRest(_a2, ["$order", "$group"]);
341
344
  const { prefix, idCol } = options;
342
345
  if ($order && $group) {
343
346
  throw Error("pg_arg.order_and_group_unsupported in " + prefix);
@@ -362,8 +365,8 @@ function getArgSql(_c, options) {
362
365
  if (value)
363
366
  where.push(getBoundCond(orderCols, value, name));
364
367
  });
365
- const orderQuery = $order && getJsonBuildObject({ $order: sql__default["default"]`${JSON.stringify($order)}::jsonb` });
366
- const cursorQuery = getJsonBuildObject({
368
+ const orderQuery = $order && getJsonBuildTrusted({ $order: sql__default["default"]`${JSON.stringify($order)}::jsonb` });
369
+ const cursorQuery = getJsonBuildTrusted({
367
370
  $cursor: sql__default["default"]`jsonb_build_array(${sql.join(groupCols || orderCols)})`
368
371
  });
369
372
  key = sql__default["default"]`(${sql.join([key, orderQuery, cursorQuery].filter(Boolean), ` || `)})`;
@@ -481,7 +484,7 @@ function del(arg, options) {
481
484
  return sql__default["default"]`
482
485
  DELETE FROM "${sql.raw(table)}"
483
486
  WHERE ${where}
484
- RETURNING (${getJsonBuildObject({ $key: arg })})`;
487
+ RETURNING (${getJsonBuildTrusted({ $key: arg })})`;
485
488
  }
486
489
  const log = debug__default["default"]("graffy:pg:db");
487
490
  class Db {
package/index.mjs CHANGED
@@ -151,7 +151,7 @@ function simplify(node) {
151
151
  return node;
152
152
  }
153
153
  const nowTimestamp = sql`cast(extract(epoch from now()) as integer)`;
154
- const getJsonBuildObject = (variadic) => {
154
+ const getJsonBuildTrusted = (variadic) => {
155
155
  const args = join(Object.entries(variadic).map(([name, value]) => {
156
156
  return sql`'${raw(name)}', ${getJsonBuildValue(value)}`;
157
157
  }));
@@ -197,7 +197,7 @@ const getSelectCols = (table, projection = null) => {
197
197
  function vertexSql(array) {
198
198
  return sql`array[${join(array.map((num) => num === Infinity ? sql`'Infinity'` : num === -Infinity ? sql`'-Infinity'` : num))}]::float8[]`;
199
199
  }
200
- function castValue$1(value, type, name, isPut) {
200
+ function castValue(value, type, name, isPut) {
201
201
  if (!type)
202
202
  throw Error("pg.write_no_column " + name);
203
203
  if (value instanceof Sql)
@@ -220,24 +220,24 @@ const getInsert = (row, options) => {
220
220
  const vals = [];
221
221
  Object.entries(row).filter(([col]) => col !== options.verCol && col[0] !== "$").concat([[options.verCol, nowTimestamp]]).forEach(([col, val]) => {
222
222
  cols.push(sql`"${raw(col)}"`);
223
- vals.push(castValue$1(val, options.schema.types[col], col, row.$put));
223
+ vals.push(castValue(val, options.schema.types[col], col, row.$put));
224
224
  });
225
225
  return { cols: join(cols, ", "), vals: join(vals, ", ") };
226
226
  };
227
227
  const getUpdates = (row, options) => {
228
- return join(Object.entries(row).filter(([col]) => col !== options.idCol && col[0] !== "$").map(([col, val]) => sql`"${raw(col)}" = ${castValue$1(val, options.schema.types[col], col, row.$put)}`).concat(sql`"${raw(options.verCol)}" = ${nowTimestamp}`), ", ");
228
+ return join(Object.entries(row).filter(([col]) => col !== options.idCol && col[0] !== "$").map(([col, val]) => sql`"${raw(col)}" = ${castValue(val, options.schema.types[col], col, row.$put)}`).concat(sql`"${raw(options.verCol)}" = ${nowTimestamp}`), ", ");
229
229
  };
230
- function getJsonUpdate(_a, col, path) {
231
- var _b = _a, { $put } = _b, object = __objRest(_b, ["$put"]);
232
- if ($put)
233
- return JSON.stringify(object);
230
+ function getJsonUpdate(object, col, path) {
231
+ if (!object || typeof object !== "object" || Array.isArray(object) || object.$put) {
232
+ return getJsonBuildValue(object);
233
+ }
234
234
  const curr = sql`"${raw(col)}"${path.length ? sql`#>${path}` : empty}`;
235
235
  if (isEmpty(object))
236
236
  return curr;
237
237
  return sql`(case jsonb_typeof(${curr})
238
238
  when 'object' then ${curr}
239
239
  else '{}'::jsonb
240
- end) || jsonb_build_object(${join(Object.entries(object).map(([key, value]) => sql`${key}::text, ${typeof value === "object" && value && !Array.isArray(value) ? getJsonUpdate(value, col, path.concat(key)) : sql`${getJsonBuildValue(value)}`}`), ", ")})`;
240
+ end) || jsonb_build_object(${join(Object.entries(object).map(([key, value]) => sql`${key}::text, ${getJsonUpdate(value, col, path.concat(key))}`), ", ")})`;
241
241
  }
242
242
  function stripAttributes(object) {
243
243
  if (typeof object !== "object" || !object)
@@ -269,27 +269,31 @@ const opSql = {
269
269
  $cts: sql`@>`,
270
270
  $ctd: sql`<@`
271
271
  };
272
- function castValue(value, type, op) {
272
+ function getBinarySql(lhs, type, op, value) {
273
273
  if (value === null && op === "$eq")
274
- return sql`IS NULL`;
274
+ return sql`${lhs} IS NULL`;
275
275
  if (value === null && op === "$neq")
276
- return sql`IS NOT NULL`;
276
+ return sql`${lhs} IS NOT NULL`;
277
277
  const sqlOp = opSql[op];
278
278
  if (!sqlOp)
279
279
  throw Error("pg.getSql_unknown_operator " + op);
280
280
  if (op === "$in" || op === "$nin") {
281
- return sql`${sqlOp} (${join(value)})`;
281
+ return sql`${lhs} ${sqlOp} (${join(value)})`;
282
+ }
283
+ if (op === "$re" || op === "$ire") {
284
+ const castLhs = type === "text" ? lhs : sql`(${lhs})::text`;
285
+ return sql`${castLhs} ${sqlOp} ${String(value)}`;
282
286
  }
283
287
  if (type === "jsonb") {
284
- return sql`${sqlOp} ${JSON.stringify(value)}::jsonb`;
288
+ return sql`${lhs} ${sqlOp} ${JSON.stringify(value)}::jsonb`;
285
289
  }
286
290
  if (type === "cube") {
287
291
  if (!Array.isArray(value) || !value.length || Array.isArray(value[0]) && value.length !== 2) {
288
- throw Error("pg.castValue_bad_cube" + JSON.stringify(value));
292
+ throw Error("pg.getBinarySql_bad_cube" + JSON.stringify(value));
289
293
  }
290
- return Array.isArray(value[0]) ? sql`${sqlOp} cube(${vertexSql(value[0])}, ${vertexSql(value[1])})` : sql`${sqlOp} cube(${vertexSql(value)})`;
294
+ return Array.isArray(value[0]) ? sql`${lhs} ${sqlOp} cube(${vertexSql(value[0])}, ${vertexSql(value[1])})` : sql`${lhs} ${sqlOp} cube(${vertexSql(value)})`;
291
295
  }
292
- return sql`${sqlOp} ${value}`;
296
+ return sql`${lhs} ${sqlOp} ${value}`;
293
297
  }
294
298
  function getSql(filter, options) {
295
299
  function getNodeSql(ast) {
@@ -309,27 +313,26 @@ function getSql(filter, options) {
309
313
  throw Error("pg.lookup_not_jsonb " + prefix);
310
314
  }
311
315
  const [lhs, type] = suffix.length ? [sql`"${raw(prefix)}" #> ${suffix}`, "jsonb"] : [sql`"${raw(prefix)}"`, types[prefix]];
312
- const rhs = castValue(ast[2], type, op);
313
- return sql`${lhs} ${rhs}`;
316
+ return getBinarySql(lhs, type, op, ast[2]);
314
317
  }
315
318
  return getNodeSql(getAst(filter));
316
319
  }
317
- const getIdMeta = ({ idCol }) => getJsonBuildObject({
320
+ const getIdMeta = ({ idCol }) => getJsonBuildTrusted({
318
321
  $key: sql`"${raw(idCol)}"`,
319
322
  $ver: nowTimestamp
320
323
  });
321
- const getArgMeta = (key, prefix, idCol) => getJsonBuildObject({
324
+ const getArgMeta = (key, prefix, idCol) => getJsonBuildTrusted({
322
325
  $key: key,
323
326
  $ref: sql`jsonb_build_array(${join(prefix.map((k) => sql`${k}::text`))}, "${raw(idCol)}")`,
324
327
  $ver: nowTimestamp
325
328
  });
326
- const getAggMeta = (key, $group) => getJsonBuildObject({
327
- $key: join([key, getJsonBuildObject({ $group })].filter(Boolean), " || "),
329
+ const getAggMeta = (key, $group) => getJsonBuildTrusted({
330
+ $key: join([key, getJsonBuildTrusted({ $group })].filter(Boolean), " || "),
328
331
  $ver: nowTimestamp
329
332
  });
330
- function getArgSql(_c, options) {
331
- var _d = _c, { $first, $last, $after, $before, $since, $until, $all, $cursor: _ } = _d, rest = __objRest(_d, ["$first", "$last", "$after", "$before", "$since", "$until", "$all", "$cursor"]);
332
- const _a = rest, { $order, $group } = _a, filter = __objRest(_a, ["$order", "$group"]);
333
+ function getArgSql(_a, options) {
334
+ var _b = _a, { $first, $last, $after, $before, $since, $until, $all, $cursor: _ } = _b, rest = __objRest(_b, ["$first", "$last", "$after", "$before", "$since", "$until", "$all", "$cursor"]);
335
+ const _a2 = rest, { $order, $group } = _a2, filter = __objRest(_a2, ["$order", "$group"]);
333
336
  const { prefix, idCol } = options;
334
337
  if ($order && $group) {
335
338
  throw Error("pg_arg.order_and_group_unsupported in " + prefix);
@@ -354,8 +357,8 @@ function getArgSql(_c, options) {
354
357
  if (value)
355
358
  where.push(getBoundCond(orderCols, value, name));
356
359
  });
357
- const orderQuery = $order && getJsonBuildObject({ $order: sql`${JSON.stringify($order)}::jsonb` });
358
- const cursorQuery = getJsonBuildObject({
360
+ const orderQuery = $order && getJsonBuildTrusted({ $order: sql`${JSON.stringify($order)}::jsonb` });
361
+ const cursorQuery = getJsonBuildTrusted({
359
362
  $cursor: sql`jsonb_build_array(${join(groupCols || orderCols)})`
360
363
  });
361
364
  key = sql`(${join([key, orderQuery, cursorQuery].filter(Boolean), ` || `)})`;
@@ -473,7 +476,7 @@ function del(arg, options) {
473
476
  return sql`
474
477
  DELETE FROM "${raw(table)}"
475
478
  WHERE ${where}
476
- RETURNING (${getJsonBuildObject({ $key: arg })})`;
479
+ RETURNING (${getJsonBuildTrusted({ $key: arg })})`;
477
480
  }
478
481
  const log = debug("graffy:pg:db");
479
482
  class Db {
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.14",
5
+ "version": "0.15.15-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.15.14",
19
+ "@graffy/common": "0.15.15-alpha.1",
20
20
  "sql-template-tag": "^4.0.0",
21
21
  "debug": "^4.3.2"
22
22
  },
@@ -1,6 +1,6 @@
1
1
  export function vertexSql(array: any): Sql;
2
2
  export const nowTimestamp: Sql;
3
- export function getJsonBuildObject(variadic: any): Sql;
3
+ export function getJsonBuildTrusted(variadic: any): Sql;
4
4
  export function lookup(prop: any): Sql;
5
5
  export function getSelectCols(table: any, projection?: any): Sql;
6
6
  export function getInsert(row: any, options: any): {