UNPKG

@riao/dbal

Version:
160 lines 6.45 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SchemaQueryRepository = void 0; const dml_1 = require("../dml"); /** * Use the Schema Query Repository to query your schema information */ class SchemaQueryRepository extends dml_1.QueryRepository { constructor(options) { var _a; super(options); this.tablesTable = 'information_schema.tables'; this.columnsTable = 'information_schema.columns'; this.databaseNameColumn = 'TABLE_SCHEMA'; this.tableNameColumn = 'TABLE_NAME'; this.tableTypeColumn = 'TABLE_TYPE'; this.columnNameColumn = 'COLUMN_NAME'; this.columnTypeColumn = 'DATA_TYPE'; this.columnPositionColumn = 'ORDINAL_POSITION'; this.returnedTableTypes = { 'BASE TABLE': 'table', VIEW: 'view', }; this.queryBuilderType = (_a = options.queryBuilderType) !== null && _a !== void 0 ? _a : this.queryBuilderType; } init(options) { super.init(options); this.database = options.database; } getSchema() { return __awaiter(this, void 0, void 0, function* () { const tables = yield this.getTablesWithColumns(); const mappedTables = {}; for (const table of tables) { mappedTables[table.name] = table; } return { tables: mappedTables, }; }); } getTablesQueryWhere() { return { [this.databaseNameColumn]: this.database }; } getTablesQuery() { return __awaiter(this, void 0, void 0, function* () { return yield this.find({ table: this.tablesTable, columns: [this.tableNameColumn, this.tableTypeColumn], where: this.getTablesQueryWhere(), }); }); } getTablesResultParser(tables) { return tables.map((table) => ({ name: table[this.tableNameColumn], type: this.returnedTableTypes[table[this.tableTypeColumn]], })); } getTables() { return __awaiter(this, void 0, void 0, function* () { const results = yield this.getTablesQuery(); return this.getTablesResultParser(results); }); } getTablesWithColumns() { return __awaiter(this, void 0, void 0, function* () { const tables = yield this.getTables(); const out = []; for (const table of tables) { const tableName = table.name; const primaryKey = yield this.getPrimaryKeyName({ table: tableName, }); const columns = yield this.getColumns({ table: tableName, primaryKey, }); const mappedColumns = {}; for (const column of columns) { mappedColumns[column.name] = column; } out.push({ name: tableName, type: table.type, columns: mappedColumns, primaryKey, }); } return out; }); } getPrimaryKeyResultParser(result) { return result === null || result === void 0 ? void 0 : result[this.columnNameColumn]; } getPrimaryKeyQuery(table) { return __awaiter(this, void 0, void 0, function* () { return yield this.findOne({ table: this.columnsTable, columns: [this.columnNameColumn], where: { [this.databaseNameColumn]: this.database, [this.tableNameColumn]: table, COLUMN_KEY: 'PRI', }, }); }); } getPrimaryKeyName(options) { return __awaiter(this, void 0, void 0, function* () { const result = yield this.getPrimaryKeyQuery(options.table); return this.getPrimaryKeyResultParser(result !== null && result !== void 0 ? result : undefined); }); } getColumnsQuery(table) { return __awaiter(this, void 0, void 0, function* () { return yield this.find({ table: this.columnsTable, columns: [this.columnNameColumn, this.columnTypeColumn], where: { [this.databaseNameColumn]: this.database, [this.tableNameColumn]: table, }, orderBy: { [this.columnPositionColumn]: 'ASC' }, }); }); } getColumnsResultParser(options) { return options.records.map((column) => ({ name: column[this.columnNameColumn], type: column[this.columnTypeColumn] .toUpperCase() .replace('CHARACTER VARYING', 'VARCHAR'), primaryKey: column[this.columnNameColumn] === options.primaryKey, })); } getColumns(options) { var _a; return __awaiter(this, void 0, void 0, function* () { const primaryKey = (_a = options.primaryKey) !== null && _a !== void 0 ? _a : (yield this.getPrimaryKeyName({ table: options.table })); const results = yield this.getColumnsQuery(options.table); return this.getColumnsResultParser({ records: results, primaryKey: primaryKey, }); }); } } exports.SchemaQueryRepository = SchemaQueryRepository; //# sourceMappingURL=schema-query-repository.js.map