setup db mariadb and postgres for strapi and next
This commit is contained in:
147
yoga-app/src/app/seed/placeholder-data.ts
Normal file
147
yoga-app/src/app/seed/placeholder-data.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
// This file contains placeholder data that you'll be replacing with real data in the Data Fetching chapter:
|
||||
// https://nextjs.org/learn/dashboard-app/fetching-data
|
||||
const users = [
|
||||
{
|
||||
id: '410544b2-4001-4271-9855-fec4b6a6442a',
|
||||
name: 'User',
|
||||
email: 'user@nextmail.com',
|
||||
password: '123456',
|
||||
},
|
||||
];
|
||||
|
||||
const customers = [
|
||||
{
|
||||
id: 'd6e15727-9fe1-4961-8c5b-ea44a9bd81aa',
|
||||
name: 'Evil Rabbit',
|
||||
email: 'evil@rabbit.com',
|
||||
image_url: '/customers/evil-rabbit.png',
|
||||
},
|
||||
{
|
||||
id: '3958dc9e-712f-4377-85e9-fec4b6a6442a',
|
||||
name: 'Delba de Oliveira',
|
||||
email: 'delba@oliveira.com',
|
||||
image_url: '/customers/delba-de-oliveira.png',
|
||||
},
|
||||
{
|
||||
id: '3958dc9e-742f-4377-85e9-fec4b6a6442a',
|
||||
name: 'Lee Robinson',
|
||||
email: 'lee@robinson.com',
|
||||
image_url: '/customers/lee-robinson.png',
|
||||
},
|
||||
{
|
||||
id: '76d65c26-f784-44a2-ac19-586678f7c2f2',
|
||||
name: 'Michael Novotny',
|
||||
email: 'michael@novotny.com',
|
||||
image_url: '/customers/michael-novotny.png',
|
||||
},
|
||||
{
|
||||
id: 'CC27C14A-0ACF-4F4A-A6C9-D45682C144B9',
|
||||
name: 'Amy Burns',
|
||||
email: 'amy@burns.com',
|
||||
image_url: '/customers/amy-burns.png',
|
||||
},
|
||||
{
|
||||
id: '13D07535-C59E-4157-A011-F8D2EF4E0CBB',
|
||||
name: 'Balazs Orban',
|
||||
email: 'balazs@orban.com',
|
||||
image_url: '/customers/balazs-orban.png',
|
||||
},
|
||||
];
|
||||
|
||||
const invoices: any[] = [
|
||||
// {
|
||||
// customer_id: customers[0].id,
|
||||
// amount: 15795,
|
||||
// status: 'pending',
|
||||
// date: '2022-12-06',
|
||||
// },
|
||||
// {
|
||||
// customer_id: customers[1].id,
|
||||
// amount: 20348,
|
||||
// status: 'pending',
|
||||
// date: '2022-11-14',
|
||||
// },
|
||||
// {
|
||||
// customer_id: customers[4].id,
|
||||
// amount: 3040,
|
||||
// status: 'paid',
|
||||
// date: '2022-10-29',
|
||||
// },
|
||||
// {
|
||||
// customer_id: customers[3].id,
|
||||
// amount: 44800,
|
||||
// status: 'paid',
|
||||
// date: '2023-09-10',
|
||||
// },
|
||||
// {
|
||||
// customer_id: customers[5].id,
|
||||
// amount: 34577,
|
||||
// status: 'pending',
|
||||
// date: '2023-08-05',
|
||||
// },
|
||||
// {
|
||||
// customer_id: customers[2].id,
|
||||
// amount: 54246,
|
||||
// status: 'pending',
|
||||
// date: '2023-07-16',
|
||||
// },
|
||||
// {
|
||||
// customer_id: customers[0].id,
|
||||
// amount: 666,
|
||||
// status: 'pending',
|
||||
// date: '2023-06-27',
|
||||
// },
|
||||
// {
|
||||
// customer_id: customers[3].id,
|
||||
// amount: 32545,
|
||||
// status: 'paid',
|
||||
// date: '2023-06-09',
|
||||
// },
|
||||
// {
|
||||
// customer_id: customers[4].id,
|
||||
// amount: 1250,
|
||||
// status: 'paid',
|
||||
// date: '2023-06-17',
|
||||
// },
|
||||
// {
|
||||
// customer_id: customers[5].id,
|
||||
// amount: 8546,
|
||||
// status: 'paid',
|
||||
// date: '2023-06-07',
|
||||
// },
|
||||
// {
|
||||
// customer_id: customers[1].id,
|
||||
// amount: 500,
|
||||
// status: 'paid',
|
||||
// date: '2023-08-19',
|
||||
// },
|
||||
// {
|
||||
// customer_id: customers[5].id,
|
||||
// amount: 8945,
|
||||
// status: 'paid',
|
||||
// date: '2023-06-03',
|
||||
// },
|
||||
// {
|
||||
// customer_id: customers[2].id,
|
||||
// amount: 1000,
|
||||
// status: 'paid',
|
||||
// date: '2022-06-05',
|
||||
// },
|
||||
];
|
||||
|
||||
const revenue = [
|
||||
{ month: 'Jan', revenue: 2000 },
|
||||
{ month: 'Feb', revenue: 1800 },
|
||||
{ month: 'Mar', revenue: 2200 },
|
||||
{ month: 'Apr', revenue: 2500 },
|
||||
{ month: 'May', revenue: 2300 },
|
||||
{ month: 'Jun', revenue: 3200 },
|
||||
{ month: 'Jul', revenue: 3500 },
|
||||
{ month: 'Aug', revenue: 3700 },
|
||||
{ month: 'Sep', revenue: 2500 },
|
||||
{ month: 'Oct', revenue: 2800 },
|
||||
{ month: 'Nov', revenue: 3000 },
|
||||
{ month: 'Dec', revenue: 4800 },
|
||||
];
|
||||
|
||||
export { users, customers, invoices, revenue };
|
||||
123
yoga-app/src/app/seed/route.ts
Normal file
123
yoga-app/src/app/seed/route.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import bcrypt from 'bcrypt';
|
||||
import { invoices, customers, revenue, users } from './placeholder-data';
|
||||
|
||||
// const client = await connectionPool.connect();
|
||||
|
||||
import connectionPool from "../../db/db";
|
||||
|
||||
async function seedUsers() {
|
||||
await connectionPool.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
|
||||
await connectionPool.query(`
|
||||
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 connectionPool.query(`
|
||||
INSERT INTO users (id, name, email, password)
|
||||
VALUES ('${user.id}', '${user.name}', '${user.email}', '${hashedPassword}')
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
`);
|
||||
}),
|
||||
);
|
||||
|
||||
return insertedUsers;
|
||||
}
|
||||
|
||||
async function seedInvoices() {
|
||||
await connectionPool.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
|
||||
|
||||
await connectionPool.query(`
|
||||
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) => connectionPool.query(`
|
||||
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;
|
||||
}
|
||||
|
||||
async function seedCustomers() {
|
||||
await connectionPool.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
|
||||
|
||||
await connectionPool.query(`
|
||||
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) => connectionPool.query(`
|
||||
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;
|
||||
}
|
||||
|
||||
async function seedRevenue() {
|
||||
await connectionPool.query(`
|
||||
CREATE TABLE IF NOT EXISTS revenue (
|
||||
month VARCHAR(4) NOT NULL UNIQUE,
|
||||
revenue INT NOT NULL
|
||||
);
|
||||
`);
|
||||
|
||||
const insertedRevenue = await Promise.all(
|
||||
revenue.map(
|
||||
(rev) => connectionPool.query(`
|
||||
INSERT INTO revenue (month, revenue)
|
||||
VALUES ('${rev.month}', '${rev.revenue}')
|
||||
ON CONFLICT (month) DO NOTHING;
|
||||
`),
|
||||
),
|
||||
);
|
||||
|
||||
return insertedRevenue;
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
// return Response.json({
|
||||
// message:
|
||||
// 'Uncomment this file and remove this line. You can delete this file when you are finished.',
|
||||
// });
|
||||
try {
|
||||
await connectionPool.query(`BEGIN`);
|
||||
await seedUsers();
|
||||
await seedCustomers();
|
||||
// await seedInvoices();
|
||||
// await seedRevenue();
|
||||
await connectionPool.query(`COMMIT`);
|
||||
|
||||
return Response.json({ message: 'Database seeded successfully' });
|
||||
} catch (error) {
|
||||
await connectionPool.query(`ROLLBACK`);
|
||||
return Response.json({ error }, { status: 500 });
|
||||
}
|
||||
}
|
||||
13
yoga-app/src/db/db.ts
Normal file
13
yoga-app/src/db/db.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
const { Pool } = require('pg');
|
||||
require ('dotenv').config();
|
||||
|
||||
const connectionPool = new Pool({
|
||||
connectionString: process.env.POSTGRES_URL,
|
||||
user: process.env.POSTGRES_USER,
|
||||
host: process.env.POSTGRES_HOST,
|
||||
database: process.env.POSTGRES_DATABASE,
|
||||
password: process.env.POSTGRES_PASSWORD,
|
||||
port: process.env.POSTGRES_PORT,
|
||||
});
|
||||
|
||||
export default connectionPool;
|
||||
Reference in New Issue
Block a user