From 87d14a9545302a02c815843c2a56c2a031a14423 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 17 Jan 2017 09:12:33 +0900 Subject: [PATCH] :v: --- package.json | 1 + src/api/models/access-token.ts | 4 +++- src/api/models/app.ts | 4 +++- src/api/models/appdata.ts | 4 +++- src/api/models/auth-session.ts | 4 +++- src/api/models/drive-file.ts | 4 +++- src/api/models/drive-folder.ts | 4 +++- src/api/models/drive-tag.ts | 4 +++- src/api/models/favorite.ts | 4 +++- src/api/models/following.ts | 4 +++- src/api/models/like.ts | 4 +++- src/api/models/messaging-history.ts | 4 +++- src/api/models/messaging-message.ts | 4 +++- src/api/models/post.ts | 4 +++- src/api/models/signin.ts | 4 +++- src/api/models/user.ts | 4 +++- src/db/mongodb.ts | 17 ++++++++++------- src/index.ts | 15 +++------------ 18 files changed, 59 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index a605f52a7..f8e12fc7f 100644 --- a/package.json +++ b/package.json @@ -108,6 +108,7 @@ "mime-types": "2.1.14", "mocha": "3.2.0", "mongodb": "2.2.21", + "mongoskin": "^2.1.0", "ms": "0.7.2", "multer": "1.2.1", "nprogress": "0.2.0", diff --git a/src/api/models/access-token.ts b/src/api/models/access-token.ts index f94df954d..a24286547 100644 --- a/src/api/models/access-token.ts +++ b/src/api/models/access-token.ts @@ -1,4 +1,6 @@ -const collection = global.db.collection('access_tokens'); +import db from '../../db/mongodb'; + +const collection = db.collection('access_tokens'); collection.createIndex('token'); collection.createIndex('hash'); diff --git a/src/api/models/app.ts b/src/api/models/app.ts index 221a53906..39bb6cfa6 100644 --- a/src/api/models/app.ts +++ b/src/api/models/app.ts @@ -1,4 +1,6 @@ -const collection = global.db.collection('apps'); +import db from '../../db/mongodb'; + +const collection = db.collection('apps'); collection.createIndex('name_id'); collection.createIndex('name_id_lower'); diff --git a/src/api/models/appdata.ts b/src/api/models/appdata.ts index 2d471c434..8dab199fe 100644 --- a/src/api/models/appdata.ts +++ b/src/api/models/appdata.ts @@ -1 +1,3 @@ -export default global.db.collection('appdata'); +import db from '../../db/mongodb'; + +export default db.collection('appdata'); diff --git a/src/api/models/auth-session.ts b/src/api/models/auth-session.ts index 6dbe2fa70..1ee8299f5 100644 --- a/src/api/models/auth-session.ts +++ b/src/api/models/auth-session.ts @@ -1 +1,3 @@ -export default global.db.collection('auth_sessions'); +import db from '../../db/mongodb'; + +export default db.collection('auth_sessions'); diff --git a/src/api/models/drive-file.ts b/src/api/models/drive-file.ts index 06ebf0200..cc49cf44c 100644 --- a/src/api/models/drive-file.ts +++ b/src/api/models/drive-file.ts @@ -1,4 +1,6 @@ -export default global.db.collection('drive_files'); +import db from '../../db/mongodb'; + +export default db.collection('drive_files'); export function validateFileName(name: string): boolean { return ( diff --git a/src/api/models/drive-folder.ts b/src/api/models/drive-folder.ts index f345b3c34..44e6c6f5c 100644 --- a/src/api/models/drive-folder.ts +++ b/src/api/models/drive-folder.ts @@ -1,4 +1,6 @@ -export default global.db.collection('drive_folders'); +import db from '../../db/mongodb'; + +export default db.collection('drive_folders'); export function isValidFolderName(name: string): boolean { return ( diff --git a/src/api/models/drive-tag.ts b/src/api/models/drive-tag.ts index 83c0a8f68..e4b044242 100644 --- a/src/api/models/drive-tag.ts +++ b/src/api/models/drive-tag.ts @@ -1 +1,3 @@ -export default global.db.collection('drive_tags'); +import db from '../../db/mongodb'; + +export default db.collection('drive_tags'); diff --git a/src/api/models/favorite.ts b/src/api/models/favorite.ts index 6d9e7c72b..448b00777 100644 --- a/src/api/models/favorite.ts +++ b/src/api/models/favorite.ts @@ -1 +1,3 @@ -export default global.db.collection('favorites'); +import db from '../../db/mongodb'; + +export default db.collection('favorites'); diff --git a/src/api/models/following.ts b/src/api/models/following.ts index f9d8a41c5..d509c60d8 100644 --- a/src/api/models/following.ts +++ b/src/api/models/following.ts @@ -1 +1,3 @@ -export default global.db.collection('following'); +import db from '../../db/mongodb'; + +export default db.collection('following'); diff --git a/src/api/models/like.ts b/src/api/models/like.ts index aa3bd75c1..7c571b91c 100644 --- a/src/api/models/like.ts +++ b/src/api/models/like.ts @@ -1 +1,3 @@ -export default global.db.collection('likes'); +import db from '../../db/mongodb'; + +export default db.collection('likes'); diff --git a/src/api/models/messaging-history.ts b/src/api/models/messaging-history.ts index 3505e94b5..a06a7dd38 100644 --- a/src/api/models/messaging-history.ts +++ b/src/api/models/messaging-history.ts @@ -1 +1,3 @@ -export default global.db.collection('messaging_histories'); +import db from '../../db/mongodb'; + +export default db.collection('messaging_histories'); diff --git a/src/api/models/messaging-message.ts b/src/api/models/messaging-message.ts index 0e900bda5..1e6865d45 100644 --- a/src/api/models/messaging-message.ts +++ b/src/api/models/messaging-message.ts @@ -1 +1,3 @@ -export default global.db.collection('messaging_messages'); +import db from '../../db/mongodb'; + +export default db.collection('messaging_messages'); diff --git a/src/api/models/post.ts b/src/api/models/post.ts index bea92a5f6..04468dd2a 100644 --- a/src/api/models/post.ts +++ b/src/api/models/post.ts @@ -1 +1,3 @@ -export default global.db.collection('posts'); +import db from '../../db/mongodb'; + +export default db.collection('posts'); diff --git a/src/api/models/signin.ts b/src/api/models/signin.ts index 896afaaf8..151a0359e 100644 --- a/src/api/models/signin.ts +++ b/src/api/models/signin.ts @@ -1 +1,3 @@ -export default global.db.collection('signin'); +import db from '../../db/mongodb'; + +export default db.collection('signin'); diff --git a/src/api/models/user.ts b/src/api/models/user.ts index ac59e2bfc..5645d3856 100644 --- a/src/api/models/user.ts +++ b/src/api/models/user.ts @@ -1,4 +1,6 @@ -const collection = global.db.collection('users'); +import db from '../../db/mongodb'; + +const collection = db.collection('users'); collection.createIndex('username'); collection.createIndex('token'); diff --git a/src/db/mongodb.ts b/src/db/mongodb.ts index e2b2479b4..ea810dba0 100644 --- a/src/db/mongodb.ts +++ b/src/db/mongodb.ts @@ -1,8 +1,11 @@ -import * as mongodb from 'mongodb'; +const mongo = require('mongoskin'); -export default async function(): Promise { - const uri = config.mongodb.user && config.mongodb.pass - ? `mongodb://${config.mongodb.user}:${config.mongodb.pass}@${config.mongodb.host}:${config.mongodb.port}/${config.mongodb.db}` - : `mongodb://${config.mongodb.host}:${config.mongodb.port}/${config.mongodb.db}`; - return await mongodb.MongoClient.connect(uri); -}; +import config from '../conf'; + +const uri = config.mongodb.user && config.mongodb.pass + ? `mongodb://${config.mongodb.user}:${config.mongodb.pass}@${config.mongodb.host}:${config.mongodb.port}/${config.mongodb.db}` + : `mongodb://${config.mongodb.host}:${config.mongodb.port}/${config.mongodb.db}`; + +const db = mongo.db(uri, { native_parser: true }); + +export default db; diff --git a/src/index.ts b/src/index.ts index b025463e3..014488795 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,7 +15,6 @@ import * as chalk from 'chalk'; import portUsed = require('tcp-port-used'); import isRoot = require('is-root'); import ProgressBar from './utils/cli/progressbar'; -import initdb from './db/mongodb'; import LastCommitInfo from './utils/lastCommitInfo'; import EnvironmentInfo from './utils/environmentInfo'; import MachineInfo from './utils/machineInfo'; @@ -106,16 +105,8 @@ async function masterMain(): Promise { * Init worker proccess */ function workerMain(): void { - // Init mongo - initdb().then(db => { - global.db = db; - - // start server - require('./server'); - }, err => { - console.error(err); - process.exit(0); - }); + // start server + require('./server'); } /** @@ -158,7 +149,7 @@ async function init(): Promise { // Try to connect to MongoDB let mongoDBLogger = new Logger('MongoDB'); try { - const db = await initdb(); + const db = require('./db/mongodb').default; mongoDBLogger.info('Successfully connected'); db.close(); } catch (e) {