tsledge 0.1.10 → 0.1.11

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,5 +1,100 @@
1
1
  # TSledge
2
2
 
3
- My playground and some helpful tools for web development
3
+ A TypeScript playground and toolkit for web development.
4
4
 
5
- ![thumbnail](docs/res/thumbnail.png)
5
+ ![thumbnail](docs/res/thumbnail.png)
6
+
7
+ ## Installation
8
+
9
+ This project requires the following dependencies:
10
+
11
+ ```bash
12
+ npm install mongoose@9.2.1
13
+ ```
14
+
15
+ ## Tutorials
16
+
17
+ ### Create Mongoose Collection
18
+
19
+ ```typescript
20
+ import mongoose from "mongoose";
21
+
22
+ export interface User {
23
+ ofUserGroup: mongoose.Schema.Types.ObjectId;
24
+ username: string;
25
+ email: string;
26
+ secretHash: string;
27
+ }
28
+
29
+ export type UserDocument = User & mongoose.Document;
30
+
31
+ const UserSchema = new mongoose.Schema<UserDocument>(
32
+ {
33
+ ofUserGroup: {
34
+ type: mongoose.Schema.Types.ObjectId,
35
+ ref: 'UserGroup',
36
+ alias: 'userGroup',
37
+ required: true,
38
+ },
39
+ username: { type: String, unique: true, required: true, filter: true }, // 'filter' is a fluent-pattern-handler feature
40
+ email: { type: String, unique: true, required: true, select: false },
41
+ secretHash: { type: String, select: false },
42
+ },
43
+ { collection: 'users', timestamps: true }
44
+ );
45
+
46
+ UserSchema.set('toJSON', {
47
+ transform: (_doc, ret: any) => {
48
+ delete ret.secretHash;
49
+ return ret;
50
+ }
51
+ });
52
+
53
+ export const UserModel = mongoose.model<UserDocument>('User', UserSchema);
54
+ ```
55
+
56
+ ### Using FluentInterface
57
+
58
+ ```typescript
59
+ router.get(
60
+ '/',
61
+ async (
62
+ req: FluentExpressRequest,
63
+ res: FluentExpressResponse
64
+ ) => {
65
+ try {
66
+ let queryBuilder = new QueryBuilder({
67
+ model: UserModel
68
+ });
69
+ let codec = await FluentPatternHandler.getInstance().exec({
70
+ req,
71
+ res,
72
+ queryBuilder
73
+ });
74
+
75
+ codec.sendToClient(res);
76
+ } catch (e) {
77
+ console.log(e);
78
+ res.status(500).json();
79
+ }
80
+ }
81
+ );
82
+ ```
83
+
84
+ ### API Examples
85
+
86
+ #### Search across all filter fields
87
+
88
+ ```http
89
+ GET /?filter=john_doe&limit=10&offset=5
90
+ ```
91
+
92
+ Search in all fields marked with `filter: true` option.
93
+
94
+ #### Search in a specific field
95
+
96
+ ```http
97
+ GET /?username=john_doe&limit=10&offset=5
98
+ ```
99
+
100
+ Search only in the `username` field.
@@ -1,20 +1,25 @@
1
- import { FluentAPIOption, FluentMiddleware, FluentExecParams } from './types';
1
+ import { FluentMiddleware, FluentExecParams } from './types';
2
2
  import { PromiseDefaultCodec } from '../core/index';
