0.2.0 • Published 4 years ago

array-ql v0.2.0

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

ArrayQL

Treat array of objects as a table SQL-alike way. Paginate, select, sort, update, insert, delete. Add calculated and default values. Runs in browser and Node.

USERS.select("id, name").where("age").between(20,30).limit(0,15).getResult();

Mainly intended for mock servers, test, debugging, prototyping purposes.

Installation

npm install array-ql

Usage

const ArrayQL = require("array-ql");

const table = new ArrayQL(dataArray, options);

const selected = table.select("id, name").where("age").between(20,30).getList();

Keywords

Conditions

Fetch result

Data manipulation (CRUD)

Additional

Options

Examples

const ArrayQL = require("array-ql");

// regular array of objects with same structure
const arr = [
  { id: 1, firstName: "Clyde",  lastName: "Griffiths", gender: "male",   age: 24 },
  { id: 5, firstName: "Sondra", lastName: "Finchley",  gender: "female", age: 22 }
]

const options = {
  idName: "id",
  // default field values
  default: { firstName: "Unknown", lastName: "", gender: null, age: null },
  getters: {
    // getter for field "name"
    name(row){ return `${row.firstName} ${row.lastName}`; }
  }
}

const users = new ArrayQL(arr, options);

users.select("id, name").where("gender").is("male").getList(); // [{id: 1, name: "Clyde Griffiths"}]

users.insert({firstName: "Agrafena"}); // {id: 6, firstName: "Agrafena",  lastName: "", name: "Agrafena ", gender: null, age: null}

users.update({lastName: "Svetlova"}); // Error: "No id specified for update"

users.update({id: 6, lastName: "Svetlova", gender: "female", age: 31}); // {id: 6, firstName: "Agrafena",  lastName: "Svetlova", name: "Agrafena Svetlova", gender: female, age: 31}

users.select("name as username").where("age").gt(30).getList(); // [{username: "Agrafena Svetlova"}]

users.select("id, name").limit(0, 2).getResult(); // {content: [{id: 1, name: "Clyde Griffiths"}, {id: 5, name "Sondra Finchley"}], totalElements: 3, totalPages: 2, last: false, first: true}