本文整理匯總了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;
}