mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 02:16:03 +00:00
Lifecycle provider to manage state instead of entity manager
This commit is contained in:
parent
dcf2a4e32c
commit
273ca638f4
@ -113,8 +113,7 @@ const createEntityManager = db => {
|
||||
|
||||
return {
|
||||
async findOne(uid, params) {
|
||||
const states = {};
|
||||
await db.lifecycles.run('beforeFindOne', uid, { params }, states);
|
||||
const states = await db.lifecycles.run('beforeFindOne', uid, { params });
|
||||
|
||||
const result = await this.createQueryBuilder(uid)
|
||||
.init(params)
|
||||
@ -128,8 +127,7 @@ const createEntityManager = db => {
|
||||
|
||||
// should we name it findOne because people are used to it ?
|
||||
async findMany(uid, params) {
|
||||
const states = {};
|
||||
await db.lifecycles.run('beforeFindMany', uid, { params }, states);
|
||||
const states = await db.lifecycles.run('beforeFindMany', uid, { params });
|
||||
|
||||
const result = await this.createQueryBuilder(uid)
|
||||
.init(params)
|
||||
@ -141,8 +139,7 @@ const createEntityManager = db => {
|
||||
},
|
||||
|
||||
async count(uid, params) {
|
||||
const states = {};
|
||||
await db.lifecycles.run('beforeCount', uid, { params }, states);
|
||||
const states = await db.lifecycles.run('beforeCount', uid, { params });
|
||||
|
||||
const res = await this.createQueryBuilder(uid)
|
||||
.init(_.pick(['_q', 'where', 'filters'], params))
|
||||
@ -158,8 +155,7 @@ const createEntityManager = db => {
|
||||
},
|
||||
|
||||
async create(uid, params) {
|
||||
const states = {};
|
||||
await db.lifecycles.run('beforeCreate', uid, { params }, states);
|
||||
const states = await db.lifecycles.run('beforeCreate', uid, { params });
|
||||
|
||||
const metadata = db.metadata.get(uid);
|
||||
const { data } = params;
|
||||
@ -193,8 +189,7 @@ const createEntityManager = db => {
|
||||
|
||||
// TODO: where do we handle relation processing for many queries ?
|
||||
async createMany(uid, params) {
|
||||
const states = {};
|
||||
await db.lifecycles.run('beforeCreateMany', uid, { params }, states);
|
||||
const states = await db.lifecycles.run('beforeCreateMany', uid, { params });
|
||||
|
||||
const metadata = db.metadata.get(uid);
|
||||
const { data } = params;
|
||||
@ -221,8 +216,7 @@ const createEntityManager = db => {
|
||||
},
|
||||
|
||||
async update(uid, params) {
|
||||
const states = {};
|
||||
await db.lifecycles.run('beforeUpdate', uid, { params }, states);
|
||||
const states = await db.lifecycles.run('beforeUpdate', uid, { params });
|
||||
|
||||
const metadata = db.metadata.get(uid);
|
||||
const { where, data } = params;
|
||||
@ -272,8 +266,7 @@ const createEntityManager = db => {
|
||||
|
||||
// TODO: where do we handle relation processing for many queries ?
|
||||
async updateMany(uid, params) {
|
||||
const states = {};
|
||||
await db.lifecycles.run('beforeUpdateMany', uid, { params }, states);
|
||||
const states = await db.lifecycles.run('beforeUpdateMany', uid, { params });
|
||||
|
||||
const metadata = db.metadata.get(uid);
|
||||
const { where, data } = params;
|
||||
@ -297,8 +290,7 @@ const createEntityManager = db => {
|
||||
},
|
||||
|
||||
async delete(uid, params) {
|
||||
const states = {};
|
||||
await db.lifecycles.run('beforeDelete', uid, { params }, states);
|
||||
const states = await db.lifecycles.run('beforeDelete', uid, { params });
|
||||
|
||||
const { where, select, populate } = params;
|
||||
|
||||
@ -333,8 +325,7 @@ const createEntityManager = db => {
|
||||
|
||||
// TODO: where do we handle relation processing for many queries ?
|
||||
async deleteMany(uid, params) {
|
||||
const states = {};
|
||||
await db.lifecycles.run('beforeDeleteMany', uid, { params }, states);
|
||||
const states = await db.lifecycles.run('beforeDeleteMany', uid, { params });
|
||||
|
||||
const { where } = params;
|
||||
|
||||
|
||||
@ -30,6 +30,12 @@ const createLifecyclesProvider = db => {
|
||||
subscribers = [];
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} action
|
||||
* @param {string} uid
|
||||
* @param {{ param?: any, result?: any }} properties
|
||||
* @param {Map<any, any>} state
|
||||
*/
|
||||
createEvent(action, uid, properties, state) {
|
||||
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++) {
|
||||
const subscriber = subscribers[i];
|
||||
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);
|
||||
if (event.state) {
|
||||
states[i] = event.state;
|
||||
states.set(subscriber, event.state || state);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@ -57,14 +70,17 @@ const createLifecyclesProvider = db => {
|
||||
const hasModel = !subscriber.models || subscriber.models.includes(uid);
|
||||
|
||||
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);
|
||||
if (event.state) {
|
||||
states[i] = event.state;
|
||||
states.set(subscriber, event.state);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return states;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user