fix: disable automatic mongoose index creation
This commit is contained in:
1 parent
4ab5722759
commit
38c2d1f1b4
2 files changed
+38
-7
No files matched your search
@@ -1,4 +1,8 @@
|
|||||||
import { resolveMongoAppName, resolveMongoDbName } from './connection.service';
|
import {
|
||||||
|
resolveMongoAppName,
|
||||||
|
resolveMongoConnectionOptions,
|
||||||
|
resolveMongoDbName,
|
||||||
|
} from './connection.service';
|
||||||
|
|
||||||
describe('resolveMongoDbName', () => {
|
describe('resolveMongoDbName', () => {
|
||||||
it('uses production database only for prod', () => {
|
it('uses production database only for prod', () => {
|
||||||
@@ -24,3 +28,18 @@ describe('resolveMongoAppName', () => {
|
|||||||
expect(resolveMongoAppName('', 1234)).toBe('lolympus-api:dev:1234');
|
expect(resolveMongoAppName('', 1234)).toBe('lolympus-api:dev:1234');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('resolveMongoConnectionOptions', () => {
|
||||||
|
it('disables automatic collection and index creation by default', () => {
|
||||||
|
expect(
|
||||||
|
resolveMongoConnectionOptions('lolympus-dev', 'lolympus-api:dev:1'),
|
||||||
|
).toEqual({
|
||||||
|
dbName: 'lolympus-dev',
|
||||||
|
appName: 'lolympus-api:dev:1',
|
||||||
|
minPoolSize: 10,
|
||||||
|
maxPoolSize: 20,
|
||||||
|
autoIndex: false,
|
||||||
|
autoCreate: false,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -16,6 +16,20 @@ export function resolveMongoAppName(
|
|||||||
return `lolympus-api:${nodeEnv || 'dev'}:${pid}`;
|
return `lolympus-api:${nodeEnv || 'dev'}:${pid}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function resolveMongoConnectionOptions(
|
||||||
|
dbName: string,
|
||||||
|
appName: string,
|
||||||
|
): mongoose.ConnectOptions {
|
||||||
|
return {
|
||||||
|
dbName,
|
||||||
|
appName,
|
||||||
|
minPoolSize: 10,
|
||||||
|
maxPoolSize: 20,
|
||||||
|
autoIndex: false,
|
||||||
|
autoCreate: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
@Injectable({ scope: Scope.DEFAULT })
|
@Injectable({ scope: Scope.DEFAULT })
|
||||||
export class ConnectionService implements OnModuleInit {
|
export class ConnectionService implements OnModuleInit {
|
||||||
private connection: mongoose.Connection | null = null;
|
private connection: mongoose.Connection | null = null;
|
||||||
@@ -34,12 +48,10 @@ export class ConnectionService implements OnModuleInit {
|
|||||||
const dbName = resolveMongoDbName();
|
const dbName = resolveMongoDbName();
|
||||||
const appName = resolveMongoAppName();
|
const appName = resolveMongoAppName();
|
||||||
const defaultConnection = await mongoose
|
const defaultConnection = await mongoose
|
||||||
.createConnection(connectionString, {
|
.createConnection(
|
||||||
dbName,
|
connectionString,
|
||||||
appName,
|
resolveMongoConnectionOptions(dbName, appName),
|
||||||
minPoolSize: 10,
|
)
|
||||||
maxPoolSize: 20,
|
|
||||||
})
|
|
||||||
.asPromise();
|
.asPromise();
|
||||||
this.connection = defaultConnection;
|
this.connection = defaultConnection;
|
||||||
console.log(
|
console.log(
|
||||||
|
|||||||
Reference in new issue
Block a user