@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
208 lines (207 loc) • 8.51 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var SchemaBuilder_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaBuilder = void 0;
const config_1 = require("../config");
const constants_1 = require("../constants");
const tables_1 = require("../core/tables");
const database_1 = __importDefault(require("../database"));
const component_1 = require("../decorators/component");
const proxy_1 = require("./proxy");
const schema = __importStar(require("./schema"));
let SchemaBuilder = class SchemaBuilder {
static { SchemaBuilder_1 = this; }
database;
config;
tables;
static schema;
constructor(database, config, tables) {
this.database = database;
this.config = config;
this.tables = tables;
}
getDefinition(name) {
return SchemaBuilder_1.schema[name];
}
static add(schema) {
SchemaBuilder_1.schema = { ...SchemaBuilder_1.schema, ...schema };
}
async tableExists(tableName) {
return await this.database.schema.hasTable(this.tables.get(tableName));
}
async getColumnNamesFromExistingTable(tableName) {
const columnInfo = await this.database
.connection(this.tables.get(tableName))
.columnInfo();
return Object.keys(columnInfo);
}
getColumnNamesFromSchemaDefinition(tableName) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [proxy, calls] = (0, proxy_1.createFluentProxy)();
// Get keys and save chaining methods defined for columns
const columns = this.getDefinition(tableName).columns(proxy, this.config);
const keys = [];
Object.entries(columns).forEach(([key, value]) => value && keys.push(key));
return keys;
}
buildColumns(tableName, tableBuilder, targetColumns) {
const [proxy, calls] = (0, proxy_1.createFluentProxy)();
// Get keys and save chaining methods defined for columns
this.getDefinition(tableName).columns(proxy, this.config);
let include = true;
let currentColumBuilder = {
name: "",
instance: undefined,
};
let currentColumnName = "";
for (const { method, args } of calls) {
// Method is to initialize Knex.ColumnBuilder
if (typeof tableBuilder[method] !== "undefined") {
currentColumnName = args[0];
// Check if the column should be added
include = targetColumns.includes(currentColumnName);
}
if (include) {
// Initialize new ColumnBuilder
if (currentColumnName !== currentColumBuilder.name) {
currentColumBuilder = {
name: currentColumnName,
// Initialize new Knex.ColumnBuilder
instance: tableBuilder[method](...args),
};
}
else {
// Call method for ColumnBuilder
currentColumBuilder.instance[method](...args);
}
}
}
}
async get(tableName) {
const columnNamesFromSchemaDefinition = this.getColumnNamesFromSchemaDefinition(tableName);
let targetColumns = columnNamesFromSchemaDefinition;
const builders = [];
let builder;
// Columns
if (await this.tableExists(tableName)) {
const columnNamesfromExistingTable = await this.getColumnNamesFromExistingTable(tableName);
targetColumns = columnNamesFromSchemaDefinition.filter((key) => !columnNamesfromExistingTable.includes(key));
if (0 >= targetColumns.length) {
return [];
}
// Alter Table
builder = this.database.schema.alterTable(this.tables.get(tableName), (table) => {
this.buildColumns(tableName, table, targetColumns);
});
}
else {
// Create Table
builder = this.database.schema.createTable(this.tables.get(tableName), (table) => {
this.buildColumns(tableName, table, targetColumns);
table.collate(this.config.config.tableCollate);
table.charset(this.config.config.tableCharset);
});
}
builders.push(builder);
const definition = this.getDefinition(tableName);
// Indexes
if (definition.indexes) {
const indexes = definition.indexes;
builder = this.database.schema.alterTable(this.tables.get(tableName), (table) => {
indexes(table, targetColumns);
});
builders.push(builder);
}
// Raw queries
if (definition.raw) {
const raw = definition.raw;
const sqls = [];
const rawQuery = (sql, binding) => {
sqls.push([sql, binding]);
};
raw(rawQuery, this.tables, targetColumns);
if (0 < sqls.length) {
for (const sql of sqls) {
builder = this.database.connection.raw(sql[0], sql[1]);
}
builders.push(builder);
}
}
return builders;
}
drop(tableName) {
return this.database.schema.dropTable(this.tables.get(tableName));
}
};
exports.SchemaBuilder = SchemaBuilder;
exports.SchemaBuilder = SchemaBuilder = SchemaBuilder_1 = __decorate([
(0, component_1.component)({ scope: constants_1.Scope.Transient }),
__metadata("design:paramtypes", [database_1.default,
config_1.Config,
tables_1.Tables])
], SchemaBuilder);
SchemaBuilder.add({
blogmeta: schema.blogMeta,
blogs: schema.blogs,
commentmeta: schema.commentMeta,
comments: schema.comments,
links: schema.links,
options: schema.options,
postmeta: schema.postMeta,
posts: schema.posts,
registration_log: schema.registrationLog,
signups: schema.signups,
site: schema.site,
sitemeta: schema.siteMeta,
term_relationships: schema.termRelationships,
term_taxonomy: schema.termTaxonomy,
termmeta: schema.termMeta,
terms: schema.terms,
usermeta: schema.userMeta,
users: schema.users,
});