@elysiumjs/core 0.4.3 → 0.5.1
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 +26 -17
- package/dist/app.d.ts +180 -0
- package/dist/cache.d.ts +113 -0
- package/dist/command.d.ts +272 -0
- package/dist/console.d.ts +166 -0
- package/dist/database.d.ts +112 -0
- package/dist/env.d.ts +33 -0
- package/dist/event.d.ts +93 -0
- package/dist/http.d.ts +246 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +19 -0
- package/dist/job.d.ts +105 -0
- package/dist/macros.d.ts +8 -0
- package/dist/middleware.d.ts +69 -0
- package/dist/model.d.ts +4802 -0
- package/dist/module.d.ts +44 -0
- package/dist/redis.d.ts +133 -0
- package/dist/repository.d.ts +163 -0
- package/dist/service.d.ts +131 -0
- package/dist/utils.d.ts +25 -0
- package/dist/wamp.d.ts +272 -0
- package/dist/websocket.d.ts +91 -0
- package/dist/worker.d.ts +219 -0
- package/package.json +16 -9
- package/tsconfig.json +34 -33
- package/index.ts +0 -54
- package/macros.ts +0 -16
- package/src/app.ts +0 -647
- package/src/cache.ts +0 -218
- package/src/command.ts +0 -908
- package/src/console.ts +0 -493
- package/src/database.ts +0 -257
- package/src/env.ts +0 -62
- package/src/event.ts +0 -227
- package/src/http.ts +0 -736
- package/src/job.ts +0 -196
- package/src/middleware.ts +0 -164
- package/src/model.ts +0 -386
- package/src/module.ts +0 -108
- package/src/redis.ts +0 -408
- package/src/repository.ts +0 -295
- package/src/service.ts +0 -351
- package/src/utils.ts +0 -95
- package/src/wamp.ts +0 -569
- package/src/websocket.ts +0 -191
- package/src/worker.ts +0 -849
- package/types.d.ts +0 -35
package/src/model.ts
DELETED
|
@@ -1,386 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import type { BuildExtraConfigColumns } from 'drizzle-orm';
|
|
16
|
-
import type {
|
|
17
|
-
PgColumn,
|
|
18
|
-
PgColumnBuilderBase,
|
|
19
|
-
PgSchema,
|
|
20
|
-
PgTable,
|
|
21
|
-
PgTableExtraConfigValue
|
|
22
|
-
} from 'drizzle-orm/pg-core';
|
|
23
|
-
import type { TSchema } from 'elysia';
|
|
24
|
-
|
|
25
|
-
import { getTableConfig, pgSchema, pgTable } from 'drizzle-orm/pg-core';
|
|
26
|
-
import { t } from 'elysia';
|
|
27
|
-
|
|
28
|
-
import { Application } from './app';
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Creates a validation schema from a Drizzle table.
|
|
32
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
33
|
-
* @param table The table to create the schema from.
|
|
34
|
-
* @param options The schema generation options.
|
|
35
|
-
* @returns A bew validation schema based on the given table.
|
|
36
|
-
*/
|
|
37
|
-
export const createSchemaFromDrizzle = (
|
|
38
|
-
table: PgTable,
|
|
39
|
-
{ mode = 'select' }: { mode?: 'create' | 'update' | 'select' } = {}
|
|
40
|
-
) => {
|
|
41
|
-
const { columns } = getTableConfig(table);
|
|
42
|
-
if (columns === undefined) return t.Object({});
|
|
43
|
-
|
|
44
|
-
const properties: { [key: string]: TSchema } = {};
|
|
45
|
-
|
|
46
|
-
for (const element of columns) {
|
|
47
|
-
if (['create', 'update'].includes(mode) && element.primary) {
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
properties[element.name] = parseTypes(element);
|
|
52
|
-
|
|
53
|
-
if (!element.notNull) {
|
|
54
|
-
properties[element.name] = t.Nullable(properties[element.name]);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (mode === 'update' || element.hasDefault) {
|
|
58
|
-
properties[element.name] = t.Optional(properties[element.name]);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return t.Object(properties);
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Creates a validation schema from a Drizzle column.
|
|
67
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
68
|
-
* @param element The Drizzle column.
|
|
69
|
-
* @returns A validation schema based on the given column.
|
|
70
|
-
*/
|
|
71
|
-
const parseTypes = (element: PgColumn) => {
|
|
72
|
-
let type = (() => {
|
|
73
|
-
switch (element.dataType) {
|
|
74
|
-
case 'string':
|
|
75
|
-
return t.String({
|
|
76
|
-
format: element.columnType === 'PgUUID' ? 'uuid' : undefined
|
|
77
|
-
});
|
|
78
|
-
case 'number':
|
|
79
|
-
return t.Number();
|
|
80
|
-
case 'boolean':
|
|
81
|
-
return t.Boolean();
|
|
82
|
-
case 'array':
|
|
83
|
-
return t.Array(t.Any());
|
|
84
|
-
case 'json':
|
|
85
|
-
return t.Object({});
|
|
86
|
-
case 'date':
|
|
87
|
-
case 'localDate':
|
|
88
|
-
case 'localDateTime':
|
|
89
|
-
return t.Date();
|
|
90
|
-
case 'bigint':
|
|
91
|
-
return t.BigInt();
|
|
92
|
-
case 'buffer':
|
|
93
|
-
return t.File();
|
|
94
|
-
default:
|
|
95
|
-
return t.Never();
|
|
96
|
-
}
|
|
97
|
-
})();
|
|
98
|
-
|
|
99
|
-
if (!element.notNull) {
|
|
100
|
-
type = t.Optional(type);
|
|
101
|
-
} else {
|
|
102
|
-
type = t.Required(type);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return type;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Type of a repository class.
|
|
110
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
111
|
-
* @template TColumnsMap The table columns config.
|
|
112
|
-
* @template TTable The drizzle's table schema wrapped by the repository.
|
|
113
|
-
*/
|
|
114
|
-
export type ModelClass<
|
|
115
|
-
TTableName extends string,
|
|
116
|
-
TColumnsMap extends Record<string, PgColumnBuilderBase>,
|
|
117
|
-
TTable extends PgTable = ReturnType<typeof pgTable<TTableName, TColumnsMap>>
|
|
118
|
-
> = {
|
|
119
|
-
/**
|
|
120
|
-
* The data type returned by the select queries.
|
|
121
|
-
*/
|
|
122
|
-
readonly $inferSelect: TTable['$inferSelect'];
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* The data type needed by the insert queries.
|
|
126
|
-
*/
|
|
127
|
-
readonly $inferInsert: TTable['$inferInsert'];
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* The data type needed by the update queries.
|
|
131
|
-
*/
|
|
132
|
-
readonly $inferUpdate: Partial<TTable['$inferSelect']>;
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* The drizzle's table schema wrapped by this model.
|
|
136
|
-
* Will be automatically wrapped by the `Tenancy.withTenant()` function when the current tenant is not `public`.
|
|
137
|
-
*/
|
|
138
|
-
readonly table: TTable;
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* The name of the table wrapped by the model.
|
|
142
|
-
*/
|
|
143
|
-
readonly tableName: TTableName;
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* The table columns configuration.
|
|
147
|
-
*/
|
|
148
|
-
readonly columns: TColumnsMap;
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* The extra configuration for the table.
|
|
152
|
-
*/
|
|
153
|
-
readonly extraConfig?: (
|
|
154
|
-
self: BuildExtraConfigColumns<TTableName, TColumnsMap, 'pg'>
|
|
155
|
-
) => PgTableExtraConfigValue[];
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* The validation schema for creating records.
|
|
159
|
-
*/
|
|
160
|
-
readonly insertSchema: TSchema;
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* The validation schema for updating records.
|
|
164
|
-
*/
|
|
165
|
-
readonly updateSchema: TSchema;
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* The validation schema for selecting records.
|
|
169
|
-
*/
|
|
170
|
-
readonly selectSchema: TSchema;
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Whether the model supports tenancy.
|
|
174
|
-
*
|
|
175
|
-
* Set it to `true` if the model supports tenancy. This means that the model will be
|
|
176
|
-
* wrapped by the `Tenancy.withTenant()` function when the current tenant is not `public`.
|
|
177
|
-
*/
|
|
178
|
-
readonly supportTenancy: boolean;
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Creates a new instance of the model.
|
|
182
|
-
* @param args The arguments to pass to the constructor.
|
|
183
|
-
* @returns A new instance of the model.
|
|
184
|
-
*/
|
|
185
|
-
new (...args: unknown[]): unknown;
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Mixin used to create a new model class.
|
|
190
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
191
|
-
* @template TColumnsMap The table columns config.
|
|
192
|
-
* @param tableName The name of the table.
|
|
193
|
-
* @param columns The table columns configuration.
|
|
194
|
-
*/
|
|
195
|
-
export const Model = <
|
|
196
|
-
TTableName extends string,
|
|
197
|
-
TColumnsMap extends Record<string, PgColumnBuilderBase>
|
|
198
|
-
>(
|
|
199
|
-
tableName: TTableName,
|
|
200
|
-
columns: TColumnsMap,
|
|
201
|
-
extraConfig?: (
|
|
202
|
-
self: BuildExtraConfigColumns<TTableName, TColumnsMap, 'pg'>
|
|
203
|
-
) => PgTableExtraConfigValue[]
|
|
204
|
-
) => {
|
|
205
|
-
const table = pgTable(tableName, columns, extraConfig);
|
|
206
|
-
|
|
207
|
-
class M {
|
|
208
|
-
/**
|
|
209
|
-
* The data type returned by the select queries.
|
|
210
|
-
*/
|
|
211
|
-
public static readonly $inferSelect = table.$inferSelect;
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* The data type needed by the insert queries.
|
|
215
|
-
*/
|
|
216
|
-
public static readonly $inferInsert = table.$inferInsert;
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* The data type needed by the update queries.
|
|
220
|
-
*/
|
|
221
|
-
public static readonly $inferUpdate: Partial<typeof table.$inferInsert> = table.$inferInsert;
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* The drizzle's table schema wrapped by this model.
|
|
225
|
-
*/
|
|
226
|
-
public static get table(): ReturnType<typeof pgTable<TTableName, TColumnsMap>> {
|
|
227
|
-
// If we are not inside an Application context, we can't use the tenancy system
|
|
228
|
-
if (!Application.instance || !Application.context.getStore) {
|
|
229
|
-
return table;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
if (this.supportTenancy) {
|
|
233
|
-
const tenant = Tenancy.getCurrentTenant() ?? 'public';
|
|
234
|
-
// @ts-expect-error typeof this strangely doesn't match the ModelClass type
|
|
235
|
-
return Tenancy.wrapTenant(tenant, this);
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
return table;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* The name of the table wrapped by the model.
|
|
243
|
-
*/
|
|
244
|
-
public static readonly tableName: string = tableName;
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* The table columns configuration.
|
|
248
|
-
*/
|
|
249
|
-
public static readonly columns: TColumnsMap = columns;
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* The extra configuration for the table.
|
|
253
|
-
*/
|
|
254
|
-
public static readonly extraConfig = extraConfig;
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* The validation schema for creating records.
|
|
258
|
-
*/
|
|
259
|
-
public static readonly insertSchema = createSchemaFromDrizzle(table, { mode: 'create' });
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* The validation schema for updating records.
|
|
263
|
-
*/
|
|
264
|
-
public static readonly updateSchema = createSchemaFromDrizzle(table, { mode: 'update' });
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* The validation schema for selecting records.
|
|
268
|
-
*/
|
|
269
|
-
public static readonly selectSchema = createSchemaFromDrizzle(table, { mode: 'select' });
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Whether the model supports tenancy.
|
|
273
|
-
*
|
|
274
|
-
* Set it to `true` if the model supports tenancy. This means that the model will be
|
|
275
|
-
* wrapped by the `Tenancy.withTenant()` function when the current tenant is not `public`.
|
|
276
|
-
*/
|
|
277
|
-
public static readonly supportTenancy: boolean = false;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
return M;
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* A map of tables registered for each tenant.
|
|
285
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
286
|
-
*/
|
|
287
|
-
const tenantSchemas: Map<string, PgTable> = new Map();
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* A map of known tenant schemas.
|
|
291
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
292
|
-
*/
|
|
293
|
-
const schemaRegistry: Map<string, PgSchema> = new Map();
|
|
294
|
-
|
|
295
|
-
export namespace Tenancy {
|
|
296
|
-
/**
|
|
297
|
-
* Gets the schema for the given tenant.
|
|
298
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
299
|
-
* @param tenant The name of the tenant.
|
|
300
|
-
* @returns The tenant schema for the given tenant.
|
|
301
|
-
*/
|
|
302
|
-
const getTenantSchema = (tenant: string) => {
|
|
303
|
-
return schemaRegistry.get(tenant) ?? registerTenant(tenant);
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* Gets the name of the current tenant.
|
|
308
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
309
|
-
* @returns The name of the current tenant, or `null` if no tenant is set.
|
|
310
|
-
*/
|
|
311
|
-
export const getCurrentTenant = (): string | null => {
|
|
312
|
-
const store = Application.context.getStore();
|
|
313
|
-
return (store?.get('tenant') ?? null) as string | null;
|
|
314
|
-
};
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* Sets the current tenant.
|
|
318
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
319
|
-
* @template T The type of the model class to wrap with the tenant.
|
|
320
|
-
* @template TColumnsMap The table columns config.
|
|
321
|
-
* @param tenant The name of the tenant.
|
|
322
|
-
* @param model The model class that wraps the `PgTable`.
|
|
323
|
-
* @returns The drizzle's table schema wrapped by the tenant.
|
|
324
|
-
*/
|
|
325
|
-
export const wrapTenant = <
|
|
326
|
-
T extends ModelClass<TTableName, TColumnsMap>,
|
|
327
|
-
TTableName extends string = T extends ModelClass<infer TTableName, infer TColumnsMap>
|
|
328
|
-
? TTableName
|
|
329
|
-
: string,
|
|
330
|
-
TColumnsMap extends Record<string, PgColumnBuilderBase> = T extends ModelClass<
|
|
331
|
-
TTableName,
|
|
332
|
-
infer TColumnsMap
|
|
333
|
-
>
|
|
334
|
-
? TColumnsMap
|
|
335
|
-
: Record<string, PgColumnBuilderBase>
|
|
336
|
-
>(
|
|
337
|
-
tenant: string,
|
|
338
|
-
model: T
|
|
339
|
-
): ReturnType<typeof pgTable<TTableName, TColumnsMap>> => {
|
|
340
|
-
if (tenant === 'public') {
|
|
341
|
-
return pgTable(model.tableName, model.columns);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
const tableName = `${tenant}.${model.tableName}`;
|
|
345
|
-
if (tenantSchemas.has(tableName)) {
|
|
346
|
-
return tenantSchemas.get(tableName)! as ReturnType<typeof pgTable<TTableName, TColumnsMap>>;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
const tenantSchema = getTenantSchema(tenant);
|
|
350
|
-
const schemaTable = tenantSchema.table(model.tableName, model.columns);
|
|
351
|
-
tenantSchemas.set(tableName, schemaTable);
|
|
352
|
-
|
|
353
|
-
return schemaTable as unknown as ReturnType<typeof pgTable<TTableName, TColumnsMap>>;
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
|
-
* Registers a new tenant schema.
|
|
358
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
359
|
-
* @param tenant The name of the tenant.
|
|
360
|
-
* @returns A new schema for the given tenant.
|
|
361
|
-
*/
|
|
362
|
-
export const registerTenant = (tenant: string) => {
|
|
363
|
-
if (schemaRegistry.has(tenant)) return schemaRegistry.get(tenant)!;
|
|
364
|
-
|
|
365
|
-
const tenantSchema = pgSchema(tenant);
|
|
366
|
-
schemaRegistry.set(tenant, tenantSchema);
|
|
367
|
-
|
|
368
|
-
return tenantSchema;
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* Runs the given callback in a context with the given tenant.
|
|
373
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
374
|
-
* @param tenant The name of the tenant.
|
|
375
|
-
* @param callback The callback to run.
|
|
376
|
-
* @returns The result of the callback.
|
|
377
|
-
*/
|
|
378
|
-
export const withTenant = <TReturn>(tenant: string, callback: () => TReturn): TReturn => {
|
|
379
|
-
const currentStore = Application.context.getStore();
|
|
380
|
-
|
|
381
|
-
const newStore = new Map(currentStore);
|
|
382
|
-
newStore.set('tenant', tenant);
|
|
383
|
-
|
|
384
|
-
return Application.context.run(newStore, callback);
|
|
385
|
-
};
|
|
386
|
-
}
|
package/src/module.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025-present Workbud Technologies Inc. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import type { Class } from 'type-fest';
|
|
16
|
-
import type { ElysiaApp, Route } from './http';
|
|
17
|
-
|
|
18
|
-
import { Elysia } from 'elysia';
|
|
19
|
-
import { assign } from 'radash';
|
|
20
|
-
|
|
21
|
-
import { applyMiddlewares } from './middleware';
|
|
22
|
-
import { nextTick, Symbols } from './utils';
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Properties required when declaring a module using the `@module()` decorator.
|
|
26
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
27
|
-
*/
|
|
28
|
-
export type ModuleProps = {
|
|
29
|
-
/**
|
|
30
|
-
* The list of controllers provided by the module.
|
|
31
|
-
*/
|
|
32
|
-
controllers?: Class<any>[];
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* The base path for all the controllers registered by this module.
|
|
36
|
-
* This value will be prefixed to each controllers path.
|
|
37
|
-
*/
|
|
38
|
-
prefix?: Route;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Type for a module class.
|
|
43
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
44
|
-
*/
|
|
45
|
-
export type ModuleClass = Class<Module>;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Base class for all modules.
|
|
49
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
50
|
-
*
|
|
51
|
-
* This class provides base features for modules, such as hooks and lifecycle methods.
|
|
52
|
-
*/
|
|
53
|
-
export abstract class Module {
|
|
54
|
-
/**
|
|
55
|
-
* Marks a class as a module.
|
|
56
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
57
|
-
* @param options The decorator options.
|
|
58
|
-
*/
|
|
59
|
-
public static register(options: ModuleProps = {}) {
|
|
60
|
-
return function (target: ModuleClass) {
|
|
61
|
-
async function handleModule(m: Module): Promise<ElysiaApp> {
|
|
62
|
-
// TODO: Use the logger service here
|
|
63
|
-
console.log(`Registering module ${target.name}`);
|
|
64
|
-
await nextTick();
|
|
65
|
-
|
|
66
|
-
const props = assign({ controllers: [] }, options) as Required<ModuleProps>;
|
|
67
|
-
|
|
68
|
-
const plugin: ElysiaApp = new Elysia({ name: target.name, prefix: options.prefix });
|
|
69
|
-
plugin.resolve({ as: 'local' }, () => ({ module: m }));
|
|
70
|
-
|
|
71
|
-
const middlewares = Reflect.getMetadata(Symbols.middlewares, target) ?? [];
|
|
72
|
-
applyMiddlewares(middlewares, plugin);
|
|
73
|
-
|
|
74
|
-
for (const controller of props.controllers) {
|
|
75
|
-
const app = Reflect.getMetadata(Symbols.elysiaPlugin, controller);
|
|
76
|
-
if (app === undefined) {
|
|
77
|
-
// TODO: Use the logger service here
|
|
78
|
-
console.error(
|
|
79
|
-
`Invalid controller class ${controller.name} registered in module ${target.name}. Ensure that you either used the @Http.controller(), the @Websocket.controller(), or the @Wamp.controller() decorators on the class.`
|
|
80
|
-
);
|
|
81
|
-
process.exit(1);
|
|
82
|
-
} else {
|
|
83
|
-
// TODO: Add middlewares here
|
|
84
|
-
plugin.use(await app());
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return plugin;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
Reflect.defineMetadata(Symbols.elysiaPlugin, handleModule, target);
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Hooks that are executed before the module is registered.
|
|
97
|
-
*/
|
|
98
|
-
public beforeRegister(): Promise<void> {
|
|
99
|
-
return Promise.resolve();
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Hooks that are executed after the module is registered.
|
|
104
|
-
*/
|
|
105
|
-
public afterRegister(): Promise<void> {
|
|
106
|
-
return Promise.resolve();
|
|
107
|
-
}
|
|
108
|
-
}
|