very-express 0.4.14 → 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.
@@ -27,7 +27,7 @@ export default async function oauthVerify(accessToken: string, refreshToken: str
27
27
  familyName: profile.name?.familyName,
28
28
  givenName: profile.name?.givenName,
29
29
  isActive: true,
30
- locale: profile._json?.locale,
30
+ locale: profile._json?.locale || "en",
31
31
  });
32
32
  newUser.save();
33
33
  return done(null, newUser);
@@ -1,5 +1,5 @@
1
- export type accessAction = "create" | "read" | "update" | "delete" | "search";
2
- export const accessActionArr: accessAction[] = ["create", "read", "update", "delete", "search"];
1
+ export type accessAction = "create" | "read" | "update" | "delete";
2
+ export const accessActionArr: accessAction[] = ["create", "read", "update", "delete"];
3
3
 
4
4
  export interface accessControl< T = accessAction > {
5
5
  [key:string]: T[]
@@ -8,15 +8,22 @@ export default class VexDbConnector {
8
8
  private mongoUrl: string;
9
9
  private sqlUrl: string;
10
10
 
11
+ private recordAccessLog: boolean = true;
11
12
 
12
13
  constructor(options:{
13
14
  mongoUrl?: string;
14
15
  sqlUrl?: string;
16
+ recordAccessLog?: boolean;
15
17
  }){
16
18
  this.mongoUrl = options.mongoUrl || "";
17
19
  this.sqlUrl = options.sqlUrl || "";
20
+ this.recordAccessLog = options.recordAccessLog || false;
21
+
22
+ // Bind the middleware method to the instance
23
+ this.middleware = this.middleware.bind(this);
18
24
  }
19
25
 
26
+
20
27
  connectMongo() : mongoose.Connection | void {
21
28
  if ( !this.mongoUrl ){
22
29
  vex.log.error("VexDbConnector : MongoUrl is not invalid");
@@ -49,11 +56,17 @@ export default class VexDbConnector {
49
56
  return mongoose.connection;
50
57
  }
51
58
 
59
+ /**
60
+ * Database Middleware
61
+ * Feature:
62
+ * - restrict access to app when DB connection down.
63
+ * - record access log
64
+ **/
52
65
  middleware(req: Request, res: Response, next: NextFunction) {
53
66
  if (mongoose.connection.readyState !== 1) {
54
67
  vex.response.send(res, 503, "Database Connection Down");
55
68
  }
56
- else {
69
+ else if (this.recordAccessLog){
57
70
  mongoose.connection.collection("accessLogs").insertOne({
58
71
  timestamp: new Date().getTime(),
59
72
  ipa: req.socket.remoteAddress || req.headers["x-forwarded-for"],
@@ -63,7 +76,9 @@ export default class VexDbConnector {
63
76
  // body: req.body,
64
77
  query: req.query,
65
78
  });
66
-
79
+ next();
80
+ }
81
+ else{
67
82
  next();
68
83
  }
69
84
  }
@@ -2,6 +2,7 @@
2
2
 
3
3
  import jsYaml from "js-yaml";
4
4
  import fs from "fs";
5
+ import { Request } from "express";
5
6
 
6
7
  export function loadYaml(yamlFilePath: string) {
7
8
  try {
@@ -17,8 +18,10 @@ export function loadYaml(yamlFilePath: string) {
17
18
  * @param fieldsString json stringified array of string
18
19
  * @error may throw an error if the fieldsString is not a valid JSON
19
20
  */
20
- export function parseFieldsSelect(selectString: any) : { [key: string]: number } | undefined {
21
- const ErrorArrStrMsg = "Invalid select string, only array of string accepted";
21
+ export function parseFieldsSelect(req: Request) : { [key: string]: number } | undefined {
22
+
23
+ const selectString = req.query._select;
24
+ const ErrorArrStrMsg = "Invalid \"_select\" string, only json array accepted";
22
25
 
23
26
  if (typeof selectString === "undefined" || selectString === "") {
24
27
  return undefined;
@@ -47,7 +50,40 @@ export function parseFieldsSelect(selectString: any) : { [key: string]: number }
47
50
  return selectFields;
48
51
  }
49
52
 
53
+ export function parseCollectionJoin(req: Request, availablePopulateOptions:{[key:string]: string}) : { [key: string]: string } | undefined {
54
+
55
+ const joinString = req.query._join;
56
+ const ErrorArrStrMsg = "Invalid \"_select\" string, only json array accepted";
57
+ const populateOptions: any = [];
58
+
59
+ if (typeof joinString === "undefined" || joinString === "") {
60
+ return undefined;
61
+ }
62
+ else if (typeof joinString !== "string") {
63
+ throw new Error(ErrorArrStrMsg);
64
+ }
65
+
66
+ const joinArr: string[] = JSON.parse(joinString);
67
+
68
+ // switch joinArr's item to populate options
69
+ if(joinArr.length > 0) {
70
+ joinArr.forEach((refKey: any) => {
71
+
72
+ if(availablePopulateOptions[refKey]) {
73
+ populateOptions.push({
74
+ path: refKey,
75
+ select: availablePopulateOptions[refKey],
76
+ options: { lean: true }
77
+ });
78
+ }
79
+ });
80
+ }
81
+
82
+ return populateOptions;
83
+ }
84
+
50
85
  export default {
51
86
  loadYaml,
52
- parseFieldsSelect
87
+ parseFieldsSelect,
88
+ parseCollectionJoin
53
89
  };
@@ -0,0 +1,40 @@
1
+ # v0.5.0 - Foreign-key & REST API
2
+
3
+ ### Define Foreign-key
4
+ add attribute at JsonSchmea's field props, `x-foreignKey: "targetCollectionName"`
5
+
6
+ Example:
7
+ - **User Collection's** field "contactId" as foreign-key of **Contact Collection**
8
+ ```JSON
9
+ {
10
+ "username": {
11
+ "type" : "string",
12
+ ...
13
+ },
14
+ "contactId": {
15
+ "type" : "string",
16
+ "x-foreignKey" : "contact"
17
+ }
18
+ }
19
+ ```
20
+ ### REST API Foreign-key supports
21
+
22
+ | Methods | Param | Type | Mandatory | Description |
23
+ | ------- | ----- | ---- | --------- | ----------- |
24
+ | GET | _join | QueryString `array<String>` | false | join collection using **Current Resource's Field Name**. target joined collection decleare by `x-forignKey` |
25
+ | GET | _select | QueryString `array<String>` | false | work with `_join`, select fields to join for target collection. empty = select all. |
26
+
27
+ **Example :**
28
+ `/user?_join=["contact"]&_select=["contactName","contactNumber"]`
29
+
30
+
31
+ **Output:**
32
+ ```JSON
33
+ {
34
+ "username": "justin",
35
+ "contactId": {
36
+ "contactName": "justin's mobile",
37
+ "contactNumber": 1122333445566
38
+ }
39
+ }
40
+
@@ -1,20 +1,18 @@
1
1
  # v0.5.0 Plan
2
2
 
3
- ### App level foreign-key
4
- - [ ] at JsonSchmea properties item, add attribute `x-foreignKey: "collectionName"`
5
- use this column value to match with target collectionName's "_id" fields.
3
+ ### foreign-key
4
+ - [x] define FK, add props at field, `x-foreignKey: "targetCollectionName"`
6
5
  ```JSON
7
6
  {
8
- "invoiceId": {
7
+ "username": {
9
8
  "type" : "string",
10
- "x-foreignKey" : "invoice"
11
9
  },
12
- "orderId": {
10
+ "contactId": {
13
11
  "type" : "string",
14
- "x-foreignKey" : "order"
12
+ "x-foreignKey" : "contact" // target collection's name
15
13
  }
16
14
  }
17
15
  ```
18
- - [ ] REST API, to request data with foreign-key collection:
19
- add field name as stringified array into querystring "join"
20
- example `/myDataApi?join=[invoiceId,orderId]`
16
+ - [x] REST API, to request data with foreign-key collection:
17
+ add field name as array into querystring "_join"
18
+ example `/user?_join=["contact"]_select=["contactNo"]&`
@@ -4,4 +4,7 @@
4
4
  - [ ] dockernize
5
5
  ### oauth2 support
6
6
  - [ ] microsoft
7
- - [ ] github
7
+ - [ ] github
8
+ ### session storage
9
+ - [ ] jwt
10
+ - [ ] ~redis~
@@ -1,4 +1,4 @@
1
1
  # v0.7.0 Plan
2
2
 
3
- ## app level foreign-key.
3
+ ## security
4
4
  - [ ] db fields encryption
@@ -4,7 +4,7 @@
4
4
 
5
5
  | Fields | Data Type | Options | Description |
6
6
  | - | - | - | - |
7
- | documentName | `string` | - | auto generated using file name. used to name **Collection**, **TS Interface**, **ODM Model** etc. |
7
+ | documentName | `string` | - | auto generated using file name. this value should be same as the collection name, it will used to name **Collection**, **TS Interface**, **ODM Model** etc. |
8
8
  | methods | `array<string>` | `get`, `post`, `put`, `patch`, `delete` | array of **REST API Method** to be enable. |
9
9
 
10
10
 
@@ -15,12 +15,13 @@
15
15
  | type | `string` | true | - | [JsonSchema's type field](https://json-schema.org/understanding-json-schema/reference/type) |
16
16
  | required | `boolean` | false | - | rest api's validator will check value cannot be falsy / undefined. |
17
17
  | x-vexData | `string` | false | `primaryKey`, `role` | determine veryExpress generation process. |
18
- | x-foreignKey | - | - | - | `documentName` from other JsonSchema, auto index. plan to implemented on [v0.5.0](./RoadMap/v0-5-0.md) |
18
+ | x-foreignKey | `string` | false | - | `documentName` from other JsonSchema, auto indexed, use to join collection |
19
+ | x-foreignValue | `array<String>` | false | - | default selected fields for target collection |
19
20
  <!-- | format | `string` | false | - | veryExpress did not handle this field | -->
20
21
 
21
22
  ### System Fields
22
- which mean there field properties is not changable.
23
- - `_id` : if defined in schema, will auto set as `"index": true`, `"x-vexData": "ObjectId"`.
23
+ System Fields is the properties to let generator run as expected,it should not be changed.
24
+ - `_id` : if defined in schema, it will auto being set as `"index": true`, `"x-vexData": "ObjectId"`.
24
25
 
25
26
 
26
27
  #### Example :
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "very-express",
3
- "version": "0.4.14",
3
+ "version": "0.5.0",
4
4
  "description": "based on json schema and openapi, generate Express RestApi server with mongoDb ",
5
5
  "author": "Justin26l",
6
6
  "license": "MIT",
@@ -48,7 +48,7 @@
48
48
  "js-yaml": "^4.1.0",
49
49
  "json-schema-to-openapi-schema": "^0.4.0",
50
50
  "json-schema-to-typescript": "^13.1.1",
51
- "json2mongoose": "^0.3.7",
51
+ "json2mongoose": "^0.4.2",
52
52
  "minimist": "^1.2.8",
53
53
  "passport": "^0.7.0",
54
54
  "passport-google-oauth20": "^2.0.0",
package/vex.config.json CHANGED
@@ -2,8 +2,8 @@
2
2
  "commitBeforeGenerate": false,
3
3
  "jsonSchemaDir": "./output/jsonSchema",
4
4
  "rootDir": "./output",
5
- "srcDir": "./output/src",
6
- "openapiDir": "./output/src/openapi",
5
+ "srcDir": "output/src",
6
+ "openapiDir": "output/src/openapi",
7
7
  "app": {
8
8
  "enableSwagger": true,
9
9
  "useUserSchema": true,
@@ -28,5 +28,5 @@
28
28
  "User"
29
29
  ]
30
30
  },
31
- "sysDir": "./output/src/system"
31
+ "sysDir": "output/src/system"
32
32
  }