folderdb v0.1.1
folderdb
folderdb allows you to store files with searchable key value attributes.
This makes it easier to keep track of files across applications as well as within an application.
Requirements
folderdb uses Mongo to store attributes
Install
npm install folderdb
Setup
var folderdb = require('folderdb')
// set up configuration
var options = {
name: 'myFolder',
path: './myFolder', // Location to store files
mongo: {
connectionString: 'mongodb://localhost:27017/test',
collectionPrefix: 'myfolder'
}
}
// init a local folder for storing files
var myFolder = folderdb.LocalFolder(options)
API
store(filePath, attributes, fn)
Moves the file to folder location and stores any attributes to mongo. Returns the file's ObjectId.
myFolder.store('path/to/file', {attribute1: value, attribute2: value}, fn(fileId) {})
replace(id, filePath, fn)
Replaces an existing file. Updates file name and extension from new file.
myFolder.replace(fileId, 'path/to/new/file', fn(file) {})
update(id, attributes, fn)
Replaces all the attributes for a file.
myFolder.update(fileId, {attribute3: value, attribute4: value}, fn(file) {})
delete(id, fn)
Deletes the file.
myFolder.delete(fileId, fn() {})
find(id, fn)
Returns the file object.
myFolder.find(fileId, fn(file) {})
query(conditions, options, fn)
Returns a list of files that meet all conditions.
// Compare functions include: $gt, $gte, $lt, $lte
var conditions = {
attribute1: 'string',
attribute2: { $gt: 1 },
attribute3: { $lt: new Date() }
}
// If options are not supplied defaults are used
// var options = {
// skip: 0,
// limit: 100
// }
myFolder.query(conditions, options, fn(listOfFiles) {
// listOfFiles[0].attributes = {
// attribute1: 'string',
// attribute2: 2,
// attribute3: Thu Jan 1 2015 15:00:00 GMT-0800 (PST)
// }
})
read(id, readOptions, fn)
Returns the file's data and file object.
// Same options as fs.readFile
var readOptions = {
encoding: 'UTF-8',
flag: 'r'
}
myFolder.read(fileId, readOptions, fn(data, file) {})
attributes(fn)
Returns a list of used attributes.
myFolder.attributes(fn(listOfAttributes) {
// listOfAttributes = [
// 'attribute1',
// 'attribute2',
// 'attribute3',
// 'attribute4'
// ]
})
Notes
File Object
[
{ _id: 54cae43f9f5be0dd3ee02ae3,
folder: 'myFolder',
name: 'fileName.jpg',
attributes: {
attribute1: value,
attribute2: value
}
}
]
Attributes
Attributes can be defined one of two ways when saving or updating. However, they will be saved as an object and not as an array.
// valid
var attributes1 = [
{attribute1: value},
{attribute2: value},
]
myFolder.store('path/to/file', attributes1, function(fileId) {})
// also valid
var attributes2 = {
attribute1: value,
attribute2: value,
}
myFolder.store('path/to/file', attributes2, function(fileId) {})