本文整理汇总了TypeScript中mongodb.connect函数的典型用法代码示例。如果您正苦于以下问题:TypeScript connect函数的具体用法?TypeScript connect怎么用?TypeScript connect使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了connect函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: init
async function init() {
log(`Connecting to system database...`);
while (!systemDB)
try {
systemDB = await mongodb.connect(make_mongo_url(config.systemDatabase));
} catch (err) {
console.log(err);
log.warn('Could not connect to system database. Retrying in 1s...');
await new Promise(r => setTimeout(r, 1e3));
}
var clientConfig = await systemDB.collection('clients').find({ id: clientID }).toArray();
if (!clientConfig.length)
await systemDB.collection('clients').insert({ id: clientID });
else
clientConfig = clientConfig[0];
if (!clientConfig.role) {
log.warn(`No client configuration in database. Client idle.`);
return;
}
log(`Role: ${colors.magenta(clientConfig.role.toUpperCase())}`);
log(`Instances: ${colors.magenta(clientConfig.instances)}`);
require(roleHandlerMap[clientConfig.role]).default(clientConfig, flags, config, numOfCores);
log.green('Master process initialized.');
}
示例2: init
export async function init() {
if(collection) {
return collection;
}
assert(dbName, 'Mongo DB name should be specified');
assert(collectionName, 'Mongo collection name should be specified');
assert(connectionString, 'Mongo connection string should be specified');
try {
const client = await connect(connectionString as string, {
useNewUrlParser: true,
});
return collection = client.db(dbName).collection<ChatShape>(collectionName as string)
} catch (error) {
throw error;
}
}
示例3: function
async function(req, res){
try{
MongoClient.connect(process.env.MONGO_URL, async (err, db)=>{
const uploadsDir = `${__dirname}/uploads`;
let filesOnDisk = fs.readdirSync(`${uploadsDir}`);
let filesFromDb = [
...await getBrandPhotos(db),
...await getContactPhotos(db),
...await getFavoursPhotos(db),
...await getMastersPhotos(db),
...await getProductsPhotos(db),
...await getModelsPhotos(db),
...await getCoursesPhotos(db),
...await getSalonsPhotos(db),
...await getTransformsPhotos(db),
].map(url=>url.split('/')[3]);
let unusedFiles = arrayDiff(filesOnDisk, filesFromDb);
let wrongRecords = arrayDiff(filesFromDb, filesOnDisk);
console.log('filesOnDisk length ', filesOnDisk.length);
console.log('filesFromDb length ', filesFromDb.length);
console.log('unusedFiles length', unusedFiles.length);
console.log('wrongRecords', wrongRecords);
unusedFiles.forEach((file:string)=>fs.unlink(`${uploadsDir}/${file}`));
console.log(`${unusedFiles.length} files deleted`);
process.exit();
});
res.status(200).end();
}catch (err){
res.status(404).end();
}
}
示例4: init
/**
* 初期化
*/
static async init() : Promise<void>
{
const log = slog.stepIn('MongoDB', 'init');
try
{
if (Config.hasMongoDB())
{
const url = Config.MONGO_URL;
Database.db = await mongodb.connect(url);
}
log.stepOut();
}
catch (err)
{
console.error('MongoDBの初期化に失敗しました。');
console.error(err.message);
log.e(err.message);
log.stepOut();
// すぐに終了するとログが出力されないので数秒待ってから終了する
setTimeout(() => process.exit(-1), 3000);
}
}
示例5: testFunc
async function testFunc(): Promise<mongodb.MongoClient> {
const testClient: mongodb.MongoClient = await mongodb.connect(connectionString);
return testClient;
}