dynamo-bao 0.3.1 → 0.3.2
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.
- package/package.json +1 -1
- package/src/fields.js +90 -1
- package/src/model.js +12 -2
package/package.json
CHANGED
package/src/fields.js
CHANGED
|
@@ -782,6 +782,35 @@ class TtlField extends DateTimeField {
|
|
|
782
782
|
* @description
|
|
783
783
|
* A field that stores a set of string values. Behind the scenes this is backed by a DynamoDB string set.
|
|
784
784
|
* This field cannot be indexed but can be used in filter expressions.
|
|
785
|
+
*
|
|
786
|
+
* Supports direct mutation operations that automatically track changes:
|
|
787
|
+
* - `add(value)` - Add a string to the set
|
|
788
|
+
* - `delete(value)` - Remove a string from the set
|
|
789
|
+
* - `clear()` - Remove all strings from the set
|
|
790
|
+
*
|
|
791
|
+
* @example
|
|
792
|
+
* // Creating with initial values
|
|
793
|
+
* const doc = new MyModel({
|
|
794
|
+
* tags: new Set(["javascript", "web", "tutorial"])
|
|
795
|
+
* });
|
|
796
|
+
*
|
|
797
|
+
* // Direct mutations (recommended - changes are auto-tracked)
|
|
798
|
+
* doc.tags.add("advanced"); // Add a tag
|
|
799
|
+
* doc.tags.delete("tutorial"); // Remove a tag
|
|
800
|
+
* doc.tags.clear(); // Clear all tags
|
|
801
|
+
* await doc.save(); // Changes are saved automatically
|
|
802
|
+
*
|
|
803
|
+
* // Alternative: Replace entire set
|
|
804
|
+
* doc.tags = new Set(["new", "tags"]);
|
|
805
|
+
* await doc.save();
|
|
806
|
+
*
|
|
807
|
+
* // Filter expressions
|
|
808
|
+
* const results = await MyModel.scan({
|
|
809
|
+
* filter: {
|
|
810
|
+
* tags: { $contains: "javascript" }, // Contains specific value
|
|
811
|
+
* tags: { $size: { $gt: 2 } } // Set size greater than 2
|
|
812
|
+
* }
|
|
813
|
+
* });
|
|
785
814
|
*/
|
|
786
815
|
class StringSetField extends BaoBaseField {
|
|
787
816
|
constructor(options = {}) {
|
|
@@ -883,7 +912,26 @@ class StringSetField extends BaoBaseField {
|
|
|
883
912
|
return arrayValue;
|
|
884
913
|
}
|
|
885
914
|
|
|
886
|
-
fromDy(value) {
|
|
915
|
+
fromDy(value, modelInstance = null, fieldName = null) {
|
|
916
|
+
const baseSet = this._createBaseSet(value);
|
|
917
|
+
|
|
918
|
+
// If no model context, return plain Set (for non-property usage)
|
|
919
|
+
if (!modelInstance || !fieldName) {
|
|
920
|
+
return baseSet;
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
// Return proxied Set that tracks changes
|
|
924
|
+
return this._createTrackedSet(baseSet, modelInstance, fieldName);
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
/**
|
|
928
|
+
* @private
|
|
929
|
+
* @description
|
|
930
|
+
* Creates a plain Set from DynamoDB data without any tracking.
|
|
931
|
+
* @param {any} value - The value from DynamoDB (array, null, or undefined)
|
|
932
|
+
* @returns {Set} A plain Set object
|
|
933
|
+
*/
|
|
934
|
+
_createBaseSet(value) {
|
|
887
935
|
if (value === undefined || value === null) {
|
|
888
936
|
return new Set(); // Return empty Set for missing or null values
|
|
889
937
|
}
|
|
@@ -895,6 +943,47 @@ class StringSetField extends BaoBaseField {
|
|
|
895
943
|
return new Set();
|
|
896
944
|
}
|
|
897
945
|
|
|
946
|
+
/**
|
|
947
|
+
* @private
|
|
948
|
+
* @description
|
|
949
|
+
* Creates a proxied Set that automatically tracks mutations and updates the model's underlying data.
|
|
950
|
+
* The proxy intercepts add(), delete(), and clear() operations to mark the field as dirty
|
|
951
|
+
* and update the model's _dyData immediately.
|
|
952
|
+
* @param {Set} baseSet - The base Set to proxy
|
|
953
|
+
* @param {BaoModel} modelInstance - The model instance that owns this field
|
|
954
|
+
* @param {string} fieldName - The name of the field in the model
|
|
955
|
+
* @returns {Proxy} A proxied Set that tracks changes
|
|
956
|
+
*/
|
|
957
|
+
_createTrackedSet(baseSet, modelInstance, fieldName) {
|
|
958
|
+
return new Proxy(baseSet, {
|
|
959
|
+
get(target, prop) {
|
|
960
|
+
const original = target[prop];
|
|
961
|
+
|
|
962
|
+
// Handle functions/methods
|
|
963
|
+
if (typeof original === "function") {
|
|
964
|
+
// For mutating methods, track changes and update underlying data
|
|
965
|
+
if (["add", "delete", "clear"].includes(prop)) {
|
|
966
|
+
return function (...args) {
|
|
967
|
+
const result = original.apply(target, args);
|
|
968
|
+
// Mark field as dirty in existing change tracking
|
|
969
|
+
modelInstance._changes.add(fieldName);
|
|
970
|
+
// Update the underlying _dyData with the mutated set
|
|
971
|
+
const field = modelInstance.constructor._getField(fieldName);
|
|
972
|
+
modelInstance._dyData[fieldName] = field.toDy(target);
|
|
973
|
+
return result;
|
|
974
|
+
};
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
// For all other methods, bind them properly to maintain context
|
|
978
|
+
return original.bind(target);
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
// For non-function properties, return as-is
|
|
982
|
+
return original;
|
|
983
|
+
},
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
|
|
898
987
|
toGsi(value) {
|
|
899
988
|
throw new ValidationError(
|
|
900
989
|
"StringSetField does not support GSI conversion (sets cannot be indexed)",
|
package/src/model.js
CHANGED
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
* This module contains the core functionality for models in Bao.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
const {
|
|
6
|
+
const {
|
|
7
|
+
RelatedFieldClass,
|
|
8
|
+
StringField,
|
|
9
|
+
StringSetFieldClass,
|
|
10
|
+
} = require("./fields");
|
|
7
11
|
const { ModelManager } = require("./model-manager");
|
|
8
12
|
const { defaultLogger: logger } = require("./utils/logger");
|
|
9
13
|
const { ObjectNotFound } = require("./object-not-found");
|
|
@@ -363,7 +367,13 @@ class BaoModel {
|
|
|
363
367
|
|
|
364
368
|
// Define property getter/setter that always works with _dyData
|
|
365
369
|
Object.defineProperty(this, fieldName, {
|
|
366
|
-
get: () =>
|
|
370
|
+
get: () => {
|
|
371
|
+
// For StringSetField, pass model context to enable proxy
|
|
372
|
+
if (field instanceof StringSetFieldClass) {
|
|
373
|
+
return field.fromDy(this._dyData[fieldName], this, fieldName);
|
|
374
|
+
}
|
|
375
|
+
return field.fromDy(this._dyData[fieldName]);
|
|
376
|
+
},
|
|
367
377
|
set: (newValue) => {
|
|
368
378
|
const oldDyValue = this._dyData[fieldName];
|
|
369
379
|
const newDyValue = field.toDy(newValue);
|