From 24cdde85d82e3653e7387d622ee9033e77e1bb7f Mon Sep 17 00:00:00 2001 From: Frieder Schlesier Date: Sun, 12 Feb 2023 13:54:48 +0100 Subject: [PATCH] add prisma ORM with initial tables Signed-off-by: Frieder Schlesier --- .env | 7 ++++ .gitignore | 4 +++ .../20230204173831_init/migration.sql | 23 ++++++++++++ prisma/migrations/migration_lock.toml | 3 ++ prisma/schema.prisma | 36 +++++++++++++++++++ src/db.ts | 17 +++++++++ 6 files changed, 90 insertions(+) create mode 100644 .env create mode 100644 prisma/migrations/20230204173831_init/migration.sql create mode 100644 prisma/migrations/migration_lock.toml create mode 100644 prisma/schema.prisma create mode 100644 src/db.ts diff --git a/.env b/.env new file mode 100644 index 0000000..69a1972 --- /dev/null +++ b/.env @@ -0,0 +1,7 @@ +# Environment variables declared in this file are automatically made available to Prisma. +# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema + +# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB. +# See the documentation for all the connection string options: https://pris.ly/d/connection-strings + +DATABASE_URL="file:./dev-test.db" diff --git a/.gitignore b/.gitignore index a547bf3..303f666 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +# database +*.db +*.db-journal + # Logs logs *.log diff --git a/prisma/migrations/20230204173831_init/migration.sql b/prisma/migrations/20230204173831_init/migration.sql new file mode 100644 index 0000000..482d451 --- /dev/null +++ b/prisma/migrations/20230204173831_init/migration.sql @@ -0,0 +1,23 @@ +-- CreateTable +CREATE TABLE "Event" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "titleFormatted" TEXT NOT NULL, + "locationFormatted" TEXT NOT NULL, + "descriptionDescriptionFormatted" TEXT NOT NULL, + "dateTime" TEXT NOT NULL, + "endDateTime" TEXT NOT NULL, + "isOfficial" BOOLEAN NOT NULL, + "videoUrl" TEXT NOT NULL, + "group" INTEGER NOT NULL, + "createdBy" INTEGER NOT NULL +); + +-- CreateTable +CREATE TABLE "EventState" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "publishYoutube" BOOLEAN NOT NULL, + "publishFacebook" BOOLEAN NOT NULL, + "publishInstagram" BOOLEAN NOT NULL, + "eventId" INTEGER NOT NULL, + CONSTRAINT "EventState_eventId_fkey" FOREIGN KEY ("eventId") REFERENCES "Event" ("id") ON DELETE RESTRICT ON UPDATE CASCADE +); 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..e6bf3ee --- /dev/null +++ b/prisma/schema.prisma @@ -0,0 +1,36 @@ +// 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 = env("DATABASE_URL") +} + +model Event { + id Int @id + titleFormatted String + locationFormatted String + descriptionDescriptionFormatted String + dateTime String + endDateTime String + isOfficial Boolean + videoUrl String + group Int + createdBy Int + EventState EventState[] +} + +// pub time, url/id +// TODO: add publish dates, store links +model EventState { + id Int @id @default(autoincrement()) + event Event @relation(fields: [eventId], references: [id]) + publishYoutube Boolean + publishFacebook Boolean + publishInstagram Boolean + eventId Int +} diff --git a/src/db.ts b/src/db.ts new file mode 100644 index 0000000..a2865ab --- /dev/null +++ b/src/db.ts @@ -0,0 +1,17 @@ +import { PrismaClient } from '@prisma/client' + +const prisma = new PrismaClient() + +async function main() { + // ... you will write your Prisma Client queries here +} + +main() + .then(async () => { + await prisma.$disconnect() + }) + .catch(async (e) => { + console.error(e) + await prisma.$disconnect() + process.exit(1) + }) \ No newline at end of file