Skip to content

Commit

Permalink
fix: Initializing DB before any action
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Mar 13, 2020
1 parent 108f22c commit 7b5959e
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 26 deletions.
9 changes: 8 additions & 1 deletion packages/daf-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ import './data-explorer'
import './graphql'
import './sdr'
import './msg'
import { initializeDb } from './setup'

const main = async () => {
await initializeDb()
program.parse(process.argv)
}

program.parse(process.argv)
if (!process.argv.slice(2).length) {
program.outputHelp()
} else {
main().catch(e => console.log(e.message))
}
6 changes: 1 addition & 5 deletions packages/daf-cli/src/credential.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Daf from 'daf-core'
import * as W3c from 'daf-w3c'
import * as DIDComm from 'daf-did-comm'
import { core, dataStore, initializeDb } from './setup'
import { core, dataStore } from './setup'
import program from 'commander'
import inquirer from 'inquirer'
import qrcode from 'qrcode-terminal'
Expand All @@ -12,8 +12,6 @@ program
.option('-s, --send', 'Send')
.option('-q, --qrcode', 'Show qrcode')
.action(async cmd => {
await initializeDb()

const identities = await core.identityManager.getIdentities()
if (identities.length === 0) {
console.error('No dids')
Expand Down Expand Up @@ -96,8 +94,6 @@ program
.option('-s, --send', 'Send')
.option('-q, --qrcode', 'Show qrcode')
.action(async cmd => {
await initializeDb()

const myIdentities = await core.identityManager.getIdentities()
if (myIdentities.length === 0) {
console.error('No dids')
Expand Down
4 changes: 1 addition & 3 deletions packages/daf-cli/src/data-explorer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { core, dataStore, initializeDb } from './setup'
import { core, dataStore } from './setup'
import program from 'commander'
import inquirer from 'inquirer'
import { formatDistanceToNow } from 'date-fns'
Expand All @@ -10,8 +10,6 @@ program
.option('-i, --identities', 'List known identities')
.option('-m, --messages', 'List messages')
.action(async cmd => {
await initializeDb()

if (cmd.identities) {
const dids = await dataStore.allIdentities()
if (dids.length === 0) {
Expand Down
3 changes: 1 addition & 2 deletions packages/daf-cli/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as TG from 'daf-trust-graph'
import * as SRD from 'daf-selective-disclosure'
import { Gql as DataGql } from 'daf-data-store'
import merge from 'lodash.merge'
import { core, dataStore, initializeDb } from './setup'
import { core, dataStore } from './setup'
import { listen } from './services'
program
.command('graphql')
Expand Down Expand Up @@ -36,7 +36,6 @@ program
context: () => ({ dataStore, core }),
introspection: true,
})
await initializeDb()
await core.setupServices()
const info = await server.listen({ port: cmd.port })
console.log(`🚀 Server ready at ${info.url}`)
Expand Down
3 changes: 1 addition & 2 deletions packages/daf-cli/src/msg.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as Daf from 'daf-core'
import { core, dataStore, initializeDb } from './setup'
import { core, dataStore } from './setup'
import program from 'commander'

program
.command('msg <raw>')
.description('Handle raw message (JWT)')
.action(async raw => {
try {
await initializeDb()
const message = await core.validateMessage(
new Daf.Message({
raw,
Expand Down
4 changes: 1 addition & 3 deletions packages/daf-cli/src/sdr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Daf from 'daf-core'
import * as DIDComm from 'daf-did-comm'
import * as SD from 'daf-selective-disclosure'
import { core, dataStore, initializeDb } from './setup'
import { core, dataStore } from './setup'
import program from 'commander'
import inquirer from 'inquirer'
import qrcode from 'qrcode-terminal'
Expand All @@ -12,8 +12,6 @@ program
.option('-s, --send', 'Send')
.option('-q, --qrcode', 'Show qrcode')
.action(async cmd => {
await initializeDb()

const identities = await core.identityManager.getIdentities()
if (identities.length === 0) {
console.error('No dids')
Expand Down
4 changes: 1 addition & 3 deletions packages/daf-cli/src/services.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventTypes, Message } from 'daf-core'
import { core, dataStore, initializeDb } from './setup'
import { core, dataStore } from './setup'
import program from 'commander'
import { setInterval } from 'timers'

Expand All @@ -12,8 +12,6 @@ program
})

export const listen = async (pollSeconds?: number) => {
await initializeDb()

core.on(EventTypes.validatedMessage, async (msg: Message) => {
console.log('New message type:', msg.type)
})
Expand Down
7 changes: 3 additions & 4 deletions packages/daf-cli/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { DafUniversalResolver } from 'daf-resolver-universal'
import * as Daf from 'daf-core'
import * as DidJwt from 'daf-did-jwt'
import * as EthrDid from 'daf-ethr-did'
import * as DafFs from 'daf-fs'
import * as DafLibSodium from 'daf-libsodium'

import * as W3c from 'daf-w3c'
Expand Down Expand Up @@ -50,8 +49,8 @@ TG.ServiceController.webSocketImpl = ws

const identityProviders = [
new EthrDid.IdentityProvider({
identityStore: new DafFs.IdentityStore(defaultPath + '/rinkeby-identity-store.json'),
kms: new DafLibSodium.KeyManagementSystem(new DafFs.KeyStore(defaultPath + '/rinkeby-kms.json')),
identityStore: new Daf.IdentityStore('rinkeby-ethr'),
kms: new DafLibSodium.KeyManagementSystem(new Daf.KeyStore()),
network: 'rinkeby',
rpcUrl: 'https://rinkeby.infura.io/v3/' + infuraProjectId,
}),
Expand Down Expand Up @@ -86,7 +85,7 @@ export const initializeDb = async () => {
type: 'sqlite',
database: defaultPath + 'database-v2.sqlite',
synchronize: true,
logging: true,
logging: false,
entities: [...Daf.Entities],
})
}
Expand Down
5 changes: 2 additions & 3 deletions packages/daf-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ export { KeyStore } from './identity/key-store'

import { Key, KeyType } from './entities/key'
import { Identity } from './entities/identity'
import { MessageMetaData } from './entities/message-meta-data'
import { Claim } from './entities/claim'
import { Credential } from './entities/credential'
import { Presentation } from './entities/presentation'
import { Message } from './entities/message'

export const Entities = [Key, Identity, Message, MessageMetaData, Claim, Credential, Presentation]
export const Entities = [Key, Identity, Message, Claim, Credential, Presentation]

export { KeyType, Key, Identity, Message, MessageMetaData, Claim, Credential, Presentation }
export { KeyType, Key, Identity, Message, Claim, Credential, Presentation }

0 comments on commit 7b5959e

Please sign in to comment.