knex.js

knex like query dynamically add

╄→尐↘猪︶ㄣ 提交于 2019-12-06 12:11:53
问题 `let add_query = ".andWhere('company_name', 'like','%marker%')"; knex('franchisee').where('franchisee_name', 'like', '%jackmarker%')${add_query} .then((resp) => { console.log("resp >>",resp) })` Hear i want to execute like query using knex npm in node js. I take variable add_query using this variable want to make knex query dynamically. Like this query- `knex('franchisee').where('franchisee_name', 'like','%jackmarker%').andWhere('company_name', 'like','%marker%') .then((resp) => { console.log

Mocha testing PostgreSQL with Knex is giving me a MigrationLocked error

醉酒当歌 提交于 2019-12-06 09:02:16
问题 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: {

How to add multiple rows using “Insert … ON DUPLICATE KEY UPDATE” using knex

£可爱£侵袭症+ 提交于 2019-12-06 07:50:19
so I've been playing around with knex lately, however I found myself on a situation where I don't know what to do anymore. so I have this query: knex.raw("INSERT INTO tablename (`col1`, `col2`, `col3`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE col2 = VALUES(`col2`)", [ ['val1', 'hello', 'world'], ['val2', 'ohayo', 'minasan'], ]); And for some reasons It throws me an error Expected 2 bindings, saw 3 . I tried making it: knex.raw("INSERT INTO tablename (`col1`, `col2`, `col3`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE col2 = VALUES(`col2`)", ['val1', 'hello', 'world'], ['val2', 'ohayo', 'minasan']

Can we always fetch date column as string (varchar) with knex and postgres?

元气小坏坏 提交于 2019-12-05 20:51:04
I have a column in postgres database with type date . It is a column like birthday, which is just a date and does not need to have a time part. When fetching this column with knex, the result is a javascript Date object. It is presumably doing new Date(row.birthday) , and this is the result that is sent to the client. The problem now is that the value that the client receives is in the standard ISO 8601 format with the time part and the Z . When the client tries to create a new Date object from this string, the client may have an erroneous date value based on where the client is located. For

Knex Migration Postgres Heroku - Error: Unable to Acquire Connection

回眸只為那壹抹淺笑 提交于 2019-12-05 19:59:59
I am trying to run my first migration which creates a single table in a heroku postgres database. When I try to run knex migrate:latest --env development I receive the error Error: Unable to acquire a connection . Things I've tried: adding ?ssl=true to the end of my connection string stored in process.env.LISTINGS_DB_URL as I'm aware this is sometimes a requirement to connect with heroku setting the env variable PGSSLMODE=require I also stumbled across this article where someone has commented that knex will not accept keys based on environment. However, I'm attempting to follow along with this

Knexjs returning mysql timestamp, datetime columns as Javascript Date object

大兔子大兔子 提交于 2019-12-05 16:56:49
i am using knexjs i insert data in the format YYYY-MM-DD HH:mm:ss e.g 2017-07-14 15:00:00 and after saving when the data is fetched the datetime column values are returned as javasript Date object. i want to return those object in the format YYYY-MM-DD HH:mm:ss but it returning in the format YYYY-MM-DDTHH:mm:ss.000Z e.g 2017-06-23T06:44:44.000Z . i am returning them by iterating and converting them manually. i was wondering if there is another way to do it like in mysql driver or knexjs configuration. currently my knexjs configuration is this. var connection = require('knex')({ client: 'mysql'

How can I retrieve unique values from a column with Knex.js?

爷,独闯天下 提交于 2019-12-05 14:19:03
I use Knex.js to communicate to Postgres database. I have rows in a table with a column named 'state' which represents a US state. How can I retrieve all unique values from this column? You are probably looking for knex.distinct + knex.pluck combo. knex .distinct() .from('tablename') .pluck('state') .then(states => { console.log(states) }) 来源: https://stackoverflow.com/questions/47263583/how-can-i-retrieve-unique-values-from-a-column-with-knex-js

KnexJS Migration With Associated Seed Data

独自空忆成欢 提交于 2019-12-05 14:08:56
I'm, in the process of learning BookshelfJS/KnexJS (switching from SequelizeJS), and I'm running into an issue with importing data into multiple tables that were created via the migrations feature within KnexJS. There's 4 tables: servers operating_systems applications applications_servers With the following constraints: servers . operating_system_id references operating_systems . id applications_servers . server_id references servers . id applications_servers . application_id references applications . id The tables get created just fine when I run knex migrate:latest --env development servers

How do you chain queries in order using knex.js?

£可爱£侵袭症+ 提交于 2019-12-05 05:02:43
I'm having some trouble understanding how the promises in Knex.js work (uses Bluebird.js for promises). I'm trying to do something pretty simple, execute different insert statements one after another in order, but I haven't been able to get it to work. Here's the code I have so far, which is meant to execute an insert on the authentication_type table, then an insert on the user_table, and then an insert on the category table. // Import database connection var knex = require('./db-connection.js'); // Add a row to authentication_type table so that user's can be created function add

knex like query dynamically add

核能气质少年 提交于 2019-12-04 17:36:51
`let add_query = ".andWhere('company_name', 'like','%marker%')"; knex('franchisee').where('franchisee_name', 'like', '%jackmarker%')${add_query} .then((resp) => { console.log("resp >>",resp) })` Hear i want to execute like query using knex npm in node js. I take variable add_query using this variable want to make knex query dynamically. Like this query- `knex('franchisee').where('franchisee_name', 'like','%jackmarker%').andWhere('company_name', 'like','%marker%') .then((resp) => { console.log("resp >>",resp) })` You can add parts of queries like this: const getResults = (options) => { let