Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
@next-model/knex-connector
Advanced tools
SQL connector for next-model package using Knex.
SQL connector for NextModel package using Knex.
Allows you to use Knex as Database Connector for NextModel:
Supports:
See GitHub project for current progress/tasks
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
import KnexConnector from '@next-model/knex-connector';
const connector = new KnexConnector({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
},
});
import KnexConnector from '@next-model/knex-connector';
const connector = new KnexConnector({
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:
import KnexConnector from '@next-model/knex-connector';
const connector = new KnexConnector({
client: 'sqlite3',
connection: {
filename: "./mydb.sqlite"
}
});
The connector is used to connect your models to a database.
const User = class User extends NextModel<UserSchema>() {
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.
function BaseModel<T extends Identifiable>() {
return class extends NextModel<T>() {
static get connector() {
return new Connector<T>();
}
}
};
const User = class User extends BaseModel<UserSchema>() {
static get modelName() {
return 'User';
}
static get schema() {
return {
id: { type: 'integer' },
name: { type: 'string' },
};
}
}
const Address = class Address extends BaseModel<AddressSchema>() {
static get modelName() {
return 'Address';
}
static get schema() {
return {
id: { type: 'integer' },
street: { type: 'string' },
};
}
}
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.
An object passed to query
will filter for object property and value.
User.query({ name: 'foo' });
select "users".* from "users" where ("name" = 'foo')
If the Object has multiple properties the properties are connected with and
.
User.query({ name: 'foo', age: 18 });
select "users".* from "users" where ("name" = 'foo' and "age" = 18)
An query
connected with another query
. A second query will encapsulate the query on the topmost layer.
User.query({ name: 'foo', age: 18 }).query({ name: 'bar' });
select "users".* from "users" where (("name" = 'foo' and "age" = 18) and ("name" = 'bar'))
Special properties are starting with an $
sign. The $and
property connects all values which are passed as Array
with an SQL and
operator.
User.query({ $and: [
{ name: 'foo' },
]});
select "users".* from "users" where (("name" = 'foo'))
User.query({ $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.query({ $and: [
{ name: 'foo' },
{ age: 18 },
]}).query({ $and: [
{ name: 'bar' },
{ age: 21 },
]});
select "users".* from "users" where ((("name" = 'foo') and ("age" = 18)) and (("name" = 'bar') and ("age" = 21)))
The $or
property works similar to the $and
property and connects all values with or
.
User.query({ $or: [
{ name: 'foo' },
]});
select "users".* from "users" where (("name" = 'foo'))
User.query({ $or: [
{ name: 'foo' },
{ name: 'bar' },
]});
select "users".* from "users" where (("name" = 'foo') or ("name" = 'bar'))
User.query({ $or: [
{ name: 'foo' },
{ age: 18 },
]}).query({ $or: [
{ name: 'bar' },
{ age: 21 },
]});
select "users".* from "users" where ((("name" = 'foo') or ("age" = 18)) and (("name" = 'bar') or ("age" = 21)))
The child object of an $not
property will be inverted.
User.query({ $not: {
name: 'foo'
}});
select "users".* from "users" where (not ("name" = 'foo'))
User.query({ $not: {
name: 'foo',
age: 18,
}});
select "users".* from "users" where (not ("name" = 'foo' and "age" = 18))
User.query({ $not: {
name: 'foo',
age: 18,
}}).query({ $not: {
name: 'bar',
age: 21,
}});
select "users".* from "users" where ((not ("name" = 'foo' and "age" = 18)) and (not ("name" = 'bar' and "age" = 21)))
The $and
, $or
and $not
properties can be nested as deeply as needed.
User.query({ $not: {
$or: [
{ name: 'foo' },
{ age: 21 },
],
}});
select "users".* from "users" where (not (("name" = 'foo') or ("age" = 21)))
User.query({ $not: {
$and: [
{ name: 'foo' },
{ $or: [
{ age: 18 },
{ age: 21 },
]},
],
}});
select "users".* from "users" where (not (("name" = 'foo') and (("age" = 18) or ("age" = 21))))
The $null
property checks for unset columns and takes the column name as value.
User.query({ $null: 'name' });
select "users".* from "users" where ("name" is null)
The $notNull
property checks if an column is set and takes the column name as value.
User.query({ $notNull: 'name' });
select "users".* from "users" where ("name" is not null)
There are five different equation properties available.
$eq
checks for equal$lt
checks for lower$gt
checks for greater$lte
checks for lower or equal$gte
checks for greater or equalThe property needs to be an object as value with the column name as key and the equation as value.
User.query({ $lt: { age: 18 } });
select "users".* from "users" where ("age" < 18)
User.query({ $lte: { age: 18 } });
select "users".* from "users" where ("age" <= 18)
Please note: Just one propery is allowed!
This is invalid:
User.query({ $lt: {
age: 18,
size: 180,
}});
This is valid:
User.query({ $and: [
{ $lt: { age: 18 } },
{ $lt: { size: 180 } },
]});
select "users".* from "users" where ("age" < 18 and "size" < 180)
The $in
property needs an object as value with the column name as key and the Array
of values as value.
User.query({ $in: {
name: ['foo', 'bar'],
}});
select "users".* from "users" where ("name" in ('foo', 'bar'))
Please note: Just one propery is allowed!
This is invalid:
User.query({ $in: {
name: ['foo', 'bar'],
age: [18, 19, 20, 21],
}});
This is valid:
User.query({ $and: [
{ $in: { name: ['foo', 'bar'] } },
{ $in: { age: [18, 19, 20, 21] } },
]});
select "users".* from "users" where ("name" in ('foo', 'bar') and "age" in (18, 19, 20, 21))
$notIn
works same as $in
but inverts the result.
User.query({ $notIn: {
name: ['foo', 'bar'],
}});
select "users".* from "users" where ("name" not in ('foo', 'bar'))
Please note: Just one propery is allowed!
This is invalid:
User.query({ $notIn: {
name: ['foo', 'bar'],
age: [18, 19, 20, 21],
}});
This is valid:
User.query({ $and: [
{ $notIn: { name: ['foo', 'bar'] } },
{ $notIn: { age: [18, 19, 20, 21] } },
]});
select "users".* from "users" where ("name" not in ('foo', 'bar') and "age" not in (18, 19, 20, 21))
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.query({ $between: {
age: [18, 21],
}});
select "users".* from "users" where ("age" between 18 and 21)
Please note: Just one propery is allowed!
This is invalid:
User.query({ $between: {
age: [18, 21],
size: [160, 185],
}});
This is valid:
User.query({ $and: [
{ $between: { age: [18, 21] } },
{ $between: { size: [160, 185] } },
]});
select "users".* from "users" where ("age" between 18 and 21 and "size" between 160 and 165)
$notBetween
works same as $between
but inverts the result.
User.query({ $notBetween: {
age: [18, 21],
}});
select "users".* from "users" where ("age" not between 18 and 21)
Please note: Just one propery is allowed!
This is invalid:
User.query({ $notBetween: {
age: [18, 21],
size: [160, 185],
}});
This is valid:
User.query({ $and: [
{ $notBetween: { age: [18, 21] } },
{ $notBetween: { size: [160, 185] } },
]});
select "users".* from "users" where ("age" not between 18 and 21 and "size" not between 160 and 165)
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.query({ $raw: {
$query: 'age = ?',
$bindings: 18,
}});
User.query({ $raw: {
$query: 'age = :age',
$bindings: { age: 18 },
}});
select "users".* from "users" where ("age" = 18)
See history for more details.
1.0.0
2018-xx-xx Complete rewrite based on TypeScript0.3.3
2017-04-05 Updated next-model dependency0.3.2
2017-02-28 Updated next-model dependency0.3.1
2017-02-27 Updated next-model dependency0.3.0
2017-02-22 Added Node 4 Support0.2.0
2017-02-21 Added new query types0.1.0
2017-02-18 Used next-model from npm instead of Github repo0.0.4
2017-02-16 Updated to NextModel v0.0.40.0.3
2017-02-12 Added CI0.0.2
2017-02-12 Added more complex query types0.0.1
2017-02-05 First release compatible with NextModel 0.0.1FAQs
SQL connector for next-model package using Knex.
The npm package @next-model/knex-connector receives a total of 1 weekly downloads. As such, @next-model/knex-connector popularity was classified as not popular.
We found that @next-model/knex-connector demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.