Skip to content

Commit

Permalink
Merge pull request #6 from senecajs/enhancements
Browse files Browse the repository at this point in the history
Enhancements
  • Loading branch information
mihaidma committed Apr 19, 2016
2 parents 8165295 + 3882bd8 commit 15c2d9f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 63 deletions.
18 changes: 6 additions & 12 deletions lib/query-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ var _ = require('lodash')
var OpParser = require('./operator_parser')

module.exports = function (seneca) {
var StandardQuery
seneca.ready(function () {
StandardQuery = seneca.export('standard-query/utils')
})
var StandardQuery = seneca.export('standard-query/utils')

function parseExtendedExpression (sTypes, currentName, currentValue, params, values) {
function parseComplexSelectOperator (sTypes, currentName, currentValue, params, values) {
Expand Down Expand Up @@ -50,17 +47,14 @@ module.exports = function (seneca) {
StandardQuery.selectstmCustom(qent, q, sTypes, buildQueryFromExpression, done)
}

function fixPrepStatement (stm, sTypes) {
return StandardQuery.fixPrepStatement(stm, sTypes)
}

function selectstmOr (qent, q, sTypes) {
return StandardQuery.selectstmOr(qent, q, sTypes)
function buildSelectStatement (qent, q, sTypes, done) {
return StandardQuery.buildSelectStatementCustom(qent, q, sTypes, selectstm, StandardQuery.selectstmOr, done)
}

return {
selectstm: selectstm,
fixPrepStatement: fixPrepStatement,
selectstmOr: selectstmOr
fixPrepStatement: StandardQuery.fixPrepStatement,
selectstmOr: StandardQuery.selectstmOr,
buildSelectStatement: buildSelectStatement
}
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "seneca-store-query",
"version": "0.0.3",
"version": "0.0.5",
"description": "Extended query for Seneca framework stores",
"main": "store-query.js",
"license": "MIT",
Expand Down Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"lodash": "3.10.1",
"node-uuid": "1.4.7",
"seneca-standard-query": "0.0.3"
"seneca-standard-query": "0.0.5"
},
"devDependencies": {
"chai": "3.5.0",
Expand All @@ -56,8 +56,8 @@
"eslint-plugin-hapi": "4.x.x",
"eslint-plugin-standard": "1.x.x",
"lab": "6.x.x",
"seneca": "1.3.0",
"seneca": "1.x.x",
"seneca-store-test": "1.0.0",
"seneca-postgres-store": "mihaidma/seneca-postgres-store#snakecamel"
"seneca-postgres-store": "2.x.x"
}
}
65 changes: 18 additions & 47 deletions store-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var name = 'store-query'

module.exports = function queryBuilder (options) {
var seneca = this
var QueryBuilder = require('./lib/query-builder')(seneca, options)

function specificTypes (storeName) {
var sTypes = {
Expand All @@ -23,57 +22,29 @@ module.exports = function queryBuilder (options) {
return sTypes
}

seneca.add({role: actionRole, hook: 'load'}, function (args, done) {
var qent = args.qent
var sTypes = specificTypes(args.target)
var q = _.clone(args.q)
q.limit$ = 1
seneca.ready(function () {
var QueryBuilder = require('./lib/query-builder')(seneca, options)

QueryBuilder.selectstm(qent, q, sTypes, function (err, query) {
return done(err, {query: query})
})
})

seneca.add({role: actionRole, hook: 'list'}, function (args, done) {
var qent = args.qent
var q = args.q
var sTypes = specificTypes(args.target)
seneca.add({role: actionRole, hook: 'load'}, function (args, done) {
var qent = args.qent
var sTypes = specificTypes(args.target)
var q = _.clone(args.q)
q.limit$ = 1

buildSelectStatement(q, sTypes, function (err, query) {
return done(err, {query: query})
QueryBuilder.selectstm(qent, q, sTypes, function (err, query) {
return done(err, {query: query})
})
})

function buildSelectStatement (q, sTypes, done) {
var query
seneca.add({role: actionRole, hook: 'list'}, function (args, done) {
var qent = args.qent
var q = args.q
var sTypes = specificTypes(args.target)

if (_.isString(q)) {
return done(null, q)
}
else if (_.isArray(q)) {
// first element in array should be query, the other being values
if (q.length === 0) {
var errorDetails = {
message: 'Invalid query',
query: q
}
seneca.log.error('Invalid query')
return done(errorDetails)
}
query = {}
query.text = QueryBuilder.fixPrepStatement(q[0], sTypes)
query.values = _.clone(q)
query.values.splice(0, 1)
return done(null, query)
}
else {
if (q.ids) {
return done(null, QueryBuilder.selectstmOr(qent, q, sTypes))
}
else {
QueryBuilder.selectstm(qent, q, sTypes, done)
}
}
}
QueryBuilder.buildSelectStatement(qent, q, sTypes, function (err, query) {
return done(err, {query: query})
})
})
})

return {
Expand Down

0 comments on commit 15c2d9f

Please sign in to comment.