@elysiumjs/core 0.4.3 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # @elysiumjs/core
2
2
 
3
- Core functionality for the Elysium framework.
3
+ Core functionalities for the Elysium.js framework.
4
+
5
+ > **NOTE**
6
+ >
7
+ > This package is part of the Elysium.js framework, which is built exclusively for Bun runtime.
4
8
 
5
9
  ## Installation
6
10
 
@@ -8,6 +12,20 @@ Core functionality for the Elysium framework.
8
12
  bun install @elysiumjs/core
9
13
  ```
10
14
 
15
+ ## Features
16
+
17
+ - Dependency Injection
18
+ - HTTP Controllers
19
+ - WebSocket support
20
+ - WAMP protocol integration
21
+ - Command-line interface (`@elysiumjs/styx`), with support for custom commands
22
+ - Event system
23
+ - Caching (memory and Redis)
24
+ - PostgreSQL database support with Drizzle ORM
25
+ - Model classes and repositories for database interactions
26
+ - Multi-tenancy support enabled
27
+ - Swagger documentation
28
+
11
29
  ## Usage
12
30
 
13
31
  Create a controller class:
@@ -39,11 +57,11 @@ export class MainModule extends Module {
39
57
  super();
40
58
  }
41
59
 
42
- public beforeRegister() {
60
+ public async beforeRegister(): Promise<void> {
43
61
  console.log('Module is being registered');
44
62
  }
45
63
 
46
- public afterRegister() {
64
+ public async afterRegister(): Promise<void> {
47
65
  console.log('Module registered successfully');
48
66
  }
49
67
  }
@@ -82,20 +100,11 @@ Start the server:
82
100
  bun run index.ts serve
83
101
  ```
84
102
 
85
- ## Features
103
+ Or, if using `styx`, run:
86
104
 
