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
23
votes
4 answers

Get count result with knex.js / bookshelf.js

I'm trying to perform a simple count with knex (since it seems to not be supported by bookshelf yet). The following code is working: bookshelf.knex('hosts').count('id').then(function(total) { res.send({ meta: { total:…
Pedro
  • 3,511
  • 2
  • 26
  • 31
22
votes
6 answers

How to loop over rows after .fetchAll Bookshelf js + knex js?

I have a MySQL Database which I need to query from node.js I am using bookshelf and knex for this. I want to get the contents of a table - I have defined a table in my model.js file. I am attempting the query like this: //select * from…
A.D
  • 1,480
  • 2
  • 18
  • 33
22
votes
6 answers

How to do knex.js migrations?

I'm still not sure how to do my migrations with knex. Here is what I have so far. It works on up, but down gives me FK constraint error even though foreign_key_checks = 0. exports.up = function(knex, Promise) { return Promise.all([ …
Eric Shell
  • 913
  • 2
  • 8
  • 19
21
votes
3 answers

How to ensure the Knex connection in nodejs

I am using NodeJS, Express, and MySQL for my project and want to use Bookshelf ORM with it. Bookshelf uses Knex for querying, modeling, and suggests to setup the DB connection through Knex(http://bookshelfjs.org/#installation). I am having trouble…
Raghav Garg
  • 3,601
  • 2
  • 23
  • 32
21
votes
2 answers

With knexjs, how do I compare two columns in the .where() function?

Using knexjs only (no bookshelf) I would like to do something like the following query: select * from table1 where column1 < column2 However, when I do this: .table("table1").select().where("column1", "<", "column2") The SQL that knexjs generates…
daveashworth
  • 580
  • 5
  • 15
21
votes
1 answer

Knex.js Schema: Multicolumn Index

Is there a way to specify a multicolumn index in the Knex.js schema? Or must one drop to raw and do an alter table?
Daniel
  • 38,041
  • 11
  • 92
  • 73
20
votes
9 answers

Knex required configuration option 'client' is missing error

Here are my files. knexfile.js require('dotenv').config(); module.exports = { development: { client: process.env.DB_CLIENT, connection: { host: process.env.DB_HOST, user: process.env.DB_USER, …
Sujin Shrestha
  • 1,203
  • 2
  • 13
  • 23
20
votes
9 answers

Removing Migrations With Knex.js In My Node.js Application

I am trying to get knex working in my node.js application. I was following a tutorial and at some point created a table but could not repeat the process. I removed the table and deleted all the migrations folders. At tis point I started over but…
wuno
  • 9,547
  • 19
  • 96
  • 180
20
votes
6 answers

create/drop database task for gulp/knex

I have an Express.js web applications which uses Knex.js as the SQL query builder and migrations engine. While Knex.js has methods for creating, dropping, and altering tables, it doesn't have methods for creating/dropping the database itself. I was…
ifeins
  • 869
  • 1
  • 9
  • 20
19
votes
4 answers

Adding a column to an existing table in Node.js & Knex

I'm using Node.js and Knex to build a service for my router. However, I can't figure out how to add a column to an existing table, any help would be appreciated. Also, I'm using PostgreSQL, but I don't think that matters for the question. So, this…
ezg
  • 715
  • 2
  • 7
  • 20
19
votes
3 answers

How do I add a Composite primary key with Knex.js?

I have 2 tables. 1 called events with an event ID and another table called tickets which I want to have primary keys of event ID and ticket ID. I am also using a PostgreSQL database. At the moment ,I have it as a foreign key but would like to have…
Coder1234
  • 389
  • 1
  • 3
  • 9
19
votes
3 answers

Why doesn't Jest complete the async operation(s) in this Node test?

I have the following simple test setup: test('what did I do to deserve this', async () => { expect.assertions(1) const data = await fetchData() // or fetchData2 expect(data).toBe('peanut butter') }) async function fetchData () { return…
blu
  • 12,905
  • 20
  • 70
  • 106
19
votes
1 answer

knex: what is the appropriate way to create an array from results?

I have an endpoint that joins the user and user_emails table as a one-to-many relationship (postgresql). It look as follows. router.get('/', function (req, res, next) { db.select('users.id', 'users.name', 'user_emails.address') .from('users') …
corvid
  • 10,733
  • 11
  • 61
  • 130
19
votes
2 answers

Knex: Create migration with FOREIGN KEY

I tried the code in the link to create FK: how to do knex.js migration I got an error on line: table.bigInteger('AddressId') .unsigned() .index() .inTable('Address') .references('id'); The error: TypeError: Object # has no…
Lei
  • 189
  • 1
  • 1
  • 4
19
votes
3 answers

knex.js - debug only SQL

Is there any way to display only SQL queries on console when debugging mode is on? I want to reduce the amount of informations which is displayed. Thanks for the help ;)
rizidoro
  • 13,073
  • 18
  • 59
  • 86