prisma initial setup
This commit is contained in:
parent
2375a1fd58
commit
ed67cb9aa1
Binary file not shown.
|
@ -0,0 +1,28 @@
|
|||
-- CreateTable
|
||||
CREATE TABLE "Story" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"word_count" INTEGER NOT NULL,
|
||||
"title" TEXT NOT NULL,
|
||||
"deleted" INTEGER NOT NULL
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Pub" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"title" TEXT NOT NULL,
|
||||
"link" TEXT NOT NULL,
|
||||
"query_after_days" INTEGER NOT NULL,
|
||||
"deleted" INTEGER NOT NULL
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Genre" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"name" TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Response" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"response" TEXT NOT NULL
|
||||
);
|
|
@ -0,0 +1,30 @@
|
|||
-- CreateTable
|
||||
CREATE TABLE "Sub" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"storyId" INTEGER NOT NULL,
|
||||
"pubId" INTEGER NOT NULL,
|
||||
"submitted" TEXT NOT NULL,
|
||||
"responded" TEXT NOT NULL,
|
||||
"responseId" INTEGER NOT NULL,
|
||||
CONSTRAINT "Sub_storyId_fkey" FOREIGN KEY ("storyId") REFERENCES "Story" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
|
||||
CONSTRAINT "Sub_pubId_fkey" FOREIGN KEY ("pubId") REFERENCES "Pub" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
|
||||
CONSTRAINT "Sub_responseId_fkey" FOREIGN KEY ("responseId") REFERENCES "Response" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "PubsGenres" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"pubId" INTEGER NOT NULL,
|
||||
"genreId" INTEGER NOT NULL,
|
||||
CONSTRAINT "PubsGenres_pubId_fkey" FOREIGN KEY ("pubId") REFERENCES "Pub" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
|
||||
CONSTRAINT "PubsGenres_genreId_fkey" FOREIGN KEY ("genreId") REFERENCES "Genre" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "StoriesGenres" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"storyId" INTEGER NOT NULL,
|
||||
"genreId" INTEGER NOT NULL,
|
||||
CONSTRAINT "StoriesGenres_storyId_fkey" FOREIGN KEY ("storyId") REFERENCES "Story" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
|
||||
CONSTRAINT "StoriesGenres_genreId_fkey" FOREIGN KEY ("genreId") REFERENCES "Genre" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
|
||||
);
|
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to drop the `PubsGenres` table. If the table is not empty, all the data it contains will be lost.
|
||||
- You are about to drop the `StoriesGenres` table. If the table is not empty, all the data it contains will be lost.
|
||||
|
||||
*/
|
||||
-- DropTable
|
||||
PRAGMA foreign_keys=off;
|
||||
DROP TABLE "PubsGenres";
|
||||
PRAGMA foreign_keys=on;
|
||||
|
||||
-- DropTable
|
||||
PRAGMA foreign_keys=off;
|
||||
DROP TABLE "StoriesGenres";
|
||||
PRAGMA foreign_keys=on;
|
|
@ -0,0 +1,16 @@
|
|||
-- RedefineTables
|
||||
PRAGMA defer_foreign_keys=ON;
|
||||
PRAGMA foreign_keys=OFF;
|
||||
CREATE TABLE "new_Genre" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"name" TEXT NOT NULL,
|
||||
"storyId" INTEGER,
|
||||
"pubId" INTEGER,
|
||||
CONSTRAINT "Genre_storyId_fkey" FOREIGN KEY ("storyId") REFERENCES "Story" ("id") ON DELETE SET NULL ON UPDATE CASCADE,
|
||||
CONSTRAINT "Genre_pubId_fkey" FOREIGN KEY ("pubId") REFERENCES "Pub" ("id") ON DELETE SET NULL ON UPDATE CASCADE
|
||||
);
|
||||
INSERT INTO "new_Genre" ("id", "name") SELECT "id", "name" FROM "Genre";
|
||||
DROP TABLE "Genre";
|
||||
ALTER TABLE "new_Genre" RENAME TO "Genre";
|
||||
PRAGMA foreign_keys=ON;
|
||||
PRAGMA defer_foreign_keys=OFF;
|
|
@ -0,0 +1,8 @@
|
|||
/*
|
||||
Warnings:
|
||||
|
||||
- A unique constraint covering the columns `[name]` on the table `Genre` will be added. If there are existing duplicate values, this will fail.
|
||||
|
||||
*/
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Genre_name_key" ON "Genre"("name");
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to drop the column `pubId` on the `Genre` table. All the data in the column will be lost.
|
||||
- You are about to drop the column `storyId` on the `Genre` table. All the data in the column will be lost.
|
||||
|
||||
*/
|
||||
-- CreateTable
|
||||
CREATE TABLE "_GenreToStory" (
|
||||
"A" INTEGER NOT NULL,
|
||||
"B" INTEGER NOT NULL,
|
||||
CONSTRAINT "_GenreToStory_A_fkey" FOREIGN KEY ("A") REFERENCES "Genre" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
CONSTRAINT "_GenreToStory_B_fkey" FOREIGN KEY ("B") REFERENCES "Story" ("id") ON DELETE CASCADE ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "_GenreToPub" (
|
||||
"A" INTEGER NOT NULL,
|
||||
"B" INTEGER NOT NULL,
|
||||
CONSTRAINT "_GenreToPub_A_fkey" FOREIGN KEY ("A") REFERENCES "Genre" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
CONSTRAINT "_GenreToPub_B_fkey" FOREIGN KEY ("B") REFERENCES "Pub" ("id") ON DELETE CASCADE ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- RedefineTables
|
||||
PRAGMA defer_foreign_keys=ON;
|
||||
PRAGMA foreign_keys=OFF;
|
||||
CREATE TABLE "new_Genre" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"name" TEXT NOT NULL
|
||||
);
|
||||
INSERT INTO "new_Genre" ("id", "name") SELECT "id", "name" FROM "Genre";
|
||||
DROP TABLE "Genre";
|
||||
ALTER TABLE "new_Genre" RENAME TO "Genre";
|
||||
CREATE UNIQUE INDEX "Genre_name_key" ON "Genre"("name");
|
||||
PRAGMA foreign_keys=ON;
|
||||
PRAGMA defer_foreign_keys=OFF;
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "_GenreToStory_AB_unique" ON "_GenreToStory"("A", "B");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "_GenreToStory_B_index" ON "_GenreToStory"("B");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "_GenreToPub_AB_unique" ON "_GenreToPub"("A", "B");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "_GenreToPub_B_index" ON "_GenreToPub"("B");
|
|
@ -0,0 +1,24 @@
|
|||
-- RedefineTables
|
||||
PRAGMA defer_foreign_keys=ON;
|
||||
PRAGMA foreign_keys=OFF;
|
||||
CREATE TABLE "new_Pub" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"title" TEXT NOT NULL,
|
||||
"link" TEXT NOT NULL,
|
||||
"query_after_days" INTEGER NOT NULL,
|
||||
"deleted" INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
INSERT INTO "new_Pub" ("deleted", "id", "link", "query_after_days", "title") SELECT "deleted", "id", "link", "query_after_days", "title" FROM "Pub";
|
||||
DROP TABLE "Pub";
|
||||
ALTER TABLE "new_Pub" RENAME TO "Pub";
|
||||
CREATE TABLE "new_Story" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"word_count" INTEGER NOT NULL,
|
||||
"title" TEXT NOT NULL,
|
||||
"deleted" INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
INSERT INTO "new_Story" ("deleted", "id", "title", "word_count") SELECT "deleted", "id", "title", "word_count" FROM "Story";
|
||||
DROP TABLE "Story";
|
||||
ALTER TABLE "new_Story" RENAME TO "Story";
|
||||
PRAGMA foreign_keys=ON;
|
||||
PRAGMA defer_foreign_keys=OFF;
|
|
@ -0,0 +1,15 @@
|
|||
-- RedefineTables
|
||||
PRAGMA defer_foreign_keys=ON;
|
||||
PRAGMA foreign_keys=OFF;
|
||||
CREATE TABLE "new_Pub" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"title" TEXT NOT NULL,
|
||||
"link" TEXT NOT NULL DEFAULT '',
|
||||
"query_after_days" INTEGER NOT NULL,
|
||||
"deleted" INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
INSERT INTO "new_Pub" ("deleted", "id", "link", "query_after_days", "title") SELECT "deleted", "id", "link", "query_after_days", "title" FROM "Pub";
|
||||
DROP TABLE "Pub";
|
||||
ALTER TABLE "new_Pub" RENAME TO "Pub";
|
||||
PRAGMA foreign_keys=ON;
|
||||
PRAGMA defer_foreign_keys=OFF;
|
|
@ -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"
|
|
@ -0,0 +1,55 @@
|
|||
// 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 = "sqlite"
|
||||
url = "file:./dev.db"
|
||||
}
|
||||
|
||||
model Story {
|
||||
id Int @id @default(autoincrement())
|
||||
word_count Int
|
||||
title String
|
||||
deleted Int @default(0)
|
||||
subs Sub[]
|
||||
genres Genre[]
|
||||
}
|
||||
|
||||
model Pub {
|
||||
id Int @id @default(autoincrement())
|
||||
title String
|
||||
link String @default("")
|
||||
query_after_days Int
|
||||
deleted Int @default(0)
|
||||
subs Sub[]
|
||||
genres Genre[]
|
||||
}
|
||||
|
||||
model Genre {
|
||||
id Int @id @default(autoincrement())
|
||||
name String @unique
|
||||
stories Story[]
|
||||
pubs Pub[]
|
||||
}
|
||||
|
||||
model Response {
|
||||
id Int @id @default(autoincrement())
|
||||
response String
|
||||
subs Sub[]
|
||||
}
|
||||
|
||||
model Sub {
|
||||
id Int @id @default(autoincrement())
|
||||
story Story @relation(fields: [storyId], references: [id])
|
||||
storyId Int
|
||||
pub Pub @relation(fields: [pubId], references: [id])
|
||||
pubId Int
|
||||
submitted String
|
||||
responded String
|
||||
response Response @relation(fields: [responseId], references: [id])
|
||||
responseId Int
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
async function main() {
|
||||
// ... you will write your Prisma Client queries here
|
||||
const story = await prisma.story.update({
|
||||
where: { id: 1 },
|
||||
data: {
|
||||
title: "Ghost Aliens of Mars",
|
||||
genres: { set: [{ id: 1 }, { id: 2 }], create: { name: "alien-punk" } }
|
||||
}
|
||||
|
||||
})
|
||||
console.log(story)
|
||||
|
||||
}
|
||||
|
||||
main()
|
||||
.then(async () => {
|
||||
await prisma.$disconnect()
|
||||
})
|
||||
.catch(async (e) => {
|
||||
console.error(e)
|
||||
await prisma.$disconnect()
|
||||
process.exit(1)
|
||||
})
|
Loading…
Reference in New Issue