@graffy/pg 0.16.6 → 0.16.8

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.
Files changed (3) hide show
  1. package/index.cjs +27 -5
  2. package/index.mjs +27 -5
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -81,7 +81,9 @@ const valid = {
81
81
  $all: true,
82
82
  $has: true,
83
83
  $cts: true,
84
- $ctd: true
84
+ $ctd: true,
85
+ $keycts: true,
86
+ $keyctd: true
85
87
  };
86
88
  const inverse = {
87
89
  $eq: "$neq",
@@ -96,6 +98,22 @@ const inverse = {
96
98
  function getAst(filter) {
97
99
  return simplify(construct(filter));
98
100
  }
101
+ function isValidSubQuery(node) {
102
+ if (!node || typeof node !== "object")
103
+ return false;
104
+ const keys = Object.keys(node);
105
+ for (const key of keys) {
106
+ if (key[0] === "$" && !["$and", "$or", "$not"].includes(key))
107
+ return false;
108
+ if (key[0] !== "$")
109
+ return true;
110
+ }
111
+ for (const key in node) {
112
+ if (!isValidSubQuery(node[key]))
113
+ return false;
114
+ }
115
+ return false;
116
+ }
99
117
  function construct(node, prop, op) {
100
118
  if (!node || typeof node !== "object" || prop && op) {
101
119
  if (op && prop)
@@ -107,6 +125,9 @@ function construct(node, prop, op) {
107
125
  if (Array.isArray(node)) {
108
126
  return ["$or", node.map((item) => construct(item, prop, op))];
109
127
  }
128
+ if (prop && isValidSubQuery(node)) {
129
+ return ["$sub", prop, construct(node)];
130
+ }
110
131
  return [
111
132
  "$and",
112
133
  Object.entries(node).map(([key, val]) => {
@@ -125,9 +146,6 @@ function construct(node, prop, op) {
125
146
  throw Error(`pgast.expected_prop_before:${key}`);
126
147
  return construct(val, prop, key);
127
148
  }
128
- if (prop) {
129
- return ["$sub", prop, construct({ [key]: val })];
130
- }
131
149
  return construct(val, key);
132
150
  })
133
151
  ];
@@ -352,7 +370,9 @@ const opSql = {
352
370
  $re: sql`~`,
353
371
  $ire: sql`~*`,
354
372
  $cts: sql`@>`,
355
- $ctd: sql`<@`
373
+ $ctd: sql`<@`,
374
+ $keycts: sql`?|`,
375
+ $keyctd: sql`?&`
356
376
  };
357
377
  function getBinarySql(lhs, type, op, value, textLhs) {
358
378
  if (value === null && op === "$eq")
@@ -379,6 +399,8 @@ function getBinarySql(lhs, type, op, value, textLhs) {
379
399
  if (typeof value === "string") {
380
400
  return sql`${textLhs} ${sqlOp} ${value}`;
381
401
  }
402
+ if ((op === "$keycts" || op === "$keyctd") && Array.isArray(value))
403
+ return sql`${lhs} ${sqlOp} ${value}::text[]`;
382
404
  return sql`${lhs} ${sqlOp} ${JSON.stringify(value)}::jsonb`;
383
405
  }
384
406
  if (type === "cube")
package/index.mjs CHANGED
@@ -79,7 +79,9 @@ const valid = {
79
79
  $all: true,
80
80
  $has: true,
81
81
  $cts: true,
82
- $ctd: true
82
+ $ctd: true,
83
+ $keycts: true,
84
+ $keyctd: true
83
85
  };
84
86
  const inverse = {
85
87
  $eq: "$neq",
@@ -94,6 +96,22 @@ const inverse = {
94
96
  function getAst(filter) {
95
97
  return simplify(construct(filter));
96
98
  }
99
+ function isValidSubQuery(node) {
100
+ if (!node || typeof node !== "object")
101
+ return false;
102
+ const keys = Object.keys(node);
103
+ for (const key of keys) {
104
+ if (key[0] === "$" && !["$and", "$or", "$not"].includes(key))
105
+ return false;
106
+ if (key[0] !== "$")
107
+ return true;
108
+ }
109
+ for (const key in node) {
110
+ if (!isValidSubQuery(node[key]))
111
+ return false;
112
+ }
113
+ return false;
114
+ }
97
115
  function construct(node, prop, op) {
98
116
  if (!node || typeof node !== "object" || prop && op) {
99
117
  if (op && prop)
@@ -105,6 +123,9 @@ function construct(node, prop, op) {
105
123
  if (Array.isArray(node)) {
106
124
  return ["$or", node.map((item) => construct(item, prop, op))];
107
125
  }
126
+ if (prop && isValidSubQuery(node)) {
127
+ return ["$sub", prop, construct(node)];
128
+ }
108
129
  return [
109
130
  "$and",
110
131
  Object.entries(node).map(([key, val]) => {
@@ -123,9 +144,6 @@ function construct(node, prop, op) {
123
144
  throw Error(`pgast.expected_prop_before:${key}`);
124
145
  return construct(val, prop, key);
125
146
  }
126
- if (prop) {
127
- return ["$sub", prop, construct({ [key]: val })];
128
- }
129
147
  return construct(val, key);
130
148
  })
131
149
  ];
@@ -350,7 +368,9 @@ const opSql = {
350
368
  $re: sql`~`,
351
369
  $ire: sql`~*`,
352
370
  $cts: sql`@>`,
353
- $ctd: sql`<@`
371
+ $ctd: sql`<@`,
372
+ $keycts: sql`?|`,
373
+ $keyctd: sql`?&`
354
374
  };
355
375
  function getBinarySql(lhs, type, op, value, textLhs) {
356
376
  if (value === null && op === "$eq")
@@ -377,6 +397,8 @@ function getBinarySql(lhs, type, op, value, textLhs) {
377
397
  if (typeof value === "string") {
378
398
  return sql`${textLhs} ${sqlOp} ${value}`;
379
399
  }
400
+ if ((op === "$keycts" || op === "$keyctd") && Array.isArray(value))
401
+ return sql`${lhs} ${sqlOp} ${value}::text[]`;
380
402
  return sql`${lhs} ${sqlOp} ${JSON.stringify(value)}::jsonb`;
381
403
  }
382
404
  if (type === "cube")
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.6",
5
+ "version": "0.16.8",
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.6",
19
+ "@graffy/common": "0.16.8",
20
20
  "debug": "^4.3.3"
21
21
  },
22
22
  "peerDependencies": {