@graffy/pg 0.15.14-alpha.1 → 0.15.14

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
@@ -158,68 +158,6 @@ function simplify(node) {
158
158
  }
159
159
  return node;
160
160
  }
161
- const opSql = {
162
- $and: `AND`,
163
- $or: `OR`,
164
- $not: sql__default["default"]`NOT`,
165
- $eq: sql__default["default"]`=`,
166
- $neq: sql__default["default"]`<>`,
167
- $in: sql__default["default"]`IN`,
168
- $nin: sql__default["default"]`NOT IN`,
169
- $lt: sql__default["default"]`<`,
170
- $lte: sql__default["default"]`<=`,
171
- $gt: sql__default["default"]`>`,
172
- $gte: sql__default["default"]`>=`,
173
- $re: sql__default["default"]`~`,
174
- $ire: sql__default["default"]`~*`,
175
- $cts: sql__default["default"]`@>`,
176
- $ctd: sql__default["default"]`<@`
177
- };
178
- function castValue$1(value, type, op) {
179
- if (value === null && op === "$eq")
180
- return sql__default["default"]`IS NULL`;
181
- if (value === null && op === "$neq")
182
- return sql__default["default"]`IS NOT NULL`;
183
- const sqlOp = opSql[op];
184
- if (!sqlOp)
185
- throw Error("pg.getSql_unknown_operator " + op);
186
- if (op === "$in" || op === "$nin") {
187
- return sql__default["default"]`${sqlOp} (${sql.join(value)})`;
188
- }
189
- if (type === "jsonb") {
190
- return sql__default["default"]`${sqlOp} ${JSON.stringify(value)}::jsonb`;
191
- }
192
- if (type === "cube") {
193
- if (!Array.isArray(value) || value.length !== 2 || !Array.isArray(value[0])) {
194
- throw Error("pg.castValue_bad_cube" + JSON.stringify(value));
195
- }
196
- return sql__default["default"]`${sqlOp} cube(${sql.join(value)})`;
197
- }
198
- return sql__default["default"]`${sqlOp} ${value}`;
199
- }
200
- function getSql(filter, options) {
201
- function getNodeSql(ast) {
202
- if (typeof ast === "boolean")
203
- return ast;
204
- const op = ast[0];
205
- if (op === "$and" || op === "$or") {
206
- return sql__default["default"]`(${sql.join(ast[1].map((node) => getNodeSql(node)), `) ${opSql[op]} (`)})`;
207
- } else if (op === "$not") {
208
- return sql__default["default"]`${opSql[op]} (${getNodeSql(ast[1])})`;
209
- }
210
- const [prefix, ...suffix] = common.encodePath(ast[1]);
211
- const { types } = options.schema;
212
- if (!types[prefix])
213
- throw Error("pg.no_column " + prefix);
214
- if (suffix.length && types[prefix] !== "jsonb") {
215
- throw Error("pg.lookup_not_jsonb " + prefix);
216
- }
217
- const [lhs, type] = suffix.length ? [sql__default["default"]`"${sql.raw(prefix)}" #> ${suffix}`, "jsonb"] : [sql__default["default"]`"${sql.raw(prefix)}"`, types[prefix]];
218
- const rhs = castValue$1(ast[2], type, op);
219
- return sql__default["default"]`${lhs} ${rhs}`;
220
- }
221
- return getNodeSql(getAst(filter));
222
- }
223
161
  const nowTimestamp = sql__default["default"]`cast(extract(epoch from now()) as integer)`;
