From ddce421ebb9be950024679e5747da1318fdf4807 Mon Sep 17 00:00:00 2001 From: byronbecker Date: Wed, 20 Mar 2024 01:05:57 -0700 Subject: [PATCH] Fix for #28: No increment in CanDB.update during creation, resulting in Natural subtraction underflow during deletion --- src/CanDB.mo | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/CanDB.mo b/src/CanDB.mo index 732adb5..0da4881 100644 --- a/src/CanDB.mo +++ b/src/CanDB.mo @@ -9,6 +9,8 @@ import Text "mo:base/Text"; import Prim "mo:⛔"; +import { size } "mo:btree/BTree"; + import E "Entity"; import RT "RangeTreeV2"; @@ -277,7 +279,14 @@ module { public func update(db: DB, options: UpdateOptions): ?E.Entity { let ovAttributeMap = RT.update(db.data, options.sk, options.updateAttributeMapFunction); switch(ovAttributeMap) { - case null { null }; + // entity did not previously exist, and was created + case null { + // Normally we would increment the count here, but due to the bug found in https://github.com/ORIGYN-SA/CanDB/issues/28 + // This will apply a hot fix to the db count if it was off by more than 1 + db.count := size(db.data); + null + }; + // entity previously existed, return the original entity case (?map) { ?{ pk = db.pk;