From adf5daf1b26bd62bc9a987a79b464eb98afeae3b Mon Sep 17 00:00:00 2001 From: Maycon Mello Date: Fri, 5 Jan 2024 15:14:24 -0300 Subject: [PATCH 1/4] improve debug logs --- .../src/entities/document/create-document.ts | 10 +++++++++- .../src/entities/document/remove-document.ts | 2 ++ .../src/entities/document/update-document.ts | 3 +++ packages/data-store/src/index.ts | 8 +++++++- packages/data-store/src/logger.ts | 6 +++++- packages/data-store/src/migration/index.ts | 3 +++ .../src/migrations/1691498362273-bootstrap.ts | 9 +++++++-- 7 files changed, 36 insertions(+), 5 deletions(-) diff --git a/packages/data-store/src/entities/document/create-document.ts b/packages/data-store/src/entities/document/create-document.ts index 45c7b7bd..9c66bfcf 100644 --- a/packages/data-store/src/entities/document/create-document.ts +++ b/packages/data-store/src/entities/document/create-document.ts @@ -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 @@ -15,6 +16,8 @@ export async function createDocument({ }: ContextProps & { json: any; }): Promise { + logger.debug(`Creating document with id ${json.id}...`); + return dataStore.db.transaction(async transactionalEntityManager => { if (json.id) { const existingDocument = await getDocumentById({ @@ -23,6 +26,7 @@ export async function createDocument({ }); if (existingDocument) { + logger.debug('Document already exists'); throw new Error(`Document with id ${json.id} already exists`); } } @@ -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; }); } diff --git a/packages/data-store/src/entities/document/remove-document.ts b/packages/data-store/src/entities/document/remove-document.ts index 80ec9dbe..eb2329cc 100644 --- a/packages/data-store/src/entities/document/remove-document.ts +++ b/packages/data-store/src/entities/document/remove-document.ts @@ -1,6 +1,7 @@ import assert from 'assert'; import {ContextProps} from '../../types'; import {DocumentEntity} from './document.entity'; +import { logger } from '../../logger'; /** * Remove document @@ -13,6 +14,7 @@ export async function removeDocument({ }: ContextProps & {id: string}): Promise { 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({ diff --git a/packages/data-store/src/entities/document/update-document.ts b/packages/data-store/src/entities/document/update-document.ts index 70b65604..071747d3 100644 --- a/packages/data-store/src/entities/document/update-document.ts +++ b/packages/data-store/src/entities/document/update-document.ts @@ -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 @@ -12,6 +13,8 @@ export async function updateDocument({ document, }: ContextProps & {document: WalletDocument}): Promise { return dataStore.db.transaction(async transactionalEntityManager => { + logger.debug(`Updating document with id ${document.id}`); + const repository = transactionalEntityManager.getRepository(DocumentEntity); const entity = await toDocumentEntity({ dataStore, diff --git a/packages/data-store/src/index.ts b/packages/data-store/src/index.ts index b01bbd0e..ad37244f 100644 --- a/packages/data-store/src/index.ts +++ b/packages/data-store/src/index.ts @@ -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; @@ -55,10 +56,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}); @@ -67,7 +71,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; } diff --git a/packages/data-store/src/logger.ts b/packages/data-store/src/logger.ts index 35e58a9a..da2cce46 100644 --- a/packages/data-store/src/logger.ts +++ b/packages/data-store/src/logger.ts @@ -15,7 +15,11 @@ const ConsoleTransport = { // const TypeORMTransport = {}; -export const logger = ConsoleTransport; +export let logger = ConsoleTransport; + +export function setLogger(impl: any) { + logger = impl; +} export const createLogger = (prefix: string) => { return { diff --git a/packages/data-store/src/migration/index.ts b/packages/data-store/src/migration/index.ts index 9e69a3c5..643ff941 100644 --- a/packages/data-store/src/migration/index.ts +++ b/packages/data-store/src/migration/index.ts @@ -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) { diff --git a/packages/data-store/src/migrations/1691498362273-bootstrap.ts b/packages/data-store/src/migrations/1691498362273-bootstrap.ts index 25d853d6..e9435bad 100644 --- a/packages/data-store/src/migrations/1691498362273-bootstrap.ts +++ b/packages/data-store/src/migrations/1691498362273-bootstrap.ts @@ -6,6 +6,7 @@ import { TableForeignKey, TableIndex, } from 'typeorm'; +import { logger } from '../logger'; export class Bootstrap1691498362273 implements MigrationInterface { name = 'Bootstrap1691498362273'; @@ -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({ @@ -111,9 +112,12 @@ export class Bootstrap1691498362273 implements MigrationInterface { columnNames: ['documentTypeEntityId'], }), ); + + logger.debug('Table bootstrap migration completed'); } public async down(queryRunner: QueryRunner): Promise { + logger.debug('Running table bootstrap migration revert'); await queryRunner.dropTable( 'document_entity__type_rel_document_type_entity', ); @@ -121,5 +125,6 @@ export class Bootstrap1691498362273 implements MigrationInterface { await queryRunner.dropTable('document_entity'); await queryRunner.dropTable('document_type_entity'); await queryRunner.dropTable('network_entity'); + logger.debug('Revert completed'); } } From 7e93ebec0acd421c8ea0b2276edd84c7c3eabc40 Mon Sep 17 00:00:00 2001 From: Maycon Mello Date: Fri, 5 Jan 2024 15:16:29 -0300 Subject: [PATCH 2/4] cleanup --- packages/data-store/src/logger.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/data-store/src/logger.ts b/packages/data-store/src/logger.ts index da2cce46..b8252e3b 100644 --- a/packages/data-store/src/logger.ts +++ b/packages/data-store/src/logger.ts @@ -13,8 +13,6 @@ const ConsoleTransport = { }, }; -// const TypeORMTransport = {}; - export let logger = ConsoleTransport; export function setLogger(impl: any) { From ba7712edb5b102e01b9acb33d142920e3b755ded Mon Sep 17 00:00:00 2001 From: Maycon Mello Date: Fri, 5 Jan 2024 15:45:43 -0300 Subject: [PATCH 3/4] cleanup --- packages/data-store/src/index.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/data-store/src/index.ts b/packages/data-store/src/index.ts index ad37244f..57dc524d 100644 --- a/packages/data-store/src/index.ts +++ b/packages/data-store/src/index.ts @@ -41,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, From 0184be6fe1c1f60cd6473d3c701ed0dc1137a567 Mon Sep 17 00:00:00 2001 From: Maycon Mello Date: Tue, 9 Jan 2024 13:31:01 -0300 Subject: [PATCH 4/4] pr feedback --- packages/data-store/src/entities/document/create-document.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/data-store/src/entities/document/create-document.ts b/packages/data-store/src/entities/document/create-document.ts index 9c66bfcf..b7bc976b 100644 --- a/packages/data-store/src/entities/document/create-document.ts +++ b/packages/data-store/src/entities/document/create-document.ts @@ -26,7 +26,7 @@ export async function createDocument({ }); if (existingDocument) { - logger.debug('Document already exists'); + logger.debug(`Document with id ${json.id} already exists`); throw new Error(`Document with id ${json.id} already exists`); } }