From 0e94900e2cc4feff6d3cd7aee84a80c810d7d82d Mon Sep 17 00:00:00 2001 From: Matti Nannt Date: Wed, 31 May 2023 15:57:10 +0200 Subject: [PATCH] enhance prisma json types (#327) --- .../api/v1/client/surveys/[surveyId]/index.ts | 12 ++--- packages/database/package.json | 4 +- packages/database/prisma/schema.prisma | 40 ++++++++++----- packages/database/src/client.ts | 1 + packages/database/types/jsonTypes.ts | 16 ++++++ pnpm-lock.yaml | 49 ++++++++++++++++++- 6 files changed, 101 insertions(+), 21 deletions(-) create mode 100644 packages/database/types/jsonTypes.ts diff --git a/apps/web/pages/api/v1/client/surveys/[surveyId]/index.ts b/apps/web/pages/api/v1/client/surveys/[surveyId]/index.ts index ee588636d6..ddd882d731 100644 --- a/apps/web/pages/api/v1/client/surveys/[surveyId]/index.ts +++ b/apps/web/pages/api/v1/client/surveys/[surveyId]/index.ts @@ -60,13 +60,11 @@ export default async function handle(req: NextApiRequest, res: NextApiResponse) } // if survey exists, return survey - return res - .status(200) - .json({ - ...survey, - brandColor: product?.brandColor, - formbricksSignature: product?.formbricksSignature, - }); + return res.status(200).json({ + ...survey, + brandColor: product?.brandColor, + formbricksSignature: product?.formbricksSignature, + }); } // Unknown HTTP Method diff --git a/packages/database/package.json b/packages/database/package.json index ec76923c4f..758bea4636 100644 --- a/packages/database/package.json +++ b/packages/database/package.json @@ -25,7 +25,9 @@ "studio": "prisma studio" }, "dependencies": { - "@prisma/client": "^4.14.1" + "@formbricks/types": "workspace:*", + "@prisma/client": "^4.14.1", + "prisma-json-types-generator": "^2.4.0" }, "devDependencies": { "@formbricks/tsconfig": "workspace:*", diff --git a/packages/database/prisma/schema.prisma b/packages/database/prisma/schema.prisma index c9817908d0..b925a6ba27 100644 --- a/packages/database/prisma/schema.prisma +++ b/packages/database/prisma/schema.prisma @@ -12,6 +12,16 @@ generator client { //provider = "prisma-dbml-generator" } +generator json { + provider = "prisma-json-types-generator" + // namespace = "PrismaJson" + // clientOutput = "" + // (./ -> relative to schema, or an importable path to require() it) + // useType = "MyType" + // In case you need to use a type, export it inside the namespace and we will add a index signature to it + // (e.g. export namespace PrismaJson { export type MyType = {a: 1, b: 2} }; will generate namespace.MyType["TYPE HERE"]) +} + enum PipelineTriggers { responseCreated responseUpdated @@ -75,17 +85,18 @@ model Person { } model Response { - id String @id @default(cuid()) - createdAt DateTime @default(now()) @map(name: "created_at") - updatedAt DateTime @updatedAt @map(name: "updated_at") - finished Boolean @default(false) - survey Survey @relation(fields: [surveyId], references: [id], onDelete: Cascade) - surveyId String - person Person? @relation(fields: [personId], references: [id], onDelete: Cascade) - personId String? - data Json @default("{}") - meta Json @default("{}") - userAttributes Json @default("[]") + id String @id @default(cuid()) + createdAt DateTime @default(now()) @map(name: "created_at") + updatedAt DateTime @updatedAt @map(name: "updated_at") + finished Boolean @default(false) + survey Survey @relation(fields: [surveyId], references: [id], onDelete: Cascade) + surveyId String + person Person? @relation(fields: [personId], references: [id], onDelete: Cascade) + personId String? + /// [ResponseData] + data Json @default("{}") + /// [ResponseMeta] + meta Json @default("{}") } enum SurveyStatus { @@ -165,7 +176,9 @@ model Survey { environment Environment @relation(fields: [environmentId], references: [id], onDelete: Cascade) environmentId String status SurveyStatus @default(draft) + /// [SurveyQuestions] questions Json @default("[]") + /// [SurveyThankYouCard] thankYouCard Json @default("{\"enabled\": false}") responses Response[] displayOption displayOptions @default(displayOnce) @@ -173,7 +186,7 @@ model Survey { triggers SurveyTrigger[] attributeFilters SurveyAttributeFilter[] displays Display[] - autoClose Int? + autoClose Int? } model Event { @@ -183,6 +196,7 @@ model Event { eventClassId String? session Session @relation(fields: [sessionId], references: [id], onDelete: Cascade) sessionId String + /// [EventProperties] properties Json @default("{}") } @@ -200,6 +214,7 @@ model EventClass { description String? type EventType events Event[] + /// [EventClassNoCodeConfig] noCodeConfig Json? environment Environment @relation(fields: [environmentId], references: [id], onDelete: Cascade) environmentId String @@ -384,5 +399,6 @@ model User { invitesAccepted Invite[] @relation("inviteAcceptedBy") role Role? objective Objective? + /// [UserNotificationSettings] notificationSettings Json @default("{}") } diff --git a/packages/database/src/client.ts b/packages/database/src/client.ts index b5b129bd43..8ab5c03d79 100644 --- a/packages/database/src/client.ts +++ b/packages/database/src/client.ts @@ -1,4 +1,5 @@ import { PrismaClient } from "@prisma/client"; +import "../types/jsonTypes"; declare global { var prisma: PrismaClient | undefined; diff --git a/packages/database/types/jsonTypes.ts b/packages/database/types/jsonTypes.ts new file mode 100644 index 0000000000..7992e85e35 --- /dev/null +++ b/packages/database/types/jsonTypes.ts @@ -0,0 +1,16 @@ +import { NoCodeConfig } from "@formbricks/types/events"; +import { Question } from "@formbricks/types/questions"; +import { ThankYouCard } from "@formbricks/types/surveys"; +import { NotificationSettings } from "@formbricks/types/users"; + +declare global { + namespace PrismaJson { + export type EventProperties = { [key: string]: string }; + export type EventClassNoCodeConfig = NoCodeConfig; + export type ResponseData = { [questionId: string]: string }; + export type ResponseMeta = { [key: string]: string }; + export type SurveyQuestions = Question[]; + export type SurveyThankYouCard = ThankYouCard; + export type UserNotificationSettings = NotificationSettings; + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f5a003fea4..267ea3840b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -352,9 +352,15 @@ importers: packages/database: dependencies: + '@formbricks/types': + specifier: workspace:* + version: link:../types '@prisma/client': specifier: ^4.14.1 version: 4.14.1(prisma@4.14.1) + prisma-json-types-generator: + specifier: ^2.4.0 + version: 2.4.0 devDependencies: '@formbricks/tsconfig': specifier: workspace:* @@ -3937,6 +3943,16 @@ packages: prisma: 4.14.1 dev: false + /@prisma/debug@4.15.0: + resolution: {integrity: sha512-dkbPz+gOVlWDBAaOEseSpAUz9NppT38UlwdryPyrwct6OClLirNC7wH+TpAQk5OZp9x59hNnfDz+T7XvL1v0/Q==} + dependencies: + '@types/debug': 4.1.8 + debug: 4.3.4 + strip-ansi: 6.0.1 + transitivePeerDependencies: + - supports-color + dev: false + /@prisma/debug@4.6.1: resolution: {integrity: sha512-BezDvSenTgQDQ6WA3TdTDGcrt0Oh4vmpZtmSOYm1KaSZiSVIL2xT0P9TFM3vtOa4wn7sn/003PyTSxyHS3mShg==} dependencies: @@ -4006,6 +4022,17 @@ packages: - supports-color dev: true + /@prisma/generator-helper@4.15.0: + resolution: {integrity: sha512-JVHNgXr0LrcqXqmFrs+BzxfyRL6cFD5GLTMVWfCLU7kqSJdWuZxfoZW995tg6mOXnBgPTf6Ocv3RY4RLQq8k4g==} + dependencies: + '@prisma/debug': 4.15.0 + '@types/cross-spawn': 6.0.2 + cross-spawn: 7.0.3 + kleur: 4.1.5 + transitivePeerDependencies: + - supports-color + dev: false + /@prisma/generator-helper@4.6.1: resolution: {integrity: sha512-70XBmqDhmpe8H35ttOJOgyg1OpppO/uelILB1SIwjeSI7PHHdU2+Y/+LkpnifkCEpSZKIhxEIPbHx17m2neAsA==} dependencies: @@ -4919,13 +4946,18 @@ packages: resolution: {integrity: sha512-KuwNhp3eza+Rhu8IFI5HUXRP0LIhqH5cAjubUvGXXthh4YYBuP2ntwEX+Cz8GJoZUHlKo247wPWOfA9LYEq4cw==} dependencies: '@types/node': 20.2.3 - dev: true /@types/debug@4.1.7: resolution: {integrity: sha512-9AonUzyTjXXhEOa0DnqpzZi6VHlqKMswga9EXjpXnnqxwLtdvPPtlO8evrI5D9S6asFRCQ6v+wpiUKbw+vKqyg==} dependencies: '@types/ms': 0.7.31 + /@types/debug@4.1.8: + resolution: {integrity: sha512-/vPO1EPOs306Cvhwv7KfVfYvOJqA/S/AXjaHQiJboCZzcNDb+TIJFN9/2C9DZ//ijSKWioNyUxD792QmDJ+HKQ==} + dependencies: + '@types/ms': 0.7.31 + dev: false + /@types/enzyme@3.10.13: resolution: {integrity: sha512-FCtoUhmFsud0Yx9fmZk179GkdZ4U9B0GFte64/Md+W/agx0L5SxsIIbhLBOxIb9y2UfBA4WQnaG1Od/UsUQs9Q==} dependencies: @@ -16461,6 +16493,17 @@ packages: - supports-color dev: true + /prisma-json-types-generator@2.4.0: + resolution: {integrity: sha512-iOdo8sBoUz2jCi+kw5nwACbe0QHcPIIEToXyvn0cW5IJhPEhoYbY/XwMhEDIEla247YhXe1yYPiKwviFm9LNpg==} + engines: {node: '>=14.0'} + hasBin: true + dependencies: + '@prisma/generator-helper': 4.15.0 + tslib: 2.5.2 + transitivePeerDependencies: + - supports-color + dev: false + /prisma@4.14.1: resolution: {integrity: sha512-z6hxzTMYqT9SIKlzD08dhzsLUpxjFKKsLpp5/kBDnSqiOjtUyyl/dC5tzxLcOa3jkEHQ8+RpB/fE3w8bgNP51g==} engines: {node: '>=14.17'} @@ -19219,6 +19262,10 @@ packages: /tslib@2.4.1: resolution: {integrity: sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==} + /tslib@2.5.2: + resolution: {integrity: sha512-5svOrSA2w3iGFDs1HibEVBGbDrAY82bFQ3HZ3ixB+88nsbsWQoKqDRb5UBYAUPEzbBn6dAp5gRNXglySbx1MlA==} + dev: false + /tsup@6.7.0(postcss@8.4.23)(typescript@5.0.4): resolution: {integrity: sha512-L3o8hGkaHnu5TdJns+mCqFsDBo83bJ44rlK7e6VdanIvpea4ArPcU3swWGsLVbXak1PqQx/V+SSmFPujBK+zEQ==} engines: {node: '>=14.18'}