Skip to content

Commit bba5c24

Browse files
committed
@effect-mongodb/services skeleton package
1 parent 00874e9 commit bba5c24

17 files changed

+265
-3
lines changed

packages/services/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024-present doubleloop.io
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/services/package.json

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"name": "@effect-mongodb/services",
3+
"private": true,
4+
"author": "doubleloop.io",
5+
"version": "0.0.4",
6+
"type": "module",
7+
"license": "MIT",
8+
"description": "A MongoDB toolkit for Effect",
9+
"homepage": "https://github.com/doubleloop-io/effect-mongodb",
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/doubleloop-io/effect-mongodb",
13+
"directory": "packages/services"
14+
},
15+
"bugs": {
16+
"url": "https://github.com/doubleloop-io/effect-mongodb/issues"
17+
},
18+
"tags": [
19+
"typescript",
20+
"mongodb",
21+
"effect",
22+
"functional-programming"
23+
],
24+
"keywords": [
25+
"typescript",
26+
"mongodb",
27+
"effect",
28+
"functional-programming"
29+
],
30+
"publishConfig": {
31+
"access": "public",
32+
"directory": "dist",
33+
"provenance": true
34+
},
35+
"packageManager": "pnpm@9.4.0",
36+
"scripts": {
37+
"codegen": "build-utils prepare-v2",
38+
"build": "pnpm build-esm && pnpm build-annotate && pnpm build-cjs && build-utils pack-v2",
39+
"build-esm": "tsc -b tsconfig.build.json",
40+
"build-cjs": "babel build/esm --plugins @babel/transform-export-namespace-from --plugins @babel/transform-modules-commonjs --out-dir build/cjs --source-maps",
41+
"build-annotate": "babel build/esm --plugins annotate-pure-calls --out-dir build/esm --source-maps",
42+
"typecheck": "pnpm check",
43+
"typecheck:w": "pnpm check --watch",
44+
"dtslint": "dtslint dtslint",
45+
"check": "tsc -b tsconfig.json",
46+
"test": "vitest",
47+
"coverage": "vitest --coverage"
48+
},
49+
"peerDependencies": {
50+
"effect": "^3.10.14",
51+
"mongodb": "^6.8.0",
52+
"effect-mongodb": "workspace:^"
53+
},
54+
"devDependencies": {
55+
"effect": "^3.10.14",
56+
"mongodb": "^6.8.0",
57+
"effect-mongodb": "workspace:^",
58+
"@types/node": "^22.5.4"
59+
}
60+
}

packages/services/src/index.ts

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { MongoDBContainer } from "@testcontainers/mongodb"
2+
import type { StartedMongoDBContainer } from "@testcontainers/mongodb"
3+
import type { GlobalSetupContext } from "vitest/node"
4+
5+
let container: StartedMongoDBContainer | undefined
6+
7+
export async function setup({ provide }: GlobalSetupContext) {
8+
container = await new MongoDBContainer("mongo:6.0.16").start()
9+
10+
provide("mongoConnectionString", container.getConnectionString())
11+
if (process.env.EFFECT_MONGODB_DEBUG === "true") {
12+
console.log(
13+
`[EFFECT_MONGODB_DEBUG] MongoDB connection string with direct connection: '${container.getConnectionString()}'`
14+
)
15+
}
16+
}
17+
18+
export async function teardown() {
19+
await container?.stop()
20+
}
21+
22+
declare module "vitest" {
23+
export interface ProvidedContext {
24+
mongoConnectionString: string
25+
}
26+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as Effect from "effect/Effect"
2+
import type { Db } from "mongodb"
3+
import { MongoClient } from "mongodb"
4+
import { afterAll, beforeAll, describe, inject } from "vitest"
5+
6+
type MongoContext = {
7+
client: Effect.Effect<MongoClient>
8+
database: Effect.Effect<Db>
9+
_client: () => MongoClient
10+
_database: () => Db
11+
}
12+
13+
export const describeMongo = (
14+
suiteName: string,
15+
tests: (ctx: MongoContext) => void
16+
) => {
17+
describe(suiteName, () => {
18+
let client: MongoClient
19+
let database: Db
20+
let databaseName: string
21+
22+
beforeAll(async () => {
23+
client = new MongoClient(inject("mongoConnectionString"), {
24+
directConnection: true
25+
})
26+
await client.connect()
27+
28+
// https://www.mongodb.com/docs/v5.2/reference/limits/#mongodb-limit-Database-Name-Case-Sensitivity
29+
databaseName = suiteName.replace(/[/\\. "$*<>:|?]/g, "-")
30+
database = client.db(databaseName)
31+
32+
const collections = await database.collections()
33+
await Promise.all(collections.map((x) => x.deleteMany({})))
34+
})
35+
36+
afterAll(async () => {
37+
await client.close()
38+
})
39+
40+
tests({
41+
_client: () => client,
42+
_database: () => database,
43+
client: Effect.sync(() => client),
44+
database: Effect.sync(() => database)
45+
})
46+
})
47+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { expect, test } from "vitest"
2+
import { describeMongo } from "./support/describe-mongo.js"
3+
4+
describeMongo("test containers", (ctx) => {
5+
test("insert and find", async () => {
6+
const db = ctx._database()
7+
const users = db.collection("users")
8+
9+
await users.insertMany([
10+
{ id: 1, name: "ANY_NAME_1" },
11+
{ id: 2, name: "ANY_NAME_2" },
12+
{ id: 3, name: "ANY_NAME_3" }
13+
])
14+
15+
const result = await users.find({}).toArray()
16+
17+
expect(result).toEqual([
18+
expect.objectContaining({ id: 1, name: "ANY_NAME_1" }),
19+
expect.objectContaining({ id: 2, name: "ANY_NAME_2" }),
20+
expect.objectContaining({ id: 3, name: "ANY_NAME_3" })
21+
])
22+
})
23+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "./tsconfig.src.json",
3+
"compilerOptions": {
4+
"tsBuildInfoFile": ".tsbuildinfo/build.tsbuildinfo",
5+
"outDir": "build/esm",
6+
"declarationDir": "build/dts",
7+
"stripInternal": true
8+
}
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"include": ["examples"],
4+
"references": [
5+
{ "path": "tsconfig.src.json" }
6+
],
7+
"compilerOptions": {
8+
"tsBuildInfoFile": ".tsbuildinfo/examples.tsbuildinfo",
9+
"rootDir": "examples",
10+
"noEmit": true,
11+
}
12+
}

packages/services/tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"include": [],
4+
"references": [
5+
{ "path": "tsconfig.src.json" },
6+
{ "path": "tsconfig.test.json" },
7+
{ "path": "tsconfig.examples.json" }
8+
]
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"include": ["src"],
4+
"compilerOptions": {
5+
"tsBuildInfoFile": ".tsbuildinfo/src.tsbuildinfo",
6+
"rootDir": "src",
7+
"outDir": "build/src",
8+
"types": ["node"]
9+
}
10+
}

0 commit comments

Comments
 (0)