knex.js

Automatically stringifying object when inserting to a MySQL JSON column with knex

血红的双手。 提交于 2019-12-13 03:58:36
问题 Let's jump straight to an example code: create table test_json_table ( data json not null ); I can insert to the table like this: const columns = { data: "{ some_json: 123 }" }; // notice that the data column is passed as string await knex('test_json_table').insert(columns); And get data from the table like this: await knex('test_json_table').select(); // returns: // [ // { data: { some_json: 123 } } // notice that the data is returned as parsed JavaScript object (not a string) // ] When

SQLITE_RANGE: bind or column out of range for INSERT statement

送分小仙女□ 提交于 2019-12-13 02:49:57
问题 I want to insert rows into a SQLite3 table using the knex.raw method. Unfortunately I get a 'SQLITE_RANGE' error, which makes my test fail. I have verified the bindings passed to the raw query in the following fashion: They respect the order of the INSERT statement They respect the specified column types They respect the number of bindings requested in the raw query Beyond that I have looked online, but couldn't find a solution to my issue. Below are the details of the operation attempted:

How should I initialize my schema via node/npm/knex?

你。 提交于 2019-12-12 04:16:09
问题 Every new dev in our project is asked to do: To create the database for the first time, run: $ mysql -u root -p < sql/schema.sql We're using knex internally in the project. How can we use it to create the schema instead or manual mysql commands? How do we hook up npm for that? Should we do it on npm install , or npm init ? How do we hook up those build targets to knex ? 回答1: Generally speaking, a shell script may satisfy your needs: create database knex migrate latest knex run seeds npm

knex select result return to a variable

拟墨画扇 提交于 2019-12-11 17:25:40
问题 I need to get knex select query result to a variable. function getUserPlanDetailsWithOutCb(user_id) { var dataArr =[]; knex('user_plans').select('*').where({ 'user_id': user_id }).then(function(result) { result.forEach(function(value) { dataArr.push(value) }); //return dataArr; }); return dataArr; } var result = getUserPlanDetailsWithOutCb(12); I have tried return value outside and inside of the call back in knex . For above code i got the result as [ ] For second one (return inside callback)

NodeJS - Cannot set Headers after they are sent to the client

久未见 提交于 2019-12-11 14:34:50
问题 So I've searched around and found out that to fix the said issue, I have to return after sending a response. But my problem is, even though I have return, I still have the error. const dbEditCourse = (req, res, db, logger) => { let { origCourse, code, description, type } = req.body; if (!code || !description || !type) { res.json({ haveEmpty: true }); return; } db.transaction((trx) => { db.select('*').from('course_strand').where('code', '=', code) .then(data => { if (data[0]) { //error happens

Unhanded rejection error :Transaction query already complete - knex, express.js

笑着哭i 提交于 2019-12-11 08:27:45
问题 I was trying to check for a value in a table first, and if it exists, delete a row in another table and insert this new data into that table. I used a transaction with a select, del(), and a insert command db.transaction(trx => { return trx('users') .where({ username: user.username }) .select('username') .returning('username') .then(retData => { retUserName = retData[0]; db('profile') .where({ username: user.username }) .del() .then(retData => { return trx .insert(profileData) .into('profile'

Knex Seed: insert out of order from promise

泄露秘密 提交于 2019-12-11 07:29:35
问题 I'm trying to seed my knex data into my postgres database. However, I noticed that inserts from one Promise.all([...]) sometimes gets inserted into the previous or latter Promise.all([..]) table. exports.seed = function(knex, Promise) { return Promise.all([ knex('users').del(), knex('songs').del(), ]).then(() => { return Promise.all([ knex('users').insert({username: 'u1', password: '1', access: 'regular'}), knex('users').insert({username: 'u2', password: '1', access: 'regular'}), knex('users'

How to return a plain value from a Knex / Postgresql query?

有些话、适合烂在心里 提交于 2019-12-11 06:45:37
问题 I'm trying to return a simple, scalar string value from a Postgres DB using Knex. So far, everything I do returns a JSON object with a key (the column name) and the value, so I have to reach into the object to get the value. If I return multiple rows, then I get multiple JSON objects, each one repeating the key. I could be returning multiple columns, in which case each row would at least need to be an array. I'm not looking for a special case where specifying a single column returns the value

Node.js - Converting a SQL query into Knex.js

老子叫甜甜 提交于 2019-12-11 06:05:45
问题 I have a SQL query (Postgres) containing several join s that I'm having trouble converting into a single Knex.js statement. Here's the SQL query: SELECT User.id, User.name, User.email, Role.name AS r_name, UserPofile.id AS p_id, UserPofile.date_of_birth AS p_dob, AuthToken.id AS at_id, AuthToken.token AS at_token, AuthToken.platform AS at_platform FROM public."user" User LEFT JOIN public."user_role" UserRole ON User.id = UserRole.user_id LEFT JOIN public."role" Role ON UserRole.role_id = Role

Create a nested return model with Knex.js

佐手、 提交于 2019-12-11 03:51:35
问题 I'm using Knex.js to query a MySQL database in a Hapi.js route. The following code works but requires a nested query: { path: '/recipes', method: 'GET', handler: (req, res) => { const getOperation = Knex.from('recipes') // .innerJoin('ingredients', 'recipes.guid', 'ingredients.recipe') .select() .orderBy('rating', 'desc') .limit(10) .then((recipes) => { if (!recipes || recipes.length === 0) { res({ error: true, errMessage: 'no recipes found' }); } const recipeGuids = recipes.map(recipe =>