Use postgres instead of sqlite. (#38)

This commit is contained in:
Ashwin Phatak
2021-06-04 15:19:30 +05:30
committed by GitHub
parent 4e0ef7c852
commit 3aaa9dd7f8
6 changed files with 144 additions and 227 deletions
+7 -3
View File
@@ -3,10 +3,14 @@
port = 3001
[database]
type = "sqlite"
database = "out/indexer.db"
type = "postgres"
host = "localhost"
port = 5432
database = "erc20-watcher"
username = "postgres"
password = "postgres"
synchronize = true
logging = false
logging = true
[upstream]
gqlEndpoint = "http://127.0.0.1:8083/graphql"
+2 -1
View File
@@ -37,10 +37,11 @@
"left-pad": "^1.3.0",
"level": "^7.0.0",
"lodash": "^4.17.21",
"pg": "^8.6.0",
"reflect-metadata": "^0.1.13",
"sqlite3": "^5.0.2",
"toml": "^3.0.0",
"typeorm": "^0.2.32",
"typeorm-naming-strategies": "^2.0.0",
"yargs": "^17.0.1"
},
"devDependencies": {
+11 -6
View File
@@ -1,5 +1,6 @@
import assert from "assert";
import { Connection, createConnection } from "typeorm";
import { SnakeNamingStrategy } from 'typeorm-naming-strategies';
import { Allowance } from "./entity/Allowance";
import { Balance } from "./entity/Balance";
@@ -18,13 +19,17 @@ export class Database {
async init() {
assert(!this._conn);
this._conn = await createConnection(this._config);
this._conn = await createConnection({
...this._config,
namingStrategy: new SnakeNamingStrategy()
});
}
async getBalance({ blockHash, token, owner }) {
return this._conn.getRepository(Balance)
.createQueryBuilder("balance")
.where("blockHash = :blockHash AND token = :token AND owner = :owner", {
.where("block_hash = :blockHash AND token = :token AND owner = :owner", {
blockHash,
token,
owner
@@ -35,7 +40,7 @@ export class Database {
async getAllowance({ blockHash, token, owner, spender }) {
return this._conn.getRepository(Allowance)
.createQueryBuilder("allowance")
.where("blockHash = :blockHash AND token = :token AND owner = :owner AND spender = :spender", {
.where("block_hash = :blockHash AND token = :token AND owner = :owner AND spender = :spender", {
blockHash,
token,
owner,
@@ -60,7 +65,7 @@ export class Database {
async didSyncEvents({ blockHash, token }) {
const numRows = await this._conn.getRepository(EventSyncProgress)
.createQueryBuilder()
.where("blockHash = :blockHash AND token = :token", {
.where("block_hash = :blockHash AND token = :token", {
blockHash,
token,
})
@@ -72,7 +77,7 @@ export class Database {
async getEvents({ blockHash, token }) {
return this._conn.getRepository(Event)
.createQueryBuilder("event")
.where("blockHash = :blockHash AND token = :token", {
.where("block_hash = :blockHash AND token = :token", {
blockHash,
token,
})
@@ -82,7 +87,7 @@ export class Database {
async getEventsByName({ blockHash, token, eventName }) {
return this._conn.getRepository(Event)
.createQueryBuilder("event")
.where("blockHash = :blockHash AND token = :token AND :eventName = :eventName", {
.where("block_hash = :blockHash AND token = :token AND :eventName = :eventName", {
blockHash,
token,
eventName
+4 -4
View File
@@ -75,7 +75,7 @@ export class Indexer {
log(JSON.stringify(result, null, 2));
const { value, proof } = result;
await this._db.saveBalance({ blockHash, token, owner, value, proof: JSON.stringify(proof) });
await this._db.saveBalance({ blockHash, token, owner, value: BigInt(value), proof: JSON.stringify(proof) });
return result;
}
@@ -103,7 +103,7 @@ export class Indexer {
log(JSON.stringify(result, null, 2));
const { value, proof } = result;
await this._db.saveAllowance({ blockHash, token, owner, spender, value, proof: JSON.stringify(proof) });
await this._db.saveAllowance({ blockHash, token, owner, spender, value: BigInt(value), proof: JSON.stringify(proof) });
return result;
}
@@ -226,13 +226,13 @@ export class Indexer {
case 'Transfer': {
event['transferFrom'] = address1;
event['transferTo'] = address2;
event['transferValue'] = value;
event['transferValue'] = BigInt(value);
break;
};
case 'Approval': {
event['approvalOwner'] = address1;
event['approvalSpender'] = address2;
event['approvalValue'] = value;
event['approvalValue'] = BigInt(value);
break;
};
}