Skip to content

Commit

Permalink
remove mongorito
Browse files Browse the repository at this point in the history
  • Loading branch information
Signorini committed Sep 24, 2019
1 parent 593bc84 commit 46614d1
Show file tree
Hide file tree
Showing 17 changed files with 819 additions and 189 deletions.
5 changes: 3 additions & 2 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const express = require('express');
const kraken = require('kraken-js');

const db_connect = require('core/libs/db_run');
const Mongorito = require('mongorito');
const dbpath = require('core/libs/dbpath')();
const dbname = require('core/libs/dbname')();
const Connector = require('core/repositories/daos/connector/connector');

/*
* Create and configure application. Also exports application instance for use by tests.
Expand All @@ -21,7 +22,7 @@ const options = {
*/

db_connect(function *() {
yield Mongorito.connect(dbpath);
yield Connector.connect(dbpath, dbname);
next(null, config);
console.log("Maestro: Mongo online");
});
Expand Down
6 changes: 6 additions & 0 deletions app/core/libs/dbname.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';


module.exports = () => {
return process.env.MAESTRO_MONGO_DATABASE || 'maestro-client';
};
7 changes: 2 additions & 5 deletions app/core/libs/dbpath.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict';


module.exports = () => {
const uri = process.env.MAESTRO_MONGO_URI || 'mongodb://localhost';
const db = process.env.MAESTRO_MONGO_DATABASE || 'maestro-audit';
return `${uri}/${db}`;
};
return process.env.MAESTRO_MONGO_URI || 'mongodb://localhost';
};
2 changes: 1 addition & 1 deletion app/core/libs/in_maker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const _ = require('lodash');
const {ObjectId} = require('mongorito');
const {ObjectId} = require('mongodb');
const NotFoundError = require('core/errors/factoryError')('NotFoundError');


Expand Down
186 changes: 180 additions & 6 deletions app/core/repositories/daos/DBConnector.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,149 @@
'use strict';

const _ = require('lodash');
const {Model} = require('mongorito');

const Promise = require('bluebird');
const Connector = require('./connector/connector');
const Model = require('./connector/model');

