mirror of
https://github.com/strapi/strapi.git
synced 2025-11-25 14:41:15 +00:00
feature: onCommit , onCallback transaction methods
This commit is contained in:
parent
5d645f2103
commit
180d61dc0c
@ -124,7 +124,12 @@ const createAuditLogsService = (strapi) => {
|
|||||||
const processedEvent = processEvent(name, ...args);
|
const processedEvent = processEvent(name, ...args);
|
||||||
|
|
||||||
if (processedEvent) {
|
if (processedEvent) {
|
||||||
await state.provider.saveEvent(processedEvent);
|
// This avoids saving logs during a transaction that was previously being committed.
|
||||||
|
// handleEvent is asynchronous, so there is a change that there is transaction being committed
|
||||||
|
// strapi.db.onTransactionEnd(() => state.provider.saveEvent(processedEvent));
|
||||||
|
await strapi.db.transaction(({ onCommit }) => {
|
||||||
|
onCommit(() => state.provider.saveEvent(processedEvent));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
packages/core/database/lib/index.d.ts
vendored
2
packages/core/database/lib/index.d.ts
vendored
@ -168,6 +168,8 @@ export interface Database {
|
|||||||
trx: Knex.Transaction;
|
trx: Knex.Transaction;
|
||||||
rollback: () => Promise<void>;
|
rollback: () => Promise<void>;
|
||||||
commit: () => Promise<void>;
|
commit: () => Promise<void>;
|
||||||
|
onCommit: (cb) => void;
|
||||||
|
onRollback: (cb) => void;
|
||||||
}) => Promise<unknown>
|
}) => Promise<unknown>
|
||||||
):
|
):
|
||||||
| Promise<unknown>
|
| Promise<unknown>
|
||||||
|
|||||||
@ -61,31 +61,29 @@ class Database {
|
|||||||
|
|
||||||
async function commit() {
|
async function commit() {
|
||||||
if (notNestedTransaction) {
|
if (notNestedTransaction) {
|
||||||
transactionCtx.clear();
|
await transactionCtx.commit(trx);
|
||||||
await trx.commit();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function rollback() {
|
async function rollback() {
|
||||||
if (notNestedTransaction) {
|
if (notNestedTransaction) {
|
||||||
transactionCtx.clear();
|
await transactionCtx.rollback(trx);
|
||||||
await trx.rollback();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!cb) {
|
if (!cb) {
|
||||||
return {
|
return { commit, rollback, get: () => trx };
|
||||||
commit,
|
|
||||||
rollback,
|
|
||||||
get() {
|
|
||||||
return trx;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return transactionCtx.run(trx, async () => {
|
return transactionCtx.run(trx, async () => {
|
||||||
try {
|
try {
|
||||||
const callbackParams = { trx, commit, rollback };
|
const callbackParams = {
|
||||||
|
trx,
|
||||||
|
commit,
|
||||||
|
rollback,
|
||||||
|
onCommit: transactionCtx.onCommit,
|
||||||
|
onRollback: transactionCtx.onRollback,
|
||||||
|
};
|
||||||
const res = await cb(callbackParams);
|
const res = await cb(callbackParams);
|
||||||
await commit();
|
await commit();
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
@ -6,7 +6,7 @@ const storage = new AsyncLocalStorage();
|
|||||||
|
|
||||||
const transactionCtx = {
|
const transactionCtx = {
|
||||||
async run(store, cb) {
|
async run(store, cb) {
|
||||||
return storage.run({ trx: store }, cb);
|
return storage.run({ trx: store, commitCallbacks: [], rollbackCallbacks: [] }, cb);
|
||||||
},
|
},
|
||||||
|
|
||||||
get() {
|
get() {
|
||||||
@ -14,11 +14,54 @@ const transactionCtx = {
|
|||||||
return store?.trx;
|
return store?.trx;
|
||||||
},
|
},
|
||||||
|
|
||||||
clear() {
|
async commit(trx) {
|
||||||
const store = storage.getStore();
|
const store = storage.getStore();
|
||||||
|
|
||||||
|
// Clear transaction from store
|
||||||
if (store?.trx) {
|
if (store?.trx) {
|
||||||
store.trx = null;
|
store.trx = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Commit transaction
|
||||||
|
await trx.commit();
|
||||||
|
|
||||||
|
if (!store?.commitCallbacks) return;
|
||||||
|
|
||||||
|
// Run callbacks
|
||||||
|
store.commitCallbacks.forEach((cb) => cb());
|
||||||
|
store.commitCallbacks = [];
|
||||||
|
},
|
||||||
|
|
||||||
|
async rollback(trx) {
|
||||||
|
const store = storage.getStore();
|
||||||
|
|
||||||
|
// Clear transaction from store
|
||||||
|
if (store?.trx) {
|
||||||
|
store.trx = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rollback transaction
|
||||||
|
await trx.rollback();
|
||||||
|
|
||||||
|
if (!store?.rollbackCallbacks) return;
|
||||||
|
|
||||||
|
// Run callbacks
|
||||||
|
store.rollbackCallbacks.forEach((cb) => cb());
|
||||||
|
store.rollbackCallbacks = [];
|
||||||
|
},
|
||||||
|
|
||||||
|
onCommit(cb) {
|
||||||
|
const store = storage.getStore();
|
||||||
|
if (store?.callbacks) {
|
||||||
|
store.callbacks.push(cb);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
onRollback(cb) {
|
||||||
|
const store = storage.getStore();
|
||||||
|
if (store?.callbacks) {
|
||||||
|
store.callbacks.push(cb);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user