Skip to content

Commit

Permalink
style(seeder): Improve logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Brayan-724 committed Sep 20, 2024
1 parent e212702 commit 3899ee8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
6 changes: 6 additions & 0 deletions seeder/db.js
Original file line number Diff line number Diff line change
@@ -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);
}
13 changes: 8 additions & 5 deletions seeder/seeder.js
Original file line number Diff line number Diff line change
@@ -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 = {};

Expand All @@ -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++) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions seeder/util.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);

0 comments on commit 3899ee8

Please sign in to comment.