Lifecycle provider to manage state instead of entity manager

This commit is contained in:
Robin Goupil 2022-04-04 19:22:05 +02:00
parent dcf2a4e32c
commit 273ca638f4
2 changed files with 30 additions and 23 deletions

View File

@ -113,8 +113,7 @@ const createEntityManager = db => {
return { return {
async findOne(uid, params) { async findOne(uid, params) {
const states = {}; const states = await db.lifecycles.run('beforeFindOne', uid, { params });
await db.lifecycles.run('beforeFindOne', uid, { params }, states);
const result = await this.createQueryBuilder(uid) const result = await this.createQueryBuilder(uid)
.init(params) .init(params)
@ -128,8 +127,7 @@ const createEntityManager = db => {
// should we name it findOne because people are used to it ? // should we name it findOne because people are used to it ?
async findMany(uid, params) { async findMany(uid, params) {
const states = {}; const states = await db.lifecycles.run('beforeFindMany', uid, { params });
await db.lifecycles.run('beforeFindMany', uid, { params }, states);
const result = await this.createQueryBuilder(uid) const result = await this.createQueryBuilder(uid)
.init(params) .init(params)
@ -141,8 +139,7 @@ const createEntityManager = db => {
}, },
async count(uid, params) { async count(uid, params) {
const states = {}; const states = await db.lifecycles.run('beforeCount', uid, { params });
await db.lifecycles.run('beforeCount', uid, { params }, states);
const res = await this.createQueryBuilder(uid) const res = await this.createQueryBuilder(uid)
.init(_.pick(['_q', 'where', 'filters'], params)) .init(_.pick(['_q', 'where', 'filters'], params))
@ -158,8 +155,7 @@ const createEntityManager = db => {
}, },
async create(uid, params) { async create(uid, params) {
const states = {}; const states = await db.lifecycles.run('beforeCreate', uid, { params });
await db.lifecycles.run('beforeCreate', uid, { params }, states);
const metadata = db.metadata.get(uid); const metadata = db.metadata.get(uid);
const { data } = params; const { data } = params;
@ -193,8 +189,7 @@ const createEntityManager = db => {
// TODO: where do we handle relation processing for many queries ? // TODO: where do we handle relation processing for many queries ?
async createMany(uid, params) { async createMany(uid, params) {
const states = {}; const states = await db.lifecycles.run('beforeCreateMany', uid, { params });
await db.lifecycles.run('beforeCreateMany', uid, { params }, states);
const metadata = db.metadata.get(uid); const metadata = db.metadata.get(uid);
const { data } = params; const { data } = params;
@ -221,8 +216,7 @@ const createEntityManager = db => {
}, },
async update(uid, params) { async update(uid, params) {
const states = {}; const states = await db.lifecycles.run('beforeUpdate', uid, { params });
await db.lifecycles.run('beforeUpdate', uid, { params }, states);
const metadata = db.metadata.get(uid); const metadata = db.metadata.get(uid);
const { where, data } = params; const { where, data } = params;
@ -272,8 +266,7 @@ const createEntityManager = db => {
// TODO: where do we handle relation processing for many queries ? // TODO: where do we handle relation processing for many queries ?
async updateMany(uid, params) { async updateMany(uid, params) {
const states = {}; const states = await db.lifecycles.run('beforeUpdateMany', uid, { params });
await db.lifecycles.run('beforeUpdateMany', uid, { params }, states);
const metadata = db.metadata.get(uid); const metadata = db.metadata.get(uid);
const { where, data } = params; const { where, data } = params;
@ -297,8 +290,7 @@ const createEntityManager = db => {
}, },
async delete(uid, params) { async delete(uid, params) {
const states = {}; const states = await db.lifecycles.run('beforeDelete', uid, { params });
await db.lifecycles.run('beforeDelete', uid, { params }, states);
const { where, select, populate } = params; const { where, select, populate } = params;
@ -333,8 +325,7 @@ const createEntityManager = db => {
// TODO: where do we handle relation processing for many queries ? // TODO: where do we handle relation processing for many queries ?
async deleteMany(uid, params) { async deleteMany(uid, params) {
const states = {}; const states = await db.lifecycles.run('beforeDeleteMany', uid, { params });
await db.lifecycles.run('beforeDeleteMany', uid, { params }, states);
const { where } = params; const { where } = params;

View File

@ -30,6 +30,12 @@ const createLifecyclesProvider = db => {
subscribers = []; subscribers = [];
}, },
/**
* @param {string} action
* @param {string} uid
* @param {{ param?: any, result?: any }} properties
* @param {Map<any, any>} state
*/
createEvent(action, uid, properties, state) { createEvent(action, uid, properties, state) {
const model = db.metadata.get(uid); const model = db.metadata.get(uid);
@ -41,14 +47,21 @@ const createLifecyclesProvider = db => {
}; };
}, },
async run(action, uid, properties, states) { /**
* @param {string} action
* @param {string} uid
* @param {{ param?: any, result?: any }} properties
* @param {Map<any, any>} states
*/
async run(action, uid, properties, states = new Map()) {
for (let i = 0; i < subscribers.length; i++) { for (let i = 0; i < subscribers.length; i++) {
const subscriber = subscribers[i]; const subscriber = subscribers[i];
if (typeof subscriber === 'function') { if (typeof subscriber === 'function') {
const event = this.createEvent(action, uid, properties, states[i]); const state = states.get(subscriber) || {};
const event = this.createEvent(action, uid, properties, state);
await subscriber(event); await subscriber(event);
if (event.state) { if (event.state) {
states[i] = event.state; states.set(subscriber, event.state || state);
} }
continue; continue;
} }
@ -57,14 +70,17 @@ const createLifecyclesProvider = db => {
const hasModel = !subscriber.models || subscriber.models.includes(uid); const hasModel = !subscriber.models || subscriber.models.includes(uid);
if (hasAction && hasModel) { if (hasAction && hasModel) {
const event = this.createEvent(action, uid, properties, states[i]); const state = states.get(subscriber) || {};
const event = this.createEvent(action, uid, properties, state);
await subscriber[action](event); await subscriber[action](event);
if (event.state) { if (event.state) {
states[i] = event.state; states.set(subscriber, event.state);
} }
} }
} }
return states;
}, },
}; };
}; };