knex.js

Mocha testing PostgreSQL with Knex is giving me a MigrationLocked error

三世轮回 提交于 2019-12-04 14:54:25
I have got a local development environment working for Node/PostgreSQL/Knex in the sense that I can post to a development database on my machine using an API. I am now trying to create tests for this functionality but am getting an error. Here's my config: //knexfile.js module.exports = { development: { client: 'pg', connection: { host: '127.0.0.1', user: 'dbUser', password: 'dbpword', port: 5432, database: 'example-name' }, migrations: { directory: __dirname + '/db/migrations' }, seeds: { directory: __dirname + '/db/seeds/development' } }, } //db.js const config = require('../knexfile.js');

How to do knex.js migrations?

邮差的信 提交于 2019-12-02 22:25:36
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([ knex.raw('SET foreign_key_checks = 0;'), /* CREATE Member table */ knex.schema.createTable('Member', function (table) { table.bigIncrements('id').primary().unsigned(); table.string('email',50); table.string('password'); /* CREATE FKS */ table.bigInteger('ReferralId').unsigned().index(); table.bigInteger('AddressId').unsigned().index().inTable('Address').references(

Database query in loop returns only an empty array

六眼飞鱼酱① 提交于 2019-12-02 16:19:25
问题 'requestingUserIds' is an array, with different ids. Each id belongs to a username in the table 'users'. That's why I want to loop, for each id in the array 'requestingUserIds', the corresponding username into the array 'requestingUserUsernames' and finally log the full array (requestingUserUsernames) into the console. But if I do it outside the then function, only an empty array is output, probably the array I initiated in the beginning. When i log the array 'requestingUserUsernames' inside

NodeJS/Knex Creating Json Response

本小妞迷上赌 提交于 2019-12-02 09:42:11
问题 I'm currently using NodeJS with knex (Postgresql) for database stuff. Problem: Imagine the following two tables in database: Table 1 PROJECT id (pk) name Table 2 EMPLOYEE id (pk) name project_id (fk) I want to create a json-response to the user that looks like the following: { projects: [ { id: 1, name: 'emxample 1', employees: [ { id: 1, name: 'example 1' }, { id: 2, name: 'example 2' } ] } ] } and so on. Making a query like: let query = knex('project').select('project.*', 'employee.*').join

Make KnexJS Transactions work with async/await

旧时模样 提交于 2019-12-02 07:22:15
问题 I'm trying to make transactions work with async/await and knexjs but to no avail. The code (snippet is for the sake of shortening the post): const updateOrder = (req, res, db, logger) => { let { status, trx_id, orNumber, returnReason } = req.body; const updateStatus = () => { return db('cart') .returning('*') .where('trx_id', '=', trx_id) .update({ status: status, or_num: orNumber, return_reason: returnReason }); } const updateDate = () => { return db('cart') .returning('*') .where('trx_id',

Make KnexJS Transactions work with async/await

☆樱花仙子☆ 提交于 2019-12-02 05:59:16
I'm trying to make transactions work with async/await and knexjs but to no avail. The code (snippet is for the sake of shortening the post): const updateOrder = (req, res, db, logger) => { let { status, trx_id, orNumber, returnReason } = req.body; const updateStatus = () => { return db('cart') .returning('*') .where('trx_id', '=', trx_id) .update({ status: status, or_num: orNumber, return_reason: returnReason }); } const updateDate = () => { return db('cart') .returning('*') .where('trx_id', '=', trx_id) .update({ date_purchased: new Date() }); } const selectItems = (order) => { return db

WHERE statement with duplicate column names over JOIN - PostgreSQL

此生再无相见时 提交于 2019-12-02 04:42:58
问题 I am trying to join two tables, a plans table and a plan_details table. Below are two examples of what the tables look like. PLANS Table +---------+------+-----------+ | user_id | plan | is_active | +---------+------+-----------+ | 1 | 10 | true | | 1 | 11 | false | | 2 | 11 | true | PLAN_DETAILS Table +---------+------+-------+-----------+ | plan_id | cost | price | is_active | +---------+------+-------+-----------+ | 10 | 19 | 199 | true | | 11 | 13 | 149 | true | I only want to only pull

NodeJS/Knex Creating Json Response

橙三吉。 提交于 2019-12-02 03:45:18
I'm currently using NodeJS with knex (Postgresql) for database stuff. Problem: Imagine the following two tables in database: Table 1 PROJECT id (pk) name Table 2 EMPLOYEE id (pk) name project_id (fk) I want to create a json-response to the user that looks like the following: { projects: [ { id: 1, name: 'emxample 1', employees: [ { id: 1, name: 'example 1' }, { id: 2, name: 'example 2' } ] } ] } and so on. Making a query like: let query = knex('project').select('project.*', 'employee.*').join('employee', 'employee.project_id', '=', 'project.id'); query.then((projects) => { res.json(projects);

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

廉价感情. 提交于 2019-12-01 17:41:20
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', 8) However, in order for our inventory application to work in a multi-user environment we need the qtyonhand value to add or subtract with what's actually in the database at that moment rather than passing a value that may be stale by the time the update statement is executed. Here are 2 different ways knex('item').increment('qtyonhand').where('rowid',8) or knex('item

Tracking DB querying time - Bookshelf/knex

ぃ、小莉子 提交于 2019-12-01 12:38:35
I would like to monitor the time taken by a query on my API's db. I so created the following function, using bookshelf-signals, a Bookshelf plugin. : bookshelf.on('fetching', () => { server.app.fetching = new Date().valueOf(); }); bookshelf.on('counting', () => { server.app.fetching = new Date().valueOf(); }); bookshelf.on('fetched', () => { server.statsd.gauge('db_query', new Date().valueOf() - server.app.fetching); }); ... so that I can retrieve the time just before and just after a fetch/count; I did the same with deleting-deleted and saving-saved. What I think I fail to understand is when