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.
const ArrayQL = require("array-ql");
const table = new ArrayQL(dataArray, options);
const selected = table.select("id, name").where("age").between(20,30).getList();
select(keys?: string) |
Resets previous select results, sets necessery keys. keys - is optional argument, comma separated string, including renaming, for example: "id, name as username". If no "keys" is set, then complete entries will be returned |
where(key: string) |
Sets active key, subsequent conditions will be applied to it |
and(key: string) |
Sets active key, and logic to "AND". Subsequent conditions will reduce resulting selection |
or(key: string) |
Sets active key, and logic to "OR". Subsequent conditions will extend resulting selection |
sort(key: string, direction?: "asc"|"desc") |
Sorts resulting selection |
orderBy(key: string, direction?: "asc"|"desc") |
Alias to sort() |
equalTo(val: any) |
Includes entries where value of active key is stirictly equal to given value (including type) |
is(val:any) |
Alias to equalTo() |
isNull() |
Includes entries where value of active key is NULL |
notNull() |
Includes entries where value of active key is not NULL |
like(val: string) |
Includes entries where active key has value which has partial case-insensitive match with given value |
lessThen(val: number) |
Includes entries where active key has value which is less then given one |
lt(val: number) |
alias to lessThen() |
lessThenOrEqual(val: number) |
Includes entries where active key has value which is less or equal to given one |
lte(val: number) |
alias to lessThenOrEqual() |
greaterThen(val: number) |
Includes entries where active key has value which is greater then given one |
gt(val: number) |
alias to lessThen() |
greaterThenOrEqual(val:number) |
Includes entries where active key has value which is greater or equal to given one |
gte(val:number) |
alias to greaterThenOrEqual() |
between(min: number, max: number) |
Includes entries where active key has value between "min" and "max", including "min" and "max" |
notBetween(min: number, max: number) |
Excludes entries where active key has value between "min" and "max", excluding "min" and "max" |
in(list: any[]) |
Leaves in resulting array entries where active key has value from "list" |
notIn(list: any[]) |
Excludes entries where active key has value from "list" |
limit(offset: number, limit: number) |
Leaves only "limit" elements from resulting array, beginning from "offset" element. Necessery for pagination . Remembers "unlimited" count for getResult() method |
const ArrayQL = require("array-ql");
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: { firstName: "Unknown", lastName: "", gender: null, age: null },
getters: {
name(row){ return `${row.firstName} ${row.lastName}`; }
}
}
const users = new ArrayQL(arr, options);
users.select("id, name").where("gender").is("male").getList();
users.insert({firstName: "Agrafena"});
users.update({lastName: "Svetlova"});
users.update({id: 6, lastName: "Svetlova", gender: "female", age: 31});
users.select("name as username").where("age").gt(30).getList();
users.select("id, name").limit(0, 2).getResult();