Cleanup from previous simplification PR.

This commit is contained in:
Lee Robinson
2024-07-05 22:15:52 -05:00
parent e4ecb6679d
commit d379162091
4 changed files with 206 additions and 383 deletions

View File

@@ -1,118 +1,122 @@
import bcrypt from 'bcrypt';
import { db } from '@vercel/postgres';
import { invoices, customers, revenue, users } from '../lib/placeholder-data';
// import bcrypt from 'bcrypt';
// import { db } from '@vercel/postgres';
// import { invoices, customers, revenue, users } from '../lib/placeholder-data';
const client = await db.connect();
// const client = await db.connect();
async function seedUsers() {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
await client.sql`
CREATE TABLE IF NOT EXISTS users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
);
`;
// async function seedUsers() {
// await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
// await client.sql`
// CREATE TABLE IF NOT EXISTS users (
// id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
// name VARCHAR(255) NOT NULL,
// email TEXT NOT NULL UNIQUE,
// password TEXT NOT NULL
// );
// `;
const insertedUsers = await Promise.all(
users.map(async (user) => {
const hashedPassword = await bcrypt.hash(user.password, 10);
return client.sql`
INSERT INTO users (id, name, email, password)
VALUES (${user.id}, ${user.name}, ${user.email}, ${hashedPassword})
ON CONFLICT (id) DO NOTHING;
`;
}),
);
// const insertedUsers = await Promise.all(
// users.map(async (user) => {
// const hashedPassword = await bcrypt.hash(user.password, 10);
// return client.sql`
// INSERT INTO users (id, name, email, password)
// VALUES (${user.id}, ${user.name}, ${user.email}, ${hashedPassword})
// ON CONFLICT (id) DO NOTHING;
// `;
// }),
// );
return insertedUsers;
}
// return insertedUsers;
// }
async function seedInvoices() {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
// async function seedInvoices() {
// await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
await client.sql`
CREATE TABLE IF NOT EXISTS invoices (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
customer_id UUID NOT NULL,
amount INT NOT NULL,
status VARCHAR(255) NOT NULL,
date DATE NOT NULL
);
`;
// await client.sql`
// CREATE TABLE IF NOT EXISTS invoices (
// id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
// customer_id UUID NOT NULL,
// amount INT NOT NULL,
// status VARCHAR(255) NOT NULL,
// date DATE NOT NULL
// );
// `;
const insertedInvoices = await Promise.all(
invoices.map(
(invoice) => client.sql`
INSERT INTO invoices (customer_id, amount, status, date)
VALUES (${invoice.customer_id}, ${invoice.amount}, ${invoice.status}, ${invoice.date})
ON CONFLICT (id) DO NOTHING;
`,
),
);
// const insertedInvoices = await Promise.all(
// invoices.map(
// (invoice) => client.sql`
// INSERT INTO invoices (customer_id, amount, status, date)
// VALUES (${invoice.customer_id}, ${invoice.amount}, ${invoice.status}, ${invoice.date})
// ON CONFLICT (id) DO NOTHING;
// `,
// ),
// );
return insertedInvoices;
}
// return insertedInvoices;
// }
async function seedCustomers() {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
// async function seedCustomers() {
// await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
await client.sql`
CREATE TABLE IF NOT EXISTS customers (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
image_url VARCHAR(255) NOT NULL
);
`;
// await client.sql`
// CREATE TABLE IF NOT EXISTS customers (
// id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
// name VARCHAR(255) NOT NULL,
// email VARCHAR(255) NOT NULL,
// image_url VARCHAR(255) NOT NULL
// );
// `;
const insertedCustomers = await Promise.all(
customers.map(
(customer) => client.sql`
INSERT INTO customers (id, name, email, image_url)
VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url})
ON CONFLICT (id) DO NOTHING;
`,
),
);
// const insertedCustomers = await Promise.all(
// customers.map(
// (customer) => client.sql`
// INSERT INTO customers (id, name, email, image_url)
// VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url})
// ON CONFLICT (id) DO NOTHING;
// `,
// ),
// );
return insertedCustomers;
}
// return insertedCustomers;
// }
async function seedRevenue() {
await client.sql`
CREATE TABLE IF NOT EXISTS revenue (
month VARCHAR(4) NOT NULL UNIQUE,
revenue INT NOT NULL
);
`;
// async function seedRevenue() {
// await client.sql`
// CREATE TABLE IF NOT EXISTS revenue (
// month VARCHAR(4) NOT NULL UNIQUE,
// revenue INT NOT NULL
// );
// `;
const insertedRevenue = await Promise.all(
revenue.map(
(rev) => client.sql`
INSERT INTO revenue (month, revenue)
VALUES (${rev.month}, ${rev.revenue})
ON CONFLICT (month) DO NOTHING;
`,
),
);
// const insertedRevenue = await Promise.all(
// revenue.map(
// (rev) => client.sql`
// INSERT INTO revenue (month, revenue)
// VALUES (${rev.month}, ${rev.revenue})
// ON CONFLICT (month) DO NOTHING;
// `,
// ),
// );
return insertedRevenue;
}
// return insertedRevenue;
// }
export async function GET() {
try {
await client.sql`BEGIN`;
await seedUsers();
await seedCustomers();
await seedInvoices();
await seedRevenue();
await client.sql`COMMIT`;
return Response.json({
message:
'Uncomment this file and remove this line. You can delete this file when you are finished.',
});
// try {
// await client.sql`BEGIN`;
// await seedUsers();
// await seedCustomers();
// await seedInvoices();
// await seedRevenue();
// await client.sql`COMMIT`;
return Response.json({ message: 'Database seeded successfully' });
} catch (error) {
await client.sql`ROLLBACK`;
return Response.json({ error }, { status: 500 });
}
// return Response.json({ message: 'Database seeded successfully' });
// } catch (error) {
// await client.sql`ROLLBACK`;
// return Response.json({ error }, { status: 500 });
// }
}

