diff --git a/libs/prisma-service/prisma/seed.ts b/libs/prisma-service/prisma/seed.ts index 1497d8eb7..239cbaab6 100644 --- a/libs/prisma-service/prisma/seed.ts +++ b/libs/prisma-service/prisma/seed.ts @@ -211,33 +211,38 @@ const createPlatformUserOrgRoles = async (): Promise => { const createLedger = async (): Promise => { try { - const { ledgerData } = JSON.parse(configData); - - const ledgerIndyNamespace = ledgerData.map(ledger => ledger.indyNamespace); - const existLedgerIndyNameSpace = await prisma.ledgers.findMany({ - where: { - indyNamespace: { - in: ledgerIndyNamespace - } - } + const { ledgerData } = JSON.parse(configData); + + const existingLedgers = await prisma.ledgers.findMany(); + + if (0 === existingLedgers.length) { + const createLedger = await prisma.ledgers.createMany({ + data: ledgerData }); - - if (0 === existLedgerIndyNameSpace.length) { - - const createLedger = await prisma.ledgers.createMany({ + logger.log('All ledgers inserted:', createLedger); + } else { + const updatesNeeded = []; + + if (existingLedgers.length !== ledgerData.length) { + updatesNeeded.push(ledgerData); + if (0 < updatesNeeded.length) { + await prisma.ledgers.deleteMany(); + + const createLedger = await prisma.ledgers.createMany({ data: ledgerData - }); - - logger.log(createLedger); + }); + logger.log('Updated ledgers:', createLedger); + } else { + logger.log('No changes in ledger data'); + } } else { - logger.log('Already seeding in ledgers'); + logger.log('No changes in ledger data'); } - - + } } catch (e) { - logger.error('An error occurred seeding createLedger:', e); + logger.error('An error occurred seeding createLedger:', e); } -}; + }; const createEcosystemRoles = async (): Promise => { try {