Questions tagged [knex.js]

Knex.js is a query builder for Postgres, MySQL and SQLite3, Oracle and MS SQL Server. It is designed to be flexible, portable, and fun to use. It features both traditional node style callbacks as well as a promise interface for cleaner async flow control, full featured query and schema builders, transaction support, connection pooling and standardized responses between different query clients and dialects.

Knex.js is a query builder for Postgres, MySQL, SQLite3, MS SQL Server and Oracle.


Features

  • transaction support
  • connection pooling
  • query and schema builders
  • standardized responses between different query clients and dialects
  • traditional node style callbacks
  • a promise interface for cleaner async flow control
  • ...

Resources

2513 questions
62
votes
18 answers

Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?

I am using the following code to make a knex connection, but frequently the error occurred Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call? Can anyone please suggest the solution for…
Shanthi Balraj
  • 621
  • 1
  • 6
  • 4
61
votes
5 answers

Can I conditionally add a where() clause to my knex query?

I want to add a where() clause in my query, but conditionally. Specifically, I want it added only if a sepecific querystring parameter is passed in the URL. Is this possible, and if so, how would I go about doing it? router.get('/questions',…
eponymous23
  • 1,088
  • 2
  • 10
  • 24
45
votes
6 answers

Knex.JS Auto Update Trigger

I am using Knex.JS migration tools. However, when creating a table, I'd like to have a column named updated_at that is automatically updated when a record is updated in the database. For example, here is a…
aashah7
  • 2,075
  • 1
  • 17
  • 24
37
votes
1 answer

Does Knex.js prevent sql injection?

I'm using a MySql database and was trying to find a MySQL alternative to tedious.js (a SQL server parameterised query builder).I'm using Node.js for my backend. I read that the .raw() command from knex.js is susceptible to sql injection, if not used…
Harsh Saudagar
  • 458
  • 1
  • 5
  • 14
35
votes
5 answers

Knex.js multiple orderBy() columns

Is it possible to do multiple orderBy() columns? knex .select() .table('products') .orderBy('id', 'asc') The orderBy() chainable only takes a single column key and a sort value, but how can I order by multiple columns?
Ian Jones
  • 1,369
  • 1
  • 10
  • 15
34
votes
4 answers

Alias a table in Knex

I have a SQL query that refers to the same table twice, and I need to alias the table to two separate aliases. I can't quite figure out how to compose this with Knex. There's a 'Words' table and a 'Users' table. The Words table has two foreign keys,…
Bobby Circle Ciraldo
  • 1,243
  • 1
  • 10
  • 14
31
votes
2 answers

How is Node.js Knex similar/different to Sequelize?

The answer I got from an IRC channel: Sequelize is an ORM that includes some query builder stuff; Knex is just a query builder, not an ORM. ORMs don't actually fit very well in many use cases, it's easy to run up against the limits of what they can…
nawK
  • 693
  • 1
  • 7
  • 13
27
votes
6 answers

How can I view the query `knex` builds?

For debugging purposes, I want to see the SQL query knex is executing. For example, I want to view SQL that knex produces for this code: knex('statistics') .del() .where({ 'stats': 'reddit', });
Dan
  • 55,715
  • 40
  • 116
  • 154
26
votes
1 answer

Knex.js - How To Update a Field With An Expression

How do we get Knex to create the following SQL statement: UPDATE item SET qtyonhand = qtyonhand + 1 WHERE rowid = 8 We're currently using the following code: knex('item') .transacting(trx) .update({qtyonhand: 10}) .where('rowid',…
A2MetalCore
  • 1,621
  • 4
  • 25
  • 49
26
votes
5 answers

Batch update in knex

I'd like to perform a batch update using Knex.js For example: 'UPDATE foo SET [theValues] WHERE idFoo = 1' 'UPDATE foo SET [theValues] WHERE idFoo = 2' with values: { name: "FooName1", checked: true } // to `idFoo = 1` { name: "FooName2", checked:…
nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
25
votes
2 answers

Knex: Rollback specific migration?

I'd like to be able to rollback a specific knex migration on the command line. For example: knex migrate:rollback('20161104101325') Is this possible?
steel
  • 11,883
  • 7
  • 72
  • 109
24
votes
5 answers

Upsert in KnexJS

I have an upsert query in PostgreSQL like: INSERT INTO table (id, name) values (1, 'Gabbar') ON CONFLICT (id) DO UPDATE SET name = 'Gabbar' WHERE table.id = 1 I need to use knex to this upsert query. How to go about this?
myusuf
  • 11,810
  • 11
  • 35
  • 50
24
votes
2 answers

Unit testing with Bookshelf.js and knex.js

I'm relatively new to Node and am working on a project using knex and bookshelf. I'm having a little bit of trouble unit testing my code and I'm not sure what I'm doing wrong. Basically I have a model (called VorcuProduct) that looks like this: var…
Marcos Chicote
  • 315
  • 2
  • 11
23
votes
5 answers

knex: select rows that are in certain date range

How can I select rows from the table that are in certain date range with knex queries? For example, selecting rows from last seven days. Knex version: 0.15.0 DB: PostgreSQL
Ziyaddin Sadigov
  • 8,893
  • 13
  • 34
  • 41
23
votes
6 answers

Get Knex.js transactions working with ES7 async/await

I'm trying to couple ES7's async/await with knex.js transactions. Although I can easily play around with non-transactional code, I'm struggling to get transactions working properly using the aforementioned async/await structure. I'm using this…
nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
1
2 3
99 100