diff --git a/.gitignore b/.gitignore index e25ff9f..024fdcc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,184 @@ cipherstash-proxy.custom.toml -.DS_Store # Temporary until this is better implemented k8s .envrc -dynamodb/app/target \ No newline at end of file +dynamodb/app/target + +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Caches + +.cache + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store diff --git a/postgres/db/Dockerfile b/postgres/db/Dockerfile index fc07a56..77d176c 100644 --- a/postgres/db/Dockerfile +++ b/postgres/db/Dockerfile @@ -1,4 +1,13 @@ FROM --platform=linux/amd64 postgres:16 as db + +# Install pgvector +RUN apt-get update && apt-get install -y git make build-essential +RUN apt-get install -y postgresql-server-dev-16 +RUN git clone --branch v0.7.4 https://github.com/pgvector/pgvector.git +WORKDIR /pgvector +RUN make +RUN make install + WORKDIR /app COPY init.sh /docker-entrypoint-initdb.d COPY dump.sql ./scripts/db/dump.sql diff --git a/postgres/examples/prisma/.env.example b/postgres/examples/prisma/.env.example new file mode 100644 index 0000000..1306b20 --- /dev/null +++ b/postgres/examples/prisma/.env.example @@ -0,0 +1 @@ +DATABASE_URL=postgres://postgres:password@localhost:5432/postgres \ No newline at end of file diff --git a/postgres/examples/prisma/README.md b/postgres/examples/prisma/README.md new file mode 100644 index 0000000..acd0538 --- /dev/null +++ b/postgres/examples/prisma/README.md @@ -0,0 +1,46 @@ +# cipherstash-prisma-orm-example + +To install dependencies: + +```bash +bun install +``` + +## Environment variables + +Copy the `.env.example` file to the two additional files: + +- `.env.local` - Used for the application `index.ts` file and connects through CipherStash Proxy +- `.env` - Used for the Prisma Migrations + +Edit the `.env.local` file to include the following environment variables: + +```bash +DATABASE_URL=postgres://postgres:password@localhost:6432/postgres +``` + +Edit the `.env` file to include the following environment variables: + +```bash +DATABASE_URL=postgres://postgres:password@localhost:5432/postgres +``` + +The only difference is the port number. + +## Running migrations + +To run migrations run the following command: + +```bash +bun prisma migrate dev +``` + +This will run the migrations against the local database. + +## Running the application + +Run the following command to start the application: + +```bash +bun run index.ts +``` diff --git a/postgres/examples/prisma/bun.lockb b/postgres/examples/prisma/bun.lockb new file mode 100755 index 0000000..7ae99b8 Binary files /dev/null and b/postgres/examples/prisma/bun.lockb differ diff --git a/postgres/examples/prisma/index.ts b/postgres/examples/prisma/index.ts new file mode 100644 index 0000000..886d9c1 --- /dev/null +++ b/postgres/examples/prisma/index.ts @@ -0,0 +1,15 @@ +import { PrismaClient } from '@prisma/client' + +const prisma = new PrismaClient() + +// Uncomment the following line to create a new user +// await prisma.user.create({ +// data: { +// firstName: 'Alice', +// email: 'alice@prisma.io', +// password: 'alice', +// }, +// }) + +const allUsers = await prisma.user.findMany() +console.dir(allUsers, { depth: null }) diff --git a/postgres/examples/prisma/package.json b/postgres/examples/prisma/package.json new file mode 100644 index 0000000..58755e4 --- /dev/null +++ b/postgres/examples/prisma/package.json @@ -0,0 +1,15 @@ +{ + "name": "cipherstash-prisma-orm-example", + "module": "index.ts", + "type": "module", + "devDependencies": { + "@types/bun": "latest" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "dependencies": { + "@prisma/client": "^5.19.1", + "prisma": "^5.19.1" + } +} \ No newline at end of file diff --git a/postgres/examples/prisma/prisma/migrations/20240913124049_init/migration.sql b/postgres/examples/prisma/prisma/migrations/20240913124049_init/migration.sql new file mode 100644 index 0000000..0622a94 --- /dev/null +++ b/postgres/examples/prisma/prisma/migrations/20240913124049_init/migration.sql @@ -0,0 +1,202 @@ +CREATE EXTENSION IF NOT EXISTS vector; + +-- CreateEnum +CREATE TYPE "JobStatus" AS ENUM ('completed', 'progress', 'failed', 'active'); + +-- CreateEnum +CREATE TYPE "UserStatus" AS ENUM ('active', 'inactive', 'suspended'); + +-- CreateEnum +CREATE TYPE "UsageType" AS ENUM ('search', 'upload'); + +-- CreateTable +CREATE TABLE "User" ( + "id" TEXT NOT NULL, + "firstName" TEXT, + "lastName" TEXT, + "email" TEXT NOT NULL, + "password" TEXT NOT NULL, + "status" "UserStatus" NOT NULL DEFAULT 'active', + "phone" TEXT, + "gender" TEXT, + "bio" TEXT, + "profilePicUrl" TEXT, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "verifiedAt" TIMESTAMP(3), + "updatedAt" TIMESTAMP(3) NOT NULL, + "deletedAt" TIMESTAMP(3), + + CONSTRAINT "User_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Role" ( + "id" TEXT NOT NULL, + "name" TEXT NOT NULL, + + CONSTRAINT "Role_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Job" ( + "id" TEXT NOT NULL, + "name" TEXT NOT NULL, + "status" "JobStatus" NOT NULL DEFAULT 'active', + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + "userId" TEXT NOT NULL, + + CONSTRAINT "Job_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "File" ( + "id" TEXT NOT NULL, + "name" TEXT NOT NULL, + "mimeType" TEXT NOT NULL, + "title" TEXT, + "description" TEXT, + "path" TEXT NOT NULL, + "processed" BOOLEAN NOT NULL DEFAULT false, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + "deletedAt" TIMESTAMP(3), + "jobId" TEXT, + "userId" TEXT NOT NULL, + "createdBy" TEXT NOT NULL, + "embedding" vector(1536), + "blurHash" TEXT, + "size" BIGINT, + "hash" TEXT, + + CONSTRAINT "File_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Collection" ( + "id" TEXT NOT NULL, + "name" TEXT NOT NULL, + "description" TEXT, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + "deletedAt" TIMESTAMP(3), + "userId" TEXT NOT NULL, + "createdBy" TEXT NOT NULL, + + CONSTRAINT "Collection_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Usage" ( + "id" TEXT NOT NULL, + "type" "UsageType" NOT NULL DEFAULT 'search', + "query" TEXT, + "count" INTEGER NOT NULL DEFAULT 1, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + "createdBy" TEXT NOT NULL, + + CONSTRAINT "Usage_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "_UserRoles" ( + "A" TEXT NOT NULL, + "B" TEXT NOT NULL +); + +-- CreateTable +CREATE TABLE "_FileCollections" ( + "A" TEXT NOT NULL, + "B" TEXT NOT NULL +); + +-- CreateIndex +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); + +-- CreateIndex +CREATE INDEX "User_email_idx" ON "User"("email"); + +-- CreateIndex +CREATE UNIQUE INDEX "Role_name_key" ON "Role"("name"); + +-- CreateIndex +CREATE INDEX "Job_status_idx" ON "Job"("status"); + +-- CreateIndex +CREATE INDEX "Job_userId_idx" ON "Job"("userId"); + +-- CreateIndex +CREATE UNIQUE INDEX "File_hash_key" ON "File"("hash"); + +-- CreateIndex +CREATE INDEX "File_processed_idx" ON "File"("processed"); + +-- CreateIndex +CREATE INDEX "File_userId_idx" ON "File"("userId"); + +-- CreateIndex +CREATE INDEX "File_jobId_idx" ON "File"("jobId"); + +-- CreateIndex +CREATE INDEX "File_createdBy_idx" ON "File"("createdBy"); + +-- CreateIndex +CREATE INDEX "File_hash_idx" ON "File"("hash"); + +-- CreateIndex +CREATE INDEX "Collection_userId_idx" ON "Collection"("userId"); + +-- CreateIndex +CREATE INDEX "Collection_createdBy_idx" ON "Collection"("createdBy"); + +-- CreateIndex +CREATE INDEX "Usage_createdBy_idx" ON "Usage"("createdBy"); + +-- CreateIndex +CREATE INDEX "Usage_type_idx" ON "Usage"("type"); + +-- CreateIndex +CREATE UNIQUE INDEX "_UserRoles_AB_unique" ON "_UserRoles"("A", "B"); + +-- CreateIndex +CREATE INDEX "_UserRoles_B_index" ON "_UserRoles"("B"); + +-- CreateIndex +CREATE UNIQUE INDEX "_FileCollections_AB_unique" ON "_FileCollections"("A", "B"); + +-- CreateIndex +CREATE INDEX "_FileCollections_B_index" ON "_FileCollections"("B"); + +-- AddForeignKey +ALTER TABLE "Job" ADD CONSTRAINT "Job_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "File" ADD CONSTRAINT "File_jobId_fkey" FOREIGN KEY ("jobId") REFERENCES "Job"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "File" ADD CONSTRAINT "File_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "File" ADD CONSTRAINT "File_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Collection" ADD CONSTRAINT "Collection_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Collection" ADD CONSTRAINT "Collection_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Usage" ADD CONSTRAINT "Usage_createdBy_fkey" FOREIGN KEY ("createdBy") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "_UserRoles" ADD CONSTRAINT "_UserRoles_A_fkey" FOREIGN KEY ("A") REFERENCES "Role"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "_UserRoles" ADD CONSTRAINT "_UserRoles_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "_FileCollections" ADD CONSTRAINT "_FileCollections_A_fkey" FOREIGN KEY ("A") REFERENCES "Collection"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "_FileCollections" ADD CONSTRAINT "_FileCollections_B_fkey" FOREIGN KEY ("B") REFERENCES "File"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/postgres/examples/prisma/prisma/migrations/migration_lock.toml b/postgres/examples/prisma/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..fbffa92 --- /dev/null +++ b/postgres/examples/prisma/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 = "postgresql" \ No newline at end of file diff --git a/postgres/examples/prisma/prisma/schema.prisma b/postgres/examples/prisma/prisma/schema.prisma new file mode 100644 index 0000000..04d5ca4 --- /dev/null +++ b/postgres/examples/prisma/prisma/schema.prisma @@ -0,0 +1,137 @@ +// This is your Prisma schema file, +// learn more about it in the docs: https://pris.ly/d/prisma-schema +// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions? +// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init +generator client { + provider = "prisma-client-js" +} + +datasource db { + provider = "postgresql" + url = env("DATABASE_URL") +} + +model User { + id String @id @default(uuid()) + firstName String? + lastName String? + email String @unique + password String + status UserStatus @default(active) + phone String? + gender String? + bio String? + profilePicUrl String? + createdAt DateTime @default(now()) + verifiedAt DateTime? + updatedAt DateTime @updatedAt + deletedAt DateTime? + roles Role[] @relation("UserRoles") + images File[] + jobs Job[] + collections Collection[] + createdFiles File[] @relation("FileCreator") + createdCollections Collection[] @relation("CollectionCreator") + usages Usage[] + + @@index([email]) +} + +model Role { + id String @id @default(uuid()) + name String @unique + users User[] @relation("UserRoles") +} + +enum JobStatus { + completed + progress + failed + active +} + +enum UserStatus { + active + inactive + suspended +} + +model Job { + id String @id + name String + status JobStatus @default(active) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + userId String + user User @relation(fields: [userId], references: [id]) + images File[] + + @@index([status]) + @@index([userId]) +} + +model File { + id String @id @default(uuid()) + name String + mimeType String + title String? + description String? + path String + processed Boolean @default(false) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + deletedAt DateTime? + jobId String? + job Job? @relation(fields: [jobId], references: [id]) + userId String + user User @relation(fields: [userId], references: [id]) + createdBy String + creator User @relation("FileCreator", fields: [createdBy], references: [id]) + embedding Unsupported("vector(1536)")? + collections Collection[] @relation("FileCollections") + blurHash String? + size BigInt? + hash String? @unique // New field for file content hash + + @@index([processed]) + @@index([userId]) + @@index([jobId]) + @@index([createdBy]) + @@index([hash]) // New index for faster lookups +} + +model Collection { + id String @id @default(uuid()) + name String + description String? + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + deletedAt DateTime? + userId String + user User @relation(fields: [userId], references: [id]) + createdBy String + creator User @relation("CollectionCreator", fields: [createdBy], references: [id]) + files File[] @relation("FileCollections") + + @@index([userId]) + @@index([createdBy]) +} + +enum UsageType { + search + upload +} + +model Usage { + id String @id @default(uuid()) + type UsageType @default(search) + query String? + count Int @default(1) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + createdBy String + user User @relation(fields: [createdBy], references: [id]) + + @@index([createdBy]) + @@index([type]) +} diff --git a/postgres/examples/prisma/tsconfig.json b/postgres/examples/prisma/tsconfig.json new file mode 100644 index 0000000..238655f --- /dev/null +++ b/postgres/examples/prisma/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + // Enable latest features + "lib": ["ESNext", "DOM"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false + } +}