UNPKG

envio

Version:

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

444 lines (412 loc) • 12.4 kB
// Generated by ReScript, PLEASE EDIT WITH CARE import * as Utils from "../Utils.res.mjs"; import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js"; import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js"; import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js"; import * as Primitive_string from "@rescript/runtime/lib/es6/Primitive_string.js"; import * as S$RescriptSchema from "rescript-schema/src/S.res.mjs"; import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js"; function makeEnumConfig(name, variants) { return { name: name, variants: variants, schema: S$RescriptSchema.$$enum(variants) }; } function mkField(fieldName, fieldType, fieldSchema, $$default, isArrayOpt, isNullableOpt, isPrimaryKeyOpt, isIndexOpt, linkedEntity, description, postgresDbName, clickhouseDbName) { let isArray = isArrayOpt !== undefined ? isArrayOpt : false; let isNullable = isNullableOpt !== undefined ? isNullableOpt : false; let isPrimaryKey = isPrimaryKeyOpt !== undefined ? isPrimaryKeyOpt : false; let isIndex = isIndexOpt !== undefined ? isIndexOpt : false; return { TAG: "Field", _0: { fieldName: fieldName, fieldType: fieldType, fieldSchema: fieldSchema, isArray: isArray, isNullable: isNullable, isPrimaryKey: isPrimaryKey, isIndex: isIndex, linkedEntity: linkedEntity, defaultValue: $$default, description: description, postgresDbName: postgresDbName, clickhouseDbName: clickhouseDbName } }; } function mkDerivedFromField(fieldName, derivedFromEntity, derivedFromField, description) { return { TAG: "DerivedFrom", _0: { fieldName: fieldName, derivedFromEntity: derivedFromEntity, derivedFromField: derivedFromField, description: description } }; } function getUserDefinedFieldName(fieldOrDerived) { return fieldOrDerived._0.fieldName; } function isLinkedEntityField(field) { return Stdlib_Option.isSome(field.linkedEntity); } function getApiFieldName(field) { if (Stdlib_Option.isSome(field.linkedEntity)) { return field.fieldName + "_id"; } else { return field.fieldName; } } function getPgDbFieldName(field) { let dbName = field.postgresDbName; if (dbName !== undefined) { return dbName; } else { return getApiFieldName(field); } } function getClickHouseDbFieldName(field) { let dbName = field.clickhouseDbName; if (dbName !== undefined) { return dbName; } else { return getApiFieldName(field); } } function getPgFieldName(fieldOrDerived) { if (fieldOrDerived.TAG === "Field") { return getPgDbFieldName(fieldOrDerived._0); } else { return fieldOrDerived._0.fieldName; } } function getPgFieldType(fieldType, pgSchema, isArray, isNumericArrayAsText, isNullable) { let columnType; if (typeof fieldType !== "object") { switch (fieldType) { case "String" : columnType = "TEXT"; break; case "Boolean" : columnType = "BOOLEAN"; break; case "Int32" : columnType = "INTEGER"; break; case "Number" : columnType = "DOUBLE PRECISION"; break; case "Serial" : columnType = "SERIAL"; break; case "BigSerial" : columnType = "BIGSERIAL"; break; case "Json" : columnType = "JSONB"; break; case "Date" : columnType = isNullable ? "TIMESTAMP WITH TIME ZONE NULL" : "TIMESTAMP WITH TIME ZONE"; break; default: columnType = "BIGINT"; } } else { switch (fieldType.type) { case "BigInt" : let precision = fieldType.precision; columnType = "NUMERIC" + ( precision !== undefined ? `(` + precision.toString() + `, 0)` : "" ); break; case "BigDecimal" : let config = fieldType.config; columnType = "NUMERIC" + ( config !== undefined ? `(` + config[0].toString() + `, ` + config[1].toString() + `)` : "" ); break; case "Enum" : columnType = `"` + pgSchema + `".` + fieldType.config.name; break; case "Entity" : columnType = "TEXT"; break; } } let isNumericAsText = isArray && isNumericArrayAsText; let columnType$1 = columnType === "NUMERIC" && isNumericAsText ? "TEXT" : columnType; return columnType$1 + ( isArray ? "[]" : "" ); } function mkTable(tableName, compositeIndicesOpt, fields, description) { let compositeIndices = compositeIndicesOpt !== undefined ? compositeIndicesOpt : []; return { tableName: tableName, fields: fields, compositeIndices: compositeIndices, description: description }; } function getPgPrimaryKeyFieldNames(table) { return Stdlib_Array.filterMap(table.fields, field => { if (field.TAG !== "Field") { return; } let field$1 = field._0; if (field$1.isPrimaryKey) { return getPgDbFieldName(field$1); } }); } function getFields(table) { return Stdlib_Array.filterMap(table.fields, field => { if (field.TAG === "Field") { return field._0; } }); } function getNonDefaultFields(table) { return Stdlib_Array.filterMap(table.fields, field => { if (field.TAG !== "Field") { return; } let field$1 = field._0; if (Stdlib_Option.isNone(field$1.defaultValue)) { return field$1; } }); } function getLinkedEntityFields(table) { return Stdlib_Array.filterMap(table.fields, field => { if (field.TAG !== "Field") { return; } let field$1 = field._0; let linkedEntityName = field$1.linkedEntity; if (linkedEntityName !== undefined) { return [ field$1, linkedEntityName ]; } }); } function getDerivedFromFields(table) { return Stdlib_Array.filterMap(table.fields, field => { if (field.TAG === "Field") { return; } else { return field._0; } }); } function getFieldByName(table, fieldName) { return table.fields.find(field => getUserDefinedFieldName(field) === fieldName); } function getFieldByApiName(table, apiFieldName) { return table.fields.find(field => { let tmp; tmp = field.TAG === "Field" ? getApiFieldName(field._0) : field._0.fieldName; return tmp === apiFieldName; }); } let queryFields = Utils.$$WeakMap.memoize(table => { let dict = {}; table.fields.forEach(field => { if (field.TAG !== "Field") { return; } let field$1 = field._0; dict[getApiFieldName(field$1)] = { fieldSchema: field$1.fieldSchema, arrayFieldSchema: S$RescriptSchema.array(field$1.fieldSchema), pgDbFieldName: getPgDbFieldName(field$1) }; }); return dict; }); function makeRowsSchema(table, rowFieldName) { return S$RescriptSchema.array(S$RescriptSchema.object(s => { let dict = {}; table.fields.forEach(field => { if (field.TAG !== "Field") { return; } let field$1 = field._0; dict[getApiFieldName(field$1)] = s.f(rowFieldName(field$1), field$1.fieldSchema); }); return dict; })); } let rowsSchema = Utils.$$WeakMap.memoize(table => makeRowsSchema(table, getApiFieldName)); let pgRowsSchema = Utils.$$WeakMap.memoize(table => makeRowsSchema(table, getPgDbFieldName)); let NonExistingTableField = /* @__PURE__ */Primitive_exceptions.create("Table.NonExistingTableField"); function getUnfilteredCompositeIndicesUnsafe(table) { return table.compositeIndices.map(compositeIndex => compositeIndex.map(indexField => { let field = getFieldByName(table, indexField.fieldName); let dbFieldName; if (field !== undefined) { dbFieldName = getPgFieldName(field); } else { throw { RE_EXN_ID: NonExistingTableField, _1: indexField.fieldName, Error: new Error() }; } return { fieldName: dbFieldName, direction: indexField.direction }; })); } function toSqlParams(table, schema, pgSchema) { let quotedFieldNames = []; let quotedNonPrimaryFieldNames = []; let arrayFieldTypes = []; let hasArrayField = { contents: false }; let dbSchema = S$RescriptSchema.schema(s => { let match = schema.t; if (typeof match !== "object") { return Stdlib_JsError.throwWithMessage("Failed creating db schema. Expected an object schema for table"); } if (match.TAG !== "object") { return Stdlib_JsError.throwWithMessage("Failed creating db schema. Expected an object schema for table"); } let dict = {}; match.items.forEach(param => { let location = param.location; let coerceSchema = schema => { let child = schema.t; if (typeof child !== "object") { switch (child) { case "bigint" : return Utils.$$BigInt.schema; case "boolean" : return S$RescriptSchema.union([ S$RescriptSchema.shape(S$RescriptSchema.literal(1), param => true), S$RescriptSchema.shape(S$RescriptSchema.literal(0), param => false) ]); default: return schema; } } else { switch (child.TAG) { case "option" : case "null" : return S$RescriptSchema.$$null(coerceSchema(child._0)); case "array" : hasArrayField.contents = true; return S$RescriptSchema.array(coerceSchema(child._0)); case "JSON" : hasArrayField.contents = true; return schema; default: return schema; } } }; let field = getFieldByApiName(table, location); let field$1; if (field !== undefined) { field$1 = field; } else { throw { RE_EXN_ID: NonExistingTableField, _1: location, Error: new Error() }; } let quotedDbName = `"` + getPgFieldName(field$1) + `"`; quotedFieldNames.push(quotedDbName); if (field$1.TAG === "Field") { if (field$1._0.isPrimaryKey) { } else { quotedNonPrimaryFieldNames.push(quotedDbName); } } let tmp; if (field$1.TAG === "Field") { let f = field$1._0; let pgFieldType = getPgFieldType(f.fieldType, pgSchema, true, false, f.isNullable); let match = f.fieldType; tmp = typeof match !== "object" ? ( match === "Boolean" ? "INTEGER" + `[]::` + pgFieldType : pgFieldType ) : ( match.type === "Enum" ? "TEXT" + `[]::` + pgFieldType : pgFieldType ); } else { tmp = "TEXT[]"; } arrayFieldTypes.push(tmp); dict[location] = s.m(coerceSchema(param.schema)); }); return dict; }); return { dbSchema: dbSchema, quotedFieldNames: quotedFieldNames, quotedNonPrimaryFieldNames: quotedNonPrimaryFieldNames, arrayFieldTypes: arrayFieldTypes, hasArrayField: hasArrayField.contents }; } function getSingleIndices(table) { let indexFields = Stdlib_Array.filterMap(table.fields, field => { if (field.TAG !== "Field") { return; } let field$1 = field._0; if (field$1.isIndex) { return getPgDbFieldName(field$1); } }); return Array.from(new Set(Stdlib_Array.filterMap(getUnfilteredCompositeIndicesUnsafe(table), cidx => { if (cidx.length !== 1) { return; } let match = cidx[0]; return [match.fieldName]; }).concat([indexFields]).flat())).toSorted(Primitive_string.compare); } function getCompositeIndices(table) { return getUnfilteredCompositeIndicesUnsafe(table).filter(ind => ind.length > 1); } let idFieldName = "id"; export { makeEnumConfig, mkField, mkDerivedFromField, getUserDefinedFieldName, isLinkedEntityField, getApiFieldName, getPgDbFieldName, getClickHouseDbFieldName, getPgFieldName, idFieldName, getPgFieldType, mkTable, getPgPrimaryKeyFieldNames, getFields, getNonDefaultFields, getLinkedEntityFields, getDerivedFromFields, getFieldByName, getFieldByApiName, queryFields, makeRowsSchema, rowsSchema, pgRowsSchema, NonExistingTableField, getUnfilteredCompositeIndicesUnsafe, toSqlParams, getSingleIndices, getCompositeIndices, } /* queryFields Not a pure module */