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

Consolidate core-api's repos into core-database & core-database-postgres #2107

Merged
merged 20 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c3ce67c
feature: introduce transactions and blocks business repos. The idea i…
paroxysm Feb 12, 2019
00ff55a
feature: implement the BlockBusinessRepo logic. And the search method…
paroxysm Feb 13, 2019
1ebd0c3
Merge remote-tracking branch 'arkecosystem/develop' into feat/refacto…
paroxysm Feb 13, 2019
86cd3b4
Merge remote-tracking branch 'arkecosystem/develop' into feat/refacto…
paroxysm Feb 16, 2019
e4b15d5
feature: implement TransactionsBusinessRepo, along with data-layer si…
paroxysm Feb 16, 2019
15346c6
feature: Use business repos on core-api v2 api
paroxysm Feb 16, 2019
43ad082
refactor: rename repo properties on the databaseService to use camel-…
paroxysm Feb 16, 2019
2d66fd5
refactor: added 'findLastByPublicKey' method that I forgot to add
paroxysm Feb 16, 2019
17e4ec4
fix: a failing test in core-api transactions.test.ts
paroxysm Feb 17, 2019
0dc9b68
fix: failing tests in core-database
paroxysm Feb 17, 2019
4667809
fix: findByTypeAndId
paroxysm Feb 17, 2019
5c2270a
Merge remote-tracking branch 'arkecosystem/develop' into feat/refacto…
paroxysm Feb 17, 2019
f2a5a3a
Merge remote-tracking branch 'arkecosystem/develop' into feat/refacto…
paroxysm Feb 17, 2019
46f7289
fix: compile-error caused by merge
paroxysm Feb 17, 2019
cb8f3d9
refactor: move 'getColumnSet', 'getSearchableFields' and 'getName' to…
paroxysm Feb 19, 2019
4a54279
Merge remote-tracking branch 'arkecosystem/develop' into feat/refacto…
paroxysm Feb 19, 2019
f3daf4a
refactor: inline some variables
paroxysm Feb 19, 2019
1b72e9e
fix: had mistakely removed support for 'senderId' parameter...
paroxysm Feb 19, 2019
a941c8d
Merge remote-tracking branch 'arkecosystem/develop' into feat/refacto…
paroxysm Feb 20, 2019
c16943a
chore: remove comment
paroxysm Feb 20, 2019
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
Next Next commit
feature: introduce transactions and blocks business repos. The idea i…
…s to have external modules interact with the business repos. The business repos optionally perform some business logic, and then call into the database repos
  • Loading branch information
paroxysm committed Feb 12, 2019
commit c3ce67cf1094e98961a5216bc48cafac193b19bb
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
77 changes: 62 additions & 15 deletions packages/core-database-postgres/src/models/block.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,119 @@
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([
private readonly columnsDescriptor: any[];

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";
}

/**
* The read-only structure with query-formatting columns.
* @return {Object}
*/
public getColumnSet() {
return 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;
}));
}

public getName(): string {
return "Block";
}

public getSearchableFields(): Database.SearchableField[] {
return this.columnsDescriptor.map(col => {
return {
fieldName: col.prop || col.name,
supportedOperators: col.supportedOperators
}
})
}
}
7 changes: 6 additions & 1 deletion packages/core-database-postgres/src/models/model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Database } from "@arkecosystem/core-interfaces";
import sql from "sql";

