Files
discourse-data-explorer/assets/javascripts/discourse/components/explorer-schema.js
T

139 lines
3.3 KiB
JavaScript
Raw Normal View History

2022-12-16 11:51:36 -06:00
import Component from "@glimmer/component";
import { debounce } from "discourse-common/utils/decorators";
import { isBlank, isEmpty } from "@ember/utils";
2022-12-16 11:51:36 -06:00
import { action } from "@ember/object";
import { tracked } from "@glimmer/tracking";
2016-02-17 17:17:43 -05:00
2022-12-16 11:51:36 -06:00
export default class ExplorerSchema extends Component {
@tracked filter;
@tracked loading;
@tracked hideSchema = this.args.hideSchema;
2015-07-14 09:44:42 -07:00
2022-12-16 11:51:36 -06:00
get transformedSchema() {
const schema = this.args.schema;
for (const key in schema) {
2015-07-08 13:45:13 -07:00
if (!schema.hasOwnProperty(key)) {
continue;
}
2020-09-04 13:23:11 +02:00
schema[key].forEach((col) => {
2015-07-28 11:18:22 -07:00
const notes_components = [];
if (col.primary) {
notes_components.push("primary key");
}
2015-07-08 13:45:13 -07:00
if (col.is_nullable) {
2015-07-28 11:18:22 -07:00
notes_components.push("null");
2015-07-08 13:45:13 -07:00
}
if (col.column_default) {
2015-07-28 11:18:22 -07:00
notes_components.push("default " + col.column_default);
2015-07-08 13:45:13 -07:00
}
if (col.fkey_info) {
2015-07-28 11:18:22 -07:00
notes_components.push("fkey " + col.fkey_info);
2015-07-08 13:45:13 -07:00
}
2015-07-28 11:18:22 -07:00
if (col.denormal) {
notes_components.push("denormal " + col.denormal);
}
const notes = notes_components.join(", ");
2015-07-09 15:46:19 -07:00
if (notes) {
col.notes = notes;
2015-07-09 15:46:19 -07:00
}
if (col.enum || col.column_desc) {
col.havepopup = true;
}
col.havetypeinfo = !!(col.notes || col.enum || col.column_desc);
2015-07-08 13:45:13 -07:00
});
}
return schema;
2022-12-16 11:51:36 -06:00
}
2015-07-08 13:45:13 -07:00
2022-12-16 11:51:36 -06:00
get filteredTables() {
2015-07-08 13:45:13 -07:00
let tables = [];
2022-12-16 11:51:36 -06:00
let filter = this.filter;
2015-07-08 13:45:13 -07:00
2022-12-16 11:51:36 -06:00
try {
if (!isBlank(this.filter)) {
filter = new RegExp(this.filter);
}
} catch {
filter = null;
}
const haveFilter = !!filter;
for (const key in this.transformedSchema) {
if (!this.transformedSchema.hasOwnProperty(key)) {
2015-07-08 13:45:13 -07:00
continue;
}
if (!haveFilter) {
tables.push({
name: key,
2022-12-16 11:51:36 -06:00
columns: this.transformedSchema[key],
2020-09-04 13:23:11 +02:00
open: false,
2015-07-08 13:45:13 -07:00
});
continue;
}
// Check the table name vs the filter
2017-08-02 17:19:38 +09:00
if (filter.source === key || filter.source + "s" === key) {
2015-07-08 13:45:13 -07:00
tables.unshift({
name: key,
2022-12-16 11:51:36 -06:00
columns: this.transformedSchema[key],
2020-09-04 13:23:11 +02:00
open: haveFilter,
2015-07-08 13:45:13 -07:00
});
} else if (filter.test(key)) {
// whole table matches
tables.push({
name: key,
2022-12-16 11:51:36 -06:00
columns: this.transformedSchema[key],
2020-09-04 13:23:11 +02:00
open: haveFilter,
2015-07-08 13:45:13 -07:00
});
} else {
// filter the columns
let filterCols = [];
2022-12-16 11:51:36 -06:00
this.transformedSchema[key].forEach((col) => {
2017-08-02 17:19:38 +09:00
if (filter.source === col.column_name) {
2015-07-08 13:45:13 -07:00
filterCols.unshift(col);
} else if (filter.test(col.column_name)) {
filterCols.push(col);
}
});
if (!isEmpty(filterCols)) {
2015-07-08 13:45:13 -07:00
tables.push({
name: key,
columns: filterCols,
2020-09-04 13:23:11 +02:00
open: haveFilter,
2015-07-08 13:45:13 -07:00
});
}
}
}
return tables;
2022-12-16 11:51:36 -06:00
}
2015-07-08 13:45:13 -07:00
2022-12-16 11:51:36 -06:00
@debounce(500)
updateFilter(value) {
this.filter = value;
this.loading = false;
}
2015-07-08 13:45:13 -07:00
2022-12-16 11:51:36 -06:00
@action
filterChanged(value) {
this.loading = true;
this.updateFilter(value);
}
2015-07-08 13:45:13 -07:00
2022-12-16 11:51:36 -06:00
@action
collapseSchema() {
this.hideSchema = true;
this.args.updateHideSchema(true);
}
2022-12-16 11:51:36 -06:00
@action
expandSchema() {
this.hideSchema = false;
this.args.updateHideSchema(false);
}
}