Skip to content

Commit

Permalink
refactor: move core-api repositories into core-database-* (#2107)
Browse files Browse the repository at this point in the history
  • Loading branch information
paroxysm authored and faustbrian committed Feb 20, 2019
1 parent 92d25e2 commit 57374e6
Show file tree
Hide file tree
Showing 41 changed files with 983 additions and 126 deletions.
3 changes: 2 additions & 1 deletion packages/core-api/src/repositories/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IRepository } from "../interfaces";
import { Repository } from "./repository";
import { buildFilterQuery } from "./utils/build-filter-query";

// TODO: Deprecate this with v1 code
export class BlockRepository extends Repository implements IRepository {
constructor() {
super();
Expand Down Expand Up @@ -66,7 +67,7 @@ export class BlockRepository extends Repository implements IRepository {

/**
* Get the last block for the given generator.
* TODO is this right?
* TODO is this right? - This is unused anyway
* @param {String} generatorPublicKey
* @return {Object}
*/
Expand Down
2 changes: 2 additions & 0 deletions packages/core-api/src/repositories/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Database, TransactionPool } from "@arkecosystem/core-interfaces";
import snakeCase from "lodash/snakeCase";
import { IRepository } from "../interfaces";


// TODO: Deprecate this with v1
export abstract class Repository implements IRepository {
public databaseService = app.resolvePlugin<Database.IDatabaseService>("database");
public cache = this.databaseService.cache;
Expand Down
2 changes: 2 additions & 0 deletions packages/core-api/src/repositories/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { IRepository } from "../interfaces";
import { Repository } from "./repository";
import { buildFilterQuery } from "./utils/build-filter-query";


// TODO: Deprecate this with v1
export class TransactionsRepository extends Repository implements IRepository {
constructor() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function buildFilterQuery(parameters, filters) {
}

if (parameters[elem].hasOwnProperty("from") || parameters[elem].hasOwnProperty("to")) {
// 'where' is declared to be an array, yet 'elem' is a string. Why are we using a string as a numerical index?
where[elem] = {};

if (parameters[elem].hasOwnProperty("from")) {
Expand Down
7 changes: 6 additions & 1 deletion packages/core-api/src/versions/2/blocks/methods.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { app } from "@arkecosystem/core-container";
import { Database } from "@arkecosystem/core-interfaces";
import Boom from "boom";
import { blocksRepository, transactionsRepository } from "../../../repositories";
import { ServerCache } from "../../../services";
import { paginate, respondWithResource, toPagination } from "../utils";

const databaseService = app.resolvePlugin<Database.IDatabaseService>("database");
const blocksRepository = databaseService.blocksBusinessRepository;
const transactionsRepository = databaseService.transactionsBusinessRepository;

const index = async request => {
const blocks = await blocksRepository.findAll({
...request.query,
Expand Down
5 changes: 3 additions & 2 deletions packages/core-api/src/versions/2/node/controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { app } from "@arkecosystem/core-container";
import { Database } from "@arkecosystem/core-interfaces";
import Boom from "boom";
import Hapi from "hapi";
import { transactionsRepository } from "../../../repositories";
import { Controller } from "../shared/controller";

export class NodeController extends Controller {
Expand Down Expand Up @@ -42,7 +42,8 @@ export class NodeController extends Controller {

public async configuration(request: Hapi.Request, h: Hapi.ResponseToolkit) {
try {
const feeStatisticsData = await transactionsRepository.getFeeStatistics();
const transactionsBusinessRepository = app.resolvePlugin<Database.IDatabaseService>("database").transactionsBusinessRepository;
const feeStatisticsData = await transactionsBusinessRepository.getFeeStatistics();

const network = this.config.get("network");
const dynamicFees = app.resolveOptions("transactionPool").dynamicFees;
Expand Down
5 changes: 4 additions & 1 deletion packages/core-api/src/versions/2/transactions/methods.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { app } from "@arkecosystem/core-container";
import { Database } from "@arkecosystem/core-interfaces";
import Boom from "boom";
import { transactionsRepository } from "../../../repositories";
import { ServerCache } from "../../../services";
import { paginate, respondWithResource, toPagination } from "../utils";

const transactionsRepository = app.resolvePlugin<Database.IDatabaseService>("database").transactionsBusinessRepository;

const index = async request => {
const transactions = await transactionsRepository.findAll({
...request.query,
Expand Down
45 changes: 30 additions & 15 deletions packages/core-database-postgres/src/models/block.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,87 @@
import { Database } from "@arkecosystem/core-interfaces";
import { bignumify } from "@arkecosystem/core-utils";
import { Model } from "./model";

export class Block extends Model {
/**
* The table associated with the model.
* @return {String}
*/
public getTable() {
return "blocks";
}

/**
* The read-only structure with query-formatting columns.
* @return {Object}
*/
public getColumnSet() {
return this.createColumnSet([
constructor(pgp) {

super(pgp);

this.columnsDescriptor = [
{
name: "id",
supportedOperators: [ Database.SearchOperator.OP_EQ ]
},
{
name: "version",
supportedOperators: [ Database.SearchOperator.OP_EQ ]
},
{
name: "timestamp",
supportedOperators: [ Database.SearchOperator.OP_LTE, Database.SearchOperator.OP_GTE ]
},
{
name: "previous_block",
prop: "previousBlock",
def: null,
supportedOperators: [ Database.SearchOperator.OP_EQ ]
},
{
name: "height",
supportedOperators: [ Database.SearchOperator.OP_LTE, Database.SearchOperator.OP_GTE ]
},
{
name: "number_of_transactions",
prop: "numberOfTransactions",
supportedOperators: [ Database.SearchOperator.OP_LTE, Database.SearchOperator.OP_GTE ]
},
{
name: "total_amount",
prop: "totalAmount",
init: col => bignumify(col.value).toFixed(),
supportedOperators: [ Database.SearchOperator.OP_LTE, Database.SearchOperator.OP_GTE ]
},
{
name: "total_fee",
prop: "totalFee",
init: col => bignumify(col.value).toFixed(),
supportedOperators: [ Database.SearchOperator.OP_LTE, Database.SearchOperator.OP_GTE ]
},
{
name: "reward",
init: col => bignumify(col.value).toFixed(),
supportedOperators: [ Database.SearchOperator.OP_LTE, Database.SearchOperator.OP_GTE ]
},
{
name: "payload_length",
prop: "payloadLength",
supportedOperators: [ Database.SearchOperator.OP_LTE, Database.SearchOperator.OP_GTE ]
},
{
name: "payload_hash",
prop: "payloadHash",
supportedOperators: [ Database.SearchOperator.OP_EQ ]
},
{
name: "generator_public_key",
prop: "generatorPublicKey",
supportedOperators: [ Database.SearchOperator.OP_EQ ]
},
{
name: "block_signature",
prop: "blockSignature",
},
]);
supportedOperators: [ Database.SearchOperator.OP_EQ ]
}
];
}

/**
* The table associated with the model.
* @return {String}
*/
public getTable() {
return "blocks";
}

}
23 changes: 11 additions & 12 deletions packages/core-database-postgres/src/models/migration.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { Model } from "./model";

export class Migration extends Model {

constructor(pgp) {
super(pgp);

this.columnsDescriptor = [
{
name: "name"
}
];
}

/**
* The table associated with the model.
* @return {String}
*/
public getTable() {
return "migrations";
}

/**
* The read-only structure with query-formatting columns.
* @return {Object}
*/
public getColumnSet() {
return this.createColumnSet([
{
name: "name",
},
]);
}
}
51 changes: 47 additions & 4 deletions packages/core-database-postgres/src/models/model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { Database } from "@arkecosystem/core-interfaces";
import sql from "sql";

export abstract class Model {
interface ColumnDescriptor {
name: string,
supportedOperators?: Database.SearchOperator[],
prop?: string,
init?: any,
def?: any
}

export abstract class Model implements Database.IDatabaseModel {

protected columnsDescriptor: ColumnDescriptor[];
protected columnSet: any;

/**
* Create a new model instance.
* @param {Object} pgp
*/
constructor(public pgp) {}
protected constructor(public pgp) {
}

/**
* Get table name for model.
Expand All @@ -17,7 +31,35 @@ export abstract class Model {
* Get table column names for model.
* @return {String[]}
*/
public abstract getColumnSet(): any;
public getColumnSet() {
if (!this.columnSet) {
this.columnSet = this.createColumnSet(this.columnsDescriptor.map(col => {
const colDef: any = {
name: col.name
};
["prop", "init", "def"].forEach(prop => {
if (col.hasOwnProperty(prop)) {
colDef[prop] = col[prop];
}
});
return colDef;
}));
}
return this.columnSet;
}

public getSearchableFields(): Database.SearchableField[] {
return this.columnsDescriptor.map(col => {
return {
fieldName: col.prop || col.name,
supportedOperators: col.supportedOperators
}
});
}

public getName(): string {
return this.constructor.name;
}

/**
* Return the model & table definition.
Expand All @@ -35,12 +77,13 @@ export abstract class Model {
});
}


/**
* Convert the "camelCase" keys to "snake_case".
* @return {ColumnSet}
* @param columns
*/
public createColumnSet(columns) {
private createColumnSet(columns) {
return new this.pgp.helpers.ColumnSet(columns, {
table: {
table: this.getTable(),
Expand Down
31 changes: 16 additions & 15 deletions packages/core-database-postgres/src/models/round.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import { Database } from "@arkecosystem/core-interfaces";
import { bignumify } from "@arkecosystem/core-utils";
import { Model } from "./model";

export class Round extends Model {
/**
* The table associated with the model.
* @return {String}
*/
public getTable() {
return "rounds";
}

/**
* The read-only structure with query-formatting columns.
* @return {Object}
*/
public getColumnSet() {
return this.createColumnSet([
constructor(pgp) {
super(pgp);
this.columnsDescriptor = [
{
name: "public_key",
prop: "publicKey",
supportedOperators: [ Database.SearchOperator.OP_EQ ]
},
{
name: "balance",
prop: "voteBalance",
init: col => bignumify(col.value).toFixed(),
supportedOperators: [ Database.SearchOperator.OP_EQ, Database.SearchOperator.OP_LTE, Database.SearchOperator.OP_GTE ]
},
{
name: "round",
supportedOperators: [ Database.SearchOperator.OP_EQ, Database.SearchOperator.OP_LTE, Database.SearchOperator.OP_GTE ]
},
]);
]
}

/**
* The table associated with the model.
* @return {String}
*/
public getTable() {
return "rounds";
}
}
Loading

0 comments on commit 57374e6

Please sign in to comment.