how to create new packages for adonis 5 #2378
Replies: 4 comments 5 replies
-
Haven't seen complete guide yet. Here's small one I made: https://mcsneaky.ap3k.pro/posts/adonis-v5-provider/ You can take a look at some relatively simple Adonis module, like Redis or Bouncer and look how they are built |
Beta Was this translation helpful? Give feedback.
-
Hello, I also would like to create new packages that can be installed by the customers on demand like a marketplace approach. These packages need its own controllers, routes, validators, middleware, migrations, ... and has to be external, shareable, plug & play. So they are not pre-registered in the application (.adonisrc.json, kernel.ts, ...) @McSneaky 's post is helpful and I created providers earlier and used them in the controllers but none of them has its own controllers, routes, validators, middleware, migrations, ... It seems this will be covered with package development in the roadmap Could you please share leading information at least about how can this be done? An existing repository to follow is also helpful. Thanks. |
Beta Was this translation helpful? Give feedback.
-
@McSneaky I've managed to have custom routes, controllers and models but they (especially the models) must be created in somewhat inconvenient way.
Sample code below. I just feel that it's not the most accurate way to do it. Besides, I don't understand why ioc resolve doesn't work with external provider the way it works with internal provider. // controller
import { IocContract } from "@ioc:Adonis/Core/Application";
import { HttpContextContract } from "@ioc:Adonis/Core/HttpContext";
import { createUserModel } from "../Models/User";
export default class MyController {
public constructor(private _container: IocContract) { // 👈 Perhaps will also pass Validator object like this
}
public async index() {
const User = await createUserModel(this._container);
return await User.all();
}
public async show(ctx: HttpContextContract) {
const id = ctx.request.param("id");
const User = await createUserModel(this._container);
return await User.findOrFail(id);
}
} // model
import { DateTime } from "luxon";
import { IocContract } from "@ioc:Adonis/Core/Application";
import { column } from "@adonisjs/lucid/build/src/Orm/Decorators";
export async function createUserModel(container: IocContract): Promise<any> { // 👈 Don't know yet how to have proper return type of User
const { BaseModel } = container.use("Adonis/Lucid/Orm");
class User extends BaseModel {
public static table = "users";
@column({ isPrimary: true })
public id: number;
@column.dateTime({ autoCreate: true })
public createdAt: DateTime;
@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime;
}
return User;
} |
Beta Was this translation helpful? Give feedback.
-
Thanks @dqgiang for your help. It really helped to understand the problem I had with the compilation. The solution is precisely the const { iocTransformer } = require("@adonisjs/ioc-transformer");
module.exports = function () {
return iocTransformer(require("typescript/lib/typescript"), { aliases: {} });
}; in your {
....
"compilerOptions": {
....
"plugins": [
{
"transform": "./transformer.js",
"after": true
}
]
}
} Currently TypeScript doesn't support custom transformers in the tsconfig.json, but supports it programmatically. And there is no way to compile your files using custom transformers using tsc command. npm i ttypescript -D Instead of ttsc |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
how can i create new packages in v5. i have seen articles like
https://steemit.com/utopian-io/@creatrixity/building-your-own-adonisjs-package
this but it is for v4 and it does not includes how to use controllers,routes,middleware etc.
is there a complete guide on create packages ? Thanks
Beta Was this translation helpful? Give feedback.
All reactions