ijmacd-query
v1.7.0
Published
SQL for JSON objects/APIs
Downloads
32
Readme
JS SQL Query
A simple (read: toy) SQL implementation made from scratch in pure JS.
Examples
Basic SELECT from a Demo table.
FROM Test SELECT n,n2,n3
⯈
Basic WHERE clause on a Demo table. SELECT defaults to SELECT *
.
FROM Test WHERE n > 2
⯈
Full CROSS JOIN on Demo tables.
FROM Test, Test
⯈
JOIN with predicate.
FROM Test AS a, Test AS b ON a.n < b.n
⯈
There are even some built-in table valued functions.
FROM RANGE(3,15, 2)
⯈
LOAD is also a table valued function to load data from an arbritrary url.
FROM LOAD('https://api.github.com/users/IJMacD/repos'), Owner
⯈
FROM LOAD('http://www.reddit.com/r/javascript.json'), data.children AS c, c.data AS d
⯈
FROM LOAD('https://dummy.restapiexample.com/api/v1/employees'), data
⯈
You can use expressions in table valued functions.
FROM RANGE(-7,0), LOAD('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&date=' || DATE(DATE_ADD(CURRENT_DATE(), value, DAY))) AS nasa WHERE media_type = 'image' SELECT nasa.*
[⯈](https://ijmacd.github.io/query/#q=FROM%20RANGE(-7%2C0)%2C%20LOAD('https%3A%2F%2Fapi.nasa.gov%2Fplanetary%2Fapod%3Fapi_key%3DDEMO_KEY%26date%3D'%20%7C%7C%20DATE(DATE_ADD(CURRENT_DATE()%2C%20value%2C%20DAY)))%20AS%20nasa%20WHERE%20media_type%20%3D%20'image'%20SELECT%20nasa.*)
Going deeper...
FROM (FROM LOAD('https://xkcd.com/info.0.json') SELECT num), RANGE(num,num - 10), LOAD('https://xkcd.com/'||value||'/info.0.json')
(won't work in browser due to XKCD CORS issues but works on server)
CTEs are supported.
WITH cte AS (FROM Test_2 WHERE c > 'd') FROM Test, cte ON CHAR(n+97) = c
⯈
As are window functions.
FROM Test SELECT n, n2, RANK() OVER(ORDER BY n2)
⯈
FROM Test SELECT n, SUM(n) OVER(PARTITION BY n2)
⯈
FROM Test SELECT n, SUM(n) OVER(ORDER BY n ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
⯈
Check out the tests for more examples of supported features.