feat(xo-server): save JSON records in Redis (#7113)
Better support of non-string properties without having to handle them one by one. To avoid breaking compatibility with older versions, this should not be merged after reading JSON records has been supported for at least a month.
This commit is contained in:
@@ -147,10 +147,13 @@ export default class Redis extends Collection {
|
|||||||
model = this._serialize(model) ?? model
|
model = this._serialize(model) ?? model
|
||||||
|
|
||||||
// Generate a new identifier if necessary.
|
// Generate a new identifier if necessary.
|
||||||
if (model.id === undefined) {
|
let { id } = model
|
||||||
model.id = generateUuid()
|
if (id === undefined) {
|
||||||
|
id = generateUuid()
|
||||||
|
} else {
|
||||||
|
// identifier is not stored as value in the database, it's already part of the key
|
||||||
|
delete model.id
|
||||||
}
|
}
|
||||||
const { id } = model
|
|
||||||
|
|
||||||
const newEntry = await redis.sAdd(prefix + '_ids', id)
|
const newEntry = await redis.sAdd(prefix + '_ids', id)
|
||||||
|
|
||||||
@@ -172,16 +175,7 @@ export default class Redis extends Collection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const key = `${prefix}:${id}`
|
const key = `${prefix}:${id}`
|
||||||
const props = {}
|
const promises = [redis.del(key), redis.set(key, JSON.stringify(model))]
|
||||||
for (const name of Object.keys(model)) {
|
|
||||||
if (name !== 'id') {
|
|
||||||
const value = model[name]
|
|
||||||
if (value !== undefined) {
|
|
||||||
props[name] = String(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const promises = [redis.del(key), redis.hSet(key, props)]
|
|
||||||
|
|
||||||
// Update indexes.
|
// Update indexes.
|
||||||
forEach(indexes, index => {
|
forEach(indexes, index => {
|
||||||
@@ -206,12 +200,13 @@ export default class Redis extends Collection {
|
|||||||
|
|
||||||
let model
|
let model
|
||||||
try {
|
try {
|
||||||
model = await redis.hGetAll(key)
|
model = await redis.get(key).then(JSON.parse)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (!error.message.startsWith('WRONGTYPE')) {
|
if (!error.message.startsWith('WRONGTYPE')) {
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
model = await redis.get(key).then(JSON.parse)
|
|
||||||
|
model = await redis.hGetAll(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
return model
|
return model
|
||||||
|
|||||||
Reference in New Issue
Block a user