ICS React Native App
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 129 lines 3.0 kB view raw
1import { Database } from "sqlite-async"; 2import { hashSync } from "bcryptjs"; 3import { faker } from "@faker-js/faker"; 4import { range } from "./util"; 5 6const createTable = async ( 7 db: Database, 8 name: string, 9 schema: string, 10): Promise<void> => { 11 await db.run(`DROP TABLE IF EXISTS ${name}`); 12 await db.run(`CREATE TABLE IF NOT EXISTS ${name} (${schema})`); 13}; 14 15export const testUser = { 16 username: "test@test.test", 17 password: "password@123", 18 fullname: "Test User", 19}; 20 21export const seed = async (db: Database): Promise<void> => { 22 await createTable( 23 db, 24 "users", 25 ` 26 id INTEGER PRIMARY KEY AUTOINCREMENT, 27 username TEXT UNIQUE, 28 password TEXT, 29 fullname TEXT, 30 created DATETIME DEFAULT CURRENT_TIMESTAMP 31 `, 32 ); 33 34 await createTable( 35 db, 36 "accounts", 37 ` 38 id INTEGER PRIMARY KEY AUTOINCREMENT, 39 user_id INTEGER, 40 name TEXT, 41 iban TEXT UNIQUE 42 `, 43 ); 44 45 await createTable( 46 db, 47 "cards", 48 ` 49 id INTEGER PRIMARY KEY AUTOINCREMENT, 50 user_id INTEGER, 51 number TEXT, 52 expiry TEXT, 53 cvv TEXT 54 `, 55 ); 56 57 await createTable( 58 db, 59 "transactions", 60 ` 61 id INTEGER PRIMARY KEY AUTOINCREMENT, 62 user_id INTEGER, 63 account_id INTEGER, 64 amount REAL, 65 type TEXT, 66 description TEXT, 67 date datetime 68 `, 69 ); 70 71 const hash = hashSync(testUser.password, 10); 72 73 const { lastID: userId } = await db.run( 74 "INSERT INTO users (username, password, fullname) VALUES (?, ?, ?);", 75 [testUser.username, hash, testUser.fullname], 76 ); 77 78 // Seed accounts 79 const accounts = range(5) 80 .map( 81 () => 82 `(${userId}, '${faker.finance.accountName()} ', '${faker.finance.iban()}')`, 83 ) 84 .join(", "); 85 86 await db.run(`INSERT INTO accounts (user_id, name, iban) VALUES ${accounts}`); 87 88 // Seed cards 89 const cards = range(5).map(() => [ 90 userId, 91 faker.finance.creditCardNumber(), 92 faker.date.future().toISOString().slice(0, 7).replace("-", "/"), 93 faker.finance.creditCardCVV(), 94 ]); 95 96 await db.run( 97 `INSERT INTO cards (user_id, number, expiry, cvv) VALUES ${cards 98 .map(() => "(?, ?, ?, ?)") 99 .join(", ")}`, 100 cards.flat(), 101 ); 102 103 const accountIds = ( 104 await db.all<{ id: number }>("SELECT id FROM accounts WHERE user_id = ?", [ 105 userId, 106 ]) 107 ).map(({ id }) => id); 108 109 await Promise.all( 110 accountIds.map(async (accountId) => { 111 const transactions = range(1000).map(() => [ 112 userId, 113 accountId, 114 faker.finance.amount({ min: -1500, max: 2500, dec: 2 }), 115 faker.finance.transactionType(), 116 faker.commerce.productName(), 117 faker.date.recent({ days: 30 }).toISOString(), 118 ]); 119 120 // Seed transactions 121 db.run( 122 `INSERT INTO transactions (user_id, account_id, amount, type, description, date) VALUES ${transactions 123 .map(() => "(?, ?, ?, ?, ?, ?)") 124 .join(", ")}`, 125 transactions.flat(), 126 ); 127 }), 128 ); 129};