cyberco-model v1.6.3
README
Cyberco Model is an Abstraction that provides a possibility to work with DB interface, ignoring the implementation of it.
https://www.npmjs.com/package/cyberco-model
###How to use
Firstly, you need to init the model with Adapter:
const CybercoModel = require('cyberco-model');
const DsAdapter = require('cyberco-model/adapters/datastore');
const DataTypes = require('cyberco-model/data-types');
class UserModel extends CybercoModel {
constructor() {
super(DsAdapter);
let schema= {
first_name: {
type: DataTypes.STRING(),
required: true,
},
last_name: {
type: DataTypes.STRING(),
required: true,
},
};
return this.define(
'user',
schema
)
}
}
module.exports = UserModel;
After that you can use it in your code:
let UserModel = require.main.require('./ds-models/user.model');
let User = new UserModel();
return User.createOne({
first_name: 'Alex',
last_name: 'Poltoratskiy'
});
Available data types(@see sequelize/lib/data-types.js):
- DataTypes.STRING()
- DataTypes.ENUM()
transfer ENUM elements as function params
status: {
type: DataTypes.ENUM('lead', 'pending', 'active', 'closed', 'dead'),
},
- DataTypes.INTEGER()
- DataTypes.FLOAT()
- DataTypes.BOOLEAN()
- DataTypes.NUMBER()
- DataTypes.DATE()
Field definition
- Simple definition
royalty_cap_enabled: DataTypes.BOOLEAN(),
Just define field type, leaving everything else default
- Extended field definition
email: {
type: DataTypes.STRING(),
required: true
default : 'no-reply@google.com'
},
- type - one of available data types
- required -
true
/false
. If true you can't create a record without specifying this field - default - a valid
type
value set to field when created without specifying it. Doesn't works with required
Available methods:
- findOne
return User.findOne({
id: 5141143767482368,
name: {
$in: ['Alex', 'John', 'Michael']
},
age: {
">": 20,
"<=": 35
},
"name.prop.something": 'some value'
})
- findMany
return User.findMany({
name: {
$in: ['Alex', 'Anton', 'Jack']
}
})
- createOne
return User.createOne({
name: 'Anton'
})
- createMany
return User.createMany([
{
name: 'four',
surname: 'four surname',
age: 4
},
{
name: 'five',
surname: 'five surname',
age: 5
},
{
name: 'six',
surname: 'six surname',
age: 6
}
])
- update
return User.update(
{
property: {
$in: ['five', 'four']
}
},
{
new_prop: 'update this property value'
})
- deleteWhere
return User.deleteWhere({
name: 'four'
})
Available params
merge_props: boolean. Used in updateWhere(). By passing merge_props = true, you can update entities merging their properties recursively.
For example,
return User.updateWhere({
id: 5686589850124288
},
{
nested_property: {
property: {
one: 'one value',
}
},
},
{
merge_props: true
}
)
Without merge_props such query will fully rewrite "nested_property" value.
#####order: object. Used in findOne(), findMany(). Is used to specify order by properties and their direction. For example,
return User.findMany({
age: {
">": 29
}
}, {
order: {
age: 'desc' // or 'asc'
}
})
#####select: array. Used in findOne(), findMany(). Specifying this list will allow to select just required set of properties. Example:
return User.findMany({
age: {
">": 15,
"<": 35
},
}, {
select: ['first_name', 'last_name']
})
#####limit: number. Specify this number if you want to limit the result. For example,
return User.findMany({
age: {
">": 15,
"<": 35
}
}, {
limit: 5,
})
#####offset: number. Specify this number if you want to skip some entities in result. For example,
return User.findMany({
age: {
">": 15,
"<": 35
}
}, {
limit: 5,
offset: 5,
})
#####with_deleted: boolean. Set TRUE if you want to get soft-deleted rows although. For example,
return User.findMany({
name: 'Jack'
}, {
with_deleted: true,
})
Working with relations
findOne and findMany can return results with relations
Currently such relations are supported:
- belongs_to
- has_many
- has_one
- belongs_to_many
Configuring relations:
const CybercoModel = require('cyberco-model');
const DsAdapter = require('cyberco-model/adapters/datastore');
const DataTypes = require('cyberco-model/data-types');
const ProfileModel = require('../ds-models/profile.model');
class UserModel extends CybercoModel {
constructor() {
super(DsAdapter);
let schema= {
email: {
type: DataTypes.STRING(),
required: true,
},
first_name: {
type: DataTypes.STRING(),
required: true,
},
last_name: {
type: DataTypes.STRING(),
required: true,
},
firebase_id: {
type: DataTypes.STRING(),
required: false,
},
};
return this.define(
'user',
schema,
true, // use soft deleting
{
profiles: {
model: require('../ds-models/profile.model'),
type: 'has_many',
target_key_property: 'user_fk_id'
},
companies: {
model: require('../ds-models/profile.model'),
type: 'belongs_to_many',
through: require('../ds-models/profile.model'),
foreign_key_property: 'user_fk_id',
target_key_property: 'company_fk_id',
},
// example of through_foreign_property relation
companies_through_prop: {
model: require('./company.model'),
type: CybercoModel.consts.RELATION_BELONGS_TO_MANY,
through_foreign_property: 'companies_ids',
}
}
)
}
}
module.exports = UserModel;
Belongs_to_many through array property.
belongs_to_many relation can be used through array property in non-relational approach.
So, let's imagine that User kind has property type array "companies_ids" where Companies` ids are stored. You can configure models' relations in such way:
User:
{
companies: {
model: require('./company.model'),
type: CybercoModel.consts.RELATION_BELONGS_TO_MANY,
through_foreign_property: 'companies_ids'
}
}
Company:
{
users: {
model: require('./user.model'),
type: CybercoModel.consts.RELATION_BELONGS_TO_MANY,
through_target_property: 'companies_ids'
}
}
Then you can pass include array property to paramsObj:
return User.findMany(
{},
{
include: ['posts', 'company', 'roles']
}
)
Result of such query will be:
[
{
"created_at": "2017-09-08T09:59:07.111Z",
"updated_at": "2017-09-08T09:59:07.111Z",
"age": 5,
"is_deleted": false,
"surname": "five surn",
"name": "five",
"id": 5191129133744128,
"company": null,
"posts": [
{
"updated_at": "2017-09-08T14:37:56.244Z",
"message": "hahah",
"is_deleted": false,
"user_fk_id": 5191129133744128,
"created_at": "2017-09-08T14:37:56.244Z",
"id": 5146118144917504
},
{
"user_fk_id": 5191129133744128,
"created_at": "2017-09-08T14:37:56.244Z",
"updated_at": "2017-09-08T14:37:56.244Z",
"message": "he",
"is_deleted": false,
"id": 5709068098338816
}
],
"roles": [
{
"name": "user",
"id": 6272018051760128
},
{
"name": "admin",
"id": 5722383033827328
}
]
},
{
"updated_at": "2017-09-08T09:59:07.111Z",
"age": 4,
"company_fk_id": 5693048138760192,
"is_deleted": false,
"surname": "four surn",
"name": "four",
"created_at": "2017-09-08T09:59:07.111Z",
"id": 5754079087165440,
"posts": [],
"company": {
"name": "google",
"id": 5693048138760192
},
"roles": [
{
"name": "admin",
"id": 5722383033827328
}
]
},
{
"updated_at": "2017-09-08T09:59:07.111Z",
"age": 6,
"is_deleted": false,
"surname": "six surn",
"name": "six",
"created_at": "2017-09-08T09:59:07.111Z",
"id": 6317029040586752,
"company": null,
"posts": [],
"roles": []
}
]
Advanced relations querying
Also you can use such relations querying
return User.findMany(
{
status: 'active'
},
{
include: [
{
relation: 'posts',
params: {
limit: 5,
order: {
created_at: 'desc' // or 'asc'
},
select: ['message']
}
},
{
relation: 'company',
where: {
name: 'google'
}
},
'roles'
]
}
)
Nested querying via relations are also available:
let User = new UserModel();
return User.findOne({
id: 5743114304094208
}, {
include: [
{
relation: 'profiles',
params: {
include: [
{
relation: 'company',
params: {
include: ['users']
}
}
]
}
}
]
})
use "required: true (false)" param to filter results if their relations are required
return Profile.findMany({}, {
include: [
{
relation: 'company',
required: true,
params: {
include: [
{
relation: 'users',
required: false
}
]
}
}
]
})
5 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago