@openveo/api
Advanced tools
Comparing version 1.1.2 to 2.0.0
@@ -0,1 +1,14 @@ | ||
# 2.0.0 / 2016-02-19 | ||
- Add support for Arrays in require('@openveo/api').util.merge function | ||
- Add Database close method | ||
- Dissociate add and get on the logger. "get" method was used to both create and get a logger. Two methods are now available "add" and "get". Thus get method no longer create a new logger, use add instead | ||
- Add translations API. Available through require('@openveo/api').i18n | ||
- Use real unique String ids when adding new entities | ||
- Correct bug when recursively creating directory using require('@openveo/api').fileSystem.mkdir with concurrent calls | ||
- require('@openveo/api').fileSystem.copy can now copy both files and directories, not just files | ||
- Update MongoDB database interface relative to MongoDB driver 2.0, Be careful Database methods may not return the same arguments | ||
- Change the prototype Database.removeProp method to add a filter argument | ||
- Correct bug when an error occured during search ("callback method called twice") | ||
# 1.1.2 / 2015-11-25 | ||
@@ -2,0 +15,0 @@ |
@@ -16,2 +16,3 @@ 'use strict'; | ||
module.exports.fileSystem = process.requireAPI('lib/fileSystem.js'); | ||
module.exports.i18n = process.requireAPI('lib/i18n.js'); | ||
module.exports.util = process.requireAPI('lib/util.js'); | ||
@@ -18,0 +19,0 @@ module.exports.logger = process.requireAPI('lib/logger.js'); |
@@ -73,4 +73,3 @@ 'use strict'; | ||
* @async | ||
* @param {Function} callback The function to call when connection | ||
* to the database is done | ||
* @param {Function} callback The function to call when connection to the database is established | ||
* - **Error** The error if an error occurred, null otherwise | ||
@@ -83,10 +82,24 @@ */ | ||
/** | ||
* Inserts a document into a collection. | ||
* Closes connection to the database. | ||
* | ||
* @method close | ||
* @async | ||
* @param {Function} callback The function to call when connection is closed | ||
* - **Error** The error if an error occurred, null otherwise | ||
*/ | ||
Database.prototype.close = function() { | ||
throw new Error('close method not implemented for this database'); | ||
}; | ||
/** | ||
* Inserts several documents into a collection. | ||
* | ||
* @method insert | ||
* @async | ||
* @param {String} collection The collection to work on | ||
* @param {Objet} data The document to insert into the collection | ||
* @param {Array} data Document(s) to insert into the collection | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Number** The total amount of documents inserted | ||
* - **Array** All the documents inserted | ||
*/ | ||
@@ -98,3 +111,3 @@ Database.prototype.insert = function() { | ||
/** | ||
* Removes a document from a collection. | ||
* Removes several documents from a collection. | ||
* | ||
@@ -104,5 +117,6 @@ * @method remove | ||
* @param {String} collection The collection to work on | ||
* @param {Object} criteria MongoDB criterias | ||
* @param {Object} filter Filters formatted like MongoDB filters | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Number** The number of deleted documents | ||
*/ | ||
@@ -119,5 +133,7 @@ Database.prototype.remove = function() { | ||
* @param {String} collection The collection to work on | ||
* @param {String} prop The property name to remove | ||
* @param {String} property The property name to remove | ||
* @param {Object} filter Filters formatted like MongoDB filters | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Number** The number of updated documents | ||
*/ | ||
@@ -129,3 +145,3 @@ Database.prototype.removeProp = function() { | ||
/** | ||
* Updates a document. | ||
* Updates several documents from collection. | ||
* | ||
@@ -135,6 +151,7 @@ * @method update | ||
* @param {String} collection The collection to work on | ||
* @param {Object} criteria MongoDB criterias | ||
* @param {Object} data The document | ||
* @param {Object} filter Filters formatted like MongoDB filters | ||
* @param {Object} data Document data | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Number** The number of updated documents | ||
*/ | ||
@@ -151,8 +168,8 @@ Database.prototype.update = function() { | ||
* @param {String} collection The collection to work on | ||
* @param {Object} criteria MongoDB criterias | ||
* @param {Object} projection MongoDB projection | ||
* @param {Number} limit An optional limit number of items to retrieve | ||
* @param {Object} [criteria] MongoDB criterias | ||
* @param {Object} [projection] MongoDB projection | ||
* @param {Number} [limit] A limit number of items to retrieve (all by default) | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Array** The list of documents | ||
* - **Array** The retrieved documents | ||
*/ | ||
@@ -162,1 +179,21 @@ Database.prototype.get = function() { | ||
}; | ||
/** | ||
* Gets an ordered list of documents by page. | ||
* | ||
* @method search | ||
* @async | ||
* @param {String} collection The collection to work on | ||
* @param {Object} [criteria] MongoDB criterias | ||
* @param {Object} [projection] MongoDB projection | ||
* @param {Number} [limit] The maximum number of expected documents | ||
* @param {Number} [page] The expected page | ||
* @param {Object} [sort] A sort object | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Array** The list of documents | ||
* - **Object** Pagination information | ||
*/ | ||
Database.prototype.search = function() { | ||
throw new Error('search method not implemented for this database'); | ||
}; |
@@ -7,6 +7,6 @@ 'use strict'; | ||
// Module dependencies | ||
var util = require('util'); | ||
var mongodb = require('mongodb'); | ||
var Database = process.requireAPI('lib/Database.js'); | ||
var utilExt = process.requireAPI('lib/util.js'); | ||
var MongoClient = mongodb.MongoClient; | ||
@@ -76,11 +76,7 @@ | ||
// Connection failed | ||
if (error) | ||
callback(error); | ||
// Connection succeeded | ||
else { | ||
if (!error) | ||
self.db = db; | ||
callback(); | ||
} | ||
callback(error); | ||
}); | ||
@@ -91,18 +87,41 @@ | ||
/** | ||
* Inserts a document into a collection. | ||
* Closes connection to the database. | ||
* | ||
* @method close | ||
* @async | ||
* @param {Function} callback The function to call when connection is closed | ||
* - **Error** The error if an error occurred, null otherwise | ||
*/ | ||
MongoDatabase.prototype.close = function(callback) { | ||
this.db.close(callback); | ||
}; | ||
/** | ||
* Inserts several documents into a collection. | ||
* | ||
* @method insert | ||
* @async | ||
* @param {String} collection The collection to work on | ||
* @param {Objet} data The document to insert into the collection | ||
* @param {Array} data Document(s) to insert into the collection | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Number** The total amount of documents inserted | ||
* - **Array** All the documents inserted | ||
*/ | ||
MongoDatabase.prototype.insert = function(collection, data, callback) { | ||
collection = this.db.collection(collection); | ||
collection.insert(data, callback); | ||
this.db.collection(collection, function(error, fetchedCollection) { | ||
if (error) | ||
return callback(error); | ||
fetchedCollection.insertMany(data, function(error, result) { | ||
if (error) | ||
callback(error); | ||
else | ||
callback(null, result.insertedCount, result.ops); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Removes a document from a collection. | ||
* Removes several documents from a collection. | ||
* | ||
@@ -112,11 +131,19 @@ * @method remove | ||
* @param {String} collection The collection to work on | ||
* @param {Object} criteria The remove criteria | ||
* @param {Object} filter Filters formatted like MongoDB filters | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Number** The number of deleted documents | ||
*/ | ||
MongoDatabase.prototype.remove = function(collection, criteria, callback) { | ||
if (criteria && Object.keys(criteria).length) { | ||
collection = this.db.collection(collection); | ||
collection.remove(criteria, callback); | ||
} | ||
MongoDatabase.prototype.remove = function(collection, filter, callback) { | ||
this.db.collection(collection, function(error, fetchedCollection) { | ||
if (error) | ||
return callback(error); | ||
fetchedCollection.deleteMany(filter, function(error, result) { | ||
if (error) | ||
callback(error); | ||
else | ||
callback(null, result.deletedCount); | ||
}); | ||
}); | ||
}; | ||
@@ -130,29 +157,32 @@ | ||
* @param {String} collection The collection to work on | ||
* @param {String} prop The property name to remove | ||
* @param {String} property The property name to remove | ||
* @param {Object} filter Filters formatted like MongoDB filters | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Number** The number of updated documents | ||
*/ | ||
MongoDatabase.prototype.removeProp = function(collection, prop, callback) { | ||
var query = {}; | ||
query[prop] = {$exists: true}; | ||
query['locked'] = {$ne: true}; | ||
MongoDatabase.prototype.removeProp = function(collection, property, filter, callback) { | ||
var mongoFilter = {}; | ||
mongoFilter[property] = {$exists: true}; | ||
mongoFilter = utilExt.merge(mongoFilter, filter); | ||
var update = {}; | ||
update['$unset'] = {}; | ||
update['$unset'][prop] = ''; | ||
update['$unset'][property] = ''; | ||
collection = this.db.collection(collection); | ||
this.db.collection(collection, function(error, fetchedCollection) { | ||
if (error) | ||
return callback(error); | ||
collection.update( | ||
query, | ||
update, | ||
{ | ||
multi: true | ||
}, | ||
callback | ||
); | ||
fetchedCollection.updateMany(mongoFilter, update, function(error, result) { | ||
if (error) | ||
callback(error); | ||
else | ||
callback(null, result.modifiedCount); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Updates a document. | ||
* Updates several documents from collection. | ||
* | ||
@@ -162,20 +192,24 @@ * @method update | ||
* @param {String} collection The collection to work on | ||
* @param {Object} criteria MongoDB criterias | ||
* @param {Object} filter Filters formatted like MongoDB filters | ||
* @param {Object} data Document data | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Number** The number of updated documents | ||
*/ | ||
MongoDatabase.prototype.update = function(collection, criteria, data, callback) { | ||
collection = this.db.collection(collection); | ||
collection.update(criteria, | ||
{ | ||
$set: data | ||
}, | ||
{ | ||
multi: true | ||
}, | ||
callback); | ||
MongoDatabase.prototype.update = function(collection, filter, data, callback) { | ||
var update = {$set: data}; | ||
this.db.collection(collection, function(error, fetchedCollection) { | ||
if (error) | ||
return callback(error); | ||
fetchedCollection.updateMany(filter, update, function(error, result) { | ||
if (error) | ||
callback(error); | ||
else | ||
callback(null, result.modifiedCount); | ||
}); | ||
}); | ||
}; | ||
/** | ||
@@ -192,14 +226,18 @@ * Gets a list of documents. | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Array** The retrieved data | ||
* - **Array** The retrieved documents | ||
*/ | ||
MongoDatabase.prototype.get = function(collection, criteria, projection, limit, callback) { | ||
collection = this.db.collection(collection); | ||
this.db.collection(collection, function(error, fetchedCollection) { | ||
if (error) | ||
return callback(error); | ||
criteria = criteria || {}; | ||
projection = projection || {}; | ||
limit = limit || -1; | ||
if (limit === -1) | ||
collection.find(criteria, projection).toArray(callback); | ||
else | ||
collection.find(criteria, projection).limit(limit).toArray(callback); | ||
criteria = criteria || {}; | ||
projection = projection || {}; | ||
limit = limit || -1; | ||
if (limit === -1) | ||
fetchedCollection.find(criteria, projection).toArray(callback); | ||
else | ||
fetchedCollection.find(criteria, projection).limit(limit).toArray(callback); | ||
}); | ||
}; | ||
@@ -224,29 +262,33 @@ | ||
MongoDatabase.prototype.search = function(collection, criteria, projection, limit, page, sort, callback) { | ||
collection = this.db.collection(collection); | ||
this.db.collection(collection, function(error, fetchedCollection) { | ||
if (error) | ||
return callback(error); | ||
criteria = criteria || {}; | ||
projection = projection || {}; | ||
limit = limit || -1; | ||
sort = sort || {}; | ||
var skip = limit * (page - 1) || 0; | ||
criteria = criteria || {}; | ||
projection = projection || {}; | ||
limit = limit || -1; | ||
sort = sort || {}; | ||
var skip = limit * (page - 1) || 0; | ||
if (limit === -1) | ||
collection.find(criteria, projection).sort(sort).toArray(callback); | ||
else { | ||
var cursor = collection.find(criteria, projection).sort(sort).skip(skip).limit(limit); | ||
cursor.toArray(function(err, res) { | ||
if (err) callback(err, null, null); | ||
cursor.count(false, null, function(error, count) { | ||
if (error) callback(error, null, null); | ||
var paginate = { | ||
count: limit, | ||
page: page, | ||
pages: Math.ceil(count / limit), | ||
size: count | ||
}; | ||
var resultArray = res || []; | ||
callback(error, resultArray, paginate); | ||
if (limit === -1) | ||
fetchedCollection.find(criteria, projection).sort(sort).toArray(callback); | ||
else { | ||
var cursor = fetchedCollection.find(criteria, projection).sort(sort).skip(skip).limit(limit); | ||
cursor.toArray(function(err, res) { | ||
if (err) return callback(err, null, null); | ||
cursor.count(false, null, function(error, count) { | ||
if (error) callback(error, null, null); | ||
var paginate = { | ||
count: limit, | ||
page: page, | ||
pages: Math.ceil(count / limit), | ||
size: count | ||
}; | ||
var resultArray = res || []; | ||
callback(error, resultArray, paginate); | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
}); | ||
}; |
@@ -11,3 +11,2 @@ 'use strict'; | ||
// Module dependencies | ||
var fs = require('fs'); | ||
@@ -35,5 +34,12 @@ var path = require('path'); | ||
// Can't create directory, parent directory does not exist | ||
if (error && error.code === 'ENOENT') { | ||
if (error && error.code === 'EEXIST') { | ||
// Can't create directory it already exists | ||
// It may have been created by another loop | ||
callback(); | ||
} else if (error && error.code === 'ENOENT') { | ||
// Can't create directory, parent directory does not exist | ||
// Create parent directory | ||
@@ -45,11 +51,16 @@ mkdirRecursive(path.dirname(directoryPath), function(error) { | ||
fs.mkdir(directoryPath, function(error) { | ||
callback(error); | ||
if (error && error.code === 'EEXIST') { | ||
// Can't create directory it already exists | ||
// It may have been created by another loop | ||
callback(); | ||
} else | ||
callback(error); | ||
}); | ||
} | ||
else | ||
} else | ||
callback(error); | ||
}); | ||
} | ||
else | ||
} else | ||
callback(error); | ||
@@ -143,2 +154,51 @@ }); | ||
/** | ||
* Copies a file. | ||
* | ||
* If directory does not exist it will be automatically created. | ||
* | ||
* @method copyFile | ||
* @private | ||
* @async | ||
* @param {String} sourceFilePath Path of the file | ||
* @param {String} destinationFilePath Final path of the file | ||
* @param {Function} callback The function to call when done | ||
* - **Error** The error if an error occurred, null otherwise | ||
*/ | ||
function copyFile(sourceFilePath, destinationFilePath, callback) { | ||
var onError = function(error) { | ||
callback(error); | ||
}; | ||
var safecopy = function(sourceFilePath, destinationFilePath, callback) { | ||
if (sourceFilePath && destinationFilePath && callback) { | ||
try { | ||
var is = fs.createReadStream(sourceFilePath); | ||
var os = fs.createWriteStream(destinationFilePath); | ||
is.on('error', onError); | ||
os.on('error', onError); | ||
is.on('end', function() { | ||
os.end(); | ||
callback(); | ||
}); | ||
is.pipe(os); | ||
} catch (e) { | ||
callback(new Error(e.message)); | ||
} | ||
} else callback(new Error('File path not defined')); | ||
}; | ||
var pathDir = path.dirname(destinationFilePath); | ||
this.mkdir(pathDir, | ||
function(error) { | ||
if (error) callback(error); | ||
else safecopy(sourceFilePath, destinationFilePath, callback); | ||
} | ||
); | ||
} | ||
/** | ||
* Extracts a tar file to the given directory. | ||
@@ -202,48 +262,66 @@ * | ||
/** | ||
* Copies a file from one directory to another. | ||
* Copies a file or a directory. | ||
* | ||
* If directory does not exist it will be automatically created. | ||
* | ||
* @method copy | ||
* @async | ||
* @param {String} sourceFilePath Path of the file to move | ||
* @param {String} destinationFilePath Final path of the file | ||
* @param {String} sourcePath Path of the source to copy | ||
* @param {String} destinationSourcePath Final path of the source | ||
* @param {Function} callback The function to call when done | ||
* - **Error** The error if an error occurred, null otherwise | ||
*/ | ||
module.exports.copy = function(sourceFilePath, destinationFilePath, callback) { | ||
module.exports.copy = function(sourcePath, destinationSourcePath, callback) { | ||
var self = this; | ||
var onError = function(error) { | ||
callback(error); | ||
}; | ||
// Get source stats to test if this is a directory or a file | ||
fs.stat(sourcePath, function(error, stats) { | ||
if (error) | ||
return callback(error); | ||
var safecopy = function(sourceFilePath, destinationFilePath, callback) { | ||
if (sourceFilePath && destinationFilePath && callback) { | ||
try { | ||
var is = fs.createReadStream(sourceFilePath); | ||
var os = fs.createWriteStream(destinationFilePath); | ||
if (stats.isDirectory()) { | ||
is.on('error', onError); | ||
os.on('error', onError); | ||
// Resource is a directory | ||
is.on('end', function() { | ||
os.end(); | ||
callback(); | ||
// Open directory | ||
fs.readdir(sourcePath, function(error, resources) { | ||
// Failed reading directory | ||
if (error) | ||
return callback(error); | ||
var pendingResourceNumber = resources.length; | ||
// Directory is empty, create it and leave | ||
if (!pendingResourceNumber) { | ||
self.mkdir(destinationSourcePath, callback); | ||
return; | ||
} | ||
// Iterate through the list of resources in the directory | ||
resources.forEach(function(resource) { | ||
var resourcePath = path.join(sourcePath, resource); | ||
var resourceDestinationPath = path.join(destinationSourcePath, resource); | ||
// Copy resource | ||
self.copy(resourcePath, resourceDestinationPath, function(error) { | ||
if (error) | ||
return callback(error); | ||
pendingResourceNumber--; | ||
if (!pendingResourceNumber) | ||
callback(); | ||
}); | ||
}); | ||
is.pipe(os); | ||
} catch (e) { | ||
callback(new Error(e.message)); | ||
} | ||
} else callback(new Error('File path not defined')); | ||
}; | ||
}); | ||
var pathDir = path.dirname(destinationFilePath); | ||
} else { | ||
this.mkdir(pathDir, | ||
function(error) { | ||
if (error) callback(error); | ||
else safecopy(sourceFilePath, destinationFilePath, callback); | ||
// Resource is a file | ||
copyFile.call(self, sourcePath, destinationSourcePath, callback); | ||
} | ||
); | ||
}); | ||
}; | ||
@@ -250,0 +328,0 @@ |
'use strict'; | ||
/** | ||
* Provides functions to manage loggers. | ||
* | ||
* @module logger | ||
* @class logger | ||
* @main logger | ||
*/ | ||
/** | ||
* Provides functions to manage loggers. | ||
* | ||
* @module logger | ||
* @class logger | ||
* @main logger | ||
*/ | ||
// Module dependencies | ||
var winston = require('winston'); | ||
/** | ||
* Gets a new logger by its name or initializes one. | ||
* Adds a new logger. | ||
* | ||
* Added loggers will also log to process standard output in development mode (not in production). | ||
* | ||
* @example | ||
@@ -28,3 +29,4 @@ * var loggerAPI = require('@openveo/api').logger; | ||
* // Initializes logger "openveo" | ||
* var logger = loggerAPI.get('openveo', conf); | ||
* loggerAPI.add('openveo', conf); | ||
* var logger = loggerAPI.get('openveo'); | ||
* | ||
@@ -34,9 +36,3 @@ * // Log something | ||
* | ||
* @example | ||
* var loggerAPI = require('@openveo/api').logger; | ||
* | ||
* // Retrieve logger "openveo" which have already been initialized | ||
* var logger = loggerAPI.get('openveo'); | ||
* | ||
* @method get | ||
* @method add | ||
* @param {String} name The name of the logger | ||
@@ -52,4 +48,4 @@ * @param {Object} [conf] Logger configuration to initialize a new logger | ||
*/ | ||
module.exports.get = function(name, conf) { | ||
if (conf) { | ||
module.exports.add = function(name, conf) { | ||
if (!winston.loggers.loggers[name] && conf) { | ||
@@ -69,2 +65,3 @@ // Create logger | ||
winston.loggers.get(name).remove(winston.transports.Console); | ||
} | ||
@@ -74,1 +71,21 @@ | ||
}; | ||
/** | ||
* Gets a logger. | ||
* | ||
* @example | ||
* var loggerAPI = require('@openveo/api').logger; | ||
* | ||
* // Get openveo logger | ||
* var logger = loggerAPI.get('openveo'); | ||
* | ||
* // Log something | ||
* logger.info('A simple log'); | ||
* | ||
* @method get | ||
* @param {String} name The name of the logger | ||
* @return {Object} A winston logger | ||
*/ | ||
module.exports.get = function(name) { | ||
return winston.loggers.get(name); | ||
}; |
@@ -7,2 +7,4 @@ 'use strict'; | ||
var shortid = require('shortid'); | ||
/** | ||
@@ -99,4 +101,8 @@ * Defines class EntityModel. | ||
* | ||
* @param {type} callback | ||
* @returns {undefined} | ||
* @method getByFilter | ||
* @async | ||
* @param {Object} filter A MongoDB filter | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Object** The entity | ||
*/ | ||
@@ -108,11 +114,15 @@ EntityModel.prototype.getByFilter = function(filter, callback) { | ||
/** | ||
* Gets a paginated list of filtered entities | ||
* Gets an ordered list of entities by page. | ||
* | ||
* @param {type} filter | ||
* @param {type} count | ||
* @param {type} page | ||
* @param {type} sort | ||
* @param {boolean} populate | ||
* @param {type} callback | ||
* @returns {undefined} | ||
* @method getPaginatedFilteredEntities | ||
* @async | ||
* @param {Object} [filter] MongoDB filter | ||
* @param {Number} [limit] The maximum number of expected entities | ||
* @param {Number} [page] The expected page | ||
* @param {Object} [sort] A sort object | ||
* @param {Boolean} [populate] true to automatically populate results with additional information | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Array** The list of entities | ||
* - **Object** Pagination information | ||
*/ | ||
@@ -135,6 +145,6 @@ EntityModel.prototype.getPaginatedFilteredEntities = function(filter, count, page, sort, populate, callback) { | ||
EntityModel.prototype.add = function(data, callback) { | ||
data.id = String(Date.now()); | ||
this.provider.add(data, function(error) { | ||
data.id = shortid.generate(); | ||
this.provider.add(data, function(error, insertCount, documents) { | ||
if (callback) | ||
callback(error, data); | ||
callback(error, documents[0]); | ||
}); | ||
@@ -159,13 +169,13 @@ }; | ||
/** | ||
* Removes an entity. | ||
* Removes one or several entities. | ||
* | ||
* @method remove | ||
* @async | ||
* @param {String} id The id of the entity to remove | ||
* @param {String|Array} ids Id(s) of the document(s) to remove from the collection | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Number** The number of removed items | ||
* - **Number** The number of deleted entities | ||
*/ | ||
EntityModel.prototype.remove = function(id, callback) { | ||
this.provider.remove(id, callback); | ||
EntityModel.prototype.remove = function(ids, callback) { | ||
this.provider.remove(ids, callback); | ||
}; |
@@ -120,19 +120,14 @@ 'use strict'; | ||
/** | ||
* * | ||
* @param {type} options | ||
* Gets an ordered list of entities by page. | ||
* | ||
* sort is a collection of key to sort with the order value (-1 : desc, 1 asc) | ||
* example ( {"name":-1, age:"1"} specifies a descending sort by the name field and then an ascending sort by | ||
* the age field | ||
* | ||
* filter is a collection of filter | ||
* example {"name": {$regex : ".*sam.*}, "age": {$lt:20}} specifies all document witch the name field contains | ||
* "sam" aged less than 20 | ||
* | ||
* @param {type} filter | ||
* @param {type} count | ||
* @param {type} page | ||
* @param {type} sort | ||
* @param {type} callback | ||
* @returns {undefined} | ||
* @method getPaginatedFilteredEntities | ||
* @async | ||
* @param {Object} [filter] MongoDB filter | ||
* @param {Number} [limit] The maximum number of expected entities | ||
* @param {Number} [page] The expected page | ||
* @param {Object} [sort] A sort object | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Array** The list of entities | ||
* - **Object** Pagination information | ||
*/ | ||
@@ -143,3 +138,2 @@ EntityProvider.prototype.getPaginatedFilteredEntities = function(filter, count, page, sort, callback) { | ||
/** | ||
@@ -167,10 +161,14 @@ * Gets all entities. | ||
* @param {Object} data Data to store into the collection, its structure depends on the entity type | ||
* @param {Function} callback The function to call when it's done | ||
* @param {Function} [callback] The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Number** The total amount of documents inserted | ||
* - **Array** All the documents inserted | ||
*/ | ||
EntityProvider.prototype.add = function(data, callback) { | ||
this.database.insert(this.collection, data, callback || function() { | ||
var datas = Array.isArray(data) ? data : [data]; | ||
// TODO Log the error if any | ||
this.database.insert(this.collection, datas, callback || function(error, result) { | ||
if (error) | ||
process.logger.error('Error while inserting entities with message : ' + | ||
error.message, datas); | ||
}); | ||
@@ -193,12 +191,10 @@ }; | ||
EntityProvider.prototype.update = function(id, data, callback) { | ||
this.database.update(this.collection, { | ||
id: id, | ||
locked: { | ||
$ne: true | ||
} | ||
}, | ||
data, callback || function() { | ||
var filter = {}; | ||
filter['locked'] = {$ne: true}; | ||
filter['id'] = id; | ||
// TODO Log the error if any | ||
this.database.update(this.collection, filter, data, callback || function(error, result) { | ||
if (error) | ||
process.logger.error('Error while updating entities message : ' + | ||
error.message, data); | ||
}); | ||
@@ -208,3 +204,3 @@ }; | ||
/** | ||
* Removes an entity. | ||
* Removes one or several entities. | ||
* | ||
@@ -215,20 +211,16 @@ * If the entity has the property "locked", it won't be removed. | ||
* @async | ||
* @param {String} id The id of the entity to remove | ||
* @param {String|Array} ids Id(s) of the document(s) to remove from the collection | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Number** The number of removed items | ||
* - **Number** The number of deleted entities | ||
*/ | ||
EntityProvider.prototype.remove = function(id, callback) { | ||
this.database.remove(this.collection, { | ||
id: { | ||
$in: id | ||
}, | ||
locked: { | ||
$ne: true | ||
} | ||
}, | ||
callback || function() { | ||
EntityProvider.prototype.remove = function(ids, callback) { | ||
var filter = {}; | ||
filter['locked'] = {$ne: true}; | ||
filter['id'] = {$in: null}; | ||
filter['id']['$in'] = (Array.isArray(ids)) ? ids : [ids]; | ||
// TODO Log the error if any | ||
this.database.remove(this.collection, filter, callback || function(error, result) { | ||
if (error) | ||
process.logger.error('Error while removing entities with message : ' + error.message, ids); | ||
}); | ||
@@ -240,16 +232,20 @@ }; | ||
* | ||
* If the entity has the property "locked", it won't be updated. | ||
* | ||
* @method removeProp | ||
* @async | ||
* @param {String} prop The property name to remove | ||
* @param {String} property The property name to remove | ||
* @param {Function} callback The function to call when it's done | ||
* - **Error** The error if an error occurred, null otherwise | ||
* - **Number** The number of removed items | ||
* - **Number** The number of modified entities | ||
*/ | ||
EntityProvider.prototype.removeProp = function(prop, callback) { | ||
this.database.removeProp( | ||
this.collection, | ||
prop, | ||
callback || function() { | ||
// TODO Log the error if any | ||
}); | ||
EntityProvider.prototype.removeProp = function(property, callback) { | ||
var filter = {}; | ||
filter['locked'] = {$ne: true}; | ||
this.database.removeProp(this.collection, property, filter, callback || function(error, result) { | ||
if (error) | ||
process.logger.error('Error while removing property from entities(s) with message : ' + | ||
error.message, property); | ||
}); | ||
}; |
'use strict'; | ||
var util = require('util'); | ||
/** | ||
@@ -34,3 +36,3 @@ * Provides functions for common JavaScript operations. | ||
// Recusively merge its properties | ||
if (typeof object2[property] === 'object') { | ||
if (typeof object2[property] === 'object' && !util.isArray(object2[property])) { | ||
object1[property] = object1[property] || {}; | ||
@@ -37,0 +39,0 @@ object1[property] = this.merge(object1[property], object2[property]); |
{ | ||
"name": "@openveo/api", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "API for OpenVeo plugins", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -19,3 +19,3 @@ # OpenVeo API | ||
Documentation is available on [Github pages](http://veo-labs.github.io/openveo-api/1.1.2). | ||
Documentation is available on [Github pages](http://veo-labs.github.io/openveo-api/2.0.0). | ||
@@ -22,0 +22,0 @@ # Contributors |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92841
19
1654
7