-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #566 from ggwadera/db/prisma
Start replacing Sequelize ORM with Prisma
- Loading branch information
Showing
12 changed files
with
394 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
import { prisma } from '../../prisma' | ||
import { lessons } from './lessons' | ||
import db from '../../helpers/dbload' | ||
|
||
describe('Lessons resolver', () => { | ||
const { Lesson } = db | ||
|
||
test('lessons should return an empty array', async () => { | ||
Lesson.findAll = jest.fn().mockReturnValue([]) | ||
prisma.lesson.findMany = jest.fn().mockReturnValue([]) | ||
expect(lessons()).toEqual([]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
import db from '../../helpers/dbload' | ||
|
||
const { Lesson } = db | ||
import { prisma } from '../../prisma' | ||
|
||
export const lessons = () => { | ||
return Lesson.findAll({ | ||
include: ['challenges'], | ||
order: [ | ||
['order', 'ASC'], | ||
['challenges', 'order', 'ASC'] | ||
] | ||
return prisma.lesson.findMany({ | ||
include: { challenges: { orderBy: { order: 'asc' } } }, | ||
orderBy: { | ||
order: 'asc' | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { PrismaClient } from '@prisma/client' | ||
|
||
declare global { | ||
// eslint-disable-next-line no-var | ||
var prismag: PrismaClient | ||
} | ||
|
||
const prismaOptions = { | ||
datasources: { | ||
db: { | ||
url: `postgresql://${process.env.DB_USER}:${process.env.DB_PW}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}` | ||
} | ||
} | ||
} | ||
|
||
// Avoid instantiating too many instances of Prisma in development | ||
// https://www.prisma.io/docs/support/help-articles/nextjs-prisma-client-dev-practices#problem | ||
export let prisma: PrismaClient | ||
|
||
// check to use this workaround only in development and not in production | ||
if (process.env.NODE_ENV === 'production') { | ||
prisma = new PrismaClient(prismaOptions) | ||
} else { | ||
if (!global.prismag) { | ||
global.prismag = new PrismaClient(prismaOptions) | ||
} | ||
prisma = global.prismag | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
-- CreateTable | ||
CREATE TABLE "Session" ( | ||
"sid" VARCHAR(36) NOT NULL, | ||
"expires" TIMESTAMPTZ(6), | ||
"data" TEXT, | ||
"createdAt" TIMESTAMPTZ(6) NOT NULL, | ||
"updatedAt" TIMESTAMPTZ(6) NOT NULL, | ||
|
||
PRIMARY KEY ("sid") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "alerts" ( | ||
"id" SERIAL NOT NULL, | ||
"text" VARCHAR(255), | ||
"type" VARCHAR(255), | ||
"url" VARCHAR(255), | ||
"urlCaption" VARCHAR(255), | ||
"createdAt" TIMESTAMPTZ(6) NOT NULL, | ||
"updatedAt" TIMESTAMPTZ(6) NOT NULL, | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "challenges" ( | ||
"id" SERIAL NOT NULL, | ||
"status" VARCHAR(255), | ||
"description" TEXT, | ||
"title" VARCHAR(255), | ||
"order" INTEGER, | ||
"createdAt" TIMESTAMPTZ(6) NOT NULL, | ||
"updatedAt" TIMESTAMPTZ(6) NOT NULL, | ||
"lessonId" INTEGER, | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "lessons" ( | ||
"id" SERIAL NOT NULL, | ||
"description" TEXT, | ||
"docUrl" VARCHAR(255), | ||
"githubUrl" VARCHAR(255), | ||
"videoUrl" VARCHAR(255), | ||
"order" INTEGER, | ||
"title" VARCHAR(255), | ||
"createdAt" TIMESTAMPTZ(6) NOT NULL, | ||
"updatedAt" TIMESTAMPTZ(6) NOT NULL, | ||
"chatUrl" VARCHAR(255), | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "stars" ( | ||
"id" SERIAL NOT NULL, | ||
"lessonId" INTEGER, | ||
"createdAt" TIMESTAMPTZ(6) NOT NULL, | ||
"updatedAt" TIMESTAMPTZ(6) NOT NULL, | ||
"studentId" INTEGER, | ||
"mentorId" INTEGER, | ||
"comment" VARCHAR(255), | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "submissions" ( | ||
"id" SERIAL NOT NULL, | ||
"mrUrl" VARCHAR(255), | ||
"diff" TEXT, | ||
"comment" TEXT, | ||
"status" VARCHAR(255), | ||
"viewCount" INTEGER DEFAULT 0, | ||
"createdAt" TIMESTAMPTZ(6) NOT NULL, | ||
"updatedAt" TIMESTAMPTZ(6) NOT NULL, | ||
"userId" INTEGER, | ||
"reviewerId" INTEGER, | ||
"challengeId" INTEGER, | ||
"lessonId" INTEGER, | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "userLessons" ( | ||
"isPassed" VARCHAR(255), | ||
"isTeaching" VARCHAR(255), | ||
"isEnrolled" VARCHAR(255), | ||
"createdAt" TIMESTAMPTZ(6) NOT NULL, | ||
"updatedAt" TIMESTAMPTZ(6) NOT NULL, | ||
"lessonId" INTEGER NOT NULL, | ||
"userId" INTEGER NOT NULL, | ||
|
||
PRIMARY KEY ("lessonId","userId") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "users" ( | ||
"id" SERIAL NOT NULL, | ||
"name" VARCHAR(255), | ||
"username" VARCHAR(255), | ||
"password" VARCHAR(255), | ||
"email" VARCHAR(255), | ||
"gsId" INTEGER, | ||
"isOnline" BOOLEAN, | ||
"createdAt" TIMESTAMPTZ(6) NOT NULL, | ||
"updatedAt" TIMESTAMPTZ(6) NOT NULL, | ||
"isAdmin" VARCHAR(255) DEFAULT false, | ||
"forgotToken" VARCHAR(255), | ||
"cliToken" VARCHAR(255), | ||
"emailVerificationToken" VARCHAR(255), | ||
"tokenExpiration" TIMESTAMPTZ(6), | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "challenges" ADD FOREIGN KEY ("lessonId") REFERENCES "lessons"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "stars" ADD FOREIGN KEY ("mentorId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "stars" ADD FOREIGN KEY ("studentId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "submissions" ADD FOREIGN KEY ("challengeId") REFERENCES "challenges"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "submissions" ADD FOREIGN KEY ("lessonId") REFERENCES "lessons"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "submissions" ADD FOREIGN KEY ("reviewerId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "submissions" ADD FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "userLessons" ADD FOREIGN KEY ("lessonId") REFERENCES "lessons"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "userLessons" ADD FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
2 changes: 2 additions & 0 deletions
2
prisma/migrations/20210323001806_star_lesson_foreignkey/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AddForeignKey | ||
ALTER TABLE "stars" ADD FOREIGN KEY ("lessonId") REFERENCES "lessons"("id") ON DELETE SET NULL ON UPDATE CASCADE; |
16 changes: 16 additions & 0 deletions
16
prisma/migrations/20210323140004_user_unique_username/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
Warnings: | ||
- The migration will add a unique constraint covering the columns `[username]` on the table `users`. If there are existing duplicate values, the migration will fail. | ||
- Made the column `username` on table `users` required. The migration will fail if there are existing NULL values in that column. | ||
*/ | ||
-- Delete duplicate usernames while keeping the latest entry | ||
DELETE FROM "users" A USING "users" B | ||
WHERE A.username = B.username AND A.id < B.id; | ||
|
||
-- AlterTable | ||
ALTER TABLE "users" ALTER COLUMN "username" SET NOT NULL; | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "users.username_unique" ON "users"("username"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (i.e. Git) | ||
provider = "postgresql" |
Oops, something went wrong.
681225a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: