From 8e7ef33edfa72cf47f64ca7b09fc0b0cb33c8093 Mon Sep 17 00:00:00 2001 From: Timur Ramazanov Date: Mon, 13 May 2019 17:35:10 +0300 Subject: [PATCH] Search by dataname and datavalue --- src/orm/entities/account.ts | 8 ++++++- src/orm/entities/account_data.ts | 3 ++- src/schema/accounts.ts | 6 +++++ src/schema/resolvers/account.ts | 32 ++++++++++++++++++-------- src/schema/resolvers/shared/account.ts | 11 ++++++++- 5 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/orm/entities/account.ts b/src/orm/entities/account.ts index acf316eb..879769b4 100644 --- a/src/orm/entities/account.ts +++ b/src/orm/entities/account.ts @@ -24,7 +24,13 @@ export class Account { @Column({ name: "inflationdest" }) inflationDestination: string; - @Column({ name: "homedomain" }) + @Column({ + name: "homedomain", + transformer: { + from: (value: string) => Buffer.from(value, "base64").toString(), + to: (value: string) => Buffer.from(value).toString("base64") + } + }) homeDomain: string; @Column({ diff --git a/src/orm/entities/account_data.ts b/src/orm/entities/account_data.ts index ba99daea..3bfb6839 100644 --- a/src/orm/entities/account_data.ts +++ b/src/orm/entities/account_data.ts @@ -1,4 +1,4 @@ -import { Column, Entity, ManyToOne, PrimaryColumn } from "typeorm"; +import { Column, Entity, JoinColumn, ManyToOne, PrimaryColumn } from "typeorm"; import { Account } from "./account"; @Entity("accountdata") @@ -17,5 +17,6 @@ export class AccountData { lastModified: number; @ManyToOne(type => Account, account => account.data) + @JoinColumn({ name: "accountid" }) account: Account; } diff --git a/src/schema/accounts.ts b/src/schema/accounts.ts index 392a22e0..a57532b4 100644 --- a/src/schema/accounts.ts +++ b/src/schema/accounts.ts @@ -135,6 +135,11 @@ export const typeDefs = gql` weight: Int! } + input DataInput { + name: String + value: String + } + extend type Query { "Get single account by the id" account(id: AccountID!): Account @@ -142,6 +147,7 @@ export const typeDefs = gql` accounts( ids: [AccountID!] homeDomain: String + data: DataInput first: Int last: Int after: String diff --git a/src/schema/resolvers/account.ts b/src/schema/resolvers/account.ts index bf4dab34..482bf5a7 100644 --- a/src/schema/resolvers/account.ts +++ b/src/schema/resolvers/account.ts @@ -13,7 +13,7 @@ import { IHorizonTransactionData } from "../../datasource/types"; -import { Account, Balance, DataEntry, Effect, Operation, Trade, Transaction } from "../../model"; +import { Account, Balance, Effect, Operation, Trade, Transaction } from "../../model"; import { BalanceFactory, EffectFactory, @@ -31,9 +31,13 @@ import { getReservedBalance } from "../../util/base_reserve"; import { paginate } from "../../util/paging"; import { toFloatAmountString } from "../../util/stellar"; -const dataEntriesResolver = createBatchResolver((source: any) => - db.dataEntries.findAllByAccountIDs(_.map(source, "id")) -); +// const dataEntriesResolver = createBatchResolver((source: AccountEntity[]) => { +// if (source.data) { +// return source.data; +// } + +// return db.dataEntries.findAllByAccountIDs(_.map(source, "id")) +// }); const balancesResolver = createBatchResolver(async (source: Account[]) => { const accountIDs = source.map(s => s.id); @@ -71,9 +75,9 @@ const accountSubscription = (event: string) => { export default { Account: { - homeDomain: (root: Account) => Buffer.from(root.homeDomain, "base64").toString(), + // homeDomain: (root: Account) => Buffer.from(root.homeDomain, "base64").toString(), reservedBalance: (root: Account) => toFloatAmountString(getReservedBalance(root.numSubentries)), - data: dataEntriesResolver, + // data: dataEntriesResolver, balances: balancesResolver, ledger: resolvers.ledger, operations: async (root: Account, args: any, ctx: IApolloContext) => { @@ -109,7 +113,7 @@ export default { return getRepository(AccountEntity).findOne(args.id); }, accounts: async (root: any, args: any) => { - const { ids, homeDomain, ...paging } = args; + const { ids, homeDomain, data, ...paging } = args; const qb = getRepository(AccountEntity).createQueryBuilder("accounts"); if (ids && ids.length !== 0) { @@ -117,10 +121,20 @@ export default { } if (homeDomain) { - qb.andWhere("decode(homedomain, 'base64') = :homeDomain", { homeDomain }); + qb.andWhere("decode(accounts.homedomain, 'base64') = :homeDomain", { homeDomain }); + } + + if (data) { + qb.innerJoinAndSelect("accounts.data", "data"); + if (data.name) { + qb.andWhere("decode(data.name, 'base64') = :name", { name: data.name }); + } + if (data.value) { + qb.andWhere("decode(data.value, 'base64') = :value", { value: data.value }); + } } - return makeConnection(await paginate(qb, paging, "accountid")); + return makeConnection(await paginate(qb, paging, "accounts.id")); } }, Subscription: { diff --git a/src/schema/resolvers/shared/account.ts b/src/schema/resolvers/shared/account.ts index 449d02c9..5979c376 100644 --- a/src/schema/resolvers/shared/account.ts +++ b/src/schema/resolvers/shared/account.ts @@ -1,3 +1,4 @@ +import { fieldsList } from "graphql-fields-list"; import { getRepository, In } from "typeorm"; import { Account, AccountID } from "../../../model"; import { Account as AccountEntity } from "../../../orm/entities/account"; @@ -10,5 +11,13 @@ export const account = createBatchResolver((source: any, args: a return ids.map(id => (id ? { id } : null)); } - return getRepository(AccountEntity).find({ id: In(ids) }); + const repo = getRepository(AccountEntity); + const requestedFields = fieldsList(info); + const findParams = { id: In(ids), relations: ([] as string[]) }; + + if (requestedFields.indexOf("data") !== -1) { + findParams.relations.push("data"); + } + + return repo.find(findParams); });