Skip to content

Commit

Permalink
Merge pull request #5 from syarul/feature/module
Browse files Browse the repository at this point in the history
Feature/module
  • Loading branch information
syarul authored Nov 26, 2023
2 parents e49f9bb + 35c495a commit 0078ae3
Show file tree
Hide file tree
Showing 12 changed files with 575 additions and 463 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function getPlayer (gameId) {
}
```

Then configure `requrse` to use these methods
Then configure `reQurse` to use these methods
```js
const confParams = {
getPlayer, getClass, getProgression, getSupport, getVanguard
Expand Down Expand Up @@ -221,7 +221,7 @@ const inventoryData = {
Demonstrate usage of method/computed field to return value that you need, in this case `count` which came from a relational collection that store the value only, you can use such logic to build a powerful query for your api.
```js
/**
* Helper function to get a item by ID.
* Helper function to get an item by ID.
*/
function getItem (count, id) {
// Returning a promise just to illustrate query support.
Expand All @@ -236,7 +236,7 @@ function getInventory ({ id }) {
}
```

Extends the requrse methods/config
Extends the reQurse methods/config
```js
const extConfig = {
methods: {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@syarul/requrse",
"version": "0.2.6",
"version": "0.2.7",
"type": "module",
"description": "Lightweight driven query language",
"main": "libs/executor.cjs",
Expand All @@ -11,9 +11,9 @@
"test:redis": "node samples/redis/redis.test.mjs",
"test:requrse": "node test/requrse.test.mjs",
"test:inflight-request-cancelation": "node test/inflightRequestCancelation.test.mjs",
"test:dems": "node test/dems.test.mjs",
"test:fantasy": "node test/fantasy.test.mjs",
"test:basic": "node test/basic.test.mjs",
"test": "npm run test:starwars && npm run test:mongoose && npm run test:mongoose-lookup && npm run test:redis && npm run test:requrse && npm run test:inflight-request-cancelation && npm run test:dems && npm run test:basic",
"test": "npm run test:starwars && npm run test:mongoose && npm run test:mongoose-lookup && npm run test:redis && npm run test:requrse && npm run test:inflight-request-cancelation && npm run test:fantasy && npm run test:basic",
"test:coverage": "c8 --exclude samples --exclude test npm run test",
"test:lcov": "c8 --exclude samples --exclude test --reporter lcov npm run test",
"coverage": "coveralls < coverage/lcov.info"
Expand Down
11 changes: 3 additions & 8 deletions samples/mongoose/mongoose-lookup.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'assert'
import mongoose from 'mongoose'
import requrseMongoose from './mongoose.middleware.mjs'
import { test as testFixture } from '../../test/fixture/test.mjs'

mongoose.Promise = global.Promise

Expand All @@ -11,14 +12,8 @@ mongoose
process.exit(1)
})

const test = (result, expected, msg = '') => {
try {
assert.deepEqual(result, expected)
console.log(`\r\n:: ${msg} ::\r\n`)
} catch(e) {
console.log(`\r\n:: Test failed: ${msg} ::`)
console.error(e)
}
const test = async (result, expected, msg = '') => {
await testFixture(msg, () => assert.deepEqual(result, expected))
}

/**
Expand Down
4 changes: 2 additions & 2 deletions samples/mongoose/mongoose.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'assert'
import mongoose from 'mongoose'
import requrseMongoose from './mongoose.middleware.mjs'
import { test as testFixture } from '../../test/fixture/test.mjs'

mongoose.Promise = global.Promise

Expand All @@ -13,9 +14,8 @@ mongoose

let arg
const test = async function(msg, run) {
console.log(`\r\n :: ${msg} ::\r\n`)
try {
arg = await run(arg)
arg = await testFixture(msg, run, arg)
} catch (e) {
console.error(e)
}
Expand Down
1 change: 0 additions & 1 deletion samples/redis/redis.middleware.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const middleware = (query, { redis, redisKey, memberKey }) => rq(query, {
return newData
},
async getMemberKeys () {

return { keys: (await redis.smembers(memberKey)).sort() }
},
async remove ({ id }) {
Expand Down
67 changes: 36 additions & 31 deletions samples/redis/redis.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'assert'
import Redis from 'ioredis'
import dotenv from 'dotenv'
import requrseRedis from './redis.middleware.mjs'
import { test } from '../../test/fixture/test.mjs'

dotenv.config()

Expand Down Expand Up @@ -79,33 +80,50 @@ async function remove (id) {
return books
}

async function test () {
await test('Save book', async () => {
for (const book of books) {
await save(book).then(console.log, console.error)
await save(book).then(result => {
assert.deepEqual(result, {
book: { create: { title: book.title } }
})
}, console.error)
}
})

await find()
await test('Find book', () =>
find()
.then(result => {
assert.deepEqual(result, { book: { find: { genre: 'Science Fiction' } } })
}, console.error)
)

let keys
let keys

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

await update(keys[0], {
await test('Update book title', () =>
update(keys[0], {
title: 'Harry Potter and the Prisoner of Azkaban'
}).then(console.log, console.error)
}).then(result => {
assert.deepEqual(result, {
book: { update: { title: 'Harry Potter and the Prisoner of Azkaban' } }
})
}, console.error)
)

await requrseRedis({
await test('Find book by genre', () =>
requrseRedis({
book: {
find: {
$params: {
Expand All @@ -115,7 +133,6 @@ async function test () {
}
}
}, modelOptions).then(result => {
console.log(result)
assert.deepEqual(result, {
book: {
find: {
Expand All @@ -124,29 +141,19 @@ 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)

await test('Remove keys, secondary keys', async () => {
for (const key of keys) {
await remove(key).then(console.log, console.error)
await remove(key).then(result => {
assert.deepEqual(result, { book: { remove: { id: 1 } } })
}, console.error)
}
})

await find()
await test('Confirm no more records exist', () =>
find()
.then(result => {
console.log(result)
assert.deepEqual(result, {
book: {
find: {
Expand All @@ -155,8 +162,6 @@ async function test () {
}
})
}, console.error)
}
)

test().then(() => {
redis.disconnect()
})
redis.disconnect()
7 changes: 5 additions & 2 deletions samples/starwars/starwars.test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import assert from 'assert'
import requrseExec from './starwars.middleware.mjs'
import { test as testFixture } from '../../test/fixture/test.mjs'

// suppress console.error message for this test
console.error = () => {}

const testList = [
0,
Expand All @@ -17,8 +21,7 @@ const testList = [

const test = (num, msg, run) => {
if (testList.includes(num)) {
console.log(`\r\n :: ${msg} ::\r\n`)
Promise.resolve(run())
Promise.resolve(testFixture(msg, run))
}
}

Expand Down
Loading

0 comments on commit 0078ae3

Please sign in to comment.