From fc7d91e98af144b7df94601729130f517a6813e1 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Sat, 13 Mar 2021 10:10:40 +0700 Subject: [PATCH] Init the prisma starter profile for cubetiq team and add some functions for migration and playaround --- .gitignore | 5 ++ package.json | 20 +++++++ prisma/.env | 1 + prisma/dev.db | Bin 0 -> 36864 bytes .../20210313025521_add_profile/migration.sql | 19 +++++++ .../20210313030124_add_profile/migration.sql | 12 +++++ prisma/migrations/migration_lock.toml | 3 ++ prisma/schema.prisma | 34 ++++++++++++ src/main.ts | 51 ++++++++++++++++++ tsconfig.json | 9 ++++ 10 files changed, 154 insertions(+) create mode 100644 .gitignore create mode 100644 package.json create mode 100644 prisma/.env create mode 100644 prisma/dev.db create mode 100644 prisma/migrations/20210313025521_add_profile/migration.sql create mode 100644 prisma/migrations/20210313030124_add_profile/migration.sql create mode 100644 prisma/migrations/migration_lock.toml create mode 100644 prisma/schema.prisma create mode 100644 src/main.ts create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d7807a8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +node_modules +yarn.lock +dist/ +build/ +.DS_Store \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..db7981f --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/prisma/.env b/prisma/.env new file mode 100644 index 0000000..d91f958 --- /dev/null +++ b/prisma/.env @@ -0,0 +1 @@ +DATABASE_URL="file:./dev.db" diff --git a/prisma/dev.db b/prisma/dev.db new file mode 100644 index 0000000000000000000000000000000000000000..b04aa6c076075505037b1664e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|1XCBhyUwD^Z|{400bZa0SG_<0uX=z W1Rwwb2teR}5?GC}EXPG6@%X>~w~5;T literal 0 HcmV?d00001 diff --git a/prisma/migrations/20210313025521_add_profile/migration.sql b/prisma/migrations/20210313025521_add_profile/migration.sql new file mode 100644 index 0000000..4141575 --- /dev/null +++ b/prisma/migrations/20210313025521_add_profile/migration.sql @@ -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"); diff --git a/prisma/migrations/20210313030124_add_profile/migration.sql b/prisma/migrations/20210313030124_add_profile/migration.sql new file mode 100644 index 0000000..bd950e5 --- /dev/null +++ b/prisma/migrations/20210313030124_add_profile/migration.sql @@ -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"); diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..e5e5c47 --- /dev/null +++ b/prisma/migrations/migration_lock.toml @@ -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" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma new file mode 100644 index 0000000..aec36b4 --- /dev/null +++ b/prisma/schema.prisma @@ -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) +} diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..e235c18 --- /dev/null +++ b/src/main.ts @@ -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(); + }); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..7095025 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "sourceMap": true, + "outDir": "dist", + "strict": true, + "lib": ["esnext", "dom"], + "esModuleInterop": true + } +}