Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
)
/**
* Parent model and it's foreign key in pivot table
*/
this.localKey = this._options.localKey || this.model.$primaryKey
this.pivotForeignKey = this._options.pivotForeignKey || snakeCase(
`${this.model.name}_${this.model.$primaryKey}`,
)
this.pivotForeignKeyAlias = `pivot_${this.pivotForeignKey}`
/**
* Related model and it's foreign key in pivot table
*/
this.relatedKey = this._options.relatedKey || this.relatedModel().$primaryKey
this.pivotRelatedForeignKey = this._options.pivotRelatedForeignKey || snakeCase(
`${this.relatedModel().name}_${this.relatedModel().$primaryKey}`,
)
this.pivotRelatedForeignKeyAlias = `pivot_${this.pivotRelatedForeignKey}`
/**
* Validate computed keys to ensure they are valid
*/
this._validateKeys()
/**
* Keys for the adapter
*/
this.localAdapterKey = this.model.$getColumn(this.localKey)!.castAs
this.relatedAdapterKey = this.relatedModel().$getColumn(this.relatedKey)!.castAs
this.booted = true
}
public changeCase (pattern?: 'pascalcase' | 'camelcase' | 'snakecase'): this {
switch (pattern) {
case 'camelcase':
this.input = camelCase(this.input)
return this
case 'pascalcase':
this.input = pascalCase(this.input)
return this
case 'snakecase':
this.input = snakeCase(this.input)
return this
default:
return this
}
}
public boot () {
if (this.booted) {
return
}
this.pivotTable = this._options.pivotTable || snakeCase(
[this.relatedModel().name, this.model.name].sort().join('_'),
)
/**
* Parent model and it's foreign key in pivot table
*/
this.localKey = this._options.localKey || this.model.$primaryKey
this.pivotForeignKey = this._options.pivotForeignKey || snakeCase(
`${this.model.name}_${this.model.$primaryKey}`,
)
this.pivotForeignKeyAlias = `pivot_${this.pivotForeignKey}`
/**
* Related model and it's foreign key in pivot table
*/
this.relatedKey = this._options.relatedKey || this.relatedModel().$primaryKey
this.pivotRelatedForeignKey = this._options.pivotRelatedForeignKey || snakeCase(
`${this.relatedModel().name}_${this.relatedModel().$primaryKey}`,
)
this.pivotRelatedForeignKeyAlias = `pivot_${this.pivotRelatedForeignKey}`
/**
* Validate computed keys to ensure they are valid
*/
public static $addColumn (name: string, options: Partial) {
const descriptor = Object.getOwnPropertyDescriptor(this.prototype, name)
const column: ColumnNode = {
primary: options.primary || false,
castAs: options.castAs || snakeCase(name),
hasGetter: !!(descriptor && descriptor.get),
hasSetter: !!(descriptor && descriptor.set),
serializeAs: options.serializeAs || snakeCase(name),
serialize: options.serialize === false ? false : true,
}
/**
* Set column as the primary column, when `primary` is to true
*/
if (column.primary) {
this.$primaryKey = name
}
this.$columns.set(name, column)
this._mappings.cast.set(column.castAs!, name)
this.$refs[name] = column.castAs
return
}
this.$booted = true
this.$primaryKey = this.$primaryKey || 'id'
Object.defineProperty(this, '$refs', { value: {} })
Object.defineProperty(this, '$columns', { value: new Map() })
Object.defineProperty(this, '$computed', { value: new Map() })
Object.defineProperty(this, '$relations', { value: new Map() })
Object.defineProperty(this, '_hooks', { value: new Hooks(this.$container) })
Object.defineProperty(this, '_mappings', { value: { cast: new Map() }})
this.$increments = this.$increments === undefined ? true : this.$increments
this.$table = this.$table === undefined ? pluralize(snakeCase(this.name)) : this.$table
}
public boot () {
if (this.booted) {
return
}
this.pivotTable = this._options.pivotTable || snakeCase(
[this.relatedModel().name, this.model.name].sort().join('_'),
)
/**
* Parent model and it's foreign key in pivot table
*/
this.localKey = this._options.localKey || this.model.$primaryKey
this.pivotForeignKey = this._options.pivotForeignKey || snakeCase(
`${this.model.name}_${this.model.$primaryKey}`,
)
this.pivotForeignKeyAlias = `pivot_${this.pivotForeignKey}`
/**
* Related model and it's foreign key in pivot table
*/
this.relatedKey = this._options.relatedKey || this.relatedModel().$primaryKey
/**
* Foreign key on the `relatedModel`. This bounds the `throughModel` with
* the `relatedModel`.
*/
public throughForeignKey: string
/**
* Adapter key for the defined `throughForeignKey`
*/
public throughForeignAdapterKey: string
/**
* Key to be used for serializing the relationship
*/
public serializeAs = this._options.serializeAs || snakeCase(this.relationName)
/**
* A flag to know if model keys valid for executing database queries or not
*/
public booted: boolean = false
constructor (
public relationName: string,
private _options: ThroughRelationNode,
public model: ModelConstructorContract,
) {
this._ensureRelatedModel()
}
/**
* Ensure that related model is defined, otherwise raise an exception, since
toTableName (filename: string) {
return tableName || snakeCase(filename.replace(prefix, ''))
},
})
const isNotSnakeCase = function(name) {
return snakeCase(name) !== name;
};
public pivotRelatedForeignKeyAlias: string
/**
* Pivot table for joining relationships
*/
public pivotTable: string
/**
* Extra pivot columns to extra
*/
public extrasPivotColumns: string[] = this._options.pivotColumns || []
/**
* Key to be used for serializing the relationship
*/
public serializeAs = this._options.serializeAs || snakeCase(this.relationName)
/**
* A flag to know if model keys are valid for executing database queries or not
*/
public booted: boolean = false
constructor (
public relationName: string,
private _options: ManyToManyRelationNode,
public model: ModelConstructorContract,
) {
this._ensureRelatedModel()
}
/**
* Ensure that related model is defined, otherwise raise an exception, since