Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor public api #80

Merged
merged 22 commits into from
Nov 8, 2016
Merged

Refactor public api #80

merged 22 commits into from
Nov 8, 2016

Conversation

dinoboff
Copy link
Collaborator

@dinoboff dinoboff commented Nov 6, 2016

Add targaryen.database(rules, data, now)

This is the public facing API:

  • targaryen.database(rules: object|Ruleset, data: object|DataNode, now: null|number): Database;
  • Database.as(auth: null|object): Database;
  • Database.read(path: string, now: null|number): Result;
  • Database.write(path: string, value: any, priority: any, now: null|number): Result;
  • Database.update(path: string, patch: object, now: null|number): Result.

Example:

const assert = require('assert');
const targaryen = require('targaryen');

const rules = {
  rules: {
    foo: {
      '.write': 'true'
    }
  }
};
const data = {foo: 1};
const auth = {uid: 'someuid'};

const database = targaryen.database(rules, data);
const {allowed, newDatabase, info} = database.as(auth).write('/foo', 2);

console.log('Rule evaluations:\n', info);
assert.ok(allowed);

assert.equal(newDatabase.rules, database.rules);
assert.equal(newDatabase.root.foo.$value(), 2);
assert.equal(newDatabase.auth, auth);

targaryen also provides targaryen.util to help write testing framework plugin.

Result main properties are path, newValue, auth, database and newDatabase.

Low level constructors (Ruleset and DataSnapshot) or functions (like pathMerger) are not accessible directly anymore.

This the public facing API:

- `targaryen.create(rules: object|Ruleset, data: object|DataNode, now: null|number): Database`;
- `Database.as(auth: null|object): Database`;
- `Database.read(path: string, now: null|number): Result`;
- `Database.write(path: string, value: any, priority: any, now: null|number): Result`;
- `Database.write(path: string, patch: object, now: null|number): Result`;

targaryen also provides `targaryen.util` to help writing testing framework plugin.
Util doesn’t need to be access directly:

- it’s available as `targaryen.util`
- No circular dependencies; plugins can import access them via `targaryen.util`;
Also rename database common variable name from `data` to `db`.
@dinoboff dinoboff changed the base branch from master to next November 6, 2016 23:45
I added basic usage of the CLI and plugins; I don’t to give the impression the API is the main way to test rules.
@dinoboff dinoboff merged commit 8a7a2c2 into goldibex:next Nov 8, 2016
@dinoboff dinoboff deleted the refactor-public-api branch November 8, 2016 13:28
@dinoboff dinoboff added this to the 3.0 milestone Nov 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant