taapi-cache
Version:
TAAPI.IO Cache Package. A convenient way to fetch candles, store them and reuse them.
216 lines (181 loc) • 5.01 kB
JavaScript
var moment = require('moment');
var mongodb = require("mongodb");
const config = require("../config/config");
const chalk = require("chalk");
class Database {
constructor() {
this.db = null;
}
async getDatabase(showOutputMessages) {
if (showOutputMessages !== true) {
showOutputMessages = false;
}
if (!this.db) {
if (showOutputMessages) {
console.log("Connecting to MongoDB...");
}
let self = this;
await mongodb.MongoClient.connect(config.database.uri(), {
useNewUrlParser: true,
useUnifiedTopology: true
}, async function (err, client) {
if (err) throw err;
if (showOutputMessages) {
console.log("Connected successfully to database!");
}
self.db = await client.db(config.database.name);
});
}
return this.db;
}
async getCacheTickers(provider, source, symbol, interval) {
let query = {
provider: provider,
source: source,
symbol: symbol,
interval: interval
};
if (!provider) {
delete query.provider;
}
if (!source) {
delete query.source;
}
if (!symbol) {
delete query.symbol;
}
if (!interval) {
delete query.interval;
}
let db = await this.getDatabase();
let cacheTickers = await db.collection("CacheTickers").find(query).project({
_id: 1,
provider: 1,
source: 1,
symbol: 1,
interval: 1,
reset: 1
}).toArray();
return cacheTickers;
}
async getCachedCandles(provider, source, symbol, interval, limit) {
if (limit == -1) {
limit = 0;
} else {
limit = limit || 300;
}
let db = await this.getDatabase();
let candles = await db.collection(this.getCandleCacheCollectionName(provider, source)).find({
symbol: symbol,
interval: interval
}).project({
timestamp: 1,
open: 1,
high: 1,
low: 1,
close: 1,
volume: 1
}).sort({
timestamp: -1
}).limit(limit).toArray();
return candles.reverse();
}
async addCacheTicker(provider, source, symbol, interval) {
await this.getDatabase().then(db => {
//const key = `${provider}_${source}_${symbol}_${interval}`;
const doc = {
provider: provider,
source: source.toLowerCase(),
symbol: symbol,
interval: interval,
reset: true
};
db.collection("CacheTickers").updateOne({
provider: provider,
source: source,
symbol: symbol,
interval: interval
}, {
$set: doc
}, {
upsert: true // Note the upsert here
}).then(async result => {
if (result.result && result.result.upserted) {// Do nothing
}
});
});
}
async updateCacheTicker(id, data) {
let db = await this.getDatabase();
await db.collection("CacheTickers").updateOne({
"_id": new mongodb.ObjectID(id)
}, {
$set: data
}).then(result => {
//response = result;
if (result.result && result.result.upserted) {// Do nothing
}
});
}
async deleteCacheTickers(id, provider, source, symbol, interval) {
let query = {};
if (id) {
query._id = new mongodb.ObjectID(`${id}`);
}
if (provider) {
query.provider = provider;
}
if (source) {
query.source = source;
}
if (symbol) {
query.symbol = symbol;
}
if (interval) {
query.interval = interval;
}
let db = await this.getDatabase();
let deletedCount = 0;
await db.collection("CacheTickers").deleteMany(query).then(result => {
deletedCount = result.deletedCount;
});
return deletedCount;
}
getCandleCacheCollectionName(provider, source) {
return `CandleCache_${provider}_${source}`;
}
async deleteCandles(provider, source, symbol, interval) {
const self = this;
await this.getDatabase().then(db => {
db.collection(self.getCandleCacheCollectionName(provider, source)).deleteMany({
symbol: symbol,
interval: interval
}).then(async result => {});
});
}
async insertCandles(provider, source, candles) {
const self = this;
await this.getDatabase().then(db => {
db.collection(self.getCandleCacheCollectionName(provider, source)).insertMany(candles).then(result => {//response = result;
});
});
}
async upsertCandles(provider, source, symbol, interval, candles) {
const self = this;
await this.getDatabase().then(db => {
for (let candleKey in candles) {
let candle = candles[candleKey];
db.collection(self.getCandleCacheCollectionName(provider, source)).updateOne({
"symbol": symbol,
"interval": interval,
"timestamp": candle.timestamp
}, {
$set: candle
}, {
upsert: true // Note the upsert here
}).then(result => {});
}
});
}
}
module.exports = new Database();