Skip to content
forked from knex/knex

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

License

Notifications You must be signed in to change notification settings

Anton-Burdin/knex

This branch is 7 commits ahead of, 109 commits behind knex/knex:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

eef6070 · Sep 30, 2022
Jul 19, 2022
May 20, 2022
Apr 13, 2022
Sep 30, 2022
May 20, 2022
Sep 27, 2022
Sep 23, 2022
Sep 27, 2022
Sep 17, 2018
Dec 30, 2020
Mar 20, 2022
May 17, 2022
Dec 29, 2019
Aug 31, 2022
May 17, 2022
Feb 5, 2022
Aug 9, 2016
Jul 21, 2022
Apr 21, 2022
Dec 30, 2020
Mar 11, 2021
Oct 22, 2018
Sep 30, 2022
May 1, 2022

Repository files navigation

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), Clickhouse) 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:

const knex = require('knex')({
  client: 'clickhouse',
  connection: {
    connection: "http://user:password@127.0.0.1:8123/dbname",
    // connection: {
    //   "database": "dbname",
    //   "host": "127.0.0.1",
    //   "port": 8123,
    //   "user": "user name",
    //   "password": "password"
    // }
  },
});

try {
  const insertedRows = await knex('candlestick')
    .insert([
      {
        pair: "btc/usdt",
        open: 50000,
        close: 50010,
        openTime: '2022-07-19 19:39:50',
        closeTime: '2022-07-19 19:39:51'
      },
    ]);

  const selectedRows = await knex("candlestick")
    .select("pair")
    // notice: use value wrapper
    .where("open", ">", knex.client.val("1", "Decimal32(8)"))
    .limit(10);

  // 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();

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 96.2%
  • TypeScript 3.7%
  • Shell 0.1%