File size: 1,088 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * SQL transactions for the modlog.
 */

import type { TransactionEnvironment } from '../../lib/sql';
import type { TransactionArguments } from '.';

export const transactions = {
	insertion: (args: TransactionArguments, env: TransactionEnvironment) => {
		const modlogInsertion = env.statements.get(args.modlogInsertionStatement);
		const altsInsertion = env.statements.get(args.altsInsertionStatement);
		if (!modlogInsertion) {
			throw new Error(`Couldn't find prepared statement for provided value (args.modlogInsertionStatement=${args.modlogInsertionStatement}`);
		}
		if (!altsInsertion) {
			throw new Error(`Couldn't find prepared statement for provided value (args.altsInsertionStatement=${args.altsInsertionStatement}`);
		}

		for (const entry of args.entries) {
			// SQLite doesn't have a boolean type, so this is a workaround.
			entry.isGlobal = Number(entry.isGlobal) as any;
			const result = modlogInsertion.run(entry);
			const rowid = result.lastInsertRowid as number;

			for (const alt of entry.alts || []) {
				altsInsertion.run(rowid, alt);
			}
		}
	},
};