-
-
Notifications
You must be signed in to change notification settings - Fork 0
Model
import {Model} from 'jeloquent';
Model: Model should be extended from your custom model class
class User extends Model {
constructor() {
const fields = [
new Field('id', true), //name, isPrimary
new Field('name'), //name, isPrimary = false
//new Field('team_id') // added by BelongsTo relation
new BelongsTo(Team), // ads relation with team
]
}
}
constructor(fields): super(fields) should contain array of field instances
className(): returns className
User.className()
//returns string of class name
"User"
addIndex(name): adds custom index in table of field.name
//an index is created on name
User.addIndex('name');
insert(data): insert models and filled with json data in table
//insert 1 user
User.insert({id: 1, name: 'test', team_id: '1'});
//insert multiple users
User.insert([
{id: 1, name: 'test', team_id: '1'},
{id: 2, name: 'test 1', team_id: '1'},
{id: 3, name: 'test 2', team_id: '1'},
{id: 4, name: 'test 3', team_id: '1'},
]);
update(data): updates models by primary key in table with given json data
User.update({
id: 1,
name: 'test'
});
delete(id): deletes model from store by primaryKey
User.delete(1);
find(ids): return model or array of models from table
User.find(1);
//returns User
User
//returns null if id can't be found
null
//find with array of ids
User.find([1,2,5]);
// returns collection of users
Collection(3) [User, User, User];
//returns null if one of the id's can't be found
Collection(3) [User, null, User]
all(): returns all models from table
User.all();
//returns collection of all users
Collection(5) [User, Users, User, User, User];
ids(): returns all primary keys from table
User.ids();
//returns collection of all users
[1, 2, 3, 4, 5];
save(): stores current model values to table
const user = new User();
user.id = 1;
user.name = 'user 1';
user.team_id = 1;
//saves user 1 in memory table
user.save();
fill(data): fills model with json data
//Updates local object values
User.find(1).fill({name: 'test 2' team_id: 2});
toJson(fromRelation): return json data of model and his nearest relations
User.find(1).toJson();
//returns all fields and relations
{id: 1, name: 'user 1', team_id: 1, team: {id: 1, name: 'team 1'}}
primaryKey: returns value of the primary key fields split with dashes
User.find(1).primaryKey
//returns
1
primaryKeyName: return the combined primary key name split with dashes
User.find(1).primaryKeyName
//returns
['id']
User.find({user_type: 'test', user_id: 1}).primaryKeyName
//returns
['user_type', 'user_id']