一覧へ戻る
drsimplegraffiti/ecommerce-ts(新しいタブで開きます)
主要言語: JavaScript
Typescript, Prisma, MySQL, ZOD
- star 数
- 5
- watcher 数
- 1
- fork 数
- 0
- issue 数
- 0
- 最終更新
- 2024/02/16
主要言語: JavaScript
Typescript, Prisma, MySQL, ZOD
create a nodemon.json file
{
"watch": [
"src"
],
"ext": ".js,.ts",
"exec": "npx ts-node ./src/index.ts"
}in the package.json file, add the start script
"scripts": { "start": "npx nodemon" },
By default you get a schema.prisma file and a prisma folder with postgresql as the default database.
// This is your Prisma schema file, // learn more about it in the docs: https://pris.ly/d/prisma-schema generator client { provider = "prisma-client-js" } datasource db { provider = "postgresql" url = env("DATABASE_URL") }
And also a .env file with the database url
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"
Install the prisma vscode extension for syntax highlighting and autocompletion.
Go to myswl and create an ecommerce database
CREATE DATABASE ecommerce;
Change the DATABASE_URL in the .env file to the new database url
DATABASE_URL="mysql://root:Bassguitar1@localhost:3306/ecommerce?schema=public"
// This is your Prisma schema file, // learn more about it in the docs: https://pris.ly/d/prisma-schema generator client { provider = "prisma-client-js" } datasource db { provider = "mysql" url = env("DATABASE_URL") } model User { id Int @id @default(autoincrement()) email String @unique name String? password String createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@map("users") // map to the users table in the database }
migrations folder with the migration file and also create the users table in the database.import { prismaClient } from ".."; //seed data const seedData = async () => { for (let i = 0; i < 10; i++) { await prismaClient.user.create({ data: { email: `user-${i + 10}-one@yopmail.com`, name: `user-${i + 10}-one`, password: "password", }, }); } } seedData().then(() => { console.log("seeded data"); process.exit(0); //exit the process after seeding data, 0 means success } ).catch((error) => { console.log(error); process.exit(1); //exit the process after seeding data, 1 means failure });
Steps to run the seed file
Or run npm run seed to run the seed command in the package.json file
{
"scripts": {
"seed": "ts-node --transpile-only ./src/utils/seed.ts"
}
}remove all .js file usin bash command