@radatek/microserver 2.3.2 → 2.3.3
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/microserver.d.ts +14 -5
- package/microserver.js +30 -14
- package/package.json +1 -1
package/microserver.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MicroServer
|
|
3
|
-
* @version 2.3.
|
|
3
|
+
* @version 2.3.3
|
|
4
4
|
* @package @radatek/microserver
|
|
5
5
|
* @copyright Darius Kisonas 2022
|
|
6
6
|
* @license MIT
|
|
@@ -199,8 +199,8 @@ export declare class WebSocket extends EventEmitter {
|
|
|
199
199
|
* ```
|
|
200
200
|
*/
|
|
201
201
|
export declare class Controller<T extends Model<any> = any> {
|
|
202
|
-
|
|
203
|
-
|
|
202
|
+
req: ServerRequest<T>;
|
|
203
|
+
res: ServerResponse<T>;
|
|
204
204
|
get model(): T | undefined;
|
|
205
205
|
constructor(req: ServerRequest<T>, res: ServerResponse<T>);
|
|
206
206
|
/** Generate routes for this controller */
|
|
@@ -733,13 +733,22 @@ export type ModelDocument<T extends ModelSchema> = {
|
|
|
733
733
|
export declare interface ModelCollections {
|
|
734
734
|
collection(name: string): Promise<MicroCollection>;
|
|
735
735
|
}
|
|
736
|
+
export declare class Models {
|
|
737
|
+
}
|
|
736
738
|
export declare class Model<TSchema extends ModelSchema> {
|
|
737
739
|
static collections: ModelCollections;
|
|
738
|
-
static models:
|
|
740
|
+
static models: Models;
|
|
739
741
|
static set db(db: any);
|
|
740
742
|
static get db(): any;
|
|
743
|
+
/** Dynamic model extension */
|
|
744
|
+
static dynamic<T extends Model<any>>(model: T, options: {
|
|
745
|
+
controller?: Controller;
|
|
746
|
+
collection?: MicroCollection<any>;
|
|
747
|
+
req?: ServerRequest;
|
|
748
|
+
} | Controller): T;
|
|
749
|
+
static register<K extends string, T extends Model<any>>(name: K, model: T): typeof Model;
|
|
741
750
|
/** Define model */
|
|
742
|
-
static define<T extends ModelSchema>(name:
|
|
751
|
+
static define<K extends string, T extends ModelSchema>(name: K, schema: T, options?: {
|
|
743
752
|
collection?: MicroCollection | Promise<MicroCollection>;
|
|
744
753
|
class?: typeof Model;
|
|
745
754
|
}): Model<T>;
|
package/microserver.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MicroServer
|
|
3
|
-
* @version 2.3.
|
|
3
|
+
* @version 2.3.3
|
|
4
4
|
* @package @radatek/microserver
|
|
5
5
|
* @copyright Darius Kisonas 2022
|
|
6
6
|
* @license MIT
|
|
@@ -838,10 +838,6 @@ export class Controller {
|
|
|
838
838
|
method = keyMatch[1];
|
|
839
839
|
url = keyMatch[2].startsWith('/') ? keyMatch[2] : ('/' + prefix + keyMatch[2]);
|
|
840
840
|
}
|
|
841
|
-
if (!url && !method) {
|
|
842
|
-
method = 'GET';
|
|
843
|
-
url = '/' + prefix + key;
|
|
844
|
-
}
|
|
845
841
|
if (!method)
|
|
846
842
|
return;
|
|
847
843
|
let autoAcl = method.toLowerCase();
|
|
@@ -908,14 +904,7 @@ export class Controller {
|
|
|
908
904
|
req.model = modelName instanceof Model ? modelName : Model.models[modelName];
|
|
909
905
|
if (!obj.model)
|
|
910
906
|
throw new InvalidData(modelName, 'model');
|
|
911
|
-
|
|
912
|
-
req.model = new Proxy(req.model, {
|
|
913
|
-
get: (target, prop) => {
|
|
914
|
-
if (prop === 'options')
|
|
915
|
-
return modelOptions;
|
|
916
|
-
return target[prop];
|
|
917
|
-
}
|
|
918
|
-
});
|
|
907
|
+
req.model = Model.dynamic(req.model, { controller: obj });
|
|
919
908
|
}
|
|
920
909
|
return func.apply(obj, req.paramsList);
|
|
921
910
|
});
|
|
@@ -2592,6 +2581,8 @@ class ModelCollectionsInternal {
|
|
|
2592
2581
|
return db.collection(name);
|
|
2593
2582
|
}
|
|
2594
2583
|
}
|
|
2584
|
+
export class Models {
|
|
2585
|
+
}
|
|
2595
2586
|
export class Model {
|
|
2596
2587
|
static set db(db) {
|
|
2597
2588
|
this.collections.db = db;
|
|
@@ -2599,6 +2590,31 @@ export class Model {
|
|
|
2599
2590
|
static get db() {
|
|
2600
2591
|
return this.collections.db;
|
|
2601
2592
|
}
|
|
2593
|
+
/** Dynamic model extension */
|
|
2594
|
+
static dynamic(model, options) {
|
|
2595
|
+
if (options instanceof Controller)
|
|
2596
|
+
options = { controller: options };
|
|
2597
|
+
const collection = options?.collection || model.collection;
|
|
2598
|
+
const modelOptions = { ...model.options };
|
|
2599
|
+
const req = options.req || options.controller?.req;
|
|
2600
|
+
if (req) {
|
|
2601
|
+
modelOptions.user = req.user;
|
|
2602
|
+
modelOptions.params = req.params;
|
|
2603
|
+
}
|
|
2604
|
+
return new Proxy(model, {
|
|
2605
|
+
get: (target, prop) => {
|
|
2606
|
+
if (prop === 'collection')
|
|
2607
|
+
return collection;
|
|
2608
|
+
if (prop === 'options')
|
|
2609
|
+
return modelOptions;
|
|
2610
|
+
return target[prop];
|
|
2611
|
+
}
|
|
2612
|
+
});
|
|
2613
|
+
}
|
|
2614
|
+
static register(name, model) {
|
|
2615
|
+
Model.models[name] = model;
|
|
2616
|
+
return this;
|
|
2617
|
+
}
|
|
2602
2618
|
/** Define model */
|
|
2603
2619
|
static define(name, schema, options) {
|
|
2604
2620
|
options = options || {};
|
|
@@ -2607,7 +2623,7 @@ export class Model {
|
|
|
2607
2623
|
const inst = options?.class
|
|
2608
2624
|
? new options.class(schema, { name, ...options })
|
|
2609
2625
|
: new Model(schema, { name, ...options });
|
|
2610
|
-
|
|
2626
|
+
this.register(name, inst);
|
|
2611
2627
|
return inst;
|
|
2612
2628
|
}
|
|
2613
2629
|
/** Create model acording to description */
|