Permalink
Browse files

first working version

  • Loading branch information...
1 parent 5b81410 commit 19dc2941e8c3abab793aaf584177205adb808c28 rezoner committed Oct 10, 2012
Showing with 487 additions and 36 deletions.
  1. +4 −0 .gitignore
  2. +11 −2 README.md
  3. +101 −10 memoria.js
  4. +198 −24 memoria.sublime-workspace
  5. +103 −0 npm-debug.log
  6. +29 −0 test-browser.html
  7. +41 −0 test-node.js
View
@@ -0,0 +1,4 @@
+*.sublime-workspace
+*.sublime-project
+/npm
+*.memoria
View
@@ -4,18 +4,27 @@ Memoria
Simple database for node.js
---------------------------
-It is not working yet.
+The library is in early development - some features are not implemented yet - but these are marked.
Memoria is designed especially for browser based games without much of relational data.
Everything happens in memory, but the data is saved to a file.
It is meant to be used with applications that doesn't share database with other instances.
### Create database:
-This creates a file named `test.memoria` to store the data.
+Looks for file `test.memoria` to restore data.
+If file doesn't exists it will be created.
var db = Memoria("test");
+It is possible to check if file was found using:
+
+ if(db.exists) {
+
+ /* some creational functions */
+
+ }
+
### Create table:
db("users", ["name", "age", "salary"]);
View
@@ -1,5 +1,11 @@
(function() {
+ var BROWSER = typeof window !== 'undefined';
+
+ if(!BROWSER) {
+ var fs = require('fs');
+ }
+
var _ = {
extend: function() {
@@ -39,36 +45,106 @@
}
return result
+ },
+
+ pack: function(s) {
+ var dict = {};
+ var data = (s + "").split("");
+ var out = [];
+ var currChar;
+ var phrase = data[0];
+ var code = 256;
+ for(var i = 1; i < data.length; i++) {
+ currChar = data[i];
+ if(dict[phrase + currChar] != null) {
+ phrase += currChar;
+ } else {
+ out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
+ dict[phrase + currChar] = code;
+ code++;
+ phrase = currChar;
+ }
+ }
+ out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
+ for(var i = 0; i < out.length; i++) {
+ out[i] = String.fromCharCode(out[i]);
+ }
+ return out.join("");
+ },
+
+ unpack: function(s) {
+ var dict = {};
+ var data = (s + "").split("");
+ var currChar = data[0];
+ var oldPhrase = currChar;
+ var out = [currChar];
+ var code = 256;
+ var phrase;
+ for(var i = 1; i < data.length; i++) {
+ var currCode = data[i].charCodeAt(0);
+ if(currCode < 256) {
+ phrase = data[i];
+ } else {
+ phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
+ }
+ out.push(phrase);
+ currChar = phrase.charAt(0);
+ dict[code] = oldPhrase + currChar;
+ code++;
+ oldPhrase = phrase;
+ }
+ return out.join("");
}
};
+
var databases = {};
- var Memoria = function(name) {
+ var Memoria = function(name, callback) {
if(!databases[name]) {
- databases[name] = new Database(name);
+ databases[name] = new Database(name, callback);
}
- return(function(database) {
+ var database = databases[name];
+
+ var wrapper = (function(database) {
return function(name, args) {
if(typeof args !== "undefined") {
return database.setupTable(name, args);
} else {
return database.table(name);
}
};
- })(databases[name]);
+ })(database);
+
+ wrapper.exists = database.exists;
+
+ return wrapper;
};
- Memoria.databses = databases;
+ Memoria.databases = databases;
+ Memoria.saveInterval = 1000;
- var Database = function(name) {
+ var Database = function(name, callback) {
this.tables = {};
+ var self = this;
+ this.filePath = name + ".memoria";
+
+ if(!BROWSER) {
+ if(this.exists = fs.existsSync(this.filePath)) {
+ this.open();
+ }
+
+ setInterval(function() {
+ self.save();
+ }, Memoria.saveInterval);
+ }
};
Database.prototype = {
+
table: function(name) {
return new Table(this, name);
},
@@ -82,6 +158,22 @@
keyToIndex: _.inverse(fields),
items: []
}
+ },
+
+ open: function() {
+ fs.readFile(this.filePath, function(err, data) {
+ if(err) {
+ throw err;
+ } else {
+ this.tables = JSON.parse(_.unpack(data));
+ }
+ });
+ },
+
+ save: function() {
+ fs.writeFile(this.filePath, _.pack(JSON.stringify(this.tables)), function(err) {
+ if(err) throw err;
+ });
}
};
@@ -188,11 +280,10 @@
});
- if(typeof window === 'undefined') {
- exports = Memoria;
- } else {
+ if(BROWSER) {
window.Memoria = Memoria;
+ } else {
+ module.exports = Memoria;
}
-
})();
Oops, something went wrong.

0 comments on commit 19dc294

Please sign in to comment.