From 2205e609123c153b8ae88b07d89f98c3367e9cc9 Mon Sep 17 00:00:00 2001 From: Jon Borgonia Date: Thu, 4 Jun 2015 07:23:29 -1000 Subject: [PATCH 1/4] adds failing tests for #17 --- test/test.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/test.js b/test/test.js index b10d9f6..67353b1 100644 --- a/test/test.js +++ b/test/test.js @@ -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); @@ -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(){ From 19f8e0fed77c0f0d5ca564f0b05918db97712fe2 Mon Sep 17 00:00:00 2001 From: Jon Borgonia Date: Thu, 4 Jun 2015 07:25:52 -1000 Subject: [PATCH 2/4] adds feature Static Columns, resolves #17 --- libs/base_model.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libs/base_model.js b/libs/base_model.js index 82efe0b..94a8619 100644 --- a/libs/base_model.js +++ b/libs/base_model.js @@ -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], @@ -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 = []; From 21b3db82a10e7d041b77f36f67e9f9668b1bac2e Mon Sep 17 00:00:00 2001 From: Jon Borgonia Date: Sun, 7 Jun 2015 09:22:16 -1000 Subject: [PATCH 3/4] #21 disabling ALLOW FILTERING by default updates tests to force allowfiltering --- libs/base_model.js | 8 +++++--- test/test.js | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/libs/base_model.js b/libs/base_model.js index 94a8619..80b800c 100644 --- a/libs/base_model.js +++ b/libs/base_model.js @@ -577,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; }; @@ -683,7 +684,8 @@ BaseModel.find = function(query_ob, options, callback){ var defaults = { raw : false, - consistency : CONSISTENCY_FIND + consistency : CONSISTENCY_FIND, + allowfiltering : false }; options = lodash.defaults(options, defaults); diff --git a/test/test.js b/test/test.js index 67353b1..75826d7 100644 --- a/test/test.js +++ b/test/test.js @@ -752,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); @@ -766,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); @@ -794,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(); }); From d9cc8e4c39d9d928d6668bbcddb2215839171fa5 Mon Sep 17 00:00:00 2001 From: Jon Borgonia Date: Sun, 7 Jun 2015 21:19:39 -1000 Subject: [PATCH 4/4] #21 adds allowfiltering option value type check and throw error --- libs/apollo_error.js | 3 +++ libs/base_model.js | 3 +++ test/test.js | 8 ++++++++ 3 files changed, 14 insertions(+) diff --git a/libs/apollo_error.js b/libs/apollo_error.js index 7c1bd6a..5949131 100644 --- a/libs/apollo_error.js +++ b/libs/apollo_error.js @@ -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' }, diff --git a/libs/base_model.js b/libs/base_model.js index 80b800c..68181b2 100644 --- a/libs/base_model.js +++ b/libs/base_model.js @@ -689,6 +689,9 @@ BaseModel.find = function(query_ob, options, callback){ }; options = lodash.defaults(options, defaults); + + if(typeof options.allowfiltering !== 'boolean') + return callback(build_error('model.find.allowfiltering')); var query; try{ diff --git a/test/test.js b/test/test.js index 75826d7..bda9135 100644 --- a/test/test.js +++ b/test/test.js @@ -843,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);