extend ModelQueryBuilderContract? #2766
varun-nambiar
started this conversation in
Ideas
Replies: 2 comments 1 reply
-
Update I got this to work now. Heres my DatabaseProvider... but import { ApplicationContract } from '@ioc:Adonis/Core/Application'
export default class DatabaseProvider {
constructor(protected app: ApplicationContract) {
}
public register() {
// Register your own bindings
this.app.container.bind('Binding', () => { })
}
public async boot() {
const ModelQueryBuilder = (await import('@ioc:Adonis/Lucid/Database')).default.ModelQueryBuilder
ModelQueryBuilder.macro('archive', function () {
// has deleted column defined?
if (this.model.$hasColumn('deleted')) {
return this.update({
deleted: 1
})
} else {
return this.delete()
}
})
}
} Its working as expected (so far) contract/database.ts declare module '@ioc:Adonis/Lucid/Model' {
interface ModelQueryBuilderContract {
archive(): this
}
} Error message: I feel like I'm very much close to get it working or I may be way too off. Thank you! |
Beta Was this translation helpful? Give feedback.
0 replies
-
Update Had to change '@IOC:Adonis/Lucid/Model' to '@IOC:Adonis/Lucid/Orm' |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is it possible to do something like this with ModelQueryBuilderContract?
https://docs.adonisjs.com/guides/response#informing-typescript-about-the-method
I tried creating the following
I'm trying to add a new method "archive" (very similar to delete)
I am able to add methods and hooks into models and achieve this for individual records (or map for each record)
Is this possible by creating a contract and then creating a DatabaseProvider (similar to what is in the guide for Response/Request contract)?
❤️
Beta Was this translation helpful? Give feedback.
All reactions