87
- - Dependency Injection
88
- - HTTP Controllers
89
- - WebSocket support
90
- - WAMP protocol integration
91
- - Command-line interface (`styx`), with support for custom commands
92
- - Event system
93
- - Caching (memory and Redis)
94
- - Worker pool with queues for background jobs (using Bun's Worker API, Redis-based implementation planned)
95
- - PostgreSQL database support with Drizzle ORM
96
- - Model classes and repositories for database interactions
97
- - Multi-tenancy support enabled
98
- - Swagger documentation
105
+ ```bash
106
+ bun styx serve
107
+ ```
99
108
 
100
109
  ## Documentation
101
110
 
@@ -109,4 +118,4 @@ Feel free to open issues for any bugs or questions you may have. Pull requests a
109
118
 
110
119
  ## License
111
120
 
112
- This project is licensed under the Apache License, Version 2.0. See the [LICENSE](../../LICENSE) file for more information.
121
+ This project is licensed under the Apache License, Version 2.0. See the [LICENSE](https://github.com/workbud/elysium/blob/main/LICENSE) file for more information.
package/dist/app.d.ts ADDED
@@ -0,0 +1,180 @@
1
+ import type { ElysiaSwaggerConfig } from '@elysiajs/swagger';
2
+ import type { AnyElysia, ElysiaConfig, ErrorContext, TSchema } from 'elysia';
3
+ import type { CommandClass } from './command';
4
+ import type { DatabaseConnectionProps } from './database';
5
+ import type { Route } from './http';
6
+ import type { ModuleClass } from './module';
7
+ import type { RedisConnectionProps } from './redis';
8
+ import type { WampClientProps } from './wamp';
9
+ import { AsyncLocalStorage } from 'node:async_hooks';
10
+ import { Elysia } from 'elysia';
11
+ import { InteractsWithConsole } from './console';
12
+ /**
13
+ * An application plugin, consisting of a function that returns an Elysia instance.
14
+ * @author Axel Nana <axel.nana@workbud.com>
15
+ */
16
+ export type ElysiumPlugin = (app: Application) => Promise<AnyElysia>;
17
+ /**
18
+ * Properties required when declaring an app using the `@app()` decorator.
19
+ * @author Axel Nana <axel.nana@workbud.com>
20
+ */
21
+ export type AppProps = {
22
+ [key: string]: any;
23
+ /**
24
+ * Enables or disables debug mode.
25
+ */
26
+ debug?: boolean;
27
+ /**
28
+ * The Elysia server configuration.
29
+ */
30
+ server?: ElysiaConfig<Route> & {
31
+ /**
32
+ * The port to listen on.
33
+ */
34
+ port?: number;
35
+ };
36
+ /**
37
+ * The wamp client configuration for the app.
38
+ */
39
+ wamp?: {
40
+ /**
41
+ * The default connection name.
42
+ */
43
+ default: string;
44
+ /**
45
+ * The list of connections.
46
+ */
47
+ connections: Record<string, WampClientProps>;
48
+ };
49
+ /**
50
+ * The list of modules provided the app.
51
+ */
52
+ modules?: ModuleClass[];
53
+ /**
54
+ * The list of CLI commands provided the app.
55
+ */
56
+ commands?: CommandClass[];
57
+ /**
58
+ * The database configuration for the app.
59
+ */
60
+ database?: {
61
+ /**
62
+ * The default connection name.
63
+ */
64
+ default: string;
65
+ /**
66
+ * The list of connections.
67
+ */
68
+ connections: Record<string, DatabaseConnectionProps>;
69
+ };
70
+ /**
71
+ * The redis configuration for the app.
72
+ */
73
+ redis?: {
74
+ /**
75
+ * The default connection name.
76
+ */
77
+ default: string;
78
+ /**
79
+ * The list of connections.
80
+ */
81
+ connections: Record<string, RedisConnectionProps>;
82
+ };
83
+ /**
84
+ * Swagger documentation configuration.
85
+ *
86
+ * Set it to `false` to disable Swagger documentation.
87
+ */
88
+ swagger?: ElysiaSwaggerConfig<Route> | false;
89
+ /**
90
+ * The validation schema for the application environment.
91
+ */
92
+ env?: TSchema;
93
+ /**
94
+ * The list of plugins to load.
95
+ */
96
+ plugins?: ElysiumPlugin[];
97
+ };
98
+ /**
99
+ * Type for the application context.
100
+ * @author Axel Nana <axel.nana@workbud.com>
101
+ */
102
+ export type AppContext = AsyncLocalStorage<Map<'tenant' | 'http:context' | 'db:tx', unknown>>;
103
+ /**
104
+ * Base class for the application main entry.
105
+ * @author Axel Nana <axel.nana@workbud.com>
106
+ */
107
+ export declare abstract class Application extends InteractsWithConsole {
108
+ #private;
109
+ private readonly _appContextStorage;
110
+ /**
111
+ * Gets the running application instance.
112
+ */
113
+ static get instance(): Application;
114
+ /**
115
+ * Gets the application context shared between asynchronous operations.
116
+ */
117
+ static get context(): AppContext;
118
+ /**
119
+ * Marks a class as the application main entry.
120
+ * @param props The decorator options.
121
+ */
122
+ static register(props?: AppProps): ClassDecorator;
123
+ /**
124
+ * Creates a new instance of the application.
125
+ */
126
+ constructor();
127
+ /**
128
+ * Gets the Elysia instance.
129
+ */
130
+ get elysia(): Elysia<Route>;
131
+ /**
132
+ * Gets the debug mode flag.
133
+ */
134
+ get isDebug(): boolean;
135
+ /**
136
+ * Sets the debug mode flag.
137
+ * @param debug The debug mode flag.
138
+ */
139
+ set isDebug(debug: boolean);
140
+ /**
141
+ * Hook that is executed when an error occurs.
142
+ * @param _error The error context.
143
+ * @returns Whether to continue the error propagation.
144
+ */
145
+ protected onError(_error: ErrorContext): Promise<boolean>;
146
+ /**
147
+ * Hook that is executed when the application starts.
148
+ * @param _elysia The Elysia instance.
149
+ */
150
+ protected onStart(_elysia: Elysia<Route>): Promise<void>;
151
+ /**
152
+ * Hook that is executed when the application stops.
153
+ * @param _elysia The Elysia instance.
154
+ */
155
+ protected onStop(_elysia: Elysia<Route>): Promise<void>;
156
+ protected run(): Promise<void>;
157
+ /**
158
+ * Retrieves the configuration value for the given key.
159
+ * @param key The key to retrieve the configuration value for.
160
+ * @returns The configuration value for the given key, or `null` if not found.
161
+ */
162
+ getConfig<T>(key: keyof AppProps): T | null;
163
+ /**
164
+ * Executes a command.
165
+ * @param command The command to execute.
166
+ * @param argv The command line arguments.
167
+ */
168
+ private commandExec;
169
+ /**
170
+ * Starts the server on the specified port.
171
+ */
172
+ private commandServe;
173
+ /**
174
+ * Describes the CLI and available commands.
175
+ */
176
+ private commandDescribe;
177
+ private commandList;
178
+ private commandDescription;
179
+ }
180
+ //# sourceMappingURL=app.d.ts.map
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Interface for a cache storage.
3
+ * @author Axel Nana <axel.nana@workbud.com>
4
+ */
5
+ export interface CacheInterface {
6
+ /**
7
+ * Retrieves the cached data for a given key.
8
+ * @typeParam T The type of the cached data.
9
+ *
10
+ * @param key The key of the cached data.
11
+ */
12
+ get<T>(key: string): Promise<T | null>;
13
+ /**
14
+ * Retrieves the cached data for a given list of keys.
15
+ * @typeParam T The type of the cached data.
16
+ *
17
+ * @param keys The list of cached data keys.
18
+ */
19
+ mget<T>(keys: string[]): Promise<Array<T | null>>;
20
+ /**
21
+ * Gets the TTL of the cached data with the given key.
22
+ * @param key The key of the cached data.
23
+ * @returns The TTL of the cached data, or `null` if the data does not exist.
24
+ */
25
+ ttl(key: string): Promise<number | null>;
26
+ /**
27
+ * Caches data under the given key and an optional TTL.
28
+ * @typeParam T The type of the data to cache.
29
+ *
30
+ * @param key The key of the cached data.
31
+ * @param data The data to cache.
32
+ * @param ttl The optional TTL of the cached data. If not defined, the data is cached forever.
33
+ * @returns The cached data.
34
+ */
35
+ set<T>(key: string, data: T, ttl?: number): Promise<T>;
36
+ /**
37
+ * Caches data under the given list of keys and an optional TTL.
38
+ * @typeParam T The type of the data to cache.
39
+ *
40
+ * @param data The list of data to cache.
41
+ * @returns The list of cached data.
42
+ */
43
+ mset<T>(data: Array<{
44
+ key: string;
45
+ value: T;
46
+ ttl?: number;
47
+ }>): Promise<Array<{
48
+ key: string;
49
+ value: T;
50
+ ttl?: number;
51
+ }>>;
52
+ /**
53
+ * Deletes the cached data with the given key.
54
+ * @typeParam T The type of the data to cache.
55
+ *
56
+ * @param key The key of the cached data.
57
+ * @returns `true` if the data was deleted, `false` otherwise.
58
+ */
59
+ del(key: string): Promise<boolean>;
60
+ /**
61
+ * Deletes the cached data with the given list of keys.
62
+ * @typeParam T The type of the data to cache.
63
+ *
64
+ * @param keys The list of cached data keys.
65
+ * @returns `true` if the data was deleted, `false` otherwise.
66
+ */
67
+ mdel(keys: string[]): Promise<boolean>;
68
+ /**
69
+ * Deletes all cached data.
70
+ * @returns `true` if the data was deleted, `false` otherwise.
71
+ */
72
+ clear(): Promise<boolean>;
73
+ /**
74
+ * Creates a sub-cache with the given tags.
75
+ * @param tags The list of tags for the sub-cache.
76
+ */
77
+ tags(...tags: string[]): Omit<CacheInterface, 'tags'>;
78
+ }
79
+ export declare abstract class Cache {
80
+ #private;
81
+ /**
82
+ * A weak cache storage for simple non-intrusive caching of data.
83
+ * @author Axel Nana <axel.nana@workbud.com>
84
+ */
85
+ static readonly weak: {
86
+ /**
87
+ * INTERNAL USAGE ONLY.
88
+ * @private
89
+ */
90
+ map: WeakMap<object, unknown>;
91
+ /**
92
+ * Gets the cached data for a given context.
93
+ * @param ctx The cache context.
94
+ * @returns The cached data.
95
+ */
96
+ get<T extends object>(ctx: object): T;
97
+ /**
98
+ * Deletes the cached data for a given context.
99
+ * @param ctx The cache context.
100
+ */
101
+ del(ctx: object): void;
102
+ };
103
+ /**
104
+ * Redis-based cache storage.
105
+ * @author Axel Nana <axel.nana@workbud.com>
106
+ */
107
+ static get redis(): CacheInterface;
108
+ /**
109
+ * Memory-based cache storage.
110
+ * @author Axel Nana <axel.nana@workbud.com>
111
+ */
112
+ static get memory(): CacheInterface;
113
+ }
114
+ //# sourceMappingURL=cache.d.ts.map
@@ -0,0 +1,273 @@
1
+ import type { Class } from 'type-fest';
2
+ import { InteractsWithConsole } from './console';
3
+ /**
4
+ * Supported command argument types.
5
+ * @author Axel Nana <axel.nana@workbud.com>
6
+ */
7
+ export declare enum CommandArgumentType {
8
+ /**
9
+ * String argument.
10
+ */
11
+ STRING = "string",
12
+ /**
13
+ * Number argument.
14
+ */
15
+ NUMBER = "number",
16
+ /**
17
+ * Boolean argument.
18
+ */
19
+ BOOLEAN = "boolean",
20
+ /**
21
+ * Enum argument.
22
+ */
23
+ ENUM = "enum",
24
+ /**
25
+ * Array argument.
26
+ */
27
+ ARRAY = "array"
28
+ }
29
+ /**
30
+ * Properties required when declaring a command argument.
31
+ * @author Axel Nana <axel.nana@workbud.com>
32
+ *
33
+ * These properties are used to define command arguments using the `@arg()` decorator. The
34
+ * values of these properties are used to generate the help message for the command.
35
+ */
36
+ export type CommandArgumentProps = {
37
+ /**
38
+ * The name of the argument.
39
+ */
40
+ name: string;
41
+ /**
42
+ * The description of the argument.
43
+ */
44
+ description?: string;
45
+ /**
46
+ * Whether the argument is required.
47
+ */
48
+ required?: boolean;
49
+ /**
50
+ * The default value for the argument.
51
+ */
52
+ default?: any;
53
+ /**
54
+ * The type of the argument.
55
+ */
56
+ type?: CommandArgumentType;
57
+ /**
58
+ * The list of allowed values for the argument.
59
+ */
60
+ enum?: string[];
61
+ /**
62
+ * The type of elements in the array.
63
+ */
64
+ arrayType?: CommandArgumentType;
65
+ };
66
+ /**
67
+ * Metadata for storing command arguments.
68
+ * @author Axel Nana <axel.nana@workbud.com>
69
+ */
70
+ type CommandArgumentMetadata = CommandArgumentProps & {
71
+ /**
72
+ * The name of the class property that the argument is associated with.
73
+ */
74
+ propertyKey: string;
75
+ };
76
+ /**
77
+ * Type for a command class.
78
+ * @author Axel Nana <axel.nana@workbud.com>
79
+ */
80
+ export type CommandClass = Class<Command> & {
81
+ /**
82
+ * The name of the command.
83
+ */
84
+ readonly command: string;
85
+ /**
86
+ * The description of the command.
87
+ */
88
+ readonly description: string;
89
+ };
90
+ /**
91
+ * Base class for commands.
92
+ *
93
+ * Commands are classes that can be executed through the CLI. They can have arguments
94
+ * that can be passed to them using the `--` syntax.
95
+ *
96
+ * @author Axel Nana <axel.nana@workbud.com>
97
+ */
98
+ export declare abstract class Command extends InteractsWithConsole {
99
+ /**
100
+ * The name of the command.
101
+ *
102
+ * This is used to identify the command in the CLI when using the `exec` command.
103
+ */
104
+ static readonly command: string;
105
+ /**
106
+ * The description of the command.
107
+ *
108
+ * This is displayed when displaying help messages for the command.
109
+ */
110
+ static readonly description: string;
111
+ /**
112
+ * Decorator for registering a command argument.
113
+ * @author Axel Nana <axel.nana@workbud.com>
114
+ * @param name The name of the argument.
115
+ * @param props Additional options for the argument.
116
+ */
117
+ static arg(props?: Partial<CommandArgumentProps>): PropertyDecorator;
118
+ /**
119
+ * The command entry point. This method is called when the command is executed using the CLI.
120
+ */
121
+ abstract run(): Promise<void>;
122
+ /**
123
+ * Initialize the command before it is executed. This method is called before the `run()` method.
124
+ * and is used to validate the arguments and initialize the command.
125
+ * @param argv The command line arguments.
126
+ * @returns Whether the command was initialized successfully.
127
+ */
128
+ init(...argv: string[]): Promise<boolean>;
129
+ /**
130
+ * Get all registered arguments for this command.
131
+ * @returns The list of registered arguments.
132
+ */
133
+ protected getArguments(): CommandArgumentMetadata[];
134
+ /**
135
+ * Generate help text for the command.
136
+ * @returns Formatted help text.
137
+ */
138
+ help(): Promise<string>;
139
+ /**
140
+ * Validate provided arguments against registered arguments.
141
+ * @param args Arguments to validate.
142
+ * @returns Object with validation result and missing required args if any.
143
+ */
144
+ protected validateArgs(args: Record<string, any>): {
145
+ valid: boolean;
146
+ missing: string[];
147
+ };
148
+ /**
149
+ * Process command line arguments and apply default values.
150
+ * @param argv The command line arguments.
151
+ * @returns Processed arguments with defaults applied.
152
+ */
153
+ protected processArgs(argv: string[]): Record<string, any>;
154
+ /**
155
+ * Start a progress bar in the console.
156
+ * @param total The total number of items to process.
157
+ * @param title Optional title for the progress bar.
158
+ */
159
+ protected progress(total: number, title?: string): CommandProgressBar;
160
+ /**
161
+ * Start a spinner in the console.
162
+ * @param title Optional title for the spinner.
163
+ * @param frames Optional custom frames for the spinner animation.
164
+ * @param frameDelay Optional delay between frames in milliseconds.
165
+ */
166
+ protected spinner(title?: string, frames?: string[], frameDelay?: number): CommandSpinner;
167
+ /**
168
+ * Infer the type of an argument based on its default value.
169
+ * @param defaultValue The default value.
170
+ * @returns The inferred argument type.
171
+ */
172
+ private inferType;
173
+ }
174
+ /**
175
+ * A class for managing and displaying a progress bar in the console.
176
+ * @author Axel Nana <axel.nana@workbud.com>
177
+ */
178
+ declare class CommandProgressBar extends InteractsWithConsole {
179
+ #private;
180
+ /**
181
+ * Create a new progress bar instance.
182
+ * @param total The total number of items to process.
183
+ * @param title Optional title for the progress bar.
184
+ */
185
+ constructor(total?: number, title?: string);
186
+ /**
187
+ * Update the progress bar with a new value.
188
+ * @param increment The number of items to increment by (default: 1).
189
+ * @param newTitle Optional new title for the progress bar.
190
+ */
191
+ update(increment?: number, newTitle?: string): void;
192
+ /**
193
+ * Complete the progress bar and reset its state.
194
+ * @param message Optional completion message to display.
195
+ */
196
+ complete(message?: string): void;
197
+ /**
198
+ * Pause the progress bar temporarily to display a message.
199
+ * @param message The message to display.
200
+ */
201
+ pause(message: string): void;
202
+ /**
203
+ * Check if the progress bar is currently active.
204
+ * @returns True if the progress bar is active, false otherwise.
205
+ */
206
+ isActive(): boolean;
207
+ /**
208
+ * Get the current progress value.
209
+ * @returns The current progress value.
210
+ */
211
+ getCurrent(): number;
212
+ /**
213
+ * Get the total progress value.
214
+ * @returns The total progress value.
215
+ */
216
+ getTotal(): number;
217
+ /**
218
+ * Get the current progress percentage.
219
+ * @returns The current progress percentage (0-100).
220
+ */
221
+ getPercentage(): number;
222
+ }
223
+ /**
224
+ * A class for displaying a spinner in the console for indeterminate operations.
225
+ * @author Axel Nana <axel.nana@workbud.com>
226
+ */
227
+ export declare class CommandSpinner extends InteractsWithConsole {
228
+ #private;
229
+ /**
230
+ * Create a new spinner instance.
231
+ * @param title Optional title for the spinner.
232
+ * @param frames Optional custom frames for the spinner animation.
233
+ * @param frameDelay Optional delay between frames in milliseconds.
234
+ */
235
+ constructor(title?: string, frames?: string[], frameDelay?: number);
236
+ /**
237
+ * Update the spinner with a new title.
238
+ * @param newTitle New title for the spinner.
239
+ */
240
+ update(newTitle: string): void;
241
+ /**
242
+ * Complete the spinner and reset its state.
243
+ * @param message Optional completion message to display.
244
+ * @param success Whether the operation was successful (affects the symbol shown).
245
+ */
246
+ complete(message?: string, success?: boolean): void;
247
+ /**
248
+ * Fail the spinner and reset its state.
249
+ * @param message Optional failure message to display.
250
+ */
251
+ fail(message?: string): void;
252
+ /**
253
+ * Pause the spinner temporarily to display a message.
254
+ * @param message The message to display.
255
+ */
256
+ pause(message: string): void;
257
+ /**
258
+ * Check if the spinner is currently active.
259
+ * @returns True if the spinner is active, false otherwise.
260
+ */
261
+ isActive(): boolean;
262
+ /**
263
+ * Get the elapsed time since the spinner started.
264
+ * @returns The elapsed time in seconds.
265
+ */
266
+ getElapsedTime(): number;
267
+ /**
268
+ * Render the current frame of the spinner.
269
+ */
270
+ private render;
271
+ }
272
+ export {};
273
+ //# sourceMappingURL=command.d.ts.map