Skip to content

Commit

Permalink
feat: Method dataStoreORMGetIdentities
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat authored and mirceanis committed Sep 7, 2020
1 parent 281b493 commit 7952fbb
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 2 deletions.
7 changes: 7 additions & 0 deletions __tests__/shared/webDidFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ export default (testContext: {
expect(bob.did).toBeDefined()
})

it('should query identities', async () => {
const identities = await agent.dataStoreORMGetIdentities()
expect(identities.length).toEqual(3)
const count = await agent.dataStoreORMGetIdentitiesCount()
expect(count).toEqual(3)
})

describe('should create verifiable credential', () => {
it('issuer: serviceIdentity', async () => {
const verifiableCredential = await agent.createVerifiableCredential({
Expand Down
65 changes: 65 additions & 0 deletions packages/daf-graphql/src/methods/data-store-orm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,74 @@ export const dataStoreORMGetVerifiableCredentials: IAgentGraphQLMethod = {
}
`,
}

export const dataStoreORMGetIdentities: IAgentGraphQLMethod = {
type: 'Query',
query: `
query dataStoreORMGetIdentities($where: [IdentitiesWhere], $order: [IdentitiesOrder], $take: Int, $skip: Int) {
dataStoreORMGetIdentities(where: $where, order: $order, take: $take, skip: $skip) {
did
provider
alias
keys {
kid
kms
type
publicKeyHex
}
services{
id
type
serviceEndpoint
description
}
}
}
`,
typeDef: `
enum IdentitiesColumns {
alias
provider
}
input IdentitiesWhere {
column: IdentitiesColumns!
value: [String]
not: Boolean
op: WhereOperation
}
input IdentitiesOrder {
column: IdentitiesColumns!
direction: OrderDirection!
}
extend type Query {
dataStoreORMGetIdentities(where: [IdentitiesWhere], order: [IdentitiesOrder], take: Int, skip: Int): [Identity]
}
`,
}

export const dataStoreORMGetIdentitiesCount: IAgentGraphQLMethod = {
type: 'Query',
query: `
query dataStoreORMGetIdentitiesCount($where: [IdentitiesWhere], $order: [IdentitiesOrder]) {
dataStoreORMGetIdentitiesCount(where: $where, order: $order)
}
`,
typeDef: `
extend type Query {
dataStoreORMGetIdentitiesCount(where: [IdentitiesWhere], order: [IdentitiesOrder]): Int
}
`,
}

export const supportedMethods: Record<string, IAgentGraphQLMethod> = {
dataStoreORMGetMessages,
dataStoreORMGetVerifiableCredentials,
dataStoreORMGetIdentities,
dataStoreORMGetIdentitiesCount,
}

export default supportedMethods
2 changes: 2 additions & 0 deletions packages/daf-rest/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const supportedMethods: Record<string, IAgentRESTMethod> = {
createSelectiveDisclosureRequest: { type: 'POST', path: '/createSelectiveDisclosureRequest' },
getVerifiableCredentialsForSdr: { type: 'POST', path: '/getVerifiableCredentialsForSdr' },
validatePresentationAgainstSdr: { type: 'POST', path: '/validatePresentationAgainstSdr' },
dataStoreORMGetIdentities: { type: 'POST', path: '/dataStoreORMGetIdentities' },
dataStoreORMGetIdentitiesCount: { type: 'POST', path: '/dataStoreORMGetIdentitiesCount' },
dataStoreORMGetMessages: { type: 'POST', path: '/dataStoreORMGetMessages' },
dataStoreORMGetMessagesCount: { type: 'POST', path: '/dataStoreORMGetMessagesCount' },
dataStoreORMGetVerifiableCredentialsByClaims: {
Expand Down
18 changes: 17 additions & 1 deletion packages/daf-typeorm/src/__tests__/data-store-orm.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { Agent, VerifiableCredential, VerifiablePresentation, IMessage, IDataStore, TAgent } from 'daf-core'
import { createConnection, Connection } from 'typeorm'
import { DataStoreORM, IDataStoreORM } from '../data-store-orm'
import { FindArgs, TClaimsColumns, TCredentialColumns, TMessageColumns, TPresentationColumns } from '../types'
import {
FindArgs,
TClaimsColumns,
TCredentialColumns,
TMessageColumns,
TPresentationColumns,
TIdentitiesColumns,
} from '../types'
import { DataStore } from '../data-store'
import { Entities } from '../index'
import fs from 'fs'
Expand Down Expand Up @@ -381,4 +388,13 @@ describe('daf-typeorm entities', () => {
const presentations = await agent.dataStoreORMGetVerifiablePresentations(args)
expect(presentations[0].verifiableCredential[0].id).toEqual('vc6')
})

it('should query identities', async () => {
const agent = makeAgent()
const identities = await agent.dataStoreORMGetIdentities()
expect(identities.length).toEqual(4)

const count = await agent.dataStoreORMGetIdentitiesCount()
expect(count).toEqual(4)
})
})
43 changes: 42 additions & 1 deletion packages/daf-typeorm/src/data-store-orm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {
VerifiableCredential,
VerifiablePresentation,
IPluginMethodMap,
IIdentity,
} from 'daf-core'
import { Message, createMessage } from './entities/message'
import { Claim } from './entities/claim'
import { Credential } from './entities/credential'
import { Presentation } from './entities/presentation'
import { Identity } from './entities/identity'
import {
Connection,
Not,
Expand All @@ -31,6 +33,7 @@ import {
TCredentialColumns,
TMessageColumns,
TPresentationColumns,
TIdentitiesColumns,
FindArgs,
} from './types'

Expand All @@ -39,6 +42,8 @@ interface IContext {
}

export interface IDataStoreORM extends IPluginMethodMap {
dataStoreORMGetIdentities: (args: FindArgs<TIdentitiesColumns>, context: IContext) => Promise<IIdentity[]>
dataStoreORMGetIdentitiesCount: (args: FindArgs<TIdentitiesColumns>, context: IContext) => Promise<number>
dataStoreORMGetMessages: (args: FindArgs<TMessageColumns>, context: IContext) => Promise<IMessage[]>
dataStoreORMGetMessagesCount: (args: FindArgs<TMessageColumns>, context: IContext) => Promise<number>
dataStoreORMGetVerifiableCredentialsByClaims: (
Expand Down Expand Up @@ -75,6 +80,8 @@ export class DataStoreORM implements IAgentPlugin {
this.dbConnection = dbConnection

this.methods = {
dataStoreORMGetIdentities: this.dataStoreORMGetIdentities.bind(this),
dataStoreORMGetIdentitiesCount: this.dataStoreORMGetIdentitiesCount.bind(this),
dataStoreORMGetMessages: this.dataStoreORMGetMessages.bind(this),
dataStoreORMGetMessagesCount: this.dataStoreORMGetMessagesCount.bind(this),
dataStoreORMGetVerifiableCredentialsByClaims: this.dataStoreORMGetVerifiableCredentialsByClaims.bind(
Expand All @@ -92,6 +99,38 @@ export class DataStoreORM implements IAgentPlugin {
}
}

// Identities

private async identitiesQuery(
args: FindArgs<TIdentitiesColumns>,
context: IContext,
): Promise<SelectQueryBuilder<Identity>> {
const where = createWhereObject(args)
let qb = (await this.dbConnection)
.getRepository(Identity)
.createQueryBuilder('identity')
.leftJoinAndSelect('identity.keys', 'keys')
.leftJoinAndSelect('identity.services', 'services')
.where(where)
qb = decorateQB(qb, 'message', args)
return qb
}

async dataStoreORMGetIdentities(
args: FindArgs<TIdentitiesColumns>,
context: IContext,
): Promise<IIdentity[]> {
const identities = await (await this.identitiesQuery(args, context)).getMany()
return identities
}

async dataStoreORMGetIdentitiesCount(
args: FindArgs<TIdentitiesColumns>,
context: IContext,
): Promise<number> {
return await (await this.identitiesQuery(args, context)).getCount()
}

// Messages

private async messagesQuery(
Expand Down Expand Up @@ -298,7 +337,9 @@ function addVerifierQuery(input: FindArgs<any>, qb: SelectQueryBuilder<any>): Se
}

function createWhereObject(
input: FindArgs<TMessageColumns | TClaimsColumns | TCredentialColumns | TPresentationColumns>,
input: FindArgs<
TMessageColumns | TClaimsColumns | TCredentialColumns | TPresentationColumns | TIdentitiesColumns
>,
): any {
if (input?.where) {
const where: Record<string, any> = {}
Expand Down
2 changes: 2 additions & 0 deletions packages/daf-typeorm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export interface FindArgs<TColumns> {
skip?: number
}

export type TIdentitiesColumns = 'did' | 'alias' | 'provider'

export type TMessageColumns =
| 'from'
| 'to'
Expand Down

0 comments on commit 7952fbb

Please sign in to comment.