Skip to content

Commit

Permalink
refactor: Export core-database typedefs (#1905)
Browse files Browse the repository at this point in the history
* refactor: Export core-database typedefs

* refactor: Use core-database typdefs in core-database-postgres

* fix: failing tests

* refactor: Export core-database-postgres typedefs
refactor: Remove un-used SPV.connection property
refactor: Remove ConnectionInterface.connection property. This available in the 'options' object that's passed via constructor.
refactor: Use inline-initialization instead of constructor initialization for concise code, unless we're accessing constructor parameters.

* refactor: Use core-database-postgres typedefs in other modules
refactor: more inline-initialization vs constructor
refactor: Removed some redundant vars in core-api Repositories, use inherited references instead.
  • Loading branch information
paroxysm authored and faustbrian committed Dec 25, 2018
1 parent 6466030 commit c5a235b
Show file tree
Hide file tree
Showing 56 changed files with 205 additions and 219 deletions.
7 changes: 4 additions & 3 deletions packages/core-api/__tests__/__support__/setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { setUpContainer } from "@arkecosystem/core-test-utils/src/helpers/container";

import { delegates } from "../../../core-test-utils/src/fixtures/testnet/delegates";
import { delegates } from "../../../core-test-utils/src/fixtures";
import { generateRound } from "./utils/generate-round";

import { queries } from "../../../core-database-postgres/src/queries";
Expand All @@ -20,7 +21,7 @@ async function setUp() {
],
});

const connection = app.resolvePlugin("database");
const connection = app.resolvePlugin<PostgresConnection>("database");
await connection.db.rounds.truncate();
await connection.buildWallets(1);
await connection.saveWallets(true);
Expand All @@ -32,7 +33,7 @@ async function tearDown() {
}