224
162
  const getJsonBuildObject = (variadic) => {
225
163
  const args = sql.join(Object.entries(variadic).map(([name, value]) => {
@@ -264,10 +202,10 @@ const getSelectCols = (table, projection = null) => {
264
202
  }
265
203
  return sql__default["default"]`jsonb_build_object(${sql.join(sqls, ", ")})`;
266
204
  };
267
- function infJoin(array) {
205
+ function vertexSql(array) {
268
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[]`;
269
207
  }
270
- function castValue(value, type, name) {
208
+ function castValue$1(value, type, name, isPut) {
271
209
  if (!type)
272
210
  throw Error("pg.write_no_column " + name);
273
211
  if (value instanceof sql.Sql)
@@ -275,13 +213,13 @@ function castValue(value, type, name) {
275
213
  if (value === null)
276
214
  return sql__default["default"]`NULL`;
277
215
  if (type === "jsonb") {
278
- return value.$put ? JSON.stringify(stripAttributes(value)) : sql__default["default"]`jsonb_strip_nulls(${getJsonUpdate(value, name, [])})`;
216
+ return isPut ? JSON.stringify(stripAttributes(value)) : sql__default["default"]`jsonb_strip_nulls(${getJsonUpdate(value, name, [])})`;
279
217
  }
280
218
  if (type === "cube") {
281
219
  if (!Array.isArray(value) || !value.length || Array.isArray(value[0]) && value.length !== 2) {
282
220
  throw Error("pg.castValue_bad_cube" + JSON.stringify(value));
283
221
  }
284
- return Array.isArray(value[0]) ? sql__default["default"]`cube(${infJoin(value[0])}, ${infJoin(value[1])})` : sql__default["default"]`cube(${infJoin(value)})`;
222
+ return Array.isArray(value[0]) ? sql__default["default"]`cube(${vertexSql(value[0])}, ${vertexSql(value[1])})` : sql__default["default"]`cube(${vertexSql(value)})`;
285
223
  }
286
224
  return value;
287
225
  }
@@ -290,12 +228,12 @@ const getInsert = (row, options) => {
290
228
  const vals = [];
291
229
  Object.entries(row).filter(([col]) => col !== options.verCol && col[0] !== "$").concat([[options.verCol, nowTimestamp]]).forEach(([col, val]) => {
292
230
  cols.push(sql__default["default"]`"${sql.raw(col)}"`);
293
- vals.push(castValue(val, options.schema.types[col], col));
231
+ vals.push(castValue$1(val, options.schema.types[col], col, row.$put));
294
232
  });
295
233
  return { cols: sql.join(cols, ", "), vals: sql.join(vals, ", ") };
296
234
  };
297
235
  const getUpdates = (row, options) => {
298
- 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)}`).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$1(val, options.schema.types[col], col, row.$put)}`).concat(sql__default["default"]`"${sql.raw(options.verCol)}" = ${nowTimestamp}`), ", ");
299
237
  };
300
238
  function getJsonUpdate(_a, col, path) {
301
239
  var _b = _a, { $put } = _b, object = __objRest(_b, ["$put"]);
@@ -322,6 +260,68 @@ function stripAttributes(object) {
322
260
  return out;
323
261
  }, {});
324
262
  }
263
+ const opSql = {
264
+ $and: `AND`,
265
+ $or: `OR`,
266
+ $not: sql__default["default"]`NOT`,
267
+ $eq: sql__default["default"]`=`,
268
+ $neq: sql__default["default"]`<>`,
269
+ $in: sql__default["default"]`IN`,
270
+ $nin: sql__default["default"]`NOT IN`,
271
+ $lt: sql__default["default"]`<`,
272
+ $lte: sql__default["default"]`<=`,
273
+ $gt: sql__default["default"]`>`,
274
+ $gte: sql__default["default"]`>=`,
275
+ $re: sql__default["default"]`~`,
276
+ $ire: sql__default["default"]`~*`,
277
+ $cts: sql__default["default"]`@>`,
278
+ $ctd: sql__default["default"]`<@`
279
+ };
280
+ function castValue(value, type, op) {
281
+ if (value === null && op === "$eq")
282
+ return sql__default["default"]`IS NULL`;
283
+ if (value === null && op === "$neq")
284
+ return sql__default["default"]`IS NOT NULL`;
285
+ const sqlOp = opSql[op];
286
+ if (!sqlOp)
287
+ throw Error("pg.getSql_unknown_operator " + op);
288
+ if (op === "$in" || op === "$nin") {
289
+ return sql__default["default"]`${sqlOp} (${sql.join(value)})`;
290
+ }
291
+ if (type === "jsonb") {
292
+ return sql__default["default"]`${sqlOp} ${JSON.stringify(value)}::jsonb`;
293
+ }
294
+ if (type === "cube") {
295
+ if (!Array.isArray(value) || !value.length || Array.isArray(value[0]) && value.length !== 2) {
296
+ throw Error("pg.castValue_bad_cube" + JSON.stringify(value));
297
+ }
298
+ return Array.isArray(value[0]) ? sql__default["default"]`${sqlOp} cube(${vertexSql(value[0])}, ${vertexSql(value[1])})` : sql__default["default"]`${sqlOp} cube(${vertexSql(value)})`;
299
+ }
300
+ return sql__default["default"]`${sqlOp} ${value}`;
301
+ }
302
+ function getSql(filter, options) {
303
+ function getNodeSql(ast) {
304
+ if (typeof ast === "boolean")
305
+ return ast;
306
+ const op = ast[0];
307
+ if (op === "$and" || op === "$or") {
308
+ return sql__default["default"]`(${sql.join(ast[1].map((node) => getNodeSql(node)), `) ${opSql[op]} (`)})`;
309
+ } else if (op === "$not") {
310
+ return sql__default["default"]`${opSql[op]} (${getNodeSql(ast[1])})`;
311
+ }
312
+ const [prefix, ...suffix] = common.encodePath(ast[1]);
313
+ const { types } = options.schema;
314
+ if (!types[prefix])
315
+ throw Error("pg.no_column " + prefix);
316
+ if (suffix.length && types[prefix] !== "jsonb") {
317
+ throw Error("pg.lookup_not_jsonb " + prefix);
318
+ }
319
+ 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}`;
322
+ }
323
+ return getNodeSql(getAst(filter));
324
+ }
325
325
  const getIdMeta = ({ idCol }) => getJsonBuildObject({
326
326
  $key: sql__default["default"]`"${sql.raw(idCol)}"`,
327
327
  $ver: nowTimestamp
package/index.mjs CHANGED
@@ -26,7 +26,7 @@ var __objRest = (source, exclude) => {
26
26
  }
27
27
  return target;
28
28
  };
29
- import { encodePath, isEmpty, isPlainObject, unwrap, decodeArgs, decodeQuery, slice, finalize, wrap, isRange, decodeGraph, mergeObject, merge, encodeGraph, wrapObject, remove } from "@graffy/common";
29
+ import { isEmpty, encodePath, isPlainObject, unwrap, decodeArgs, decodeQuery, slice, finalize, wrap, isRange, decodeGraph, mergeObject, merge, encodeGraph, wrapObject, remove } from "@graffy/common";
30
30
  import { Pool, Client } from "pg";
31
31
  import sql, { join, raw, Sql, empty } from "sql-template-tag";
32
32
  import debug from "debug";
@@ -150,68 +150,6 @@ function simplify(node) {
150
150
  }
151
151
  return node;
152
152
  }
153
- const opSql = {
154
- $and: `AND`,
155
- $or: `OR`,
156
- $not: sql`NOT`,
157
- $eq: sql`=`,
158
- $neq: sql`<>`,
159
- $in: sql`IN`,
160
- $nin: sql`NOT IN`,
161
- $lt: sql`<`,
162
- $lte: sql`<=`,
163
- $gt: sql`>`,
164
- $gte: sql`>=`,
165
- $re: sql`~`,
166
- $ire: sql`~*`,
167
- $cts: sql`@>`,
168
- $ctd: sql`<@`
169
- };
170
- function castValue$1(value, type, op) {
171
- if (value === null && op === "$eq")
172
- return sql`IS NULL`;
173
- if (value === null && op === "$neq")
174
- return sql`IS NOT NULL`;
175
- const sqlOp = opSql[op];
176
- if (!sqlOp)
177
- throw Error("pg.getSql_unknown_operator " + op);
178
- if (op === "$in" || op === "$nin") {
179
- return sql`${sqlOp} (${join(value)})`;
180
- }
181
- if (type === "jsonb") {
182
- return sql`${sqlOp} ${JSON.stringify(value)}::jsonb`;
183
- }
184
- if (type === "cube") {
185
- if (!Array.isArray(value) || value.length !== 2 || !Array.isArray(value[0])) {
186
- throw Error("pg.castValue_bad_cube" + JSON.stringify(value));
187
- }
188
- return sql`${sqlOp} cube(${join(value)})`;
189
- }
190
- return sql`${sqlOp} ${value}`;
191
- }
192
- function getSql(filter, options) {
193
- function getNodeSql(ast) {
194
- if (typeof ast === "boolean")
195
- return ast;
196
- const op = ast[0];
197
- if (op === "$and" || op === "$or") {
198
- return sql`(${join(ast[1].map((node) => getNodeSql(node)), `) ${opSql[op]} (`)})`;
199
- } else if (op === "$not") {
200
- return sql`${opSql[op]} (${getNodeSql(ast[1])})`;
201
- }
202
- const [prefix, ...suffix] = encodePath(ast[1]);
203
- const { types } = options.schema;
204
- if (!types[prefix])
205
- throw Error("pg.no_column " + prefix);
206
- if (suffix.length && types[prefix] !== "jsonb") {
207
- throw Error("pg.lookup_not_jsonb " + prefix);
208
- }
209
- const [lhs, type] = suffix.length ? [sql`"${raw(prefix)}" #> ${suffix}`, "jsonb"] : [sql`"${raw(prefix)}"`, types[prefix]];
210
- const rhs = castValue$1(ast[2], type, op);
211
- return sql`${lhs} ${rhs}`;
212
- }
213
- return getNodeSql(getAst(filter));
214
- }
215
153
  const nowTimestamp = sql`cast(extract(epoch from now()) as integer)`;
216
154
  const getJsonBuildObject = (variadic) => {
217
155
  const args = join(Object.entries(variadic).map(([name, value]) => {
@@ -256,10 +194,10 @@ const getSelectCols = (table, projection = null) => {
256
194
  }
257
195
  return sql`jsonb_build_object(${join(sqls, ", ")})`;
258
196
  };
259
- function infJoin(array) {
197
+ function vertexSql(array) {
260
198
  return sql`array[${join(array.map((num) => num === Infinity ? sql`'Infinity'` : num === -Infinity ? sql`'-Infinity'` : num))}]::float8[]`;
261
199
  }
262
- function castValue(value, type, name) {
200
+ function castValue$1(value, type, name, isPut) {
263
201
  if (!type)
264
202
  throw Error("pg.write_no_column " + name);
265
203
  if (value instanceof Sql)
@@ -267,13 +205,13 @@ function castValue(value, type, name) {
267
205
  if (value === null)
268
206
  return sql`NULL`;
269
207
  if (type === "jsonb") {
270
- return value.$put ? JSON.stringify(stripAttributes(value)) : sql`jsonb_strip_nulls(${getJsonUpdate(value, name, [])})`;
208
+ return isPut ? JSON.stringify(stripAttributes(value)) : sql`jsonb_strip_nulls(${getJsonUpdate(value, name, [])})`;
271
209
  }
272
210
  if (type === "cube") {
273
211
  if (!Array.isArray(value) || !value.length || Array.isArray(value[0]) && value.length !== 2) {
274
212
  throw Error("pg.castValue_bad_cube" + JSON.stringify(value));
275
213
  }
276
- return Array.isArray(value[0]) ? sql`cube(${infJoin(value[0])}, ${infJoin(value[1])})` : sql`cube(${infJoin(value)})`;
214
+ return Array.isArray(value[0]) ? sql`cube(${vertexSql(value[0])}, ${vertexSql(value[1])})` : sql`cube(${vertexSql(value)})`;
277
215
  }
278
216
  return value;
279
217
  }
@@ -282,12 +220,12 @@ const getInsert = (row, options) => {
282
220
  const vals = [];
283
221
  Object.entries(row).filter(([col]) => col !== options.verCol && col[0] !== "$").concat([[options.verCol, nowTimestamp]]).forEach(([col, val]) => {
284
222
  cols.push(sql`"${raw(col)}"`);
285
- vals.push(castValue(val, options.schema.types[col], col));
223
+ vals.push(castValue$1(val, options.schema.types[col], col, row.$put));
286
224
  });
287
225
  return { cols: join(cols, ", "), vals: join(vals, ", ") };
288
226
  };
289
227
  const getUpdates = (row, options) => {
290
- 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)}`).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$1(val, options.schema.types[col], col, row.$put)}`).concat(sql`"${raw(options.verCol)}" = ${nowTimestamp}`), ", ");
291
229
  };
292
230
  function getJsonUpdate(_a, col, path) {
293
231
  var _b = _a, { $put } = _b, object = __objRest(_b, ["$put"]);
@@ -314,6 +252,68 @@ function stripAttributes(object) {
314
252
  return out;
315
253
  }, {});
316
254
  }
255
+ const opSql = {
256
+ $and: `AND`,
257
+ $or: `OR`,
258
+ $not: sql`NOT`,
259
+ $eq: sql`=`,
260
+ $neq: sql`<>`,
261
+ $in: sql`IN`,
262
+ $nin: sql`NOT IN`,
263
+ $lt: sql`<`,
264
+ $lte: sql`<=`,
265
+ $gt: sql`>`,
266
+ $gte: sql`>=`,
267
+ $re: sql`~`,
268
+ $ire: sql`~*`,
269
+ $cts: sql`@>`,
270
+ $ctd: sql`<@`
271
+ };
272
+ function castValue(value, type, op) {
273
+ if (value === null && op === "$eq")
274
+ return sql`IS NULL`;
275
+ if (value === null && op === "$neq")
276
+ return sql`IS NOT NULL`;
277
+ const sqlOp = opSql[op];
278
+ if (!sqlOp)
279
+ throw Error("pg.getSql_unknown_operator " + op);
280
+ if (op === "$in" || op === "$nin") {
281
+ return sql`${sqlOp} (${join(value)})`;
282
+ }
283
+ if (type === "jsonb") {
284
+ return sql`${sqlOp} ${JSON.stringify(value)}::jsonb`;
285
+ }
286
+ if (type === "cube") {
287
+ if (!Array.isArray(value) || !value.length || Array.isArray(value[0]) && value.length !== 2) {
288
+ throw Error("pg.castValue_bad_cube" + JSON.stringify(value));
289
+ }
290
+ return Array.isArray(value[0]) ? sql`${sqlOp} cube(${vertexSql(value[0])}, ${vertexSql(value[1])})` : sql`${sqlOp} cube(${vertexSql(value)})`;
291
+ }
292
+ return sql`${sqlOp} ${value}`;
293
+ }
294
+ function getSql(filter, options) {
295
+ function getNodeSql(ast) {
296
+ if (typeof ast === "boolean")
297
+ return ast;
298
+ const op = ast[0];
299
+ if (op === "$and" || op === "$or") {
300
+ return sql`(${join(ast[1].map((node) => getNodeSql(node)), `) ${opSql[op]} (`)})`;
301
+ } else if (op === "$not") {
302
+ return sql`${opSql[op]} (${getNodeSql(ast[1])})`;
303
+ }
304
+ const [prefix, ...suffix] = encodePath(ast[1]);
305
+ const { types } = options.schema;
306
+ if (!types[prefix])
307
+ throw Error("pg.no_column " + prefix);
308
+ if (suffix.length && types[prefix] !== "jsonb") {
309
+ throw Error("pg.lookup_not_jsonb " + prefix);
310
+ }
311
+ 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}`;
314
+ }
315
+ return getNodeSql(getAst(filter));
316
+ }
317
317
  const getIdMeta = ({ idCol }) => getJsonBuildObject({
318
318
  $key: sql`"${raw(idCol)}"`,
319
319
  $ver: nowTimestamp
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-alpha.1",
5
+ "version": "0.15.14",
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-alpha.1",
19
+ "@graffy/common": "0.15.14",
20
20
  "sql-template-tag": "^4.0.0",
21
21
  "debug": "^4.3.2"
22
22
  },
@@ -1,3 +1,4 @@
1
+ export function vertexSql(array: any): Sql;
1
2
  export const nowTimestamp: Sql;
2
3
  export function getJsonBuildObject(variadic: any): Sql;
3
4
  export function lookup(prop: any): Sql;