diff --git a/seeder/db.js b/seeder/db.js index a27820b..3f7c78e 100644 --- a/seeder/db.js +++ b/seeder/db.js @@ -1,5 +1,11 @@ import { DatabaseSync } from 'node:sqlite'; +import { BOLD, DIM, getIdentation, RESET, UP_ARROW } from './util.js'; const defaultDB = '.wrangler/state/v3/d1/56d060060cda9b9bbea5ad9ecf937a6610673e7d83efb6a4863cae9a061cc103.sqlite'; export const database = new DatabaseSync(process.argv[2] ?? defaultDB); + +export function runSql(sql, ...args) { + console.log(`${getIdentation()}${DIM}${UP_ARROW} Executing: ${BOLD}${sql}${RESET}`); + return database.prepare(sql).all(...args); +} diff --git a/seeder/seeder.js b/seeder/seeder.js index 11d6fcc..6571f06 100644 --- a/seeder/seeder.js +++ b/seeder/seeder.js @@ -1,5 +1,5 @@ import { database } from './db.js'; -import { ARROW, BOLD, DIM, ERROR, L_PURPLE, L_RED, RESET, UP_ARROW } from './util.js'; +import { addIdentation, ARROW, BOLD, DIM, ERROR, getIdentation, L_PURPLE, L_RED, RESET, subIdentation, UP_ARROW } from './util.js'; const DATA = {}; @@ -13,13 +13,14 @@ export function seedTable(count, table, fields, extra) { DATA[table] = []; const d = []; - console.log(`${L_PURPLE} ${ARROW} Creating ${count} rows in ${table}${RESET}`); + console.log(`${getIdentation()}${L_PURPLE}${ARROW} Creating ${count} rows in ${table}${RESET}`); + addIdentation(); const keys = Object.keys(fields); const tableKeys = keys.join(', '); const tableValues = new Array(keys.length).fill('?').join(', '); const sql = `INSERT INTO ${table} (${tableKeys}) VALUES (${tableValues})`; - console.log(`${DIM}${ARROW} Using: ${BOLD}${sql}${RESET}`); + console.log(`${getIdentation()}${DIM}${ARROW} Using: ${BOLD}${sql}${RESET}`); const query = database.prepare(sql); for (let idx = 0; idx < count; idx++) { @@ -64,7 +65,7 @@ ${RESET}`); } }); - console.log(`${DIM}${UP_ARROW} Executing with ${BOLD}(${logValues.join(', ')})${RESET}`); + console.log(`${getIdentation()}${DIM}${UP_ARROW} Executing with ${BOLD}(${logValues.join(', ')})${RESET}`); try { query.run(...values); @@ -96,7 +97,9 @@ ${RESET}`); } } - console.log(`${L_PURPLE} ${ARROW} Created ${count} rows in ${table}${RESET}`); + subIdentation(); + + console.log(`${getIdentation()}${L_PURPLE}${ARROW} Created ${count} rows in ${table}${RESET}`); return d; } diff --git a/seeder/util.js b/seeder/util.js index 811aa17..3297124 100644 --- a/seeder/util.js +++ b/seeder/util.js @@ -1,5 +1,4 @@ import { faker } from '@faker-js/faker'; -import { database } from './db.js'; export const RESET = '\x1b[0m'; export const BOLD = '\x1b[1m'; @@ -12,9 +11,10 @@ export const ARROW = '▶'; export const ERROR = '⨯'; export const UP_ARROW = '↑'; -export const getCreatedAt = () => Math.floor(+faker.date.past() / 1000); +let identation = 0; + +export const addIdentation = () => identation += 1; +export const subIdentation = () => identation = Math.max(0, identation - 1); +export const getIdentation = () => DIM + "╎ ".repeat(identation) + RESET; -export function runSql(sql, ...args) { - console.log(`${DIM}${UP_ARROW} Executing: ${BOLD}${sql}${RESET}`); - return database.prepare(sql).all(...args); -} +export const getCreatedAt = () => Math.floor(+faker.date.past() / 1000);