0.3.3 • Published 7 years ago

next-model-knex-connector v0.3.3

Weekly downloads
6
License
MIT
Repository
github
Last release
7 years ago

NextModelKnexConnector

SQL connector for NextModel package using Knex. Build Status

Allows you to use Knex as Database Connector for NextModel:

Supports:

  • pg
  • sqlite3
  • mysql
  • mysql2
  • mariasql (not tested)
  • strong-oracle (not tested)
  • oracle
  • mssql (not tested)

Roadmap / Where can i contribute

See GitHub project for current progress/tasks

  • Fix Typos
  • CI is missing for some Databases
  • Add more examples
  • Add exists, join and subqueries
  • There are already some tests, but not every test case is covered.

TOC

Example

Create Connector

Constructor options are passed to Knex.

Its recommended to set useNullAsDefault to true unless all model attributes are set.

The client parameter is required and determines which client adapter will be used with the library.

The connection options are passed directly to the appropriate database client to create the connection, and may be either an object, or a connection string

const connector = new NextModelKnexConnector({
  client: 'mysql',
  connection: {
    host : '127.0.0.1',
    user : 'your_database_user',
    password : 'your_database_password',
    database : 'myapp_test'
  },
});
const connector = new NextModelKnexConnector({
  client: 'pg',
  connection: process.env.PG_CONNECTION_STRING,
  searchPath: 'knex,public'
});

Note: When you use the SQLite3 adapter, there is a filename required, not a network connection. For example:

const connector = new NextModelKnexConnector({
  client: 'sqlite3',
  connection: {
    filename: "./mydb.sqlite"
  }
});

Use Connector

The connector is used to connect your models to a database.

const User = class User extends NextModel {
  static get connector() {
    return connector;
  }

  static get modelName() {
    return 'User';
  }

  static get schema() {
    return {
      id: { type: 'integer' },
      name: { type: 'string' },
    };
  }
}

Create an base model with the connector to use it with multiple models.

const BaseModel = class BaseModel extends NextModel {
  static get connector() {
    return connector;
  }
});

const User = class User extends BaseModel {
  static get modelName() {
    return 'User';
  }

  static get schema() {
    return {
      id: { type: 'integer' },
      name: { type: 'string' },
    };
  }
}

const Address = class Address extends BaseModel {
  static get modelName() {
    return 'Address';
  }

  static get schema() {
    return {
      id: { type: 'integer' },
      street: { type: 'string' },
    };
  }
}

Build Queries

This connector uses Knex to query SQL databases, but the query syntax is different from the Knex documentation. Samples of possible queries are listed below.

Where

An object passed as where clause will query for object property and value.

User.where({ name: 'foo' });
select "users".* from "users" where ("name" = 'foo')

If the Object has multiple properties the properties are connected with and.

User.where({ name: 'foo', age: 18 });
select "users".* from "users" where ("name" = 'foo' and "age" = 18)

An where query can be connected with another where or an orWhere. A second query will encapsulate the query on the topmost layer.

User.where({ name: 'foo', age: 18 }).orWhere({ name: 'bar' });
select "users".* from "users" where (("name" = 'foo' and "age" = 18) or ("name" = 'bar'))

And

Special properties are starting with an $ sign. The $and property connects all values which are passed as Array with an SQL and operator.

User.where({ $and: [
  { name: 'foo' },
]});
select "users".* from "users" where (("name" = 'foo'))
User.where({ $and: [
  { name: 'foo' },
  { age: 18 },
]});
select "users".* from "users" where (("name" = 'foo') and ("age" = 18))

The special properties can also chained with other where queries.

User.where({ $and: [
  { name: 'foo' },
  { age: 18 },
]}).orWhere({ $and: [
  { name: 'bar' },
  { age: 21 },
]});
select "users".* from "users" where ((("name" = 'foo') and ("age" = 18)) or (("name" = 'bar') and ("age" = 21)))

Or

The $or property works similar to the $and property and connects all values with or.

User.where({ $or: [
  { name: 'foo' },
]});
select "users".* from "users" where (("name" = 'foo'))
User.where({ $or: [
  { name: 'foo' },
  { name: 'bar' },
]});
select "users".* from "users" where (("name" = 'foo') or ("name" = 'bar'))
User.where({ $or: [
  { name: 'foo' },
  { age: 18 },
]}).where({ $or: [
  { name: 'bar' },
  { age: 21 },
]});
select "users".* from "users" where ((("name" = 'foo') or ("age" = 18)) and (("name" = 'bar') or ("age" = 21)))

Not

The child object of an $not property will be inverted.