View File

@@ -1,118 +1,122 @@
import bcrypt from 'bcrypt';
import { db } from '@vercel/postgres';
import { invoices, customers, revenue, users } from '../lib/placeholder-data';
// import bcrypt from 'bcrypt';
// import { db } from '@vercel/postgres';
// import { invoices, customers, revenue, users } from '../lib/placeholder-data';
const client = await db.connect();
// const client = await db.connect();
async function seedUsers() {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
await client.sql`
CREATE TABLE IF NOT EXISTS users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
);
`;
// async function seedUsers() {
// await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
// await client.sql`
// CREATE TABLE IF NOT EXISTS users (
// id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
// name VARCHAR(255) NOT NULL,
// email TEXT NOT NULL UNIQUE,
// password TEXT NOT NULL
// );
// `;
const insertedUsers = await Promise.all(
users.map(async (user) => {
const hashedPassword = await bcrypt.hash(user.password, 10);
return client.sql`
INSERT INTO users (id, name, email, password)
VALUES (${user.id}, ${user.name}, ${user.email}, ${hashedPassword})
ON CONFLICT (id) DO NOTHING;
`;
}),
);
// const insertedUsers = await Promise.all(
// users.map(async (user) => {
// const hashedPassword = await bcrypt.hash(user.password, 10);
// return client.sql`
// INSERT INTO users (id, name, email, password)
// VALUES (${user.id}, ${user.name}, ${user.email}, ${hashedPassword})
// ON CONFLICT (id) DO NOTHING;
// `;
// }),
// );
return insertedUsers;
}
// return insertedUsers;
// }
async function seedInvoices() {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
// async function seedInvoices() {
// await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
await client.sql`
CREATE TABLE IF NOT EXISTS invoices (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
customer_id UUID NOT NULL,
amount INT NOT NULL,
status VARCHAR(255) NOT NULL,
date DATE NOT NULL
);
`;
// await client.sql`
// CREATE TABLE IF NOT EXISTS invoices (
// id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
// customer_id UUID NOT NULL,
// amount INT NOT NULL,
// status VARCHAR(255) NOT NULL,
// date DATE NOT NULL
// );
// `;
const insertedInvoices = await Promise.all(
invoices.map(
(invoice) => client.sql`
INSERT INTO invoices (customer_id, amount, status, date)
VALUES (${invoice.customer_id}, ${invoice.amount}, ${invoice.status}, ${invoice.date})
ON CONFLICT (id) DO NOTHING;
`,
),
);
// const insertedInvoices = await Promise.all(
// invoices.map(
// (invoice) => client.sql`
// INSERT INTO invoices (customer_id, amount, status, date)
// VALUES (${invoice.customer_id}, ${invoice.amount}, ${invoice.status}, ${invoice.date})
// ON CONFLICT (id) DO NOTHING;
// `,
// ),
// );
return insertedInvoices;
}
// return insertedInvoices;
// }
async function seedCustomers() {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
// async function seedCustomers() {
// await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
await client.sql`
CREATE TABLE IF NOT EXISTS customers (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
image_url VARCHAR(255) NOT NULL
);
`;
// await client.sql`
// CREATE TABLE IF NOT EXISTS customers (
// id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
// name VARCHAR(255) NOT NULL,
// email VARCHAR(255) NOT NULL,
// image_url VARCHAR(255) NOT NULL
// );
// `;
const insertedCustomers = await Promise.all(
customers.map(
(customer) => client.sql`
INSERT INTO customers (id, name, email, image_url)
VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url})
ON CONFLICT (id) DO NOTHING;
`,
),
);
// const insertedCustomers = await Promise.all(
// customers.map(
// (customer) => client.sql`
// INSERT INTO customers (id, name, email, image_url)
// VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url})
// ON CONFLICT (id) DO NOTHING;
// `,
// ),
// );
return insertedCustomers;
}
// return insertedCustomers;
// }
async function seedRevenue() {
await client.sql`
CREATE TABLE IF NOT EXISTS revenue (
month VARCHAR(4) NOT NULL UNIQUE,
revenue INT NOT NULL
);
`;
// async function seedRevenue() {
// await client.sql`
// CREATE TABLE IF NOT EXISTS revenue (
// month VARCHAR(4) NOT NULL UNIQUE,
// revenue INT NOT NULL
// );
// `;
const insertedRevenue = await Promise.all(
revenue.map(
(rev) => client.sql`
INSERT INTO revenue (month, revenue)
VALUES (${rev.month}, ${rev.revenue})
ON CONFLICT (month) DO NOTHING;
`,
),
);
// const insertedRevenue = await Promise.all(
// revenue.map(
// (rev) => client.sql`
// INSERT INTO revenue (month, revenue)
// VALUES (${rev.month}, ${rev.revenue})
// ON CONFLICT (month) DO NOTHING;
// `,
// ),
// );
return insertedRevenue;
}
// return insertedRevenue;
// }
export async function GET() {
try {
await client.sql`BEGIN`;
await seedUsers();
await seedCustomers();
await seedInvoices();
await seedRevenue();
await client.sql`COMMIT`;
return Response.json({
message:
'Uncomment this file and remove this line. You can delete this file when you are finished.',
});
// try {
// await client.sql`BEGIN`;
// await seedUsers();
// await seedCustomers();
// await seedInvoices();
// await seedRevenue();
// await client.sql`COMMIT`;
return Response.json({ message: 'Database seeded successfully' });
} catch (error) {
await client.sql`ROLLBACK`;
return Response.json({ error }, { status: 500 });
}
// return Response.json({ message: 'Database seeded successfully' });
// } catch (error) {
// await client.sql`ROLLBACK`;
// return Response.json({ error }, { status: 500 });
// }
}

