Skip to content

Commit

Permalink
Merge pull request #191 from docknetwork/feat/improve-debug-logs
Browse files Browse the repository at this point in the history
improve debug logs
  • Loading branch information
maycon-mello authored Jan 9, 2024
2 parents 77fab61 + 149f3d7 commit 27e51c6
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 10 deletions.
10 changes: 9 additions & 1 deletion packages/data-store/src/entities/document/create-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { v4 as uuid } from 'uuid';
import { DocumentEntity } from './document.entity';
import { getOrCreateDocumentTypes, saveOptions } from './helpers';
import { getDocumentById } from './get-document-by-id';
import { logger } from '../../logger';

/**
* Create document
Expand All @@ -15,6 +16,8 @@ export async function createDocument({
}: ContextProps & {
json: any;
}): Promise<WalletDocument> {
logger.debug(`Creating document with id ${json.id}...`);

return dataStore.db.transaction(async transactionalEntityManager => {
if (json.id) {
const existingDocument = await getDocumentById({
Expand All @@ -23,6 +26,7 @@ export async function createDocument({
});

if (existingDocument) {
logger.debug(`Document with id ${json.id} already exists`);
throw new Error(`Document with id ${json.id} already exists`);
}
}
Expand Down Expand Up @@ -61,6 +65,10 @@ export async function createDocument({

const repository = transactionalEntityManager.getRepository(DocumentEntity);

return repository.save(entity, saveOptions);
const result = await repository.save(entity, saveOptions);

logger.debug(`Document added to the wallet`);

return result;
});
}
2 changes: 2 additions & 0 deletions packages/data-store/src/entities/document/remove-document.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'assert';
import {ContextProps} from '../../types';
import {DocumentEntity} from './document.entity';
import { logger } from '../../logger';

/**
* Remove document
Expand All @@ -13,6 +14,7 @@ export async function removeDocument({
}: ContextProps & {id: string}): Promise<void> {
assert(!!id, 'Document id is required');

logger.debug(`Removing document with id ${id}`);
return dataStore.db.transaction(async transactionalEntityManager => {
const repository = transactionalEntityManager.getRepository(DocumentEntity);
await repository.delete({
Expand Down
3 changes: 3 additions & 0 deletions packages/data-store/src/entities/document/update-document.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ContextProps, WalletDocument } from '../../types';
import { saveOptions, toDocumentEntity } from './helpers';
import { DocumentEntity } from './document.entity';
import { logger } from '../../logger';

/**
* Update document
Expand All @@ -12,6 +13,8 @@ export async function updateDocument({
document,
}: ContextProps & {document: WalletDocument}): Promise<WalletDocument> {
return dataStore.db.transaction(async transactionalEntityManager => {
logger.debug(`Updating document with id ${document.id}`);

const repository = transactionalEntityManager.getRepository(DocumentEntity);
const entity = await toDocumentEntity({
dataStore,
Expand Down
12 changes: 7 additions & 5 deletions packages/data-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {DataSource} from './typeorm';
import assert from 'assert';
import {getWallet, updateWallet} from './entities/wallet.entity';
import { getV1LocalStorage } from './migration/migration1/v1-data-store';
import { getAllDocuments } from './entities/document';


export const getLocalStorage = getV1LocalStorage;
Expand Down Expand Up @@ -40,10 +41,6 @@ export async function createDataStore(
options.defaultNetwork = options.networks[0].id;
}

logger.debug(
`Initializing data store with configs: ${JSON.stringify(options)}`,
);

const dataSource: DataSource = await initializeTypeORM(options);
const dataStore: DataStore = {
db: dataSource,
Expand All @@ -55,10 +52,13 @@ export async function createDataStore(
networks: options.networks,
resolveDocumentNetwork: options.documentNetworkResolver,
setNetwork: (networkId: string) => {
logger.debug(`Setting network to ${networkId}`);
return updateNetwork({dataStore, networkId});
},
};

logger.debug('Data store initialized');

await migrate({dataStore});

const wallet = await getWallet({dataStore});
Expand All @@ -67,7 +67,9 @@ export async function createDataStore(
item => item.id === wallet.networkId,
);

console.log('current saved wallet on db', wallet);
getAllDocuments({dataStore}).then(documents => {
logger.debug(`Wallet loaded with ${documents.length} documents`);
});

return dataStore;
}
Expand Down
6 changes: 4 additions & 2 deletions packages/data-store/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ const ConsoleTransport = {
},
};

// const TypeORMTransport = {};
export let logger = ConsoleTransport;

export const logger = ConsoleTransport;
export function setLogger(impl: any) {
logger = impl;
}

export const createLogger = (prefix: string) => {
return {
Expand Down
3 changes: 3 additions & 0 deletions packages/data-store/src/migration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ export async function migrate({dataStore}: ContextProps) {
// dataStore.version = 'v1';
// If no configs exist, create a new one
if (!existingConfigs) {
logger.debug('Wallet not found in the database, creating a new wallet...');
const isV1DataStore = await isRunningOnV1DataStore({dataStore});
logger.debug(`Is v1 data store: ${isV1DataStore}`);
dataStore.version = isV1DataStore ? 'v1' : CURRENT_DATA_STORE_VERSION;
await createWallet({
dataStore,
});
logger.debug('universal wallet created');
}

for (const migrate of migrations) {
Expand Down
9 changes: 7 additions & 2 deletions packages/data-store/src/migrations/1691498362273-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
TableForeignKey,
TableIndex,
} from 'typeorm';
import { logger } from '../logger';

export class Bootstrap1691498362273 implements MigrationInterface {
name = 'Bootstrap1691498362273';
Expand All @@ -14,11 +15,11 @@ export class Bootstrap1691498362273 implements MigrationInterface {
const hasNetworkEntityTable = await queryRunner.hasTable('network_entity');

if (hasNetworkEntityTable) {
console.log('Table already bootstrapped');
logger.debug('Table already bootstrapped');
return;
}

console.log('Running table bootstrap migration');
logger.debug('Running table bootstrap migration');

await queryRunner.createTable(
new Table({
Expand Down Expand Up @@ -111,15 +112,19 @@ export class Bootstrap1691498362273 implements MigrationInterface {
columnNames: ['documentTypeEntityId'],
}),
);

logger.debug('Table bootstrap migration completed');
}

public async down(queryRunner: QueryRunner): Promise<void> {
logger.debug('Running table bootstrap migration revert');
await queryRunner.dropTable(
'document_entity__type_rel_document_type_entity',
);
await queryRunner.dropTable('wallet_entity');
await queryRunner.dropTable('document_entity');
await queryRunner.dropTable('document_type_entity');
await queryRunner.dropTable('network_entity');
logger.debug('Revert completed');
}
}

0 comments on commit 27e51c6

Please sign in to comment.