From 2d22512c07038d577dceb9311deb35cc3c875df0 Mon Sep 17 00:00:00 2001 From: Gabriel Jacinto Date: Sun, 1 Nov 2015 15:55:22 -0200 Subject: [PATCH] Fix some bugs, update README.md, add context --- .gitignore | 3 +- README.md | 11 ++++-- src/{jsondb.js => _json.js} | 10 ++--- src/context.js | 78 +++++++++++++++++++++++++++++++++++++ src/jatabase.js | 32 ++++++++++++--- src/model.js | 11 +++--- src/operations/add.js | 12 +++++- 7 files changed, 137 insertions(+), 20 deletions(-) rename src/{jsondb.js => _json.js} (74%) create mode 100644 src/context.js diff --git a/.gitignore b/.gitignore index 9c35c89..fab443a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules/ npm-debug.log test* -src/model_class.js \ No newline at end of file +src/model_class.js +.test.context.json \ No newline at end of file diff --git a/README.md b/README.md index 92ac206..94ed1e8 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,16 @@ jatabase ========= -This is advisable when you need a local simple database without any security -- is a JSON file. +![](https://img.shields.io/badge/status-development-red.svg) ![](https://img.shields.io/npm/v/jatabase.svg) -**Status**: Development +This is advisable when you need a local simple database without any security -- is a JSON file. ## Installing ```console $ npm install jatabase --save ``` +![](https://nodei.co/npm/jatabase.png?downloads=true&downloadRank=true&stars=true) + ## Usage All the persistence methods use promises, so they have an async way of using and a sync way. @@ -210,4 +212,7 @@ productsModel.has(5); // Using another fields productsModel.has({name: 'Pear TV'}); -``` \ No newline at end of file +``` + +# License +[MIT License](https://github.com/gabrieljmj/jatabase/blob/dev/LICENSE.md) 2015 © Gabriel Jacinto. \ No newline at end of file diff --git a/src/jsondb.js b/src/_json.js similarity index 74% rename from src/jsondb.js rename to src/_json.js index b978d13..c44b93d 100644 --- a/src/jsondb.js +++ b/src/_json.js @@ -10,21 +10,21 @@ var fs = require('fs'); -var JsonDB = function (file) { +var _Json = function (file) { validateFile(file); this.file = file; }; -JsonDB.prototype.update = function (data) { - return fs.writeFile(this.file, JSON.stringify(data, null, ' '), 'utf8', function (err) { +_Json.prototype.update = function (data) { + return fs.writeFile(this.file, JSON.stringify(data), 'utf8', function (err) { if (err != null) { throw Error(err); } }); }; -JsonDB.prototype.updateKey = function (key, value) { +_Json.prototype.updateKey = function (key, value) { let data = require(this.file); data[key] = value; @@ -45,4 +45,4 @@ let validateFile = function (file) { }); }; -module.exports = JsonDB; \ No newline at end of file +module.exports = _Json; \ No newline at end of file diff --git a/src/context.js b/src/context.js new file mode 100644 index 0000000..e912043 --- /dev/null +++ b/src/context.js @@ -0,0 +1,78 @@ +/** + * Jatabase + * + * @author Gabriel Jacinto + * @license MIT License + * @package jatabase + */ + +'use strict'; + +var _json = require('./_json'), + fs = require('fs'); + +let Context = function (file) { + validateFile(file); + + this.file = file; + this._json = new _json(file); +}; + +/** + * Set the last insert ID of a collection + * + * @param {String} from + * @param {Integer} id + */ +Context.prototype.setLastInsertId = function (from, id) { + let contextFromCollection = getContextFromCollection(this, from, true); + contextFromCollection['last_insert_id'] = id; + + this._json.updateKey(from, contextFromCollection); +}; + +/** + * Return the last ID of a collection + * + * @param {String} from + * + * @return {Integer} + */ +Context.prototype.lastInsertId = function (from) { + let contextFromCollection = getContextFromCollection(this, from, true); + + return contextFromCollection.last_insert_id; +}; + +let getContextFromCollection = function (Context, from, create) { + let context = require(Context.file); + + if (typeof context[from] == 'undefined') { + if (!create) { + throw Error('Context for ' + from + ' not found.'); + } + + context[from] = {last_insert_id: null}; + } + + return context[from]; +}; + +/** + * Verify if the file exists and if is a JSON + * + * @param {String} file + */ +let validateFile = function (file) { + if (fileParts[fileParts.length - 1].toLowerCase() != 'json') { + throw Error('File must have .json extension.'); + } + + fs.stat(file, function(err, stat) { + if (err != null && err.code == 'ENOENT') { + throw Error('File does not exists: ' + file); + } + }); +}; + +module.exports = Context; \ No newline at end of file diff --git a/src/jatabase.js b/src/jatabase.js index 0d32667..b8e9792 100644 --- a/src/jatabase.js +++ b/src/jatabase.js @@ -8,9 +8,12 @@ 'use strict'; -var model = require('./model'); +var model = require('./model'), + fs = require('fs'), + path = require('path'), + Context = require('./context'); -var jatabase = function (file) { +let jatabase = function (file) { this.file = file; }; @@ -23,15 +26,34 @@ var jatabase = function (file) { * @return {Model} */ jatabase.prototype.createModel = function (collection, fields) { - var newModel = function (db, collection) { + let newModel = function (db, collection, _fields, context) { this.file = db; this.collection = collection; - this.fields = fields; + this.fields = _fields; + this.context = context; }; newModel.prototype = new model; - return new newModel(this.file, collection); + return new newModel(this.file, collection, fields, createContext(this.file)); +}; + +let createContext = function (file) { + let dir = path.dirname(file), + fileName = path.basename(file, '.json'), + contextFile = dir + '/.' + fileName + '.context.json'; + + fs.stat(contextFile, function(err, stat) { + if (err != null && err.code == 'ENOENT') { + fs.writeFile(contextFile, JSON.stringify({users: []}), function (err) { + if (err !== null) { + console.error(err); + } + }); + } + }); + + return new Context(contextFile); }; module.exports = jatabase; \ No newline at end of file diff --git a/src/model.js b/src/model.js index 0676bb3..433e056 100644 --- a/src/model.js +++ b/src/model.js @@ -9,7 +9,7 @@ 'use strict'; var fs = require('fs'), - JsonDB = require('./jsondb'), + _json = require('./_json'), utils = require('./utils'); var ModelValidator = function (model) { @@ -38,10 +38,11 @@ var operation = function (operation) { * @param {String} db * @param {String} collection */ -function Model (db, collection) { +function Model (db, collection, fields, context) { this.file = db; this.collection = collection; - this.fields; + this.fields = fields; + this.context = context; }; /** @@ -366,12 +367,12 @@ Model.prototype._validateFile = function () { }; Model.prototype._saveModifications = function (data) { - let json = new JsonDB(this.file); + let json = new _json(this.file); json.update(data); }; Model.prototype._saveModificationOnKey = function (key, data) { - let json = new JsonDB(this.file); + let json = new _json(this.file); json.updateKey(key, data); }; diff --git a/src/operations/add.js b/src/operations/add.js index 5978caa..beea053 100644 --- a/src/operations/add.js +++ b/src/operations/add.js @@ -15,7 +15,17 @@ module.exports = function (Model) { if (Model._validateFields(fields)) { let db = require(Model.file), collection = db[Model.collection], - id = collection.length ? collection[collection.length - 1].id + 1 : 1; + context = Model.context, + id; + + if (context.lastInsertId(Model.collection) == null) { + id = collection.length ? collection[collection.length - 1].id + 1 : 1; + } else { + id = collection.length ? context.lastInsertId(Model.collection) + 1 : 1; + } + + context.setLastInsertId(Model.collection, id); + collection.push(utils.object.merge({id: id}, fields)); Model._saveModificationOnKey(Model.collection, collection); }