Skip to content

parpeoficial/stackerjs-db-mysql-adapter

Repository files navigation

Travis Test Coverage Maintainability Dependencies npm

NPM

StackerJS

Database: MySQL Adapter

An MySQL adapter for StackerJS apps with ORM interacting with MySQL databases.

QueryBuilder

Insert

import { QueryBuilder } from 'stackerjs-db-mysql-adapter';

let query = new QueryBuilder()
    .insert()
    .set({
        'full_name': 'mysql adapter',
        'age': 2
    })
    .into('person')
    .parse();

    // INSERT INTO person (`full_name`, `age`) VALUES ("mysql adapter", 2);

Select

import { QueryBuilder, QueryCriteria } from 'stackerjs-db-mysql-adapter';

let criteria = new QueryCriteria(),
    query = new QueryBuilder()
        .select()
        .set('id', ['full_name', 'name'], 'age')
        .from('person')
        .where(
            criteria.andX(
                criteria.eq('active', true),
                criteria.gte('age', 18),
                criteria.like('name', '%george%')
            )
        )
        .parse();

    // SELECT `person`.`id`, `person`.`full_name` AS name, `person`.`age` FROM person
    // WHERE (`active` = 1 AND `age` >= 18 AND `name` LIKE "%george%");

Update

import { QueryBuilder } from 'stackerjs-db-mysql-adapter';

let query = new QueryBuilder()
    .update()
    .set({
        'full_name': 'still mysql adapter'
    })
    .into('person')
    .where({
        'id': 1
    })
    .parse();

    // UPDATE person SET `full_name` = "still mysql adapter" WHERE `id` = 1;

Delete

import { QueryBuilder } from 'stackerjs-db-mysql-adapter';

let query = new QueryBuilder()
    .delete()
    .into('person')
    .where({
        'id': 1
    })
    .parse();

    // DELETE FROM person WHERE `id` = 1;    

Filtering

You can build filters using String, Object or QueryCriteria class

Comparision String Object Query Criteria
Equal field = value { field: value } or { field: [ 'eq': value ] } or { field: { 'eq': value } } new QueryCriteria.eq(field, value)
Non equal field <> value { field: [ 'neq': value ] } or { field: { 'neq': value } } new QueryCriteria.neq(field, value)