MCPcopy
hub / github.com/aspen-cloud/triplit / set

Function set

packages/db/src/db-transaction.ts:345–390  ·  view source on GitHub ↗
(target, prop, newValue)

Source from the content-addressed store, hash-verified

343 return Reflect.get(target, prop);
344 },
345 set(target, prop, newValue) {
346 if (typeof prop === 'symbol') return true;
347 // TODO: generally we don't expect an "Input" to be undefined because optional properties
348 // are checked at the top level instead of at the specific attribute
349 let changeAtThisProp = newValue;
350 if (newValue instanceof Set) {
351 let setChanges: Record<string, boolean> = {};
352 // if we are overwriting an existing set, we need to clear out the old values
353 if (target[prop] instanceof Set) {
354 for (const item of target[prop]) {
355 setChanges[item] = false;
356 }
357 }
358 for (const item of newValue) {
359 setChanges[item] = true;
360 }
361 changeAtThisProp = setChanges;
362 } else if (newValue === undefined) {
363 changeAtThisProp = null;
364 } else if (newValue instanceof Date) {
365 changeAtThisProp = newValue.toISOString();
366 } else if (
367 typeof newValue === 'object' &&
368 newValue !== null &&
369 // replace Arrays wholesale, don't merge
370 !Array.isArray(newValue)
371 ) {
372 if (!changes[prop]) {
373 changes[prop] = {};
374 }
375 changeAtThisProp = {};
376 // if we're doing object reassignment
377 // merge by setting existing keys to null
378 if (typeof target[prop] === 'object' && target[prop] !== null) {
379 for (const key in target[prop]) {
380 changeAtThisProp[key] = null;
381 }
382 }
383 for (const key in newValue) {
384 changeAtThisProp[key] = newValue[key];
385 }
386 }
387 changes[prop] = changeAtThisProp;
388
389 return Reflect.set(target, prop, newValue);
390 },
391 deleteProperty(target, prop) {
392 if (typeof prop === 'symbol') return true;
393 changes[prop] = null;

Callers

nothing calls this directly

Calls 1

setMethod · 0.65

Tested by

no test coverage detected