Skip to content

Quick Setup

mbroersen edited this page Oct 12, 2020 · 11 revisions

Install

npm i jeloquent

Create classes

Models.js

import {BelongsTo, Model, Field, HasMany, HasOne, HasManyThrough, MorphOne, MorphTo} from 'jeloquent';

/**
*   Has id as primary key
*   Has team_id index
*   Can access related Comments by calling .comments
*   Can access Team by calling .team
*   Can access Avatar by calling .avatar
*   Can access UserAdress by calling .user_address
**/
class User extends Model {
    constructor() {
        const fields = [
            new Field('id', true),
            new Field('name'),
            //new Field('team_id'), added by BelongsTo Relation
            new BelongsTo(Team),
            new HasMany(Comment),
            new MorphOne(Avatar),
            new HasOne(UserAddress),
        ];
        super(fields);
    }
}

/**
*   Has id as primary key
*   Has user_id index
*   Can access User by calling .user
**/
class UserAddress extends Model{

    constructor() {
        const fields = [
            new Field('id', true),
            new Field('city'),
            new Field('street'),
            new Field('house_number'),
            //new Field('user_id'), added by BelongsTo Relation
            new BelongsTo(User),
        ];
        super(fields);
    }
}


/**
*   Has id as primary key
*   Can access related Users by calling .users
*   Can access all users comments by calling .comments
*   Can access Avatar by calling .avatar
**/
class Team extends Model {

    constructor() {
        const fields = [
            new Field('id', true),
            new Field('name'),
            new HasMany(User),
            new HasManyThrough(Comment, User),
            new MorphOne(Avatar),
        ];

        super(fields);
    }
}

/**
*   Has id as primary key
*   Has user_id index
*   Has user.team_id index through team HasManyThrough
*   Can access related User by calling .user 
**/
class Comment extends Model {

    constructor() {
        const fields = [
            new Field('id', true),
            new Field('title'),
            new Field('text'),
            //new Field('user_id'), added by BelongsTo Relation
            new BelongsTo(User),
        ];

        super(fields);
    }
}

/**
 * Has composed primary key {avatar_id: '', avatar_type: ''}
 * Can access parent object by calling .my_parent
 *
 */
class Avatar extends Model {
    constructor() {
        const fields = [
            new Field('img_url'),
            new Field('avatar_id', true),
            new Field('avatar_type', true),
            new MorphTo('my_parent'),
        ];

        super(fields);
    }
}

export {
    Team,
    User,
    Comment,
    Avatar,
    UserAddress,
}

Setup store

Store.js

import {Database, Store} from 'jeloquent';
import {User, Team, Comment, Avatar} from './Models.js';

const models = [
    User,
    Team,
    Comment,
    Avatar,
];

const store = new Store();
store.add(new Database('app', models));
store.use('app');

Insert an array of json data objects

User.insert(dataUsers);
Team.insert(dataTeams);
Comment.insert(dataComments);
Avatar.insert(dataAvatars);
UserAddress.insert(dataUserAddress);
Clone this wiki locally