Skip to content
Snippets Groups Projects
Unverified Commit 03b8fe77 authored by Alexander Polyankin's avatar Alexander Polyankin Committed by GitHub
Browse files

Cleanup `Schema` entity - fix types, get rid off Base (#30694)

parent fe501169
No related branches found
No related tags found
No related merge requests found
// eslint-disable-next-line @typescript-eslint/ban-ts-comment import { humanize, titleize } from "metabase/lib/formatting";
// @ts-nocheck import { NormalizedSchema } from "metabase-types/api";
import { titleize, humanize } from "metabase/lib/formatting";
import Base from "./Base";
import type Metadata from "./Metadata"; import type Metadata from "./Metadata";
import type Database from "./Database"; import type Database from "./Database";
import type Table from "./Table"; import type Table from "./Table";
/**
* Wrapper class for a {@link Database} schema. Contains {@link Table}s.
*/
export default class Schema extends Base { export default class Schema {
id: string; private readonly schema: NormalizedSchema;
name: string; metadata?: Metadata;
database: Database; database?: Database;
tables: Table[]; tables: Table[] = [];
metadata: Metadata;
constructor(schema: NormalizedSchema) {
this.schema = schema;
}
get id() {
return this.schema.id;
}
get name() {
return this.schema.name;
}
getPlainObject() {
return this.schema;
}
displayName() { displayName() {
return this.name ? titleize(humanize(this.name)) : null; return this.name ? titleize(humanize(this.name)) : null;
...@@ -23,18 +33,4 @@ export default class Schema extends Base { ...@@ -23,18 +33,4 @@ export default class Schema extends Base {
getTables() { getTables() {
return this.tables; return this.tables;
} }
/**
* @private
* @param {string} name
* @param {Database} database
* @param {Table[]} tables
*/
/* istanbul ignore next */
_constructor(name, database, tables) {
this.name = name;
this.database = database;
this.tables = tables;
}
} }
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import Schema from "./Schema"; import Schema from "./Schema";
import Base from "./Base";
describe("Schema", () => { describe("Schema", () => {
describe("instantiation", () => { describe("instantiation", () => {
it("should create an instance of Schema", () => { it("should create an instance of Schema", () => {
expect(new Schema()).toBeInstanceOf(Schema); expect(new Schema({ id: "1:public", name: "public" })).toBeInstanceOf(
Schema,
);
}); });
it("should add `object` props to the instance (because it extends Base)", () => { it("should add `object` props to the instance", () => {
expect(new Schema()).toBeInstanceOf(Base);
expect( expect(
new Schema({ new Schema({
foo: "bar", id: "1:public",
name: "public",
}), }),
).toHaveProperty("foo", "bar"); ).toHaveProperty("name", "public");
}); });
}); });
describe("displayName", () => { describe("displayName", () => {
it("should return a formatted `name` string", () => { it("should return a formatted `name` string", () => {
const schema = new Schema({ const schema = new Schema({
id: "name: public",
name: "foo_bar", name: "foo_bar",
}); });
expect(schema.displayName()).toBe("Foo Bar"); expect(schema.displayName()).toBe("Foo Bar");
......
import { checkNotNull } from "metabase/core/utils/types";
import type { ConcreteTableId } from "metabase-types/api"; import type { ConcreteTableId } from "metabase-types/api";
import type Database from "metabase-lib/metadata/Database"; import type Database from "metabase-lib/metadata/Database";
import type Schema from "metabase-lib/metadata/Schema"; import type Schema from "metabase-lib/metadata/Schema";
...@@ -9,7 +10,7 @@ export const getDatabaseEntityId = (databaseEntity: Database) => ({ ...@@ -9,7 +10,7 @@ export const getDatabaseEntityId = (databaseEntity: Database) => ({
}); });
export const getSchemaEntityId = (schemaEntity: Schema) => ({ export const getSchemaEntityId = (schemaEntity: Schema) => ({
databaseId: schemaEntity.database.id, databaseId: checkNotNull(schemaEntity.database).id,
schemaName: schemaEntity.name, schemaName: schemaEntity.name,
}); });
......
...@@ -107,7 +107,7 @@ const DataSelectorDatabaseSchemaPicker = ({ ...@@ -107,7 +107,7 @@ const DataSelectorDatabaseSchemaPicker = ({
} }
let openSection = selectedSchema let openSection = selectedSchema
? databases.findIndex(db => db.id === selectedSchema.database.id) ? databases.findIndex(db => db.id === selectedSchema.database?.id)
: selectedDatabase : selectedDatabase
? databases.findIndex(db => db.id === selectedDatabase.id) ? databases.findIndex(db => db.id === selectedDatabase.id)
: -1; : -1;
......
...@@ -127,10 +127,8 @@ export const getMetadata: ( ...@@ -127,10 +127,8 @@ export const getMetadata: (
); );
}); });
// schema // schema
hydrate( hydrate(metadata.schemas, "database", schema =>
metadata.schemas, metadata.database(schema.getPlainObject().database),
"database",
schema => metadata.database(schema.database) as Database,
); );
// table // table
...@@ -151,10 +149,11 @@ export const getMetadata: ( ...@@ -151,10 +149,11 @@ export const getMetadata: (
); );
}); });
hydrate(metadata.schemas, "tables", schema => hydrate(metadata.schemas, "tables", schema => {
schema.tables const tableIds = schema.getPlainObject().tables;
return tableIds
? // use the schema tables if they exist ? // use the schema tables if they exist
schema.tables.map(table => metadata.table(table)) tableIds.map(table => metadata.table(table))
: schema.database && schema.database.tables.length > 0 : schema.database && schema.database.tables.length > 0
? // if the schema has a database with tables, use those ? // if the schema has a database with tables, use those
schema.database.tables.filter( schema.database.tables.filter(
...@@ -163,8 +162,8 @@ export const getMetadata: ( ...@@ -163,8 +162,8 @@ export const getMetadata: (
: // otherwise use any loaded tables that match the schema id : // otherwise use any loaded tables that match the schema id
Object.values(metadata.tables).filter( Object.values(metadata.tables).filter(
table => table.schema && table.schema.id === schema.id, table => table.schema && table.schema.id === schema.id,
), );
); });
// segments // segments
hydrate( hydrate(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment