next-model-local-storage-connector
v0.4.2
Published
LocalStorage connector for next-model package.
Downloads
8
Readme
NextModelLocalStorageConnector
LocalStorage connector for NextModel package.
Special keys for queries:
- $and
- $or
- $not
- $null
- $notNull
- $in
- $notIn
- $between
- $notBetween
- $eq
- $lt
- $lte
- $gt
- $gte
- $match
- $filter
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
The constructor allows to pass an prefix or postfix.
const connector = new NextModelKnexConnector({
prefix: 'app_',
});
const connector = new NextModelKnexConnector({
postfix: '_test',
});
const connector = new NextModelKnexConnector({
prefix: 'app_',
postfix: '_test',
});
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 allows to filter the data. 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' });
If the Object has multiple properties the properties are connected with and
.
User.where({ name: 'foo', 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' });
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' },
]});
User.where({ $and: [
{ name: 'foo' },
{ 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 },
]});
Or
The $or
property works similar to the $and
property and connects all values with or
.
User.where({ $or: [
{ name: 'foo' },
]});
User.where({ $or: [
{ name: 'foo' },
{ name: 'bar' },
]});
User.where({ $or: [
{ name: 'foo' },
{ age: 18 },
]}).where({ $or: [
{ name: 'bar' },
{ age: 21 },
]});
Not
The child object of an $not
property will be inverted.
User.where({ $not: {
name: 'foo'
}});
User.where({ $not: {
name: 'foo',
age: 18,
}});
User.where({ $not: {
name: 'foo',
age: 18,
}}).where({ $not: {
name: 'bar',
age: 21,
}});
Nesting
The $and
, $or
and $not
properties can be nested as deeply as needed.
User.where({ $not: {
$or: [
{ name: 'foo' },
{ age: 21 },
],
}});
User.where({ $not: {
$and: [
{ name: 'foo' },
{ $or: [
{ age: 18 },
{ age: 21 },
]},
],
}});
Null
The $null
property checks for unset columns and takes the column name as value.
User.where({ $null: 'name' });
NotNull
The $notNull
property checks if an column is set and takes the column name as value.
User.where({ $notNull: 'name' });
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 } });
User.where({ $lt: { age: 18, size: 180 } });
User.where({ $lte: { age: 18 } });
User.where({ $lte: { age: 18, 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'],
}});
If multiple properties are present they get connected by an and
operator.
User.where({ $in: {
name: ['foo', 'bar'],
age: [18, 19, 20, 21],
}});
NotIn
$notIn
works same as $in
but inverts the result.
User.where({ $notIn: {
name: ['foo', 'bar'],
}});
User.where({ $notIn: {
name: ['foo', 'bar'],
age: [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],
}});
If multiple properties are present they get connected by an and
operator.
User.where({ $between: {
age: [18, 21],
size: [160, 185],
}});
NotBetween
$notBetween
works same as $between
but inverts the result.
User.where({ $notBetween: {
age: [18, 21],
}});
User.where({ $notBetween: {
age: [18, 21],
size: [160, 185],
}});
Match
The $match
property needs an object as value with the column name as key and an regex
with the min and max values as value.
User.where({ $between: {
age: [18, 21],
}});
If multiple properties are present they get connected by an and
operator.
User.where({ $between: {
age: [18, 21],
size: [160, 185],
}});
Filter
The $filter
property allows to write custom filter queries. Pass an function to filter the items.
User.where({ $filter: (item) => {
return ...
}});
Changelog
See history for more details.
0.1.0
2017-02-25 First release compatible with NextModel 0.2.00.2.0
2017-02-25 Added missing dependency for CI0.3.0
2017-02-25 Improved browser compatibility0.4.0
2017-02-27 Stored nextId separately0.4.1
2017-02-27 Updated next-model dependency0.4.2
2017-02-28 Updated next-model dependency