not-node 6.4.13 → 6.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "not-node",
3
- "version": "6.4.13",
3
+ "version": "6.5.0",
4
4
  "description": "node complimentary part for client side notFramework.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -4,7 +4,10 @@ const getApp = require("../getApp.js"),
4
4
  configInit = require("not-config"),
5
5
  { sayForModule } = require("not-locale"),
6
6
  { objHas, isFunc, executeFunctionAsAsync } = require("../common"),
7
- LogInit = require("not-log");
7
+ LogInit = require("not-log"),
8
+ genericFormsGenerators = require("../generic/forms"),
9
+ createDefaultFormInstance = require("../generic/form.js");
10
+
8
11
  module.exports = ({
9
12
  target,
10
13
  MODULE_NAME,
@@ -33,7 +36,9 @@ module.exports = ({
33
36
  accessRulesBuilders = {}, //per action builders, {actionName: (preapared: any, req: ExpressRequest)=>{/** pass or throw an exception **/ */}}
34
37
  accessRuleBuilder = false, //universal will be used if no dedicated action builder was found
35
38
  defaultAccessRule = false, //if builder is not found, safe by default
36
- createForm = Form.createDefaultInstance,
39
+ createForm = createDefaultFormInstance,
40
+ formValidators = {},
41
+ dataValidators = {},
37
42
  }) => {
38
43
  const Log = LogInit(target, `${MODEL_NAME}/Routes'`);
39
44
  const say = sayForModule(MODULE_NAME);
@@ -80,26 +85,66 @@ module.exports = ({
80
85
  }
81
86
  };
82
87
 
88
+ const initGenericActionForm = ({
89
+ app,
90
+ MODULE_NAME,
91
+ MODEL_NAME,
92
+ actionName,
93
+ validators,
94
+ dataValidators,
95
+ }) => {
96
+ if (Object.keys(genericFormsGenerators).includes(actionName)) {
97
+ return new genericFormsGenerators[actionName]({
98
+ app,
99
+ MODULE_NAME,
100
+ MODEL_NAME,
101
+ actionName,
102
+ validators,
103
+ dataValidators,
104
+ })(app);
105
+ } else {
106
+ createForm({
107
+ app: getApp(),
108
+ MODULE_NAME,
109
+ MODEL_NAME,
110
+ actionName,
111
+ });
112
+ }
113
+ };
114
+
83
115
  const getForm = (actionName) => {
84
- const form = getApp().getForm(
85
- Form.createPath(MODULE_NAME, MODEL_NAME, actionName)
116
+ //trying inited and cached
117
+ const fullFormPath = Form.createPath(
118
+ MODULE_NAME,
119
+ MODEL_NAME,
120
+ actionName
86
121
  );
122
+ const form = getApp().getForm(fullFormPath);
87
123
  if (form) {
88
124
  return form;
89
125
  }
126
+
90
127
  const form2 = getApp().getForm(
91
128
  Form.createPath(MODULE_NAME, undefined, actionName)
92
129
  );
130
+
93
131
  if (form2) {
94
132
  return form2;
95
133
  }
96
-
97
- return createForm({
98
- app: getApp(),
134
+ //creating new from generics lib
135
+ const newelyCreatedForm = initGenericActionForm({
99
136
  MODULE_NAME,
100
137
  MODEL_NAME,
101
138
  actionName,
139
+ app: getApp(),
140
+ validators: formValidators,
141
+ dataValidators,
102
142
  });
143
+ //caching
144
+ getApp()
145
+ .getModule(MODULE_NAME)
146
+ .setForm(fullFormPath.split("//")[1], newelyCreatedForm);
147
+ return newelyCreatedForm;
103
148
  };
104
149
 
105
150
  const beforeDecorator = async (req, res, next) => {
package/src/form/form.js CHANGED
@@ -116,6 +116,7 @@ class Form {
116
116
  this.#MODEL_NAME = MODEL_NAME;
117
117
  this.#MODULE_NAME = MODULE_NAME;
118
118
  this.#setFields(app, FIELDS);
119
+
119
120
  this.#createValidationSchema(app);
120
121
  this.#augmentValidationSchema();
121
122
  this.#addInstructions(INSTRUCTIONS);
@@ -202,24 +203,6 @@ class Form {
202
203
  }
203
204
  }
204
205
 
205
- /**
206
- *
207
- * @param {object} options
208
- * @param {import('../app.js')} options.app
209
- * @param {string} options.MODULE_NAME
210
- * @param {string} options.MODEL_NAME
211
- * @param {string} options.actionName
212
- * @returns {object} instance of Form
213
- */
214
- static createDefaultInstance({ app, MODULE_NAME, MODEL_NAME, actionName }) {
215
- const FIELDS = [
216
- ["identity", "not-node//identity"],
217
- ["data", `${MODULE_NAME}//_${firstLetterToLower(MODEL_NAME)}`],
218
- ];
219
- const FORM_NAME = Form.createName(MODULE_NAME, MODEL_NAME, actionName);
220
- return new Form({ FIELDS, FORM_NAME, app, MODULE_NAME });
221
- }
222
-
223
206
  /**
224
207
  *
225
208
  *
@@ -1,8 +1,57 @@
1
1
  const Schema = require("mongoose").Schema;
2
2
  const getApp = require("../getApp");
3
3
  const { firstLetterToLower } = require("../common");
4
+ const genericDataForm = require("./forms/form._data");
5
+ const Form = require("../form/form");
4
6
 
5
- module.exports = ({ MODULE_NAME, MODEL_NAME, actionName = "data" }) => {
7
+ const initGenericDataForm = ({
8
+ MODULE_NAME,
9
+ MODEL_NAME,
10
+ actionName,
11
+ formName,
12
+ validators = [],
13
+ afterExtract = async (input /*, req = null*/) => input,
14
+ }) => {
15
+ try {
16
+ const cls = genericDataForm({
17
+ MODULE_NAME,
18
+ MODEL_NAME,
19
+ actionName,
20
+ validators,
21
+ afterExtract,
22
+ });
23
+ const App = getApp();
24
+ App.getModule(MODULE_NAME).setFormConstructor(formName, cls);
25
+ App.getModule(MODULE_NAME).setForm(formName, new cls(App));
26
+ return true;
27
+ } catch (e) {
28
+ getApp().logger.error(e);
29
+ return false;
30
+ }
31
+ };
32
+
33
+ module.exports = ({
34
+ MODULE_NAME,
35
+ MODEL_NAME,
36
+ actionName = "_data",
37
+ FORM_PATH,
38
+ validators = [],
39
+ afterExtract = async (input /*, req = null*/) => input,
40
+ }) => {
41
+ if (!FORM_PATH) {
42
+ FORM_PATH = Form.createPath(MODULE_NAME, MODEL_NAME, actionName);
43
+ }
44
+ const formName = FORM_PATH.split("//")[1];
45
+ if (!getApp().getModule(MODULE_NAME).getFormConstructor(formName)) {
46
+ initGenericDataForm({
47
+ MODULE_NAME,
48
+ MODEL_NAME,
49
+ actionName,
50
+ formName,
51
+ validators,
52
+ afterExtract,
53
+ });
54
+ }
6
55
  return {
7
56
  model: {
8
57
  type: Schema.Types.Mixed,
@@ -10,9 +59,7 @@ module.exports = ({ MODULE_NAME, MODEL_NAME, actionName = "data" }) => {
10
59
  validate: [
11
60
  {
12
61
  validator(val) {
13
- return getApp()
14
- .getForm(`${MODULE_NAME}//_${actionName}`)
15
- .run(val);
62
+ return getApp().getForm(FORM_PATH).run(val);
16
63
  },
17
64
  message: `${MODULE_NAME}:${firstLetterToLower(
18
65
  MODEL_NAME
@@ -0,0 +1,54 @@
1
+ const getApp = require("../getApp");
2
+ const genericDataField = require("./field._data");
3
+ const Form = require("../form/form");
4
+
5
+ const initGenericDataField = (
6
+ MODULE_NAME,
7
+ MODEL_NAME,
8
+ actionName = "_data",
9
+ validators = [],
10
+ afterExtract = async (input /*, req = null*/) => input
11
+ ) => {
12
+ //not-module//modelName._data
13
+ const fieldGenericPath = Form.createPath(
14
+ MODULE_NAME,
15
+ MODEL_NAME,
16
+ actionName
17
+ );
18
+ //modelName._data
19
+ const fieldGenericName = fieldGenericPath.split("//")[1];
20
+ const App = getApp();
21
+ //if already exists - returning full path to it
22
+ if (App.getModule(MODULE_NAME).getField(fieldGenericName)) {
23
+ return fieldGenericPath;
24
+ }
25
+ //create new definition, register, return
26
+ return genericDataField({
27
+ MODULE_NAME,
28
+ MODEL_NAME,
29
+ FORM_PATH: fieldGenericPath,
30
+ validators,
31
+ afterExtract,
32
+ });
33
+ };
34
+
35
+ const getGenericDataField = ({
36
+ fieldPath,
37
+ MODULE_NAME,
38
+ MODEL_NAME,
39
+ validators,
40
+ afterExtract,
41
+ }) => {
42
+ if (getApp().getField(fieldPath)) {
43
+ return fieldPath;
44
+ } else {
45
+ return initGenericDataField({
46
+ MODULE_NAME,
47
+ MODEL_NAME,
48
+ validators,
49
+ afterExtract,
50
+ });
51
+ }
52
+ };
53
+
54
+ module.exports = { getGenericDataField, initGenericDataField };
@@ -0,0 +1,29 @@
1
+ const Common = require("../common.js");
2
+ const Form = require("../form/form.js");
3
+
4
+ /**
5
+ *
6
+ * @param {object} options
7
+ * @param {import('../app.js')} options.app
8
+ * @param {string} options.MODULE_NAME
9
+ * @param {string} options.MODEL_NAME
10
+ * @param {string} options.actionName
11
+ * @returns {object} instance of Form
12
+ */
13
+ function createDefaultInstance({
14
+ app,
15
+ MODULE_NAME,
16
+ MODEL_NAME,
17
+ actionName,
18
+ /* validators = [],
19
+ dataValidators = [],*/
20
+ }) {
21
+ const FIELDS = [
22
+ ["identity", "not-node//identity"],
23
+ ["data", `${MODULE_NAME}//_${Common.firstLetterToLower(MODEL_NAME)}`],
24
+ ];
25
+ const FORM_NAME = Form.createName(MODULE_NAME, MODEL_NAME, actionName);
26
+ return new Form({ FIELDS, FORM_NAME, app, MODULE_NAME });
27
+ }
28
+
29
+ module.exports = createDefaultInstance;
@@ -1,4 +1,4 @@
1
- const Form = require("../form/form");
1
+ const Form = require("../../form/form");
2
2
 
3
3
  module.exports = ({
4
4
  MODULE_NAME,
@@ -1,5 +1,5 @@
1
1
  //DB related validation tools
2
- const Form = require("../form/form");
2
+ const Form = require("../../form/form");
3
3
 
4
4
  //form
5
5
  const STANDART_FIELDS = [["identity", "not-node//identity"]];
@@ -0,0 +1,9 @@
1
+ const genericListForm = require("./form.list");
2
+
3
+ module.exports = ({ MODULE_NAME, MODEL_NAME, actionName = "count" }) => {
4
+ return genericListForm({
5
+ MODULE_NAME,
6
+ MODEL_NAME,
7
+ actionName,
8
+ });
9
+ };
@@ -0,0 +1,58 @@
1
+ const Form = require("../../form/form");
2
+ const { getGenericDataField } = require("../field");
3
+ /**
4
+ * Creates generic Crud form, if data field is not defined in fields/ it creates and registers field in module and sub _data form
5
+ *
6
+ * @param {Object} param
7
+ * @param {string} param.MODULE_NAME
8
+ * @param {string} param.MODEL_NAME
9
+ * @param {string} [param.actionName="create"]
10
+ * @param {string} [param.dataFieldName = "_data"]
11
+ * @param {Array<function>} [param.validators = []]
12
+ * @param {Array<function>} [param.dataValidators = []],
13
+ * @param {function} [param.afterExtract = async (input , req) => input]
14
+ * @param {function} [param.afterDataExtract = async (input, req) => input]
15
+ */
16
+ module.exports = ({
17
+ MODULE_NAME,
18
+ MODEL_NAME,
19
+ actionName = "create",
20
+ dataFieldName = "data",
21
+ validators = [],
22
+ dataValidators = [],
23
+ afterExtract = async (input /*, req*/) => input,
24
+ afterDataExtract = async (input /*, req*/) => input,
25
+ }) => {
26
+ const defaultDataFieldPath = Form.createPath(
27
+ MODULE_NAME,
28
+ MODEL_NAME,
29
+ dataFieldName
30
+ );
31
+
32
+ const dataFieldDefinition = getGenericDataField({
33
+ fieldPath: defaultDataFieldPath,
34
+ MODULE_NAME,
35
+ MODEL_NAME,
36
+ validators: dataValidators,
37
+ afterExtract: afterDataExtract,
38
+ });
39
+
40
+ const FIELDS = [
41
+ ["identity", "not-node//identity"],
42
+ ["data", dataFieldDefinition],
43
+ ];
44
+ return class extends Form {
45
+ constructor({ app }) {
46
+ super({ FIELDS, app, MODULE_NAME, MODEL_NAME, actionName });
47
+ }
48
+
49
+ async afterExtract(input, req) {
50
+ input = await super.afterExtract(input, req);
51
+ return await afterExtract(input, req);
52
+ }
53
+
54
+ getFormValidationRules() {
55
+ return validators;
56
+ }
57
+ };
58
+ };
@@ -0,0 +1,9 @@
1
+ const genericGetByIdForm = require("./form.getById");
2
+
3
+ module.exports = ({ MODULE_NAME, MODEL_NAME, actionName = "delete" }) => {
4
+ return genericGetByIdForm({
5
+ MODULE_NAME,
6
+ MODEL_NAME,
7
+ actionName,
8
+ });
9
+ };
@@ -0,0 +1,9 @@
1
+ const genericGetByIdForm = require("./form.getById");
2
+
3
+ module.exports = ({ MODULE_NAME, MODEL_NAME, actionName = "get" }) => {
4
+ return genericGetByIdForm({
5
+ MODULE_NAME,
6
+ MODEL_NAME,
7
+ actionName,
8
+ });
9
+ };
@@ -1,5 +1,5 @@
1
1
  //DB related validation tools
2
- const Form = require("../form/form");
2
+ const Form = require("../../form/form");
3
3
 
4
4
  //form
5
5
  const FIELDS = [
@@ -1,5 +1,5 @@
1
1
  //DB related validation tools
2
- const Form = require("../form/form");
2
+ const Form = require("../../form/form");
3
3
 
4
4
  //form
5
5
  const FIELDS = [
@@ -0,0 +1,9 @@
1
+ const genericGetByIdForm = require("./form.getById");
2
+
3
+ module.exports = ({ MODULE_NAME, MODEL_NAME, actionName = "getRaw" }) => {
4
+ return genericGetByIdForm({
5
+ MODULE_NAME,
6
+ MODEL_NAME,
7
+ actionName,
8
+ });
9
+ };
@@ -1,7 +1,7 @@
1
- const Form = require("../form/form");
1
+ const Form = require("../../form/form");
2
2
 
3
3
  const notFilter = require("not-filter");
4
- const notAppIdentity = require("../identity");
4
+ const notAppIdentity = require("../../identity");
5
5
 
6
6
  const FIELDS = [
7
7
  ["query", `not-filter//_filterQuery`],
@@ -32,8 +32,8 @@ const FactoryFormList = ({ MODULE_NAME, MODEL_NAME, actionName = "list" }) => {
32
32
  /**
33
33
  *
34
34
  *
35
- * @param {import('../types').notNodeExpressRequest} req
36
- * @return {Promise<import('../types').PreparedData>}
35
+ * @param {import('../../types').notNodeExpressRequest} req
36
+ * @return {Promise<import('../../types').PreparedData>}
37
37
  */
38
38
  async extract(req) {
39
39
  const envs = this.extractRequestEnvs(req);
@@ -0,0 +1,9 @@
1
+ const genericAuthorizedActionForm = require("./form.authorizedAction");
2
+
3
+ module.exports = ({ MODULE_NAME, MODEL_NAME, actionName = "listAll" }) => {
4
+ return genericAuthorizedActionForm({
5
+ MODULE_NAME,
6
+ MODEL_NAME,
7
+ actionName,
8
+ });
9
+ };
@@ -1,7 +1,7 @@
1
- const Form = require("../form/form");
1
+ const Form = require("../../form/form");
2
2
 
3
3
  const notFilter = require("not-filter");
4
- const notAppIdentity = require("../identity");
4
+ const notAppIdentity = require("../../identity");
5
5
 
6
6
  const FIELDS = [
7
7
  ["query", `not-filter//_filterQuery`],
@@ -36,8 +36,8 @@ const FactoryFormListAndCount = ({
36
36
  /**
37
37
  *
38
38
  *
39
- * @param {import('../types').notNodeExpressRequest} req
40
- * @return {Promise<import('../types').PreparedData>}
39
+ * @param {import('../../types').notNodeExpressRequest} req
40
+ * @return {Promise<import('../../types').PreparedData>}
41
41
  */
42
42
  async extract(req) {
43
43
  const envs = this.extractRequestEnvs(req);
@@ -0,0 +1,62 @@
1
+ const Form = require("../../form/form");
2
+ const { getGenericDataField } = require("../field");
3
+ /**
4
+ * Creates generic crUd form, if data field is not defined in fields/ it creates and registers field in module and sub _data form
5
+ *
6
+ * @param {Object} param
7
+ * @param {string} param.MODULE_NAME
8
+ * @param {string} param.MODEL_NAME
9
+ * @param {string} [param.actionName="update"]
10
+ * @param {string} [param.dataFieldName = "_data"]
11
+ * @param {Array<function>} [param.validators = []]
12
+ * @param {Array<function>} [param.dataValidators = []],
13
+ * @param {function} [param.afterExtract = async (input , req) => input]
14
+ * @param {function} [param.afterDataExtract = async (input, req) => input]
15
+ */
16
+ function genericUpdateForm({
17
+ MODULE_NAME,
18
+ MODEL_NAME,
19
+ actionName = "update",
20
+ dataFieldName = "_data",
21
+ validators = [],
22
+ dataValidators = [],
23
+ afterExtract = async (input /*, req*/) => input,
24
+ afterDataExtract = async (input /*, req*/) => input,
25
+ }) {
26
+ const defaultDataFieldPath = Form.createPath(
27
+ MODULE_NAME,
28
+ MODEL_NAME,
29
+ dataFieldName
30
+ );
31
+
32
+ const dataFieldDefinition = getGenericDataField({
33
+ fieldPath: defaultDataFieldPath,
34
+ MODULE_NAME,
35
+ MODEL_NAME,
36
+ validators: dataValidators,
37
+ afterExtract: afterDataExtract,
38
+ });
39
+
40
+ const FIELDS = [
41
+ ["targetId", { required: true }, "not-node//objectId"],
42
+ ["identity", "not-node//identity"],
43
+ ["data", dataFieldDefinition],
44
+ ];
45
+
46
+ return class extends Form {
47
+ constructor({ app }) {
48
+ super({ FIELDS, app, MODULE_NAME, MODEL_NAME, actionName });
49
+ }
50
+
51
+ async afterExtract(input, req) {
52
+ input = await super.afterExtract(input, req);
53
+ return await afterExtract(input, req);
54
+ }
55
+
56
+ getFormValidationRules() {
57
+ return validators;
58
+ }
59
+ };
60
+ }
61
+
62
+ module.exports = genericUpdateForm;
@@ -0,0 +1,12 @@
1
+ module.exports = {
2
+ create: require("./form.create"),
3
+ update: require("./form.update"),
4
+ list: require("./form.list"),
5
+ listAndCount: require("./form.listAndCount"),
6
+ listAll: require("./form.listAll"),
7
+ count: require("./form.count"),
8
+ get: require("./form.get"),
9
+ getByID: require("./form.getByID"),
10
+ getRaw: require("./form.getRaw"),
11
+ delete: require("./form.delete"),
12
+ };
@@ -1,14 +1,14 @@
1
1
  module.exports.GenericLogic = require("./logic.js");
2
2
  module.exports.GenericRoute = require("./route.js");
3
- module.exports.GenericGetByIdForm = require("./form.getById.js");
4
- module.exports.GenericGetByIDForm = require("./form.getByID.js");
3
+ module.exports.GenericGetByIdForm = require("./forms/form.getById.js");
4
+ module.exports.GenericGetByIDForm = require("./forms/form.getByID.js");
5
5
  module.exports.GenericDataField = require("./field._data.js");
6
- module.exports.GenericDataForm = require("./form._data.js");
7
- module.exports.GenericCreateForm = require("./form.create.js");
8
- module.exports.GenericUpdateForm = require("./form.update.js");
9
- module.exports.GenericAuthorizedActionForm = require("./form.authorizedAction.js");
10
- module.exports.GenericListAndCountForm = require("./form.listAndCount.js");
11
- module.exports.GenericListForm = require("./form.list.js");
6
+ module.exports.GenericDataForm = require("./forms/form._data.js");
7
+ module.exports.GenericCreateForm = require("./forms/form.create.js");
8
+ module.exports.GenericUpdateForm = require("./forms/form.update.js");
9
+ module.exports.GenericAuthorizedActionForm = require("./forms/form.authorizedAction.js");
10
+ module.exports.GenericListAndCountForm = require("./forms/form.listAndCount.js");
11
+ module.exports.GenericListForm = require("./forms/form.list.js");
12
12
 
13
13
  const FORMS = {};
14
14
 
@@ -1,29 +0,0 @@
1
- const Form = require("../form/form");
2
-
3
- module.exports = ({
4
- MODULE_NAME,
5
- MODEL_NAME,
6
- actionName = "create",
7
- dataFieldName = "data",
8
- validators = [],
9
- afterExtract = async (input /*, req*/) => input,
10
- }) => {
11
- const FIELDS = [
12
- ["identity", "not-node//identity"],
13
- ["data", `${MODULE_NAME}//_${dataFieldName}`],
14
- ];
15
- return class extends Form {
16
- constructor({ app }) {
17
- super({ FIELDS, app, MODULE_NAME, MODEL_NAME, actionName });
18
- }
19
-
20
- async afterExtract(input, req) {
21
- input = await super.afterExtract(input, req);
22
- return await afterExtract(input, req);
23
- }
24
-
25
- getFormValidationRules() {
26
- return validators;
27
- }
28
- };
29
- };
@@ -1,31 +0,0 @@
1
- const Form = require("../form/form");
2
-
3
- module.exports = ({
4
- MODULE_NAME,
5
- MODEL_NAME,
6
- actionName = "update",
7
- dataFieldName = "data",
8
- validators = [],
9
- afterExtract = async (input /*, req*/) => input,
10
- }) => {
11
- const FIELDS = [
12
- ["targetId", { required: true }, "not-node//objectId"],
13
- ["identity", "not-node//identity"],
14
- ["data", `${MODULE_NAME}//_${dataFieldName}`],
15
- ];
16
-
17
- return class extends Form {
18
- constructor({ app }) {
19
- super({ FIELDS, app, MODULE_NAME, MODEL_NAME, actionName });
20
- }
21
-
22
- async afterExtract(input, req) {
23
- input = await super.afterExtract(input, req);
24
- return await afterExtract(input, req);
25
- }
26
-
27
- getFormValidationRules() {
28
- return validators;
29
- }
30
- };
31
- };