Repository: goldibex/targaryen
Pull request #98 (2016-12-09 13:33:24)

Move database
move "database.js", "store.js", "results.js" and "ruleset.js" into "database/". It leave in "lib/", only util scripts and the "parser" and "database" packages.


Patch:
diff --git a/README.md b/README.md
index 8973143..e98b64d 100644
--- a/README.md
+++ b/README.md
@@ -139,6 +139,16 @@ Targaryen statically analyzes your security rules using [esprima](http://esprima
     - `newDatabase`: the resulting database;
     - `newValue`: the value written to the database.
 
+- `targaryen.store(data: object|DataNode, options: {now: number|null, path: string|null, priority: string|number|null}): DataNode`
+
+    Can be used to create the database root ahead of time and check its validity.
+
+    The `path` option defines the relative path of the data from the root; e.g. `targaryen.store(1, {path: 'foo/bar/baz'})` is equivalent to `targaryen.store({foo: {bar: {baz: 1}}})`.
+
+- `targaryen.store(rules: object|Ruleset): Ruleset`
+
+    Can be used to create the database rule set ahead of time and check its validity.
+
 
 ## Why is it named Targaryen?
 
diff --git a/index.js b/index.js
index f5cc7b2..5602041 100644
--- a/index.js
+++ b/index.js
@@ -7,6 +7,8 @@ const database = require('./lib/database');
 
 exports.util = helpers;
 exports.database = database.create;
+exports.store = database.store;
+exports.ruleset = database.ruleset;
 
 // Deprecate direct access to plugin helpers
 Object.defineProperties(exports, [
diff --git a/lib/database.js b/lib/database/index.js
similarity index 97%
rename from lib/database.js
rename to lib/database/index.js
index 98917c3..0a58696 100644
--- a/lib/database.js
+++ b/lib/database/index.js
@@ -6,7 +6,7 @@
 
 'use strict';
 
-const paths = require('./paths');
+const paths = require('../paths');
 const store = require('./store');
 const ruleset = require('./ruleset');
 const results = require('./results');
@@ -380,5 +380,13 @@ exports.snapshot = function(path, value, now) {
   return db
     .with({data: db.root.$set(path, value, undefined, now), now})
     .snapshot();
+};
 
+// aliases
+exports.store = store.create;
+exports.ruleset = ruleset.create;
+exports.results = {
+  read: results.read,
+  write: results.write,
+  update: results.update
 };
diff --git a/lib/results.js b/lib/database/results.js
similarity index 99%
rename from lib/results.js
rename to lib/database/results.js
index 80a97e6..451494b 100644
--- a/lib/results.js
+++ b/lib/database/results.js
@@ -4,7 +4,7 @@
 
 'use strict';
 
-const pad = require('./pad');
+const pad = require('../pad');
 
 /**
  * Hold an evaluation result.
diff --git a/lib/ruleset.js b/lib/database/ruleset.js
similarity index 99%
rename from lib/ruleset.js
rename to lib/database/ruleset.js
index cd5f87f..67521db 100644
--- a/lib/ruleset.js
+++ b/lib/database/ruleset.js
@@ -4,8 +4,8 @@
 
 'use strict';
 
-const parser = require('./parser');
-const paths = require('./paths');
+const parser = require('../parser');
+const paths = require('../paths');
 const results = require('./results');
 
 /**
diff --git a/lib/store.js b/lib/database/store.js
similarity index 99%
rename from lib/store.js
rename to lib/database/store.js
index c3c1baa..57db948 100644
--- a/lib/store.js
+++ b/lib/database/store.js
@@ -4,7 +4,7 @@
 
 'use strict';
 
-const paths = require('./paths');
+const paths = require('../paths');
 const primitiveTypes = new Set(['string', 'number', 'boolean']);
 const priorityTypes = new Set(['string', 'number']);
 const invalidChar = ['.', '$', '[', ']', '#', '/'];
diff --git a/lib/util.js b/lib/util.js
index ab6c3c1..a2bd59b 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -4,8 +4,6 @@
 
 'use strict';
 
-const ruleset = require('./ruleset');
-const store = require('./store');
 const database = require('./database');
 const pad = require('./pad');
 
@@ -157,7 +155,7 @@ exports.setFirebaseData = function(value, now) {
   now = now || Date.now();
 
   try {
-    value = store.create(value, {now});
+    value = database.store(value, {now});
   } catch (e) {
     db = data = undefined;
     throw new Error('Proposed Firebase data is not valid: ' + e.message);
@@ -181,7 +179,7 @@ exports.getFirebaseData = function() {
 exports.setFirebaseRules = function(ruleDefinition) {
 
   try {
-    rules = ruleset.create(ruleDefinition);
+    rules = database.ruleset(ruleDefinition);
   } catch (e) {
     throw new Error('Proposed Firebase rules are not valid: ' + e.message);
   }


Test patch:
diff --git a/test/spec/lib/database.js b/test/spec/lib/database/index.js
similarity index 99%
rename from test/spec/lib/database.js
rename to test/spec/lib/database/index.js
index ba23896..237141b 100644
--- a/test/spec/lib/database.js
+++ b/test/spec/lib/database/index.js
@@ -11,7 +11,7 @@
 
 'use strict';
 
-const database = require('../../../lib/database');
+const database = require('../../../../lib/database');
 
 const TS = Object.freeze({'.sv': 'timestamp'});
 
diff --git a/test/spec/lib/ruleset.js b/test/spec/lib/database/ruleset.js
similarity index 99%
rename from test/spec/lib/ruleset.js
rename to test/spec/lib/database/ruleset.js
index 419c1fd..7717a13 100644
--- a/test/spec/lib/ruleset.js
+++ b/test/spec/lib/database/ruleset.js
@@ -8,7 +8,7 @@
 
 'use strict';
 
-const ruleset = require('../../../lib/ruleset');
+const ruleset = require('../../../../lib/database/ruleset');
 
 const invalidRulesets = {
   'are null': null,
diff --git a/test/spec/lib/store.js b/test/spec/lib/database/store.js
similarity index 99%
rename from test/spec/lib/store.js
rename to test/spec/lib/database/store.js
index 8250e08..0864029 100644
--- a/test/spec/lib/store.js
+++ b/test/spec/lib/database/store.js
@@ -4,7 +4,7 @@
 
 'use strict';
 
-const store = require('../../../lib/store');
+const store = require('../../../../lib/database/store');
 
 describe('store', function() {
   const _now = Date.now;
diff --git a/test/spec/lib/util.js b/test/spec/lib/util.js
index 3b40f8b..04cd5e4 100644
--- a/test/spec/lib/util.js
+++ b/test/spec/lib/util.js
@@ -2,7 +2,7 @@
 'use strict';
 
 const util = require('../../../lib/util');
-const results = require('../../../lib/results');
+const database = require('../../../lib/database');
 
 describe('util', function() {
 
@@ -53,31 +53,31 @@ describe('util', function() {
     });
 
     it('should describe unauthenticated users', function() {
-      result = results.read(path, data);
+      result = database.results.read(path, data);
       expect(util.readableError(result)).to.contain('unauthenticated user');
     });
 
     it('should describe anonymous users', function() {
-      result = results.read(path, data.as(util.users.anonymous));
+      result = database.results.read(path, data.as(util.users.anonymous));
       expect(util.readableError(result)).to.contain('user authenticated anonymously');
     });
 
     it('should describe password users', function() {
-      result = results.read(path, data.as(util.users.password));
+      result = database.results.read(path, data.as(util.users.password));
       expect(util.readableError(result)).to.contain('user authenticated via Password Login');
     });
 
     it('should describe auth', function() {
       const auth = {uid: 123};
 
-      result = results.read(path, data.as(auth));
+      result = database.results.read(path, data.as(auth));
       expect(util.readableError(result)).to.contain(JSON.stringify(auth));
     });
 
     it('should describe auth with $description', function() {
       const $description = 'some description';
 
-      result = results.read(path, data.as({$description}));
+      result = database.results.read(path, data.as({$description}));
       expect(util.readableError(result)).to.contain($description);
     });
 
@@ -85,7 +85,7 @@ describe('util', function() {
       const auth = util.users[provider.toLowerCase()];
 
       it('should describe Facebook users', function() {
-        result = results.read(path, data.as(auth));
+        result = database.results.read(path, data.as(auth));
         expect(util.readableError(result)).to.contain(`user authenticated via ${provider}`);
       });
 
@@ -104,7 +104,7 @@ describe('util', function() {
       beforeEach(function() {
         util.setFirebaseRules({rules: {}});
         util.setFirebaseData(null);
-        result = results.read(path, util.getFirebaseData().as({$description}));
+        result = database.results.read(path, util.getFirebaseData().as({$description}));
       });
 
       it('should include the path', function() {
@@ -144,7 +144,7 @@ describe('util', function() {
 
         const data = util.getFirebaseData().as({$description});
 
-        result = results.write(path, data, data, value);
+        result = database.results.write(path, data, data, value);
       });
 
       it('should include the path', function() {