當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript mongodb.connect函數代碼示例

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

}
開發者ID:raelgor,項目名稱:zen-arena,代碼行數:33,代碼來源:main.ts

示例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;
    }
}
開發者ID:ddddm,項目名稱:remind-to-pay-netflix-bot,代碼行數:18,代碼來源:index.ts

示例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();
            }
        }
開發者ID:galyna,項目名稱:PalamarGroup,代碼行數:37,代碼來源:photo.endpoint.ts

示例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);
        }
    }
開發者ID:nandai,項目名稱:web-service-template,代碼行數:27,代碼來源:mongodb.ts

示例5: testFunc

async function testFunc(): Promise<mongodb.MongoClient> {
    const testClient: mongodb.MongoClient = await mongodb.connect(connectionString);
    return testClient;
}
開發者ID:CNBoland,項目名稱:DefinitelyTyped,代碼行數:4,代碼來源:mongodb-tests.ts


注:本文中的mongodb.connect函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。