项目作者: tgriesser

项目描述 :
PostgreSQL,MySQL和SQLite3的查询构建器,设计灵活,便携且易于使用。
高级语言: JavaScript
项目地址: git://github.com/tgriesser/knex.git
创建时间: 2012-12-29T05:18:25Z
项目社区:https://github.com/tgriesser/knex

开源协议:MIT License

下载


knex.js

npm version
npm downloads

Coverage Status
Dependencies Status
Gitter chat

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

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

Node.js versions 12+ 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:

  1. const knex = require('knex')({
  2. client: 'sqlite3',
  3. connection: {
  4. filename: './data.db',
  5. },
  6. });
  7. try {
  8. // Create a table
  9. await knex.schema
  10. .createTable('users', (table) => {
  11. table.increments('id');
  12. table.string('user_name');
  13. })
  14. // ...and another
  15. .createTable('accounts', (table) => {
  16. table.increments('id');
  17. table.string('account_name');
  18. table.integer('user_id').unsigned().references('users.id');
  19. });
  20. // Then query the table...
  21. const insertedRows = await knex('users').insert({ user_name: 'Tim' });
  22. // ...and using the insert id, insert into the other table.
  23. await knex('accounts').insert({
  24. account_name: 'knex',
  25. user_id: insertedRows[0],
  26. });
  27. // Query both of the rows.
  28. const selectedRows = await knex('users')
  29. .join('accounts', 'users.id', 'accounts.user_id')
  30. .select('users.user_name as user', 'accounts.account_name as account');
  31. // map over the results
  32. const enrichedRows = selectedRows.map((row) => ({ ...row, active: true }));
  33. // Finally, add a catch statement
  34. } catch (e) {
  35. console.error(e);
  36. }

TypeScript example

  1. import { Knex, knex } from 'knex';
  2. interface User {
  3. id: number;
  4. age: number;
  5. name: string;
  6. active: boolean;
  7. departmentId: number;
  8. }
  9. const config: Knex.Config = {
  10. client: 'sqlite3',
  11. connection: {
  12. filename: './data.db',
  13. },
  14. };
  15. const knexInstance = knex(config);
  16. try {
  17. const users = await knex<User>('users').select('id', 'age');
  18. } catch (err) {
  19. // error handling
  20. }

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:

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

You can also just do the default import:

  1. 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:

  1. /**
  2. * @type {Knex}
  3. */
  4. const database = knex({
  5. client: 'mysql',
  6. connection: {
  7. host: '127.0.0.1',
  8. user: 'your_database_user',
  9. password: 'your_database_password',
  10. database: 'myapp_test',
  11. },
  12. });
  13. database.migrate.latest();