78 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-01-18 14:59:10 +02:00
import { EventEmitter } from 'events';
import { randomUUID } from 'crypto';
import { Strapi } from '@strapi/strapi';
2023-01-23 17:50:53 +02:00
type TransactionCallback = (trx?: unknown) => Promise<void>;
export const createTransaction = (strapi: Strapi) => {
const fns: { fn: TransactionCallback; uuid: string }[] = [];
2023-01-18 14:59:10 +02:00
let done = false;
2023-01-23 17:50:53 +02:00
let resume: null | (() => void) = null;
2023-01-18 14:59:10 +02:00
const e = new EventEmitter();
e.on('spawn', (uuid, cb) => {
fns.push({ fn: cb, uuid });
2023-01-23 17:50:53 +02:00
resume?.();
2023-01-18 14:59:10 +02:00
});
e.on('close', () => {
done = true;
2023-01-23 17:50:53 +02:00
resume?.();
2023-01-18 14:59:10 +02:00
});
strapi.db.transaction(async (trx) => {
2023-01-23 17:50:53 +02:00
e.on('rollback', async () => {
await trx.rollback();
});
e.on('commit', async () => {
await trx.commit();
});
2023-01-18 14:59:10 +02:00
while (!done) {
while (fns.length) {
const item = fns.shift();
if (item) {
const { fn, uuid } = item;
try {
const res = await fn(trx);
e.emit(uuid, { data: res });
} catch (error) {
e.emit(uuid, { error });
}
}
}
if (!done && !fns.length) {
// eslint-disable-next-line @typescript-eslint/no-loop-func
await new Promise<void>((resolve) => {
resume = resolve;
});
}
}
});
return {
2023-01-23 17:50:53 +02:00
async attach<T = undefined>(callback: TransactionCallback): Promise<T | undefined> {
2023-01-18 14:59:10 +02:00
const uuid = randomUUID();
e.emit('spawn', uuid, callback);
return new Promise<T | undefined>((resolve, reject) => {
e.on(uuid, ({ data, error }) => {
if (data) {
resolve(data);
}
if (error) {
2023-01-23 12:38:30 +02:00
reject(error);
2023-01-18 14:59:10 +02:00
}
resolve(undefined);
});
});
},
2023-01-23 17:50:53 +02:00
end() {
2023-01-18 14:59:10 +02:00
return e.emit('close');
},
2023-01-23 17:50:53 +02:00
rollback() {
return e.emit('rollback');
},
2023-01-18 14:59:10 +02:00
};
};