knex

WebJar for knex

License

License

MIT
Categories

Categories

Github Development Tools Version Controls
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

github-com-tgriesser-knex
Last Version

Last Version

0.12.6
Release Date

Release Date

Type

Type

jar
Description

Description

knex
WebJar for knex
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/tgriesser/knex

Download github-com-tgriesser-knex

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/github-com-tgriesser-knex/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>github-com-tgriesser-knex</artifactId>
    <version>0.12.6</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/github-com-tgriesser-knex/
implementation 'org.webjars.npm:github-com-tgriesser-knex:0.12.6'
// https://jarcasting.com/artifacts/org.webjars.npm/github-com-tgriesser-knex/
implementation ("org.webjars.npm:github-com-tgriesser-knex:0.12.6")
'org.webjars.npm:github-com-tgriesser-knex:jar:0.12.6'
<dependency org="org.webjars.npm" name="github-com-tgriesser-knex" rev="0.12.6">
  <artifact name="github-com-tgriesser-knex" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='github-com-tgriesser-knex', version='0.12.6')
)
libraryDependencies += "org.webjars.npm" % "github-com-tgriesser-knex" % "0.12.6"
[org.webjars.npm/github-com-tgriesser-knex "0.12.6"]

Dependencies

compile (17)

Group / Artifact Type Version
org.webjars.npm : readable-stream jar [1.1.12,2)
org.webjars.npm : babel-runtime jar [6.11.6,7)
org.webjars.npm : node-uuid jar [1.4.7,2)
org.webjars.npm : commander jar [2.2.0,3)
org.webjars.npm : minimist jar [1.1.0,1.2)
org.webjars.npm : tildify jar [1.0.0,1.1)
org.webjars.npm : debug jar [2.1.3,3)
org.webjars.npm : liftoff jar [2.2.0,2.3)
org.webjars.npm : v8flags jar [2.0.2,3)
org.webjars.npm : inherits jar [2.0.1,2.1)
org.webjars.npm : chalk jar [1.0.0,2)
org.webjars.npm » generic-pool jar [2.4.2,3)
org.webjars.npm : pg-connection-string jar [0.1.3,0.2)
org.webjars.npm : bluebird jar [3.4.6,4)
org.webjars.npm : interpret jar [0.6.5,0.7)
org.webjars.npm : mkdirp jar [0.5.0,0.6)
org.webjars.npm : lodash jar [4.6.0,5)

Project Modules

There are no modules declared in this project.

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 (MSSQL, MySQL, PostgreSQL, 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();

Versions

Version
0.12.6