move to npm module system

parent 82d5fc13
/**
* Created by danilaeremin on 17.06.16.
*/
'use strict';
const dbUtils = require('db-utils');
module.exports = function (AccessToken) {
return {
saveAccessToken : function(token, userId, scope, refreshToken) {
return dbUtils.dbCreateItem(AccessToken,{token: token, refreshToken: refreshToken, user: userId, scope: scope});
},
getAccessToken : function(token) {
return dbUtils.dbFindOne(AccessToken, {token: token});
},
removeAccessToken : function(token) {
return dbUtils.dbRemoveItem(AccessToken, {token: token});
},
removeAccessTokenByRefresh : function(token) {
return dbUtils.dbRemoveItem(AccessToken, {refreshToken: token});
}
};
}
......@@ -4,20 +4,18 @@
'use strict';
var dbUtils = require('./dbUtils');
const dbUtils = require('db-utils');
var Client = require('./models/mongoose').Client;
// RefreshToken
exports.saveClient = function(client, secret, redirect) {
return dbUtils.dbCreateItem(Client,{ client: client, secret: secret, redirect: redirect });
};
exports.getClient = function (client) {
return dbUtils.dbFindOne(Client, {client: client});
};
exports.removeClient = function (client) {
return dbUtils.dbRemoveItem(Client, {client: client});
};
module.exports = function (Client) {
return {
saveClient : function(client, secret, redirect) {
return dbUtils.dbCreateItem(Client,{ client: client, secret: secret, redirect: redirect });
},
getClient : function(client) {
return dbUtils.dbFindOne(Client, {client: client});
},
removeClient : function(client) {
return dbUtils.dbRemoveItem(Client, {client: client});
}
};
}
\ No newline at end of file
......@@ -4,18 +4,18 @@
'use strict';
var dbUtils = require('./dbUtils');
const dbUtils = require('db-utils');
var Code = require('./models/mongoose').Code;
exports.saveCode = function(code, userId, scope, clientId, redirect) {
return dbUtils.dbCreateItem(Code,{ code: code, user: userId, scope: scope, client: clientId, redirect:redirect });
};
exports.getCode = function (code) {
return dbUtils.dbFindOne(Code, {code: code}, [{ path: 'client', select: 'client' }, { path: 'user', select: 'username' }]);
};
exports.removeCode = function (code) {
return dbUtils.dbRemoveItem(Code, {code: code});
};
module.exports = function (Code) {
return {
saveCode : function(code, userId, scope, clientId, redirect) {
return dbUtils.dbCreateItem(Code,{ code: code, user: userId, scope: scope, client: clientId, redirect:redirect });
},
getCode : function (code) {
return dbUtils.dbFindOne(Code, {code: code}, [{ path: 'client', select: 'client' }, { path: 'user', select: 'username' }]);
},
removeCode : function (code) {
return dbUtils.dbRemoveItem(Code, {code: code});
}
};
}
......@@ -4,23 +4,21 @@
'use strict';
var dbUtils = require('./dbUtils');
var Course = require('./models/mongoose').Course;
exports.addCourse = function(owner, name, info, description) {
return dbUtils.dbCreateItem(Course,{ owner: owner, name: name, info: info, description: description});
};
exports.getCourseById = function (courseId) {
return dbUtils.dbFindOne(Course, {_id: courseId}, [{ path: 'owner' }]);
};
exports.getCourseByOwner= function (owner) {
return dbUtils.dbFind(Course, {owner: owner}, [{ path: 'owner' }]);
};
exports.removeCourseById = function (courseId) {
return dbUtils.dbRemoveItem(Course, {_id: courseId});
};
const dbUtils = require('db-utils');
module.exports = function (Course) {
return {
addCourse : function(owner, name, info, description) {
return dbUtils.dbCreateItem(Course,{ owner: owner, name: name, info: info, description: description});
},
getCourseById : function (courseId) {
return dbUtils.dbFindOne(Course, {_id: courseId}, [{ path: 'owner' }]);
},
getCourseByOwner : function (owner) {
return dbUtils.dbFind(Course, {owner: owner}, [{ path: 'owner' }]);
},
removeCourseById : function (courseId) {
return dbUtils.dbRemoveItem(Course, {_id: courseId});
}
};
}
/**
* Created by danilaeremin on 08.07.15.
*/
'use strict';
var importModule = function (path) {
var module = require(path);
for (var f in module) {
exports[f] = module[f];
}
};
importModule('./dbUtils');
importModule('./dbTokens');
importModule('./dbUser');
importModule('./dbCode');
importModule('./dbCourse');
importModule('./dbClient');
exports.mongoose = require('./models/mongoose').mongoose;
/**
* Created by danilaeremin on 17.06.16.
*/
'use strict';
const dbUtils = require('db-utils');
module.exports = function (RefreshToken) {
return {
saveRefreshToken : function(token, userId, scope, clientId) {
return dbUtils.dbCreateItem(RefreshToken,{ token: token, user: userId, scope: scope, client: clientId });
},
getRefreshToken : function(token) {
return dbUtils.dbFindOne(RefreshToken, {token: token}, [{ path: 'client', select: 'client' }, { path: 'user', select: 'username' }]);
},
removeRefreshToken : function(token) {
return dbUtils.dbRemoveItem(RefreshToken, {token: token});
}
};
}
\ No newline at end of file
/**
* Created by danilaeremin on 17.06.16.
*/
'use strict';
var dbUtils = require('./dbUtils');
var AccessToken = require('./models/mongoose').AccessToken;
var RefreshToken = require('./models/mongoose').RefreshToken;
// AccessToken
exports.saveAccessToken = function(token, userId, scope, refreshToken) {
return dbUtils.dbCreateItem(AccessToken,{token: token, refreshToken: refreshToken, user: userId, scope: scope});
};
exports.getAccessToken = function (token) {
return dbUtils.dbFindOne(AccessToken, {token: token});
};
exports.removeAccessToken = function (token) {
return dbUtils.dbRemoveItem(AccessToken, {token: token});
};
exports.removeAccessTokenByRefresh = function (token) {
return dbUtils.dbRemoveItem(AccessToken, {refreshToken: token});
};
// RefreshToken
exports.saveRefreshToken = function(token, userId, scope, clientId) {
return dbUtils.dbCreateItem(RefreshToken,{ token: token, user: userId, scope: scope, client: clientId });
};
exports.getRefreshToken = function (token) {
return dbUtils.dbFindOne(RefreshToken, {token: token}, [{ path: 'client', select: 'client' }, { path: 'user', select: 'username' }]);
};
exports.removeRefreshToken = function (token) {
return dbUtils.dbRemoveItem(RefreshToken, {token: token});
};
......@@ -4,95 +4,81 @@
'use strict';
var dbUtils = require('./dbUtils');
var User = require('./models/mongoose').User;
exports.getUsers = function () {
return dbUtils.dbFind(User, {});
};
exports.getUsersWithType = function (type) {
return dbUtils.dbFind(User, {accountType : type});
};
exports.getUserById = function (uId) {
return dbUtils.dbFindOne(User, {_id: uId});
};
exports.getUserByLogin = function (login) {
return dbUtils.dbFindOne(User, {login: login});
};
exports.getUserByIds = function (ids) {
return dbUtils.dbFind(User, { _id: { $in: ids }});
};
var createUser = function (user_object) {
return dbUtils.dbCreateItem(User, user_object);
};
exports.createUser = function (password, accountType, mail, name, surname, meta) {
var newUser = {
username : surname + " " + name[0] + ".",
login : mail,
password : password,
accountType : accountType,
mail : mail,
name : name,
surname : surname,
meta:meta,
activated : false
};
return createUser(newUser,callback);
};
exports.createSocialUser = function (socialId, accountType, mail, name, surname, meta) {
var newUser = {
username : surname + " " + name[0] + ".",
login : socialId,
password : ' ',
accountType : accountType,
mail : mail,
name : name,
surname : surname,
meta:meta,
activated : true,
isSocial : true
};
return createUser(newUser,callback);
};
exports.changeUserAccountType = function (userId, accountType) {
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {accountType : accountType});
};
exports.activateUser = function (userId) {
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {activated : true});
};
exports.blockUser = function (userId) {
return dbUtils.dbFindOneAndUpdate(User, {_id: userId}, {activated: false});
};
exports.updateUserLastLogin = function (userId) {
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {lastLogin : new Date()});
};
exports.updatePassword = function (userId, password) {
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {password : password});
};
exports.updateUserInfo = function (userId, mail, name, surname, username, login) {
var update = {
username : username,
mail : mail,
name : name,
surname : surname,
login : login
};
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, update);
};
\ No newline at end of file
const dbUtils = require('db-utils');
module.exports = function (User) {
return {
getUsers : function () {
return dbUtils.dbFind(User, {});
},
getUsersWithType : function (type) {
return dbUtils.dbFind(User, {accountType : type});
},
getUserById : function (uId) {
return dbUtils.dbFindOne(User, {_id: uId});
},
getUserByLogin : function (login) {
return dbUtils.dbFindOne(User, {login: login});
},
getUserByIds : function (ids) {
return dbUtils.dbFind(User, { _id: { $in: ids }});
},
createUser : function (password, accountType, mail, name, surname, meta) {
var newUser = {
username : surname + " " + name[0] + ".",
login : mail,
password : password,
accountType : accountType,
mail : mail,
name : name,
surname : surname,
meta:meta,
activated : false
};
return dbUtils.dbCreateItem(User, newUser);
},
createSocialUser : function (socialId, accountType, mail, name, surname, meta) {
var newUser = {
username : surname + " " + name[0] + ".",
login : socialId,
password : ' ',
accountType : accountType,
mail : mail,
name : name,
surname : surname,
meta:meta,
activated : true,
isSocial : true
};
return dbUtils.dbCreateItem(User, newUser);
},
changeUserAccountType : function (userId, accountType) {
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {accountType : accountType});
},
activateUser : function (userId) {
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {activated : true});
},
blockUser : function (userId) {
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {activated : false});
},
updateUserLastLogin : function (userId) {
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {lastLogin : new Date()});
},
updatePassword : function (userId, password) {
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, {password : password});
},
updateUserInfo : function (userId, mail, name, surname, username, login) {
var update = {
username : username,
mail : mail,
name : name,
surname : surname,
login : login
};
return dbUtils.dbFindOneAndUpdate(User, {_id : userId}, update);
}
}
}
/**
* Created by danilaeremin on 14.06.16.
*/
'use strict';
var Mongoose = require('./models/mongoose').mongoose;
exports.str2ObjectId = function (str) {
return new Mongoose.Types.ObjectId(str);
};
exports.dbFind = function (table, search, population) {
var find = table.find(search);
if(population) {
for (var i = 0; i < population.length; ++i) {
find = find.populate(population[i]);
}
}
return find.exec();
};
exports.dbFindOne = function (table, search, population) {
var find = table.findOne(search);
if(population) {
for (var i = 0; i < population.length; ++i) {
find = find.populate(population[i]);
}
}
return find.exec ();
};
exports.dbFindOneAndUpdate = function (table, search, update) {
return table.findOneAndUpdate(search, update, {new: true}).exec ();
};
exports.dbFindAndUpdate = function (table, search, update) {
return table.update(search, update, {multi: true}).exec ();
};
exports.dbCreateItem = function (table, data) {
var item = new table(data);
return item.save();
};
exports.dbRemoveItem = function (table, data) {
return table.remove(data).exec();
};
exports.dbSaveItem = function (item) {
return item.save();
};
exports.dbDuplicateAndUpdate = function (table, search, update) {
return exports.findOne (table, search)
.then((doc) => {
if (doc) {
doc = doc.data.toObject();
delete doc._id;
for (var u in update) {
if (update.hasOwnProperty(u)) {
res[u] = update[u];
}
}
return exports.dbCreateItem(table, res);
} else {
throw new Error('No document');
}
});
};
exports.dbAggregate = function (table, aggregate) {
return table.aggregate(aggregate).exec();
};
const dbUtils = require('db-utils');
let dbHelper = undefined;
function moduleExport (m) {
for (var f in m) {
if (m.hasOwnProperty(f)) {
dbHelper[f] = m[f];
}
}
}
module.exports = function (uri) {
if (dbHelper) {
return dbHelper;
}
dbHelper = {};
let connection = dbUtils.connect(uri);
let models = ['AccessToken','RefreshToken','User','Code','Client','Course'];
for (let i = 0; i < models.length; i++) {
let schema = require('./db/models/' + models[i]);
let model = connection.model(models[i], schema);
let helper = require('./db/db' + models[i])(model);
moduleExport(helper);
}
return dbHelper;
}
\ No newline at end of file
......@@ -33,6 +33,4 @@ var AccessTokenSchema = new mongoose.Schema({
}
});
var AccessToken = mongoose.model('AccessToken', AccessTokenSchema);
module.exports = AccessToken;
\ No newline at end of file
module.exports = AccessTokenSchema;
\ No newline at end of file
......@@ -27,6 +27,4 @@ var ClientSchema = new mongoose.Schema({
}
});
var Client = mongoose.model('Client', ClientSchema);
module.exports = Client;
\ No newline at end of file
module.exports = ClientSchema;
\ No newline at end of file
......@@ -37,6 +37,4 @@ var CodeSchema = new mongoose.Schema({
}
});
var Code = mongoose.model('Code', CodeSchema);
module.exports = Code;
\ No newline at end of file
module.exports = CodeSchema;
\ No newline at end of file
......@@ -34,6 +34,4 @@ var CourseSchema = new mongoose.Schema({
}
});
var Course = mongoose.model('Course', CourseSchema);
module.exports = Course;
\ No newline at end of file
module.exports = CourseSchema;
\ No newline at end of file
......@@ -32,6 +32,4 @@ var RefreshTokenSchema = new mongoose.Schema({
}
});
var RefreshToken = mongoose.model('RefreshToken', RefreshTokenSchema);
module.exports = RefreshToken;
\ No newline at end of file
module.exports = RefreshTokenSchema;
\ No newline at end of file
......@@ -129,6 +129,4 @@ UserSchema.methods.validPassword = isValidPassword;
UserSchema.pre('save', hashPassword);
UserSchema.pre('findOneAndUpdate', hashPasswordOnUpdate);
var User = mongoose.model('User', UserSchema);
module.exports = User;
\ No newline at end of file
module.exports = UserSchema;
\ No newline at end of file
'use strict';
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var config = global.config;
var log = global.log;
mongoose.connect(config.get('mirera-db'));
var db = mongoose.connection;
db.on('error', function (err) {
log.error('connection error:', err.message);
});
db.once('open', function callback () {
log.info("Connected to DB!");
});
module.exports = {
AccessToken: require('./AccessToken'),
RefreshToken: require('./RefreshToken'),
User: require('./User'),
Code: require('./Code'),
Client: require('./Client'),
Course: require('./Course'),
mongoose: mongoose
};