const { sql } = require('@vercel/postgres'); const { invoices, customers, revenue } = require('../app/lib/dummy-data.js'); async function seedInvoices() { try { // Create the "invoices" table if it doesn't exist const createTable = await sql` CREATE TABLE IF NOT EXISTS invoices ( id SERIAL PRIMARY KEY, customer_id INT NOT NULL, amount INT NOT NULL, status VARCHAR(255) NOT NULL, date DATE NOT NULL ); `; console.log(`Created "invoices" table`); // Insert data into the "invoices" table const insertedInvoices = await Promise.all( invoices.map( (invoice) => sql` INSERT INTO invoices (id, customer_id, amount, status, date) VALUES (${invoice.id}, ${invoice.customer_id}, ${invoice.amount}, ${invoice.status}, ${invoice.date}) ON CONFLICT (id) DO NOTHING; `, ), ); console.log(`Seeded ${insertedInvoices.length} invoices`); return { createTable, invoices: insertedInvoices, }; } catch (error) { console.error('Error seeding invoices:', error); throw error; } } async function seedCustomers() { try { // Create the "invoices" table if it doesn't exist const createTable = await sql` CREATE TABLE IF NOT EXISTS customers ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, image_url VARCHAR(255) NOT NULL ); `; console.log(`Created "customers" table`); // Insert data into the "customers" table const insertedCustomers = await Promise.all( customers.map( (customer) => sql` INSERT INTO customers (id, name, email, image_url) VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url}) ON CONFLICT (id) DO NOTHING; `, ), ); console.log(`Seeded ${insertedCustomers.length} customers`); return { createTable, customers: insertedCustomers, }; } catch (error) { console.error('Error seeding customers:', error); throw error; } } async function seedRevenue() { try { // Create the "revenue" table if it doesn't exist const createTable = await sql` CREATE TABLE IF NOT EXISTS revenue ( month VARCHAR(4) NOT NULL UNIQUE, revenue INT NOT NULL ); `; console.log(`Created "revenue" table`); // Insert data into the "revenue" table const insertedRevenue = await Promise.all( revenue.map( (rev) => sql` INSERT INTO revenue (month, revenue) VALUES (${rev.month}, ${rev.revenue}) ON CONFLICT (month) DO NOTHING; `, ), ); console.log(`Seeded ${insertedRevenue.length} revenue`); return { createTable, revenue: insertedRevenue, }; } catch (error) { console.error('Error seeding revenue:', error); throw error; } } (async () => { await seedCustomers(); await seedInvoices(); await seedRevenue(); })();