Skip to content
Open
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
3 changes: 3 additions & 0 deletions libs/apollo_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ var AERROR_TYPES = {
'model.find.limittype': {
msg: 'Invalid limit value'
},
'model.find.allowfiltering': {
msg: 'Invalid allow filtering value. Must be Boolean'
},
'model.find.dberror': {
msg: 'Error during find query on DB -> %s'
},
Expand Down
25 changes: 21 additions & 4 deletions libs/base_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,14 @@ BaseModel._create_table_query = function(table_name,schema){
continue;
}
field_type = schemer.get_field_type(schema, k);
rows.push(util.format('"%s" %s',k,field_type));
rows.push(
util.format(
'"%s" %s %s',
k,
field_type,
schema.fields[k]["static"] ? "static" : " "
)
);
}

var partition_key = schema.key[0],
Expand Down Expand Up @@ -371,6 +378,11 @@ BaseModel._get_db_table_schema = function (callback){
db_schema.key = [[]];
db_schema.key[row.component_index+1] = row.column_name;
}
else if(row.type == 'static'){
if(!db_schema['static'])
db_schema['static'] = [];
db_schema['static'].push(row.column_name);
}
if(row.index_name){
if(!db_schema.indexes)
db_schema.indexes = [];
Expand Down Expand Up @@ -565,11 +577,12 @@ BaseModel._create_find_query = function(query_ob, options){
}
var where = this._create_where_clause(query_ob);
var query = util.format(
'SELECT * FROM "%s" %s %s %s ALLOW FILTERING;',
'SELECT * FROM "%s" %s %s %s %s;',
this._properties.table_name,
where,
order_keys.length ? 'ORDER BY '+ order_keys.join(', '):' ',
limit ? 'LIMIT '+limit : ' '
limit ? 'LIMIT '+limit : ' ',
options.allowfiltering ? 'ALLOW FILTERING' : ' '
);
return query;
};
Expand Down Expand Up @@ -671,10 +684,14 @@ BaseModel.find = function(query_ob, options, callback){

var defaults = {
raw : false,
consistency : CONSISTENCY_FIND
consistency : CONSISTENCY_FIND,
allowfiltering : false
};

options = lodash.defaults(options, defaults);

if(typeof options.allowfiltering !== 'boolean')
return callback(build_error('model.find.allowfiltering'));

var query;
try{
Expand Down
55 changes: 52 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ describe('Apollo > ', function(){
},
key:[["name", "surname"]]
};

var model_test_static = {
fields:{
username: "text",
id: {
"type":"uuid",
"default": {"$db_function": "uuid()"}
},
name:{
"type":"text",
"static":true
},
surname:{
"type":"text",
"static":true
},
favorites:{
"type":"text",
"static":false
},
likes:"text",
},
key:[["username"],"id"]
};

it('add model', function(){
var TestModel = ap.add_model("test1", model_test1);
Expand Down Expand Up @@ -180,6 +204,23 @@ describe('Apollo > ', function(){
assert.notOk(ins.complete_name);
});

it('adds model with static fields', function(done){
var TestModel = ap.add_model("teststaticfields", model_test_static);
TestModel._create_table(function (err) {
TestModel._get_db_table_schema(function(err, schema){
assert.notOk(err);
assert.property(schema,'static');
assert.isArray(schema['static']);
assert.equal(schema['static'][0], 'name');
assert.equal(schema['static'][1], 'surname');
assert.isFunction(TestModel);
assert.property(TestModel,'find');
assert.isFalse(TestModel.is_table_ready());
done();
});
})
});

describe('Validation >', function(){

it('field validation', function(){
Expand Down Expand Up @@ -711,7 +752,7 @@ describe('Apollo > ', function(){
});

it('basic find', function(done){
TestModel.find({'v1':1, 'v4':'foo', 'v5':true},function(err, results){
TestModel.find({'v1':1, 'v4':'foo', 'v5':true},{ allowfiltering: true },function(err, results){
assert.lengthOf(results, 1);
var result = results[0];
assert.instanceOf(result, TestModel);
Expand All @@ -725,7 +766,7 @@ describe('Apollo > ', function(){
});

it('basic find with raw results', function(done){
TestModel.find({'v1':1, 'v4':'foo', 'v5':true},{ raw: true },function(err, results){
TestModel.find({'v1':1, 'v4':'foo', 'v5':true},{ raw: true, allowfiltering: true },function(err, results){
assert.lengthOf(results, 1);
var result = results[0];
assert.notInstanceOf(result, TestModel);
Expand Down Expand Up @@ -753,7 +794,7 @@ describe('Apollo > ', function(){
});

it('using >= ($gte) in clustering key', function(done){
TestModel.find({'v3':{'$gte':1 } },function(err, results){
TestModel.find({'v3':{'$gte':1 } }, { allowfiltering: true },function(err, results){
assert.lengthOf(results, 3);
done();
});
Expand Down Expand Up @@ -802,6 +843,14 @@ describe('Apollo > ', function(){
});
});

it('faulty find (invalid allowfiltering option value)', function(done){
TestModel.find({},{ allowfiltering: 'true' },function(err, results){
assert.ok(err);
assert.propertyVal(err,'name','apollo.model.find.allowfiltering');
done();
});
});

it('ordering by a clustering key ($orderby)', function(done){
TestModel.find({'v1':11, 'v2':{'$in':['twelve','twentytwo']}, '$orderby':{'$desc' :'v3'} },function(err, results){
assert.notOk(err);
Expand Down