tspace-mysql
Version:
Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.
868 lines • 36.6 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RelationHandler = void 0;
const pluralize_1 = __importDefault(require("pluralize"));
const Model_1 = require("../Model");
class RelationHandler {
$model;
$constants;
$logger;
constructor(model) {
this.$model = model;
this.$constants = this.$model["$constants"];
this.$logger = this.$model["$logger"];
}
async load(parents, relation) {
const relationIsBelongsToMany = relation.relation === this.$constants('RELATIONSHIP').belongsToMany;
if (relationIsBelongsToMany) {
return await this._belongsToMany(parents, relation);
}
if (!Object.keys(relation)?.length)
return [];
const { localKey, foreignKey } = this._valueInRelation(relation);
let parentIds = parents
.map((parent) => {
const key = parent[localKey];
if (key === undefined) {
throw this._assertError([
`This relationship lacks a primary in the '${relation?.name}' relation.`,
`Please review the query to identify whether the key '${localKey}' is missing.`
].join(' '));
}
return +key;
})
.filter(d => d != null);
parentIds = Array.from(new Set(parentIds));
const query = relation.query;
if (query == null) {
throw this._assertError(`The callback query '${relation.name}' is unknown.`);
}
if (relation.count) {
const childs = await query
.meta('SUBORDINATE')
.whereIn(foreignKey, parentIds)
.selectRaw(`${this.$constants('COUNT')}(\`${foreignKey}\`) ${this.$constants('AS')} \`aggregate\``)
.debug(this.$model['$state'].get('DEBUG'))
.when(relation.trashed, (query) => query.onlyTrashed())
.when(relation.all, (query) => query.disableSoftDelete())
.bind(this.$model['$pool'].get())
.groupBy(foreignKey)
.get();
return this._relationMapData({
parents,
childs,
relation
});
}
const childs = await query
.meta('SUBORDINATE')
.whereIn(foreignKey, parentIds)
.debug(this.$model['$state'].get('DEBUG'))
.when(relation.trashed, (query) => query.onlyTrashed())
.when(relation.all, (query) => query.disableSoftDelete())
.bind(this.$model['$pool'].get())
.get();
if (this.$model['$state'].get('DEBUG')) {
this.$model['$state'].set('QUERIES', [
...this.$model['$state'].get('QUERIES'),
query.toString()
]);
}
return this._relationMapData({
parents,
childs,
relation
});
}
loadExists() {
const relations = this.$model['$state'].get('RELATIONS');
for (const index in relations) {
const relation = relations[index];
if (!Object.keys(relation)?.length)
continue;
if (relation.exists == null && relation.notExists == null)
continue;
const { localKey, foreignKey, pivot, modelPivot } = this._valueInRelation(relation);
const query = relation.query;
if (query == null) {
throw this._assertError(`The callback query '${relation.name}' is unknown.`);
}
const clone = new Model_1.Model().clone(query);
const cloneRelations = clone['$state'].get('RELATIONS');
if (cloneRelations.length) {
for (const r of cloneRelations) {
if (r.query == null)
continue;
if (r.exists == null && r.notExists)
continue;
const sql = clone['$relation']?._handleRelationExists(r);
if (sql == null || sql === '')
continue;
if (r.notExists) {
clone.whereNotExists(sql);
continue;
}
clone.whereExists(sql);
}
}
if (relation.relation === this.$constants('RELATIONSHIP').belongsToMany) {
const thisPivot = modelPivot
? new modelPivot
: new Model_1.Model().table(String(pivot));
const sql = clone
.bind(this.$model['$pool'].get())
.select1()
.whereReference(query.bindColumn(foreignKey), thisPivot.bindColumn(localKey))
.toString();
if (relation.notExists) {
thisPivot.whereNotExists(sql);
}
else {
thisPivot.whereExists(sql);
}
const sqlPivot = thisPivot
.bind(this.$model['$pool'].get())
.select1()
.whereReference(this.$model.bindColumn(foreignKey), thisPivot.bindColumn([
pluralize_1.default.singular(this.$model.getTableName()),
foreignKey
].join("_")))
.toString();
if (relation.notExists) {
this.$model['whereNotExists'](sqlPivot);
continue;
}
this.$model['whereExists'](sqlPivot);
continue;
}
const sql = clone
.bind(this.$model['$pool'].get())
.select1()
.whereReference(this.$model.bindColumn(localKey), query.bindColumn(foreignKey))
.unset({
orderBy: true,
limit: true
})
.toString();
if (relation.notExists) {
this.$model['whereNotExists'](sql);
continue;
}
this.$model['whereExists'](sql);
}
const sql = this.$model['_queryBuilder']().select();
return sql;
}
getSqlExists(name, cb) {
const relation = this.$model['$state'].get('RELATION')?.find((data) => data.name === name);
if (relation == null) {
throw this._assertError(`This name relation '${name}' not be register in Model '${this.$model.constructor?.name}.'`);
}
const relationHasExists = Object.values(this.$constants('RELATIONSHIP'))?.includes(relation.relation);
if (!relationHasExists) {
throw this._assertError(`The relationship '${relation.relation}' is unknown.`);
}
if (relation.model == null) {
throw this._assertError(`Model for the relationship is unknown.`);
}
relation.query = cb(relation.query == null ? new relation.model() : relation.query);
const { localKey, foreignKey } = this._valueInRelation(relation);
const query = relation.query;
if (query == null) {
throw this._assertError(`The callback query '${relation.name}' is unknown.`);
}
const clone = new Model_1.Model().clone(query);
const sql = clone
.bind(this.$model['$pool'].get())
.select1()
.whereReference(this.$model.bindColumn(localKey), query.bindColumn(foreignKey))
.unset({
orderBy: true,
limit: true
})
.toString();
return sql;
}
apply(nameRelations, type) {
const relations = nameRelations.map((name) => {
const relation = this.$model['$state'].get('RELATION')?.find((data) => data.name === name);
if (relation == null) {
throw this._assertError(`This name relation '${name}' not be register in Model '${this.$model.constructor?.name}.'`);
}
const relationHasExists = Object.values(this.$constants('RELATIONSHIP'))?.includes(relation.relation);
if (!relationHasExists) {
throw this._assertError(`The relationship '${relation.relation}' is unknown.`);
}
if (relation.model == null) {
throw this._assertError(`Model for the relationship is unknown.`);
}
relation.query = relation.query == null ? new relation.model() : relation.query;
return relation;
});
for (const relation of relations) {
if (type === 'default')
break;
relation[type] = true;
}
return this.$model['$state'].get('RELATIONS').length
? [...relations.map((w) => {
const exists = this.$model['$state'].get('RELATIONS').find((r) => r.name === w.name);
if (exists)
return null;
return w;
}).filter((d) => d != null),
...this.$model['$state'].get('RELATIONS')]
: relations;
}
callback(nameRelation, cb) {
const relation = this.$model['$state'].get('RELATIONS').find((data) => data.name === nameRelation);
if (relation == null) {
throw this._assertError(`This name relation '${nameRelation}' not be register in Model '${this.$model.constructor?.name}.'`);
}
const relationHasExists = Object.values(this.$constants('RELATIONSHIP'))?.includes(relation.relation);
if (!relationHasExists) {
throw this._assertError(`The relationship '${relation.relation}' is unknown.`);
}
if (relation.model == null) {
throw this._assertError(`Model for the relationship is unknown.`);
}
relation.query = cb(relation.query == null ? new relation.model() : relation.query);
return;
}
callbackPivot(nameRelation, cb) {
const relation = this.$model['$state'].get('RELATIONS').find((data) => data.name === nameRelation);
if (relation == null) {
throw this._assertError(`This name relation '${nameRelation}' not be register in Model '${this.$model.constructor?.name}'`);
}
const relationHasExists = Object.values(this.$constants('RELATIONSHIP'))?.includes(relation.relation);
if (!relationHasExists) {
throw this._assertError(`The relationship '${relation.relation}' is unknown.`);
}
if (relation.relation !== this.$constants('RELATIONSHIP').belongsToMany) {
throw this._assertError(`The pivot options support 'belongsToMany' relations exclusively.`);
}
if (relation.modelPivot == null) {
throw this._assertError(`Model pivot for the relationship is unknown`);
}
relation.queryPivot = cb(relation.queryPivot == null ? new relation.modelPivot() : relation.queryPivot);
return;
}
returnCallback(nameRelation) {
const relation = this.$model['$state'].get('RELATION').find((data) => data.name === nameRelation);
if (relation == null)
return null;
const relationHasExists = Object.values(this.$constants('RELATIONSHIP'))?.includes(relation.relation);
if (!relationHasExists) {
throw this._assertError(`The relationship '${relation.relation}' is unknown.`);
}
return relation.query == null ? new relation.model() : relation.query;
}
hasOne({ name, as, model, localKey, foreignKey, freezeTable }) {
const relation = {
name,
model,
as,
relation: this.$constants('RELATIONSHIP').hasOne,
localKey,
foreignKey,
freezeTable,
query: null
};
return this.$model['$state'].set('RELATION', [...this.$model['$state'].get('RELATION'), relation]);
}
hasMany({ name, as, model, localKey, foreignKey, freezeTable }) {
const relation = {
name,
model,
as,
relation: this.$constants('RELATIONSHIP').hasMany,
localKey,
foreignKey,
freezeTable,
query: null
};
return this.$model['$state'].set('RELATION', [...this.$model['$state'].get('RELATION'), relation]);
}
belongsTo({ name, as, model, localKey, foreignKey, freezeTable }) {
const relation = {
name,
model,
as,
relation: this.$constants('RELATIONSHIP').belongsTo,
localKey,
foreignKey,
freezeTable,
query: null
};
return this.$model['$state']
.set('RELATION', [
...this.$model['$state'].get('RELATION'),
relation
]);
}
belongsToMany({ name, as, model, localKey, foreignKey, freezeTable, pivot, oldVersion, modelPivot }) {
const relation = {
name,
model,
as,
relation: this.$constants('RELATIONSHIP').belongsToMany,
localKey,
foreignKey,
freezeTable,
pivot,
oldVersion,
query: null,
modelPivot
};
return this.$model['$state']
.set('RELATION', [
...this.$model['$state'].get('RELATION'),
relation
]);
}
manyToMany({ name, as, model, localKey, foreignKey, freezeTable, pivot, oldVersion, modelPivot }) {
const relation = {
name,
model,
as,
relation: this.$constants('RELATIONSHIP').belongsToMany,
localKey,
foreignKey,
freezeTable,
pivot,
oldVersion,
query: null,
modelPivot
};
return this.$model['$state']
.set('RELATION', [
...this.$model['$state'].get('RELATION'),
relation
]);
}
hasOneBuilder({ name, as, model, localKey, foreignKey, freezeTable, }, callback) {
const nameRelation = name == null
? this._functionTRelationOptionsName()
: String(name);
const relation = {
name: nameRelation,
model,
as,
relation: this.$constants('RELATIONSHIP').hasOne,
localKey,
foreignKey,
freezeTable,
query: null
};
const r = this._relationBuilder(nameRelation, relation);
if (callback == null) {
r.query = new r.model();
return this;
}
r.query = callback(new r.model());
return;
}
hasManyBuilder({ name, as, model, localKey, foreignKey, freezeTable, }, callback) {
const nameRelation = name == null
? this._functionTRelationOptionsName()
: String(name);
const relation = {
name: nameRelation,
model,
as,
relation: this.$constants('RELATIONSHIP').hasMany,
localKey,
foreignKey,
freezeTable,
query: null
};
const r = this._relationBuilder(nameRelation, relation);
if (callback == null) {
r.query = new r.model();
return this;
}
r.query = callback(new r.model());
return;
}
belongsToBuilder({ name, as, model, localKey, foreignKey, freezeTable, }, callback) {
const nameRelation = name == null
? this._functionTRelationOptionsName()
: String(name);
const relation = {
name: nameRelation,
model,
as,
relation: this.$constants('RELATIONSHIP').belongsTo,
localKey,
foreignKey,
freezeTable,
query: null
};
const r = this._relationBuilder(nameRelation, relation);
if (callback == null) {
r.query = new r.model();
return this;
}
r.query = callback(new r.model());
return;
}
belongsToManyBuilder({ name, as, model, localKey, foreignKey, freezeTable, pivot }, callback) {
const nameRelation = name == null
? this._functionTRelationOptionsName()
: String(name);
const relation = {
name: nameRelation,
model,
as,
relation: this.$constants('RELATIONSHIP').belongsToMany,
localKey,
foreignKey,
freezeTable,
pivot,
query: null
};
const r = this._relationBuilder(nameRelation, relation);
if (callback == null) {
r.query = new r.model();
return this;
}
r.query = callback(new r.model());
return;
}
_handleRelationExists(relation) {
if (!Object.keys(relation)?.length) {
throw this._assertError(`Unknown the relation`);
}
if (relation.exists == null && relation.notExists == null)
return '';
const { localKey, foreignKey } = this._valueInRelation(relation);
const query = relation.query;
if (query == null) {
throw this._assertError(`Unknown callback query in the relation name : '${relation.name}']`);
}
const clone = new Model_1.Model().clone(query);
const cloneRelations = clone['$state'].get('RELATIONS');
if (cloneRelations.length) {
for (const r of cloneRelations) {
if (r.exists == null && r.notExists == null)
continue;
if (r.query == null)
continue;
if (r.relation === this.$constants('RELATIONSHIP').belongsToMany) {
const data = clone['$relation']?._valueInRelation(r);
if (data == null)
continue;
const { modelPivot, pivot, foreignKey } = data;
const thisPivot = modelPivot == null && pivot == null
? new Model_1.Model().table(this._valuePattern([
pluralize_1.default.singular(clone.getTableName()),
pluralize_1.default.singular(this.$model['getTableName']())
].join("_")))
: modelPivot != null
? new modelPivot
: new Model_1.Model().table(`${pivot}`);
const sqlPivot = thisPivot
.bind(this.$model['$pool'].get())
.select1()
.whereReference(this.$model.bindColumn(foreignKey), thisPivot.bindColumn([
pluralize_1.default.singular(this.$model.getTableName()),
foreignKey
].join("_")))
.unset({
orderBy: true,
limit: true
})
.toString();
clone.whereExists(sqlPivot);
if (r.notExists) {
thisPivot.whereNotExists(sqlPivot);
}
else {
thisPivot.whereExists(sqlPivot);
}
continue;
}
const sql = clone['$relation']?._handleRelationExists(r) ?? null;
if (sql == null || sql === '')
continue;
if (r.notExists) {
clone.whereNotExists(sql);
continue;
}
clone.whereExists(sql);
}
}
if (relation.relation === this.$constants('RELATIONSHIP').belongsToMany) {
const data = clone['$relation']?._valueInRelation(relation);
if (data != null) {
const { modelPivot, pivot, foreignKey } = data;
const thisPivot = modelPivot == null && pivot == null
? new Model_1.Model().table(this._valuePattern([
pluralize_1.default.singular(clone.getTableName()),
pluralize_1.default.singular(this.$model['getTableName']())
].join("_")))
: modelPivot != null
? new modelPivot
: new Model_1.Model().table(`${pivot}`);
const sql = clone
.bind(this.$model['$pool'].get())
.select1()
.whereReference(clone.bindColumn(foreignKey), thisPivot.bindColumn([
pluralize_1.default.singular(clone.getTableName()),
foreignKey
].join("_")))
.unset({
orderBy: true,
limit: true
})
.toString();
if (relation.notExists) {
thisPivot.whereNotExists(sql);
}
else {
thisPivot.whereExists(sql);
}
const sqlPivot = thisPivot
.bind(this.$model['$pool'].get())
.select1()
.whereReference(this.$model.bindColumn(foreignKey), thisPivot.bindColumn([
pluralize_1.default.singular(this.$model.getTableName()),
foreignKey
].join("_")))
.unset({
orderBy: true,
limit: true
})
.toString();
if (relation.notExists) {
clone.whereNotExists(sqlPivot);
}
else {
clone.whereExists(sqlPivot);
}
return sqlPivot;
}
}
if (relation.exists == null && relation.notExists == null)
return '';
const sql = clone
.bind(this.$model['$pool'].get())
.select1()
.whereReference(this.$model.bindColumn(localKey), clone.bindColumn(foreignKey))
.unset({
orderBy: true,
limit: true
})
.toString();
return sql;
}
_relationBuilder(nameRelation, relation) {
this.$model['$state'].set('RELATION', [...this.$model['$state'].get('RELATION'), relation]);
this.$model['with'](nameRelation);
const r = this.$model['$state'].get('RELATIONS').find((data) => data.name === nameRelation);
this._assertError(relation == null, `The relation '${nameRelation}' is not registered in the model '${this.$model.constructor?.name}'.`);
return r;
}
_functionTRelationOptionsName() {
const functionName = [...this.$logger.get()][this.$logger.get().length - 2];
return functionName.replace(/([A-Z])/g, (str) => `_${str.toLowerCase()}`);
}
_relationMapData({ parents, childs, relation }) {
const { name, as, relation: relationName, localKey, foreignKey } = this._valueInRelation(relation);
const alias = as ?? name;
const relationIsHasOneOrBelongsTo = [
this.$constants('RELATIONSHIP').hasOne,
this.$constants('RELATIONSHIP').belongsTo
].some(r => r === relationName);
const relationIsHasManyOrBelongsToMany = [
this.$constants('RELATIONSHIP').hasMany,
this.$constants('RELATIONSHIP').belongsToMany
].some(r => r === relationName);
const children = [...childs]
.reduce((prev, curr) => {
const key = +curr[foreignKey];
if (Number.isNaN(key)) {
throw this._assertError([
`This relationship lacks a foreign key in the '${relation?.name}' relation.`,
`Please review the query to identify whether the key '${foreignKey}' is missing.`
].join(' '));
}
if (!prev[key]) {
prev[key] = { [foreignKey]: key, values: [] };
}
prev[key].values.push({ ...curr });
return prev;
}, {});
for (const parent of parents) {
if (relationIsHasOneOrBelongsTo)
parent[alias] = relation.count ? 0 : null;
if (relationIsHasManyOrBelongsToMany)
parent[alias] = relation.count ? 0 : [];
const match = children[+parent[localKey]];
if (match == null)
continue;
const childrens = match?.values ?? [];
if (relation.count) {
if (parent[alias] == 0) {
const count = Number(childrens[0]?.aggregate ?? 0);
parent[alias] = relationIsHasOneOrBelongsTo
? count > 1 ? 1 : count
: count;
}
continue;
}
if (relationIsHasOneOrBelongsTo) {
if (parent[alias] == null) {
parent[alias] = childrens[0] ?? null;
}
continue;
}
parent[alias] = childrens;
}
if (this.$model['$state'].get('HIDDEN').length)
this.$model['_hiddenColumnModel'](parents);
return parents;
}
async _belongsToMany(parents, relation) {
let { name, foreignKey, localKey, pivot, oldVersion, modelPivot, queryPivot, as } = this._valueInRelation(relation);
const localKeyId = parents.map((parent) => {
const data = parent[foreignKey];
if (data === undefined) {
throw this._assertError([
`This relationship lacks a foreign key in the '${relation?.name}' relation.`,
`Please review the query to identify whether the key '${foreignKey}' is missing.`
].join(' '));
}
return data;
}).filter((d) => d != null);
const mainResultIds = Array.from(new Set(localKeyId));
const modelRelation = relation.query == null ? new relation.model() : relation.query;
const relationColumn = this.$model['_classToTableName'](modelRelation.constructor.name, { singular: true });
const mainlocalKey = 'id';
const relationForeignKey = this._valuePattern(`${relationColumn}Id`);
const localKeyPivotTable = this._valuePattern([pluralize_1.default.singular(this.$model['getTableName']()), foreignKey].join("_"));
const pivotTable = String((relation.pivot ?? pivot));
const sqlPivotExists = new Model_1.Model()
.copyModel(modelRelation)
.select1()
.whereReference(modelRelation.bindColumn(foreignKey), `\`${pivotTable}\`.\`${localKey}\``)
.toString();
queryPivot = queryPivot == null
? modelPivot
? new modelPivot()
: new Model_1.Model().table(pivotTable)
: queryPivot;
if (relation.count) {
const pivotResults = await queryPivot
.meta('SUBORDINATE')
.whereIn(localKeyPivotTable, mainResultIds)
.selectRaw(`${this.$constants('COUNT')}(${localKeyPivotTable}) ${this.$constants('AS')} \`aggregate\``)
.when(relation.exists, (query) => query.whereExists(sqlPivotExists))
.when(relation.trashed, (query) => query.onlyTrashed())
.when(relation.all, (query) => query.disableSoftDelete())
.groupBy(localKeyPivotTable)
.bind(this.$model['$pool'].get())
.debug(this.$model['$state'].get('DEBUG'))
.get();
for (const parent of parents) {
if (parent[name] == null)
parent[name] = 0;
for (const pivotResult of pivotResults) {
if (pivotResult[localKeyPivotTable] !== parent[foreignKey])
continue;
parent[name] = Number(pivotResult.aggregate ?? 0);
}
}
if (this.$model['$state'].get('HIDDEN').length)
this.$model['_hiddenColumnModel'](parents);
return parents;
}
const pivotResults = await queryPivot
.meta('SUBORDINATE')
.whereIn(localKeyPivotTable, mainResultIds)
.when(relation.query != null, (query) => query.select(localKeyPivotTable, localKey))
.when(relation.exists, (query) => query.whereExists(sqlPivotExists))
.when(relation.trashed, (query) => query.onlyTrashed())
.when(relation.all, (query) => query.disableSoftDelete())
.bind(this.$model['$pool'].get())
.debug(this.$model['$state'].get('DEBUG'))
.get();
const relationIds = Array.from(new Set(pivotResults
.map((pivotResult) => pivotResult[relationForeignKey])
.filter((d) => d != null)));
const relationResults = await modelRelation
.meta('SUBORDINATE')
.whereIn(mainlocalKey, relationIds)
.when(relation.trashed, (query) => query.disableSoftDelete())
.bind(this.$model['$pool'].get())
.debug(this.$model['$state'].get('DEBUG'))
.get();
const alias = as ?? name;
if (oldVersion) {
for (const pivotResult of pivotResults) {
for (const relationResult of relationResults) {
if (relationResult[mainlocalKey] !== pivotResult[relationForeignKey])
continue;
pivotResult[relationColumn] = relationResult;
}
}
for (const parent of parents) {
if (parent[alias] == null)
parent[alias] = [];
for (const pivotResult of pivotResults) {
if (pivotResult[localKeyPivotTable] !== parent[foreignKey])
continue;
parent[alias].push(pivotResult);
}
}
if (this.$model['$state'].get('HIDDEN').length)
this.$model['_hiddenColumnModel'](parents);
return parents;
}
const children = [...pivotResults]
.reduce((prev, curr) => {
const key = +curr[localKeyPivotTable];
if (Number.isNaN(key)) {
throw this._assertError([
`This relationship lacks a prisma key in pivot table the '${relation?.name}' relation.`,
`Please review the query to identify whether the key '${localKeyPivotTable}' is missing.`
].join(' '));
}
if (!prev[key]) {
prev[key] = { [localKeyPivotTable]: key, values: [] };
}
prev[key].values.push({ ...curr });
return prev;
}, {});
for (const parent of parents) {
if (parent[alias] == null)
parent[alias] = [];
const match = children[`${parent[foreignKey]}`];
if (match == null)
continue;
const childrens = match?.values ?? [];
for (const children of childrens) {
const data = relationResults.find(relationResult => relationResult[foreignKey] === children[localKey]);
if (data == null)
continue;
parent[alias].push(data);
}
}
if (this.$model['$state'].get('HIDDEN').length) {
this.$model['_hiddenColumnModel'](parents);
}
return parents;
}
_valueInRelation(relationModel) {
this._assertError(relationModel?.query instanceof Promise, 'The Promise method does not support nested relations.');
this._assertError(!(relationModel?.query instanceof Model_1.Model), 'The callback function only supports instances of the Model class.');
const relation = relationModel.relation;
const model = relationModel.model;
const modelPivot = relationModel.modelPivot;
const oldVersion = relationModel.oldVersion;
const query = relationModel?.query;
const queryPivot = relationModel?.queryPivot;
const table = relationModel.freezeTable
? relationModel.freezeTable
: relationModel.query?.getTableName();
let pivot = null;
const name = relationModel.name;
const as = relationModel.as;
this._assertError(!model || model == null, 'Model not found.');
let localKey = this._valuePattern(relationModel.localKey
? relationModel.localKey
: this.$model['$state'].get('PRIMARY_KEY'));
let foreignKey = relationModel.foreignKey
? relationModel.foreignKey
: this._valuePattern([
`${pluralize_1.default.singular(this.$model['getTableName']())}`,
`${this.$model['$state'].get('PRIMARY_KEY')}`
].join('_'));
const checkTRelationOptionsIsBelongsTo = [
relationModel.localKey == null,
relationModel.foreignKey == null,
relation === this.$constants('RELATIONSHIP').belongsTo
].every(r => r);
if (checkTRelationOptionsIsBelongsTo) {
foreignKey = this._valuePattern(localKey);
localKey = this._valuePattern([
`${pluralize_1.default.singular(table ?? '')}`,
`${this.$model['$state'].get('PRIMARY_KEY')}`
].join('_'));
}
const checkTRelationOptionsIsBelongsToMany = [
relationModel.localKey == null,
relationModel.foreignKey == null,
relation === this.$constants('RELATIONSHIP').belongsToMany
].every(r => r);
if (checkTRelationOptionsIsBelongsToMany) {
localKey = this._valuePattern([
`${pluralize_1.default.singular(table ?? '')}`,
`${this.$model['$state'].get('PRIMARY_KEY')}`
].join('_'));
foreignKey = 'id';
const query = relationModel.query;
const defaultPivot = this._valuePattern([
pluralize_1.default.singular(this.$model['getTableName']()),
pluralize_1.default.singular(query.getTableName())
].sort().join('_'));
pivot = relationModel.pivot || (modelPivot ? new modelPivot().getTableName() : defaultPivot);
}
return {
name,
as,
relation,
table,
localKey,
foreignKey,
model,
query,
queryPivot,
pivot,
oldVersion,
modelPivot
};
}
_valuePattern(value) {
const schema = this.$model['$state'].get('SCHEMA_TABLE');
const snakeCase = (str) => str.replace(/([A-Z])/g, (v) => `_${v.toLowerCase()}`);
const camelCase = (str) => str.replace(/(.(_|-|\s)+.)/g, (v) => `${v[0]}${v[v.length - 1].toUpperCase()}`);
switch (this.$model['$state'].get('PATTERN')) {
case this.$constants('PATTERN').snake_case: {
if (schema == null) {
return snakeCase(value);
}
const find = schema[value];
if (find == null || find.column == null) {
return snakeCase(value);
}
return find.column;
}
case this.$constants('PATTERN').camelCase: {
if (schema == null) {
return camelCase(value);
}
const find = schema[value];
if (find == null || find.column == null) {
return camelCase(value);
}
return find.column;
}
default: return value;
}
}
_assertError(condition = true, message = 'error') {
if (typeof condition === 'string') {
throw new Error(condition);
}
if (condition)
throw new Error(message);
return;
}
}
exports.RelationHandler = RelationHandler;
exports.default = RelationHandler;
//# sourceMappingURL=Relation.js.map