当前位置: 首页 > 软件库 > 数据库相关 > >

knex

授权协议 MIT License
开发语言 C/C++
所属分类 数据库相关
软件类型 开源软件
地区 不详
投 递 者 雍河
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

knex.js

npm downloads

A SQL query builder that is flexible, portable, and fun to use!

A batteries-included, multi-dialect (MSSQL, MySQL, PostgreSQL, SQLite3, Oracle (including Oracle Wallet Authentication)) query builder forNode.js, featuring:

Node.js versions 10+ are supported.

You can report bugs and discuss features on the GitHub issues page or send tweets to @kibertoad.

For support and questions, join our Gitter channel.

For knex-based Object Relational Mapper, see:

To see the SQL that Knex will generate for a given query, you can use Knex Query Lab

Examples

We have several examples on the website. Here is the first one to get you started:

const knex = require('knex')({
  client: 'sqlite3',
  connection: {
    filename: './data.db',
  },
});

try {

  // Create a table
  await knex.schema
    .createTable('users', table => {
      table.increments('id');
      table.string('user_name');
    })
    // ...and another
    .createTable('accounts', table => {
      table.increments('id');
      table.string('account_name');
      table
        .integer('user_id')
        .unsigned()
        .references('users.id');
    })

  // Then query the table...
  const insertedRows = await knex('users').insert({ user_name: 'Tim' })

  // ...and using the insert id, insert into the other table.
  await knex('accounts').insert({ account_name: 'knex', user_id: insertedRows[0] })

  // Query both of the rows.
  const selectedRows = await knex('users')
    .join('accounts', 'users.id', 'accounts.user_id')
    .select('users.user_name as user', 'accounts.account_name as account')

  // map over the results
  const enrichedRows = selectedRows.map(row => ({ ...row, active: true }))

  // Finally, add a catch statement
} catch(e) {
  console.error(e);
};

TypeScript example

import { Knex, knex } from 'knex'

interface User {
  id: number;
  age: number;
  name: string;
  active: boolean;
  departmentId: number;
}

const config: Knex.Config = {
  client: 'sqlite3',
  connection: {
    filename: './data.db',
  },
};

const knexInstance = knex(config);

try {
  const users = await knex<User>('users').select('id', 'age');
} catch (err) {
  // error handling
}

Usage as ESM module

If you are launching your Node application with --experimental-modules, knex.mjs should be picked up automatically and named ESM import should work out-of-the-box.Otherwise, if you want to use named imports, you'll have to import knex like this:

import { knex } from 'knex/knex.mjs'

You can also just do the default import:

import knex from 'knex'

If you are not using TypeScript and would like the IntelliSense of your IDE to work correctly, it is recommended to set the type explicitly:

/**
 * @type {Knex}
 */
const database = knex({
    client: 'mysql',
    connection: {
      host : '127.0.0.1',
      user : 'your_database_user',
      password : 'your_database_password',
      database : 'myapp_test'
    }
  });
database.migrate.latest();
  • knex.js是bookshelf框架的基础,其核心是query builder。这篇文章参考自Knex.js官网,翻译并总结了该框架的方法。 0 安装 #以PostgreSql为例 sudo npm install knex --save sudo npm install pg --save 0 1 2 #以PostgreSql为例 sudonpminstallknex--save sudonp

  • //建表 knex migrate:make steup 转载于:https://www.cnblogs.com/mjhelloworld/p/6958521.html

  • 创建knex对象 注:页面引用knex.min.js后,才能创建knex 创建不含数据连接的knex对象 var myknex = new knex.Client({ client: 'mysql', }) 创建含有数据连接的knex对象(待验证) var pg = new knex.Client({ client: 'websql', connection: { h

  • knex是nodejs中访问数据库的一个模块,支持多种数据库,并且使用knex可以使用js代码维护数据库表,官网: 本文讲创建数据表的部分,关于配置和迁移配置请参见官方文档: 一、创建迁移文件 knex migrate:make users 其中 users = 表名,执行该命令后将会在配置的迁移文件目录生成文件:20201203093439_users.js 打开文件,内容如下: exports

  • 1. 查询 knex('table').select() 2. 带条件查询 knex('table').where('id', 1).select() knex('table').where({'id': 1}).select() 3. 新增 knex('table').insert({'name': '名字'}) 4. 更新(更新所有数据) knex('table').update('name

  • nodejs学习 1、express & nodejs & mysql 新建项目 2、nodejs尝试登陆接口 3、nodejs的前端项目搭建以及登陆接口开发 4、前端上传图片formdata格式,后端接口处理 5、vue+nodejs双表联动 6、nodejs插件knex & 日志打印 7、vue3+nodejs基于RSA加密的身份认证 前端代码地址(不完全功能) 后端代码地址(不完全功能) k

 相关资料
  • 我有一个向Vue前端提供数据的Node/Express API,现在正在添加用于身份验证和授权的Passport。我有一个createUser函数,它成功地添加了用户,但在这样做时函数挂起。我肯定这只是我错过的一件愚蠢的事情,比如没有调用(尽管我已经调用了),我希望有一个头脑更清醒的人看一下。 //authrotes.js //authController.js //auth/_helpers.j

  • 问题内容: 我得到的输出正确,实际上,这两个操作被视为一个事务单元;如果一个失败,那么两个都会失败。 在此代码示例中:我正在进行以下交易 (1)插入(2)更新 我的处理方式是将数据库操作嵌套在.then内。我的问题是该代码是否偶然正确?我是Promise和Knex的新手。 这行得通,但是我觉得我仍然在做错事。寻找评论。 问题答案: 您需要从内部查询中返回一个Promise,以便将外链与此链接在一起

  • 问题内容: 我想在查询中添加一个子句,但 有条件地 。具体来说,我只希望在URL中传递单独的querystring参数时才添加它。这可能吗?如果可以,我将如何去做呢? 问题答案: 您可以将查询存储在变量中,应用条件where子句,然后执行它,如下所示:

  • 问题内容: 我对Node相对较新,并且正在使用knex和书架进行项目。我在对代码进行单元测试时遇到了一些麻烦,但是我不确定自己做错了什么。 基本上,我有一个看起来像这样的模型(称为VorcuProduct): 还有一个函数,如果数据库中不存在VorcuProduct,它将保存它。非常简单。执行此操作的函数如下所示: 哪种方法可以在不影响数据库的情况下进行测试?我是否需要模拟以返回模型或未定义模型(

  • 问题内容: 我有一个SQL查询,该查询两次引用同一张表,因此我需要将该表别名为两个单独的别名。我不太清楚如何与Knex一起编写。 有一个“单词”表和一个“用户”表。Words表具有两个外键,即“ author_id”和“ winner_id”,引用了Users表的“ id”列。 这是我要在Knex中编写的SQL: 我对如何在Knex中做到这一点有些迷惑。我的第一次尝试不涉及别名,因此出现了“表使用

  • 我正在使用Knex.js来处理与数据库的连接。我正在尝试防止连接池破坏空闲的连接。 我的配置是这样的 然而,我仍然不断得到 在一段时间不活动后。 据我所知,当足够的时间过去时,应该将连接从池中丢弃。因此,如果连接有一段时间没有使用(这就是我的情况),池中将没有连接,我尝试的第一次调用将失败,并出现给定错误。随后的调用顺利进行(直到新的超时) 我的问题是——如何防止这种情况? 编辑 在我的应用空闲一

  • 我不确定似乎是什么问题,但这种膝盖迁移失败了。尽管我是编写迁移的新手,但我坚信此迁移文件是正确的。生成的错误如下 代码如下。最初,这些迁移函数在单独的文件中,我认为它失败了,因为文件没有同步执行,这导致我编写了一个文件。我不确定这是否有帮助,但是当我删除包含外键引用(UserRoles、RolePer的、令牌)的表的代码时,其余的似乎都在工作。

  • 我似乎无法用knex迁移数据库。在up命令中,它失败。 我得到一个错误。 我发誓它昨天还在工作。我试着删除并重建数据库。运气不好。怎么了?我该如何解决这个问题? 在knex消息源中似乎没有任何明显的挖掘。