-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20230514020025_transactions.js
38 lines (31 loc) · 1.31 KB
/
20230514020025_transactions.js
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
32
33
34
35
36
37
38
// @ts-check
const { randPastDate, randNumber } = require("@ngneat/falso");
const NUMBER_BATCHES = 100;
const NUMBER_TRANSACTIONS_PER_BATCH = 100;
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.seed = async function (knex) {
await knex("transactions").del();
const idsAccounts = (await knex("accounts").select("id")).map((item) => item.id);
// lotes por limitação de transações no SQLite
for (let i = 0; i < NUMBER_BATCHES; i++) {
const transactions = Array.from({ length: NUMBER_TRANSACTIONS_PER_BATCH }, () => ({
account_id: idsAccounts[randNumber({ min: 0, max: idsAccounts.length - 1 })],
value: randNumber({ min: -500, max: 500, fraction: 2 }),
date_time: randPastDate(),
}));
await knex("transactions").insert(transactions);
}
// atualizar os saldos
for (const idAccount of idsAccounts) {
const transactions = await knex("transactions").where({ account_id: idAccount });
const balance = transactions.reduce((acc, item) => acc + +item.value, 0);
if (balance >= 0) {
await knex("accounts").update({ balance }).where({ id: idAccount });
} else {
await knex("transactions").insert({ account_id: idAccount, value: -+balance });
}
}
};