Skip to content

Commit

Permalink
add more logs on internal apis (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecarriou committed Jan 11, 2020
1 parent 9e055a2 commit d89ec61
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 25 deletions.
45 changes: 35 additions & 10 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,11 +729,13 @@ DatabaseCollection.prototype.insert = function insert(document) {
$metamodel.prepareObject(obj, $metamodel.getModel(this.name));

exports.store[this.name][obj[$mson.ID]] = obj;
result.push(obj[$mson.ID]);

$log.documentInserted(obj[$mson.ID], this.name);

Component = $component.get(this.name);
if (Component) {
component = new Component(obj);
result.push(component.id());
} else {
if ($history.isEnabled() && this.name.indexOf('_') !== 0) {
$history.pushState({
Expand Down Expand Up @@ -787,7 +789,7 @@ DatabaseCollection.prototype.insert = function insert(document) {
* @param {Object} update update to make
* @param {Object} options
* {Boolean} upsert true if we create a document when no document is found by the query
* @returns {Number} Number of documents updated
* @returns {Array} array of id of updated documents
* @description Update documents into a collection
*
* @example
Expand All @@ -797,12 +799,13 @@ DatabaseCollection.prototype.insert = function insert(document) {
*/
DatabaseCollection.prototype.update = function update(query, update, options) {
var docs = this.find(query);
var updated = 0;
var result = [];
var i = 0;
var length = docs.length;
var attributeName = '';
var schema = $metamodel.getModel(this.name);
var type = '';
var createdDocumentId = [];

options = options || {};
if (typeof options.upsert === 'undefined') {
Expand All @@ -815,8 +818,10 @@ DatabaseCollection.prototype.update = function update(query, update, options) {
if (query[$mson.ID]) {
update[$mson.ID] = query[$mson.ID];
}
this.insert(update);
updated = updated + 1;
createdDocumentId = this.insert(update);
if (createdDocumentId.length === 1) {
result.push(createdDocumentId[0]);
}
}

for (i = 0; i < length; i++) {
Expand Down Expand Up @@ -867,7 +872,14 @@ DatabaseCollection.prototype.update = function update(query, update, options) {
}

docs[i][attributeName] = update[attributeName];
updated = updated + 1;

$log.documentUpdated(
docs[i][$mson.ID],
this.name,
attributeName,
update[attributeName]
);
result.push(docs[i][$mson.ID]);

if ($helper.isRuntime() && $helper.getRuntime().require('db')) {
$helper
Expand Down Expand Up @@ -924,7 +936,14 @@ DatabaseCollection.prototype.update = function update(query, update, options) {

docs[i][attributeName] = update[attributeName];

updated = updated + 1;
$log.documentUpdated(
docs[i][$mson.ID],
this.name,
attributeName,
update[attributeName]
);
result.push(docs[i][$mson.ID]);

if ($helper.isRuntime() && $helper.getRuntime().require('db')) {
$helper
.getRuntime()
Expand All @@ -942,7 +961,7 @@ DatabaseCollection.prototype.update = function update(query, update, options) {
}
}

return updated;
return result;
};

/**
Expand Down Expand Up @@ -983,6 +1002,9 @@ DatabaseCollection.prototype.remove = function remove(query) {

delete exports.store[this.name][id];

$log.documentRemoved(id, this.name);
result.push(id);

component = $component.get(id);
if (component) {
component.destroy();
Expand All @@ -996,7 +1018,6 @@ DatabaseCollection.prototype.remove = function remove(query) {
id: id
});
}
result.push(id);
}
}
}.bind(this)
Expand All @@ -1017,6 +1038,9 @@ DatabaseCollection.prototype.remove = function remove(query) {

delete exports.store[this.name][id];

$log.documentRemoved(id, this.name);
result.push(id);

component = $component.get(id);
if (component) {
component.destroy();
Expand All @@ -1030,7 +1054,6 @@ DatabaseCollection.prototype.remove = function remove(query) {
id: id
});
}
result.push(id);
}
}
}
Expand All @@ -1047,6 +1070,8 @@ DatabaseCollection.prototype.remove = function remove(query) {

delete exports.store[this.name][id];

$log.documentRemoved(id, this.name);

if (coreDb.indexOf(this.name) === -1) {
component = $component.get(id);
if (component) {
Expand Down
55 changes: 53 additions & 2 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -1199,10 +1199,61 @@ exports.historyDocumentUpdated = function historyDocumentRemoved(
fieldName +
"' of component of id '" +
id +
"' with value '" +
value +
"' (collection '" +
collectionName +
"') with value '" +
"')"
);
};

/**
* @method documentRemoved
* @param {String} id id of the document
* @param {String} collection name of the collection
* @description A document from a collection was removed
*/
exports.documentRemoved = function documentRemoved(id, collection) {
getLogger().debug(
"the document '" + id + "' was removed (collection '" + collection + "')"
);
};

/**
* @method documentInserted
* @param {String} id id of the document
* @param {String} collection name of the collection
* @description A document was inserted into a collection
*/
exports.documentInserted = function documentInserted(id, collection) {
getLogger().debug(
"the document '" + id + "' was inserted (collection '" + collection + "')"
);
};

/**
* @method documentUpdated
* @param {String} id id of the document
* @param {String} collection name of the collection
* @param {String} property property of the document
* @param {String} value new value of the property
* @description A document was updated
*/
exports.documentUpdated = function documentUpdated(
id,
collection,
property,
value
) {
getLogger().debug(
"the property '" +
property +
"' of the document '" +
id +
"' was updated with the value '" +
value +
"'"
"' (collection '" +
collection +
"')"
);
};
46 changes: 33 additions & 13 deletions test/module/db-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,13 @@ describe('System Runtime db component', () => {
});

it('can add a document', () => {
db._Runtime.insert({
const docAdded = db._Runtime.insert({
'_id': 'runtime1',
'version': '0.0.0'
});

expect(docAdded[0]).equal('runtime1');

const result = db._Runtime.find({
'_id': 'runtime1'
});
Expand All @@ -177,11 +180,14 @@ describe('System Runtime db component', () => {
});

it('can update a document', () => {
db._Runtime.update({
const updatedDoc = db._Runtime.update({
'_id': 'runtime1'
}, {
'version': '0.0.4'
});
'version': '0.0.4'
});

expect(updatedDoc[0]).equal('runtime1');

const result = db._Runtime.find({
'_id': 'runtime1'
})[0];
Expand All @@ -190,13 +196,18 @@ describe('System Runtime db component', () => {
});

it('can update many documents', () => {
db._Runtime.update([{
const updatedDoc = db._Runtime.update([{
'_id': 'runtime2'
}, {
'_id': 'runtime3'
}], {
'version': '0.0.4'
});
'version': '0.0.4'
});

expect(updatedDoc.length).equal(2);
expect(updatedDoc[0]).equal('runtime2');
expect(updatedDoc[1]).equal('runtime3');

const result = db._Runtime.find({
'_id': 'runtime3'
})[0];
Expand All @@ -208,10 +219,10 @@ describe('System Runtime db component', () => {
db._Runtime.update({
'_id': 'runtime5'
}, {
'version': '0.0.1'
}, {
'upsert': true
});
'version': '0.0.1'
}, {
'upsert': true
});
const result = db._Runtime.find({
'_id': 'runtime5'
});
Expand All @@ -225,9 +236,13 @@ describe('System Runtime db component', () => {
'_id': runtimeid,
'version': '0.0.0'
});
db._Runtime.remove({

const removedDocument = db._Runtime.remove({
'_id': runtimeid
});

expect(removedDocument[0]).equal(runtimeid);

const result = db._Runtime.find({
'_id': runtimeid
});
Expand All @@ -246,11 +261,16 @@ describe('System Runtime db component', () => {
'_id': runtimeid2,
'version': '0.0.0'
}]);
db._Runtime.remove([{
const removedDocument = db._Runtime.remove([{
'_id': runtimeid1
}, {
'_id': runtimeid2
}]);

expect(removedDocument.length).equal(2);
expect(removedDocument[0]).equal(runtimeid1);
expect(removedDocument[1]).equal(runtimeid2);

const result1 = db._Runtime.find({
'_id': runtimeid1
});
Expand Down

0 comments on commit d89ec61

Please sign in to comment.