Skip to content

Commit

Permalink
Merge pull request #67 from nezuo/preserve-metadata
Browse files Browse the repository at this point in the history
Preserve metadata for save and close
  • Loading branch information
nezuo authored Aug 24, 2024
2 parents 20eef10 + 6766fb7 commit 094ba28
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
8 changes: 4 additions & 4 deletions src/Document.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
22 changes: 21 additions & 1 deletion src/init.test.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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

0 comments on commit 094ba28

Please sign in to comment.