Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/knexPublic

A query builder for PostgreSQL, MySQL, CockroachDB, SQL Server, SQLite3 and Oracle, designed to be flexible, portable, and fun to use.

License

NotificationsYou must be signed in to change notification settings

knex/knex

Repository files navigation

npm versionnpm downloadsCoverage StatusDependencies StatusGitter chat

A SQL query builder that isflexible,portable, andfun to use!

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

Node.js versions 12+ are supported.

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

For support and questions, join ourGitter channel.

For knex-based Object Relational Mapper, see:

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

Examples

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

constknex=require('knex')({client:'sqlite3',connection:{filename:'./data.db',},});try{// Create a tableawaitknex.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...constinsertedRows=awaitknex('users').insert({user_name:'Tim'});// ...and using the insert id, insert into the other table.awaitknex('accounts').insert({account_name:'knex',user_id:insertedRows[0],});// Query both of the rows.constselectedRows=awaitknex('users').join('accounts','users.id','accounts.user_id').select('users.user_name as user','accounts.account_name as account');// map over the resultsconstenrichedRows=selectedRows.map((row)=>({ ...row,active:true}));// Finally, add a catch statement}catch(e){console.error(e);}

TypeScript example

import{Knex,knex}from'knex';interfaceUser{id:number;age:number;name:string;active:boolean;departmentId:number;}constconfig:Knex.Config={client:'sqlite3',connection:{filename:'./data.db',},};constknexInstance=knex(config);try{constusers=awaitknex<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:

importknexfrom'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} */constdatabase=knex({client:'mysql',connection:{host:'127.0.0.1',user:'your_database_user',password:'your_database_password',database:'myapp_test',},});database.migrate.latest();

About

A query builder for PostgreSQL, MySQL, CockroachDB, SQL Server, SQLite3 and Oracle, designed to be flexible, portable, and fun to use.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp