fs.js

Copyright(c) 2013 Stefano Balietti NDDB file systems routines

Overrids save and load methods available in browser, with methods that

use the Node.JS fs api.

require('../external/cycle.js');

var fs = require('fs'),
csv = require('ya-csv');

var J = require('JSUS').JSUS;

var NDDB = module.parent.exports.NDDB;

NDDB.storageAvailable

Dummy function that returns always true.

Overrides default storageAvailable method for the browser.

Returns
boolean true
NDDB.prototype.storageAvailable = function() {
    return true;
}

NDDB.save

Saves the database to a persistent medium in JSON format

Saves to the file system using the standard fs.writeFile method.

Cyclic objects are decycled, and do not cause errors. Upon loading, the cycles are restored.

Params
file string The file system path, or the identifier for the browser database
callback function Optional. A callback to execute after the database is saved
boolean compress Optional. If TRUE, output will be compressed. Defaults, FALSE
Returns
boolean TRUE, if operation is successful
See
NDDB.load
See
NDDB.stringify
See
NDDB.prototype.save = function(file, callback, compress) {
    if ('string' !== typeof file) {
        throw new TypeError(this._getConstrName() +
                            '.save: you must specify a valid file name.');
    }
    compress = compress || false;

Save in Node.js

    fs.writeFileSync(file, this.stringify(compress), 'utf-8');
    if (callback) callback();
    return true;

};

NDDB.load

Loads a JSON object into the database from a persistent medium

Loads from the file system using fs.readFileSync or fs.readFile methods.

Cyclic objects previously decycled will be retrocycled.

Params
file string The file system path, or the identifier for the browser database
cb function Optional. A callback to execute after the database is loaded
Returns
boolean TRUE, if operation is successful
See
NDDB.loadCSV
See
NDDB.save
See
NDDB.stringify
See
JSUS.parse
See
NDDB.prototype.load = function(file, cb, options) {
    var loadCb, loadedString;
    if ('string' !== typeof file) {
        throw new TypeError(this._getConstrName() + 
                            '.load: you must specify a valid file name.');
    }

    loadCb = function(s) {
        var items, i;
        items = J.parse(s);
        for (i = 0; i < items.length; i++) {

retrocycle if possible

            items[i] = NDDB.retrocycle(items[i]);
        }
        this.importDB(items);
    }

    loadedString = fs.readFileSync(file, 'utf-8');
    loadCb.call(this, loadedString);
    return true;
};

NDDB.loadCSV

Loads the content of a csv file into the database

Uses ya-csv to load the csv file

Params
file string The path to the file to load,
cb function Optional. A callback to execute after the database is loaded
options object Optional. A configuration object to pass to ya-csv.createCsvFileReader
Returns
boolean TRUE, if operation is successful
See
NDDB.load
See
NDDB.prototype.loadCSV = function(file, cb, options) {
    var reader, that;
    that = this;

Mix options

    options = options || {};

    if ('undefined' === typeof options.columnsFromHeader) {
        options.columnsFromHeader = true;
    }

    reader = csv.createCsvFileReader(file, options);

    if (options.columnNames) {
        reader.setColumnNames(options.columnNames);
    }

    reader.addListener('data', function(data) {
        that.insert(data);
    });

    reader.addListener('end', function(data) {
        if (cb) cb();
    });

    return true;
};