-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add gets to websockets * 7.0.1
- Loading branch information
Showing
8 changed files
with
231 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const writeResponse = require('write-response'); | ||
|
||
const orderByFields = require('../../../utils/orderByFields'); | ||
|
||
const { | ||
COMMAND, | ||
GET, | ||
STATUS, | ||
DATA, | ||
COLLECTION_ID, | ||
QUERY, | ||
DOCUMENTS, | ||
FIELDS, | ||
ORDER, | ||
LIMIT | ||
} = require('../../constants'); | ||
|
||
function askOnAllNodes (state, data) { | ||
return Promise.all( | ||
state.nodes.map(node => node.connection.send(data)) | ||
); | ||
} | ||
|
||
async function handleGetAll (state, request, response, { collectionId, url }) { | ||
const limit = url.searchParams.get('limit') && JSON.parse(url.searchParams.get('limit')); | ||
const orders = url.searchParams.get('order') && JSON.parse(url.searchParams.get('order')); | ||
|
||
const responses = await askOnAllNodes(state, { | ||
[COMMAND]: GET, | ||
[DATA]: { | ||
[COLLECTION_ID]: collectionId, | ||
[QUERY]: url.searchParams.get('query') && JSON.parse(url.searchParams.get('query')), | ||
[FIELDS]: url.searchParams.get('fields') && JSON.parse(url.searchParams.get('fields')), | ||
[ORDER]: orders || undefined, | ||
[LIMIT]: limit || undefined | ||
} | ||
}); | ||
|
||
if (responses.find(response => response[STATUS] >= 500)) { | ||
writeResponse(500, responses[0][DATA], response); | ||
return; | ||
} | ||
|
||
if (!responses.find(response => response[STATUS] === 200)) { | ||
writeResponse(200, [], response); | ||
return; | ||
} | ||
|
||
let results = responses | ||
.map(response => response[DOCUMENTS]) | ||
.flat() | ||
.filter(item => !!item); | ||
|
||
if (limit) { | ||
results = results.slice(0, limit); | ||
} | ||
|
||
if (orders) { | ||
orders.forEach(order => { | ||
orderByFields(results, order); | ||
}); | ||
} | ||
|
||
writeResponse(200, results, response); | ||
} | ||
|
||
module.exports = handleGetAll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const writeResponse = require('write-response'); | ||
|
||
const { | ||
COMMAND, | ||
GET, | ||
STATUS, | ||
DATA, | ||
COLLECTION_ID, | ||
RESOURCE_ID, | ||
QUERY, | ||
LIMIT, | ||
DOCUMENTS, | ||
FIELDS | ||
} = require('../../constants'); | ||
|
||
function askOnAllNodes (state, data) { | ||
return Promise.all( | ||
state.nodes.map(node => node.connection.send(data)) | ||
); | ||
} | ||
|
||
async function handleGetOne (state, request, response, { collectionId, resourceId, url }) { | ||
const responses = await askOnAllNodes(state, { | ||
[COMMAND]: GET, | ||
[DATA]: { | ||
[COLLECTION_ID]: collectionId, | ||
[QUERY]: url.searchParams.get('query') && JSON.parse(url.searchParams.get('query')), | ||
[RESOURCE_ID]: resourceId, | ||
[LIMIT]: 1, | ||
[FIELDS]: url.searchParams.get('fields') && JSON.parse(url.searchParams.get('fields')) | ||
} | ||
}); | ||
|
||
if (responses.find(response => response[STATUS] >= 500)) { | ||
writeResponse(500, responses[0][DATA], response); | ||
return; | ||
} | ||
|
||
if (!responses.find(response => response[STATUS] === 200)) { | ||
writeResponse(404, {}, response); | ||
return; | ||
} | ||
|
||
const results = responses | ||
.map(response => response[DOCUMENTS] || []) | ||
.flat(); | ||
|
||
if (results.length === 0) { | ||
writeResponse(404, {}, response); | ||
return; | ||
} | ||
|
||
writeResponse(200, results[0], response); | ||
} | ||
|
||
module.exports = handleGetOne; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const { | ||
COMMAND, | ||
DATA, | ||
GET, | ||
STATUS, | ||
DOCUMENTS, | ||
LIMIT, | ||
ORDER | ||
} = require('../../constants'); | ||
|
||
const orderByFields = require('../../../utils/orderByFields'); | ||
|
||
function askOnAllNodes (state, data) { | ||
return Promise.all( | ||
state.nodes.map(node => node.connection.send(data)) | ||
); | ||
} | ||
|
||
async function handleGetAll (acceptId, state, data, socket) { | ||
const responses = await askOnAllNodes(state, { | ||
[COMMAND]: GET, | ||
[DATA]: data | ||
}); | ||
|
||
if (responses.find(response => response[STATUS] >= 500)) { | ||
socket.send(JSON.stringify(['A', acceptId, responses])); | ||
return; | ||
} | ||
|
||
if (!responses.find(response => response[STATUS] === 200)) { | ||
socket.send(JSON.stringify(['A', acceptId, []])); | ||
return; | ||
} | ||
|
||
let results = responses | ||
.map(response => response[DOCUMENTS]) | ||
.flat() | ||
.filter(item => !!item); | ||
|
||
if (data[LIMIT]) { | ||
results = results.slice(0, data[LIMIT]); | ||
} | ||
|
||
if (data[ORDER]) { | ||
data[ORDER].forEach(order => { | ||
orderByFields(results, order); | ||
}); | ||
} | ||
|
||
socket.send(JSON.stringify(['A', acceptId, results])); | ||
} | ||
|
||
module.exports = handleGetAll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters