mongoose#connect TypeScript Examples
The following examples show how to use
mongoose#connect.
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: Mongo.ts From ModMail with MIT License | 6 votes |
private constructor(caller: Mail) {
this.caller = caller;
this.DB = connect(process.env.MONGO_URI!, {
useUnifiedTopology: true,
useNewUrlParser: true
})
.then(() => console.log('[DATABASE] Connection established.'))
.catch((err) => console.error(`[DATABASE] Connection error:\n ${err}`));
}
Example #2
Source File: Database.ts From void-wa with GNU General Public License v3.0 | 6 votes |
public async connect(): Promise<void> {
const uri = process.env.MONGODB_URI;
if (!uri) {
this.client.log.error("MONGODB_URI is missing, please fill the value!");
process.exit(1);
}
try {
const { connection } = await connect(uri, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false });
connection.once("open", () => this.client.log.info("Database connection opened!"));
connection.on("connected", () => this.client.log.info("Database connected!"));
connection.on("error", error => this.client.log.error(error));
this.connection = connection;
this.connected = true;
} catch (e) {
this.client.log.error(e);
this.connection = undefined;
this.connected = false;
}
}
Example #3
Source File: index.ts From vt-api with GNU Affero General Public License v3.0 | 5 votes |
// establish connection and log on status change
connect(URI, options);
Example #4
Source File: index.ts From xi-jinping-bot with GNU Affero General Public License v3.0 | 5 votes |
connect(process.env.MONGO_URI)
Example #5
Source File: index.ts From graphql-mesh with MIT License | 4 votes |
async getMeshSource(): Promise<MeshSource> {
if (this.config.connectionString) {
connect(this.config.connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
} as ConnectOptions).catch(e => console.error(e));
await this.pubsub.subscribe('destroy', () => disconnect());
}
const schemaComposer = new SchemaComposer();
await Promise.all([
Promise.all(
this.config.models?.map(async modelConfig => {
const model = await loadFromModuleExportExpression<Model<Document<any, any, any>>>(modelConfig.path, {
defaultExportName: modelConfig.name,
cwd: this.baseDir,
importFn: this.importFn,
});
if (!model) {
throw new Error(`Model ${modelConfig.name} cannot be imported ${modelConfig.path}!`);
}
const modelTC = composeWithMongoose(model, modelConfig.options as any);
await Promise.all([
Promise.all(
modelQueryOperations.map(async queryOperation =>
schemaComposer.Query.addFields({
[`${modelConfig.name}_${queryOperation}`]: modelTC.getResolver(queryOperation),
})
)
),
Promise.all(
modelMutationOperations.map(async mutationOperation =>
schemaComposer.Mutation.addFields({
[`${modelConfig.name}_${mutationOperation}`]: modelTC.getResolver(mutationOperation),
})
)
),
]);
if (this.config.autoTypeMerging) {
modelTC.setDirectiveByName('key', {
selectionSet: /* GraphQL */ `
{
id
}
`,
});
modelTC.setFieldDirectiveByName(`${modelConfig.name}_dataLoaderMany`, 'merge');
}
}) || []
),
Promise.all(
this.config.discriminators?.map(async discriminatorConfig => {
const discriminator = await loadFromModuleExportExpression<any>(discriminatorConfig.path, {
defaultExportName: discriminatorConfig.name,
cwd: this.baseDir,
importFn: this.importFn,
});
const discriminatorTC = composeWithMongooseDiscriminators(discriminator, discriminatorConfig.options as any);
await Promise.all([
Promise.all(
modelQueryOperations.map(async queryOperation =>
schemaComposer.Query.addFields({
[`${discriminatorConfig.name}_${queryOperation}`]: discriminatorTC.getResolver(queryOperation),
})
)
),
Promise.all(
modelMutationOperations.map(async mutationOperation =>
schemaComposer.Mutation.addFields({
[`${discriminatorConfig.name}_${mutationOperation}`]: discriminatorTC.getResolver(mutationOperation),
})
)
),
]);
if (this.config.autoTypeMerging) {
discriminatorTC.setDirectiveByName('key', {
selectionSet: /* GraphQL */ `
{
id
}
`,
});
discriminatorTC.setFieldDirectiveByName(`${discriminatorConfig.name}_dataLoaderMany`, 'merge');
}
}) || []
),
]);
// graphql-compose doesn't add @defer and @stream to the schema
specifiedDirectives.forEach(directive => schemaComposer.addDirective(directive));
if (this.config.autoTypeMerging) {
const defaultStitchingDirectives = stitchingDirectives();
defaultStitchingDirectives.allStitchingDirectives.forEach(directive => schemaComposer.addDirective(directive));
}
const schema = schemaComposer.buildSchema();
return {
schema,
};
}