Skip to content
Snippets Groups Projects
Database.ts 4.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-nocheck
    
    import { Database as IDatabase, NativePermissions } from "metabase-types/api";
    
    import { generateSchemaId } from "metabase-lib/metadata/utils/schema";
    import { createLookupByProperty, memoizeClass } from "metabase-lib/utils";
    
    import Question from "../Question";
    import Base from "./Base";
    
    import Table from "./Table";
    import Schema from "./Schema";
    import Metadata from "./Metadata";
    
     * @typedef { import("./Metadata").SchemaName } SchemaName
    
    /**
     * Wrapper class for database metadata objects. Contains {@link Schema}s, {@link Table}s, {@link Metric}s, {@link Segment}s.
    
     *
     * Backed by types/Database data structure which matches the backend API contract
    
    class DatabaseInner extends Base {
    
      id: number;
      name: string;
    
      description: string;
      tables: Table[];
      schemas: Schema[];
      metadata: Metadata;
    
      native_permissions: NativePermissions;
    
      // Only appears in  GET /api/database/:id
      "can-manage"?: boolean;
    
    
      getPlainObject(): IDatabase {
        return this._plainObject;
      }
    
    
      // TODO Atte Keinänen 6/11/17: List all fields here (currently only in types/Database)
    
    Tom Robinson's avatar
    Tom Robinson committed
        return this.name;
      }
    
    
      // SCHEMAS
    
      /**
       * @param {SchemaName} [schemaName]
       */
      schema(schemaName) {
    
        return this.metadata.schema(generateSchemaId(this.id, schemaName));
    
        return this.schemas.map(s => s.name).sort((a, b) => a.localeCompare(b));
    
      getSchemas() {
        return this.schemas;
      }
    
      schemasCount() {
        return this.schemas.length;
      }
    
      getTables() {
        return this.tables;
      }
    
    
      // TABLES
      tablesLookup() {
        return createLookupByProperty(this.tables, "id");
      }
    
      // @deprecated: use tablesLookup
      get tables_lookup() {
        return this.tablesLookup();
      }
    
      // FEATURES
    
    
       * @typedef {import("./Metadata").DatabaseFeature} DatabaseFeature
    
       * @typedef {"join"} VirtualDatabaseFeature
       * @param {DatabaseFeature | VirtualDatabaseFeature} [feature]
       */
      hasFeature(feature) {
    
        if (!feature) {
          return true;
        }
    
    Tom Robinson's avatar
    Tom Robinson committed
        const set = new Set(this.features);
    
    Tom Robinson's avatar
    Tom Robinson committed
        if (feature === "join") {
          return (
            set.has("left-join") ||
            set.has("right-join") ||
            set.has("inner-join") ||
            set.has("full-join")
          );
        } else {
          return set.has(feature);
        }
      }
    
    
      supportsPivots() {
        return this.hasFeature("expressions") && this.hasFeature("left-join");
      }
    
    
      supportsExpressions() {
        return this.hasFeature("expressions");
      }
    
    
      isPersisted() {
        return this.hasFeature("persist-models-enabled");
      }
    
      supportsPersistence() {
        return this.hasFeature("persist-models");
      }
    
    
        return this.question().setDefaultQuery().setDefaultDisplay();
    
      question(
        query = {
          "source-table": null,
        },
      ) {
    
        return Question.create({
          metadata: this.metadata,
    
          dataset_query: {
            database: this.id,
            type: "query",
            query: query,
          },
    
      nativeQuestion(native = {}) {
    
        return Question.create({
          metadata: this.metadata,
    
          dataset_query: {
            database: this.id,
            type: "native",
            native: {
              query: "",
              "template-tags": {},
              ...native,
            },
          },
    
      nativeQuery(native) {
        return this.nativeQuestion(native).query();
    
    
      /** Returns a database containing only the saved questions from the same database, if any */
    
        return this.metadata.databasesList().find(db => db.is_saved_questions);
    
    
      /**
       * @private
       * @param {number} id
       * @param {string} name
       * @param {?string} description
       * @param {Table[]} tables
       * @param {Schema[]} schemas
       * @param {Metadata} metadata
       * @param {boolean} auto_run_queries
       */
    
      _constructor(
        id,
        name,
        description,
        tables,
        schemas,
        metadata,
        auto_run_queries,
      ) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.tables = tables;
        this.schemas = schemas;
        this.metadata = metadata;
        this.auto_run_queries = auto_run_queries;
      }
    
    
    export default class Database extends memoizeClass<DatabaseInner>(
      "tablesLookup",
    )(DatabaseInner) {}