Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decorate unexpectedly overwrites decorators everywhere #647

Open
Hetch3t opened this issue May 21, 2024 · 1 comment
Open

decorate unexpectedly overwrites decorators everywhere #647

Hetch3t opened this issue May 21, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@Hetch3t
Copy link

Hetch3t commented May 21, 2024

What version of Elysia.JS is running?

1.0.20

What platform is your computer?

Darwin 23.1.0 x86_64 i386

What steps can reproduce the bug?

If I have Controller factory as follows, the decorate doesn't work as expected - it gets overwritten every time I run the factory:

// controller.factory.ts

import Elysia, { t } from "elysia";

import { capitalize } from "./common";
import { transformValidation } from "./expand";
import ConsoleLogger from "./modules/Logger";

export default abstract class Controller {

	static init<P extends string, S extends Record<string, any>>({
		prefix,
		name = `${capitalize(prefix.split("/")[1])}Controller`,
		services = {} as S,
	}: {
		readonly prefix    : P;
		readonly name?     : string;
		readonly services? : S;
	}) {
		return new Elysia({ prefix, name, seed: { name, prefix } })
			.decorate({
				logger   : new ConsoleLogger(name),
				services,
			});
	}

}
// controller.factory.ts

		// Also tried
		return new Elysia({ prefix, name, seed: { name, prefix } })
			.decorate(decorators => {
				return {
					logger   : new ConsoleLogger(name),
					...decorators,
					services : { ...decorators.services, ...services },
				};
			});

Example controllers:

// accounts.controller.ts

export const AccountsController = Controller.init({
	prefix   : "/accounts",
	services : { accountsService: new AccountsService() },
})
	.get(
		"/",
		async ({ logger, services: { accountsService } }) => {
			logger.log('GET /tag-groups');

			const accounts = await accountsService.findAll();

			return accounts;
		}
	);
// tag-groups.controller.ts

export const TagGroupsController = Controller.init({
	prefix   : "/tag-groups",
	services : { tagGroupsService: new TagGroupsService() },
})
	.get(
		"/",
		async ({ logger, services: { tagGroupsService } }) => {
			logger.log('GET /tag-groups');
			
			const tagGroups = await tagGroupsService.findAll();

			return tagGroups;
		}
	);

Server start:

// index.ts

const app = new Elysia()
	.group("/api", api =>
		api
			.use(AccountsController)
			.use(TagGroupsController)
	)
	.listen(Config.PORT);

console.log(`🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}`);

So with this setup:

  • ctx.logger gets overwritten by the last controller (so AccountsController will have TagGroupsController's logger)
  • ctx.services get overwritten by the last controller in both approaches (direct one and with destruct of the previous state), so ctx.services.accountsService is always undefined, it's only tagGroupsService there
  • TypeScript types work correctly

How can I deal with that? There is no scope setting for decorate. Everything works fine with derive, but it's weird to have static services / logger creation on every request. What am I doing wrong? Any help is appreciated.

What is the expected behavior?

Expected to have correct logger instance and correct services object in context

What do you see instead?

I see last used controller's logger and last used controller services

Additional information

No response

@Hetch3t Hetch3t added the bug Something isn't working label May 21, 2024
@Hetch3t
Copy link
Author

Hetch3t commented Jun 8, 2024

Maybe there is some workaround? Currently I struggle to find one, even putting everything to store/state doesn't help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant