All Projects → knex → Knex

knex / Knex

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

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
shell
77523 projects

Projects that are alternatives of or similar to Knex

Openrecord
Make ORMs great again!
Stars: ✭ 474 (-96.86%)
Mutual labels:  sql, mysql, postgresql, sqlite3
Sqlcheck
Automatically identify anti-patterns in SQL queries
Stars: ✭ 2,062 (-86.33%)
Mutual labels:  sql, mysql, postgresql, sqlite3
Shmig
Database migration tool written in BASH.
Stars: ✭ 408 (-97.29%)
Mutual labels:  sql, mysql, postgresql, sqlite3
Sworm
a write-only ORM for Node.js
Stars: ✭ 128 (-99.15%)
Mutual labels:  sql, mysql, postgresql, sqlite3
Vscode Sqltools
Database management for VSCode
Stars: ✭ 741 (-95.09%)
Mutual labels:  sql, mysql, postgresql, sqlite3
Walkable
A Clojure(script) SQL library for building APIs: Datomic® (GraphQL-ish) pull syntax, data driven configuration, dynamic filtering with relations in mind
Stars: ✭ 384 (-97.45%)
Mutual labels:  sql, mysql, postgresql, sqlite3
Qb
The database toolkit for go
Stars: ✭ 524 (-96.53%)
Mutual labels:  sql, mysql, postgresql, sqlite3
Trdsql
CLI tool that can execute SQL queries on CSV, LTSV, JSON and TBLN. Can output to various formats.
Stars: ✭ 593 (-96.07%)
Mutual labels:  sql, mysql, postgresql, sqlite3
Pmacct
pmacct is a small set of multi-purpose passive network monitoring tools [NetFlow IPFIX sFlow libpcap BGP BMP RPKI IGP Streaming Telemetry].
Stars: ✭ 677 (-95.51%)
Mutual labels:  sql, mysql, postgresql, sqlite3
Lucid
AdonisJS official SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
Stars: ✭ 613 (-95.94%)
Mutual labels:  sql, mysql, postgresql, sqlite3
Electrocrud
Database CRUD Application Built on Electron | MySQL, Postgres, SQLite
Stars: ✭ 1,267 (-91.6%)
Mutual labels:  sql, mysql, postgresql, sqlite3
Usql
Universal command-line interface for SQL databases
Stars: ✭ 6,869 (-54.46%)
Mutual labels:  sql, mysql, postgresql, sqlite3
Rom Sql
SQL support for rom-rb
Stars: ✭ 169 (-98.88%)
Mutual labels:  sql, mysql, postgresql, sqlite3
Kangaroo
SQL client and admin tool for popular databases
Stars: ✭ 127 (-99.16%)
Mutual labels:  sql, mysql, postgresql
Sql Template Tag
ES2015 tagged template string for preparing SQL statements, works with `pg` and `mysql`
Stars: ✭ 132 (-99.12%)
Mutual labels:  sql, mysql, postgresql
Yuniql
Free and open source schema versioning and database migration made natively with .NET Core.
Stars: ✭ 156 (-98.97%)
Mutual labels:  sql, mysql, postgresql
Sql Kit
*️⃣ Build SQL queries in Swift. Extensible, protocol-based design that supports DQL, DML, and DDL.
Stars: ✭ 115 (-99.24%)
Mutual labels:  sql, mysql, postgresql
Querybuilder
SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
Stars: ✭ 2,111 (-86%)
Mutual labels:  sql, mysql, postgresql
Shardingsphere
Build criterion and ecosystem above multi-model databases
Stars: ✭ 14,989 (-0.62%)
Mutual labels:  sql, mysql, postgresql
Sqhell.vim
An SQL wrapper for vim
Stars: ✭ 113 (-99.25%)
Mutual labels:  sql, mysql, sqlite3

knex.js

npm version npm downloads Coverage Status Dependencies Status Gitter chat Language Grade: JavaScript

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 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();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].