knex.js

MySQL与Node.js

萝らか妹 提交于 2020-02-27 20:55:13
我刚刚开始接触Node.js。 我来自PHP背景,因此我习惯于使用MySQL满足所有数据库需求。 如何在Node.js中使用MySQL? #1楼 通过安装库连接mysql数据库。 在这里,选择了稳定且易于使用的node-mysql模块。 npm install mysql@2.0.0-alpha2 var http = require('http'), mysql = require('mysql'); var sqlInfo = { host: 'localhost', user: 'root', password: 'urpass', database: 'dbname' } client = mysql.createConnection(sqlInfo); client.connect(); 对于NodeJS mysql连接和查询示例 #2楼 由于这是旧线程,因此只需添加更新: 要安装MySQL node.js驱动程序: 如果仅 npm install mysql ,则需要与运行服务器所在的目录相同。 我建议按照以下示例之一进行操作: 对于全局安装: npm install -g mysql 对于本地安装: 1-将其添加到依赖项的 package.json 中: "dependencies": { "mysql": "~2.3.2", ... 2-运行 npm install

Properly batch nested promises in Node

 ̄綄美尐妖づ 提交于 2020-02-26 01:59:10
问题 I'm running a knex seed in Node and need to batch an additional query to my database due to restrictions on my server. I'm starting to get the hang of promises and async/await, but I'm having trouble getting it to work at several levels deep (what's throwing me off in particular at this point is that it seems to interfere with the batching in a way that I can't quite make sense of). My seed file looks like this: exports.seed = async function(knex) { const fs = require('fs'); const _ = require

Properly batch nested promises in Node

元气小坏坏 提交于 2020-02-26 01:57:32
问题 I'm running a knex seed in Node and need to batch an additional query to my database due to restrictions on my server. I'm starting to get the hang of promises and async/await, but I'm having trouble getting it to work at several levels deep (what's throwing me off in particular at this point is that it seems to interfere with the batching in a way that I can't quite make sense of). My seed file looks like this: exports.seed = async function(knex) { const fs = require('fs'); const _ = require

Is knex.where prone to sql injection attacks?

╄→尐↘猪︶ㄣ 提交于 2020-02-04 22:59:27
问题 This is a follow up question to https://stackoverflow.com/a/50337990/1370984 . It mentions knex('table').where('description', 'like', '%${term}%') as prone to sql injection attacks. Even a comment mentions the first case as prone to injection attacks. Yet the reference provided never mentions .where being prone to injection attacks. Is this a mistake? Why would knex allow .where to be prone to injection attacks but not .whereRaw('description like \'%??%\'', [term]) . Aren't the arguments

Is knex.where prone to sql injection attacks?

拜拜、爱过 提交于 2020-02-04 22:58:27
问题 This is a follow up question to https://stackoverflow.com/a/50337990/1370984 . It mentions knex('table').where('description', 'like', '%${term}%') as prone to sql injection attacks. Even a comment mentions the first case as prone to injection attacks. Yet the reference provided never mentions .where being prone to injection attacks. Is this a mistake? Why would knex allow .where to be prone to injection attacks but not .whereRaw('description like \'%??%\'', [term]) . Aren't the arguments

Raw Sql statement to group by column with different strings for the same name

柔情痞子 提交于 2020-01-25 00:19:05
问题 Fairly new to creating a more complex sql statement, I'm trying to do a group by a name where the name can come in different forms. for example, name can be "Kane, Patrick", "P.Kane, Patrick", "Kane, Patrick* what i have so far below which queries around 7000 results: SELECT SUM(games_played) as games_played, SUM(goals) as goals, SUM(points) as points, player_name FROM player_stats GROUP BY player_name; example resulting json [ {games_played: 123, goals: 12, points: 40, player_name: "Kane,

How to write parameterized sql query to prevent SQL injection?

浪尽此生 提交于 2020-01-24 05:25:29
问题 I initially discovered that this was an issue when I tried to search for terms that had been prepended with a hashtag, which it turns out is a comment delimiter in SQL. The search returned nothing, because it ignored the #term that came after the hashtag. So now I'm having trouble finding the proper way of escaping the user's input. It seems to me that this would both solve the hashtag issue and also address the much larger problem, SQL injection. Here is the snippet I am working with

Where can I find documentation for the types of knex errors?

こ雲淡風輕ζ 提交于 2020-01-17 02:19:27
问题 I've scoured the internet but it seems that I can't find documentation for the different types of Knex errors. I would like to know these so I can implement proper error handling for my project. Where can I find this? They briefly mention the query error object here but no further depth is given. Am I missing something? It seems basic to me that they should have this well-documented. 回答1: What @Mikael said. It's a passthrough. For SQLite there are lists of DB errors here and here. The db

How do I escape % in Knex where like query?

▼魔方 西西 提交于 2020-01-11 12:06:30
问题 I'm using knex to generate my SQL queries. In knex documentation, it shows this knex('users').where('columnName', 'like', '%rowlikeme%') Now in my application, I did this: function search(term) { term = "%" + term + "%"; knex('table').where('description', 'like', term); // ... } How can I escape % so that it searches for % as part of the term as well? Thanks. 回答1: For this case I use rather string interpolation from es6 (safe version) knex('table').where('description', 'like', `%${term}%`) or

Bookshelf.js save one to many relation

老子叫甜甜 提交于 2020-01-06 14:45:40
问题 I am trying to save a one to many relation My models are Foo foo = bookshelf.Model.extend({ tableName: 'foo', bar: function () { return this.hasMany('bar', 'barId'); } Bar bar = bookshelf.Model.extend({ tableName: 'bar', foo: function () { return this.belongsTo('foo', 'barId'); } What I am trying to do var Foo = { id: 7 } new Bar({ blah: blah.val }) .foo() .attach(foo); The error I am getting (intermediate value).foo().attach is not a function Any help will be appreciated. 回答1: I don't think