Skip to content

Commit

Permalink
adding database infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehenrty committed May 17, 2017
1 parent 0010c37 commit 795d293
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
19 changes: 19 additions & 0 deletions server/src/lib/db.ts
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);
});
}
}
34 changes: 34 additions & 0 deletions server/src/lib/db/base.ts
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);
}
}
62 changes: 62 additions & 0 deletions server/src/lib/db/connection.ts
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();
}
}
}
6 changes: 6 additions & 0 deletions server/src/lib/db/user.ts
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) {
}
}
26 changes: 26 additions & 0 deletions server/src/promisify.ts
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);
}));
}

0 comments on commit 795d293

Please sign in to comment.