-1
Hello, I’m creating a solution that will use Elasticsearch as a non-relational database. In the solution I want to control the history of a document as in the example below (this example is not using Elasticsearch yet, it is just an example of the functions of a repository):
// exemplo de collection de dados e collection de snapshots referentes a uma entity
const users = {}, users_snapshots = {}
const getUser = (_id) => users[_id]
const newUser = (user = {}, _ctor) => {
const _id = Date.now() // so pra gerar id's diferentes...
users[_id] = { ...user, _id, _ctor, _created: new Date() }
return users[_id]
}
const updateUser = (_id, _ctor, changes = {}) => {
const old = users[_id]
if(old) {
users[_id] = { ...old, ...changes, _ctor, _updated: new Date() }
if(!users_snapshots[_id]) users_snapshots[_id] = []
users_snapshots.push({ ...old })
return users[_id]
}
throw new Error(`can't update user with _id: ${_id}. not found`)
}
const disableUser = (_id, _ctor) => {
const old = users[_id]
if(old && !old._deleted) {
users[_id] = { ...old, _ctor, _deleted: new Date() }
users_snapshots.push({ ...old })
return users[_id]
}
throw new Error(`can't disable user with _id: ${_id}. not found`)
}
const enableUser = (_id, _ctor) => {
const old = user[_id]
if(old && old._deleted) {
users[_id] = { ...old, _ctor, _deleted: null }
users_snapshots.push({ ...old })
return users[_id]
}
throw new Error(`can't enable user with _id: ${_id}. not found`)
}
const searchUser = (filter = () => 1, offset = 0, limit = 50) => Object
.entries(users)
.filter(filter)
.slice(offset, offset + limit)
.map(item => ({ ...item })
const searchUserSnapshots = (_id, filter = () => 1, offset = 0, limit = 50) => Object
.entries(users_snapshots[_id] || [])
.filter(filter)
.slice(offset, offset + limit)
.map(item => ({ ...item })
The above example would be a structure that whenever I update a document I save a snapshot of that document. The idea is that I keep a history of the data so that the user can do rollback’s or view which user made a modification (by property _ctor
).
Today I can do this generation of old states of a die in relational banks using triggers like AFTER UPDATE
and in mongodb using pre-hooks in Mongoose. Since I need the performance that Elasticsearch can provide me, I would also like to have the above behavior for that bench.
Could someone tell me if I can create a Rigger (as in relational databases) or if I have to manually implement as in the example above?