Skip to content

Commit

Permalink
Merge pull request #1822 from nestjs/10.0.0
Browse files Browse the repository at this point in the history
chore: laying the grounds for 10.0.0
  • Loading branch information
kamilmysliwiec authored Jun 15, 2023
2 parents 903a45c + 0979e93 commit 307692f
Show file tree
Hide file tree
Showing 12 changed files with 16,100 additions and 1,407 deletions.
21 changes: 10 additions & 11 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ version: 2
aliases:
- &restore-cache
restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
key: dependency-cache-{{ checksum "package.json" }}
- &install-deps
run:
name: Install dependencies
command: npm ci
name: Install dependencies
command: npm ci
- &build-packages
run:
name: Build
command: npm run build
name: Build
command: npm run build

jobs:
build:
Expand All @@ -34,23 +34,23 @@ jobs:
- ./node_modules
- run:
name: Build
command: npm run build
command: npm run build
integration_tests:
working_directory: ~/nest
machine: true
steps:
- checkout
- run:
- run:
name: Prepare nvm
command: |
echo 'export NVM_DIR="/opt/circleci/.nvm"' >> $BASH_ENV
echo ' [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> $BASH_ENV
- run:
- run:
name: Upgrade Node.js
command: |
nvm install v12
nvm install v16
node -v
nvm alias default v12
nvm alias default v16
- run:
name: Install Docker Compose
command: |
Expand Down Expand Up @@ -78,4 +78,3 @@ workflows:
- integration_tests:
requires:
- build

1 change: 0 additions & 1 deletion index.d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions index.js

This file was deleted.

1 change: 0 additions & 1 deletion index.ts

This file was deleted.

8 changes: 4 additions & 4 deletions lib/factories/schema.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import { TypeMetadataStorage } from '../storages/type-metadata.storage';
import { DefinitionsFactory } from './definitions.factory';

export class SchemaFactory {
// TODO: remove unused, deprecated type argument
// eslint-disable-next-line @typescript-eslint/no-unused-vars
static createForClass<TClass = any, _TDeprecatedTypeArgument = any>(
static createForClass<TClass = any>(
target: Type<TClass>,
): mongoose.Schema<TClass> {
const schemaDefinition = DefinitionsFactory.createForClass(target);
const schemaMetadata =
TypeMetadataStorage.getSchemaMetadataByTarget(target);
const schemaOpts = schemaMetadata?.options;

return new mongoose.Schema<TClass>(
schemaDefinition as SchemaDefinition<SchemaDefinitionType<TClass>>,
schemaMetadata && schemaMetadata.options,
schemaOpts as mongoose.SchemaOptions<any>,
);
}
}
10 changes: 6 additions & 4 deletions lib/interfaces/mongoose-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { ModuleMetadata, Type } from '@nestjs/common';
import { ConnectOptions, MongooseError } from 'mongoose';

export interface MongooseModuleOptions
extends ConnectOptions,
Record<string, any> {
export interface MongooseModuleOptions extends ConnectOptions {
uri?: string;
retryAttempts?: number;
retryDelay?: number;
connectionName?: string;
connectionFactory?: (connection: any, name: string) => any;
connectionErrorFactory?: (error: MongooseError) => MongooseError;
lazyConnection?: boolean;
}

export interface MongooseOptionsFactory {
Expand All @@ -18,7 +17,10 @@ export interface MongooseOptionsFactory {
| MongooseModuleOptions;
}

export interface MongooseModuleFactoryOptions extends Omit<MongooseModuleOptions, 'connectionName'> {}
export type MongooseModuleFactoryOptions = Omit<
MongooseModuleOptions,
'connectionName'
>;

export interface MongooseModuleAsyncOptions
extends Pick<ModuleMetadata, 'imports'> {
Expand Down
48 changes: 36 additions & 12 deletions lib/mongoose-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import * as mongoose from 'mongoose';
import { ConnectOptions, Connection } from 'mongoose';
import { defer, lastValueFrom } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { getConnectionToken, handleRetry } from './common/mongoose.utils';
Expand Down Expand Up @@ -41,28 +42,34 @@ export class MongooseCoreModule implements OnApplicationShutdown {
connectionName,
connectionFactory,
connectionErrorFactory,
lazyConnection,
...mongooseOptions
} = options;

const mongooseConnectionFactory =
connectionFactory || ((connection) => connection);

const mongooseConnectionError =
connectionErrorFactory || ((error) => error);
connectionErrorFactory || ((error) => error);

const mongooseConnectionName = getConnectionToken(connectionName);

const mongooseConnectionNameProvider = {
provide: MONGOOSE_CONNECTION_NAME,
useValue: mongooseConnectionName,
};

const connectionProvider = {
provide: mongooseConnectionName,
useFactory: async (): Promise<any> =>
await lastValueFrom(
defer(async () =>
mongooseConnectionFactory(
await mongoose.createConnection(uri, mongooseOptions).asPromise(),
await this.createMongooseConnection(
uri,
mongooseOptions,
lazyConnection,
),
mongooseConnectionName,
),
).pipe(
Expand Down Expand Up @@ -99,21 +106,24 @@ export class MongooseCoreModule implements OnApplicationShutdown {
uri,
connectionFactory,
connectionErrorFactory,
lazyConnection,
...mongooseOptions
} = mongooseModuleOptions;

const mongooseConnectionFactory =
connectionFactory || ((connection) => connection);

const mongooseConnectionError =
connectionErrorFactory || ((error) => error);
connectionErrorFactory || ((error) => error);

return await lastValueFrom(
defer(async () =>
mongooseConnectionFactory(
await mongoose
.createConnection(uri as string, mongooseOptions)
.asPromise(),
await this.createMongooseConnection(
uri as string,
mongooseOptions,
lazyConnection,
),
mongooseConnectionName,
),
).pipe(
Expand All @@ -139,11 +149,6 @@ export class MongooseCoreModule implements OnApplicationShutdown {
};
}

async onApplicationShutdown() {
const connection = this.moduleRef.get<any>(this.connectionName);
connection && (await connection.close());
}

private static createAsyncProviders(
options: MongooseModuleAsyncOptions,
): Provider[] {
Expand Down Expand Up @@ -181,4 +186,23 @@ export class MongooseCoreModule implements OnApplicationShutdown {
inject,
};
}

private static async createMongooseConnection(
uri: string,
mongooseOptions: ConnectOptions,
lazyConnection?: boolean,
): Promise<Connection> {
const connection = mongoose.createConnection(uri, mongooseOptions);

if (lazyConnection) {
return connection;
}

return connection.asPromise();
}

async onApplicationShutdown() {
const connection = this.moduleRef.get<any>(this.connectionName);
connection && (await connection.close());
}
}
Loading

0 comments on commit 307692f

Please sign in to comment.