diff --git a/apps/web/app/api/deps/route.ts b/apps/web/app/api/deps/route.ts index 4c80dc0..afa5310 100644 --- a/apps/web/app/api/deps/route.ts +++ b/apps/web/app/api/deps/route.ts @@ -1,5 +1,7 @@ -import { getAppDepsHandler } from '@verity/deps-api'; +import { NextRequest } from 'next/server'; -export async function GET(request: Request) { - return getAppDepsHandler(request); +import { getApplicationDependencies } from '@verity/deps-api'; + +export async function GET(request: NextRequest) { + return getApplicationDependencies(request); } diff --git a/libs/auth/src/lib/auth.ts b/libs/auth/src/lib/auth.ts index fd8ab72..923291c 100644 --- a/libs/auth/src/lib/auth.ts +++ b/libs/auth/src/lib/auth.ts @@ -2,9 +2,8 @@ import NextAuth from 'next-auth'; import Keycloak from 'next-auth/providers/keycloak'; import { PrismaAdapter } from '@auth/prisma-adapter'; -import { PrismaClient } from '@prisma/client'; -const prisma = new PrismaClient(); +import { prisma } from '@verity/prisma'; export const { handlers, signIn, signOut, auth } = NextAuth({ adapter: PrismaAdapter(prisma), diff --git a/libs/deps-api/src/lib/deps-api.ts b/libs/deps-api/src/lib/deps-api.ts index 9022f44..19e653e 100644 --- a/libs/deps-api/src/lib/deps-api.ts +++ b/libs/deps-api/src/lib/deps-api.ts @@ -1,17 +1,16 @@ -import { PrismaClient } from '@prisma/client'; +import { NextRequest, NextResponse } from 'next/server'; -import { getQueryParams } from '@verity/utils'; +import { prisma } from '@verity/prisma'; -const prisma = new PrismaClient(); - -export const getAppDepsHandler = async (request: Request) => { - const { app, version } = getQueryParams(request); +export const getApplicationDependencies = async (request: NextRequest) => { + const app = request.nextUrl.searchParams.get('app'); + const version = request.nextUrl.searchParams.get('version'); if (!app || !version) { return new Response('app and version params are required', { status: 400 }); } - const rawAppVersion = await prisma.appVersion.findFirst({ + const rawAppVersion = await prisma.version.findFirst({ where: { appId: app, value: version, @@ -34,18 +33,13 @@ export const getAppDepsHandler = async (request: Request) => { return new Response('app version not found', { status: 404 }); } - const dependencies = rawAppVersion.dependencies.reduce((acc, dep) => { - const appId = dep.dependencyAppVersion?.appId; - - if (!appId) { - return acc; - } else { - return { - ...acc, - [appId]: dep.dependencyAppVersion?.value, - }; - } - }, {}); + const dependencies = rawAppVersion.dependencies.reduce( + (acc, dep) => ({ + ...acc, + [dep.dependencyAppVersion.appId]: dep.dependencyAppVersion.value, + }), + {}, + ); - return new Response(JSON.stringify(dependencies), { status: 200 }); + return NextResponse.json(dependencies); }; diff --git a/libs/prisma/.eslintrc.json b/libs/prisma/.eslintrc.json new file mode 100644 index 0000000..a39ac5d --- /dev/null +++ b/libs/prisma/.eslintrc.json @@ -0,0 +1,18 @@ +{ + "extends": ["plugin:@nx/react", "../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], + "rules": {} + }, + { + "files": ["*.ts", "*.tsx"], + "rules": {} + }, + { + "files": ["*.js", "*.jsx"], + "rules": {} + } + ] +} diff --git a/libs/prisma/README.md b/libs/prisma/README.md new file mode 100644 index 0000000..60954c4 --- /dev/null +++ b/libs/prisma/README.md @@ -0,0 +1,7 @@ +# prisma + +This library was generated with [Nx](https://nx.dev). + +## Running unit tests + +Run `nx test prisma` to execute the unit tests via [Jest](https://jestjs.io). diff --git a/libs/prisma/project.json b/libs/prisma/project.json new file mode 100644 index 0000000..c4fd47d --- /dev/null +++ b/libs/prisma/project.json @@ -0,0 +1,9 @@ +{ + "name": "prisma", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "libs/prisma/src", + "projectType": "library", + "tags": [], + "// targets": "to see all targets run: nx show project prisma --web", + "targets": {} +} diff --git a/libs/prisma/src/index.ts b/libs/prisma/src/index.ts new file mode 100644 index 0000000..7c095d7 --- /dev/null +++ b/libs/prisma/src/index.ts @@ -0,0 +1 @@ +export { default as prisma } from './lib/prisma-client'; diff --git a/libs/prisma/src/lib/prisma-client.ts b/libs/prisma/src/lib/prisma-client.ts new file mode 100644 index 0000000..6d1f46c --- /dev/null +++ b/libs/prisma/src/lib/prisma-client.ts @@ -0,0 +1,15 @@ +import { PrismaClient } from '@prisma/client'; + +const prismaClientSingleton = () => { + return new PrismaClient(); +}; + +declare const globalThis: { + prismaGlobal: ReturnType; +} & typeof global; + +const prisma = globalThis.prismaGlobal ?? prismaClientSingleton(); + +export default prisma; + +if (process.env.NODE_ENV !== 'production') globalThis.prismaGlobal = prisma; diff --git a/libs/prisma/tsconfig.json b/libs/prisma/tsconfig.json new file mode 100644 index 0000000..95cfeb2 --- /dev/null +++ b/libs/prisma/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "jsx": "react-jsx", + "allowJs": false, + "esModuleInterop": false, + "allowSyntheticDefaultImports": true, + "strict": true + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + } + ], + "extends": "../../tsconfig.base.json" +} diff --git a/libs/prisma/tsconfig.lib.json b/libs/prisma/tsconfig.lib.json new file mode 100644 index 0000000..08e579b --- /dev/null +++ b/libs/prisma/tsconfig.lib.json @@ -0,0 +1,25 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "types": [ + "node", + "@nx/react/typings/cssmodule.d.ts", + "@nx/react/typings/image.d.ts", + "next", + "@nx/next/typings/image.d.ts" + ] + }, + "exclude": [ + "jest.config.ts", + "src/**/*.spec.ts", + "src/**/*.test.ts", + "src/**/*.spec.tsx", + "src/**/*.test.tsx", + "src/**/*.spec.js", + "src/**/*.test.js", + "src/**/*.spec.jsx", + "src/**/*.test.jsx" + ], + "include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"] +} diff --git a/libs/utils/src/lib/utils.ts b/libs/utils/src/lib/utils.ts index adf113c..2819a83 100644 --- a/libs/utils/src/lib/utils.ts +++ b/libs/utils/src/lib/utils.ts @@ -4,8 +4,3 @@ import { twMerge } from 'tailwind-merge'; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); } - -export function getQueryParams(request: Request): Record { - const url = new URL(request.url); - return Object.fromEntries(url.searchParams.entries()); -} diff --git a/prisma/migrations/20240603122534_deps_tables/migration.sql b/prisma/migrations/20240603122534_deps_tables/migration.sql deleted file mode 100644 index d9f6505..0000000 --- a/prisma/migrations/20240603122534_deps_tables/migration.sql +++ /dev/null @@ -1,46 +0,0 @@ --- CreateTable -CREATE TABLE "App" ( - "id" TEXT NOT NULL, - "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - - CONSTRAINT "App_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "AppVersion" ( - "id" SERIAL NOT NULL, - "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "value" TEXT NOT NULL, - "builtAt" TIMESTAMP(3), - "appId" TEXT NOT NULL, - - CONSTRAINT "AppVersion_pkey" PRIMARY KEY ("appId","value") -); - --- CreateTable -CREATE TABLE "AppVersionDependency" ( - "id" SERIAL NOT NULL, - "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - "dependantAppVersionId" INTEGER NOT NULL, - "dependencyAppVersionId" INTEGER NOT NULL, - - CONSTRAINT "AppVersionDependency_pkey" PRIMARY KEY ("id") -); - --- CreateIndex -CREATE UNIQUE INDEX "App_id_key" ON "App"("id"); - --- CreateIndex -CREATE UNIQUE INDEX "AppVersion_id_key" ON "AppVersion"("id"); - --- CreateIndex -CREATE UNIQUE INDEX "AppVersionDependency_id_key" ON "AppVersionDependency"("id"); - --- AddForeignKey -ALTER TABLE "AppVersion" ADD CONSTRAINT "AppVersion_appId_fkey" FOREIGN KEY ("appId") REFERENCES "App"("id") ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "AppVersionDependency" ADD CONSTRAINT "AppVersionDependency_dependantAppVersionId_fkey" FOREIGN KEY ("dependantAppVersionId") REFERENCES "AppVersion"("id") ON DELETE RESTRICT ON UPDATE CASCADE; - --- AddForeignKey -ALTER TABLE "AppVersionDependency" ADD CONSTRAINT "AppVersionDependency_dependencyAppVersionId_fkey" FOREIGN KEY ("dependencyAppVersionId") REFERENCES "AppVersion"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/20240603143636_deps_tables/migration.sql b/prisma/migrations/20240603143636_deps_tables/migration.sql new file mode 100644 index 0000000..1bccd22 --- /dev/null +++ b/prisma/migrations/20240603143636_deps_tables/migration.sql @@ -0,0 +1,46 @@ +-- CreateTable +CREATE TABLE "App" ( + "id" TEXT NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "App_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Version" ( + "id" SERIAL NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "value" TEXT NOT NULL, + "builtAt" TIMESTAMP(3), + "appId" TEXT NOT NULL, + + CONSTRAINT "Version_pkey" PRIMARY KEY ("appId","value") +); + +-- CreateTable +CREATE TABLE "Dependency" ( + "id" SERIAL NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "dependantAppVersionId" INTEGER NOT NULL, + "dependencyAppVersionId" INTEGER NOT NULL, + + CONSTRAINT "Dependency_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "App_id_key" ON "App"("id"); + +-- CreateIndex +CREATE UNIQUE INDEX "Version_id_key" ON "Version"("id"); + +-- CreateIndex +CREATE UNIQUE INDEX "Dependency_id_key" ON "Dependency"("id"); + +-- AddForeignKey +ALTER TABLE "Version" ADD CONSTRAINT "Version_appId_fkey" FOREIGN KEY ("appId") REFERENCES "App"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Dependency" ADD CONSTRAINT "Dependency_dependantAppVersionId_fkey" FOREIGN KEY ("dependantAppVersionId") REFERENCES "Version"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Dependency" ADD CONSTRAINT "Dependency_dependencyAppVersionId_fkey" FOREIGN KEY ("dependencyAppVersionId") REFERENCES "Version"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 8ebefab..124cf68 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -80,29 +80,29 @@ model Authenticator { model App { id String @id @unique createdAt DateTime @default(now()) - versions AppVersion[] + versions Version[] } -model AppVersion { +model Version { id Int @unique @default(autoincrement()) createdAt DateTime @default(now()) value String builtAt DateTime? appId String - dependencies AppVersionDependency[] @relation("AppVersionDependency") - dependsOn AppVersionDependency[] @relation("AppVersionDependsOn") + dependencies Dependency[] @relation("AppVersionDependency") + dependsOn Dependency[] @relation("AppVersionDependsOn") app App @relation(fields: [appId], references: [id]) @@id([appId, value]) } -model AppVersionDependency { +model Dependency { id Int @id @unique @default(autoincrement()) createdAt DateTime @default(now()) dependantAppVersionId Int dependencyAppVersionId Int - dependantAppVersion AppVersion? @relation("AppVersionDependency", fields: [dependantAppVersionId], references: [id]) - dependencyAppVersion AppVersion? @relation("AppVersionDependsOn", fields: [dependencyAppVersionId], references: [id]) + dependantAppVersion Version @relation("AppVersionDependency", fields: [dependantAppVersionId], references: [id], onDelete: Cascade) + dependencyAppVersion Version @relation("AppVersionDependsOn", fields: [dependencyAppVersionId], references: [id]) } diff --git a/tsconfig.base.json b/tsconfig.base.json index d604da3..7a5b0ea 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -18,6 +18,7 @@ "@verity/auth": ["libs/auth/src/index.ts"], "@verity/auth/server": ["libs/auth/src/server.ts"], "@verity/deps-api": ["libs/deps-api/src/server.ts"], + "@verity/prisma": ["libs/prisma/src/index.ts"], "@verity/ui": ["libs/ui/src/index.ts"], "@verity/ui/*": ["libs/ui/src/lib/*"], "@verity/ui/server": ["libs/ui/src/server.ts"],