Skip to content

Commit

Permalink
Search by dataname and datavalue
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie-wasp committed May 13, 2019
1 parent 534a7a7 commit 8e7ef33
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/orm/entities/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
3 changes: 2 additions & 1 deletion src/orm/entities/account_data.ts
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -17,5 +17,6 @@ export class AccountData {
lastModified: number;

@ManyToOne(type => Account, account => account.data)
@JoinColumn({ name: "accountid" })
account: Account;
}
6 changes: 6 additions & 0 deletions src/schema/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,19 @@ 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
"Get a lists of accounts by the ids"
accounts(
ids: [AccountID!]
homeDomain: String
data: DataInput
first: Int
last: Int
after: String
Expand Down
32 changes: 23 additions & 9 deletions src/schema/resolvers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,9 +31,13 @@ import { getReservedBalance } from "../../util/base_reserve";
import { paginate } from "../../util/paging";
import { toFloatAmountString } from "../../util/stellar";

const dataEntriesResolver = createBatchResolver<Account, DataEntry[]>((source: any) =>
db.dataEntries.findAllByAccountIDs(_.map(source, "id"))
);
// const dataEntriesResolver = createBatchResolver<Account, DataEntry[]>((source: AccountEntity[]) => {
// if (source.data) {
// return source.data;
// }

// return db.dataEntries.findAllByAccountIDs(_.map(source, "id"))
// });

const balancesResolver = createBatchResolver<Account, Balance[]>(async (source: Account[]) => {
const accountIDs = source.map(s => s.id);
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -109,18 +113,28 @@ 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) {
qb.whereInIds(ids);
}

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<Account>(await paginate(qb, paging, "accountid"));
return makeConnection<Account>(await paginate(qb, paging, "accounts.id"));
}
},
Subscription: {
Expand Down
11 changes: 10 additions & 1 deletion src/schema/resolvers/shared/account.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -10,5 +11,13 @@ export const account = createBatchResolver<any, Account[]>((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);
});

0 comments on commit 8e7ef33

Please sign in to comment.