Skip to content

Commit

Permalink
Add initial prisma mongodb test
Browse files Browse the repository at this point in the history
  • Loading branch information
timokoessler committed Nov 22, 2024
1 parent 33bb42e commit e411408
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
42 changes: 41 additions & 1 deletion library/sinks/Prisma.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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:[email protected]: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: "[email protected]",
},
});

await client.user.deleteMany();

await client.$disconnect();
});
27 changes: 27 additions & 0 deletions library/sinks/fixtures/prisma/mongodb/schema.prisma
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions sample-apps/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -69,3 +81,5 @@ volumes:
driver: local
clickhouse:
driver: local
mongodb-replica:
driver: local

0 comments on commit e411408

Please sign in to comment.