index.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import * as dotenv from "dotenv";
  2. import * as chalk from "chalk";
  3. import * as figlet from "figlet";
  4. import * as commander from "commander";
  5. import * as path from "path";
  6. import { configure, getLogger } from "log4js";
  7. import {
  8. QueryNodeManager,
  9. DatabaseManager,
  10. SubstrateEvent,
  11. } from "@joystream/hydra-indexer-lib/lib";
  12. // Mappings use!
  13. export { DatabaseManager as DB, getLogger, SubstrateEvent };
  14. const logger = getLogger();
  15. const withErrors = (command: (...args: any[]) => Promise<void>) => {
  16. return async (...args: any[]) => {
  17. try {
  18. await command(...args);
  19. } catch (e) {
  20. console.log(chalk.red(e.stack));
  21. process.exit(1);
  22. }
  23. };
  24. };
  25. const withEnvs = (command: () => Promise<void>) => {
  26. return async (opts: any) => {
  27. setUp(opts);
  28. await command();
  29. };
  30. };
  31. function main(): commander.Command {
  32. console.log(chalk.green(figlet.textSync("Query_node-Indexer")));
  33. const program = new commander.Command();
  34. const version = require("./package.json").version;
  35. program.version(version).description("Query_node Indexer");
  36. program
  37. .command("index")
  38. .option("-h, --height", "starting block height")
  39. .option("-e, --env <file>", ".env file location", "../../.env")
  40. .description("Index all events and extrinsics in the substrate chain")
  41. .action(withErrors(withEnvs(runIndexer)));
  42. program
  43. .command("process")
  44. .option("-h, --height", "starting block height")
  45. .option("-e, --env <file>", ".env file location", "../../.env")
  46. .description("Process the event and extrinsic mappings using the index")
  47. .action(withErrors(withEnvs(runProcessor)));
  48. program
  49. .command("migrate")
  50. .description("Create the indexer schema")
  51. .option("-e, --env <file>", ".env file location", "../../.env")
  52. .action(withErrors(withEnvs(runMigrations)));
  53. program.parse(process.argv);
  54. return program;
  55. }
  56. function setUp(opts: any) {
  57. // dotenv config
  58. dotenv.config();
  59. dotenv.config({ path: opts.env });
  60. if (opts.height) {
  61. process.env.BLOCK_HEIGHT = opts.height;
  62. } else if (!process.env.BLOCK_HEIGHT) {
  63. process.env.BLOCK_HEIGHT = "0";
  64. }
  65. //log4js config
  66. if (opts.logging) {
  67. configure(opts.logging);
  68. } else {
  69. // log4js default: DEBUG to console output;
  70. getLogger().level = "debug";
  71. }
  72. }
  73. async function runProcessor() {
  74. const node = new QueryNodeManager();
  75. const atBlock = process.env.BLOCK_HEIGHT;
  76. await node.process({
  77. atBlock: atBlock && atBlock !== "0" ? Number.parseInt(atBlock) : undefined,
  78. processingPack: require("../../mappings"),
  79. entities: [
  80. path.join(__dirname, "../graphql-server/src/modules/**/*.model.ts"),
  81. ],
  82. indexerEndpointURL: process.env.INDEXER_ENDPOINT_URL,
  83. });
  84. }
  85. async function runIndexer() {
  86. const node = new QueryNodeManager();
  87. const atBlock = process.env.BLOCK_HEIGHT;
  88. await node.index({
  89. wsProviderURI: process.env.WS_PROVIDER_ENDPOINT_URI || "",
  90. atBlock: atBlock && atBlock !== "0" ? Number.parseInt(atBlock) : undefined,
  91. types: process.env.TYPES_JSON
  92. ? (require(process.env.TYPES_JSON) as Record<
  93. string,
  94. Record<string, string>
  95. >)
  96. : {},
  97. });
  98. }
  99. async function runMigrations() {
  100. logger.info(`Running migrations`);
  101. await QueryNodeManager.migrate();
  102. // TODO: here should be TypeORM migrations...
  103. }
  104. main();