Skip to content

Commit

Permalink
Uninstall mock-require
Browse files Browse the repository at this point in the history
  • Loading branch information
XVincentX authored and DrMegavolt committed Nov 15, 2017
1 parent ff7c212 commit 7def2c9
Show file tree
Hide file tree
Showing 39 changed files with 635 additions and 621 deletions.
13 changes: 13 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ jobs:
- run: npm install
- run: npm test

"node-8-real-redis":
docker:
- image: circleci/node:8
- image: redis:alpine
working_directory: ~/repo
environment:
- EG_DB_EMULATE: 0
steps:
- checkout
- run: npm install
- run: npm test

deploy:
docker:
- image: circleci/node:8
Expand All @@ -46,6 +58,7 @@ workflows:
jobs:
- node-6
- node-8
- node-8-real-redis
- deploy:
filters:
tags:
Expand Down
67 changes: 17 additions & 50 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,24 @@
const logger = require('./logger').db;
const redisCommands = require('redis-commands');
require('util.promisify/shim')(); // NOTE: shim for native node 8.0 uril.promisify
const util = require('util');
const fs = require('fs');
let db;
const config = require('./config');
const redisOptions = config.systemConfig.db && config.systemConfig.db.redis;

module.exports = function () {
if (db) {
return db;
}
const config = require('./config');
const redisOptions = config.systemConfig.db && config.systemConfig.db.redis;
// special mode, will emulate all redis commands.
// designed for demo and test scenarious to avoid having real Redis instance
let emulate = process.argv[2] === 'emulate' || redisOptions.emulate;

// special mode, will emulate all redis commands.
// designed for demo and test scenarious to avoid having real Redis instance
const emulate = process.argv[2] === 'emulate' || redisOptions.emulate;
if (process.env.EG_DB_EMULATE) {
emulate = !!parseInt(process.env.EG_DB_EMULATE);
}

const redis = require(emulate ? 'fakeredis' : 'redis');
promisify(redis.RedisClient.prototype, redisCommands.list);
promisify(redis.Multi.prototype, ['exec', 'execAtomic']);
const Redis = require(emulate ? 'ioredis-mock' : 'ioredis');
const db = new Redis(redisOptions);

function promisify (obj, methods) {
methods.forEach((method) => {
if (obj[method]) {
obj[method + 'Async'] = util.promisify(obj[method]);
}
});
}
db.on('ready', function () {
logger.debug('Redis is ready');
});

// TLS for redis, allowing for TLS options to be specified as file paths.
if (redisOptions.tls) {
if (redisOptions.tls.keyFile) {
redisOptions.tls.key = fs.readFileSync(redisOptions.tls.keyFile);
}
db.on('error', function (err) {
logger.error('Error in Redis: ', err);
});

if (redisOptions.tls.certFile) {
redisOptions.tls.cert = fs.readFileSync(redisOptions.tls.certFile);
}

if (redisOptions.tls.caFile) {
redisOptions.tls.ca = fs.readFileSync(redisOptions.tls.caFile);
}
}

db = redis.createClient(redisOptions);

db.on('ready', function () {
logger.debug('Redis is ready');
});

db.on('error', function (err) {
logger.error('Error in Redis: ', err);
});

return db;
};
module.exports = db;
15 changes: 7 additions & 8 deletions lib/services/authorization-codes/authorization-code.dao.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const db = require('../../db')();
const isEmpty = require('lodash.isempty');
const db = require('../../db');
const config = require('../../config');

const dao = {};
Expand All @@ -10,13 +9,13 @@ const authCodeNamespace = 'auth-code';
dao.save = function (code) {
// key for the code hash table
const redisCodeKey = config.systemConfig.db.redis.namespace.concat('-', authCodeNamespace).concat(':', code.id);
return db.hmsetAsync(redisCodeKey, code);
return db.hmset(redisCodeKey, code);
};

