mongoose#createConnection TypeScript Examples
The following examples show how to use
mongoose#createConnection.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: database-connection.providers.spec.ts From nestjs-rest-sample with GNU General Public License v3.0 | 6 votes |
describe('DatabaseConnectionProviders', () => {
let conn: any;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [ConfigModule.forFeature(mongodbConfig)],
providers: [...databaseConnectionProviders],
}).compile();
conn = module.get<Connection>(DATABASE_CONNECTION);
});
it('DATABASE_CONNECTION should be defined', () => {
expect(conn).toBeDefined();
});
it('connect is called', () => {
//expect(conn).toBeDefined();
//expect(createConnection).toHaveBeenCalledTimes(1); // it is 2 here. why?
expect(createConnection).toHaveBeenCalledWith("mongodb://localhost/blog", {
// useNewUrlParser: true,
// useUnifiedTopology: true,
//see: https://mongoosejs.com/docs/deprecations.html#findandmodify
// useFindAndModify: false
});
})
});
Example #2
Source File: database-connection.providers.ts From nestjs-rest-sample with GNU General Public License v3.0 | 6 votes |
databaseConnectionProviders = [
{
provide: DATABASE_CONNECTION,
useFactory: (dbConfig: ConfigType<typeof mongodbConfig>): Connection => {
const conn = createConnection(dbConfig.uri, {
//useNewUrlParser: true,
//useUnifiedTopology: true,
//see: https://mongoosejs.com/docs/deprecations.html#findandmodify
//useFindAndModify: false,
});
// conn.on('disconnect', () => {
// console.log('Disconnecting to MongoDB');
// });
return conn;
},
inject: [mongodbConfig.KEY],
},
]
Example #3
Source File: database-connection.providers.spec.ts From nestjs-rest-sample with GNU General Public License v3.0 | 5 votes |
jest.mock('mongoose', () => ({
createConnection: jest.fn().mockImplementation(
(uri:any, options:any)=>({} as any)
),
Connection: jest.fn()
}))
Example #4
Source File: tenancy-core.module.ts From nestjs-tenancy with MIT License | 5 votes |
/**
* Get the connection for the tenant
*
* @private
* @static
* @param {String} tenantId
* @param {TenancyModuleOptions} moduleOptions
* @param {ConnectionMap} connMap
* @param {ModelDefinitionMap} modelDefMap
* @returns {Promise<Connection>}
* @memberof TenancyCoreModule
*/
private static async getConnection(
tenantId: string,
moduleOptions: TenancyModuleOptions,
connMap: ConnectionMap,
modelDefMap: ModelDefinitionMap,
): Promise<Connection> {
// Check if validator is set, if so call the `validate` method on it
if (moduleOptions.validator) {
await moduleOptions.validator(tenantId).validate();
}
// Check if tenantId exist in the connection map
const exists = connMap.has(tenantId);
// Return the connection if exist
if (exists) {
const connection = connMap.get(tenantId) as Connection;
if (moduleOptions.forceCreateCollections) {
// For transactional support the Models/Collections has exist in the
// tenant database, otherwise it will throw error
await Promise.all(
Object.entries(connection.models).map(([k, m]) => m.createCollection())
);
}
return connection;
}
// Otherwise create a new connection
const uri = await Promise.resolve(moduleOptions.uri(tenantId))
const connection = createConnection(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
...moduleOptions.options(),
});
// Attach connection to the models passed in the map
modelDefMap.forEach(async (definition: any) => {
const { name, schema, collection } = definition;
const modelCreated = connection.model(name, schema, collection);
if (moduleOptions.forceCreateCollections) {
// For transactional support the Models/Collections has exist in the
// tenant database, otherwise it will throw error
await modelCreated.createCollection();
}
});
// Add the new connection to the map
connMap.set(tenantId, connection);
return connection;
}