Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: schema #229

Merged
merged 6 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions apps/studio/prisma/generated/generatedTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ColumnType } from "kysely"
import type { ColumnType, GeneratedAlways } from "kysely"
export type Generated<T> =
T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
Expand All @@ -8,43 +8,43 @@ export type Timestamp = ColumnType<Date, Date | string, Date | string>
import type { RoleType } from "./generatedEnums"

export type Blob = {
id: string
id: GeneratedAlways<number>
content: unknown
}
export type Footer = {
id: string
siteId: string
id: GeneratedAlways<number>
siteId: number
content: unknown
}
export type Navbar = {
id: string
siteId: string
id: GeneratedAlways<number>
siteId: number
content: unknown
}
export type Permission = {
id: string
resourceId: string
userId: string
id: GeneratedAlways<number>
resourceId: number
userId: number
role: RoleType
}
export type Resource = {
id: string
id: GeneratedAlways<number>
name: string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this change affect the current tRPC schema, specifically the types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, they've been updated here

siteId: string
parentId: string | null
blobId: string | null
siteId: number
parentId: number | null
blobId: number | null
}
export type Site = {
id: string
id: GeneratedAlways<number>
name: string
config: unknown
}
export type SiteMember = {
userId: string
siteId: string
userId: number
siteId: number
}
export type User = {
id: string
id: GeneratedAlways<number>
name: string
email: string
phone: string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
Warnings:

- The primary key for the `Blob` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The `id` column on the `Blob` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The primary key for the `Footer` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The `id` column on the `Footer` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The primary key for the `Navbar` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The `id` column on the `Navbar` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The primary key for the `Permission` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The `id` column on the `Permission` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The primary key for the `Resource` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The `id` column on the `Resource` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `parentId` column on the `Resource` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The `blobId` column on the `Resource` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The primary key for the `Site` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The `id` column on the `Site` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The primary key for the `SiteMember` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The primary key for the `User` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The `id` column on the `User` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- Changed the type of `siteId` on the `Footer` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `siteId` on the `Navbar` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `resourceId` on the `Permission` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `userId` on the `Permission` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `siteId` on the `Resource` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `userId` on the `SiteMember` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.
- Changed the type of `siteId` on the `SiteMember` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.

*/
-- DropForeignKey
ALTER TABLE "Footer" DROP CONSTRAINT "Footer_siteId_fkey";

-- DropForeignKey
ALTER TABLE "Navbar" DROP CONSTRAINT "Navbar_siteId_fkey";

-- DropForeignKey
ALTER TABLE "Permission" DROP CONSTRAINT "Permission_resourceId_fkey";

-- DropForeignKey
ALTER TABLE "Permission" DROP CONSTRAINT "Permission_userId_fkey";

-- DropForeignKey
ALTER TABLE "Resource" DROP CONSTRAINT "Resource_blobId_fkey";

-- DropForeignKey
ALTER TABLE "Resource" DROP CONSTRAINT "Resource_parentId_fkey";

-- DropForeignKey
ALTER TABLE "Resource" DROP CONSTRAINT "Resource_siteId_fkey";

-- DropForeignKey
ALTER TABLE "SiteMember" DROP CONSTRAINT "SiteMember_siteId_fkey";

-- DropForeignKey
ALTER TABLE "SiteMember" DROP CONSTRAINT "SiteMember_userId_fkey";

-- AlterTable
ALTER TABLE "Blob" DROP CONSTRAINT "Blob_pkey",
DROP COLUMN "id",
ADD COLUMN "id" SERIAL NOT NULL,
ADD CONSTRAINT "Blob_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "Footer" DROP CONSTRAINT "Footer_pkey",
DROP COLUMN "id",
ADD COLUMN "id" SERIAL NOT NULL,
DROP COLUMN "siteId",
ADD COLUMN "siteId" INTEGER NOT NULL,
ADD CONSTRAINT "Footer_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "Navbar" DROP CONSTRAINT "Navbar_pkey",
DROP COLUMN "id",
ADD COLUMN "id" SERIAL NOT NULL,
DROP COLUMN "siteId",
ADD COLUMN "siteId" INTEGER NOT NULL,
ADD CONSTRAINT "Navbar_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "Permission" DROP CONSTRAINT "Permission_pkey",
DROP COLUMN "id",
ADD COLUMN "id" SERIAL NOT NULL,
DROP COLUMN "resourceId",
ADD COLUMN "resourceId" INTEGER NOT NULL,
DROP COLUMN "userId",
ADD COLUMN "userId" INTEGER NOT NULL,
ADD CONSTRAINT "Permission_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "Resource" DROP CONSTRAINT "Resource_pkey",
DROP COLUMN "id",
ADD COLUMN "id" SERIAL NOT NULL,
DROP COLUMN "siteId",
ADD COLUMN "siteId" INTEGER NOT NULL,
DROP COLUMN "parentId",
ADD COLUMN "parentId" INTEGER,
DROP COLUMN "blobId",
ADD COLUMN "blobId" INTEGER,
ADD CONSTRAINT "Resource_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "Site" DROP CONSTRAINT "Site_pkey",
DROP COLUMN "id",
ADD COLUMN "id" SERIAL NOT NULL,
ADD CONSTRAINT "Site_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "SiteMember" DROP CONSTRAINT "SiteMember_pkey",
DROP COLUMN "userId",
ADD COLUMN "userId" INTEGER NOT NULL,
DROP COLUMN "siteId",
ADD COLUMN "siteId" INTEGER NOT NULL,
ADD CONSTRAINT "SiteMember_pkey" PRIMARY KEY ("siteId", "userId");

-- AlterTable
ALTER TABLE "User" DROP CONSTRAINT "User_pkey",
DROP COLUMN "id",
ADD COLUMN "id" SERIAL NOT NULL,
ADD CONSTRAINT "User_pkey" PRIMARY KEY ("id");

-- CreateIndex
CREATE UNIQUE INDEX "Footer_siteId_key" ON "Footer"("siteId");

-- CreateIndex
CREATE UNIQUE INDEX "Navbar_siteId_key" ON "Navbar"("siteId");

-- CreateIndex
CREATE UNIQUE INDEX "Resource_blobId_key" ON "Resource"("blobId");

-- AddForeignKey
ALTER TABLE "Resource" ADD CONSTRAINT "Resource_siteId_fkey" FOREIGN KEY ("siteId") REFERENCES "Site"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Resource" ADD CONSTRAINT "Resource_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "Resource"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Resource" ADD CONSTRAINT "Resource_blobId_fkey" FOREIGN KEY ("blobId") REFERENCES "Blob"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Permission" ADD CONSTRAINT "Permission_resourceId_fkey" FOREIGN KEY ("resourceId") REFERENCES "Resource"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Permission" ADD CONSTRAINT "Permission_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Navbar" ADD CONSTRAINT "Navbar_siteId_fkey" FOREIGN KEY ("siteId") REFERENCES "Site"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Footer" ADD CONSTRAINT "Footer_siteId_fkey" FOREIGN KEY ("siteId") REFERENCES "Site"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SiteMember" ADD CONSTRAINT "SiteMember_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "SiteMember" ADD CONSTRAINT "SiteMember_siteId_fkey" FOREIGN KEY ("siteId") REFERENCES "Site"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
40 changes: 20 additions & 20 deletions apps/studio/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ model VerificationToken {
}

model Resource {
id String @id @default(cuid())
id Int @id @default(autoincrement())
name String
siteId String
siteId Int
site Site @relation(fields: [siteId], references: [id])
parentId String?
parentId Int?
parent Resource? @relation("parentRelation", fields: [parentId], references: [id])
blobId String? @unique
blobId Int? @unique
blob Blob? @relation(fields: [blobId], references: [id])

children Resource[] @relation("parentRelation")
Expand All @@ -42,7 +42,7 @@ model Resource {
}

model User {
id String @id @default(cuid())
id Int @id @default(autoincrement())
name String
email String @unique
phone String
Expand All @@ -53,18 +53,18 @@ model User {
}

model Permission {
id String @id @default(cuid())
resourceId String
id Int @id @default(autoincrement())
resourceId Int
resource Resource @relation(fields: [resourceId], references: [id])
userId String
userId Int
user User @relation(fields: [userId], references: [id])
role RoleType

// Delete the row if either resource or user is deleted, default behavior cascade
}

model Site {
id String @id @default(cuid())
id Int @id @default(autoincrement())
name String @unique
resources Resource[]
siteMembers SiteMember[]
Expand All @@ -78,32 +78,32 @@ model Site {
}

model Navbar {
id String @id @default(cuid())
siteId String @unique
site Site @relation(fields: [siteId], references: [id])
id Int @id @default(autoincrement())
siteId Int @unique
site Site @relation(fields: [siteId], references: [id])
content Json
}

model Footer {
id String @id @default(cuid())
siteId String @unique
site Site @relation(fields: [siteId], references: [id])
id Int @id @default(autoincrement())
siteId Int @unique
site Site @relation(fields: [siteId], references: [id])
content Json
}

model Blob {
id String @id @default(cuid())
id Int @id @default(autoincrement())
content Json

// Should the blob be deleted on deletion of the resource?
resource Resource?
}

model SiteMember {
userId String
user User @relation(fields: [userId], references: [id])
siteId String
site Site @relation(fields: [siteId], references: [id])
userId Int
user User @relation(fields: [userId], references: [id])
siteId Int
site Site @relation(fields: [siteId], references: [id])

@@id([siteId, userId])
}
Expand Down
20 changes: 7 additions & 13 deletions apps/studio/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '~/server/modules/resource/resource.types'
import { db } from '../src/server/modules/database'

const NAV_BAR_ITEMS = [
const NAV_BAR_ITEMS: Navbar['items'] = [
{
name: 'Expandable nav item',
url: '/item-one',
Expand Down Expand Up @@ -51,10 +51,9 @@ const NAV_BAR_ITEMS = [
]

async function main() {
await db
const { id } = await db
.insertInto('Site')
.values({
id: '1',
name: 'Ministry of Trade and Industry',
config: {
theme: 'isomer-next',
Expand All @@ -66,13 +65,13 @@ async function main() {
isGovernment: true,
} satisfies SiteConfig,
})
.execute()
.returning('id')
.executeTakeFirstOrThrow()

await db
.insertInto('Footer')
.values({
id: '1',
siteId: '1',
siteId: id,
content: {
name: 'A foot',
contactUsLink: '/contact-us',
Expand All @@ -86,13 +85,8 @@ async function main() {
await db
.insertInto('Navbar')
.values({
id: '1',
siteId: '1',
content: {
name: 'navi',
url: 'www.isomer.gov.sg',
items: NAV_BAR_ITEMS,
} satisfies Navbar,
siteId: id,
content: { items: NAV_BAR_ITEMS } satisfies Navbar,
seaerchin marked this conversation as resolved.
Show resolved Hide resolved
})
.execute()
}
Expand Down
8 changes: 6 additions & 2 deletions apps/studio/src/server/modules/resource/resource.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export interface NavbarItem {
name: string
import { type IsomerSiteProps } from '@opengovsg/isomer-components'

export type Navbar = { items: IsomerSiteProps['navBarItems'] }

export interface FooterItem {
title: string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I suspect most of the types that we need on the CMS should be able to be derived from the exported props one way or another, so we might not need to re-define them here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported for ease of use cos it's very tedious to type Isomer...['...'] every time ._.

url: string
Comment on lines +1 to +6
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is cherry-picked, downstream will refer correctly to IsomerSiteProps

description?: string
}
Expand Down
Loading