class Dao extends Model {

_db() {
let db = this.db ? this.db() : Connector._connection;
return Promise.resolve(db);
}

_collection() {
return this._db().then((db) => {
if (_.isString(this.collection))
return Connector._collection(db, this.collection);

let defaultName = this.constructor.name.toLowerCase();
let name = _.result(this, 'collection', defaultName);

this.collection = this.constructor.prototype.collection = name;

return Connector._collection(db, this.collection);
});
}

get(key) {
let value = key ? _.get(this.attributes, key) : this.attributes;
return value && value.constructor === Object ? _.clone(value) : value;
}

set(key, value) {
// if object passed instead of key-value pair
// iterate and call set on each item
if (_.isObject(key)) {
let attrs = key;

Object.keys(attrs).forEach((k) => {
this.set(k, attrs[k]);
});

return;
}

// check if the value actually changed
let previousValue = this.get(key);

if (previousValue !== value) {
_.set(this.previous, key, previousValue);
_.set(this.attributes, key, value);
_.set(this.changed, key, true);
}

return value;
}

save(options) {
let id = this.get('_id');
let fn = id ? this.update : this.create;

if (!options)
options = {};

let populate = this.options.populate || {};

Object.keys(populate).forEach((key) => {
let value = this.get(key);

if (Array.isArray(value)) {
value = value.map((doc) => doc.get('_id'));
} else {
value = value.get('_id');
}

this.set(key, value);
});

return fn.call(this, options);
}

create() {
let attrs = this.attributes;
let date = new Date();

this.set({
'created_at': date,
'updated_at': date
});

return this._collection()
.then((collection) => collection.insertOne(attrs))
.then((inserted) => {
let doc = inserted.ops[0];
this.set('_id', doc._id);
})
.return(this);
}


update() {
let attrs = this.attributes;

this.set('updated_at', new Date());

return this._collection()
.then((collection) => collection.updateOne({ _id: attrs._id }, attrs))
.return(this);
}

inc(props) {
let id = this.get('_id');

if (!id)
throw new Error('Can\'t atomically increment a property of unsaved document.');

return this._collection()
.then( (collection) => collection.updateOne({ _id: id }, { '$inc': props }))
.then(() => {
Object.keys(props).forEach((key) => {
let value = this.get(key);
value += props[key];
this.set(key, value);
});
})
.return(this);
}

updateFull(filter, options) {
const opts = _.get(options, 'oUpdater', '');
const opp = `update${opts}Factory`;
this.set('updated_at', new Date());

return this[opp](filter, null, options);
let entity = filter;
let entry = null;

return this._collection()
.then((collection) => {
const subs = entry ? {[entry]: this.get()} : this.get();
return collection.replaceOne(entity, subs, options);
})
.then((e) => {
return this.isUpdater = e.result;
})
.return(this);
}

updateAndModify(filter, options) {
Expand All @@ -22,22 +154,64 @@ class Dao extends Model {
return this[opp](filter, '$set', options);
}

updateByPushUnique(filter, options) {
const opts = _.get(options, 'oUpdater', '');
const opp = `update${opts}Factory`;

return this[opp](filter, '$addToSet', options);
}

updateByPull(filter, options) {
const opts = _.get(options, 'oUpdater', '');
const opp = `update${opts}Factory`;

return this[opp](filter, '$pull', options);
}

incrementBY(filter, options) {
const {oUpdater} = options;
const opp = `update${oUpdater||''}Factory`;

return this[opp](filter, '$inc', options);
}

updateFactory(entity, entry, options) {

return this._collection()
.tap(() => {
return this._runHooks('before', 'update');
.then((collection) => {
const subs = entry ? {[entry]: this.get()} : this.get();
return collection.updateOne(entity, subs, options);
})
.then((e) => {
return this.isUpdater = e.result;
})
.return(this);
}

updateManyFactory(entity, entry, options) {

return this._collection()
.then((collection) => {
const subs = entry ? {[entry]: this.get()} : this.get();
return collection.update(entity, subs, options);
return collection.updateMany(entity, subs, options);
})
.then((e) => {
return this.isUpdater = e.result;
})
.return(this);
}

updateBatch(data, options) {

return this._collection()
.then((collection) => {
return collection.bulkWrite(data, options);
})
.then((e) => {
return this.isBatch = e;
})
.return(this);
}
}

module.exports = Dao;
93 changes: 93 additions & 0 deletions app/core/repositories/daos/connector/connector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'use strict';

const _ = require('lodash');
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;

/**
* Connector
*
* Main class, manages mongodb connection and collections
*/

class Connector {}

Connector.connect = function (path, dbname, options = {}) {

this.url = path;
this.dbname = path;

const strOpts = {
useUnifiedTopology: true,
useNewUrlParser: true
};

let connection = MongoClient.connect(path, strOpts, options)
.then((client) => {
if (!this.db)
this.db = client.db(dbname);

return this.db;
})
.catch(console.log);

if (!this._connection)
this._connection = connection;

return connection;
};


/**
* Disconnect from a database
*
* @api public
*/

Connector.disconnect = function () {
return this.db.close();
};


/**
* Alias for .disconnect()
*
* @api public
*/

Connector.close = function () {
return Connector.disconnect.apply(this, arguments);
};


/**
* Return a co-wrapped monk collection
*
* @api private
*/

Connector._collection = function (db, name) {
const url = this.url;
let collections = this._collections[url];

if (!collections)
collections = this._collections[url] = {};

if (collections[name])
return collections[name];

let collection = db.collection(name);
collections[name] = collection;
return collections[name];
};


/**
* Cache for collections
*
* @api private
*/

Connector._collections = {};

module.exports = Connector;
17 changes: 17 additions & 0 deletions app/core/repositories/daos/connector/libs/to-objectid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const {ObjectId} = require('mongodb');

/**
* Ensure that ids are always instances of ObjectId
*/

function toObjectId (id) {
if (id instanceof ObjectId) {
return id;
}

return new ObjectId(id);
}

module.exports = toObjectId;
Loading

0 comments on commit 46614d1

Please sign in to comment.