Skip to content

Commit

Permalink
added default export, redis test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shahrul committed Sep 30, 2023
1 parent fb56f53 commit a3494c3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ You can check [samples](https://github.com/syarul/requrse/blob/main/samples) fol

A basic usage of reQurse.
```javascript
import queryExec from 'requrse'
import rq from 'requrse'

queryExec(query, { methods, config })
rq(query, { methods, config })
```
- **query**: *(object)* ***required*** JSON like query.
- **methods**: *(object)* ***required*** define methods/computed fields that exist in the query.
- **config**: *(object)* ***optional*** extend and added parameterize control over methods.
```js
await queryExec({
await rq({
Test: {
test: {
greeting: '*'
Expand All @@ -44,7 +44,7 @@ await queryExec({

By default methods will automatically resolve promises.
```js
await queryExec({
await rq({
Test: {
test: {
person: {
Expand All @@ -65,7 +65,7 @@ await queryExec({

You can pass arguments using `$params` parameter.
```js
await queryExec({
await rq({
Test: {
test: {
person: {
Expand All @@ -87,7 +87,7 @@ await queryExec({
```
Not limited to database queries, you can also manage API endpoints too
```js
queryExec({
rq({
Test: {
test: {
request: {
Expand Down Expand Up @@ -119,7 +119,7 @@ queryExec({

You can add options `config` to map methods with different name. This allow a consistent structure of the query.
```js
await queryExec({
await rq({
Test: {
test: {
person: {
Expand All @@ -145,7 +145,7 @@ await queryExec({

With `config` you can specify custom parameter to map result or use it as input for your methods.
```js
await queryExec({
await rq({
Test: {
test: {
occupation: 1,
Expand Down Expand Up @@ -190,7 +190,7 @@ await queryExec({

The query tree is resolve recursively, so you can have very complex query structure.
```js
await queryExec({
await rq({
Test: {
test: {
person: {
Expand Down
4 changes: 1 addition & 3 deletions libs/executeQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ const executeQuery = async (query, currentQuery, opts, mergeQuery = {}) => {
if (alias) {
key = `${key}/${alias}`
}
if(currentQuery instanceof Promise) {
currentQuery = await currentQuery
}
if (value instanceof Object) {
currentQuery = await resolvePromises(currentQuery)
result = await executeQuery(value, currentQuery, { methods, config }, mergeQuery)
Expand All @@ -91,6 +88,7 @@ const executeQuery = async (query, currentQuery, opts, mergeQuery = {}) => {
}
buildEntries.push([key, result])
} else {
currentQuery = await resolvePromises(currentQuery)
// resolved scalar/non-scalar value, and consequently same types
buildEntries.push([key, typeof currentQuery === typeof value ? currentQuery : value === '*' ? currentQuery : value])
}
Expand Down
3 changes: 3 additions & 0 deletions libs/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ const rq = (query, opts) => {
}
}

global.rq = rq
exports.rq = rq
module.exports = rq
module.exports.default = rq

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@syarul/requrse",
"version": "0.1.9",
"version": "0.1.10",
"description": "Lightweight driven query language",
"main": "libs/executor.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion samples/redis/redis.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ module.exports = (query, { redis, redisKey, memberKey }) => queryExec(query, {
return newData
},
async getMemberKeys () {
return { keys: await redis.smembers(memberKey) }

return { keys: (await redis.smembers(memberKey)).sort() }
},
async remove ({ id }) {
const [, ...secondaryKeys] = [].slice.call(arguments).pop()
Expand Down
15 changes: 15 additions & 0 deletions samples/redis/redis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ async function test () {
})
}, console.error)

await requrseRedis({
book: {
getMemberKeys: '*'
}
}, modelOptions).then(result => {
console.log(result)
assert.deepEqual(result, {
book: {
getMemberKeys: {
keys: ['0', '1']
}
}
})
}, console.error)

for (const key of keys) {
await remove(key).then(console.log, console.error)
}
Expand Down

0 comments on commit a3494c3

Please sign in to comment.