Skip to content

Commit

Permalink
chore: use common db for askar and exchanges api
Browse files Browse the repository at this point in the history
- replace better-sqlite3 with sqlite
- rename in-memory-db.ts to db-config.ts
- TypeOrmSQLiteModule to accept inMemory boolean param; helps in db initialization for app and unit tests

Signed-off-by: ShikharBhatt <[email protected]>
  • Loading branch information
ShikharBhatt authored and jrhender committed Oct 3, 2024
1 parent ecf0990 commit 6cf5a3c
Show file tree
Hide file tree
Showing 10 changed files with 421 additions and 46 deletions.
4 changes: 2 additions & 2 deletions apps/vc-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"rimraf": "^4.0.0",
"rxjs": "^7.8.1",
"typeorm": "^0.3.17",
"better-sqlite3": "~8.4.0",
"did-resolver": "~4.1.0",
"class-validator": "~0.14.0",
"class-transformer": "~0.5.1",
Expand All @@ -53,7 +52,8 @@
"@credo-ts/core": "^0.5.3",
"@credo-ts/node": "^0.5.3",
"@hyperledger/aries-askar-nodejs": "^0.2.1",
"@hyperledger/aries-askar-shared": "^0.2.3"
"@hyperledger/aries-askar-shared": "^0.2.3",
"sqlite3": "~5.1.7"
},
"devDependencies": {
"@nestjs/cli": "^10.1.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/vc-api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ConfigModule } from '@nestjs/config';
import { KeyModule } from './key/key.module';
import { DidModule } from './did/did.module';
import { VcApiModule } from './vc-api/vc-api.module';
import { TypeOrmSQLiteModule } from './in-memory-db';
import { TypeOrmSQLiteModule } from './db-config';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { SeederModule } from './seeder/seeder.module';
Expand Down
4 changes: 3 additions & 1 deletion apps/vc-api/src/config/env-vars-validation-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import * as Joi from 'joi';
import { homedir } from 'os';

export const envVarsValidationSchema = Joi.object({
NODE_ENV: Joi.string().valid('development', 'production', 'test').default('development'),
Expand All @@ -12,5 +13,6 @@ export const envVarsValidationSchema = Joi.object({
CREDO_LABEL: Joi.string().default('vc-api-agent'),
CREDO_WALLET_ID: Joi.string().default('vc-api-wallet'),
CREDO_WALLET_KEY: Joi.string().default('vc-api-wallet-key-0001'),
CREDO_WALLET_DB_TYPE: Joi.string().default('sqlite')
CREDO_WALLET_DB_TYPE: Joi.string().default('sqlite'),
DB_BASE_PATH: Joi.string().default(`${homedir}/.vc-api`)
});
9 changes: 7 additions & 2 deletions apps/vc-api/src/credo/credo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ export class CredoService implements OnModuleInit, OnModuleDestroy {
id: this.configService.get<string>('CREDO_WALLET_ID'),
key: this.configService.get<string>('CREDO_WALLET_KEY'),
storage: {
type: this.configService.get<string>('CREDO_WALLET_DB_TYPE')
type: this.configService.get<string>('CREDO_WALLET_DB_TYPE'),
config: {
path: `${this.configService.get<string>('DB_BASE_PATH')}/${this.configService.get<string>(
'CREDO_WALLET_ID'
)}/sqlite.db`
}
}
};

Expand Down Expand Up @@ -84,7 +89,7 @@ export class CredoService implements OnModuleInit, OnModuleDestroy {
private async cleanup() {
if (this.agent.isInitialized) {
// close askar agent connection
await this.agent.wallet.close();
await this.wallet.close();
await this.agent.shutdown();
}
}
Expand Down
25 changes: 25 additions & 0 deletions apps/vc-api/src/db-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2021 - 2023 Energy Web Foundation
* SPDX-License-Identifier: Apache-2.0
*/

import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';

/**
* Inspired by https://dev.to/webeleon/unit-testing-nestjs-with-typeorm-in-memory-l6m
* @param inMemory // default set to false; set to true for unit testing
*/
export const TypeOrmSQLiteModule = (inMemory = false) =>
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
type: 'sqlite',
database: !inMemory ? `${configService.get<string>('DB_BASE_PATH')}/exchanges.db` : ':memory:',
// dropSchema: true, // disabling this option to avoid dropping askar tables
autoLoadEntities: true, // https://docs.nestjs.com/techniques/database#auto-load-entities
synchronize: true,
keepConnectionAlive: true // https://github.com/nestjs/typeorm/issues/61
}),
inject: [ConfigService]
});
4 changes: 2 additions & 2 deletions apps/vc-api/src/did/did.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TypeOrmSQLiteModule } from '../in-memory-db';
import { TypeOrmSQLiteModule } from '../db-config';
import { KeyModule } from '../key/key.module';
import { DIDController } from './did.controller';
import { DIDService } from './did.service';
Expand All @@ -21,7 +21,7 @@ describe('DidController', () => {
imports: [
KeyModule,
CredoModule,
TypeOrmSQLiteModule(),
TypeOrmSQLiteModule(true),
TypeOrmModule.forFeature([DIDDocumentEntity, VerificationMethodEntity])
],
controllers: [DIDController],
Expand Down
4 changes: 2 additions & 2 deletions apps/vc-api/src/did/did.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import { DIDKeyFactory } from '@energyweb/ssi-did';
import { TypeOrmSQLiteModule } from '../in-memory-db';
import { TypeOrmSQLiteModule } from '../db-config';
import { KeyModule } from '../key/key.module';
import { KeyService } from '../key/key.service';
import { DIDService } from './did.service';
Expand All @@ -26,7 +26,7 @@ describe('DIDService', () => {
imports: [
KeyModule,
CredoModule,
TypeOrmSQLiteModule(),
TypeOrmSQLiteModule(true),
TypeOrmModule.forFeature([DIDDocumentEntity, VerificationMethodEntity])
],
providers: [
Expand Down
19 changes: 0 additions & 19 deletions apps/vc-api/src/in-memory-db.ts

This file was deleted.

4 changes: 2 additions & 2 deletions apps/vc-api/src/key/key.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import { JWK } from 'jose';
import { TypeOrmSQLiteModule } from '../in-memory-db';
import { TypeOrmSQLiteModule } from '../db-config';
import { KeyPair } from './key-pair.entity';
import { KeyService } from './key.service';
import { CredoModule } from '../credo/credo.module';
Expand All @@ -22,7 +22,7 @@ describe('KeyService', () => {

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [TypeOrmSQLiteModule(), TypeOrmModule.forFeature([KeyPair]), CredoModule],
imports: [TypeOrmSQLiteModule(true), TypeOrmModule.forFeature([KeyPair]), CredoModule],
providers: [
KeyService,
{
Expand Down
Loading

0 comments on commit 6cf5a3c

Please sign in to comment.