3
3
  export declare class FluentPatternHandler {
4
+ /**
5
+ * Singleton instance of FluentPatternHandler. The class is designed
6
+ */
4
7
  private static _singleton;
5
- private _options;
8
+ /**
9
+ * Array of middleware functions to be executed before the main query execution in the exec method. Each function receives the execution parameters and can modify them as needed.
10
+ */
6
11
  private _execMiddlewareFunctions;
7
12
  /**
8
- * Constructor for QueryPatternExecutor.
9
- * @param options - Array of query pattern options for filtering.
13
+ * Constructor for FluentPatternHandler.
14
+ * @param execMiddleware - Optional array of middleware functions to be executed before the main query execution in the exec method.
10
15
  */
11
- constructor(options?: FluentAPIOption[], execMiddleware?: FluentMiddleware[]);
16
+ constructor(execMiddleware?: FluentMiddleware[]);
12
17
  /**
13
18
  * Initializes the singleton instance of FluentPatternHandler with the provided options.
14
- * @param options - Array of query pattern options for filtering.
19
+ * @param execMiddleware - Optional array of middleware functions to be executed before the main query execution in the exec method.
15
20
  * @returns Singleton instance of FluentPatternHandler.
16
21
  */
17
- static init(options?: FluentAPIOption[], execMiddleware?: FluentMiddleware[]): FluentPatternHandler;
22
+ static init(execMiddleware?: FluentMiddleware[]): FluentPatternHandler;
18
23
  /**
19
24
  * Returns the singleton instance of FluentPatternHandler.
20
25
  * @returns Singleton instance of FluentPatternHandler.
@@ -34,7 +39,7 @@ export declare class FluentPatternHandler {
34
39
  */
35
40
  private _applyParameters;
36
41
  /**
37
- * Retrieves filter fields for the given model from the options configuration.
42
+ * Generates filter fields for the given Mongoose model based on schema options.
38
43
  * @param model - The Mongoose model.
39
44
  * @returns Array of filter fields.
40
45
  */
@@ -1 +1 @@
1
- {"version":3,"file":"fluent-pattern-handler.d.ts","sourceRoot":"","sources":["../../src/fluent-interface/fluent-pattern-handler.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,eAAe,EAEf,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,SAAS,CAAC;AACjB,OAAO,EAA8B,mBAAmB,EAAgB,MAAM,eAAe,CAAC;AAY9F,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,MAAM,CAAC,UAAU,CAAuB;IAChD,OAAO,CAAC,QAAQ,CAAoB;IACpC,OAAO,CAAC,wBAAwB,CAA0B;IAE1D;;;OAGG;gBACS,OAAO,GAAE,eAAe,EAAO,EAAE,cAAc,GAAE,gBAAgB,EAAO;IAWpF;;;;OAIG;WACW,IAAI,CAChB,OAAO,GAAE,eAAe,EAAO,EAC/B,cAAc,GAAE,gBAAgB,EAAO,GACtC,oBAAoB;IAQvB;;;OAGG;WACW,WAAW,IAAI,oBAAoB;IASjD;;;;OAIG;IACH,OAAO,CAAC,wBAAwB;IAsChC;;;;;OAKG;IACH,OAAO,CAAC,gBAAgB;IAoCxB;;;;OAIG;IACH,OAAO,CAAC,wBAAwB;IAYhC;;;;OAIG;IACH,OAAO,CAAC,qBAAqB;IAa/B;;;;OAIG;IACY,IAAI,CAAC,CAAC,GAAG,GAAG,EAAE,MAAM,EAAE,gBAAgB,GAAG,mBAAmB;CAgB1E"}
1
+ {"version":3,"file":"fluent-pattern-handler.d.ts","sourceRoot":"","sources":["../../src/fluent-interface/fluent-pattern-handler.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,SAAS,CAAC;AACjB,OAAO,EAA8B,mBAAmB,EAAgB,MAAM,eAAe,CAAC;AAY9F,qBAAa,oBAAoB;IAC/B;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,UAAU,CAAuB;IAChD;;OAEG;IACH,OAAO,CAAC,wBAAwB,CAA0B;IAE1D;;;OAGG;gBACS,cAAc,GAAE,gBAAgB,EAAO;IAUnD;;;;OAIG;WACW,IAAI,CAAC,cAAc,GAAE,gBAAgB,EAAO,GAAG,oBAAoB;IAQjF;;;OAGG;WACW,WAAW,IAAI,oBAAoB;IASjD;;;;OAIG;IACH,OAAO,CAAC,wBAAwB;IAsChC;;;;;OAKG;IACH,OAAO,CAAC,gBAAgB;IAoCxB;;;;OAIG;IACH,OAAO,CAAC,wBAAwB;IAWhC;;;;OAIG;IACH,OAAO,CAAC,qBAAqB;IAa7B;;;;OAIG;IACU,IAAI,CAAC,CAAC,GAAG,GAAG,EAAE,MAAM,EAAE,gBAAgB,GAAG,mBAAmB;CAgB1E"}
@@ -1,9 +1,5 @@
1
- import mongoose from 'mongoose';
2
1
  import { Request, Response } from 'express';
3
2
  import { DefaultResponseBody, KeysEnum, QueryBuilder } from '../core/index';
4
- export interface FluentRequestParams extends Record<string, string> {
5
- collection: string;
6
- }
7
3
  export interface FluentRequestBody {
8
4
  }
9
5
  export interface FluentRequestQuery {
@@ -19,11 +15,7 @@ export interface FluentRequestQuery {
19
15
  * Attributes of FluentRequestQuery used for validation and parsing.
20
16
  */
21
17
  export declare const fluentRequestQueryAttributes: KeysEnum<FluentRequestQuery>;
22
- export interface FluentAPIOption<T = any> {
23
- model: mongoose.Model<T>;
24
- filters?: string[];
25
- }
26
- export type FluentExpressRequest = Request<FluentRequestParams, DefaultResponseBody, FluentRequestBody, FluentRequestQuery>;
18
+ export type FluentExpressRequest = Request<any, DefaultResponseBody, FluentRequestBody, FluentRequestQuery>;
27
19
  export type FluentExpressResponse = Response<DefaultResponseBody>;
28
20
  export type FluentExecParams = {
29
21
  req: FluentEx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AC;CAC5B,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,CAAC,MAAM,EAAE,gBAAgB,KAAK,IAAI,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/fluent-interface/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE5E,MAAM,WAAW,iBAAiB;CAAG;AAErC,MAAM,WAAW,kBAAkB;IACjC,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,eAAO,MAAM,4BAA4B,EAAE,QAAQ,CAAC,kBAAkB,CAQrE,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG,OAAO,CACxC,GAAG,EACH,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,CACnB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,QAAQ,CAAC,mBAAmB,CAAC,CAAC;AAElE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,GAAG,EAAE,oBAAoB,CAAC;IAC1B,GAAG,EAAE,qBAAqB,CAAC;IAC3B,YAAY,EAAE,YAAY,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG,CAAC,MAAM,EAAE,gBAAgB,KAAK,IAAI,CAAC"}
package/dist/index.js CHANGED
@@ -810,30 +810,32 @@ async function httpRequest(config) {
810
810
  // src/fluent-interface/fluent-pattern-handler.ts
811
811
  var FluentPatternHandler = class _FluentPatternHandler {
812
812
  /**
813
- * Constructor for QueryPatternExecutor.
814
- * @param options - Array of query pattern options for filtering.
813
+ * Constructor for FluentPatternHandler.
814
+ * @param execMiddleware - Optional array of middleware functions to be executed before the main query execution in the exec method.
815
815
  */
816
- constructor(options = [], execMiddleware = []) {
816
+ constructor(execMiddleware = []) {
817
+ /**
818
+ * Array of middleware functions to be executed before the main query execution in the exec method. Each function receives the execution parameters and can modify them as needed.
819
+ */
817
820
  this._execMiddlewareFunctions = [];
818
821
  if (_FluentPatternHandler._singleton) {
819
822
  throw new Error(
820
823
  "FluentPatternHandler is a singleton class. Use FluentPatternHandler.getInstance() to access the instance."
821
824
  );
822
825
  }
823
- this._options = options;
824
826
  this._execMiddlewareFunctions = execMiddleware;
825
827
  _FluentPatternHandler._singleton = this;
826
828
  }
827
829
  /**
828
830
  * Initializes the singleton instance of FluentPatternHandler with the provided options.
829
- * @param options - Array of query pattern options for filtering.
831
+ * @param execMiddleware - Optional array of middleware functions to be executed before the main query execution in the exec method.
830
832
  * @returns Singleton instance of FluentPatternHandler.
831
833
  */
832
- static init(options = [], execMiddleware = []) {
834
+ static init(execMiddleware = []) {
833
835
  if (_FluentPatternHandler._singleton != void 0) {
834
836
  throw new Error("FluentPatternHandler is already initialized");
835
837
  }
836
- _FluentPatternHandler._singleton = new _FluentPatternHandler(options, execMiddleware);
838
+ _FluentPatternHandler._singleton = new _FluentPatternHandler(execMiddleware);
837
839
  return _FluentPatternHandler._singleton;
838
840
  }
839
841
  /**
@@ -872,7 +874,7 @@ var FluentPatternHandler = class _FluentPatternHandler {
872
874
  excluded = JSON.parse(excludedJSON);
873
875
  if (!Array.isArray(excluded)) throw new Error("Excluded must be an array");
874
876
  } catch (error) {
875
- console.warn("[QueryPatternExecutor] Invalid excluded parameter:", error);
877
+ console.warn("[FluentPatternHandler] Invalid excluded parameter:", error);
876
878
  }
877
879
  }
878
880
  let ids;
@@ -881,7 +883,7 @@ var FluentPatternHandler = class _FluentPatternHandler {
881
883
  ids = JSON.parse(idsJSON);
882
884
  if (!Array.isArray(ids)) throw new Error("Ids must be an array");
883
885
  } catch (error) {
884
- console.warn("[QueryPatternExecutor] Invalid ids parameter:", error);
886
+ console.warn("[FluentPatternHandler] Invalid ids parameter:", error);
885
887
  }
886
888
  }
887
889
  return { filter, limit, offset, id, excluded, ids, filterFields };
@@ -924,20 +926,19 @@ var FluentPatternHandler = class _FluentPatternHandler {
924
926
  }
925
927
  }
926
928
  /**
927
- * Retrieves filter fields for the given model from the options configuration.
929
+ * Generates filter fields for the given Mongoose model based on schema options.
928
930
  * @param model - The Mongoose model.
929
931
  * @returns Array of filter fields.
930
932
  */
931
933
  _getFilterFieldsForModel(model) {
932
- for (const option of this._options) {
933
- if (option.model.collection.name === model.collection.name) {
934
- if (option.filters) {
935
- return option.filters;
936
- }
937
- return [];
934
+ let filterFields = [];
935
+ let schema = model.schema;
936
+ schema.eachPath((path2, type) => {
937
+ if (type?.options?.filter == true) {
938
+ filterFields.push(path2);
938
939
  }
939
- }
940
- return [];
940
+ });
941
+ return filterFields;
941
942
  }
942
943
  /**
943
944
  * Builds execution parameters for the query builder.
@@ -955,7 +956,7 @@ var FluentPatternHandler = class _FluentPatternHandler {
955
956
  /**
956
957
  * Executes the query builder with applied filters and returns the result.
957
958
  * @param params Execution parameters including the query builder and request query.
958
- * @returns
959
+ * @returns
959
960
  */
960
961
  async exec(params) {
961
962
  try {
@@ -969,7 +970,7 @@ var FluentPatternHandler = class _FluentPatternHandler {
969
970
  const execConfig = this._buildExecutionConfig(queryParams);
970
971
  return await params.queryBuilder.exec(execConfig);
971
972
  } catch (err) {
972
- console.error("[ERROR - QueryPatternExecutor]", err);
973
+ console.error("[ERROR - FluentPatternHandler]", err);
973
974
  return new Codec({ data: [], meta: { total: 0 } }, 500);
974
975
  }
975
976
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Niklas Wockenfuß",
3
3
  "homepage": "https://niklaswockenfuss.de/",
4
4
  "name": "tsledge",
5
- "version": "0.1.10",
5
+ "version": "0.1.11",
6
6
  "license": "MIT",
7
7
  "description": "My playground and some helpful tools for web development ",
8
8
  "main": "dist/index.js",