This commit is contained in:
Francesco Esposito 2019-08-22 17:31:05 +02:00
parent 06a9e3dec5
commit 44435cf18d
1 changed files with 71 additions and 0 deletions

71
src/db/index.js Normal file
View File

@ -0,0 +1,71 @@
import mongoose from 'mongoose';
import UserModel from './user.model';
import logger from '../logger';
import config from '../config';
const options = {
useNewUrlParser: true,
user: config.database.user,
pass: config.database.password,
};
mongoose.connect(config.database.host, options);
mongoose.set('useFindAndModify', false);
const db = mongoose.connection;
db.on('error', (err) => logger.error(err));
db.once('open', () => logger.info('Database connected'));
export const initUser = (id) => (
new Promise((resolve, reject) => {
UserModel.findOneAndUpdate({ id }, { id }, { upsert: true }, (err, doc) => {
if (err) {
logger.error(err);
reject(err);
}
resolve(doc);
});
})
);
export const setCity = (userId, cityId) => {
const document = {
city: {
id: cityId,
},
};
return new Promise((resolve, reject) => {
UserModel.findOneAndUpdate({ id: userId }, document, { new: true }, (err, doc) => {
if (err) {
logger.error(err);
reject(err);
}
resolve(doc);
});
});
};
export const findAssociedCity = (id) => (
new Promise((resolve, reject) => {
UserModel.findOne({ id }, (err, doc) => {
if (err) {
logger.error(err);
reject(err);
}
resolve(doc.city.id);
});
})
);
export { UserModel };