@graffy/pg 0.16.7 → 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 +19 -3
  2. package/index.mjs +19 -3
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -98,6 +98,22 @@ const inverse = {
98
98
  function getAst(filter) {
99
99
  return simplify(construct(filter));
100
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
+ }
101
117
  function construct(node, prop, op) {
102
118
  if (!node || typeof node !== "object" || prop && op) {
103
119
  if (op && prop)
@@ -109,6 +125,9 @@ function construct(node, prop, op) {
109
125
  if (Array.isArray(node)) {
110
126
  return ["$or", node.map((item) => construct(item, prop, op))];
111
127
  }
128
+ if (prop && isValidSubQuery(node)) {
129
+ return ["$sub", prop, construct(node)];
130
+ }
112
131
  return [
113
132
  "$and",
114
133
  Object.entries(node).map(([key, val]) => {
@@ -127,9 +146,6 @@ function construct(node, prop, op) {
127
146
  throw Error(`pgast.expected_prop_before:${key}`);
128
147
  return construct(val, prop, key);
129
148
  }
130
- if (prop) {
131
- return ["$sub", prop, construct({ [key]: val })];
132
- }
133
149
  return construct(val, key);
134
150
  })
135
151
  ];
package/index.mjs CHANGED
@@ -96,6 +96,22 @@ const inverse = {
96
96
  function getAst(filter) {
97
97
  return simplify(construct(filter));
98
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
+ }
99
115
  function construct(node, prop, op) {
100
116
  if (!node || typeof node !== "object" || prop && op) {
101
117
  if (op && prop)
@@ -107,6 +123,9 @@ function construct(node, prop, op) {
107
123
  if (Array.isArray(node)) {
108
124
  return ["$or", node.map((item) => construct(item, prop, op))];
109
125
  }
126
+ if (prop && isValidSubQuery(node)) {
127
+ return ["$sub", prop, construct(node)];
128
+ }
110
129
  return [
111
130
  "$and",
112
131
  Object.entries(node).map(([key, val]) => {
@@ -125,9 +144,6 @@ function construct(node, prop, op) {
125
144
  throw Error(`pgast.expected_prop_before:${key}`);
126
145
  return construct(val, prop, key);
127
146
  }
128
- if (prop) {
129
- return ["$sub", prop, construct({ [key]: val })];
130
- }
131
147
  return construct(val, key);
132
148
  })
133
149
  ];
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.7",
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.7",
19
+ "@graffy/common": "0.16.8",
20
20
  "debug": "^4.3.3"
21
21
  },
22
22
  "peerDependencies": {