Init the prisma starter profile for cubetiq team and add some functions for migration and playaround
This commit is contained in:
commit
fc7d91e98a
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
node_modules
|
||||
yarn.lock
|
||||
dist/
|
||||
build/
|
||||
.DS_Store
|
20
package.json
Normal file
20
package.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "prisma-starter",
|
||||
"license": "MIT",
|
||||
"main": "main.ts",
|
||||
"devDependencies": {
|
||||
"prisma": "2.18.0",
|
||||
"ts-node": "9.1.1",
|
||||
"typescript": "4.2.3"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "ts-node ./src/main.ts",
|
||||
"migrate": "prisma migrate dev --name add-profile --preview-feature"
|
||||
},
|
||||
"dependencies": {
|
||||
"@prisma/client": "2.18.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
}
|
||||
}
|
1
prisma/.env
Normal file
1
prisma/.env
Normal file
@ -0,0 +1 @@
|
||||
DATABASE_URL="file:./dev.db"
|
BIN
prisma/dev.db
Normal file
BIN
prisma/dev.db
Normal file
Binary file not shown.
19
prisma/migrations/20210313025521_add_profile/migration.sql
Normal file
19
prisma/migrations/20210313025521_add_profile/migration.sql
Normal file
@ -0,0 +1,19 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "Post" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"title" TEXT NOT NULL,
|
||||
"content" TEXT,
|
||||
"published" BOOLEAN NOT NULL DEFAULT false,
|
||||
"authorId" INTEGER,
|
||||
FOREIGN KEY ("authorId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "User" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"email" TEXT NOT NULL,
|
||||
"name" TEXT
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "User.email_unique" ON "User"("email");
|
12
prisma/migrations/20210313030124_add_profile/migration.sql
Normal file
12
prisma/migrations/20210313030124_add_profile/migration.sql
Normal file
@ -0,0 +1,12 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "Profile" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"bio" TEXT NOT NULL,
|
||||
"avatar" TEXT,
|
||||
"userId" INTEGER NOT NULL,
|
||||
"enabled" BOOLEAN NOT NULL DEFAULT true,
|
||||
FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Profile.userId_unique" ON "Profile"("userId");
|
3
prisma/migrations/migration_lock.toml
Normal file
3
prisma/migrations/migration_lock.toml
Normal file
@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "sqlite"
|
34
prisma/schema.prisma
Normal file
34
prisma/schema.prisma
Normal file
@ -0,0 +1,34 @@
|
||||
datasource db {
|
||||
provider = "sqlite"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
model Post {
|
||||
id Int @id @default(autoincrement())
|
||||
title String
|
||||
content String?
|
||||
published Boolean @default(false)
|
||||
author User? @relation(fields: [authorId], references: [id])
|
||||
authorId Int?
|
||||
}
|
||||
|
||||
model User {
|
||||
id Int @id @default(autoincrement())
|
||||
email String @unique
|
||||
name String?
|
||||
posts Post[]
|
||||
profile Profile?
|
||||
}
|
||||
|
||||
model Profile {
|
||||
id Int @id @default(autoincrement())
|
||||
bio String
|
||||
avatar String?
|
||||
user User @relation(fields: [userId], references: [id])
|
||||
userId Int @unique
|
||||
enabled Boolean @default(true)
|
||||
}
|
51
src/main.ts
Normal file
51
src/main.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import { PrismaClient, User } from "@prisma/client";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
// A `main` function so that you can use async/await
|
||||
async function main() {
|
||||
// create user
|
||||
await createUser()
|
||||
.then((user) => {
|
||||
// create profile for user
|
||||
createProfile(user);
|
||||
})
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
});
|
||||
|
||||
const allUsers = await prisma.user.findMany();
|
||||
console.log("All users => ", allUsers);
|
||||
}
|
||||
|
||||
async function createUser() {
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
name: "CUBETIQ Solution",
|
||||
email: "ops@cubetiqs.com",
|
||||
},
|
||||
});
|
||||
|
||||
console.log("User => ", user);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
async function createProfile(user: User) {
|
||||
const profile = await prisma.profile.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
bio: "Software Company",
|
||||
},
|
||||
});
|
||||
|
||||
console.log("Profile => ", profile);
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
throw e;
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
9
tsconfig.json
Normal file
9
tsconfig.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"sourceMap": true,
|
||||
"outDir": "dist",
|
||||
"strict": true,
|
||||
"lib": ["esnext", "dom"],
|
||||
"esModuleInterop": true
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user