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

Add query builder #244

Merged
merged 5 commits into from
Jan 24, 2016
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
5 changes: 5 additions & 0 deletions lib/orbit-common/oql/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ var QueryExpression = Class.extend({
init(op, args) {
this.op = op;
this.args = args;
},

toString() {
const formattedArgs = this.args.map(arg => '' + arg).join(', ');
return `${this.op}(${formattedArgs})`;
}
});

Expand Down
39 changes: 39 additions & 0 deletions lib/orbit-common/query-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Class } from 'orbit/lib/objects';
import { queryExpression as oqe } from 'orbit-common/oql/expressions';
import OrbitQueryBuilder from 'orbit/query-builder';
import {
TermBase,
Cursor
} from 'orbit/query-builder/terms';
import {
RecordCursor,
Records
} from 'orbit-common/query-builder/terms';

class QueryBuilder extends OrbitQueryBuilder {
constructor(options = {}) {
super(options);
this._typeTerms = this._createTypeTerms(options.terms.recordsOfType);
}

recordsOfType(type) {
const TypeTerm = this._typeTerms[type] || Records;

return new TypeTerm(oqe('recordsOfType', type));
}

_createTypeTerms(typeOptions) {
if (!typeOptions) { return {}; }

const typeTerms = {};

Object.keys(typeOptions).forEach(type => {
const scopes = typeOptions[type];
typeTerms[type] = Records.withScopes(scopes);
});

return typeTerms;
}
}

export default QueryBuilder;
52 changes: 52 additions & 0 deletions lib/orbit-common/query-builder/terms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { queryExpression as oqe } from 'orbit-common/oql/expressions';

import {
Cursor,
TermBase
} from 'orbit/query-builder/terms';

class RecordCursor extends Cursor {
attribute(name) {
return this.get(`attributes/${name}`);
}
}

class Records extends TermBase {
filter(predicateExpression) {
const filterBuilder = new RecordCursor();
return new this.constructor(oqe('filter', this._oqe, predicateExpression(filterBuilder)));
}

filterAttributes(attributeValues) {
const attributeExpressions = Object.keys(attributeValues).map(attribute => {
return oqe('equal',
oqe('get', `attributes/${attribute}`),
attributeValues[attribute]);
});

const andExpression = attributeExpressions.length === 1 ? attributeExpressions[0]
: oqe('and', ...attributeExpressions);

return new this.constructor(oqe('filter', this._oqe, andExpression));
}

build() {
return this._oqe;
}

static withScopes(scopes) {
const typeTerm = function(oqe) {
Records.call(this, oqe);
};

typeTerm.prototype = Object.create(Records.prototype);
Object.assign(typeTerm.prototype, scopes);

return typeTerm;
}
}

export {
RecordCursor,
Records
};
25 changes: 25 additions & 0 deletions lib/orbit/query-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { queryExpression as oqe } from 'orbit-common/oql/expressions';
import { Value } from 'orbit/query-builder/terms';

class QueryBuilder {
constructor(options = {}) {
this._options = options;

options.terms = options.terms || {};
}

get(path) {
return new Value(oqe('get', path));
}

// should probably go somewhere else
or(a, b) {
return oqe('or', a, b);
}

and(a, b) {
return oqe('and', a, b);
}
}

export default QueryBuilder;
24 changes: 24 additions & 0 deletions lib/orbit/query-builder/terms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { queryExpression as oqe } from 'orbit-common/oql/expressions';

class TermBase {
constructor(oqe) {
this._oqe = oqe;
}
}

class Cursor extends TermBase {
get(path) {
return new Value(oqe('get', path));
}
}

class Value extends TermBase {
equal(value) {
return oqe('equal', this._oqe, value);
}
}

export {
TermBase,
Cursor
};
140 changes: 140 additions & 0 deletions test/tests/orbit-common/unit/query-builder-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import 'tests/test-helper';
import { queryExpression as oqe } from 'orbit-common/oql/expressions';
import QueryBuilder from 'orbit-common/query-builder';

function assertEqualQuery(actual, expected) {
function jsonify(query) {
return JSON.parse(JSON.stringify(query));
}

deepEqual(actual.toString(), expected.toString());
}

module('OC - QueryBuilder', function(hooks) {
let qb;

hooks.beforeEach(function() {
qb = new QueryBuilder();
});

test('recordsOfType', function(assert) {
assertEqualQuery(
qb.recordsOfType('planet').build(),

oqe('recordsOfType', 'planet')
);
});

test('recordsOfType/filter/equal/get', function(assert) {
assertEqualQuery(
qb
.recordsOfType('planet')
.filter(record => record.get('attributes/name').equal('Pluto'))
.build(),

oqe('filter',
oqe('recordsOfType', 'planet'),
oqe('equal', oqe('get', 'attributes/name'), 'Pluto'))
);
});

test('recordsOfType/filter/equal/get/or', function(assert) {
assertEqualQuery(
qb
.recordsOfType('planet')
.filter(record =>
qb.or(
record.get('attributes/name').equal('Jupiter'),
record.get('attributes/name').equal('Pluto')
)
)
.build(),

oqe('filter',
oqe('recordsOfType', 'planet'),
oqe('or',
oqe('equal', oqe('get', 'attributes/name'), 'Jupiter'),
oqe('equal', oqe('get', 'attributes/name'), 'Pluto')))
);
});

test('recordsOfType/filter/equal/get/and', function(assert) {
assertEqualQuery(
qb
.recordsOfType('planet')
.filter(record =>
qb.and(
record.get('attributes/name').equal('Jupiter'),
record.get('attributes/name').equal('Pluto')
)
)
.build(),

oqe('filter',
oqe('recordsOfType', 'planet'),
oqe('and',
oqe('equal', oqe('get', 'attributes/name'), 'Jupiter'),
oqe('equal', oqe('get', 'attributes/name'), 'Pluto')))
);
});

test('recordsOfType/filter/equal/attribute/and', function(assert) {
assertEqualQuery(
qb
.recordsOfType('planet')
.filter(record =>
qb.and(
record.attribute('name').equal('Jupiter'),
record.attribute('name').equal('Pluto')
)
)
.build(),

oqe('filter',
oqe('recordsOfType', 'planet'),
oqe('and',
oqe('equal', oqe('get', 'attributes/name'), 'Jupiter'),
oqe('equal', oqe('get', 'attributes/name'), 'Pluto')))
);
});

test('recordsOfType/filterAttributes', function(assert) {
assertEqualQuery(
qb
.recordsOfType('planet')
.filterAttributes({ 'name': 'Jupiter', 'age': '23000000' })
.build(),

oqe('filter',
oqe('recordsOfType', 'planet'),
oqe('and',
oqe('equal', oqe('get', 'attributes/name'), 'Jupiter'),
oqe('equal', oqe('get', 'attributes/age'), '23000000')))
);
});

test('recordsOfType with scopes', function(assert) {
const planetScopes = {
namedPluto() {
return this.filterAttributes({ name: 'Pluto' });
}
};

const qb = new QueryBuilder({
terms: {
recordsOfType: { planet: planetScopes }
}
});

assertEqualQuery(
qb
.recordsOfType('planet')
.namedPluto()
.build(),

oqe('filter',
oqe('recordsOfType', 'planet'),
oqe('equal', oqe('get', 'attributes/name'), 'Pluto'))
);
});
});