Fetch record id from registry for deployment record

This commit is contained in:
Prathamesh Musale 2024-05-08 12:35:40 +05:30
parent 7a49cfe0bb
commit 4aba3d0168

View File

@ -7,9 +7,15 @@ import { Registry } from '@cerc-io/registry-sdk';
import { getConfig, getGasAndFees, getConnectionInfo, txOutput } from './util'; import { getConfig, getGasAndFees, getConnectionInfo, txOutput } from './util';
const recordTypeToRecordField = new Map<string, string>([
['WatcherRecord', 'watcher'],
['SubgraphRecord', 'subgraph'],
['ServiceRecord', 'service']
]);
async function main () { async function main () {
const argv = getArgs(); const argv = getArgs();
const { records: recordsDir, config } = argv; const { records: recordsDir, config, type: recordType } = argv;
const { services: { registry: registryConfig } } = getConfig(config as string); const { services: { registry: registryConfig } } = getConfig(config as string);
@ -31,6 +37,8 @@ async function main () {
const jsonFiles = files.filter(file => path.extname(file).toLowerCase() === '.json'); const jsonFiles = files.filter(file => path.extname(file).toLowerCase() === '.json');
// Read record from each JSON file // Read record from each JSON file
console.log('**************************************');
console.log(`Publishing records from ${recordsDir}`);
for (let i = 0; i < jsonFiles.length; i++) { for (let i = 0; i < jsonFiles.length; i++) {
const file = jsonFiles[i]; const file = jsonFiles[i];
@ -42,30 +50,44 @@ async function main () {
console.log(`Published record from ${file}`); console.log(`Published record from ${file}`);
txOutput(result, JSON.stringify(result, undefined, 2), argv.output, argv.verbose); txOutput(result, JSON.stringify(result, undefined, 2), argv.output, argv.verbose);
}
// Find the deployment record using filename // Check if deployment record files exist
const deploymentRecordsDir = path.resolve(recordsDir, 'deployments'); const deploymentRecordsDir = path.resolve(recordsDir, 'deployments');
if (!fs.statSync(deploymentRecordsDir).isDirectory()) {
return;
}
if (fs.statSync(deploymentRecordsDir).isDirectory()) { console.log('**************************************');
const recordName = path.basename(file, path.extname(file)); console.log(`Publishing deployment records from ${deploymentRecordsDir}`);
const deploymentRecordFile = `${recordName}Deployment.json`;
const deploymentRecordFilePath = path.resolve(deploymentRecordsDir, deploymentRecordFile);
// Check if deployment record exists // List record files
if (fs.existsSync(deploymentRecordFilePath)) { const deploymentFiles = fs.readdirSync(deploymentRecordsDir);
console.log(`Deployment record ${deploymentRecordFile} found`); const deploymentJsonFiles = deploymentFiles.filter(file => path.extname(file).toLowerCase() === '.json');
const deploymentRecord = readRecord(deploymentRecordFilePath);
// Set watcher field to watcher record id for (let i = 0; i < deploymentJsonFiles.length; i++) {
deploymentRecord.watcher = result.id; const file = deploymentJsonFiles[i];
// Publish deployment record const filePath = path.resolve(deploymentRecordsDir, file);
const deploymentResult = await registry.setRecord({ privateKey: userKey, record: deploymentRecord, bondId }, userKey, fee); const deploymentRecord = readRecord(filePath);
console.log(`Published deployment record from ${deploymentRecordFile}`); // Find record using name and given type
txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), argv.output, argv.verbose); const recordName = deploymentRecord.name;
} const queryResult = await registry.queryRecords({ type: recordType, name: recordName }, true);
if (queryResult.length === 0) {
throw new Error(`Record not found, type: ${recordType}, name: ${recordName}`);
} }
const recordId = queryResult[0].id;
// Set record field to record id
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
deploymentRecord[recordTypeToRecordField.get(recordType)!] = recordId;
// Publish record
const deploymentResult = await registry.setRecord({ privateKey: userKey, record: deploymentRecord, bondId }, userKey, fee);
console.log(`Published record from ${file}`);
txOutput(deploymentResult, JSON.stringify(deploymentResult, undefined, 2), argv.output, argv.verbose);
} }
} }
@ -97,6 +119,12 @@ function getArgs (): any {
type: 'string', type: 'string',
demandOption: true demandOption: true
}) })
.option('type', {
alias: 't',
describe: 'Record type',
type: 'string',
demandOption: true
})
.help().argv; .help().argv;
} }