@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
189 lines (188 loc) • 7.13 kB
JavaScript
"use strict";
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 __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var MetaQuery_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetaQuery = void 0;
const component_1 = require("../decorators/component");
const alias_1 = require("./alias");
const query_builder_1 = require("./query-builder");
const query_builders_1 = require("./query-builders");
let MetaQuery = MetaQuery_1 = class MetaQuery extends query_builder_1.QueryBuilder {
alias;
builders;
builder;
table;
primaryTable;
columns;
#valueType = "CHAR";
constructor(alias, builders, builder, table, primaryTable, columns) {
super(alias, builders, builder, MetaQuery_1);
this.alias = alias;
this.builders = builders;
this.builder = builder;
this.table = table;
this.primaryTable = primaryTable;
this.columns = columns;
}
setPrimaryTable(table) {
this.columns = {
primary: "post" === table || "user" === table
? "ID"
: "site" === table
? "id"
: `${table}_id`,
meta: `${table}_id`,
};
this.primaryTable = table == "site" ? "site" : `${table}s`;
this.table = `${table}meta`;
return this;
}
get from() {
this.builder.from(this.alias.as(this.table));
return this;
}
select(columns) {
this.builder.select(columns);
return this;
}
valueType(type) {
if (!type.match(/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:(d+(?:,s?d+)?))?|DECIMAL(?:(d+(?:,s?d+)?))?)$/)) {
type = "CHAR";
}
this.#valueType = "NUMERIC" === type ? "SIGNED" : type;
return this;
}
joinPrimary(type = "inner") {
const { as, column } = this.alias;
this.builder[`${type}Join`](as(this.primaryTable), column(this.table, this.columns.meta), column(this.primaryTable, this.columns.primary));
return this;
}
withIds(ids, options) {
const { joinPrimary = true } = options ?? {};
this.builder.clear("join");
if (joinPrimary) {
this.joinPrimary();
}
if (options?.not === true) {
this.builder.not;
}
this.builder.whereIn(
//this.alias.column(this.primaryTable, this.columns.primary),
this.alias.column(this.table, this.columns.meta), ids);
return this;
}
onNotExists(column, value) {
return this.onExists(column, value, true);
}
onExists(column, value, not = false) {
this.builder.clear("join");
const { column: toColumn, as } = this.alias;
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
this.builder[not ? "leftJoin" : "innerJoin"](as(this.primaryTable), function (qb) {
qb.on(toColumn(self.table, self.columns.meta), toColumn(self.primaryTable, self.columns.primary))[not ? "onNotExists" : "onExists"](function () {
this.select(toColumn(self.table, self.columns.meta))
.from(as(self.table))
.where(toColumn(self.table, column), value)
.limit(1);
});
});
return this;
}
castValue(v, as = this.#valueType) {
return this.builders.raw(`CAST(? as ${as})`, [v]);
}
whereKeyLike(value, options) {
const { column } = this.alias;
if (options?.not === true) {
this.builder.not.whereILike(column(this.table, "meta_key"), `%${value}%`);
}
else {
this.builder.whereILike(column(this.table, "meta_key"), `%${value}%`);
}
return this;
}
whereKeyNotLike(value) {
return this.whereKeyLike(value, { not: true });
}
whereLike(key, value, options) {
const { column } = this.alias;
this.builder.andWhere((subBuilder) => {
subBuilder.where(column(this.table, "meta_key"), key);
if (options?.not === true) {
subBuilder.not;
}
subBuilder.whereILike(column(this.table, "meta_value"), `%${value}%`);
});
return this;
}
whereNotLike(key, value) {
return this.whereLike(key, value, { not: true });
}
whereIn(key, obj) {
const { column } = this.alias;
this.builder.andWhere((subBuilder) => {
subBuilder
.where(column(this.table, "meta_key"), key)
.whereIn(column(this.table, "meta_value"), obj);
});
return this;
}
whereBetween(key, range) {
const { column } = this.alias;
this.builder.andWhere((subBuilder) => {
subBuilder
.where(column(this.table, "meta_key"), key)
.whereBetween(column(this.table, "meta_value"), range);
});
return this;
}
withKeys(keys) {
const { column } = this.alias;
this.builder.andWhere((subBuilder) => {
for (const key of keys) {
subBuilder.orWhere(column(this.table, "meta_key"), key);
}
});
return this;
}
where(key, value, op = "=") {
const { column } = this.alias;
//this.builder.where(toColumn(this.table, column), op, this.castValue(value));
this.builder.andWhere((subBuilder) => {
subBuilder.where(column(this.table, "meta_key"), key);
if (value) {
subBuilder.where(column(this.table, "meta_value"), op, value
//this.castValue(value)
);
}
});
return this;
}
regex(key, regex) {
const { column } = this.alias;
const value = this.#valueType === "BINARY"
? this.castValue(regex.source)
: `'${regex.source}'`;
this.builder.andWhere((subBuilder) => {
subBuilder
.where(column(this.table, "meta_key"), key)
.whereRaw(`?? REGEXP ${value}`, [column(this.table, "meta_value")]);
});
return this;
}
};
exports.MetaQuery = MetaQuery;
exports.MetaQuery = MetaQuery = MetaQuery_1 = __decorate([
(0, component_1.queryBuilder)(),
__metadata("design:paramtypes", [alias_1.Alias,
query_builders_1.QueryBuilders, Object, String, String, Object])
], MetaQuery);