-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix some bugs, update README.md, add context
- Loading branch information
1 parent
7e85522
commit 2d22512
Showing
7 changed files
with
137 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules/ | ||
npm-debug.log | ||
test* | ||
src/model_class.js | ||
src/model_class.js | ||
.test.context.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Jatabase | ||
* | ||
* @author Gabriel Jacinto <gamjj74@hotmail.com> | ||
* @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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters