Skip to content

Commit

Permalink
feat: Add controller db
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran committed May 4, 2023
1 parent c10d1b5 commit aea3272
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 51 deletions.
68 changes: 31 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"node-cache": "^5.1.2",
"pg": "^8.10.0",
"pg-connection-string": "^2.5.0",
"swagger-ui-express": "^4.6.1"
"swagger-ui-express": "^4.6.1",
"typeorm": "^0.3.15"
},
"devDependencies": {
"@semantic-release/changelog": "^6.0.2",
Expand Down
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CORS_ERROR_MSG } from './types/constants'
import * as swagger from 'swagger-ui-express'
import * as swaggerJson from '../swagger.json'
import { IssuerController } from './controllers/issuer'
import { Connection } from './database/connection/connection'

require('dotenv').config()

Expand All @@ -17,6 +18,7 @@ class App {
this.express = express()
this.middleware()
this.routes()
// Connection.instance.connect()
}

private middleware() {
Expand Down
42 changes: 42 additions & 0 deletions src/database/connection/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { parse } from 'pg-connection-string'
import { DataSource } from 'typeorm'
import { migrations, Entities } from '@veramo/data-store'
import { CustomerEntity } from '../entities/customer.entity'

const { ISSUER_DATABASE_URL} = process.env

export class Connection {
public dbConnection : DataSource
public static instance = new Connection()

constructor () {
const config = parse(ISSUER_DATABASE_URL)
if(!(config.host && config.port && config.database)) {
throw new Error(`Error: Invalid Database url`)
}

this.dbConnection = new DataSource({
type: 'postgres',
host: config.host,
port: Number(config.port),
username: config.user,
password: config.password,
database: config.database,
ssl: config.ssl ? true : false,
migrations,
entities: [...Entities, CustomerEntity],
synchronize: true,
logging: ['error', 'info', 'warn']
})
}

public async connect() {
try {
await this.dbConnection.initialize()
} catch (error) {
throw new Error(`Error initializing db: ${error}`)
}
}

}

27 changes: 27 additions & 0 deletions src/database/entities/customer.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'

@Entity('customers')
export class CustomerEntity {
@Column('string')
customerId

@Column('string', {array: true})
kids

@Column('string', {array: true})
dids: string[]

@Column('string', {array: true})
claimIds: string[]

@Column('string', {array: true})
presentationIds: string[]

constructor({ customerId, kids=[], dids=[], claimIds=[], presentationIds=[] }: { customerId: string, kids?: string[], dids?: string[], claimIds?: string[], presentationIds?: string[]}) {
this.customerId = customerId
this.kids = kids
this.dids = dids
this.claimIds = claimIds
this.presentationIds = presentationIds
}
}
17 changes: 17 additions & 0 deletions src/services/customer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Connection } from '../database/connection/connection'
import { CustomerEntity } from '../database/entities/customer.entity'


export class CustomerService {
public async create(customer: CustomerEntity) {
return await Connection.instance.dbConnection.manager.save(customer)
}

public async update(customer: CustomerEntity, customerId: string) {
return await Connection.instance.dbConnection.manager.save({ customerId, customer })
}

public async get() {
return await Connection.instance.dbConnection.manager.find(CustomerEntity)
}
}
15 changes: 2 additions & 13 deletions src/services/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import { KeyStore, DIDStore, PrivateKeyStore, migrations, Entities } from '@vera
import { Resolver, ResolverRegistry } from 'did-resolver'
import { CheqdDIDProvider, getResolver as CheqdDidResolver } from '@cheqd/did-provider-cheqd'
import { v4 } from 'uuid'
import { DataSource } from 'typeorm'

import { cheqdDidRegex, DefaultRPCUrl } from '../types/types'
import { CheqdNetwork } from '@cheqd/sdk'
import { CredentialIssuerLD, LdDefaultContexts, VeramoEd25519Signature2018 } from '@veramo/credential-ld'
import { parse } from 'pg-connection-string'
import { Connection } from '../database/connection/connection'

require('dotenv').config()

Expand Down Expand Up @@ -53,18 +53,7 @@ export class Identity {
if(!(config.host && config.port && config.database)) {
throw new Error(`Error: Invalid Database url`)
}
const dbConnection = new DataSource({
type: 'postgres',
host: config.host,
port: Number(config.port),
username: config.user,
password: config.password,
database: config.database,
ssl: config.ssl ? true : false,
migrations,
entities: Entities,
logging: ['error', 'info', 'warn']
})
const dbConnection = Connection.instance.dbConnection
return createAgent<IDIDManager & IKeyManager & IDataStore & IResolver>({
plugins: [
new KeyManager({
Expand Down

0 comments on commit aea3272

Please sign in to comment.