From b2e7bc8494f85a6e198ceb57d2bae64667aaa164 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Tue, 30 Jul 2024 11:46:42 +0200 Subject: [PATCH] Allow for array extension in dynamic attributes, disallow attribute overrides --- lib/data/datastore.test.ts | 32 ++++++++++++++++++- lib/data/datastore.ts | 63 +++++++++++++++++++------------------- 2 files changed, 63 insertions(+), 32 deletions(-) diff --git a/lib/data/datastore.test.ts b/lib/data/datastore.test.ts index 75e3e372..1d6fd9c8 100644 --- a/lib/data/datastore.test.ts +++ b/lib/data/datastore.test.ts @@ -3,7 +3,7 @@ import { IndexedDBKvPrimitives } from "../data/indexeddb_kv_primitives.ts"; import { cleanupEmptyObjects, DataStore } from "../data/datastore.ts"; import { DenoKvPrimitives } from "../data/deno_kv_primitives.ts"; import { KvPrimitives } from "../data/kv_primitives.ts"; -import { assertEquals } from "$std/testing/asserts.ts"; +import { assertEquals, assertThrows } from "$std/testing/asserts.ts"; import { PrefixedKvPrimitives } from "../data/prefixed_kv_primitives.ts"; import { Query } from "../../plug-api/types.ts"; @@ -124,6 +124,28 @@ async function test(db: KvPrimitives) { ]], }, }, + // Test extending existing array attributes + { + where: ["boolean", true], + attributes: { + "listAttribute": ["array", [["string", "newValue1"]]], + "nested.listAttribute": ["array", [["string", "newValue1"]]], + }, + }, + { + where: ["boolean", true], + attributes: { + "listAttribute": ["array", [["string", "newValue2"]]], + "nested.listAttribute": ["array", [["string", "newValue2"]]], + }, + }, + // Test not being able to override existing attributes + { + where: ["boolean", true], + attributes: { + "lastName": ["string", "Shouldn't be set"], + }, + }, { where: ["=", ["attr", "tags"], ["string", "person"]], attributes: { @@ -143,12 +165,16 @@ async function test(db: KvPrimitives) { const pristineCopy = JSON.parse(JSON.stringify(obj)); datastore.enrichObject(obj); + // console.log("Enrhiched", obj); assertEquals(obj.fullName, "Pete Smith"); + assertEquals(obj.lastName, "Smith"); assertEquals(obj.pageDecoration, { prefix: { bla: { doh: "🧑 Pete Smith" } }, }); assertEquals(obj.existingObjAttribute.something, true); assertEquals(obj.existingObjAttribute.another, "value"); + assertEquals(obj.listAttribute, ["newValue1", "newValue2"]); + assertEquals(obj.nested.listAttribute, ["newValue1", "newValue2"]); // And now let's clean it again datastore.cleanEnrichedObject(obj); @@ -162,6 +188,10 @@ async function test(db: KvPrimitives) { attributes: {}, }, ]; + + assertThrows(() => { + datastore.enrichObject({}); + }); } Deno.test("Test Deno KV DataStore", async () => { diff --git a/lib/data/datastore.ts b/lib/data/datastore.ts index 0f28525c..898859b8 100644 --- a/lib/data/datastore.ts +++ b/lib/data/datastore.ts @@ -136,7 +136,8 @@ export class DataStore { } /** - * Enriches the object with the attributes defined in the object enrichers on the fly + * Enriches the object with the attributes defined in the object enrichers on the fly. + * Will add a `$dynamicAttributes` array to the object to keep track of the dynamic attributes set (for cleanup) * @param object * @returns */ @@ -192,7 +193,22 @@ export class DataStore { `Enricher dynamic attribute expression cannot be an async function: ${expression}`, ); } - objectValue[selectorParts[selectorParts.length - 1]] = value; + const lastPart = selectorParts[selectorParts.length - 1]; + if (objectValue[lastPart] !== undefined) { + // The attribute already exists, we should merge the values if we can, or ignore the new value + if (Array.isArray(objectValue[lastPart]) && Array.isArray(value)) { + // If the attribute already exists and is an array, we should merge the arrays + objectValue[lastPart] = [...objectValue[lastPart], ...value]; + } else { + // We can't merge the values, so we just ignore the new value + } + } else { // New attribute + objectValue[lastPart] = value; + if (!object.$dynamicAttributes) { + object.$dynamicAttributes = []; + } + object.$dynamicAttributes.push(attributeSelector); + } } } } @@ -204,44 +220,29 @@ export class DataStore { * @returns */ cleanEnrichedObject(object: any) { - // Check if this object looks like an object value - if (!object || typeof object !== "object") { + // Check if this is an enriched object + if (!object || !object.$dynamicAttributes) { // Skip return; } - for (const enricher of this.objectEnrichers) { - if ( - evalQueryExpression( - enricher.where, - object, - {}, // We will not support variables in enrichers for now - this.functionMap, - ) - ) { - // The `where` matches so we should clean this object from the dynamic attributes - for ( - const [attributeSelector, _expression] of Object.entries( - enricher.attributes, - ) - ) { - // Recursively travel to the attribute based on the selector, which may contain .'s to go deeper - let objectValue = object; - const selectorParts = attributeSelector.split("."); - for (const part of selectorParts.slice(0, -1)) { - if (typeof objectValue[part] !== "object") { - // This shouldn't happen, but let's back out - break; - } - objectValue = objectValue[part]; - } - - delete objectValue[selectorParts[selectorParts.length - 1]]; + for (const attributeSelector of object.$dynamicAttributes) { + // Recursively travel to the attribute based on the selector, which may contain .'s to go deeper + let objectValue = object; + const selectorParts = attributeSelector.split("."); + for (const part of selectorParts.slice(0, -1)) { + if (typeof objectValue[part] !== "object") { + // This shouldn't happen, but let's back out + break; } + objectValue = objectValue[part]; } + + delete objectValue[selectorParts[selectorParts.length - 1]]; } // Clean up empty objects, this is somewhat questionable, because it also means that if the user intentionally kept empty objects in there, these will be wiped cleanupEmptyObjects(object); + delete object.$dynamicAttributes; } }