async function calculateRanks() {
const connection = app.resolvePlugin("database");
const connection = app.resolvePlugin<PostgresConnection>("database");

const rows = await connection.query.manyOrNone(queries.spv.delegatesRanks);

Expand Down
7 changes: 4 additions & 3 deletions packages/core-api/__tests__/v2/handlers/blocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { utils } from "../utils";

import { models } from "@arkecosystem/crypto";
import genesisBlock from "../../../../core-test-utils/src/config/testnet/genesisBlock.json";
import { blocks2to100 } from "../../../../core-test-utils/src/fixtures/testnet/blocks2to100";
import { resetBlockchain } from "../../../../core-test-utils/src/helpers/blockchain";
import { blocks2to100 } from "../../../../core-test-utils/src/fixtures";
import { resetBlockchain } from "../../../../core-test-utils/src/helpers";

import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";

const container = app;
const { Block } = models;
Expand Down Expand Up @@ -145,7 +146,7 @@ describe("API 2.0 - Blocks", () => {
it("should POST a search for blocks with the exact specified previousBlock", async () => {
// save a new block so that we can make the request with previousBlock
const block2 = new Block(blocks2to100[0]);
const database = container.resolvePlugin("database");
const database = container.resolvePlugin<PostgresConnection>("database");
await database.saveBlock(block2);

const response = await utils[request]("POST", "blocks/search", {
Expand Down
13 changes: 8 additions & 5 deletions packages/core-api/__tests__/v2/handlers/delegates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { models } from "@arkecosystem/crypto";
const { Block } = models;

import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";

const delegate = {
username: "genesis_9",
Expand Down Expand Up @@ -63,8 +64,9 @@ describe("API 2.0 - Delegates", () => {
expect(response.data.data).toBeArray();

response.data.data.forEach(utils.expectDelegate);
expect(response.data.data.sort((a, b) =>
a.production.productivity > b.production.productivity)).toEqual(response.data.data);
expect(
response.data.data.sort((a, b) => a.production.productivity > b.production.productivity),
).toEqual(response.data.data);
});
},
);
Expand All @@ -78,8 +80,9 @@ describe("API 2.0 - Delegates", () => {
expect(response.data.data).toBeArray();

response.data.data.forEach(utils.expectDelegate);
expect(response.data.data.sort((a, b) =>
a.production.approval > b.production.approval)).toEqual(response.data.data);
expect(response.data.data.sort((a, b) => a.production.approval > b.production.approval)).toEqual(
response.data.data,
);
});
},
);
Expand Down Expand Up @@ -152,7 +155,7 @@ describe("API 2.0 - Delegates", () => {
it("should GET all blocks for a delegate by the given identifier", async () => {
// save a new block so that we can make the request with generatorPublicKey
const block2 = new Block(blocks2to100[0]);
const database = app.resolvePlugin("database");
const database = app.resolvePlugin<PostgresConnection>("database");
await database.saveBlock(block2);

const response = await utils[request](
Expand Down
1 change: 1 addition & 0 deletions packages/core-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"dependencies": {
"@arkecosystem/core-container": "^2.1.0",
"@arkecosystem/core-database-postgres": "^2.1.0",
"@arkecosystem/core-logger": "^2.1.0",
"@arkecosystem/core-http-utils": "^2.1.0",
"@arkecosystem/core-transaction-pool": "^2.1.0",
Expand Down
2 changes: 0 additions & 2 deletions packages/core-api/src/interfaces/repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Hapi from "hapi";

export interface IRepository {
database: any;
cache: any;
Expand Down
7 changes: 5 additions & 2 deletions packages/core-api/src/repositories/blocks.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { app } from "@arkecosystem/core-container";
import { IRepository } from "../interfaces/repository";
import { Repository } from "./repository";
import { buildFilterQuery } from "./utils/build-filter-query";

export class BlockRepository extends Repository implements IRepository {
constructor() {
super();
}

/**
* Get all blocks for the given parameters.
* @param {Object} parameters
Expand Down Expand Up @@ -115,7 +118,7 @@ export class BlockRepository extends Repository implements IRepository {
});
}

public getModel(): object {
public getModel(): any {
return this.database.models.block;
}

Expand Down
28 changes: 13 additions & 15 deletions packages/core-api/src/repositories/repository.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import snakeCase from "lodash/snakeCase";

export class Repository {
public database: any;
public cache: any;
public transactionPool: any;
public model: any;
public query: any;
import { IRepository } from "../interfaces/repository";

export abstract class Repository implements IRepository {
public database = app.resolvePlugin<PostgresConnection>("database");
public cache = this.database.getCache();
public transactionPool = app.resolvePlugin("transactionPool");
public model = this.getModel();
public query = this.model.query();
public columns: string[] = [];

public constructor() {
this.database = app.resolvePlugin("database");
this.cache = this.database.getCache();
this.transactionPool = app.resolvePlugin("transactionPool");
// @ts-ignore
this.model = this.getModel();
this.query = this.model.query();

protected constructor() {
this.__mapColumns();
}

// todo: Introduce a generic param to return type-safe models
public abstract getModel(): any;

public async _find(query): Promise<any> {
return this.database.query.oneOrNone(query.toQuery());
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core-api/src/repositories/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { app } from "@arkecosystem/core-container";
import { constants, slots } from "@arkecosystem/crypto";
import dayjs from "dayjs-ext";
import partition from "lodash/partition";
Expand All @@ -7,6 +6,10 @@ import { Repository } from "./repository";
import { buildFilterQuery } from "./utils/build-filter-query";

export class TransactionsRepository extends Repository implements IRepository {
constructor() {
super();
}

/**
* Get all transactions.
* @param {Object} params
Expand Down
3 changes: 2 additions & 1 deletion packages/core-api/src/versions/1/accounts/methods.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { generateCacheKey, getCacheTimeout } from "../../utils";
import { paginate, respondWith, toCollection, toResource } from "../utils";

const database = app.resolvePlugin("database");
const database = app.resolvePlugin<PostgresConnection>("database");

const index = async request => {
const { rows } = await database.wallets.findAll({
Expand Down
3 changes: 2 additions & 1 deletion packages/core-api/src/versions/1/delegates/methods.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { generateCacheKey, getCacheTimeout } from "../../utils";
import { paginate, respondWith, toCollection, toResource } from "../utils";

const database = app.resolvePlugin("database");
const database = app.resolvePlugin<PostgresConnection>("database");

const index = async request => {
const { count, rows } = await database.delegates.paginate({
Expand Down
17 changes: 6 additions & 11 deletions packages/core-api/src/versions/1/shared/controller.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { AbstractLogger } from "@arkecosystem/core-logger";
import Hapi from "hapi";
import { paginate, respondWith, respondWithCache, toCollection, toResource } from "../utils";

export class Controller {
protected config: any;
protected blockchain: any;
protected database: any;
protected logger: any;

public constructor() {
this.config = app.getConfig();
this.blockchain = app.resolvePlugin("blockchain");
this.database = app.resolvePlugin("database");
this.logger = app.resolvePlugin("logger");
}
protected config = app.getConfig();
protected blockchain = app.resolvePlugin("blockchain");
protected database = app.resolvePlugin<PostgresConnection>("database");
protected logger = app.resolvePlugin<AbstractLogger>("logger");

protected paginate(request: Hapi.Request): any {
return paginate(request);
Expand Down
3 changes: 2 additions & 1 deletion packages/core-api/src/versions/2/blocks/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { bignumify, formatTimestamp } from "@arkecosystem/core-utils";

export function transformBlock(model) {
const database = app.resolvePlugin("database");
const database = app.resolvePlugin<PostgresConnection>("database");
const generator = database.walletManager.findByPublicKey(model.generatorPublicKey);

model.reward = bignumify(model.reward);
Expand Down
3 changes: 2 additions & 1 deletion packages/core-api/src/versions/2/delegates/methods.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import Boom from "boom";
import orderBy from "lodash/orderBy";
import { blocksRepository } from "../../../repositories";
import { generateCacheKey, getCacheTimeout } from "../../utils";
import { paginate, respondWithResource, toPagination } from "../utils";

const database = app.resolvePlugin("database");
const database = app.resolvePlugin<PostgresConnection>("database");

const index = async request => {
const delegates = await database.delegates.paginate({
Expand Down
13 changes: 4 additions & 9 deletions packages/core-api/src/versions/2/shared/controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";

import Hapi from "hapi";
import {
Expand All @@ -12,15 +13,9 @@ import {
} from "../utils";

export class Controller {
protected config: any;
protected blockchain: any;
protected database: any;

public constructor() {
this.config = app.getConfig();
this.blockchain = app.resolvePlugin("blockchain");
this.database = app.resolvePlugin("database");
}
protected config = app.getConfig();
protected blockchain = app.resolvePlugin("blockchain");
protected database = app.resolvePlugin<PostgresConnection>("database");

protected paginate(request: Hapi.Request): any {
return paginate(request);
Expand Down
3 changes: 2 additions & 1 deletion packages/core-api/src/versions/2/wallets/methods.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import Boom from "boom";
import { transactionsRepository } from "../../../repositories";
import { generateCacheKey, getCacheTimeout } from "../../utils";
import { paginate, respondWithResource, toPagination } from "../utils";

const database = app.resolvePlugin("database");
const database = app.resolvePlugin<PostgresConnection>("database");

const index = async request => {
const wallets = await database.wallets.findAll({
Expand Down
1 change: 1 addition & 0 deletions packages/core-blockchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"dependencies": {
"@arkecosystem/core-container": "^2.1.0",
"@arkecosystem/core-database-postgres": "^2.1.0",
"@arkecosystem/core-logger": "^2.1.0",
"@arkecosystem/core-utils": "^2.1.0",
"@arkecosystem/crypto": "^2.1.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/core-blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* tslint:disable:max-line-length */
import { app } from "@arkecosystem/core-container";
import { PostgresConnection } from "@arkecosystem/core-database-postgres";
import { AbstractLogger } from "@arkecosystem/core-logger";
import { models, slots } from "@arkecosystem/crypto";

Expand Down Expand Up @@ -690,7 +691,7 @@ export class Blockchain {
* @return {ConnectionInterface}
*/
get database() {
return app.resolvePlugin("database");
return app.resolvePlugin<PostgresConnection>("database");
}

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/core-container/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ export class Container {
logger.info("Ark Core is trying to gracefully shut down to avoid data corruption :pizza:");

try {
/* TODO: core-database-postgres has a dep on core-container. Yet we have code in core-container fetching a reference to core-database-postgres.
If we try to import core-database-postgres types, we create a circular dependency: core-container -> core-database-postgres -> core-container.
The only thing we're doing here is trying to save the wallets upon shutdown. The code can and should be moved into core-database-postgres instead
and leverage either the plugins `tearDown` method or the event-emitter's 'shutdown' event
*/
const database = this.resolvePlugin("database");
if (database) {
const emitter = this.resolvePlugin("event-emitter");
Expand Down
3 changes: 2 additions & 1 deletion packages/core-database-postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"Brian Faust <brian@ark.io>"
],
"license": "MIT",
"main": "dist/index.js",
"main": "dist/index",
"types": "dist/index",
"files": [
"dist"
],
Expand Down
9 changes: 4 additions & 5 deletions packages/core-database-postgres/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ import { Bignum, models } from "@arkecosystem/crypto";
import { SPV } from "./spv";

import { migrations } from "./migrations";
import { Model } from "./models";
import { repositories } from "./repositories";
import { QueryExecutor } from "./sql/query-executor";
import { camelizeColumns } from "./utils";

const { Block, Transaction } = models;

export class PostgresConnection extends ConnectionInterface {
public models: {};
public models: { [key: string]: Model } = {};
public query: QueryExecutor;
private db: any;
public db: any;
private cache: Map<any, any>;
private pgp: any;
private spvFinished: boolean;
Expand Down Expand Up @@ -175,7 +176,7 @@ export class PostgresConnection extends ConnectionInterface {
* @param {Array} delegates
* @return {Array}
*/
public async getActiveDelegates(height, delegates) {
public async getActiveDelegates(height, delegates?) {
const maxDelegates = this.config.getMilestone(height).activeDelegates;
const round = Math.floor((height - 1) / maxDelegates) + 1;

Expand Down Expand Up @@ -660,8 +661,6 @@ export class PostgresConnection extends ConnectionInterface {
* @return {void}
*/
public async __registerModels() {
this.models = {};

for (const [key, Value] of Object.entries(require("./models"))) {
this.models[key.toLowerCase()] = new (Value as any)(this.pgp);
}
Expand Down
Loading

0 comments on commit c5a235b

Please sign in to comment.