Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support afterID in queries #30

Draft
wants to merge 1 commit into
base: hive
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require('dotenv').config();
const { MongoClient } = require('mongodb');
const { MongoClient, ObjectId } = require('mongodb');
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
Expand Down Expand Up @@ -34,6 +34,7 @@ historyRouter.get('/', async (req, res) => {
symbol,
timestampStart,
timestampEnd,
afterID,
} = query;

console.log(`[${pid}] got request: ${JSON.stringify(query)} with IP ${ip}`);
Expand Down Expand Up @@ -84,8 +85,12 @@ historyRouter.get('/', async (req, res) => {
};
}

if (afterID) {
mongoQuery._id = { $lt: ObjectId(afterID) };
}

const result = await accountsHistoryColl.find(mongoQuery, {
sort: { timestamp: -1 },
sort: { timestamp: -1, _id: -1 },
skip: sOffset,
limit: sLimit,
}).toArray();
Expand Down Expand Up @@ -116,6 +121,7 @@ nftHistoryRouter.get('/', async (req, res) => {
limit,
timestampStart,
timestampEnd,
afterID,
} = query;

console.log(`[${pid}] got nft request: ${JSON.stringify(query)} with IP ${ip}`);
Expand Down Expand Up @@ -175,6 +181,10 @@ nftHistoryRouter.get('/', async (req, res) => {
};
}

if (afterID) {
matchQuery._id = { $lt: ObjectId(afterID) };
}

const mongoQuery = [
{
$match: matchQuery,
Expand All @@ -201,7 +211,7 @@ nftHistoryRouter.get('/', async (req, res) => {
fromItems: 0,
},
},
{ $sort: { timestamp: -1 } },
{ $sort: { timestamp: -1, _id: -1 } },
{ $skip: sOffset },
{ $limit: sLimit },
];
Expand Down Expand Up @@ -232,6 +242,7 @@ marketRouter.get('/', async (req, res) => {
symbol,
timestampStart,
timestampEnd,
afterID,
} = query;

console.log(`[${pid}] got market request: ${JSON.stringify(query)} with IP ${ip}`);
Expand All @@ -255,8 +266,12 @@ marketRouter.get('/', async (req, res) => {
};
}

if (afterID) {
mongoQuery._id = { $lt: ObjectId(afterID) };
}

const result = await marketHistoryColl.find(mongoQuery, {
sort: { timestamp: -1 },
sort: { timestamp: -1, _id: -1 },
limit: 500,
}).toArray();

Expand Down