knex/knex
Fork: 2132 Star: 19412 (更新于 2024-12-02 14:46:48)
license: MIT
Language: JavaScript .
A query builder for PostgreSQL, MySQL, CockroachDB, SQL Server, SQLite3 and Oracle, designed to be flexible, portable, and fun to use.
最后发布版本: 3.1.0 ( 2023-12-08 07:17:57)
knex.js
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:
- transactions
- connection pooling
- streaming queries
- both a promise and callback API
- a thorough test suite
Node.js versions 12+ are supported.
- Take a look at the full documentation to get started!
- Browse the list of plugins and tools built for knex
- Check out our recipes wiki to search for solutions to some specific problems
- In case of upgrading from an older version, see migration guide
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:
- https://github.com/Vincit/objection.js
- https://github.com/mikro-orm/mikro-orm
- https://bookshelfjs.org
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();
最近版本更新:(数据更新于 2024-09-09 10:30:42)
2023-12-08 07:17:57 3.1.0
2023-10-06 20:23:40 3.0.1
2023-10-06 14:56:38 3.0.0
2023-07-13 05:26:26 2.5.1
2023-07-09 05:04:46 2.5.0
2023-03-29 19:15:20 2.4.2
2023-03-29 19:11:39 2.4.1
2023-01-06 23:45:16 2.4.0
2022-09-02 05:17:24 2.3.0
2022-07-19 05:26:26 2.2.0
主题(topics):
javascript, knex, mysql, postgresql, sql, sqlite3
knex/knex同语言 JavaScript最近更新仓库
2024-12-21 12:14:02 layui/layui
2024-12-21 08:27:48 gethomepage/homepage
2024-12-21 03:44:01 emberjs/ember.js
2024-12-20 02:57:09 nodejs/node
2024-12-19 17:54:54 MHSanaei/3x-ui
2024-12-14 02:13:44 bigskysoftware/htmx