From 9ad7486fdc243393bef071faac14e650c5e524db Mon Sep 17 00:00:00 2001 From: nezuo Date: Fri, 23 Aug 2024 21:49:52 -0700 Subject: [PATCH 1/2] Preserve metadata for save and close --- src/Document.luau | 8 ++++---- src/init.test.luau | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Document.luau b/src/Document.luau index 71d30df..2c03351 100644 --- a/src/Document.luau +++ b/src/Document.luau @@ -137,7 +137,7 @@ function Document:save() return runCallback(self, "beforeSave", self.beforeSaveCallback) :andThen(function() return self.collection.data - :save(self.collection.dataStore, self.key, function(value) + :save(self.collection.dataStore, self.key, function(value, keyInfo) if value == nil then return "fail", Error.new("DocumentRemoved", "The document was removed") end @@ -157,7 +157,7 @@ function Document:save() value.data = self.data - return "succeed", value, self.userIds + return "succeed", value, self.userIds, keyInfo:GetMetadata() end) :andThen(function(_, keyInfo) self.lastKeyInfo = keyInfo @@ -192,7 +192,7 @@ function Document:close() self.collection.autoSave:removeDocument(self) end) :andThen(function() - return self.collection.data:save(self.collection.dataStore, self.key, function(value) + return self.collection.data:save(self.collection.dataStore, self.key, function(value, keyInfo) if value == nil then return "fail", Error.new("DocumentRemoved", "The document was removed") end @@ -213,7 +213,7 @@ function Document:close() value.data = self.data value.lockId = nil - return "succeed", value, self.userIds + return "succeed", value, self.userIds, keyInfo:GetMetadata() end) end) :andThen(function(_, keyInfo) diff --git a/src/init.test.luau b/src/init.test.luau index af62bec..5499462 100644 --- a/src/init.test.luau +++ b/src/init.test.luau @@ -1087,7 +1087,7 @@ return function(x) assert(coroutine.status(thread) == "dead", "") end) - x.testFOCUS("should preserve userids/metadata", function(context) + x.test("should preserve userids/metadata", function(context) local collection = context.lapis.createCollection("collection", defaultOptions()) local document = collection:load("document"):expect() @@ -1116,4 +1116,24 @@ return function(x) assertEqual(keyInfo:GetMetadata().foo, "bar") end) end) + + x.test("preserves metadata", function(context) + local collection = context.lapis.createCollection("collection", defaultOptions()) + + context.write("collection", "document", { apples = 30 }, nil, nil, { foo = "bar" }) + + local function verifyMetadata() + local keyInfo = context.getKeyInfo("collection", "document") + assertEqual(keyInfo:GetMetadata().foo, "bar") + end + + local document = collection:load("document"):expect() + verifyMetadata() + + document:save():expect() + verifyMetadata() + + document:close():expect() + verifyMetadata() + end) end From 6766fb7d2c3a60ada4880fd2ecb6106193980a68 Mon Sep 17 00:00:00 2001 From: nezuo Date: Fri, 23 Aug 2024 21:51:28 -0700 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c5e902..db01e18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,12 +9,14 @@ * `beforeSave`/`beforeClose` threw an error. * `validate` threw an error. * `validate` didn't return `true`. +* `DataStoreKeyInfo:GetMetadata()` is now preserved. Before, it would be erased anytime a `Document` was saved or closed. ([#67]) [#61]: https://github.com/nezuo/lapis/pull/61 [#62]: https://github.com/nezuo/lapis/pull/62 [#64]: https://github.com/nezuo/lapis/pull/64 [#65]: https://github.com/nezuo/lapis/pull/65 [#66]: https://github.com/nezuo/lapis/pull/66 +[#67]: https://github.com/nezuo/lapis/pull/67 ## 0.3.2 - August 6, 2024 * Added `Collection:read` to view a document's data without editing or session locking it. ([#59])