Comparing version 0.1.0 to 0.2.0
25
one.js
/* @flow */ | ||
module.exports = function one /* ::<T> */ (rows /* :Array<T> */) /* :T */ | ||
var one | ||
= module.exports | ||
= function one /* ::<T> */ (rows /* :Array<T> */) /* :T */ | ||
{ | ||
@@ -9,10 +11,27 @@ if (rows.length === 1) | ||
} | ||
else if (rows.length > 1) | ||
else if (rows.length === 0) | ||
{ | ||
throw new TypeError('knexed/one/no-rows') | ||
} | ||
else | ||
{ | ||
throw new TypeError('knexed/one/more-rows') | ||
} | ||
} | ||
one.maybe | ||
= function one__maybe /* ::<T> */ (rows /* :Array<T> */) /* :?T */ | ||
{ | ||
if (rows.length === 1) | ||
{ | ||
return rows[0] | ||
} | ||
else if (rows.length === 0) | ||
{ | ||
return null | ||
} | ||
else | ||
{ | ||
throw new TypeError('knexed/one/no-rows') | ||
throw new TypeError('knexed/one/more-rows') | ||
} | ||
} |
{ | ||
"name": "knexed", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
@@ -18,8 +18,12 @@ "description": "knex utilities", | ||
{ | ||
"test": "mocha test/*.test.js", | ||
"st": "eslint test/", | ||
"flow": "flow", | ||
"st": "eslint ./*.js test/", | ||
"flow": "flow", | ||
"test": "mocha --check-leaks --recursive test/*.test.js", | ||
"cover": "istanbul cover _mocha -- --check-leaks --recursive test/*.test.js", | ||
"check": "npm run st && npm run flow", | ||
"all": "npm run check && npm test" | ||
"all": "npm run check && npm test", | ||
"coveralls": "istanbul-coveralls" | ||
}, | ||
@@ -29,2 +33,5 @@ | ||
{ | ||
"knex": | ||
"0.12", | ||
"mocha": | ||
@@ -39,2 +46,11 @@ "3", | ||
"lodash": | ||
"4", | ||
"istanbul": | ||
"0.4", | ||
"istanbul-coveralls": | ||
"1", | ||
"flow-bin": | ||
@@ -41,0 +57,0 @@ "0.32", |
# knexed | ||
Utilities for [knex](http://knexjs.org/) library. | ||
[![Travis](https://img.shields.io/travis/StreetStrider/knexed.svg?style=flat-square)](https://travis-ci.org/StreetStrider/knexed) | ||
[![Coveralls](https://img.shields.io/coveralls/StreetStrider/knexed.svg?style=flat-square)](https://coveralls.io/github/StreetStrider/knexed) | ||
[![npm|knexed](http://img.shields.io/badge/npm-knexed-CB3837.svg?style=flat-square)](https://www.npmjs.org/package/knexed) | ||
[![flowtype](http://img.shields.io/badge/flow-type-EBBF3A.svg?style=flat-square)](#flow) | ||
[![MIT licensed](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](license.txt) | ||
Utilities for [Knex](http://knexjs.org/) library. | ||
## API | ||
Here's examples: | ||
```js | ||
knex('table').select() | ||
/* build query … */ | ||
/* use one of utilities: */ | ||
.then(one) // pick exact 1 row | ||
.then(one.maybe) // pick 0..1 rows | ||
.then(exists) // true/false depending on rows existence | ||
.then(exists.not) // negated true/false on existence | ||
.then(count) // pick rows count | ||
``` | ||
## flow | ||
We're providing built-in [Flow](https://flowtype.org/) type definitions. | ||
## license | ||
MIT. | ||
© Strider, 2016. |
@@ -9,11 +9,9 @@ /* @flow */ | ||
var one = require('../one') | ||
var ds = dataset.series(kx, 1, 4) | ||
describe('one', () => | ||
{ | ||
var one = require('../one') | ||
var ds = dataset(kx, table => table.integer('n'), | ||
[ | ||
{ n: 1 }, { n: 2 }, { n: 3 } | ||
]) | ||
it('one(rows = 1) → row', () => | ||
@@ -67,1 +65,50 @@ { | ||
}) | ||
describe('one.maybe', () => | ||
{ | ||
it('one.maybe(rows = 1) → row', () => | ||
{ | ||
return ds | ||
.then(ds => | ||
{ | ||
return ds().select().where('n', 1) | ||
}) | ||
.then(one.maybe) | ||
.then(row => | ||
{ | ||
expect(row).an('object') | ||
expect(row).property('n') | ||
expect(row.n).a('number') | ||
}) | ||
}) | ||
it('one.maybe(rows = 0) → null', () => | ||
{ | ||
return ds | ||
.then(ds => | ||
{ | ||
return ds().select().where('n', 7) | ||
}) | ||
.then(one.maybe) | ||
.then(row => | ||
{ | ||
expect(row).null | ||
}) | ||
}) | ||
it('one.maybe(rows = many) → throws TypeError', () => | ||
{ | ||
return test_error({ | ||
message: 'knexed/one/more-rows' | ||
}, | ||
() => | ||
{ | ||
return ds | ||
.then(ds => | ||
{ | ||
return ds().select() | ||
}) | ||
.then(one.maybe) | ||
}) | ||
}) | ||
}) |
/* @flow */ | ||
module.exports = function dataset ( | ||
kx /* :Function & Object */, | ||
schema /* :Function */, | ||
data /* :Array<Object> | Object */ | ||
var next = 1 | ||
var dataset | ||
= module.exports | ||
= function dataset | ||
( | ||
kx /* :Function & Object */, | ||
schema /* :Function */, | ||
data /* :Array<Object> | Object */ | ||
) | ||
{ | ||
return kx.schema.createTable('dataset', schema) | ||
var name = 'dataset' + next | ||
++ next | ||
return kx.schema.createTable(name, schema) | ||
.then(() => | ||
{ | ||
return () => kx('dataset') | ||
return () => kx(name) | ||
}) | ||
@@ -23,1 +31,18 @@ .then(ds => | ||
} | ||
var range = require('lodash/range') | ||
dataset.series = function series | ||
( | ||
kx /* :Function & Object */, | ||
start /* :number */, | ||
end /* :?number */ | ||
) | ||
{ | ||
var datarange | ||
datarange = range(start, end) | ||
datarange = datarange.map(n => ({ n: n })) | ||
return dataset(kx, table => table.integer('n'), datarange) | ||
} |
@@ -12,4 +12,5 @@ | ||
filename: ':memory:' | ||
} | ||
}, | ||
useNullAsDefault: true | ||
}) | ||
} |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
10307
20
378
32
10
1