User.where({ $not: {
  name: 'foo'
}});
select "users".* from "users" where (not ("name" = 'foo'))
User.where({ $not: {
  name: 'foo',
  age: 18,
}});
select "users".* from "users" where (not ("name" = 'foo' and "age" = 18))
User.where({ $not: {
  name: 'foo',
  age: 18,
}}).where({ $not: {
  name: 'bar',
  age: 21,
}});
select "users".* from "users" where ((not ("name" = 'foo' and "age" = 18)) and (not ("name" = 'bar' and "age" = 21)))

Nesting

The $and, $or and $not properties can be nested as deeply as needed.

User.where({ $not: {
  $or: [
    { name: 'foo' },
    { age: 21 },
  ],
}});
select "users".* from "users" where (not (("name" = 'foo') or ("age" = 21)))
User.where({ $not: {
  $and: [
    { name: 'foo' },
    { $or: [
      { age: 18 },
      { age: 21 },
    ]},
  ],
}});
select "users".* from "users" where (not (("name" = 'foo') and (("age" = 18) or ("age" = 21))))

Null

The $null property checks for unset columns and takes the column name as value.

User.where({ $null: 'name' });
select "users".* from "users" where ("name" is null)

NotNull

The $notNull property checks if an column is set and takes the column name as value.

User.where({ $notNull: 'name' });
select "users".* from "users" where ("name" is not null)

Equation

There are five different equation properties available.

  • $eq checks for equal
  • $lt checks for lower
  • $gt checks for greater

$lt, $gt also allows equal values.

The property needs to be an object as value with the column name as key and the equation as value.

User.where({ $lt: { age: 18 } });
select "users".* from "users" where ("age" < 18)
User.where({ $lt: { age: 18, size: 180 } });
select "users".* from "users" where ("age" < 18 and "size" < 180)
User.where({ $lte: { age: 18 } });
select "users".* from "users" where ("age" <= 18)
User.where({ $lte: { age: 18, size: 180 } });
select "users".* from "users" where ("age" <= 18 and "size" <= 180)

In

The $in property needs an object as value with the column name as key and the Array of values as value.

User.where({ $in: {
  name: ['foo', 'bar'],
}});
select "users".* from "users" where ("name" in ('foo', 'bar'))

If multiple properties are present they get connected by an and operator.

User.where({ $in: {
  name: ['foo', 'bar'],
  age: [18, 19, 20, 21],
}});
select "users".* from "users" where ("name" in ('foo', 'bar') and "age" in (18, 19, 20, 21))

NotIn

$notIn works same as $in but inverts the result.

User.where({ $notIn: {
  name: ['foo', 'bar'],
}});
select "users".* from "users" where ("name" not in ('foo', 'bar'))
User.where({ $notIn: {
  name: ['foo', 'bar'],
  age: [18, 19, 20, 21],
}});
select "users".* from "users" where ("name" not in ('foo', 'bar') and "age" not in (18, 19, 20, 21))

Between

The $between property needs an object as value with the column name as key and an Array with the min and max values as value.

User.where({ $between: {
  age: [18, 21],
}});
select "users".* from "users" where ("age" between 18 and 21)

If multiple properties are present they get connected by an and operator.

User.where({ $between: {
  age: [18, 21],
  size: [160, 185],
}});
select "users".* from "users" where ("age" between 18 and 21 and "size" between 160 and 165)

NotBetween

$notBetween works same as $between but inverts the result.

User.where({ $notBetween: {
  age: [18, 21],
}});
select "users".* from "users" where ("age" not between 18 and 21)
User.where({ $notBetween: {
  age: [18, 21],
  size: [160, 185],
}});
select "users".* from "users" where ("age" not between 18 and 21 and "size" not between 160 and 165)

Raw

The $raw property allows to write custom and database specific queries. Pass queries as object, where key is the query and value are the bindings.

Note: See Knex documentation for more details about bindings.

User.where({ $raw: {
  ['age = ?']: [18],
}});
select "users".* from "users" where ("age" = 18)
User.where({ $raw: {
  ['age = ?']: [18],
  ['name = ?']: ['foo'],
}});
select "users".* from "users" where ("age" = 18 and "name" = 'foo')

Changelog

See history for more details.

  • 0.0.1 2017-02-05 First release compatible with NextModel 0.0.1
  • 0.0.2 2017-02-12 Added more complex query types
  • 0.0.3 2017-02-12 Added CI
  • 0.0.4 2017-02-16 Updated to NextModel v0.0.4
  • 0.1.0 2017-02-18 Used next-model from npm instead of Github repo
  • 0.2.0 2017-02-21 Added new query types
  • 0.3.0 2017-02-22 Added Node 4 Support
  • 0.3.1 2017-02-27 Updated next-model dependency
  • 0.3.2 2017-02-28 Updated next-model dependency
  • 0.3.3 2017-04-05 Updated next-model dependency
0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago

0.0.4

7 years ago