UNPKG

jsonschema2ddl

Version:
123 lines (122 loc) 4.38 kB
import { Table } from './models'; interface JSONSchemaToDatabaseParams { database_flavor?: string; db_schema_name?: string | undefined; root_table_name?: string; log_level?: string | undefined; abbreviations?: any; extra_columns?: []; } interface CreateTablesParams { drop_schema?: boolean; drop_tables?: boolean; drop_cascade?: boolean; auto_commit?: boolean; } interface CreateLinksParams { auto_commit?: boolean; } /** * JSONSchemaToDatabase is the base. * * Typically you want to instantiate a `JSONSchemaToPostgres` object and * run :func:`create_tables` to create all the tables. Run :func:`create_links` * to populate all references properly and add foreign keys between tables. * Optionally, you can run :func:`analyze` finally which optimizes the tables. * * Attributes: * schema (Dict): the schema to translate to tables. * database_flavor (str): the flavor of the db. One of Postgres or Redshift. * db_schema_name (str): the name of the schema in the database to create the tables. * root_table_name (str): Name of the root table for the schema. * abbreviations (Dict): Dictionary of abbreviations for columns. * extra_columns (List[Dict]): List of extra columns. * log_level (str): Log level of the deployment. Default 'DEBUG'. */ export declare class JSONSchemaToDatabase { schema: any; database_flavor: string; db_schema_name: string | undefined; abbreviations: any; extra_columns: []; root_table_name?: string; log_level: string | undefined; table_definitions: Record<string, Table>; constructor(schema: any, { database_flavor, db_schema_name, root_table_name, log_level, abbreviations, extra_columns, }?: JSONSchemaToDatabaseParams); /** * Validates the jsonschema itself against the `$schema` url. * Currently, some redirections are not supported. * * Raises: * jsonschema.ValidationError: Schema is invalid */ private _validate_schema; /** * Creates the table definitions. * * Returns: * Dict[str, Table]: A dictionary with tables ids and the tables objects to create. */ private _create_table_definitions; /** * Helper method to execute and debug a query. * * Args: * cursor (psycopg2.cursor): Cursor object of the db connection. * query (str): query to execute. * args (List, optional): List of arguments for the execute command. Defaults to None. * query_ok_to_print (bool, optional): Defaults to True. * * @param conn * @param query * @param args * @param query_ok_to_print */ private _execute; /** * Create the tables for the schema * * Args: * conn (psocopg2.connection): Connection object to the db. * drop_schema (bool, optional): Whether or not drop the schema if exists. * Defaults to False. * drop_tables (bool, optional): Whether or not drop the tables if exists. * Defaults to False. * drop_cascade (bool, optional): Execute drops with cascade. Defaults to True. * auto_commit (bool, optional): autocomit after finishing. Defaults to False. * * @param conn * @param drop_schema * @param drop_tables * @param drop_cascade * @param auto_commit */ create_tables(conn: any, { drop_schema, drop_tables, drop_cascade, auto_commit }?: CreateTablesParams): Promise<void>; generate_ddl(include_comments?: boolean): Promise<string>; /** * Adds foreign keys between tables. * * Args: * conn(psocopg2.connection): connection object. * auto_commit(bool, Optional): Defaults to False. */ create_links(conn: any, { auto_commit }?: CreateLinksParams): Promise<void>; /** * Runs `analyze` on each table. This improves performance. * See the `Postgres documentation for Analyze * <https://www.postgresql.org/docs/9.1/static/sql-analyze.html>`_ * * Args: * conn(psocopg2.connection): connection object. * * @param conn */ analyze(conn: any): Promise<void>; } /** * Shorthand for JSONSchemaToDatabase(..., database_flavor='redshift') */ export declare class JSONSchemaToRedshift extends JSONSchemaToDatabase { constructor(schema: any, params?: JSONSchemaToDatabaseParams); } export {};