Skip to content

Commit

Permalink
feat(data-store): use DataSource instead of Connection (#970)
Browse files Browse the repository at this point in the history
fixes #947
  • Loading branch information
mirceanis authored Jul 28, 2022
1 parent 31e3946 commit 3377930
Show file tree
Hide file tree
Showing 41 changed files with 358 additions and 256 deletions.
13 changes: 8 additions & 5 deletions __tests__/initial.migration.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// noinspection ES6PreferShortImport

/**
* This suite runs through a few agent operations using data that was created before
* TypeORM migrations were available (before Veramo 3.0.0)
Expand Down Expand Up @@ -32,7 +34,7 @@ import { KeyManager } from '../packages/key-manager/src'
import { DIDManager } from '../packages/did-manager/src'
import { FakeDidProvider, FakeDidResolver } from '../packages/test-utils/src'

import { Connection, ConnectionOptions, createConnection } from 'typeorm'
import { DataSourceOptions, DataSource } from 'typeorm'
import { Resolver } from 'did-resolver'
import { getResolver as ethrDidResolver } from 'ethr-did-resolver'
import { getResolver as webDidResolver } from 'web-did-resolver'
Expand All @@ -58,18 +60,19 @@ describe('database initial migration tests', () => {

function createTestsUsingOptions(
databaseBeforeFile: string,
connectionOverrides: Partial<ConnectionOptions>,
connectionOverrides: Partial<DataSourceOptions>,
) {
describe('using pre-migration database fixture', () => {
const databaseFile = databaseBeforeFile + '.tmp'
type TestingAgentPlugins = IDIDManager & IKeyManager & IDataStore & IDataStoreORM & IResolver & IDIDComm
let agent: TAgent<TestingAgentPlugins>
let dbConnection: Promise<Connection>
let dbConnection: DataSource

beforeAll(async () => {
fs.copyFileSync(databaseBeforeFile, databaseFile)

dbConnection = createConnection({
// intentionally using DataSource instead of Promise<DataSource> to test compatibility
dbConnection = new DataSource({
name: 'test',
type: 'sqlite',
database: databaseFile,
Expand All @@ -79,7 +82,7 @@ describe('database initial migration tests', () => {
logging: false,
entities: Entities,
...connectionOverrides,
} as ConnectionOptions)
} as DataSourceOptions)

agent = createAgent<TestingAgentPlugins>({
context: {
Expand Down
20 changes: 13 additions & 7 deletions __tests__/localAgent.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// noinspection ES6PreferShortImport

/**
* This runs a suite of ./shared tests using an agent configured for local operations,
* using a SQLite db for storage of credentials, presentations, messages as well as keys and DIDs.
Expand Down Expand Up @@ -45,17 +47,17 @@ import { DIDDiscovery, IDIDDiscovery } from '../packages/did-discovery/src'

import {
DataStore,
DataStoreDiscoveryProvider,
DataStoreORM,
DIDStore,
Entities,
KeyStore,
migrations,
PrivateKeyStore,
DataStoreDiscoveryProvider,
} from '../packages/data-store/src'
import { FakeDidProvider, FakeDidResolver } from '../packages/test-utils/src'
import { BrokenDiscoveryProvider, FakeDidProvider, FakeDidResolver } from '../packages/test-utils/src'

import { Connection, createConnection } from 'typeorm'
import { DataSource } from 'typeorm'
import { createGanacheProvider } from './utils/ganache-provider'
import { createEthersProvider } from './utils/ethers-provider'
import { Resolver } from 'did-resolver'
Expand Down Expand Up @@ -102,13 +104,13 @@ let agent: TAgent<
ISelectiveDisclosure &
IDIDDiscovery
>
let dbConnection: Promise<Connection>
let dbConnection: Promise<DataSource>
let databaseFile: string

const setup = async (options?: IAgentOptions): Promise<boolean> => {
databaseFile =
options?.context?.databaseFile || `./tmp/local-database-${Math.random().toPrecision(5)}.sqlite`
dbConnection = createConnection({
dbConnection = new DataSource({
name: options?.context?.['dbName'] || 'test',
type: 'sqlite',
database: databaseFile,
Expand All @@ -119,7 +121,7 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
entities: Entities,
// allow shared tests to override connection options
...options?.context?.dbConnectionOptions,
})
}).initialize()

const { provider, registry } = await createGanacheProvider()
const ethersProvider = createEthersProvider()
Expand Down Expand Up @@ -228,7 +230,11 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
}),
new SelectiveDisclosure(),
new DIDDiscovery({
providers: [new AliasDiscoveryProvider(), new DataStoreDiscoveryProvider()],
providers: [
new AliasDiscoveryProvider(),
new DataStoreDiscoveryProvider(),
new BrokenDiscoveryProvider(),
],
}),
...(options?.plugins || []),
],
Expand Down
13 changes: 2 additions & 11 deletions __tests__/localJsonStoreAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,10 @@ import { EthrDIDProvider } from '../packages/did-provider-ethr/src'
import { WebDIDProvider } from '../packages/did-provider-web/src'
import { getDidKeyResolver, KeyDIDProvider } from '../packages/did-provider-key/src'
import { DIDComm, DIDCommMessageHandler, IDIDComm } from '../packages/did-comm/src'
import {
ISelectiveDisclosure,
SdrMessageHandler,
SelectiveDisclosure,
} from '../packages/selective-disclosure/src'
import { ISelectiveDisclosure, SdrMessageHandler, SelectiveDisclosure, } from '../packages/selective-disclosure/src'
import { KeyManagementSystem, SecretBox } from '../packages/kms-local/src'
import { Web3KeyManagementSystem } from '../packages/kms-web3/src'
import {
DataStoreJson,
DIDStoreJson,
KeyStoreJson,
PrivateKeyStoreJson,
} from '../packages/data-store-json/src'
import { DataStoreJson, DIDStoreJson, KeyStoreJson, PrivateKeyStoreJson, } from '../packages/data-store-json/src'
import { FakeDidProvider, FakeDidResolver } from '../packages/test-utils/src'

import { Resolver } from 'did-resolver'
Expand Down
51 changes: 27 additions & 24 deletions __tests__/localMemoryStoreAgent.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// noinspection ES6PreferShortImport

/**
* This runs a suite of ./shared tests using an agent configured for local operations,
* using a SQLite db for storage of credentials and an in-memory store for keys and DIDs.
Expand All @@ -17,7 +19,7 @@ import {
import { MessageHandler } from '../packages/message-handler/src'
import { KeyManager, MemoryKeyStore, MemoryPrivateKeyStore } from '../packages/key-manager/src'
import { DIDManager, MemoryDIDStore } from '../packages/did-manager/src'
import { Connection, createConnection } from 'typeorm'
import { DataSource } from 'typeorm'
import { DIDResolverPlugin } from '../packages/did-resolver/src'
import { JwtMessageHandler } from '../packages/did-jwt/src'
import { CredentialIssuer, ICredentialIssuer, W3cMessageHandler } from '../packages/credential-w3c/src'
Expand Down Expand Up @@ -71,21 +73,22 @@ const infuraProjectId = '3586660d179141e3801c3895de1c2eba'

let agent: TAgent<
IDIDManager &
IKeyManager &
IDataStore &
IDataStoreORM &
IResolver &
IMessageHandler &
IDIDComm &
ICredentialIssuer &
ICredentialIssuerLD &
ICredentialIssuerEIP712 &
ISelectiveDisclosure
IKeyManager &
IDataStore &
IDataStoreORM &
IResolver &
IMessageHandler &
IDIDComm &
ICredentialIssuer &
ICredentialIssuerLD &
ICredentialIssuerEIP712 &
ISelectiveDisclosure
>
let dbConnection: Promise<Connection>
let dbConnection: DataSource

const setup = async (options?: IAgentOptions): Promise<boolean> => {
dbConnection = createConnection({
// intentionally not initializing here to test compatibility
dbConnection = new DataSource({
name: 'test',
type: 'sqlite',
database: databaseFile,
Expand All @@ -98,16 +101,16 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {

agent = createAgent<
IDIDManager &
IKeyManager &
IDataStore &
IDataStoreORM &
IResolver &
IMessageHandler &
IDIDComm &
ICredentialIssuer &
ICredentialIssuerLD &
ICredentialIssuerEIP712 &
ISelectiveDisclosure
IKeyManager &
IDataStore &
IDataStoreORM &
IResolver &
IMessageHandler &
IDIDComm &
ICredentialIssuer &
ICredentialIssuerLD &
ICredentialIssuerEIP712 &
ISelectiveDisclosure
>({
...options,
context: {
Expand Down Expand Up @@ -218,6 +221,6 @@ describe('Local in-memory integration tests', () => {
didManager(testContext)
messageHandler(testContext)
didCommPacking(testContext)
utils(testContext)
utils(testContext)
credentialStatus(testContext)
})
27 changes: 17 additions & 10 deletions __tests__/restAgent.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// noinspection ES6PreferShortImport

/**
* This runs a suite of ./shared tests using an agent configured for remote operations.
* There is a local agent that only uses @veramo/remove-client and a remote agent that provides the actual functionality.
* There is a local agent that only uses @veramo/remove-client and a remote agent that provides the actual
* functionality.
*
* This suite also runs a messaging server to run through some examples of DIDComm using did:fake identifiers.
* See didWithFakeDidFlow() for more details.
Expand Down Expand Up @@ -46,20 +49,20 @@ import { KeyManagementSystem, SecretBox } from '../packages/kms-local/src'
import { Web3KeyManagementSystem } from '../packages/kms-web3/src'
import {
DataStore,
DataStoreDiscoveryProvider,
DataStoreORM,
DIDStore,
Entities,
KeyStore,
migrations,
PrivateKeyStore,
DataStoreDiscoveryProvider,
} from '../packages/data-store/src'
import { Connection, createConnection } from 'typeorm'
import { AgentRestClient } from '../packages/remote-client/src'
import { AgentRouter, MessagingRouter, RequestWithAgentRouter } from '../packages/remote-server/src'
import { DIDDiscovery, IDIDDiscovery } from '../packages/did-discovery/src'
import { FakeDidProvider, FakeDidResolver } from '../packages/test-utils/src'
import { BrokenDiscoveryProvider, FakeDidProvider, FakeDidResolver } from '../packages/test-utils/src'

import { DataSource } from 'typeorm'
import { Resolver } from 'did-resolver'
import { getResolver as ethrDidResolver } from 'ethr-did-resolver'
import { getResolver as webDidResolver } from 'web-did-resolver'
Expand Down Expand Up @@ -93,7 +96,7 @@ const secretKey = '29739248cad1bd1a0fc4d9b75cd4d2990de535baf5caadfdf8d8f86664aa8
const port = 3002
const basePath = '/agent'

let dbConnection: Promise<Connection>
let dbConnection: Promise<DataSource>
let serverAgent: IAgent
let restServer: Server

Expand Down Expand Up @@ -123,7 +126,7 @@ const getAgent = (options?: IAgentOptions) =>
})

const setup = async (options?: IAgentOptions): Promise<boolean> => {
dbConnection = createConnection({
dbConnection = new DataSource({
name: options?.context?.['dbName'] || 'sqlite-test',
type: 'sqlite',
database: databaseFile,
Expand All @@ -132,7 +135,7 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
migrationsRun: true,
logging: false,
entities: Entities,
})
}).initialize()

serverAgent = new Agent({
...options,
Expand All @@ -141,7 +144,7 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
store: new KeyStore(dbConnection),
kms: {
local: new KeyManagementSystem(new PrivateKeyStore(dbConnection, new SecretBox(secretKey))),
web3: new Web3KeyManagementSystem({})
web3: new Web3KeyManagementSystem({}),
},
}),
new DIDManager({
Expand Down Expand Up @@ -205,7 +208,11 @@ const setup = async (options?: IAgentOptions): Promise<boolean> => {
}),
new SelectiveDisclosure(),
new DIDDiscovery({
providers: [new AliasDiscoveryProvider(), new DataStoreDiscoveryProvider()],
providers: [
new AliasDiscoveryProvider(),
new DataStoreDiscoveryProvider(),
new BrokenDiscoveryProvider(),
],
}),
...(options?.plugins || []),
],
Expand Down Expand Up @@ -267,6 +274,6 @@ describe('REST integration tests', () => {
didCommPacking(testContext)
didWithFakeDidFlow(testContext)
didDiscovery(testContext)
utils(testContext)
utils(testContext)
credentialStatus(testContext)
})
Loading

0 comments on commit 3377930

Please sign in to comment.