chore(xo-server/redis): proper (un)serialization support

This commit is contained in:
Julien Fontanet 2023-10-23 15:06:42 +02:00
parent c7eb7db463
commit 225a67ae3b

View File

@ -35,6 +35,16 @@ import Collection, { ModelAlreadyExists } from '../collection.mjs'
const VERSION = '20170905'
export default class Redis extends Collection {
// Prepare a record before storing in the database
//
// Input object can be mutated or a new one returned
_serialize(record) {}
// Clean a record after being fetched from the database
//
// Input object can be mutated or a new one returned
_unserialize(record) {}
constructor({ connection, indexes = [], namespace }) {
super()
@ -128,6 +138,12 @@ export default class Redis extends Collection {
return Promise.all(
map(models, async model => {
// don't mutate param
model = JSON.parse(JSON.stringify(model))
// allow specific serialization
model = this._serialize(model) ?? model
// Generate a new identifier if necessary.
if (model.id === undefined) {
model.id = generateUuid()
@ -196,7 +212,7 @@ export default class Redis extends Collection {
model = await redis.get(key).then(JSON.parse)
}
return model
return model === null ? null : this._unserialize(model) ?? model
}
_get(properties) {