GithubHelp home page GithubHelp logo

derianandre / jql Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 1.12 MB

A simple intuitive JSON Query Language inspired by MySQL and jslinq made in javascript.

License: MIT License

JavaScript 100.00%
jql json-query-language json query mysql linq jslinq

jql's Introduction

JQL logo

JQL – JSON Query Language

A simple intuitive JSON Query Language inspired by MySQL and jslinq made in javascript.

This is a work in progress and not ready for production use.


JQL and MySQL

You can do simple queries just like MySQL and it can even be shorter

SELECT * FROM `data` WHERE `active` = true OR `email` LIKE '%{$email}%' LIMIT 5

With JQL this can be done like this:

// This works:
JQL(data).select('*').where(`active = true || email ~ "${email}"`).limit(5);

// This also works:
	// ✅ Smaller footprint 😏
	// ✅ You can remove .select('*') as we already have all the data 🙄
	// ✅ You can also use: "<string>" or '<string>', AND or &&, OR or || 🥵
JQL(data).where(`active = true OR email ~ '${email}'`).limit(5);

// Or with a function like this:
JQL(data).where((i) => { 
	return i.email.contains(email) || i.active == true;
}).limit(5);

Operators

Supported operators for expressions are very basic. Use a function for a more complex predicate.

Comparasion

~ ~~ ~= = == != === !== < > <= >=

Note: ~ is insensitive while ~~ or ~= is case sensitive.

Logical

&& AND || OR

Example Data

const data = [
	{ "id": 1, "name": "Aleta Nelsen", "email": "[email protected]", "active": true },
	{ "id": 2, "name": "Davidde Madgett", "email": "[email protected]", "active": true },
	{ "id": 3, "name": "Monty Gulston", "email": "[email protected]", "active": true },
	{ "id": 4, "name": "Edie Cardiff", "email": "[email protected]", "active": false },
	{ "id": 5, "name": "Kim Guion", "email": "[email protected]", "active": false },
	{ "id": 6, "name": "Tymothy Wingar", "email": "[email protected]", "active": false },
	{ "id": 7, "name": "Carolus Walworche", "email": "[email protected]", "active": false }
	...
]

Functions

🔍 Query

Function Description Variable Type Description (Var)
.select() Select the data keys (columns) you want expression string, array Expression that you want to select and store into the variables
.where() Filter the data with an expression or a function. expression string, function Combination of one or more predicates using the logical operators

.select()

Select the data keys (columns) you want Carefull with this, is not the same as MYSQL! The order matters.

Example

JQL(data).select('name, active').where('id > 5 && active == false').limit(5).log();
 (0) []
// .select() will give you an array with two columns and then you are searching for a key that isn't longer in the array.

JQL(data).where('id > 5 && active == false').select('name, active').limit(5).log();
 (5) [
	0: {name: "Eugenio Goodall", active: false}
	1: {name: "Dorian Simonnot", active: false}
	2: {name: "Cyb Botwood", active: false}
	3: {name: "Jandy Eddoes", active: false}
	4: {name: "Connie Riddoch", active: false}
]

.where()

Filter the data with an expression or a function. You can use multiple conditions, as a string or you can use a function.

Example

// With a query
JQL(data).where('email ~ a && active == false').log();

// With a function
JQL(data).where((i) => {
	// Dot notation (a bit smaller 🤓)
	return (i.email.includes('a') && i.active == false)
}).log();
// Or like this
JQL(data).where((i) => {
	// Bracket notation
	return (i['email'].includes('a') && i['active'] == false)
}).log();

// Both will have the same result:
 (3) [
	0: { "id": 4, "name": "Edie Cardiff", "email": "[email protected]", "active": false },
	1: { "id": 6, "name": "Tymothy Wingar", "email": "[email protected]", "active": false },
	2: { "id": 7, "name": "Carolus Walworche", "email": "[email protected]", "active": false }
]

📉 Data

Function Description
.data() or .array() or .result() Return the items
.count() Return the data length
.first() Return the first item
.last() Return the last item

.data() or .array() or .result()

Get that precious data

Example

let result = JQL(data).items;
// Or like this
let result = JQL(data).data();

// Both will have the same result:
console.log(result);
 (n) [{}, {}, {}, ]

.count()

Get the data.length

Example

let result = JQL(data).length;
// Or like this
let result = JQL(data).count();

// Both will have the same result:
console.log(result);
 Length (n)

.first()

Return the first item as data (true) or constructor (false)

Example

let result = JQL(data).last(); 
console.log(result);
 {id: 1, name: "Waldon Shortell", email: "[email protected]", gender: "Cis Man", ipv4: "145.153.192.113", …}

let result = JQL(data).last(false); 
console.log(result);
 JQL {items: Array(1), length: 1, select: ƒ, where: ƒ, limit: ƒ, …}

.last()

Return the last item as data (true) or constructor (false)

Example

let result = JQL(data).last(); 
console.log(result);
 {id: 1000, name: "Lusa Ellesworth", email: "[email protected]", gender: "Transfeminine", ipv4: "25.26.80.211", …}

let result = JQL(data).last(false); 
console.log(result);
 JQL {items: Array(1), length: 1, select: ƒ, where: ƒ, limit: ƒ, …}

💻 Logging

Debug like a pro!

Function Description Variable Type Default
.dir() Do a better console.dir() args object { collapse: true, items: true, limit: 10, options: {} }
.log() Do a better console.log() args object { collapse: true, items: true, limit: 10 }
.table() Do a better console.table() args object, string { collapse: true, columns: false, limit: 10 }

.dir()

Arguments

Argument Default Type Description
collapse true boolean Collapse the log information
items true boolean Log items or constructor function
limit 10 integer The number of elements that the log will show
options {} object Options of console.dir()

Example

let result = JQL(data).log()
 (n) [{}, {}, {}, ]

let result = JQL(data).log({ items: false });
 i {items: Array(4), length: 4, select: ƒ, where: ƒ, data: ƒ, …}

.log()

Arguments

Argument Default Type Description
collapse true boolean Collapse the log information
items true boolean Log: items (true) / constructor (false)
limit 10 integer The number of elements that the log will show

Example

let result = JQL(data).log()
 (n) [{}, {}, {}, ]

let result = JQL(data).log({ items: false });
 i {items: Array(4), length: 4, select: ƒ, where: ƒ, data: ƒ, …}

.table()

Arguments

Argument Default Type Description
collapse true boolean Collapse the log information
columns false string, array Columns of the table to show
limit 10 integer The number of elements that the log will show

Example

let result = JQL(data).table({ columns: ['id', 'email'] });

| id | email                      |
|----|----------------------------|
| 0  | anelsen0@printfriendly.com |
| 1  | dmadgett1@youtu.be         |
|... | ...                        |
 Array (n)

Why tho?

I have another project that I made with an API (express) and MongoDB (mongoose) and I wanted to have offline support when I exported my WebApp with Capacitorjs as an Android "native" App. The problem was that there is no way to have a nodejs server inside the App so I can't use the express API so I figuered out that I can export my DB with a Node command and save it inside my static folder so I can use JSON as an static database right? pretty simple.

Then I got into JSLINQ and it's pretty much what I needed. But I just like to have somethings my way and that's it haha.

TL;DR: I just wanted to make things in my on way and to learn about how to do it in the process.

Big thanks to JSLINQ for their awesome work and being open-source.


JQL

Derian André

jql's People

Contributors

derianandre avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.