-
Notifications
You must be signed in to change notification settings - Fork 818
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0010c37
commit 795d293
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import Connection from './db/connection'; | ||
import User from './db/user'; | ||
|
||
export default class DB { | ||
connection: Connection; | ||
user: User; | ||
|
||
constructor() { | ||
this.connection = new Connection(); | ||
} | ||
|
||
init() { | ||
return this.connection.init() | ||
.then(() => { | ||
let users = this.connection.getCollection('users'); | ||
this.user = new User(users); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import promisify from '../../promisify'; | ||
import Connection from './connection'; | ||
|
||
/** | ||
* Base object for dealing with data in a mongo collection (ie. table). | ||
*/ | ||
export default class BaseDB { | ||
|
||
conn: Connection; | ||
collection: any; | ||
name: string; | ||
|
||
constructor(conn: Connection, name: string) { | ||
this.conn = conn; | ||
this.name = name; | ||
this.collection = this.conn.getCollection(this.name); | ||
} | ||
|
||
private makePromise(method: Function, args?: any[]) { | ||
return promisify(this.conn, method, args); | ||
} | ||
|
||
count() { | ||
return this.makePromise(this.collection.count); | ||
} | ||
|
||
clear() { | ||
return this.makePromise(this.collection.remove); | ||
} | ||
|
||
drop() { | ||
return this.makePromise(this.collection.drop); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Db, Collection } from 'mongodb'; | ||
|
||
const path = require('path'); | ||
const Promise = require('bluebird'); | ||
const mongo = require('mongodb'); | ||
const Client = mongo.MongoClient; | ||
|
||
const REPO_PATH = path.resolve(__dirname, '../../../'); | ||
const CONFIG_PATH = path.resolve(REPO_PATH, 'config.json'); | ||
|
||
export default class Connection { | ||
config: any; | ||
db: Db; | ||
|
||
constructor() { | ||
this.config = require(CONFIG_PATH); | ||
this.db = null; | ||
} | ||
|
||
private getConnectionString(user: string, pass: string, | ||
port: string, name: string) { | ||
return `mongodb://${user}:${pass}@localhost:${port}/${name}`; | ||
} | ||
|
||
init(): Promise<void> { | ||
return new Promise((resolve: EventListener, reject: EventListener) => { | ||
let user = this.config.DB_USER; | ||
let pass = this.config.DB_PASS; | ||
let port = this.config.DB_PORT; | ||
let name = this.config.DB_NAME; | ||
let url = this.getConnectionString(user, pass, port, name); | ||
|
||
mongo.MongoClient.connect(url, (err: Error, db: Db) => { | ||
if (err) { | ||
console.error('could not connect to mongo', err); | ||
reject(new Event('Connection Error')); | ||
return; | ||
} | ||
|
||
this.db = db; | ||
resolve(null); | ||
}); | ||
}); | ||
} | ||
|
||
getCollection(name: string): Collection { | ||
if (!this.db) { | ||
console.error('cannot get connection, no mongo db created, call init'); | ||
return null; | ||
} | ||
|
||
return this.db.collection(name); | ||
} | ||
|
||
|
||
|
||
disconnect() { | ||
if (this.db) { | ||
this.db.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Collection } from 'mongodb'; | ||
|
||
export default class User { | ||
constructor(public collection: Collection) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
let Promise = require('bluebird'); | ||
|
||
/** | ||
* Turn a callback function into a promise function. | ||
*/ | ||
export default function make(context: any, method: Function, args?: any[]) { | ||
if (!Array.isArray(args)) { | ||
args = [args]; | ||
} | ||
|
||
return new Promise((resolve: EventListener, reject: EventListener) => { | ||
method.apply(context, args.concat([(err: Event, result: any) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(result); | ||
}])); | ||
}); | ||
} | ||
|
||
export function map(context: any, method: Function, items: any[]) { | ||
return Promise.all(items.map(item => { | ||
return method.call(context, item); | ||
})); | ||
} |