dao.find = function (criteria) {
return db.hgetallAsync(config.systemConfig.db.redis.namespace.concat('-', authCodeNamespace).concat(':', criteria.id))
return db.hgetall(config.systemConfig.db.redis.namespace.concat('-', authCodeNamespace).concat(':', criteria.id))
.then((code) => {
if (!code) {
if (isEmpty(code)) {
return null;
}
code.expiresAt = parseInt(code.expiresAt);
Expand All @@ -31,11 +30,11 @@ dao.find = function (criteria) {
};

dao.get = function (id) {
return db.hgetallAsync(config.systemConfig.db.redis.namespace.concat('-', authCodeNamespace).concat(':', id));
return db.hgetall(config.systemConfig.db.redis.namespace.concat('-', authCodeNamespace).concat(':', id));
};

dao.remove = function (id) {
return db.delAsync(config.systemConfig.db.redis.namespace.concat('-', authCodeNamespace).concat(':', id));
return db.del(config.systemConfig.db.redis.namespace.concat('-', authCodeNamespace).concat(':', id));
};

module.exports = dao;
28 changes: 14 additions & 14 deletions lib/services/consumers/application.dao.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const db = require('../../db')();
const db = require('../../db');
const config = require('../../config');

const dao = {};
Expand Down Expand Up @@ -47,7 +45,7 @@ dao.update = function (id, props) {
const hashKey = appHashKey(id);

return db
.hmsetAsync(hashKey, props)
.hmset(appHashKey, props)
.then(function (res) {
return !!res;
});
Expand All @@ -56,11 +54,11 @@ dao.update = function (id, props) {
dao.findAll = function (query) {
const key = appHashKey('');
const startFrom = query.start || 0;
return db.scanAsync(startFrom, 'MATCH', key + '*', 'COUNT', '100').then(resp => {
return db.scan(startFrom, 'MATCH', key + '*', 'COUNT', '100').then(resp => {
const nextKey = resp[0];
const appKeys = resp[1];
if (!appKeys || appKeys.length === 0) return Promise.resolve({ apps: [], nextKey: 0 });
const promises = appKeys.map(key => db.hgetallAsync(key));
const promises = appKeys.map(key => db.hgetall(key));
return Promise.all(promises).then(apps => {
return {
apps,
Expand All @@ -86,7 +84,7 @@ dao.find = function (appName) {
};

dao.get = function (id) {
return db.hgetallAsync(appHashKey(id))
return db.hgetall(appHashKey(id))
.then(function (app) {
if (!app || !Object.keys(app).length) {
return false;
Expand All @@ -103,15 +101,15 @@ dao.getAll = function (userId) {
};

dao.getAllAppIdsByUser = function (userId) {
return db.smembersAsync(userAppsHashKey(userId));
return db.smembers(userAppsHashKey(userId));
};

dao.activate = function (id) {
return db.hmsetAsync(appHashKey(id), ['isActive', 'true', 'updatedAt', String(new Date())]);
return db.hmset(appHashKey(id), ['isActive', 'true', 'updatedAt', String(new Date())]);
};

dao.deactivate = function (id) {
return db.hmsetAsync(appHashKey(id), ['isActive', 'false', 'updatedAt', String(new Date())]);
return db.hmset(appHashKey(id), ['isActive', 'false', 'updatedAt', String(new Date())]);
};

dao.deactivateAll = function (userId) {
Expand All @@ -128,15 +126,17 @@ dao.remove = function ({ name, id, userId }) {
.del(appHashKey(id))
.srem(userAppsHashKey(userId), id)
.srem(appNameSetKey(name), id)
.execAsync()
.exec()
.then(responses => responses.every(res => res));
};

dao.removeAll = function (userId) {
return this.getAllAppIdsByUser(userId)
.then(appIds => Promise.all(appIds.map(appId => this.get(appId))))
.then(apps => Promise.all(apps.map(app => this.remove(app, userId))))
.then(responses => responses.every(res => res));
.then(appIds => {
const removeAppPromises = appIds.map(appId => this.remove(appId, userId));
return Promise.all(removeAppPromises)
.then(responses => responses.every(res => res));
});
};

module.exports = dao;
25 changes: 12 additions & 13 deletions lib/services/consumers/user.dao.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const db = require('../../db')();
const isEmpty = require('lodash.isempty');
const db = require('../../db');
const config = require('../../config');

const dao = {};
Expand All @@ -18,14 +17,14 @@ dao.insert = function (user) {
.multi()
.hmset(redisUserKey, user)
.sadd(redisUsernameSetKey, user.id)
.execAsync()
.exec()
.then(res => res.every(val => val));
};

dao.getUserById = function (userId) {
return db.hgetallAsync(config.systemConfig.db.redis.namespace.concat('-', userNamespace).concat(':', userId))
return db.hgetall(config.systemConfig.db.redis.namespace.concat('-', userNamespace).concat(':', userId))
.then(function (user) {
if (!user || !Object.keys(user).length) {
if (!user || isEmpty(user)) {
return false;
}
return user;
Expand All @@ -35,11 +34,11 @@ dao.getUserById = function (userId) {
dao.findAll = function (query) {
const startFrom = query.start || 0;
const key = config.systemConfig.db.redis.namespace.concat('-', userNamespace).concat(':');
return db.scanAsync(startFrom, 'MATCH', key + '*', 'COUNT', '100').then(resp => {
return db.scan(startFrom, 'MATCH', key + '*', 'COUNT', '100').then(resp => {
const nextKey = resp[0];
const userKeys = resp[1];
if (!userKeys || userKeys.length === 0) return Promise.resolve({ users: [], nextKey: 0 });
const promises = userKeys.map(key => db.hgetallAsync(key));
const promises = userKeys.map(key => db.hgetall(key));
return Promise.all(promises).then(users => {
return {
users,
Expand All @@ -50,7 +49,7 @@ dao.findAll = function (query) {
};

dao.find = function (username) {
return db.smembersAsync(config.systemConfig.db.redis.namespace.concat('-', usernameNamespace).concat(':', username))
return db.smembers(config.systemConfig.db.redis.namespace.concat('-', usernameNamespace).concat(':', username))
.then(function (Ids) {
if (Ids && Ids.length !== 0) {
return Ids[0];
Expand All @@ -62,16 +61,16 @@ dao.update = function (userId, props) {
// key for the user in redis
const redisUserKey = config.systemConfig.db.redis.namespace.concat('-', userNamespace).concat(':', userId);
return db
.hmsetAsync(redisUserKey, props)
.hmset(redisUserKey, props)
.then(res => !!res);
};

dao.activate = function (id) {
return db.hmsetAsync(config.systemConfig.db.redis.namespace.concat('-', userNamespace).concat(':', id), ['isActive', 'true', 'updatedAt', String(new Date())]);
return db.hmset(config.systemConfig.db.redis.namespace.concat('-', userNamespace).concat(':', id), ['isActive', 'true', 'updatedAt', String(new Date())]);
};

dao.deactivate = function (id) {
return db.hmsetAsync(config.systemConfig.db.redis.namespace.concat('-', userNamespace).concat(':', id), ['isActive', 'false', 'updatedAt', String(new Date())]);
return db.hmset(config.systemConfig.db.redis.namespace.concat('-', userNamespace).concat(':', id), ['isActive', 'false', 'updatedAt', String(new Date())]);
};

dao.remove = function (userId) {
Expand All @@ -84,7 +83,7 @@ dao.remove = function (userId) {
.multi()
.del(config.systemConfig.db.redis.namespace.concat('-', userNamespace).concat(':', userId))
.srem(config.systemConfig.db.redis.namespace.concat('-', usernameNamespace).concat(':', user.username), userId)
.execAsync()
.exec()
.then(replies => replies.every(res => res));
});
};
Expand Down
Loading

0 comments on commit 7def2c9

Please sign in to comment.