Builder pattern for UsergridQuery functional but incomplete
diff --git a/helpers/index.js b/helpers/index.js
index da72609..bc11d72 100644
--- a/helpers/index.js
+++ b/helpers/index.js
@@ -1,12 +1,16 @@
var mutability = require('./mutability'),
cb = require('./callbackCheck'),
buildUrl = require('./buildUrl'),
- userAgent = require('./userAgent')
+ userAgent = require('./userAgent'),
+ isNumeric = require('./isNumeric'),
+ query = require('./query')
module.exports = {
setImmutable: mutability.setImmutable,
setMutable: mutability.setMutable,
cb: cb,
buildUrl: buildUrl,
- userAgent: userAgent
+ userAgent: userAgent,
+ isNumeric: isNumeric,
+ query: query
}
\ No newline at end of file
diff --git a/helpers/isNumeric.js b/helpers/isNumeric.js
new file mode 100644
index 0000000..25cf469
--- /dev/null
+++ b/helpers/isNumeric.js
@@ -0,0 +1,5 @@
+'use strict'
+
+module.exports = function(n) {
+ return !isNaN(parseFloat(n)) && isFinite(n)
+}
\ No newline at end of file
diff --git a/helpers/query.js b/helpers/query.js
new file mode 100644
index 0000000..0b3e6cd
--- /dev/null
+++ b/helpers/query.js
@@ -0,0 +1,10 @@
+'use strict'
+
+var isNumeric = require('./isNumeric'),
+ util = require('util')
+
+module.exports = {
+ useQuotesIfRequired: function(value) {
+ return isNumeric(value) ? value : util.format('\'%s\'', value)
+ }
+}
\ No newline at end of file
diff --git a/lib/client.js b/lib/client.js
index 7a61df2..bf2d35b 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -18,11 +18,11 @@
self.appId = ok(arguments).getIfExists('1') || ok(opts).getIfExists('appId') || ok(config).getIfExists('usergrid.appId')
// optional initializer properites
- self.baseUrl = ok(opts).getIfExists('baseUrl') || ok(config).getIfExists('usergrid.baseUrl') || "https://api.usergrid.com" // default: https://api.usergrid.com
- self.clientId = ok(opts).getIfExists('clientId') || ok(config).getIfExists('usergrid.clientId') || "" // default: undefined
- self.clientSecret = ok(opts).getIfExists('clientSecret') || ok(config).getIfExists('usergrid.clientSecret') || "" // default: undefined
+ self.baseUrl = ok(opts).getIfExists('baseUrl') || ok(config).getIfExists('usergrid.baseUrl') || 'https://api.usergrid.com' // default: https://api.usergrid.com
+ self.clientId = ok(opts).getIfExists('clientId') || ok(config).getIfExists('usergrid.clientId') || '' // default: undefined
+ self.clientSecret = ok(opts).getIfExists('clientSecret') || ok(config).getIfExists('usergrid.clientSecret') || '' // default: undefined
self.tokenTtl = ok(opts).getIfExists('tokenTtl') || ok(config).getIfExists('usergrid.tokenTtl') || 3600 // time in seconds, default: 3600
- self.authFallback = ok(opts).getIfExists('authFallback') || ok(config).getIfExists('usergrid.authFallback') || "none" // ("none"|"app") default: "none"
+ self.authFallback = ok(opts).getIfExists('authFallback') || ok(config).getIfExists('usergrid.authFallback') || 'none' // ('none'|'app') default: 'none'
// optional post-init properties
self.paginationPreloadPages = 0 // number of pages to preload
@@ -30,7 +30,7 @@
self.paginationCursors = [] // array of pagination cursors
if (self.orgId !== undefined && self.appId !== undefined) {
- return self;
+ return self
} else {
throw new Error('\'orgId\' and \'appId\' parameters are required when instantiating UsergridClient')
}
@@ -78,7 +78,7 @@
}, callback || uuidOrName)
}
-Object.defineProperty(UsergridClient.prototype, "appAuth", {
+Object.defineProperty(UsergridClient.prototype, 'appAuth', {
get: function() {
return this._appAuth
},
@@ -116,7 +116,7 @@
uri: helpers.buildUrl(opts),
headers: helpers.userAgent,
body: {
- grant_type: "client_credentials",
+ grant_type: 'client_credentials',
client_id: opts.clientId || self.appAuth.clientId,
client_secret: opts.clientSecret || self.appAuth.clientSecret
},
diff --git a/lib/query.js b/lib/query.js
index ec626c6..3bf650e 100644
--- a/lib/query.js
+++ b/lib/query.js
@@ -1 +1,88 @@
-UsergridQuery.js
\ No newline at end of file
+'use strict'
+
+var config = require('../config.json'),
+ helpers = require('../helpers'),
+ util = require('util')
+ // _ = require('underscore')
+
+var UsergridQuery = function(type) {
+
+ var eq, gt, gte, lt, lte, contains, locationWithin, sort
+
+ // builder pattern
+ var self = {
+ type: function(value) {
+ self._type = value
+ return self
+ },
+ collection: function(value) {
+ self._type = value
+ return self
+ },
+ limit: function(value) {
+ self._limit = value
+ return self
+ },
+ eq: function(key, value) {
+ eq = util.format('%s = %s', key, helpers.query.useQuotesIfRequired(value))
+ return self
+ },
+ gt: function(key, value) {
+ gt = util.format('%s > %s', key, helpers.query.useQuotesIfRequired(value))
+ return self
+ },
+ gte: function(key, value) {
+ gte = util.format('%s >= %s', key, helpers.query.useQuotesIfRequired(value))
+ return self
+ },
+ lt: function(key, value) {
+ lt = util.format('%s < %s', key, helpers.query.useQuotesIfRequired(value))
+ return self
+ },
+ lte: function(key, value) {
+ lte = util.format('%s <= %s', key, helpers.query.useQuotesIfRequired(value))
+ return self
+ },
+ contains: function(key, value) {
+ contains = util.format('%s contains %s', key, helpers.query.useQuotesIfRequired(value))
+ return self
+ },
+ locationWithin: function(distanceInMeters, lat, lng) {
+ locationWithin = util.format('location within %s of %s, %s', distanceInMeters, lat, lng)
+ return self
+ },
+ asc: function(key) {
+ this.sort(key, 'asc')
+ return self
+ },
+ desc: function(key) {
+ this.sort(key, 'desc')
+ return self
+ },
+ sort: function(key, order) {
+ sort = (key && order) ? util.format(' order by %s %s', key, order) : ''
+ return self
+ },
+ or: function() {
+ return self
+ },
+ fromString: function(string) {
+ self.ql = string
+ return self
+ }
+ }
+
+ // required properties
+ self._type = self._type || type
+
+ // public accessors
+ Object.defineProperty(self, '_ql', {
+ get: function() {
+ return util.format('select * where %s%s', eq || gt || gte || lt || lte || contains || '', sort || '')
+ }
+ })
+
+ return self
+}
+
+module.exports = UsergridQuery
\ No newline at end of file
diff --git a/tests/index.js b/tests/index.js
index 3e7cbf3..6dd6a8e 100644
--- a/tests/index.js
+++ b/tests/index.js
@@ -6,4 +6,8 @@
describe('UsergridClient', function() {
return require('./lib/client')
+})
+
+describe('UsergridQuery', function() {
+ return require('./lib/query')
})
\ No newline at end of file
diff --git a/tests/lib/client.js b/tests/lib/client.js
index 48ed930..5023637 100644
--- a/tests/lib/client.js
+++ b/tests/lib/client.js
@@ -20,7 +20,7 @@
})
})
-describe.skip('GET()', function() {
+describe('GET()', function() {
this.slow(1000)
this.timeout(6000)
@@ -59,7 +59,7 @@
})
})
-describe.skip('POST()', function() {
+describe('POST()', function() {
this.slow(1000)
this.timeout(3000)
@@ -98,7 +98,7 @@
})
})
-describe.skip('PUT()', function() {
+describe('PUT()', function() {
this.slow(1000)
this.timeout(3000)
@@ -136,7 +136,7 @@
})
})
-describe.skip('DELETE()', function() {
+describe('DELETE()', function() {
this.slow(1000)
this.timeout(6000)
diff --git a/tests/lib/query.js b/tests/lib/query.js
new file mode 100644
index 0000000..abde631
--- /dev/null
+++ b/tests/lib/query.js
@@ -0,0 +1,41 @@
+'use strict'
+
+var should = require('should'),
+ config = require('../../config.json'),
+ UsergridClient = require('../../lib/client'),
+ UsergridQuery = require('../../lib/query')
+
+describe('type', function() {
+ it('query._type should equal \'cats\' when passing \'type\' as a parameter to UsergridQuery', function() {
+ var query = new UsergridQuery('cats')
+ query.should.have.property('_type').equal('cats')
+ })
+
+ it('query._type should equal \'cats\' when calling .type() builder method', function() {
+ var query = new UsergridQuery().type('cats')
+ query.should.have.property('_type').equal('cats')
+ })
+
+ it('query._type should equal \'cats\' when calling .collection() builder method', function() {
+ var query = new UsergridQuery().collection('cats')
+ query.should.have.property('_type').equal('cats')
+ })
+})
+
+describe('limit', function() {
+ it('query._limit should equal 10', function() {
+ var query = new UsergridQuery('cats').limit(10)
+ query.should.have.property('_limit').equal(10)
+ })
+})
+
+describe('eq', function() {
+ it('query._ql should equal \"select * where color = \'black\'\"', function() {
+ var query = new UsergridQuery().collection('cats').eq('color', 'black')
+ query.should.have.property('_ql').equal('select * where color = \'black\'')
+ })
+})
+
+// console.log(.desc('color').ql)
+// console.log(new UsergridQuery().collection('cats').gt('weight', 2.4).desc('color').ql)
+// console.log(new UsergridQuery().collection('cats').limit(10).limit)
\ No newline at end of file