export abstract class Model {
export abstract class Model implements Database.IDatabaseModel {
/**
* Create a new model instance.
* @param {Object} pgp
Expand Down Expand Up @@ -48,4 +49,8 @@ export abstract class Model {
},
});
}

public abstract getName(): string;

public abstract getSearchableFields(): Database.SearchableField[];
}
4 changes: 4 additions & 0 deletions packages/core-database-postgres/src/repositories/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@ export class BlocksRepository extends Repository implements Database.IBlocksRepo
public getModel() {
return new Block(this.pgp);
}

public async search(params: Database.SearchParameters) {
return undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Database } from "@arkecosystem/core-interfaces";
import { delegateCalculator } from "@arkecosystem/core-utils";
import { Bignum, constants, crypto, models } from "@arkecosystem/crypto";
import genesisBlockTestnet from "../../../core-test-utils/src/config/testnet/genesisBlock.json";
import { DelegatesRepository, WalletsRepository } from "../../src";
import { DelegatesBusinessRepository, WalletsBusinessRepository } from "../../src";
import { DatabaseService } from "../../src/database-service";
import { setUp, tearDown } from "../__support__/setup";

Expand Down Expand Up @@ -36,8 +36,8 @@ beforeEach(async done => {
const { WalletManager } = require("../../src/wallet-manager");
walletManager = new WalletManager();

repository = new DelegatesRepository(() => databaseService);
walletsRepository = new WalletsRepository(() => databaseService);
repository = new DelegatesBusinessRepository(() => databaseService);
walletsRepository = new WalletsBusinessRepository(() => databaseService);
databaseService = new DatabaseService(null, null, walletManager, walletsRepository, repository);

done();
Expand Down
4 changes: 2 additions & 2 deletions packages/core-database/__tests__/repositories/wallets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import uniq from "lodash/uniq";
import genesisBlockTestnet from "../../../core-test-utils/src/config/testnet/genesisBlock.json";
import { setUp, tearDown } from "../__support__/setup";

import { WalletsRepository } from "../../src";
import { WalletsBusinessRepository } from "../../src";
import { DatabaseService } from "../../src/database-service";

const { Block, Wallet } = models;
Expand Down Expand Up @@ -37,7 +37,7 @@ beforeEach(async done => {
const { WalletManager } = require("../../src/wallet-manager");
walletManager = new WalletManager();

repository = new WalletsRepository(() => databaseService);
repository = new WalletsBusinessRepository(() => databaseService);

databaseService = new DatabaseService(null, null, walletManager, repository, null);

Expand Down
6 changes: 3 additions & 3 deletions packages/core-database/src/database-service-factory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Database } from "@arkecosystem/core-interfaces";
import { DatabaseService } from "./database-service";
import { DelegatesRepository } from "./repositories/delegates";
import { WalletsRepository } from "./repositories/wallets";
import { DelegatesBusinessRepository } from "./repositories/delegates-business-repository";
import { WalletsBusinessRepository } from "./repositories/wallets-business-repository";

// Allow extenders of core-database to provide, optionally, a IWalletManager concrete in addition to a IDatabaseConnection, but keep the business repos common
export const databaseServiceFactory = async (opts: any, walletManager: Database.IWalletManager, connection: Database.IDatabaseConnection): Promise<Database.IDatabaseService> => {
let databaseService: DatabaseService;
databaseService = new DatabaseService(opts, connection, walletManager, new WalletsRepository(() => databaseService), new DelegatesRepository(() => databaseService));
databaseService = new DatabaseService(opts, connection, walletManager, new WalletsBusinessRepository(() => databaseService), new DelegatesBusinessRepository(() => databaseService));
await databaseService.init();
return databaseService;
};
Expand Down
8 changes: 7 additions & 1 deletion packages/core-database/src/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class DatabaseService implements Database.IDatabaseService {
public options: any;
public wallets: Database.IWalletsBusinessRepository;
public delegates: Database.IDelegatesBusinessRepository;
public blocks: Database.IBlocksBusinessRepository;
public transactions: Database.ITransactionsBusinessRepository;
public blocksInCurrentRound: any[] = null;
public stateStarted: boolean = false;
public restoredDatabaseIntegrity: boolean = false;
Expand All @@ -33,13 +35,17 @@ export class DatabaseService implements Database.IDatabaseService {
connection: Database.IDatabaseConnection,
walletManager: Database.IWalletManager,
walletsBusinessRepository: Database.IWalletsBusinessRepository,
delegatesBusinessRepository: Database.IDelegatesBusinessRepository
delegatesBusinessRepository: Database.IDelegatesBusinessRepository,
transactionsBusinessRepository: Database.ITransactionsBusinessRepository,
blocksBusinessRepository: Database.IBlocksBusinessRepository
) {
this.connection = connection;
this.walletManager = walletManager;
this.options = options;
this.wallets = walletsBusinessRepository;
this.delegates = delegatesBusinessRepository;
this.blocks = blocksBusinessRepository;
this.transactions = transactionsBusinessRepository;

this.registerListeners();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core-database/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export * from "./manager";
export * from "./database-service-factory";
export * from "./wallet-manager";
export * from "./repositories/delegates";
export * from "./repositories/wallets";
export * from "./repositories/delegates-business-repository";
export * from "./repositories/wallets-business-repository";
export * from "./plugin";
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Database } from "@arkecosystem/core-interfaces";
import { SearchParameterConverter } from "./utils/search-parameter-converter";

export class BlocksBusinessRepository implements Database.IBlocksBusinessRepository {

constructor(private databaseServiceProvider: () => Database.IDatabaseService) {
}

public async findAll(params: any) {
const blocksRepository = this.databaseServiceProvider().connection.blocksRepository;
const searchParameters = new SearchParameterConverter(blocksRepository.getModel()).convert(params);
return await blocksRepository.search(searchParameters);
}

public async findAllByGenerator(generatorPublicKey: string, paginate: any) {
const params = { ...{ generatorPublicKey }, ...paginate };
return await this.findAll(params);
}

public async findByHeight(height: number) {
const params = { ...{ height } };
return await this.findAll(params);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return await this.findAll(params);
return await this.findAll({ height });

}

public async findById(id: string) {
return await this.databaseServiceProvider().connection.blocksRepository.findById(id);
}

public async search(params: any) {
return await this.findAll(params);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { delegateCalculator } from "@arkecosystem/core-utils";
import orderBy from "lodash/orderBy";
import limitRows from "./utils/limit-rows";

export class DelegatesRepository implements Database.IDelegatesBusinessRepository {
export class DelegatesBusinessRepository implements Database.IDelegatesBusinessRepository {

/**
* Create a new delegate repository instance.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Database } from "@arkecosystem/core-interfaces";

export class TransactionsBusinessRepository implements Database.ITransactionsBusinessRepository {

constructor(private databaseServiceProvider: () => Database.IDatabaseService) {
}

public allVotesBySender(senderPublicKey: any, parameters: any): Promise<any> {
return undefined;
}

public findAll(params: any, sequenceOrder: "asc" | "desc"): Promise<any> {
return undefined;
}

public findAllByBlock(blockId: any, parameters: any): Promise<any> {
return undefined;
}

public findAllByRecipient(recipientId: any, parameters: any): Promise<any> {
return undefined;
}

public findAllBySender(senderPublicKey: any, parameters: any): Promise<any> {
return undefined;
}

public findAllByType(type: any, parameters: any): Promise<any> {
return undefined;
}

public findAllByWallet(wallet: any, parameters: any): Promise<any> {
return undefined;
}

public findAllLegacy(parameters: any): Promise<any> {
return undefined;
}

public findById(id: string): Promise<any> {
return undefined;
}

public findByTypeAndId(type: any, id: string): Promise<any> {
return undefined;
}

public getFeeStatistics(): Promise<any> {
return undefined;
}

public search(parameters: any): Promise<any> {
return undefined;
}

}
Loading