@slango/mangusta 2.1.39 → 2.1.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,22 +0,0 @@
1
- import type { TestProject } from 'vitest/node';
2
-
3
- import { MongoMemoryServer } from 'mongodb-memory-server';
4
-
5
- let mongoServer: MongoMemoryServer;
6
-
7
- export async function setup(project: TestProject) {
8
- mongoServer = await MongoMemoryServer.create();
9
- const uri = mongoServer.getUri();
10
-
11
- // Type assertion needed as vitest's ProvidedContext typing is complex
12
- (project.provide as (key: string, value: string) => void)('MONGO_URI', uri);
13
-
14
- console.log('[mangusta] MongoDB Memory Server started:', uri);
15
- }
16
-
17
- export async function teardown() {
18
- if (mongoServer) {
19
- await mongoServer.stop();
20
- console.log('[mangusta] MongoDB Memory Server stopped');
21
- }
22
- }
@@ -1,29 +0,0 @@
1
- import mongoose, { Document, Model, Schema } from 'mongoose';
2
-
3
- import { PluginFunction } from '../types.js';
4
-
5
- export interface CreateModelWithPluginParams<Doc extends Document, Options> {
6
- buildSchema?: () => Schema<Doc>;
7
- modelName: string;
8
- plugin: PluginFunction<Options>;
9
- pluginOptions?: Options;
10
- }
11
-
12
- export const createModelWithPlugin = <Doc extends Document, Options>({
13
- buildSchema,
14
- modelName,
15
- plugin,
16
- pluginOptions,
17
- }: CreateModelWithPluginParams<Doc, Options>): { model: Model<Doc>; schema: Schema<Doc> } => {
18
- if (mongoose.models[modelName]) {
19
- delete mongoose.models[modelName];
20
- }
21
-
22
- const schema = buildSchema ? buildSchema() : new Schema<Doc>({});
23
-
24
- schema.plugin(plugin, pluginOptions);
25
-
26
- const modelInstance = mongoose.model<Doc>(modelName, schema);
27
-
28
- return { model: modelInstance, schema };
29
- };
@@ -1,54 +0,0 @@
1
- import { MongoMemoryServer } from 'mongodb-memory-server';
2
- import mongoose from 'mongoose';
3
- import { afterAll, afterEach, beforeAll } from 'vitest';
4
-
5
- const ConnectionStates = mongoose.ConnectionStates;
6
-
7
- type SetupMongoTestEnvironmentOptions = {
8
- preserveCollections?: string[];
9
- };
10
-
11
- export function setupMongoTestEnvironment({
12
- preserveCollections = [],
13
- }: SetupMongoTestEnvironmentOptions = {}) {
14
- let mongoServer: MongoMemoryServer;
15
-
16
- beforeAll(async () => {
17
- mongoServer = await MongoMemoryServer.create();
18
- const uri = mongoServer.getUri();
19
- process.env.MONGO_URI = uri;
20
- await mongoose.connect(uri, { dbName: 'test-db' });
21
-
22
- await waitForDatabaseConnection();
23
- });
24
-
25
- afterAll(async () => {
26
- await mongoose.disconnect();
27
- await mongoServer.stop();
28
- });
29
-
30
- afterEach(async () => {
31
- if (mongoose.connection.db) {
32
- const collections = await mongoose.connection.db.collections();
33
- for (const collection of collections) {
34
- if (!preserveCollections.includes(collection.collectionName)) {
35
- await collection.deleteMany({});
36
- }
37
- }
38
- }
39
- });
40
- }
41
-
42
- async function waitForDatabaseConnection(maxRetries = 5, interval = 1000) {
43
- let retries = 0;
44
-
45
- while (mongoose.connection.readyState !== ConnectionStates.connected && retries < maxRetries) {
46
- await new Promise((resolve) => setTimeout(resolve, interval));
47
- retries++;
48
- console.log(`Retrying database connection (${retries}/${maxRetries})...`);
49
- }
50
-
51
- if (mongoose.connection.readyState !== ConnectionStates.connected) {
52
- throw new Error('Failed to connect to the database within the allowed retries.');
53
- }
54
- }
@@ -1,52 +0,0 @@
1
- /**
2
- * Module for managing the test MongoDB URI.
3
- * This allows tests to access the current test database URI without relying on environment variables.
4
- */
5
-
6
- let currentTestMongoUri: null | string = null;
7
-
8
- /**
9
- * Sets the current test MongoDB URI.
10
- * Called internally by setupMongoTestEnvironment().
11
- * @internal
12
- */
13
- export function setTestMongoUri(uri: string): void {
14
- currentTestMongoUri = uri;
15
- }
16
-
17
- /**
18
- * Gets the current test MongoDB URI.
19
- * Use this to configure frameworks (like NestJS) that need the database connection string.
20
- *
21
- * @example
22
- * ```typescript
23
- * // In your test setup
24
- * import { getTestMongoUri } from '@slango/mangusta/test-utils/testMongoUri';
25
- *
26
- * const moduleFixture = await Test.createTestingModule({
27
- * imports: [AppModule],
28
- * })
29
- * .overrideProvider(DATABASE_URI)
30
- * .useFactory({ factory: () => getTestMongoUri() })
31
- * .compile();
32
- * ```
33
- *
34
- * @throws Error if called before setupMongoTestEnvironment() has run
35
- */
36
- export function getTestMongoUri(): string {
37
- if (!currentTestMongoUri) {
38
- throw new Error(
39
- 'Test MongoDB URI not available. Ensure setupMongoTestEnvironment() is called in beforeAll.',
40
- );
41
- }
42
- return currentTestMongoUri;
43
- }
44
-
45
- /**
46
- * Clears the current test MongoDB URI.
47
- * Called internally by setupMongoTestEnvironment() in afterAll.
48
- * @internal
49
- */
50
- export function clearTestMongoUri(): void {
51
- currentTestMongoUri = null;
52
- }
@@ -1,7 +0,0 @@
1
- import 'vitest';
2
-
3
- declare module 'vitest' {
4
- export interface ProvidedContext {
5
- MONGO_URI: string;
6
- }
7
- }
package/src/types.ts DELETED
@@ -1,4 +0,0 @@
1
- import { Schema } from 'mongoose';
2
-
3
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
- export type PluginFunction<Options = any> = (schema: Schema, options?: Options) => void;
@@ -1,10 +0,0 @@
1
- {
2
- "$schema": "https://json.schemastore.org/tsconfig",
3
- "extends": "./tsconfig.json",
4
- "compilerOptions": {
5
- "noEmit": false,
6
- "outDir": "./dist",
7
- "rootDir": "./src"
8
- },
9
- "exclude": ["node_modules", "dist", "src/**/*.spec.ts"]
10
- }