Comparing version 0.4.0 to 0.4.1
@@ -98,2 +98,13 @@ | ||
// updateWhereEq :: Bucket -> Predicate -> Updates -> Promise Int | ||
const updateWhereEq = R.curry((bucket, predicate, updates) => | ||
getAll(bucket) | ||
.then(R.filter(R.whereEq(predicate))) | ||
.each(row => { | ||
store[bucket][row.id] = R.merge(store[bucket][row.id], updates) | ||
}) | ||
.then(R.length) | ||
) | ||
/** @TODO Query Routing | ||
@@ -133,2 +144,3 @@ * const query = QueryRouter(config.query_route_file)(store) | ||
, update | ||
, updateWhereEq | ||
, upsert | ||
@@ -135,0 +147,0 @@ , getById |
@@ -57,2 +57,19 @@ | ||
// _predicatesToWhereEq :: Predicate -> String | ||
const _predicateToWhereEq = R.compose( | ||
R.join(' AND ') | ||
, R.values | ||
, R.mapObjIndexed(R.cond([ | ||
[ R.equals(null), (value, key) => `${key} IS NULL` ] | ||
, [ R.is(String), (value, key) => `${key} = '${value}'` ] | ||
, [ R.T, (value, key) => `${key} = ${value}` ] | ||
])) | ||
) | ||
const updateWhereEq = R.curry((mysql, table, predicates, record) => | ||
Query.updateWhere(mysql, table, _predicateToWhereEq(predicates), [], record) | ||
) | ||
const getIdForUpsert = R.curry((mysql, table, keys, params) => | ||
@@ -139,2 +156,3 @@ Query.getWhereParams( | ||
/** @TODO Query Routing | ||
@@ -148,11 +166,12 @@ * const query = R.curry((mysql, table, query_name, params) => | ||
module.exports = (mysql) => ({ | ||
insert : insert(mysql) | ||
, update : update(mysql) | ||
, upsert : upsert(mysql) | ||
, getAll : getAll(mysql) | ||
, getById : getById(mysql) | ||
, projectAll : projectAll(mysql) | ||
, findBy : findBy(mysql) | ||
, findOneBy : findOneBy(mysql) | ||
, findById : findById(mysql) | ||
insert : insert(mysql) | ||
, update : update(mysql) | ||
, updateWhereEq : updateWhereEq(mysql) | ||
, upsert : upsert(mysql) | ||
, getAll : getAll(mysql) | ||
, getById : getById(mysql) | ||
, projectAll : projectAll(mysql) | ||
, findBy : findBy(mysql) | ||
, findOneBy : findOneBy(mysql) | ||
, findById : findById(mysql) | ||
}) |
{ | ||
"name": "kuss", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Combined Universal Storage Service", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
/*eslint-env node, mocha*/ | ||
/* eslint no-magic-numbers: 0 */ | ||
/* eslint max-nested-callbacks: [ 'error', 5 ] */ | ||
const { expect } = require('chai'); | ||
@@ -39,2 +42,39 @@ const MemoryStore = require('../../lib/memory'); | ||
describe('::updateWhereEq', function() { | ||
it('should update records that satisfy the predicate', function() { | ||
const bucket = 'test' | ||
const data1 = { id : '1', foo: 'bar1', boo: 'fuck1' } | ||
const data2 = { id : '2', foo: 'bar1', boo: 'fuck2' } | ||
const data3 = { id : '3', foo: 'bar3', boo: 'fuck3' } | ||
const state = { | ||
[bucket] : { | ||
[data1.id] : data1 | ||
, [data2.id] : data2 | ||
, [data3.id] : data3 | ||
} | ||
} | ||
const tempStore = MemoryStore(state) | ||
return tempStore.updateWhereEq(bucket)({ foo : 'bar1' })({ boo : 'hoo' }) | ||
.then(updated => { | ||
expect(updated).to.eql(2) | ||
expect(state[bucket][data1.id].id).to.eql(data1.id) | ||
expect(state[bucket][data1.id].foo).to.eql(data1.foo) | ||
expect(state[bucket][data1.id].boo).to.eql('hoo') | ||
expect(state[bucket][data2.id].id).to.eql(data2.id) | ||
expect(state[bucket][data2.id].foo).to.eql(data2.foo) | ||
expect(state[bucket][data2.id].boo).to.eql('hoo') | ||
expect(state[bucket][data3.id]).to.eql(data3) | ||
}) | ||
}) | ||
}) | ||
describe('::upsert', function() { | ||
@@ -41,0 +81,0 @@ |
/*eslint-env node, mocha*/ | ||
/* eslint no-magic-numbers: 0 */ | ||
/* eslint 'max-len': [ 'error', 100 ] */ | ||
const { expect } = require('chai') | ||
const R = require('ramda') | ||
@@ -83,2 +85,25 @@ const MySqlStore = require('../../../lib/mysql') | ||
describe('::updateWhereEq', function() { | ||
it('should execute an updateWhereEq query on the given table', function() { | ||
const table = 'test' | ||
const updates = { foo: 'bar', bar: 'baz' } | ||
const predicate = { foo : 1, bar : null, one : 'one' } | ||
const sql_regex = /^UPDATE `test` SET \? WHERE foo = 1 AND bar IS NULL AND one = \'one\' .*/ | ||
mysql.query = (actual_sql, actual_updates, cb) => { | ||
expect(actual_sql).to.match(sql_regex) | ||
expect(actual_updates).to.deep.equal([updates]) | ||
cb(null, { affectedRows: 3 }) | ||
} | ||
return store.updateWhereEq(table)(predicate)(updates) | ||
.then(result => { | ||
expect(result).to.eql(3) | ||
}) | ||
}) | ||
}) | ||
describe('::upsert', function() { | ||
@@ -85,0 +110,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50197
1358