|
1 |
| -// import bcrypt from 'bcrypt'; |
2 |
| -// import { db } from '@vercel/postgres'; |
3 |
| -// import { invoices, customers, revenue, users } from '../lib/placeholder-data'; |
4 |
| - |
5 |
| -// const client = await db.connect(); |
6 |
| - |
7 |
| -// async function seedUsers() { |
8 |
| -// await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; |
9 |
| -// await client.sql` |
10 |
| -// CREATE TABLE IF NOT EXISTS users ( |
11 |
| -// id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, |
12 |
| -// name VARCHAR(255) NOT NULL, |
13 |
| -// email TEXT NOT NULL UNIQUE, |
14 |
| -// password TEXT NOT NULL |
15 |
| -// ); |
16 |
| -// `; |
17 |
| - |
18 |
| -// const insertedUsers = await Promise.all( |
19 |
| -// users.map(async (user) => { |
20 |
| -// const hashedPassword = await bcrypt.hash(user.password, 10); |
21 |
| -// return client.sql` |
22 |
| -// INSERT INTO users (id, name, email, password) |
23 |
| -// VALUES (${user.id}, ${user.name}, ${user.email}, ${hashedPassword}) |
24 |
| -// ON CONFLICT (id) DO NOTHING; |
25 |
| -// `; |
26 |
| -// }), |
27 |
| -// ); |
28 |
| - |
29 |
| -// return insertedUsers; |
30 |
| -// } |
31 |
| - |
32 |
| -// async function seedInvoices() { |
33 |
| -// await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; |
34 |
| - |
35 |
| -// await client.sql` |
36 |
| -// CREATE TABLE IF NOT EXISTS invoices ( |
37 |
| -// id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, |
38 |
| -// customer_id UUID NOT NULL, |
39 |
| -// amount INT NOT NULL, |
40 |
| -// status VARCHAR(255) NOT NULL, |
41 |
| -// date DATE NOT NULL |
42 |
| -// ); |
43 |
| -// `; |
44 |
| - |
45 |
| -// const insertedInvoices = await Promise.all( |
46 |
| -// invoices.map( |
47 |
| -// (invoice) => client.sql` |
48 |
| -// INSERT INTO invoices (customer_id, amount, status, date) |
49 |
| -// VALUES (${invoice.customer_id}, ${invoice.amount}, ${invoice.status}, ${invoice.date}) |
50 |
| -// ON CONFLICT (id) DO NOTHING; |
51 |
| -// `, |
52 |
| -// ), |
53 |
| -// ); |
54 |
| - |
55 |
| -// return insertedInvoices; |
56 |
| -// } |
57 |
| - |
58 |
| -// async function seedCustomers() { |
59 |
| -// await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; |
60 |
| - |
61 |
| -// await client.sql` |
62 |
| -// CREATE TABLE IF NOT EXISTS customers ( |
63 |
| -// id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, |
64 |
| -// name VARCHAR(255) NOT NULL, |
65 |
| -// email VARCHAR(255) NOT NULL, |
66 |
| -// image_url VARCHAR(255) NOT NULL |
67 |
| -// ); |
68 |
| -// `; |
69 |
| - |
70 |
| -// const insertedCustomers = await Promise.all( |
71 |
| -// customers.map( |
72 |
| -// (customer) => client.sql` |
73 |
| -// INSERT INTO customers (id, name, email, image_url) |
74 |
| -// VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url}) |
75 |
| -// ON CONFLICT (id) DO NOTHING; |
76 |
| -// `, |
77 |
| -// ), |
78 |
| -// ); |
79 |
| - |
80 |
| -// return insertedCustomers; |
81 |
| -// } |
82 |
| - |
83 |
| -// async function seedRevenue() { |
84 |
| -// await client.sql` |
85 |
| -// CREATE TABLE IF NOT EXISTS revenue ( |
86 |
| -// month VARCHAR(4) NOT NULL UNIQUE, |
87 |
| -// revenue INT NOT NULL |
88 |
| -// ); |
89 |
| -// `; |
90 |
| - |
91 |
| -// const insertedRevenue = await Promise.all( |
92 |
| -// revenue.map( |
93 |
| -// (rev) => client.sql` |
94 |
| -// INSERT INTO revenue (month, revenue) |
95 |
| -// VALUES (${rev.month}, ${rev.revenue}) |
96 |
| -// ON CONFLICT (month) DO NOTHING; |
97 |
| -// `, |
98 |
| -// ), |
99 |
| -// ); |
100 |
| - |
101 |
| -// return insertedRevenue; |
102 |
| -// } |
| 1 | +import bcrypt from 'bcrypt'; |
| 2 | +import { db } from '@vercel/postgres'; |
| 3 | +import { invoices, customers, revenue, users } from '../lib/placeholder-data'; |
| 4 | + |
| 5 | +const client = await db.connect(); |
| 6 | + |
| 7 | +async function seedUsers() { |
| 8 | + await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; |
| 9 | + await client.sql` |
| 10 | + CREATE TABLE IF NOT EXISTS users ( |
| 11 | + id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, |
| 12 | + name VARCHAR(255) NOT NULL, |
| 13 | + email TEXT NOT NULL UNIQUE, |
| 14 | + password TEXT NOT NULL |
| 15 | + ); |
| 16 | + `; |
| 17 | + |
| 18 | + const insertedUsers = await Promise.all( |
| 19 | + users.map(async (user) => { |
| 20 | + const hashedPassword = await bcrypt.hash(user.password, 10); |
| 21 | + return client.sql` |
| 22 | + INSERT INTO users (id, name, email, password) |
| 23 | + VALUES (${user.id}, ${user.name}, ${user.email}, ${hashedPassword}) |
| 24 | + ON CONFLICT (id) DO NOTHING; |
| 25 | + `; |
| 26 | + }), |
| 27 | + ); |
| 28 | + |
| 29 | + return insertedUsers; |
| 30 | +} |
| 31 | + |
| 32 | +async function seedInvoices() { |
| 33 | + await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; |
| 34 | + |
| 35 | + await client.sql` |
| 36 | + CREATE TABLE IF NOT EXISTS invoices ( |
| 37 | + id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, |
| 38 | + customer_id UUID NOT NULL, |
| 39 | + amount INT NOT NULL, |
| 40 | + status VARCHAR(255) NOT NULL, |
| 41 | + date DATE NOT NULL |
| 42 | + ); |
| 43 | + `; |
| 44 | + |
| 45 | + const insertedInvoices = await Promise.all( |
| 46 | + invoices.map( |
| 47 | + (invoice) => client.sql` |
| 48 | + INSERT INTO invoices (customer_id, amount, status, date) |
| 49 | + VALUES (${invoice.customer_id}, ${invoice.amount}, ${invoice.status}, ${invoice.date}) |
| 50 | + ON CONFLICT (id) DO NOTHING; |
| 51 | + `, |
| 52 | + ), |
| 53 | + ); |
| 54 | + |
| 55 | + return insertedInvoices; |
| 56 | +} |
| 57 | + |
| 58 | +async function seedCustomers() { |
| 59 | + await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; |
| 60 | + |
| 61 | + await client.sql` |
| 62 | + CREATE TABLE IF NOT EXISTS customers ( |
| 63 | + id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, |
| 64 | + name VARCHAR(255) NOT NULL, |
| 65 | + email VARCHAR(255) NOT NULL, |
| 66 | + image_url VARCHAR(255) NOT NULL |
| 67 | + ); |
| 68 | + `; |
| 69 | + |
| 70 | + const insertedCustomers = await Promise.all( |
| 71 | + customers.map( |
| 72 | + (customer) => client.sql` |
| 73 | + INSERT INTO customers (id, name, email, image_url) |
| 74 | + VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url}) |
| 75 | + ON CONFLICT (id) DO NOTHING; |
| 76 | + `, |
| 77 | + ), |
| 78 | + ); |
| 79 | + |
| 80 | + return insertedCustomers; |
| 81 | +} |
| 82 | + |
| 83 | +async function seedRevenue() { |
| 84 | + await client.sql` |
| 85 | + CREATE TABLE IF NOT EXISTS revenue ( |
| 86 | + month VARCHAR(4) NOT NULL UNIQUE, |
| 87 | + revenue INT NOT NULL |
| 88 | + ); |
| 89 | + `; |
| 90 | + |
| 91 | + const insertedRevenue = await Promise.all( |
| 92 | + revenue.map( |
| 93 | + (rev) => client.sql` |
| 94 | + INSERT INTO revenue (month, revenue) |
| 95 | + VALUES (${rev.month}, ${rev.revenue}) |
| 96 | + ON CONFLICT (month) DO NOTHING; |
| 97 | + `, |
| 98 | + ), |
| 99 | + ); |
| 100 | + |
| 101 | + return insertedRevenue; |
| 102 | +} |
103 | 103 |
|
104 | 104 | export async function GET() {
|
105 |
| - return Response.json({ |
106 |
| - message: |
107 |
| - 'Uncomment this file and remove this line. You can delete this file when you are finished.', |
108 |
| - }); |
109 |
| - // try { |
110 |
| - // await client.sql`BEGIN`; |
111 |
| - // await seedUsers(); |
112 |
| - // await seedCustomers(); |
113 |
| - // await seedInvoices(); |
114 |
| - // await seedRevenue(); |
115 |
| - // await client.sql`COMMIT`; |
116 |
| - |
117 |
| - // return Response.json({ message: 'Database seeded successfully' }); |
118 |
| - // } catch (error) { |
119 |
| - // await client.sql`ROLLBACK`; |
120 |
| - // return Response.json({ error }, { status: 500 }); |
121 |
| - // } |
| 105 | + // return Response.json({ |
| 106 | + // message: |
| 107 | + // 'Uncomment this file and remove this line. You can delete this file when you are finished.', |
| 108 | + // }); |
| 109 | + try { |
| 110 | + await client.sql`BEGIN`; |
| 111 | + await seedUsers(); |
| 112 | + await seedCustomers(); |
| 113 | + await seedInvoices(); |
| 114 | + await seedRevenue(); |
| 115 | + await client.sql`COMMIT`; |
| 116 | + |
| 117 | + return Response.json({ message: 'Database seeded successfully' }); |
| 118 | + } catch (error) { |
| 119 | + await client.sql`ROLLBACK`; |
| 120 | + return Response.json({ error }, { status: 500 }); |
| 121 | + } |
122 | 122 | }
|
0 commit comments