UNPKG

@quory/core

Version:

Quickly extract relationships from any database

202 lines (188 loc) 6.97 kB
type Row = Record<string, string | number | Date>; type GenericDataType = "binary" | "number" | "boolean" | "text" | "datetime" | "hierarchical" | "geometric" | "money" | "other"; type TableColumn = { name: string; tableName: string; schemaName: string; isNullable: boolean; dataType: string; genericDataType: GenericDataType; includedInPrimaryKey: boolean; }; type Relationship = { localSchema: string; localTable: string; localColumn: string; foreignSchema: string; foreignTable: string; foreignColumn: string; }; interface DatabaseDriver { getAllColumnsInDatabase(): Promise<TableColumn[]>; getAllForeignKeysInDatabase(): Promise<Relationship[]>; exec(sql: string): Promise<Row[]>; testConnection(): Promise<void>; } declare function getSchemas(databaseDriver: DatabaseDriver): Promise<DatabaseSchema[]>; declare enum ConditionOperator { AND = "and", OR = "or", LIKE = "like", EQUALS = "=", GREATER_THAN = ">", LESS_THAN = "<", GREATER_THAN_OR_EQUAL = ">=", LESS_THAN_OR_EQUAL = "<=", NOT_EQUALS = "<>", NOT_LIKE = "not like", NOT_IN = "not in", IN = "in" } type BooleanConditionOperator = Extract<ConditionOperator, ConditionOperator.AND | ConditionOperator.OR>; declare const booleanConditionOperators: ConditionOperator[]; declare const isBooleanCondition: (condition: Condition) => condition is BooleanCondition; type ValueConditionOperator = Exclude<ConditionOperator, ConditionOperator.AND | ConditionOperator.OR | ConditionOperator.IN | ConditionOperator.NOT_IN>; declare const valueConditionOperators: ConditionOperator[]; declare const isValueCondition: (condition: Condition) => condition is ValueCondition; type ListConditionOperator = Extract<ConditionOperator, ConditionOperator.IN | ConditionOperator.NOT_IN>; declare const listConditionOperators: ConditionOperator[]; declare const isListCondition: (condition: Condition) => condition is ListCondition; type BooleanCondition = { operator: BooleanConditionOperator; conditions: Condition[]; }; type ValueCondition = { column: string; operator: ValueConditionOperator; value: string; }; type ListCondition = { column: string; operator: ListConditionOperator; values: string[]; }; type Condition = BooleanCondition | ValueCondition | ListCondition; type JoinDef = { tableRef: string; via?: string[] | undefined; select: string[] | "*"; where?: Condition | undefined; joins?: JoinDef[] | undefined; orderBy?: { column: string; direction: "asc" | "desc"; priority?: number | undefined; }[] | undefined; }; interface Query { base: JoinDef; limit?: number | undefined; } type JoinDefWithAlias = Omit<JoinDef, "joins"> & { joinAlias: string; joins: JoinDefWithAlias[]; }; type JoinDefWithPath = JoinDefWithAlias & { pathFromBase: string[]; }; type FlattenedJoinDef = Omit<JoinDefWithPath, "joins">; type PreparedJoinDef = Omit<FlattenedJoinDef, "pathFromBase"> & { select: string[]; joins: PreparedJoinDef[]; }; interface PreparedQuery extends Query { base: PreparedJoinDef; } type FlattenedPreparedJoinDef = Omit<PreparedJoinDef, "childJoins"> & { parent: FlattenedPreparedJoinDef | null; childJoins: { joinAlias: string; tableRef: string; }[]; }; declare function runQuery(databaseDriver: DatabaseDriver, databaseSchemas: DatabaseSchema[], query: Query): Promise<{ sql: string; meta: { preparedQuery: PreparedQuery; joinsList: FlattenedPreparedJoinDef[]; }; rows: { joinAlias: string; tableRef: string; data: Row; }[][]; }>; declare function getCountForQuery(databaseDriver: DatabaseDriver, databaseSchemas: DatabaseSchema[], query: Omit<Query, "limit">): Promise<{ count: number; meta: { preparedQuery: PreparedQuery; }; }>; declare function getRelationsForTable(databaseSchemas: DatabaseSchema[], schemaName: string, tableName: string, maxJoins?: number): (DatabaseTableInfo & { schemaName: string; shortestJoinPath: number; })[]; declare function getEntitiesAndJunctions(schemaRelationships: DatabaseSchema[]): { junctions: string[]; entities: string[]; }; declare function findTableFromSchemas(schemas: DatabaseSchema[], schemaName: string, tableName: string): { schemaName: string; name: string; type?: "entity" | "junction"; columns: { name: string; dataType: string; genericDataType: GenericDataType; isNullable: boolean; includedInPrimaryKey: boolean; foreignKeys: { foreignSchemaName: string; foreignTableName: string; foreignColumnName: string; hasForeignKeyConstraint: boolean; confidence: number; }[]; foreignKeyReferences: { localSchemaName: string; localTableName: string; localColumnName: string; hasForeignKeyConstraint: boolean; confidence: number; }[]; }[]; }; declare function splitTableRef(tableRef: string): [string, string]; declare function isConditionComplete(condition: Condition): boolean; declare function parseToCompleteCondition(condition: Condition): Condition | null; declare function areQueriesEqual(schemas: DatabaseSchema[], queryA: Query, queryB: Query): boolean; type DatabaseTableInfo = { name: string; type?: "entity" | "junction"; columns: { name: string; dataType: string; genericDataType: GenericDataType; isNullable: boolean; includedInPrimaryKey: boolean; foreignKeys: { foreignSchemaName: string; foreignTableName: string; foreignColumnName: string; hasForeignKeyConstraint: boolean; confidence: number; }[]; foreignKeyReferences: { localSchemaName: string; localTableName: string; localColumnName: string; hasForeignKeyConstraint: boolean; confidence: number; }[]; }[]; }; type DatabaseSchema = { name: string; tables: DatabaseTableInfo[]; }; export { type BooleanCondition, type BooleanConditionOperator, type Condition, ConditionOperator, type DatabaseDriver, type DatabaseSchema, type DatabaseTableInfo, type GenericDataType, type JoinDef, type ListCondition, type ListConditionOperator, type PreparedJoinDef, type PreparedQuery, type Query, type Relationship, type Row, type TableColumn, type ValueCondition, type ValueConditionOperator, areQueriesEqual, booleanConditionOperators, findTableFromSchemas, getCountForQuery, getEntitiesAndJunctions, getRelationsForTable, getSchemas, isBooleanCondition, isConditionComplete, isListCondition, isValueCondition, listConditionOperators, parseToCompleteCondition, runQuery, splitTableRef, valueConditionOperators };