mandarina-server 2.1.16 → 2.1.18
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/Action/CustomAction.js +45 -81
- package/src/Errors/TableInstanceNotFound.js +9 -20
- package/src/Errors/UniqueActionError.js +10 -23
- package/src/Mandarina.js +32 -48
- package/src/Table/Table.js +206 -282
package/package.json
CHANGED
|
@@ -8,46 +8,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
13
|
};
|
|
38
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
15
|
+
const Schema_1 = require("mandarina/build/Schema/Schema");
|
|
16
|
+
const TableInstanceNotFound_1 = require("../Errors/TableInstanceNotFound");
|
|
17
|
+
const UniqueActionError_1 = require("../Errors/UniqueActionError");
|
|
18
|
+
const Mandarina_1 = __importDefault(require("../Mandarina"));
|
|
19
|
+
class CustomAction {
|
|
44
20
|
/**
|
|
45
21
|
*
|
|
46
22
|
*
|
|
47
23
|
* @param schema
|
|
48
24
|
* @param actionOptions
|
|
49
25
|
*/
|
|
50
|
-
|
|
26
|
+
constructor(schema, actionOptions) {
|
|
51
27
|
CustomAction.instances = CustomAction.instances || {};
|
|
52
28
|
if (schema instanceof Schema_1.Schema) {
|
|
53
29
|
this.schema = schema;
|
|
@@ -62,60 +38,48 @@ var CustomAction = /** @class */ (function () {
|
|
|
62
38
|
this.actions = actionOptions.actions;
|
|
63
39
|
CustomAction.instances[this.name] = this;
|
|
64
40
|
}
|
|
65
|
-
|
|
41
|
+
static getInstance(name) {
|
|
66
42
|
if (!CustomAction.instances[name]) {
|
|
67
43
|
throw new TableInstanceNotFound_1.TableInstanceNotFound(name);
|
|
68
44
|
}
|
|
69
45
|
return CustomAction.instances[name];
|
|
70
|
-
}
|
|
71
|
-
|
|
46
|
+
}
|
|
47
|
+
getFields() {
|
|
72
48
|
return this.schema && this.schema.getFields();
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
context.result = result;
|
|
102
|
-
if (!CustomAction.hook) return [3 /*break*/, 4];
|
|
103
|
-
context.schemaName = this.name;
|
|
104
|
-
context.name = action;
|
|
105
|
-
return [4 /*yield*/, CustomAction.hook(_, args, context, info)];
|
|
106
|
-
case 3:
|
|
107
|
-
_a.sent();
|
|
108
|
-
_a.label = 4;
|
|
109
|
-
case 4: return [2 /*return*/, result];
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
}); };
|
|
49
|
+
}
|
|
50
|
+
getActions() {
|
|
51
|
+
const actions = this.actions || {};
|
|
52
|
+
let result = {};
|
|
53
|
+
Object.keys(actions).forEach((action) => {
|
|
54
|
+
result[action] = (_, args, context, info) => __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
const user = yield Mandarina_1.default.config.getUser(context);
|
|
56
|
+
const roles = (user && user.roles) || [];
|
|
57
|
+
const permissions = actions[action].permission || [];
|
|
58
|
+
if (permissions.includes('nobody')) {
|
|
59
|
+
throw new Error('Action not allowed');
|
|
60
|
+
}
|
|
61
|
+
if (permissions.includes('everybody') || permissions.some(permission => roles.includes(permission))) {
|
|
62
|
+
console.log('***************', `Action "${action}" not allowed for this user`, '***************');
|
|
63
|
+
console.warn(user && user.id, action);
|
|
64
|
+
console.warn(user && user.roles, permissions);
|
|
65
|
+
console.log('*****************************************************************************');
|
|
66
|
+
//throw new Error(`Action "${action}" not allowed for this user`)
|
|
67
|
+
}
|
|
68
|
+
const result = yield actions[action].action(_, args, context, info);
|
|
69
|
+
context.result = result;
|
|
70
|
+
if (CustomAction.hook) {
|
|
71
|
+
context.schemaName = this.name;
|
|
72
|
+
context.name = action;
|
|
73
|
+
yield CustomAction.hook(_, args, context, info);
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
});
|
|
113
77
|
});
|
|
114
78
|
return result;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
CustomAction.hook = hook;
|
|
118
|
-
};
|
|
119
|
-
return CustomAction;
|
|
120
|
-
}());
|
|
79
|
+
}
|
|
80
|
+
}
|
|
121
81
|
exports.CustomAction = CustomAction;
|
|
82
|
+
CustomAction.setGlobalHook = (hook) => {
|
|
83
|
+
CustomAction.hook = hook;
|
|
84
|
+
};
|
|
85
|
+
//# sourceMappingURL=CustomAction.js.map
|
|
@@ -1,24 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
extendStatics(d, b);
|
|
11
|
-
function __() { this.constructor = d; }
|
|
12
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
|
-
};
|
|
14
|
-
})();
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
15
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return _super.call(this, "The table " + schemaName + " was not instantiated") || this;
|
|
6
|
+
const BaseError_1 = __importDefault(require("mandarina/build/Errors/BaseError"));
|
|
7
|
+
class TableInstanceNotFound extends BaseError_1.default {
|
|
8
|
+
constructor(schemaName) {
|
|
9
|
+
super(`The table ${schemaName} was not instantiated`);
|
|
21
10
|
}
|
|
22
|
-
|
|
23
|
-
}(BaseError_1.default));
|
|
11
|
+
}
|
|
24
12
|
exports.TableInstanceNotFound = TableInstanceNotFound;
|
|
13
|
+
//# sourceMappingURL=TableInstanceNotFound.js.map
|
|
@@ -1,27 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
extendStatics(d, b);
|
|
11
|
-
function __() { this.constructor = d; }
|
|
12
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
13
|
-
};
|
|
14
|
-
})();
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
15
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
var name = typeof schemaName === "string" ? schemaName : schemaName.name;
|
|
22
|
-
_this = _super.call(this, "The action \"" + name + "\" already exists, action name should be unique.") || this;
|
|
23
|
-
return _this;
|
|
6
|
+
const BaseError_1 = __importDefault(require("mandarina/build/Errors/BaseError"));
|
|
7
|
+
class UniqueActionError extends BaseError_1.default {
|
|
8
|
+
constructor(schemaName) {
|
|
9
|
+
const name = typeof schemaName === "string" ? schemaName : schemaName.name;
|
|
10
|
+
super(`The action "${name}" already exists, action name should be unique.`);
|
|
24
11
|
}
|
|
25
|
-
|
|
26
|
-
}(BaseError_1.default));
|
|
12
|
+
}
|
|
27
13
|
exports.UniqueActionError = UniqueActionError;
|
|
14
|
+
//# sourceMappingURL=UniqueActionError.js.map
|
package/src/Mandarina.js
CHANGED
|
@@ -1,57 +1,41 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
Mandarina.load = function () {
|
|
21
|
-
var config = utils_1.getConfig();
|
|
3
|
+
const Table_1 = require("./Table/Table");
|
|
4
|
+
const CustomAction_1 = require("./Action/CustomAction");
|
|
5
|
+
const utils_1 = require("./cli/utils");
|
|
6
|
+
class Mandarina {
|
|
7
|
+
static load() {
|
|
8
|
+
const config = utils_1.getConfig();
|
|
22
9
|
utils_1.loadSchemas(config.dir);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
for (
|
|
27
|
-
|
|
28
|
-
Query =
|
|
10
|
+
}
|
|
11
|
+
static getQuery() {
|
|
12
|
+
let Query = {};
|
|
13
|
+
for (const tableName in Table_1.Table.instances) {
|
|
14
|
+
const table = Table_1.Table.getInstance(tableName);
|
|
15
|
+
Query = Object.assign(Object.assign({}, Query), table.getDefaultActions('query'));
|
|
29
16
|
}
|
|
30
17
|
return Query;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
for (
|
|
35
|
-
|
|
36
|
-
Mutation =
|
|
18
|
+
}
|
|
19
|
+
static getMutation() {
|
|
20
|
+
let Mutation = {};
|
|
21
|
+
for (const tableName in Table_1.Table.instances) {
|
|
22
|
+
const table = Table_1.Table.getInstance(tableName);
|
|
23
|
+
Mutation = Object.assign(Object.assign({}, Mutation), table.getDefaultActions('mutation'));
|
|
37
24
|
}
|
|
38
|
-
for (
|
|
39
|
-
|
|
40
|
-
Mutation =
|
|
25
|
+
for (const actionName in CustomAction_1.CustomAction.instances) {
|
|
26
|
+
const action = CustomAction_1.CustomAction.getInstance(actionName);
|
|
27
|
+
Mutation = Object.assign(Object.assign({}, Mutation), action.getActions());
|
|
41
28
|
}
|
|
42
29
|
return Mutation;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
getUser: function (_a) {
|
|
46
|
-
var user = _a.user;
|
|
47
|
-
return user;
|
|
48
|
-
},
|
|
49
|
-
};
|
|
50
|
-
Mandarina.configure = function (options) {
|
|
51
|
-
if (options.getUser) {
|
|
52
|
-
Mandarina.config.getUser = options.getUser;
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
return Mandarina;
|
|
56
|
-
}());
|
|
30
|
+
}
|
|
31
|
+
}
|
|
57
32
|
exports.default = Mandarina;
|
|
33
|
+
Mandarina.config = {
|
|
34
|
+
getUser: ({ user }) => user,
|
|
35
|
+
};
|
|
36
|
+
Mandarina.configure = (options) => {
|
|
37
|
+
if (options.getUser) {
|
|
38
|
+
Mandarina.config.getUser = options.getUser;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
//# sourceMappingURL=Mandarina.js.map
|
package/src/Table/Table.js
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -19,44 +8,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
19
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
9
|
});
|
|
21
10
|
};
|
|
22
|
-
var
|
|
23
|
-
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (_) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
48
13
|
};
|
|
49
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
15
|
+
const mandarina_1 = require("mandarina");
|
|
16
|
+
const UniqueSchemaError_1 = require("mandarina/build/Errors/UniqueSchemaError");
|
|
17
|
+
const SchemaInstanceNotFound_1 = require("mandarina/build/Errors/SchemaInstanceNotFound");
|
|
18
|
+
const utils_1 = require("mandarina/build/Schema/utils");
|
|
19
|
+
const MissingIDTableError_1 = require("mandarina/build/Errors/MissingIDTableError");
|
|
20
|
+
const Mandarina_1 = __importDefault(require("../Mandarina"));
|
|
21
|
+
const Mutate_1 = require("mandarina/build/Operations/Mutate");
|
|
22
|
+
const language_1 = require("graphql/language");
|
|
23
|
+
const stringify_object_1 = __importDefault(require("stringify-object"));
|
|
24
|
+
const lodash_1 = require("lodash");
|
|
60
25
|
// import {flatten, unflatten} from "flat";
|
|
61
26
|
/**
|
|
62
27
|
*
|
|
@@ -65,21 +30,16 @@ var lodash_1 = require("lodash");
|
|
|
65
30
|
* Form
|
|
66
31
|
* List of results
|
|
67
32
|
*/
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
this.insertWhereIntoInfo = function (info, user, isSingleMutation, action, operationName) {
|
|
72
|
-
if (isSingleMutation === void 0) { isSingleMutation = false; }
|
|
33
|
+
class Table {
|
|
34
|
+
constructor(schema, tableOptions) {
|
|
35
|
+
this.insertWhereIntoInfo = (info, user, isSingleMutation = false, action, operationName) => {
|
|
73
36
|
var _a, _b;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
})) || [];
|
|
81
|
-
var query = language_1.visit(info.operation, {
|
|
82
|
-
enter: function (node, key, parent, path, ancestors) {
|
|
37
|
+
const field = [];
|
|
38
|
+
const fields = new Set();
|
|
39
|
+
let required = false;
|
|
40
|
+
const allowedVariables = ((_b = (_a = info.fieldNodes[0]) === null || _a === void 0 ? void 0 : _a.arguments) === null || _b === void 0 ? void 0 : _b.map(({ name: { value } }) => value)) || [];
|
|
41
|
+
const query = language_1.visit(info.operation, {
|
|
42
|
+
enter: (node, key, parent, path, ancestors) => {
|
|
83
43
|
if (node.kind === 'VariableDefinition') {
|
|
84
44
|
if (!allowedVariables.includes(node === null || node === void 0 ? void 0 : node.variable.name.value)) {
|
|
85
45
|
return null;
|
|
@@ -90,31 +50,31 @@ var Table = /** @class */ (function () {
|
|
|
90
50
|
}
|
|
91
51
|
if (node.kind === 'Field' && node.name.value !== '__typename') {
|
|
92
52
|
field.push(node.name.value);
|
|
93
|
-
|
|
94
|
-
|
|
53
|
+
const internalField = field.slice(1).join('.');
|
|
54
|
+
let table;
|
|
95
55
|
if (internalField) {
|
|
96
56
|
fields.add(internalField);
|
|
97
|
-
|
|
57
|
+
const def = this.schema.getPathDefinition(internalField);
|
|
98
58
|
if (def.isTable && def.isArray) {
|
|
99
59
|
table = Table.instances[def.type];
|
|
100
60
|
}
|
|
101
61
|
}
|
|
102
62
|
else {
|
|
103
|
-
table =
|
|
63
|
+
table = this;
|
|
104
64
|
}
|
|
105
|
-
if (table && table.options.where && (!isSingleMutation && table ===
|
|
106
|
-
|
|
65
|
+
if (table && table.options.where && (!isSingleMutation && table === this)) {
|
|
66
|
+
const where = table.options.where(user, action, operationName);
|
|
107
67
|
if (!where || lodash_1.isEmpty(where))
|
|
108
68
|
return;
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if (originalWhereObj && table ===
|
|
69
|
+
const clone = Mutate_1.deepClone(node);
|
|
70
|
+
const originalWhereObj = clone.arguments ? clone.arguments.find((a) => a.name.value === 'where') : null;
|
|
71
|
+
let originalWhereString = '';
|
|
72
|
+
if (originalWhereObj && table === this) {
|
|
113
73
|
originalWhereString = language_1.print(originalWhereObj.value);
|
|
114
74
|
required = true;
|
|
115
75
|
}
|
|
116
|
-
|
|
117
|
-
|
|
76
|
+
const newWhereString = stringify_object_1.default(where, { singleQuotes: false });
|
|
77
|
+
let finalWhereString = originalWhereString ? `{AND:[${originalWhereString},${newWhereString}]}` : newWhereString;
|
|
118
78
|
if (originalWhereObj) {
|
|
119
79
|
originalWhereObj.value = language_1.parseValue(new language_1.Source(finalWhereString));
|
|
120
80
|
}
|
|
@@ -130,17 +90,17 @@ var Table = /** @class */ (function () {
|
|
|
130
90
|
}
|
|
131
91
|
return;
|
|
132
92
|
},
|
|
133
|
-
leave
|
|
93
|
+
leave(node) {
|
|
134
94
|
if (node.kind === 'Field' && node.name.value !== '__typename') {
|
|
135
95
|
field.pop();
|
|
136
96
|
}
|
|
137
97
|
}
|
|
138
98
|
});
|
|
139
|
-
|
|
99
|
+
let queryString = language_1.print(query);
|
|
140
100
|
if (required) {
|
|
141
101
|
queryString = queryString.replace(/\$where: (\w*)Input(,| |\))/, '$where: $1Input!$2');
|
|
142
102
|
}
|
|
143
|
-
return { fields: Array.from(fields), queryString
|
|
103
|
+
return { fields: Array.from(fields), queryString };
|
|
144
104
|
};
|
|
145
105
|
Table.instances = Table.instances || {};
|
|
146
106
|
this.schema = schema;
|
|
@@ -151,18 +111,18 @@ var Table = /** @class */ (function () {
|
|
|
151
111
|
if (Table.instances[this.name]) {
|
|
152
112
|
throw new UniqueSchemaError_1.UniqueSchemaError(this.name);
|
|
153
113
|
}
|
|
154
|
-
this.options =
|
|
114
|
+
this.options = Object.assign({}, tableOptions);
|
|
155
115
|
Table.instances[this.name] = this;
|
|
156
116
|
}
|
|
157
|
-
|
|
117
|
+
static getInstance(name) {
|
|
158
118
|
if (!Table.instances[name]) {
|
|
159
119
|
throw new SchemaInstanceNotFound_1.SchemaInstanceNotFound(name);
|
|
160
120
|
}
|
|
161
121
|
return Table.instances[name];
|
|
162
|
-
}
|
|
163
|
-
|
|
122
|
+
}
|
|
123
|
+
getFields() {
|
|
164
124
|
return this.schema.getFields();
|
|
165
|
-
}
|
|
125
|
+
}
|
|
166
126
|
/*
|
|
167
127
|
* It apply the fields permissions policy by action and roles, throw an exception if is not a valid request
|
|
168
128
|
*
|
|
@@ -182,114 +142,112 @@ var Table = /** @class */ (function () {
|
|
|
182
142
|
}
|
|
183
143
|
}
|
|
184
144
|
*/
|
|
185
|
-
|
|
186
|
-
|
|
145
|
+
shouldHasManyUpdate() {
|
|
146
|
+
const fields = this.schema.getFields().filter(f => f !== 'createdAt' && f !== 'createdAt');
|
|
187
147
|
return fields.length > 0;
|
|
188
|
-
}
|
|
148
|
+
}
|
|
189
149
|
//Insert where option in to the query
|
|
190
150
|
// static dotConcat = (a: string | undefined, b: string) => a ? `${a}.${b}` : b;
|
|
191
|
-
|
|
192
|
-
var _this = this;
|
|
151
|
+
getDefaultActions(type) {
|
|
193
152
|
// OperationName for query is user or users, for mutation are createUser, updateUser ....
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
operationNames.forEach(
|
|
197
|
-
if (!
|
|
153
|
+
const operationNames = Object.values(this.schema.names[type]);
|
|
154
|
+
const resultResolvers = {};
|
|
155
|
+
operationNames.forEach((operationName) => {
|
|
156
|
+
if (!this.shouldHasManyUpdate())
|
|
198
157
|
return;
|
|
199
|
-
resultResolvers[operationName] =
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
return [4 /*yield*/, context.prisma.exists[this.name](finalWhere)];
|
|
226
|
-
case 3:
|
|
227
|
-
exists = (_b.sent());
|
|
228
|
-
if (!exists) {
|
|
229
|
-
throw new Error(action + " on " + this.schema.name + " not found for " + JSON.stringify(where));
|
|
230
|
-
}
|
|
231
|
-
_b.label = 4;
|
|
232
|
-
case 4:
|
|
233
|
-
//VALIDATE IF USER CAN MUTATE THOSE FIELDS
|
|
234
|
-
this.schema.validateMutation(action, Mutate_1.deepClone(args), user && user.roles || []);
|
|
235
|
-
return [4 /*yield*/, this.callHook(this.name, "before" + capitalizedAction, _, args, context, info)];
|
|
236
|
-
case 5:
|
|
237
|
-
_b.sent();
|
|
238
|
-
return [4 /*yield*/, context.prisma.request(queryString, args)];
|
|
239
|
-
case 6:
|
|
240
|
-
data = (_b.sent());
|
|
241
|
-
if (data.errors) {
|
|
242
|
-
console.error(data.errors);
|
|
243
|
-
}
|
|
244
|
-
result = Object.values(data.data)[0];
|
|
245
|
-
context.result = result;
|
|
246
|
-
return [4 /*yield*/, this.callHook(this.name, "after" + capitalizedAction, _, args, context, info)];
|
|
247
|
-
case 7:
|
|
248
|
-
_b.sent();
|
|
249
|
-
this.schema.validateQuery(fields, user && user.roles || []);
|
|
250
|
-
_b.label = 8;
|
|
251
|
-
case 8:
|
|
252
|
-
if (!(type === 'query')) return [3 /*break*/, 12];
|
|
253
|
-
// console.dir(JSON.parse(JSON.stringify(info)),{depth:1})
|
|
254
|
-
return [4 /*yield*/, this.callHook(this.name, 'beforeQuery', _, args, context, info)];
|
|
255
|
-
case 9:
|
|
256
|
-
// console.dir(JSON.parse(JSON.stringify(info)),{depth:1})
|
|
257
|
-
_b.sent();
|
|
258
|
-
if (!!info.fieldName.match(/Connection$/)) {
|
|
259
|
-
this.schema.validateConnection(user && user.roles || []);
|
|
260
|
-
}
|
|
261
|
-
else {
|
|
262
|
-
this.schema.validateQuery(fields, user && user.roles || []);
|
|
263
|
-
}
|
|
264
|
-
//Validate if the roles is able to read those fields
|
|
265
|
-
if (operationName === this.schema.names.query.single) {
|
|
266
|
-
queryString = queryString.replace(operationName, this.schema.names.query.plural);
|
|
267
|
-
queryString = queryString.replace(new RegExp(this.schema.names.input.where.single + "!?"), this.schema.names.input.where.plural + '!');
|
|
268
|
-
}
|
|
269
|
-
return [4 /*yield*/, context.prisma.request(queryString, args)];
|
|
270
|
-
case 10:
|
|
271
|
-
data = (_b.sent());
|
|
272
|
-
if (data.errors) {
|
|
273
|
-
console.error(data.errors);
|
|
274
|
-
}
|
|
275
|
-
result = Object.values(data.data)[0];
|
|
276
|
-
if (operationName === this.schema.names.query.single) {
|
|
277
|
-
result = data.data[this.schema.names.query.plural];
|
|
278
|
-
result = result && result.length === 1 ? result[0] : null;
|
|
279
|
-
}
|
|
280
|
-
context.result = result;
|
|
281
|
-
return [4 /*yield*/, this.callHook(this.name, 'afterQuery', _, args, context, info)];
|
|
282
|
-
case 11:
|
|
283
|
-
_b.sent();
|
|
284
|
-
_b.label = 12;
|
|
285
|
-
case 12: return [2 /*return*/, result];
|
|
158
|
+
resultResolvers[operationName] = (_, args = {}, context, info) => __awaiter(this, void 0, void 0, function* () {
|
|
159
|
+
context.originalArgs = args;
|
|
160
|
+
const user = yield Mandarina_1.default.config.getUser(context);
|
|
161
|
+
const subOperationName = operationName.substr(0, 6);
|
|
162
|
+
const action = (['create', 'update', 'delete'].includes(subOperationName) ? subOperationName : 'read');
|
|
163
|
+
context.action = action;
|
|
164
|
+
let result;
|
|
165
|
+
const capitalizedAction = utils_1.capitalize(action);
|
|
166
|
+
context.operationName = operationName;
|
|
167
|
+
yield this.callHook(this.name, 'beforeValidate', _, args, context, info);
|
|
168
|
+
const isSingleMutation = operationName === this.schema.names.mutation.update || operationName === this.schema.names.mutation.create;
|
|
169
|
+
let { queryString, fields } = this.insertWhereIntoInfo(info, user, isSingleMutation, action, operationName);
|
|
170
|
+
if (type === 'mutation') {
|
|
171
|
+
// if (errors.length > 0) {
|
|
172
|
+
// await this.callHook('validationFailed', action, _, args, context, info);
|
|
173
|
+
// } else {
|
|
174
|
+
// await this.callHook('afterValidate', action, _, args, context, info);
|
|
175
|
+
// }
|
|
176
|
+
if (isSingleMutation && (action === 'update' || action === 'delete')) {
|
|
177
|
+
const where = this.options.where && this.options.where(user, action, operationName);
|
|
178
|
+
if (where) {
|
|
179
|
+
let finalWhere = args.where ? { AND: [args.where, where] } : where;
|
|
180
|
+
const exists = (yield context.prisma.exists[this.name](finalWhere));
|
|
181
|
+
if (!exists) {
|
|
182
|
+
throw new Error(`${action} on ${this.schema.name} not found for ${JSON.stringify(where)}`);
|
|
183
|
+
}
|
|
286
184
|
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
|
|
185
|
+
}
|
|
186
|
+
//VALIDATE IF USER CAN MUTATE THOSE FIELDS
|
|
187
|
+
this.schema.validateMutation(action, Mutate_1.deepClone(args), user && user.roles || []);
|
|
188
|
+
yield this.callHook(this.name, `before${capitalizedAction}`, _, args, context, info);
|
|
189
|
+
/*
|
|
190
|
+
HACK https://github.com/prisma/prisma/issues/4327
|
|
191
|
+
*/
|
|
192
|
+
// if (`before${capitalizedAction}` === 'beforeUpdate' || `before${capitalizedAction}` === 'beforeCreate') {
|
|
193
|
+
// const flat = flatten(args.data)
|
|
194
|
+
// const where = args.where
|
|
195
|
+
// let run = false
|
|
196
|
+
// let withDeleteMany: any = {}
|
|
197
|
+
// let withoutDeleteMany: any = {}
|
|
198
|
+
// Object.keys(flat).forEach((key) => {
|
|
199
|
+
// if (key.match(/\.deleteMany\.0$/)) {
|
|
200
|
+
// run = true
|
|
201
|
+
// withDeleteMany[key] = flat[key]
|
|
202
|
+
// } else {
|
|
203
|
+
// withoutDeleteMany[key] = flat[key]
|
|
204
|
+
// }
|
|
205
|
+
// })
|
|
206
|
+
// if (run) {
|
|
207
|
+
// await prismamethod({where, data: unflatten(withdeletemany)}, info);
|
|
208
|
+
// args.data = unflatten(withoutDeleteMany)
|
|
209
|
+
// }
|
|
210
|
+
// }
|
|
211
|
+
const data = (yield context.prisma.request(queryString, args));
|
|
212
|
+
if (data.errors) {
|
|
213
|
+
console.error(data.errors);
|
|
214
|
+
}
|
|
215
|
+
result = Object.values(data.data)[0];
|
|
216
|
+
context.result = result;
|
|
217
|
+
yield this.callHook(this.name, `after${capitalizedAction}`, _, args, context, info);
|
|
218
|
+
this.schema.validateQuery(fields, user && user.roles || []);
|
|
219
|
+
}
|
|
220
|
+
if (type === 'query') {
|
|
221
|
+
// console.dir(JSON.parse(JSON.stringify(info)),{depth:1})
|
|
222
|
+
yield this.callHook(this.name, 'beforeQuery', _, args, context, info);
|
|
223
|
+
if (!!info.fieldName.match(/Connection$/)) {
|
|
224
|
+
this.schema.validateConnection(user && user.roles || []);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
this.schema.validateQuery(fields, user && user.roles || []);
|
|
228
|
+
}
|
|
229
|
+
//Validate if the roles is able to read those fields
|
|
230
|
+
if (operationName === this.schema.names.query.single) {
|
|
231
|
+
queryString = queryString.replace(operationName, this.schema.names.query.plural);
|
|
232
|
+
queryString = queryString.replace(new RegExp(`${this.schema.names.input.where.single}!?`), this.schema.names.input.where.plural + '!');
|
|
233
|
+
}
|
|
234
|
+
const data = (yield context.prisma.request(queryString, args));
|
|
235
|
+
if (data.errors) {
|
|
236
|
+
console.error(data.errors);
|
|
237
|
+
}
|
|
238
|
+
result = Object.values(data.data)[0];
|
|
239
|
+
if (operationName === this.schema.names.query.single) {
|
|
240
|
+
result = data.data[this.schema.names.query.plural];
|
|
241
|
+
result = result && result.length === 1 ? result[0] : null;
|
|
242
|
+
}
|
|
243
|
+
context.result = result;
|
|
244
|
+
yield this.callHook(this.name, 'afterQuery', _, args, context, info);
|
|
245
|
+
}
|
|
246
|
+
return result;
|
|
247
|
+
});
|
|
290
248
|
});
|
|
291
249
|
return resultResolvers;
|
|
292
|
-
}
|
|
250
|
+
}
|
|
293
251
|
/**
|
|
294
252
|
* Go back a mutation object to the original object
|
|
295
253
|
*/
|
|
@@ -303,114 +261,80 @@ var Table = /** @class */ (function () {
|
|
|
303
261
|
* @param context
|
|
304
262
|
* @param info
|
|
305
263
|
*/
|
|
306
|
-
|
|
307
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
264
|
+
callHook(schemaName, name, _, args, context, info) {
|
|
265
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
266
|
+
try {
|
|
267
|
+
let prefix = '';
|
|
268
|
+
if (name.indexOf('before') === 0)
|
|
269
|
+
prefix = 'before';
|
|
270
|
+
if (name.indexOf('after') === 0)
|
|
271
|
+
prefix = 'after';
|
|
272
|
+
const globalHookHandler = Table.hooks[name];
|
|
273
|
+
const hookHandler = this.options.hooks && this.options.hooks[name];
|
|
274
|
+
if (args.data && prefix) {
|
|
275
|
+
const fields = Object.keys(args.data);
|
|
276
|
+
const schema = mandarina_1.Schema.getInstance(schemaName);
|
|
277
|
+
for (const field of fields) {
|
|
278
|
+
const def = schema.getPathDefinition(field);
|
|
279
|
+
const inline = !!(def.table && def.table.relation && def.table.relation.link === 'INLINE');
|
|
280
|
+
if (def.isTable) {
|
|
281
|
+
const operations = Object.keys(args.data[field]);
|
|
282
|
+
if (!Table.instances[def.type]) {
|
|
283
|
+
//console.warn(`No table for ${def.type} no neasted hooks applied`)
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const table = Table.getInstance(def.type);
|
|
287
|
+
for (const operation of operations) {
|
|
288
|
+
const hookName = `${prefix}${utils_1.capitalize(operation)}`;
|
|
289
|
+
const args2 = args.data[field][operation];
|
|
290
|
+
if (Array.isArray(args2)) {
|
|
291
|
+
for (const arg2 of args2) {
|
|
292
|
+
if (inline) {
|
|
293
|
+
yield table.callHook(def.type, hookName, _, arg2.data ? arg2 : { data: arg2 }, context, info);
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
yield table.callHook(def.type, hookName, _, arg2.data ? arg2 : { data: arg2 }, context, info);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
if (inline) {
|
|
302
|
+
yield table.callHook(def.type, hookName, _, args2.data ? args2 : { data: args2 }, context, info);
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
yield table.callHook(def.type, hookName, _, args2.data ? args2 : { data: args2 }, context, info);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
335
309
|
}
|
|
336
|
-
|
|
337
|
-
_a = 0, operations_1 = operations;
|
|
338
|
-
_c.label = 2;
|
|
339
|
-
case 2:
|
|
340
|
-
if (!(_a < operations_1.length)) return [3 /*break*/, 14];
|
|
341
|
-
operation = operations_1[_a];
|
|
342
|
-
hookName = "" + prefix + utils_1.capitalize(operation);
|
|
343
|
-
args2 = args.data[field][operation];
|
|
344
|
-
if (!Array.isArray(args2)) return [3 /*break*/, 9];
|
|
345
|
-
_b = 0, args2_1 = args2;
|
|
346
|
-
_c.label = 3;
|
|
347
|
-
case 3:
|
|
348
|
-
if (!(_b < args2_1.length)) return [3 /*break*/, 8];
|
|
349
|
-
arg2 = args2_1[_b];
|
|
350
|
-
if (!inline) return [3 /*break*/, 5];
|
|
351
|
-
return [4 /*yield*/, table.callHook(def.type, hookName, _, arg2.data ? arg2 : { data: arg2 }, context, info)];
|
|
352
|
-
case 4:
|
|
353
|
-
_c.sent();
|
|
354
|
-
return [3 /*break*/, 7];
|
|
355
|
-
case 5: return [4 /*yield*/, table.callHook(def.type, hookName, _, arg2.data ? arg2 : { data: arg2 }, context, info)];
|
|
356
|
-
case 6:
|
|
357
|
-
_c.sent();
|
|
358
|
-
_c.label = 7;
|
|
359
|
-
case 7:
|
|
360
|
-
_b++;
|
|
361
|
-
return [3 /*break*/, 3];
|
|
362
|
-
case 8: return [3 /*break*/, 13];
|
|
363
|
-
case 9:
|
|
364
|
-
if (!inline) return [3 /*break*/, 11];
|
|
365
|
-
return [4 /*yield*/, table.callHook(def.type, hookName, _, args2.data ? args2 : { data: args2 }, context, info)];
|
|
366
|
-
case 10:
|
|
367
|
-
_c.sent();
|
|
368
|
-
return [3 /*break*/, 13];
|
|
369
|
-
case 11: return [4 /*yield*/, table.callHook(def.type, hookName, _, args2.data ? args2 : { data: args2 }, context, info)];
|
|
370
|
-
case 12:
|
|
371
|
-
_c.sent();
|
|
372
|
-
_c.label = 13;
|
|
373
|
-
case 13:
|
|
374
|
-
_a++;
|
|
375
|
-
return [3 /*break*/, 2];
|
|
376
|
-
case 14:
|
|
377
|
-
_i++;
|
|
378
|
-
return [3 /*break*/, 1];
|
|
379
|
-
case 15:
|
|
380
|
-
if (!globalHookHandler) return [3 /*break*/, 17];
|
|
381
|
-
context.schemaName = schemaName;
|
|
382
|
-
context.name = name;
|
|
383
|
-
return [4 /*yield*/, globalHookHandler(_, args, context, info)];
|
|
384
|
-
case 16:
|
|
385
|
-
_c.sent();
|
|
386
|
-
_c.label = 17;
|
|
387
|
-
case 17:
|
|
388
|
-
if (!hookHandler) return [3 /*break*/, 19];
|
|
389
|
-
return [4 /*yield*/, hookHandler(_, args, context, info)];
|
|
390
|
-
case 18:
|
|
391
|
-
_c.sent();
|
|
392
|
-
_c.label = 19;
|
|
393
|
-
case 19: return [3 /*break*/, 21];
|
|
394
|
-
case 20:
|
|
395
|
-
e_1 = _c.sent();
|
|
396
|
-
console.error("Error executing hook: \"" + name + "\" in Table: " + schemaName + "\"");
|
|
397
|
-
console.error(e_1);
|
|
398
|
-
throw e_1;
|
|
399
|
-
case 21: return [2 /*return*/];
|
|
310
|
+
}
|
|
400
311
|
}
|
|
401
|
-
|
|
312
|
+
if (globalHookHandler) {
|
|
313
|
+
context.schemaName = schemaName;
|
|
314
|
+
context.name = name;
|
|
315
|
+
yield globalHookHandler(_, args, context, info);
|
|
316
|
+
}
|
|
317
|
+
if (hookHandler) {
|
|
318
|
+
yield hookHandler(_, args, context, info);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
catch (e) {
|
|
322
|
+
console.error(`Error executing hook: "${name}" in Table: ${schemaName}"`);
|
|
323
|
+
console.error(e);
|
|
324
|
+
throw e;
|
|
325
|
+
}
|
|
402
326
|
});
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
Table.setGlobalHooks = function (hooks) {
|
|
406
|
-
Table.hooks = hooks;
|
|
407
|
-
};
|
|
408
|
-
return Table;
|
|
409
|
-
}());
|
|
327
|
+
}
|
|
328
|
+
}
|
|
410
329
|
exports.Table = Table;
|
|
330
|
+
Table.hooks = {};
|
|
331
|
+
Table.setGlobalHooks = (hooks) => {
|
|
332
|
+
Table.hooks = hooks;
|
|
333
|
+
};
|
|
411
334
|
// let time = new Date().getTime()
|
|
412
335
|
// export function bm(description = '',...args:any) {
|
|
413
336
|
// description && console.info(description,...args, new Date().getTime() - time)
|
|
414
337
|
// time = new Date().getTime()
|
|
415
338
|
//
|
|
416
339
|
// }
|
|
340
|
+
//# sourceMappingURL=Table.js.map
|