Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public parseRelationName (relationName: string) {
const relations = relationName.split('.')
const primary = relations.shift()!
const relation = this._model.$getRelation(primary)
/**
* Undefined relationship
*/
if (!relation) {
throw new Exception(
`${primary} is not defined as a relationship on ${this._model.name} model`,
500,
'E_UNDEFINED_RELATIONSHIP',
)
}
return {
primary,
relation,
children: relations.length ? { relationName: relations.join('') } : null,
}
}
private _ensureCanPerformWrites () {
if (this.client && this.client.mode === 'read') {
throw new Exception('Updates and deletes cannot be performed in read mode')
}
}
const Model = this.constructor as typeof BaseModel
const relation = Model.$relations.get(key as string)
/**
* Ignore when relation is not defined
*/
if (!relation) {
return
}
/**
* Reset array before invoking $pushRelated
*/
if (MANY_RELATIONS.includes(relation.type)) {
if (!Array.isArray(models)) {
throw new Exception(
`${Model.name}.${key} must be an array when setting ${relation.type} relationship`,
)
}
this.$preloaded[key] = []
}
return this.$pushRelated(key, models)
}
private _ensureIsntDeleted () {
if (this.$isDeleted) {
throw new Exception('Cannot mutate delete model instance', 500, 'E_MODEL_DELETED')
}
}
public makePath (templatePath: string): string {
if (this._preRegistered.has(templatePath)) {
return templatePath
}
const [ diskName, template ] = extractDiskAndTemplateName(templatePath)
/**
* Raise exception when disk name is not defined
*/
const mountedDir = this._mountedDirs.get(diskName)
if (!mountedDir) {
throw new Exception(`{${diskName}} namespace is not mounted`, 500, 'E_UNMOUNTED_DISK_NAME')
}
return join(mountedDir, template)
}
private _validateKeys () {
const relationRef = `${this.model.name}.${this.relationName}`
if (!this.model.$hasColumn(this.localKey)) {
const ref = `${this.model.name}.${this.localKey}`
throw new Exception(
`${ref} required by ${relationRef} relation is missing`,
500,
'E_MISSING_RELATED_LOCAL_KEY',
)
}
if (!this.throughModel().$hasColumn(this.foreignKey)) {
const ref = `${this.throughModel().name}.${this.foreignKey}`
throw new Exception(
`${ref} required by ${relationRef} relation is missing`,
500,
'E_MISSING_RELATED_FOREIGN_KEY',
)
}
if (!this.throughModel().$hasColumn(this.throughLocalKey)) {
const ref = `${this.throughModel().name}.${this.throughLocalKey}`
throw new Exception(
`${ref} required by ${relationRef} relation is missing`,
500,
'E_MISSING_THROUGH_LOCAL_KEY',
)
}
if (!this.relatedModel().$hasColumn(this.throughForeignKey)) {
private _ensureRelatedModel () {
if (!this._options.relatedModel) {
throw new Exception(
'Related model reference is required to construct the relationship',
500,
'E_MISSING_RELATED_MODEL',
)
}
}
export function ensureRelation (
name: string,
relation?: T,
): relation is T {
if (!relation) {
throw new Exception(`Cannot process unregistered relationship ${name}`, 500)
}
return true
}
'E_MISSING_RELATED_LOCAL_KEY',
)
}
if (!this.throughModel().$hasColumn(this.foreignKey)) {
const ref = `${this.throughModel().name}.${this.foreignKey}`
throw new Exception(
`${ref} required by ${relationRef} relation is missing`,
500,
'E_MISSING_RELATED_FOREIGN_KEY',
)
}
if (!this.throughModel().$hasColumn(this.throughLocalKey)) {
const ref = `${this.throughModel().name}.${this.throughLocalKey}`
throw new Exception(
`${ref} required by ${relationRef} relation is missing`,
500,
'E_MISSING_THROUGH_LOCAL_KEY',
)
}
if (!this.relatedModel().$hasColumn(this.throughForeignKey)) {
const ref = `${this.relatedModel().name}.${this.throughForeignKey}`
throw new Exception(
`${ref} required by ${relationRef} relation is missing`,
500,
'E_MISSING_THROUGH_FOREIGN_KEY',
)
}
}
/**
* Ensure template content is defined as a string
*/
if (typeof (contents.template) !== 'string') {
throw new Exception(
'Make sure to define the template content as a string',
500,
'E_MISSING_TEMPLATE_CONTENTS',
)
}
/**
* Do not overwrite existing template with same template path
*/
if (this._preRegistered.has(templatePath)) {
throw new Exception(
`Cannot override previously registered {${templatePath}} template`,
500,
'E_DUPLICATE_TEMPLATE_PATH',
)
}
this._preRegistered.set(templatePath, contents)
}
}