diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index e5964598..3ee27d14 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -51,6 +51,16 @@ jobs: "CLICKHOUSE_DEFAULT_ACCESS": "MANAGEMENT=1" ports: - "27019:8123" + mongodb-replica: + image: bitnami/mongodb:8.0 + env: + MONGODB_ADVERTISED_HOSTNAME: 127.0.0.1 + MONGODB_REPLICA_SET_MODE: primary + MONGODB_ROOT_USER: root + MONGODB_ROOT_PASSWORD: password + MONGODB_REPLICA_SET_KEY: replicasetkey123 + ports: + - "27020:27017" strategy: fail-fast: false matrix: diff --git a/library/sinks/Prisma.test.ts b/library/sinks/Prisma.test.ts index 0e898870..122eb744 100644 --- a/library/sinks/Prisma.test.ts +++ b/library/sinks/Prisma.test.ts @@ -4,7 +4,7 @@ import { Prisma } from "./Prisma"; import { createTestAgent } from "../helpers/createTestAgent"; import { promisify } from "util"; import { exec as execCb } from "child_process"; -import path = require("path"); +import * as path from "path"; const execAsync = promisify(execCb); @@ -213,3 +213,43 @@ t.test("it works with postgres", async (t) => { await client.$disconnect(); }); + +t.test("it works with mongodb", async (t) => { + const agent = createTestAgent(); + agent.start([new Prisma()]); + + process.env.DATABASE_URL = + "mongodb://root:password@127.0.0.1:27020/prisma?authSource=admin&directConnection=true"; + + // Generate prismajs client + const { stdout, stderr } = await execAsync( + "npx prisma generate", // Generate prisma client, reset db and apply migrations + { + cwd: path.join(__dirname, "fixtures/prisma/mongodb"), + } + ); + + if (stderr) { + t.fail(stderr); + } + + // Clear require cache + for (const key in require.cache) { + delete require.cache[key]; + } + + const { PrismaClient } = require("@prisma/client"); + + const client = new PrismaClient(); + + await client.user.create({ + data: { + name: "Alice", + email: "alice@example.com", + }, + }); + + await client.user.deleteMany(); + + await client.$disconnect(); +}); diff --git a/library/sinks/fixtures/prisma/mongodb/schema.prisma b/library/sinks/fixtures/prisma/mongodb/schema.prisma new file mode 100644 index 00000000..e73734a9 --- /dev/null +++ b/library/sinks/fixtures/prisma/mongodb/schema.prisma @@ -0,0 +1,27 @@ +// 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 = "mongodb" + url = env("DATABASE_URL") +} + +model User { + id String @id @default(auto()) @map("_id") @db.ObjectId + email String @unique + name String? + posts Post[] +} + +model Post { + id String @id @default(auto()) @map("_id") @db.ObjectId + slug String @unique + title String + body String + author User @relation(fields: [authorId], references: [id]) + authorId String @db.ObjectId +} \ No newline at end of file diff --git a/sample-apps/docker-compose.yml b/sample-apps/docker-compose.yml index db4b2819..7533b86e 100644 --- a/sample-apps/docker-compose.yml +++ b/sample-apps/docker-compose.yml @@ -57,6 +57,18 @@ services: - "27019:8123" volumes: - clickhouse:/var/lib/clickhouse + mongodb-replica: + image: bitnami/mongodb:4.4 # Newer versions do not run on Apple Silicon + environment: + - MONGODB_ADVERTISED_HOSTNAME=127.0.0.1 + - MONGODB_REPLICA_SET_MODE=primary + - MONGODB_ROOT_USER=root + - MONGODB_ROOT_PASSWORD=password + - MONGODB_REPLICA_SET_KEY=replicasetkey123 + ports: + - "27020:27017" + volumes: + - "mongodb-replica:/bitnami/mongodb" volumes: mongodb: @@ -69,3 +81,5 @@ volumes: driver: local clickhouse: driver: local + mongodb-replica: + driver: local