View File

@@ -1,6 +0,0 @@
const styleguide = require('@vercel/style-guide/prettier');
module.exports = {
...styleguide,
plugins: [...styleguide.plugins, 'prettier-plugin-tailwindcss'],
};

View File

@@ -1,179 +0,0 @@
const { db } = require('@vercel/postgres');
const {
invoices,
customers,
revenue,
users,
} = require('../app/lib/placeholder-data.js');
const bcrypt = require('bcrypt');
async function seedUsers(client) {
try {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
// Create the "users" table if it doesn't exist
const createTable = await client.sql`
CREATE TABLE IF NOT EXISTS users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
);
`;
console.log(`Created "users" table`);
// Insert data into the "users" table
const insertedUsers = await Promise.all(
users.map(async (user) => {
const hashedPassword = await bcrypt.hash(user.password, 10);
return client.sql`
INSERT INTO users (id, name, email, password)
VALUES (${user.id}, ${user.name}, ${user.email}, ${hashedPassword})
ON CONFLICT (id) DO NOTHING;
`;
}),
);
console.log(`Seeded ${insertedUsers.length} users`);
return {
createTable,
users: insertedUsers,
};
} catch (error) {
console.error('Error seeding users:', error);
throw error;
}
}
async function seedInvoices(client) {
try {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
// Create the "invoices" table if it doesn't exist
const createTable = await client.sql`
CREATE TABLE IF NOT EXISTS invoices (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
customer_id UUID 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) => client.sql`
INSERT INTO invoices (customer_id, amount, status, date)
VALUES (${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(client) {
try {
await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`;
// Create the "customers" table if it doesn't exist
const createTable = await client.sql`
CREATE TABLE IF NOT EXISTS customers (
id UUID DEFAULT uuid_generate_v4() 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) => client.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(client) {
try {
// Create the "revenue" table if it doesn't exist
const createTable = await client.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) => client.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 function main() {
const client = await db.connect();
await seedUsers(client);
await seedCustomers(client);
await seedInvoices(client);
await seedRevenue(client);
await client.end();
}
main().catch((err) => {
console.error(
'An error occurred while attempting to seed the database:',
err,
);
});