mirror of
https://github.com/knex/knex.git
synced 2025-12-27 06:58:39 +00:00
working on the tests, internal cleanup
This commit is contained in:
parent
96e5b2731c
commit
09e9c60912
30
README.md
30
README.md
@ -1,13 +1,21 @@
|
||||
# Knex Query Builder
|
||||
```
|
||||
._____. ______
|
||||
| | / /
|
||||
| | / /
|
||||
| | _____ ._____ ._____. / /
|
||||
| | / / | \ | | ,_____________. ______ / /
|
||||
| | / / | \ | | / \ \ \ / /
|
||||
| | / / | \ | | / ,______. \ \ \ / /
|
||||
| | / / | \ | | / / \ \ \ \/ /
|
||||
| |/ \ | \ | | | | ____\ | \ /
|
||||
| |\ \ | \| | | | / | \ \
|
||||
| | \ \ | |\ | | | /__________| / \
|
||||
| | \ \ | | \ | \ \ / /\ \
|
||||
| | \ \ | | \ | \ \__________/\ / / \ \
|
||||
| | \ \ | | \ | \ \ / / \ \
|
||||
|_____| \_____\ |_____| \______| \_______________/ /_____/ \_____\
|
||||
```
|
||||
|
||||
Insert:
|
||||
Returns - (array) insertIds (if applicable),
|
||||
Knex.js is a multi-dialect query builder for Node.js and other environments.
|
||||
|
||||
Update:
|
||||
Returns - affected row count
|
||||
|
||||
Select:
|
||||
Returns - [rows]
|
||||
|
||||
Delete:
|
||||
Returns - affected row count
|
||||
[http://knexjs.org](knexjs.org)
|
||||
17
index.html
17
index.html
@ -537,7 +537,7 @@ Knex('users')
|
||||
</p>
|
||||
|
||||
<pre>
|
||||
// generates: "INSERT INTO `books`
|
||||
// generates: "insert into `books`
|
||||
Knex('books').insert({title: 'Slaughterhouse Five'})
|
||||
|
||||
// generates "select * from `books`;"
|
||||
@ -552,15 +552,20 @@ Knex('users')
|
||||
<br />
|
||||
Creates an <tt>update</tt> query, taking either a hash of properties to be updated into the row, or
|
||||
an array of updates, to be executed as a single update command.
|
||||
</p>
|
||||
|
||||
<pre>
|
||||
// generates: "select `title`, `author`, `year` from `books`;"
|
||||
Knex('books').select('title', 'author', 'year')
|
||||
Knex('books').update({
|
||||
title: '',
|
||||
author: '',
|
||||
year:
|
||||
})
|
||||
|
||||
// generates "select * from `books`;"
|
||||
Knex('books').select()
|
||||
Knex('users').update({
|
||||
|
||||
});
|
||||
</pre>
|
||||
</p>
|
||||
|
||||
<p id="Builder-del">
|
||||
<b class="header">del / delete</b><code>.del()</code>
|
||||
@ -836,7 +841,7 @@ Knex.Schema.table('users', function (table) {
|
||||
Knex.createTable('accounts', function() {
|
||||
t.increments().primary();
|
||||
t.string('email').unique();
|
||||
|
||||
|
||||
});
|
||||
</pre>
|
||||
|
||||
|
||||
5
knex.js
5
knex.js
@ -870,6 +870,11 @@
|
||||
return this;
|
||||
},
|
||||
|
||||
andOn: function() {
|
||||
this.on.apply(this, arguments);
|
||||
return this;
|
||||
},
|
||||
|
||||
orOn: function(first, operator, second) {
|
||||
this.clauses.push({first: first, operator: operator, second: second, bool: 'or'});
|
||||
return this;
|
||||
|
||||
32
test/lib/joins.js
Normal file
32
test/lib/joins.js
Normal file
@ -0,0 +1,32 @@
|
||||
var Q = require('q');
|
||||
module.exports = function(Knex, dbName, handler, type) {
|
||||
|
||||
describe(dbName, function() {
|
||||
|
||||
it('uses inner join by default', function(ok) {
|
||||
Knex('accounts')
|
||||
.join('test_table_two', 'accounts.id', '=', 'test_table_two.account_id')
|
||||
.select('accounts.*', 'test_table_two.details')
|
||||
.then(handler(ok), ok);
|
||||
});
|
||||
|
||||
it('takes a fifth parameter to specify the join type', function(ok) {
|
||||
Knex('accounts')
|
||||
.join('test_table_two', 'accounts.id', '=', 'test_table_two.account_id', 'left')
|
||||
.select('accounts.*', 'test_table_two.details')
|
||||
.then(handler(ok), ok);
|
||||
});
|
||||
|
||||
it('accepts a callback as the second argument for advanced joins', function(ok) {
|
||||
Knex('accounts').join('test_table_two', function(join) {
|
||||
join.on('accounts.id', '=', 'test_table_two.account_id');
|
||||
join.orOn('accounts.email', '=', 'test_table_two.details');
|
||||
}, 'left')
|
||||
.select()
|
||||
.then(handler(ok), ok);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
@ -90,33 +90,6 @@ module.exports = function(Knex, dbName, handler, type) {
|
||||
.then(handler(ok), ok);
|
||||
});
|
||||
|
||||
describe('joins', function() {
|
||||
|
||||
it('uses inner join by default', function(ok) {
|
||||
Knex('accounts')
|
||||
.join('test_table_two', 'accounts.id', '=', 'test_table_two.account_id')
|
||||
.select('accounts.*', 'test_table_two.details')
|
||||
.then(handler(ok), ok);
|
||||
});
|
||||
|
||||
it('takes a fifth parameter to specify the join type', function(ok) {
|
||||
Knex('accounts')
|
||||
.join('test_table_two', 'accounts.id', '=', 'test_table_two.account_id', 'left')
|
||||
.select('accounts.*', 'test_table_two.details')
|
||||
.then(handler(ok), ok);
|
||||
});
|
||||
|
||||
it('accepts a callback as the second argument for advanced joins', function(ok) {
|
||||
Knex('accounts').join('test_table_two', function(join) {
|
||||
join.on('accounts.id', '=', 'test_table_two.account_id');
|
||||
join.orOn('accounts.email', '=', 'test_table_two.details');
|
||||
}, 'left')
|
||||
.select()
|
||||
.then(handler(ok), ok);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
104
test/regular.js
104
test/regular.js
@ -5,6 +5,61 @@ var dev = parseInt(process.env.KNEX_DEV, 10);
|
||||
var out = (dev ? require('./index').output : require('./shared/output'));
|
||||
var assert = require('assert');
|
||||
|
||||
module.exports = function(Knex, type) {
|
||||
|
||||
describe('DB Tests - ' + type, function() {
|
||||
|
||||
describe('Knex.Builder', function() {
|
||||
|
||||
before(function(ok) {
|
||||
var val = handler(type, 'schema');
|
||||
require('./lib/schema')(Knex, function() {
|
||||
setTimeout(function() { ok(); }, 100);
|
||||
}, function(err) {
|
||||
throw new Error(err);
|
||||
}, type);
|
||||
});
|
||||
|
||||
describe('Inserts', function() {
|
||||
require('./lib/inserts')(Knex, type, handler(type, 'inserts'), 'DB');
|
||||
});
|
||||
|
||||
describe('Updates', function() {
|
||||
require('./lib/updates')(Knex, type, handler(type, 'updates'), 'DB');
|
||||
});
|
||||
|
||||
describe('Selects', function() {
|
||||
require('./lib/selects')(Knex, type, handler(type, 'selects'), 'DB');
|
||||
});
|
||||
|
||||
describe('Joins', function() {
|
||||
require('./lib/joins')(Knex, type, handler(type, 'joins'), 'DB');
|
||||
});
|
||||
|
||||
describe('Deletes', function() {
|
||||
require('./lib/deletes')(Knex, type, handler(type, 'deletes'), 'DB');
|
||||
});
|
||||
|
||||
describe('Aggregates, Truncate', function() {
|
||||
require('./lib/additional')(Knex, type, handler(type, 'additional'), 'DB');
|
||||
});
|
||||
|
||||
describe('Deletes', function() {
|
||||
require('./lib/unions')(Knex, type, handler(type, 'unions'), 'DB');
|
||||
});
|
||||
|
||||
after(function(ok) {
|
||||
if (dev) require('fs').writeFileSync('./test/shared/output.js', 'module.exports = ' + objectdump(out));
|
||||
ok();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
|
||||
var handler = function(instance, section) {
|
||||
var item = 1;
|
||||
return function(resolver, isAll) {
|
||||
@ -48,52 +103,3 @@ var omitDates = function(item) {
|
||||
return item;
|
||||
};
|
||||
|
||||
module.exports = function(Knex, type) {
|
||||
|
||||
describe('DB Tests - ' + type, function() {
|
||||
|
||||
describe('Knex.Builder', function() {
|
||||
|
||||
before(function(ok) {
|
||||
var val = handler(type, 'schema');
|
||||
require('./lib/schema')(Knex, function() {
|
||||
setTimeout(function() { ok(); }, 100);
|
||||
}, function(err) {
|
||||
throw new Error(err);
|
||||
}, type);
|
||||
});
|
||||
|
||||
describe('Inserts', function() {
|
||||
require('./lib/inserts')(Knex, type, handler(type, 'inserts'), 'DB');
|
||||
});
|
||||
|
||||
describe('Updates', function() {
|
||||
require('./lib/updates')(Knex, type, handler(type, 'updates'), 'DB');
|
||||
});
|
||||
|
||||
describe('Selects', function() {
|
||||
require('./lib/selects')(Knex, type, handler(type, 'selects'), 'DB');
|
||||
});
|
||||
|
||||
describe('Deletes', function() {
|
||||
require('./lib/deletes')(Knex, type, handler(type, 'deletes'), 'DB');
|
||||
});
|
||||
|
||||
describe('Aggregates, Truncate', function() {
|
||||
require('./lib/additional')(Knex, type, handler(type, 'additional'), 'DB');
|
||||
});
|
||||
|
||||
describe('Deletes', function() {
|
||||
require('./lib/unions')(Knex, type, handler(type, 'unions'), 'DB');
|
||||
});
|
||||
|
||||
after(function(ok) {
|
||||
if (dev) require('fs').writeFileSync('./test/shared/output.js', 'module.exports = ' + objectdump(out));
|
||||
ok();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
@ -490,7 +490,7 @@ module.exports = {
|
||||
bindings: [1,100,200,300]
|
||||
}
|
||||
},
|
||||
'selects.19': {
|
||||
'joins.1': {
|
||||
mysql: {
|
||||
sql: 'select `accounts`.*, `test_table_two`.`details` from `accounts` inner join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id`',
|
||||
bindings: []
|
||||
@ -504,7 +504,7 @@ module.exports = {
|
||||
bindings: []
|
||||
}
|
||||
},
|
||||
'selects.20': {
|
||||
'joins.2': {
|
||||
mysql: {
|
||||
sql: 'select `accounts`.*, `test_table_two`.`details` from `accounts` left join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id`',
|
||||
bindings: []
|
||||
@ -518,7 +518,7 @@ module.exports = {
|
||||
bindings: []
|
||||
}
|
||||
},
|
||||
'selects.21': {
|
||||
'joins.3': {
|
||||
mysql: {
|
||||
sql: 'select * from `accounts` left join `test_table_two` on `accounts`.`id` = `test_table_two`.`account_id` or `accounts`.`email` = `test_table_two`.`details`',
|
||||
bindings: []
|
||||
@ -1786,7 +1786,7 @@ module.exports = {
|
||||
phone: null
|
||||
}]
|
||||
},
|
||||
'selects.19': {
|
||||
'joins.1': {
|
||||
mysql: [{
|
||||
id: 1,
|
||||
first_name: 'User',
|
||||
@ -1818,7 +1818,7 @@ module.exports = {
|
||||
details: 'Lorem ipsum Minim nostrud Excepteur consectetur enim ut qui sint in veniam in nulla anim do cillum sunt voluptate Duis non incididunt.'
|
||||
}]
|
||||
},
|
||||
'selects.20': {
|
||||
'joins.2': {
|
||||
mysql: [{
|
||||
id: 1,
|
||||
first_name: 'User',
|
||||
@ -1985,7 +1985,7 @@ module.exports = {
|
||||
details: null
|
||||
}]
|
||||
},
|
||||
'selects.21': {
|
||||
'joins.3': {
|
||||
mysql: [{
|
||||
id: 1,
|
||||
first_name: 'User',
|
||||
|
||||
108
test/string.js
108
test/string.js
@ -5,6 +5,62 @@ var dev = parseInt(process.env.KNEX_DEV, 10);
|
||||
var out = (dev ? require('./index').output : require('./shared/output'));
|
||||
var assert = require('assert');
|
||||
|
||||
module.exports = function(Knex, type) {
|
||||
|
||||
var dfd = Q.defer();
|
||||
|
||||
describe('String Tests', function() {
|
||||
|
||||
before(function(ok) {
|
||||
var val = handler(type, 'schema');
|
||||
require('./lib/schema')(Knex, val(ok, true), function(err) {
|
||||
throw new Error(err);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Knex.Builder', function() {
|
||||
|
||||
describe('Inserts', function() {
|
||||
require('./lib/inserts')(Knex, type, handler(type, 'inserts'), 'String');
|
||||
});
|
||||
|
||||
describe('Updates', function() {
|
||||
require('./lib/updates')(Knex, type, handler(type, 'updates'), 'String');
|
||||
});
|
||||
|
||||
describe('Selects', function() {
|
||||
require('./lib/selects')(Knex, type, handler(type, 'selects'), 'String');
|
||||
});
|
||||
|
||||
describe('Joins', function() {
|
||||
require('./lib/joins')(Knex, type, handler(type, 'joins'), 'String');
|
||||
});
|
||||
|
||||
describe('Deletes', function() {
|
||||
require('./lib/deletes')(Knex, type, handler(type, 'deletes'), 'String');
|
||||
});
|
||||
|
||||
describe('Aggregates, Truncate', function() {
|
||||
require('./lib/additional')(Knex, type, handler(type, 'additional'), 'String');
|
||||
});
|
||||
|
||||
describe('Deletes', function() {
|
||||
require('./lib/unions')(Knex, type, handler(type, 'unions'), 'String');
|
||||
});
|
||||
|
||||
after(function(ok) {
|
||||
if (dev) require('fs').writeFileSync('./test/shared/output.js', 'module.exports = ' + objectdump(out));
|
||||
dfd.resolve();
|
||||
ok();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
return dfd.promise;
|
||||
};
|
||||
|
||||
var handler = function(instance, section) {
|
||||
var item = 1;
|
||||
return function(resolver, isAll) {
|
||||
@ -37,56 +93,4 @@ var handler = function(instance, section) {
|
||||
return fn;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = function(Knex, type) {
|
||||
|
||||
var dfd = Q.defer();
|
||||
|
||||
describe('String Tests', function() {
|
||||
|
||||
before(function(ok) {
|
||||
var val = handler(type, 'schema');
|
||||
require('./lib/schema')(Knex, val(ok, true), function(err) {
|
||||
throw new Error(err);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Knex.Builder', function() {
|
||||
|
||||
describe('Inserts', function() {
|
||||
require('./lib/inserts')(Knex, type, handler(type, 'inserts'), 'String');
|
||||
});
|
||||
|
||||
describe('Updates', function() {
|
||||
require('./lib/updates')(Knex, type, handler(type, 'updates'), 'String');
|
||||
});
|
||||
|
||||
describe('Selects', function() {
|
||||
require('./lib/selects')(Knex, type, handler(type, 'selects'), 'String');
|
||||
});
|
||||
|
||||
describe('Deletes', function() {
|
||||
require('./lib/deletes')(Knex, type, handler(type, 'deletes'), 'String');
|
||||
});
|
||||
|
||||
describe('Aggregates, Truncate', function() {
|
||||
require('./lib/additional')(Knex, type, handler(type, 'additional'), 'String');
|
||||
});
|
||||
|
||||
describe('Deletes', function() {
|
||||
require('./lib/unions')(Knex, type, handler(type, 'unions'), 'String');
|
||||
});
|
||||
|
||||
after(function(ok) {
|
||||
if (dev) require('fs').writeFileSync('./test/shared/output.js', 'module.exports = ' + objectdump(out));
|
||||
dfd.resolve();
|
||||
ok();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
return dfd.promise;
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user