-
Hello there, I'm wondering that how can I create service provider. There is no page that tell me about providers in V5 docs and I'm trying to create a provider that will be use for CDN(AWS S3) transactions. I've created a provider but I don't know how can I bind my service with its Config file to IoC container. I've created a config file that named as "services.ts" in config folder but when I look at to other config files (e.g. database.ts etc.), there is a type for this typed config file in ioc:Adonis/../.. (e.g. DatabaseConfig type etc.). If I define these types in config file that I've created, does it will be effective? Otherwise where should I define these types for using this config file (services.ts) in the provider that I've create? Actually this not the main problem, this was just an example. Is there a template for my problem? Thanks already. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I don't think Service Provider docs exist for v5 yet. My process for figuring out how they worked included:
Confusingly enough, the v4 service provider documentation states "The register method is used to register bindings, and you should never try to use any other binding inside this method", which is slightly confusing since some of the official providers explicitly call Since it seems like the algorithm the application container follows is "call register on everything" -> "call boot on everything", I'm guessing that the general rule is:
To answer your concrete question, I'd look to the Official Redis Provider to see how it uses the |
Beta Was this translation helpful? Give feedback.
-
I've recently faced a similar problem (creating a service provider for S3 file management).
import { MultipartFileContract } from "@ioc:Adonis/Core/BodyParser"
export class S3Service {
public async upload(file: MultipartFileContract): Promise<void> {
// Upload the file to your S3 provider ...
}
public async download(file: string): Promise<Readable> {
// Stream file from S3 ...
}
}
providers/ImageServiceProvider.ts export default class ImageServiceProvider {
public register() {
this.container.singleton("MyCompany/ImageService", () => {
const S3Service = require("../services/S3Service").S3Service // Maybe this can be improved (with import(...)?) 🤷♂️
return new S3Service()
})
}
}
contracts/ImageService.d.ts declare module "@ioc:MyCompany/ImageService" {
import { S3Service } from "services/S3Service"
const imageService: S3Service
export default imageService
}
app/Controllers/Http/ExampleController import { HttpContextContract } from "@ioc:Adonis/Core/HttpContext"
import ImageService from "@ioc:MyCompany/ImageService"
export default class ExampleController {
public async store({ request }: HttpContextContract) {
// Validation ...
if (validated.image?.tmpPath) {
await ImageService.upload(validated.image) // 👈 with Typescript support
}
}
} Because there isn't an official documentation yet, this solution may not be perfect. |
Beta Was this translation helpful? Give feedback.
I've recently faced a similar problem (creating a service provider for S3 file management).
node ace make:provider ImageServiceProvider
to create and register your Service Provider/services
inside the project root). Let's call itS3Service
for now with the following stub:S3Service
class to get res…