Skip to content
This repository was archived by the owner on Jul 16, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ node_js:
- stable
before_script:
- npm install -g gulp
script: "npm start"
script: "npm start"
after_script:
- cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
</p>

[![Build Status](https://travis-ci.org/davideast/Querybase.svg?branch=master)](https://travis-ci.org/davideast/Querybase)
[![Coverage Status](https://coveralls.io/repos/github/davideast/Querybase/badge.svg?branch=master)](https://coveralls.io/github/davideast/Querybase?branch=master)

## What is Querybase?

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"license": "Apache-2.0",
"devDependencies": {
"chai": "^3.4.1",
"coveralls": "^2.11.9",
"del": "^2.2.0",
"firebase-server": "^0.5.1",
"gulp": "^3.9.0",
Expand Down
41 changes: 23 additions & 18 deletions src/querybase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface QueryPredicate {
* Defines the utility methods used in Querybase
*/
interface QuerybaseUtils {
indexKey(): string;
isCommonJS(): boolean;
isString(value): boolean;
isObject(value): boolean;
Expand All @@ -30,6 +31,10 @@ interface QuerybaseUtils {

const _: QuerybaseUtils = {

indexKey(): string {
return '~~';
},

/**
* Detects whether it is a node enviroment
* @return {boolean}
Expand Down Expand Up @@ -72,7 +77,7 @@ const _: QuerybaseUtils = {
* @return {string}
*/
createKey(propOne, propTwo) {
return `${propOne}~~${propTwo}`;
return `${propOne}${_.indexKey()}${propTwo}`;
},

/**
Expand Down Expand Up @@ -334,8 +339,8 @@ class Querybase {

// for multiple criteria in the object,
// encode the keys and values provided
const criteriaIndex = this.encodeKey(keys.join('~~'));
const criteriaValues = this.encodeKey(values.join('~~'));
const criteriaIndex = this.encodeKey(keys.join(_.indexKey()));
const criteriaValues = this.encodeKey(values.join(_.indexKey()));

return {
predicate: criteriaIndex,
Expand Down Expand Up @@ -368,8 +373,8 @@ class Querybase {
* @return {FirebaseRef}
* @example
* // set of criteria
* const ref = new Firebase("<my-app>/people");
* const querybaseRef = new Querybase(ref, ['name', 'location', 'age']);
* const firebaseRef = firebase.database.ref.child('people');
* const querybaseRef = querybase.ref(firebaseRef, ['name', 'age', 'location']);
* querybaseRef.where({
* name: 'David',
* age: 27
Expand All @@ -395,7 +400,7 @@ class Querybase {
/**
* Creates a set of composite keys with composite data. Creates every
* possible combination of keys with respecive combined values. Redudant
* keys are not included ('name_age' vs. 'age_name').
* keys are not included ('name~~age' vs. 'age~~name').
* @param {any[]} indexes
* @param {Object} data
* @param {Object?} indexHash for recursive check
Expand All @@ -407,10 +412,10 @@ class Querybase {
*
* // compositeKeys
* {
* 'name_age': 'David_27',
* 'name_age_location': 'David_27_SF',
* 'name_location': 'David_SF',
* 'age_location': '27_SF'
* 'name~~age': 'David~~27',
* 'name~~age~~location': 'David~~27~~SF',
* 'name~~location': 'David~~SF',
* 'age~~location': '27~~SF'
* }
*/
private _createCompositeIndex(indexes: any[], data: Object, indexHash?: Object) {
Expand All @@ -430,7 +435,7 @@ class Querybase {
// create a copy of the array to not modifiy the original properties
const propCop = indexes.slice();
// remove the first property, this ensures no
// redundant keys are created (age_name vs. name_age)
// redundant keys are created (age~~name vs. name~~age)
const mainProp = propCop.shift();
// recursive check for the indexHash
indexHash = indexHash || {};
Expand All @@ -441,15 +446,15 @@ class Querybase {

// first level keys
// ex: ['name', 'age', 'location']
// -> 'name_age'
// -> 'name_location'
// -> 'age_location'
// -> 'name~~age'
// -> 'name~~location'
// -> 'age~~location'
indexHash[_.createKey(mainProp, prop)] =
_.createKey(data[mainProp], data[prop]);

// create indexes for all property combinations
// ex: ['name', 'age', 'location']
// -> 'name_age_location'
// -> 'name~~age~~location'
propCop.forEach((subProp) => {
propString = _.createKey(propString, subProp);
valueString = _.createKey(valueString, data[subProp]);
Expand Down Expand Up @@ -500,7 +505,7 @@ class Querybase {
* @return {string}
*/
encodeKey(value: string): string {
return "querybase~~" + _.encodeBase64(value);
return `querybase${_.indexKey()}` + _.encodeBase64(value);
}

/**
Expand Down Expand Up @@ -539,8 +544,8 @@ class Querybase {
* @param {FirebaseQuery} query
*
* @example
* const firebaseRef = new Firebase('<my-app>/people');
* const querybaseRef = new Querybase(firebaseRef, ['name', 'age', 'location']);
* const firebaseRef = firebase.database.ref.child('people');
* const querybaseRef = querybase.ref(firebaseRef, ['name', 'age', 'location']);
*
* // Querybase for a single string criteria, returns
* // a QuerybaseQuery, which returns a Firebase Ref
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/querybase-utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ describe('QuerybaseUtils', () => {

it('should exist', () => { expect(_).to.exist; })

describe('indexKey', () => {

it('should return the proper key', () => {
assert.equal('~~', _.indexKey());
});

});

describe('isString', () => {

it('should return true for a string value', () => {
Expand Down