not-node 6.4.13 → 6.4.14
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 +1 -1
- package/src/generic/field._data.js +51 -4
- package/src/generic/field.js +54 -0
- package/src/generic/form.create.js +31 -2
- package/src/generic/form.update.js +37 -6
package/package.json
CHANGED
|
@@ -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("./form._data");
|
|
5
|
+
const Form = require("../form/form");
|
|
4
6
|
|
|
5
|
-
|
|
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 };
|
|
@@ -1,16 +1,45 @@
|
|
|
1
1
|
const Form = require("../form/form");
|
|
2
|
-
|
|
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
|
+
*/
|
|
3
16
|
module.exports = ({
|
|
4
17
|
MODULE_NAME,
|
|
5
18
|
MODEL_NAME,
|
|
6
19
|
actionName = "create",
|
|
7
20
|
dataFieldName = "data",
|
|
8
21
|
validators = [],
|
|
22
|
+
dataValidators = [],
|
|
9
23
|
afterExtract = async (input /*, req*/) => input,
|
|
24
|
+
afterDataExtract = async (input /*, req*/) => input,
|
|
10
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
|
+
|
|
11
40
|
const FIELDS = [
|
|
12
41
|
["identity", "not-node//identity"],
|
|
13
|
-
["data",
|
|
42
|
+
["data", dataFieldDefinition],
|
|
14
43
|
];
|
|
15
44
|
return class extends Form {
|
|
16
45
|
constructor({ app }) {
|
|
@@ -1,17 +1,46 @@
|
|
|
1
1
|
const Form = require("../form/form");
|
|
2
|
-
|
|
3
|
-
|
|
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({
|
|
4
17
|
MODULE_NAME,
|
|
5
18
|
MODEL_NAME,
|
|
6
19
|
actionName = "update",
|
|
7
|
-
dataFieldName = "
|
|
20
|
+
dataFieldName = "_data",
|
|
8
21
|
validators = [],
|
|
22
|
+
dataValidators = [],
|
|
9
23
|
afterExtract = async (input /*, req*/) => input,
|
|
10
|
-
|
|
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
|
+
|
|
11
40
|
const FIELDS = [
|
|
12
41
|
["targetId", { required: true }, "not-node//objectId"],
|
|
13
42
|
["identity", "not-node//identity"],
|
|
14
|
-
["data",
|
|
43
|
+
["data", dataFieldDefinition],
|
|
15
44
|
];
|
|
16
45
|
|
|
17
46
|
return class extends Form {
|
|
@@ -28,4 +57,6 @@ module.exports = ({
|
|
|
28
57
|
return validators;
|
|
29
58
|
}
|
|
30
59
|
};
|
|
31
|
-
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
module.exports = genericUpdateForm;
|