Skip to content

Commit

Permalink
feat: Setup seeder functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Brayan-724 committed Sep 20, 2024
1 parent 0461fec commit e212702
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 51 deletions.
5 changes: 5 additions & 0 deletions seeder/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { DatabaseSync } from 'node:sqlite';

const defaultDB =
'.wrangler/state/v3/d1/56d060060cda9b9bbea5ad9ecf937a6610673e7d83efb6a4863cae9a061cc103.sqlite';
export const database = new DatabaseSync(process.argv[2] ?? defaultDB);
155 changes: 104 additions & 51 deletions seeder/seeder.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,113 @@
import { DatabaseSync } from 'node:sqlite';
import { faker } from '@faker-js/faker';
const database = new DatabaseSync(
'.wrangler/state/v3/d1/56d060060cda9b9bbea5ad9ecf937a6610673e7d83efb6a4863cae9a061cc103.sqlite'
);

function cleanDB() {
const delete_form = database.prepare('DELETE FROM Form');
const delete_question = database.prepare('DELETE FROM Question');
const delete_answer = database.prepare('DELETE FROM Answer');
const delete_session = database.prepare('DELETE FROM Session');
const delete_external = database.prepare('DELETE FROM External');
delete_form.all();
delete_question.all();
delete_answer.all();
delete_session.all();
delete_external.all();
console.log('Tablas Form, Question, Answer, Session, External han sido limpiadas.');
}
import { database } from './db.js';
import { ARROW, BOLD, DIM, ERROR, L_PURPLE, L_RED, RESET, UP_ARROW } from './util.js';

cleanDB();

function seedForm(n) {
const q = database.prepare('INSERT INTO Form (external_id, token, kind, email, created_at, deleted) VALUES (?, ?, ?, ?, ?, ?)');
for (let i = 0; i < n; i++) {
const random_external_id = faker.string.uuid();
const random_token = faker.string.uuid();
const random_kind = 'Github';
const random_email = faker.internet.email();
const random_created_at = Math.floor(Date.now() / 1000);
const deleted = 0;
q.run(random_external_id, random_token, random_kind, random_email, random_created_at, deleted);
}
console.log(`${n} registros insertados en la tabla External.`);
const DATA = {};

}
/**
* @param {number} count
* @param {string} table
* @param {{ [k: string]: (data: object, index: number) => string | number }} fields
* @param {null | (data: object, index: number) => void} extra
*/
export function seedTable(count, table, fields, extra) {
DATA[table] = [];
const d = [];

console.log(`${L_PURPLE} ${ARROW} Creating ${count} rows in ${table}${RESET}`);
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}`);
const query = database.prepare(sql);

for (let idx = 0; idx < count; idx++) {
const values = [];
const data = {};

for (const key of keys) {
const val = fields[key](data, idx);

if (typeof val === 'boolean') {
console.warn(`
${L_RED}${ERROR} Boolean values are not supported by SQLITE. Try using numbers instead (${
val ? 1 : 0
}).
╰┬ Table: ${table}
├ Field: ${key}
├ Value: ${val}
${RESET}`);
process.exit(2);
} else if (val !== null && typeof val !== 'string' && typeof val !== 'number') {
console.warn(`
${L_RED}${ERROR} ${typeof val} values are not supported by SQLITE.
╰┬ Table: ${table}
├ Field: ${key}
├ Value: ${val}
${RESET}`);
process.exit(2);
}

values.push(val);
data[key] = val;
}

function seedQuestion() { }
const logValues = values.map((val) => {
val = `${val}`;
const v = val.slice(0, 10);

function seedAnswer() { }
if (v.length === val.length) {
return v;
} else {
return v + '..';
}
});

function seedSession() { }
console.log(`${DIM}${UP_ARROW} Executing with ${BOLD}(${logValues.join(', ')})${RESET}`);

function seedExternal(n) {
const q = database.prepare('INSERT INTO External (external_id, token, kind, email, created_at, deleted) VALUES (?, ?, ?, ?, ?, ?)');
for (let i = 0; i < n; i++) {
const random_external_id = faker.string.uuid();
const random_token = faker.string.uuid();
const random_kind = 'Github';
const random_email = faker.internet.email();
const random_created_at = Math.floor(Date.now() / 1000);
const deleted = 0;
q.run(random_external_id, random_token, random_kind, random_email, random_created_at, deleted);
}
console.log(`${n} registros insertados en la tabla External.`);
try {
query.run(...values);
DATA[table].push(data);
d.push(data);

if (extra) {
extra(data, idx);
}
} catch (e) {
const error = `${e}`;
const getBacktrace = () =>
e.stack
.split('\n')
.slice(2, -3)
.join('\n')
.replaceAll('file://', '')
.replaceAll(process.cwd(), '.');
if (error.includes('NOT NULL')) {
const field = error.split('.')[1];

console.error(`\n${L_RED}${ERROR} ${table} require field "${field}"`);
console.error(getBacktrace());
process.exit(3);
}

console.error(e);
process.exit(3);
}
}

console.log(`${L_PURPLE} ${ARROW} Created ${count} rows in ${table}${RESET}`);

return d;
}

seedExternal(5);
export function getRows(table) {
const data = DATA[table];

if (!data) {
console.error(`Use the seeder first for ${table}`);
process.exit(1);
}

database.close();
return data;
}
20 changes: 20 additions & 0 deletions seeder/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { faker } from '@faker-js/faker';
import { database } from './db.js';

export const RESET = '\x1b[0m';
export const BOLD = '\x1b[1m';
export const DIM = '\x1b[2m';

export const L_RED = '\x1b[1;91m';
export const L_PURPLE = '\x1b[1;95m';

export const ARROW = '▶';
export const ERROR = '⨯';
export const UP_ARROW = '↑';

export const getCreatedAt = () => Math.floor(+faker.date.past() / 1000);

export function runSql(sql, ...args) {
console.log(`${DIM}${UP_ARROW} Executing: ${BOLD}${sql}${RESET}`);
return database.prepare(sql).all(...args);
}

0 comments on commit e212702

Please sign in to comment.