UNPKG

envio

Version:

A latency and sync speed optimized, developer friendly blockchain data indexer.

314 lines (297 loc) • 11.2 kB
// Generated by ReScript, PLEASE EDIT WITH CARE 'use strict'; var Rest = require("./vendored/Rest.res.js"); var Table = require("./db/Table.res.js"); var Utils = require("./Utils.res.js"); var Schema = require("./db/Schema.res.js"); var Logging = require("./Logging.res.js"); var Belt_Array = require("rescript/lib/js/belt_Array.js"); var InternalTable = require("./db/InternalTable.res.js"); var S$RescriptSchema = require("rescript-schema/src/S.res.js"); var Caml_js_exceptions = require("rescript/lib/js/caml_js_exceptions.js"); function auth(s) { return { role: s.header("X-Hasura-Role", S$RescriptSchema.string), secret: s.header("X-Hasura-Admin-Secret", S$RescriptSchema.string) }; } var responses = [ (function (s) { s.status(200); s.data(S$RescriptSchema.unknown); return "QuerySucceeded"; }), (function (s) { s.field("code", S$RescriptSchema.$$enum([ "already-exists", "already-tracked" ])); return "AlreadyDone"; }) ]; function clearMetadataRoute() { return { method: "POST", path: "", input: (function (s) { s.field("type", S$RescriptSchema.literal("clear_metadata")); s.field("args", S$RescriptSchema.literal({})); return auth(s); }), responses: responses }; } function trackTablesRoute() { return { method: "POST", path: "", input: (function (s) { s.field("type", S$RescriptSchema.literal("pg_track_tables")); return { args: s.field("args", S$RescriptSchema.json(false)), auth: auth(s) }; }), responses: responses }; } function createSelectPermissionRoute() { return { method: "POST", path: "", input: (function (s) { s.field("type", S$RescriptSchema.literal("pg_create_select_permission")); return { args: s.field("args", S$RescriptSchema.json(false)), auth: auth(s) }; }), responses: responses }; } function rawBodyRoute() { return { method: "POST", path: "", input: (function (s) { return { bodyString: s.rawBody(S$RescriptSchema.string), auth: auth(s) }; }), responses: responses }; } function bulkKeepGoingRoute() { return { method: "POST", path: "", input: (function (s) { s.field("type", S$RescriptSchema.literal("bulk_keep_going")); return { args: s.field("args", S$RescriptSchema.json(false)), auth: auth(s) }; }), responses: [(function (s) { s.status(200); return s.data(S$RescriptSchema.json(false)); })] }; } var bulkKeepGoingErrorsSchema = S$RescriptSchema.transform(S$RescriptSchema.array(S$RescriptSchema.union([ S$RescriptSchema.object(function (s) { s.tag("message", "success"); }), S$RescriptSchema.object(function (s) { return s.f("error", S$RescriptSchema.string); }) ])), (function (param) { return { p: (function (a) { return Belt_Array.keepMapU(a, (function (a) { return a; })); }) }; })); async function clearHasuraMetadata(endpoint, auth) { try { var result = await Rest.$$fetch(clearMetadataRoute, auth, Rest.client(endpoint, undefined)); var msg; msg = result === "QuerySucceeded" ? "Hasura metadata cleared" : "Hasura metadata already cleared"; return Logging.trace(msg); } catch (raw_exn){ var exn = Caml_js_exceptions.internalToOCamlException(raw_exn); return Logging.error({ msg: "EE806: There was an issue clearing metadata in hasura - indexing may still work - but you may have issues querying the data in hasura.", err: Utils.prettifyExn(exn) }); } } async function trackTables(endpoint, auth, pgSchema, tableNames) { try { var result = await Rest.$$fetch(trackTablesRoute, { auth: auth, args: { allow_warnings: false, tables: tableNames.map(function (tableName) { return { table: { name: tableName, schema: pgSchema }, configuration: { custom_name: tableName } }; }) } }, Rest.client(endpoint, undefined)); var msg; msg = result === "QuerySucceeded" ? "Hasura finished tracking tables" : "Hasura tables already tracked"; return Logging.trace({ msg: msg, tableNames: tableNames }); } catch (raw_exn){ var exn = Caml_js_exceptions.internalToOCamlException(raw_exn); return Logging.error({ msg: "EE807: There was an issue tracking tables in hasura - indexing may still work - but you may have issues querying the data in hasura.", tableNames: tableNames, err: Utils.prettifyExn(exn) }); } } function createSelectPermissionOperation(tableName, pgSchema, responseLimit, aggregateEntities) { return { type: "pg_create_select_permission", args: { table: { schema: pgSchema, name: tableName }, role: "public", source: "default", permission: { columns: "*", filter: {}, limit: responseLimit, allow_aggregations: aggregateEntities.includes(tableName) } } }; } function createEntityRelationshipOperation(pgSchema, tableName, relationshipType, relationalKey, objectName, mappedEntity, isDerivedFrom) { var derivedFromTo = isDerivedFrom ? "\"id\": \"" + relationalKey + "\"" : "\"" + relationalKey + "_id\" : \"id\""; return { type: "pg_create_" + relationshipType + "_relationship", args: { table: { schema: pgSchema, name: tableName }, name: objectName, source: "default", using: { manual_configuration: { remote_table: { schema: pgSchema, name: mappedEntity }, column_mapping: JSON.parse("{" + derivedFromTo + "}") } } } }; } async function executeBulkKeepGoing(endpoint, auth, operations) { if (operations.length === 0) { return Logging.trace("No hasura bulk configuration operations to execute"); } try { var result = await Rest.$$fetch(bulkKeepGoingRoute, { auth: auth, args: operations }, Rest.client(endpoint, undefined)); var errors; try { errors = S$RescriptSchema.parseJsonOrThrow(result, bulkKeepGoingErrorsSchema); } catch (raw_error){ var error = Caml_js_exceptions.internalToOCamlException(raw_error); errors = error.RE_EXN_ID === S$RescriptSchema.Raised ? [S$RescriptSchema.$$Error.message(error._1)] : [Utils.prettifyExn(error)]; } if (errors.length !== 0) { return Logging.warn({ msg: "Hasura configuration completed with errors. Indexing will still work - but you may have issues querying data via GraphQL.", errors: errors, operations: operations.length }); } else { return Logging.trace({ msg: "Hasura configuration completed", operations: operations.length }); } } catch (raw_exn){ var exn = Caml_js_exceptions.internalToOCamlException(raw_exn); return Logging.error({ msg: "EE809: There was an issue executing bulk operations in hasura - indexing may still work - but you may have issues querying the data in hasura.", operations: operations.length, err: Utils.prettifyExn(exn) }); } } async function trackDatabase(endpoint, auth, pgSchema, userEntities, aggregateEntities, responseLimit, schema) { var exposedInternalTableNames = [ InternalTable.RawEvents.table.tableName, InternalTable.Views.metaViewName, InternalTable.Views.chainMetadataViewName ]; var userTableNames = userEntities.map(function (entity) { return entity.table.tableName; }); var tableNames = Belt_Array.concatMany([ exposedInternalTableNames, userTableNames ]); Logging.info("Tracking tables in Hasura"); await clearHasuraMetadata(endpoint, auth); await trackTables(endpoint, auth, pgSchema, tableNames); var allOperations = []; tableNames.forEach(function (tableName) { allOperations.push(createSelectPermissionOperation(tableName, pgSchema, responseLimit, aggregateEntities)); }); userEntities.forEach(function (entityConfig) { var match = entityConfig.table; var tableName = match.tableName; Table.getDerivedFromFields(entityConfig.table).forEach(function (derivedFromField) { var relationalFieldName = Utils.unwrapResultExn(Schema.getDerivedFromFieldName(schema, derivedFromField)); allOperations.push(createEntityRelationshipOperation(pgSchema, tableName, "array", relationalFieldName, derivedFromField.fieldName, derivedFromField.derivedFromEntity, true)); }); Table.getLinkedEntityFields(entityConfig.table).forEach(function (param) { var field = param[0]; allOperations.push(createEntityRelationshipOperation(pgSchema, tableName, "object", field.fieldName, field.fieldName, param[1], false)); }); }); return await executeBulkKeepGoing(endpoint, auth, allOperations); } exports.auth = auth; exports.responses = responses; exports.clearMetadataRoute = clearMetadataRoute; exports.trackTablesRoute = trackTablesRoute; exports.createSelectPermissionRoute = createSelectPermissionRoute; exports.rawBodyRoute = rawBodyRoute; exports.bulkKeepGoingRoute = bulkKeepGoingRoute; exports.bulkKeepGoingErrorsSchema = bulkKeepGoingErrorsSchema; exports.clearHasuraMetadata = clearHasuraMetadata; exports.trackTables = trackTables; exports.createSelectPermissionOperation = createSelectPermissionOperation; exports.createEntityRelationshipOperation = createEntityRelationshipOperation; exports.executeBulkKeepGoing = executeBulkKeepGoing; exports.trackDatabase = trackDatabase; /* bulkKeepGoingErrorsSchema Not a pure module */