@graffy/pg 0.16.14 → 0.16.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.
Files changed (3) hide show
  1. package/index.cjs +26 -11
  2. package/index.mjs +26 -11
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -149,7 +149,7 @@ function castValue(value, type, name, isPut) {
149
149
  if (value === null)
150
150
  return sql`NULL`;
151
151
  if (type === "jsonb") {
152
- return isPut ? JSON.stringify(stripAttributes(value)) : getJsonUpdate(value, name, []);
152
+ return isPut ? JSON.stringify(stripAttributes(value)) : getJsonUpdate(value, name, [])[0];
153
153
  }
154
154
  if (type === "cube")
155
155
  return cubeLiteralSql(value);
@@ -207,21 +207,36 @@ const getUpdates = (row, options) => {
207
207
  );
208
208
  };
209
209
  function getJsonUpdate(object, col, path) {
210
- if (!object || typeof object !== "object" || Array.isArray(object) || object.$put) {
211
- return getJsonBuildValue(object);
210
+ if (!object || typeof object !== "object" || Array.isArray(object) || object.$put || object.$val) {
211
+ let patch2 = stripAttributes(object);
212
+ if (object == null ? void 0 : object.$val)
213
+ patch2 = { $val: patch2 };
214
+ return [sql`${JSON.stringify(patch2)}::jsonb`, patch2 === null];
212
215
  }
213
216
  const curr = sql`"${raw(col)}"${path.length ? sql`#>${path}` : empty}`;
214
217
  if (common.isEmpty(object))
215
- return curr;
216
- return sql`nullif(jsonb_strip_nulls((case jsonb_typeof(${curr})
218
+ return [curr, false];
219
+ const baseSql = sql`case jsonb_typeof(${curr})
217
220
  when 'object' then ${curr}
218
221
  else '{}'::jsonb
219
- end) || jsonb_build_object(${join(
220
- Object.entries(object).map(
221
- ([key, value]) => sql`${key}::text, ${getJsonUpdate(value, col, path.concat(key))}`
222
- ),
223
- ", "
224
- )})), '{}'::jsonb)`;
222
+ end`;
223
+ let maybeNull = true;
224
+ let hasNulls = false;
225
+ const patchSqls = Object.entries(object).map(([key, value]) => {
226
+ const [valSql, nullable] = getJsonUpdate(value, col, path.concat(key));
227
+ maybeNull && (maybeNull = nullable);
228
+ hasNulls || (hasNulls = nullable);
229
+ return sql`${key}::text, ${valSql}`;
230
+ });
231
+ let clause = sql`${baseSql} || jsonb_build_object(${join(patchSqls, ", ")})`;
232
+ if (hasNulls) {
233
+ clause = sql`(select jsonb_object_agg(key, value)
234
+ from jsonb_each(${clause}) where value <> 'null'::jsonb)`;
235
+ }
236
+ if (maybeNull) {
237
+ clause = sql`nullif(${clause}, '{}'::jsonb)`;
238
+ }
239
+ return [clause, maybeNull];
225
240
  }
226
241
  function stripAttributes(object) {
227
242
  if (typeof object !== "object" || !object)
package/index.mjs CHANGED
@@ -147,7 +147,7 @@ function castValue(value, type, name, isPut) {
147
147
  if (value === null)
148
148
  return sql`NULL`;
149
149
  if (type === "jsonb") {
150
- return isPut ? JSON.stringify(stripAttributes(value)) : getJsonUpdate(value, name, []);
150
+ return isPut ? JSON.stringify(stripAttributes(value)) : getJsonUpdate(value, name, [])[0];
151
151
  }
152
152
  if (type === "cube")
153
153
  return cubeLiteralSql(value);
@@ -205,21 +205,36 @@ const getUpdates = (row, options) => {
205
205
  );
206
206
  };
207
207
  function getJsonUpdate(object, col, path) {
208
- if (!object || typeof object !== "object" || Array.isArray(object) || object.$put) {
209
- return getJsonBuildValue(object);
208
+ if (!object || typeof object !== "object" || Array.isArray(object) || object.$put || object.$val) {
209
+ let patch2 = stripAttributes(object);
210
+ if (object == null ? void 0 : object.$val)
211
+ patch2 = { $val: patch2 };
212
+ return [sql`${JSON.stringify(patch2)}::jsonb`, patch2 === null];
210
213
  }
211
214
  const curr = sql`"${raw(col)}"${path.length ? sql`#>${path}` : empty}`;
212
215
  if (isEmpty(object))
213
- return curr;
214
- return sql`nullif(jsonb_strip_nulls((case jsonb_typeof(${curr})
216
+ return [curr, false];
217
+ const baseSql = sql`case jsonb_typeof(${curr})
215
218
  when 'object' then ${curr}
216
219
  else '{}'::jsonb
217
- end) || jsonb_build_object(${join(
218
- Object.entries(object).map(
219
- ([key, value]) => sql`${key}::text, ${getJsonUpdate(value, col, path.concat(key))}`
220
- ),
221
- ", "
222
- )})), '{}'::jsonb)`;
220
+ end`;
221
+ let maybeNull = true;
222
+ let hasNulls = false;
223
+ const patchSqls = Object.entries(object).map(([key, value]) => {
224
+ const [valSql, nullable] = getJsonUpdate(value, col, path.concat(key));
225
+ maybeNull && (maybeNull = nullable);
226
+ hasNulls || (hasNulls = nullable);
227
+ return sql`${key}::text, ${valSql}`;
228
+ });
229
+ let clause = sql`${baseSql} || jsonb_build_object(${join(patchSqls, ", ")})`;
230
+ if (hasNulls) {
231
+ clause = sql`(select jsonb_object_agg(key, value)
232
+ from jsonb_each(${clause}) where value <> 'null'::jsonb)`;
233
+ }
234
+ if (maybeNull) {
235
+ clause = sql`nullif(${clause}, '{}'::jsonb)`;
236
+ }
237
+ return [clause, maybeNull];
223
238
  }
224
239
  function stripAttributes(object) {
225
240
  if (typeof object !== "object" || !object)
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.14",
5
+ "version": "0.16.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.16.14",
19
+ "@graffy/common": "0.16.15-alpha.1",
20
20
  "debug": "^4.3.3"
21
21
  },
22
22
  "peerDependencies": {