Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function listDirectoryFiles (
scanDirectory: string,
appRoot: string,
filterFn?: CommandsListFilterFn,
): string[] {
return fsReadAll(scanDirectory)
.filter((name) => !name.endsWith('.json')) // remove .json files
.map((name) => {
const relativePath = relative(appRoot, join(scanDirectory, name))
return slash(relativePath.startsWith('../') ? relativePath : `./${relativePath}`)
})
.filter((name) => {
if (typeof (filterFn) === 'function') {
return filterFn(name)
}
return Array.isArray(filterFn) ? !filterFn.includes(name) : true
})
}
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,
}
}
public async handle (argv: string[]) {
this.setupApplication()
await this.importAssembler(argv[0])
const manifest = new this.ace.Manifest(dirname(resolveFrom(this.appRoot, '@adonisjs/assembler')))
const kernel = new this.ace.Kernel(this.application)
/**
* Showing commands help
*/
kernel.flag('help', async (value, _, command) => {
if (!value) {
return
}
kernel.printHelp(command)
process.exit(0)
}, { alias: 'h' })
kernel.useManifest(manifest)
await kernel.handle(argv)
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
}