@spinajs/orm-api 2.0.131 → 2.0.133
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/lib/cjs/config/orm-http.js +1 -1
- package/lib/cjs/controllers/crud/Create.d.ts +16 -0
- package/lib/cjs/controllers/crud/Create.d.ts.map +1 -0
- package/lib/cjs/controllers/crud/Create.js +145 -0
- package/lib/cjs/controllers/crud/Create.js.map +1 -0
- package/lib/cjs/controllers/crud/Crud.d.ts +13 -0
- package/lib/cjs/controllers/crud/Crud.d.ts.map +1 -0
- package/lib/cjs/controllers/crud/Crud.js +22 -0
- package/lib/cjs/controllers/crud/Crud.js.map +1 -0
- package/lib/cjs/controllers/crud/Delete.d.ts +16 -0
- package/lib/cjs/controllers/crud/Delete.d.ts.map +1 -0
- package/lib/cjs/controllers/crud/Delete.js +158 -0
- package/lib/cjs/controllers/crud/Delete.js.map +1 -0
- package/lib/cjs/controllers/crud/Read.d.ts +35 -0
- package/lib/cjs/controllers/crud/Read.d.ts.map +1 -0
- package/lib/cjs/controllers/crud/Read.js +230 -0
- package/lib/cjs/controllers/crud/Read.js.map +1 -0
- package/lib/cjs/controllers/crud/Update.d.ts +13 -0
- package/lib/cjs/controllers/crud/Update.d.ts.map +1 -0
- package/lib/cjs/controllers/crud/Update.js +99 -0
- package/lib/cjs/controllers/crud/Update.js.map +1 -0
- package/lib/mjs/config/orm-http.js +1 -1
- package/lib/mjs/controllers/crud/Create.d.ts +16 -0
- package/lib/mjs/controllers/crud/Create.d.ts.map +1 -0
- package/lib/mjs/controllers/crud/Create.js +139 -0
- package/lib/mjs/controllers/crud/Create.js.map +1 -0
- package/lib/mjs/controllers/crud/Crud.d.ts +13 -0
- package/lib/mjs/controllers/crud/Crud.d.ts.map +1 -0
- package/lib/mjs/controllers/crud/Crud.js +18 -0
- package/lib/mjs/controllers/crud/Crud.js.map +1 -0
- package/lib/mjs/controllers/crud/Delete.d.ts +16 -0
- package/lib/mjs/controllers/crud/Delete.d.ts.map +1 -0
- package/lib/mjs/controllers/crud/Delete.js +155 -0
- package/lib/mjs/controllers/crud/Delete.js.map +1 -0
- package/lib/mjs/controllers/crud/Read.d.ts +35 -0
- package/lib/mjs/controllers/crud/Read.d.ts.map +1 -0
- package/lib/mjs/controllers/crud/Read.js +227 -0
- package/lib/mjs/controllers/crud/Read.js.map +1 -0
- package/lib/mjs/controllers/crud/Update.d.ts +13 -0
- package/lib/mjs/controllers/crud/Update.d.ts.map +1 -0
- package/lib/mjs/controllers/crud/Update.js +96 -0
- package/lib/mjs/controllers/crud/Update.js.map +1 -0
- package/lib/tsconfig.cjs.tsbuildinfo +1 -1
- package/lib/tsconfig.mjs.tsbuildinfo +1 -1
- package/package.json +17 -18
- package/lib/cjs/controllers/Collections.d.ts +0 -33
- package/lib/cjs/controllers/Collections.d.ts.map +0 -1
- package/lib/cjs/controllers/Collections.js +0 -417
- package/lib/cjs/controllers/Collections.js.map +0 -1
- package/lib/mjs/controllers/Collections.d.ts +0 -33
- package/lib/mjs/controllers/Collections.d.ts.map +0 -1
- package/lib/mjs/controllers/Collections.js +0 -414
- package/lib/mjs/controllers/Collections.js.map +0 -1
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.CrudRead = void 0;
|
|
16
|
+
const orm_1 = require("@spinajs/orm");
|
|
17
|
+
const http_1 = require("@spinajs/http");
|
|
18
|
+
const rbac_http_1 = require("@spinajs/rbac-http");
|
|
19
|
+
const rbac_1 = require("@spinajs/rbac");
|
|
20
|
+
const di_1 = require("@spinajs/di");
|
|
21
|
+
const exceptions_1 = require("@spinajs/exceptions");
|
|
22
|
+
const log_1 = require("@spinajs/log");
|
|
23
|
+
require("@spinajs/rbac");
|
|
24
|
+
const ModelType_js_1 = require("../../route-args/ModelType.js");
|
|
25
|
+
const QueryArgs_js_1 = require("../../dto/QueryArgs.js");
|
|
26
|
+
const QueryFilter_js_1 = require("../../dto/QueryFilter.js");
|
|
27
|
+
const QueryIncludes_js_1 = require("../../dto/QueryIncludes.js");
|
|
28
|
+
const Crud_js_1 = require("./Crud.js");
|
|
29
|
+
const FindModelType_js_1 = require("../../policies/FindModelType.js");
|
|
30
|
+
let CrudRead = class CrudRead extends Crud_js_1.Crud {
|
|
31
|
+
checkAny(resource, user) {
|
|
32
|
+
// if we have user
|
|
33
|
+
if (user) {
|
|
34
|
+
return this.Ac.can(user.Role).readAny(resource);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
// if not try guest account
|
|
38
|
+
return this.Ac.can('guest').readAny(resource);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
checkOwn(resource, user) {
|
|
42
|
+
// if we have user
|
|
43
|
+
if (user) {
|
|
44
|
+
return this.Ac.can(user.Role).readOwn(resource);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
// if not try guest account
|
|
48
|
+
return this.Ac.can('guest').readOwn(resource);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
*
|
|
53
|
+
* Creates query on model and ansures that user have proper permission to access it
|
|
54
|
+
*
|
|
55
|
+
* @param model model to query against
|
|
56
|
+
* @param user user for permission check
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
getSafeQuery(model, user) {
|
|
60
|
+
const descriptor = this.getModelDescriptor(model);
|
|
61
|
+
const query = model.query();
|
|
62
|
+
// retrieve resource name, if model does not have @OrmResource decorator set
|
|
63
|
+
// model name is default resource name
|
|
64
|
+
let resource = descriptor.RbacResource ? descriptor.RbacResource : descriptor.Name;
|
|
65
|
+
let permission = this.checkAny(resource, user);
|
|
66
|
+
if (!permission.granted) {
|
|
67
|
+
// permission for readAny not granted, check for own
|
|
68
|
+
permission = this.checkOwn(resource, user);
|
|
69
|
+
if (permission.granted && user) {
|
|
70
|
+
if (model.ensureOwnership) {
|
|
71
|
+
model.ensureOwnership(query, user, descriptor);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
throw new exceptions_1.UnexpectedServerError(`Resource ${resource} does not have ensureOwnership method implemented`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (!permission.granted) {
|
|
78
|
+
throw new exceptions_1.Forbidden(`You do not access to ${resource}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { query, permission };
|
|
82
|
+
}
|
|
83
|
+
async getAll(model, getParams, filters, includes, user) {
|
|
84
|
+
const { query, permission } = this.getSafeQuery(model, user);
|
|
85
|
+
query
|
|
86
|
+
.where(filters)
|
|
87
|
+
.select('*')
|
|
88
|
+
.populate(includes)
|
|
89
|
+
.order(getParams.order, getParams.orderDirection ?? orm_1.SortOrder.ASC)
|
|
90
|
+
.skip(getParams.page * getParams.perPage ?? 0)
|
|
91
|
+
.take(getParams.perPage ?? 10);
|
|
92
|
+
const count = await query.clone().clearColumns().count('*', 'count').takeFirst().asRaw();
|
|
93
|
+
const result = await query;
|
|
94
|
+
return new http_1.Ok({
|
|
95
|
+
Data: result.map((x) => x.dehydrateWithRelations()).map((x) => permission.filter(x)),
|
|
96
|
+
Total: count.count,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async get(user, model, id, includes) {
|
|
100
|
+
const descriptor = this.getModelDescriptor(model);
|
|
101
|
+
const { query, permission } = this.getSafeQuery(model, user);
|
|
102
|
+
const result = await query
|
|
103
|
+
.select('*')
|
|
104
|
+
.where(descriptor.PrimaryKey, id)
|
|
105
|
+
.populate(includes)
|
|
106
|
+
.firstOrThrow(new exceptions_1.ResourceNotFound(`Record with id ${id} not found`, {
|
|
107
|
+
Resource: model.name,
|
|
108
|
+
[descriptor.PrimaryKey]: id,
|
|
109
|
+
}));
|
|
110
|
+
return new http_1.Ok(permission.filter(result.dehydrateWithRelations()));
|
|
111
|
+
}
|
|
112
|
+
async getRelation(model, id, relation, relationId, includes, user) {
|
|
113
|
+
const mDescriptor = this.getModelDescriptor(model);
|
|
114
|
+
const rDescriptor = this.getRelationDescriptor(model, relation);
|
|
115
|
+
const rmDescriptor = this.getModelDescriptor(rDescriptor.TargetModel);
|
|
116
|
+
const { query, permission } = this.getSafeQuery(rDescriptor.TargetModel, user);
|
|
117
|
+
if (!mDescriptor.Relations.has(relation)) {
|
|
118
|
+
return new exceptions_1.BadRequest(`Resource ${mDescriptor.Name} does not have relation ${relation} defined`);
|
|
119
|
+
}
|
|
120
|
+
const pExists = await model.exists(id);
|
|
121
|
+
if (!pExists) {
|
|
122
|
+
throw new exceptions_1.ResourceNotFound(`Record with id ${id} not found`, {
|
|
123
|
+
Resource: model.name,
|
|
124
|
+
[mDescriptor.PrimaryKey]: id,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
const result = await query
|
|
128
|
+
.select('*')
|
|
129
|
+
.populate(includes)
|
|
130
|
+
.where(rmDescriptor.PrimaryKey, relationId)
|
|
131
|
+
.firstOrThrow(new exceptions_1.ResourceNotFound(`Record with id ${relationId} not found`, {
|
|
132
|
+
Resource: rDescriptor.TargetModel.name,
|
|
133
|
+
[rmDescriptor.PrimaryKey]: relationId,
|
|
134
|
+
}));
|
|
135
|
+
return new http_1.Ok(permission.filter(result.dehydrateWithRelations()));
|
|
136
|
+
}
|
|
137
|
+
async getRelations(model, id, relation, params, filters, includes, user) {
|
|
138
|
+
const mDescriptor = this.getModelDescriptor(model);
|
|
139
|
+
const rDescriptor = this.getRelationDescriptor(model, relation);
|
|
140
|
+
const { query, permission } = this.getSafeQuery(rDescriptor.TargetModel, user);
|
|
141
|
+
if (!mDescriptor.Relations.has(relation)) {
|
|
142
|
+
return new exceptions_1.BadRequest(`Resource ${mDescriptor.Name} does not have relation ${relation} defined`);
|
|
143
|
+
}
|
|
144
|
+
const pExists = await model.exists(id);
|
|
145
|
+
if (!pExists) {
|
|
146
|
+
throw new exceptions_1.ResourceNotFound(`Record with id ${id} not found`, {
|
|
147
|
+
Resource: model.name,
|
|
148
|
+
[mDescriptor.PrimaryKey]: id,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
query
|
|
152
|
+
.where(filters)
|
|
153
|
+
.where(rDescriptor.ForeignKey, id)
|
|
154
|
+
.select('*')
|
|
155
|
+
.populate(includes)
|
|
156
|
+
.order(params.order, params.orderDirection ?? orm_1.SortOrder.ASC)
|
|
157
|
+
.skip(params.page * params.perPage ?? 0)
|
|
158
|
+
.take(params.perPage ?? 10);
|
|
159
|
+
const count = await query.clone().clearColumns().count('*', 'count').takeFirst().asRaw();
|
|
160
|
+
const result = await query;
|
|
161
|
+
return new http_1.Ok({
|
|
162
|
+
Data: result.map((x) => x.dehydrateWithRelations()).map((x) => permission.filter(x)),
|
|
163
|
+
Total: count,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
__decorate([
|
|
168
|
+
(0, log_1.Logger)('orm-http:api'),
|
|
169
|
+
__metadata("design:type", log_1.Log)
|
|
170
|
+
], CrudRead.prototype, "Log", void 0);
|
|
171
|
+
__decorate([
|
|
172
|
+
(0, di_1.Autoinject)(),
|
|
173
|
+
__metadata("design:type", orm_1.Orm)
|
|
174
|
+
], CrudRead.prototype, "Orm", void 0);
|
|
175
|
+
__decorate([
|
|
176
|
+
(0, di_1.Autoinject)(),
|
|
177
|
+
__metadata("design:type", rbac_1.AccessControl)
|
|
178
|
+
], CrudRead.prototype, "Ac", void 0);
|
|
179
|
+
__decorate([
|
|
180
|
+
(0, http_1.Get)(':model'),
|
|
181
|
+
__param(0, (0, ModelType_js_1.ModelType)()),
|
|
182
|
+
__param(1, (0, http_1.Query)()),
|
|
183
|
+
__param(2, (0, http_1.Query)()),
|
|
184
|
+
__param(3, (0, http_1.Query)()),
|
|
185
|
+
__param(4, (0, rbac_http_1.User)()),
|
|
186
|
+
__metadata("design:type", Function),
|
|
187
|
+
__metadata("design:paramtypes", [Object, QueryArgs_js_1.QueryArgs, QueryFilter_js_1.QueryFilter, QueryIncludes_js_1.QueryIncludes, rbac_1.User]),
|
|
188
|
+
__metadata("design:returntype", Promise)
|
|
189
|
+
], CrudRead.prototype, "getAll", null);
|
|
190
|
+
__decorate([
|
|
191
|
+
(0, http_1.Get)(':model/:id'),
|
|
192
|
+
__param(0, (0, rbac_http_1.User)()),
|
|
193
|
+
__param(1, (0, ModelType_js_1.ModelType)()),
|
|
194
|
+
__param(2, (0, http_1.Param)()),
|
|
195
|
+
__param(3, (0, http_1.Query)()),
|
|
196
|
+
__metadata("design:type", Function),
|
|
197
|
+
__metadata("design:paramtypes", [rbac_1.User, Object, Number, QueryIncludes_js_1.QueryIncludes]),
|
|
198
|
+
__metadata("design:returntype", Promise)
|
|
199
|
+
], CrudRead.prototype, "get", null);
|
|
200
|
+
__decorate([
|
|
201
|
+
(0, http_1.Get)(':model/:id/:relation/:relationId'),
|
|
202
|
+
__param(0, (0, ModelType_js_1.ModelType)()),
|
|
203
|
+
__param(1, (0, http_1.Param)()),
|
|
204
|
+
__param(2, (0, http_1.Param)()),
|
|
205
|
+
__param(3, (0, http_1.Param)()),
|
|
206
|
+
__param(4, (0, http_1.Query)()),
|
|
207
|
+
__param(5, (0, rbac_http_1.User)()),
|
|
208
|
+
__metadata("design:type", Function),
|
|
209
|
+
__metadata("design:paramtypes", [Object, Object, String, Object, QueryIncludes_js_1.QueryIncludes, rbac_1.User]),
|
|
210
|
+
__metadata("design:returntype", Promise)
|
|
211
|
+
], CrudRead.prototype, "getRelation", null);
|
|
212
|
+
__decorate([
|
|
213
|
+
(0, http_1.Get)(':model/:id/:relation'),
|
|
214
|
+
__param(0, (0, ModelType_js_1.ModelType)()),
|
|
215
|
+
__param(1, (0, http_1.Param)()),
|
|
216
|
+
__param(2, (0, http_1.Param)()),
|
|
217
|
+
__param(3, (0, http_1.Query)()),
|
|
218
|
+
__param(4, (0, http_1.Query)()),
|
|
219
|
+
__param(5, (0, http_1.Query)()),
|
|
220
|
+
__param(6, (0, rbac_http_1.User)()),
|
|
221
|
+
__metadata("design:type", Function),
|
|
222
|
+
__metadata("design:paramtypes", [Object, Object, String, QueryArgs_js_1.QueryArgs, QueryFilter_js_1.QueryFilter, QueryIncludes_js_1.QueryIncludes, rbac_1.User]),
|
|
223
|
+
__metadata("design:returntype", Promise)
|
|
224
|
+
], CrudRead.prototype, "getRelations", null);
|
|
225
|
+
CrudRead = __decorate([
|
|
226
|
+
(0, http_1.BasePath)('crud'),
|
|
227
|
+
(0, http_1.Policy)(FindModelType_js_1.FindModelType)
|
|
228
|
+
], CrudRead);
|
|
229
|
+
exports.CrudRead = CrudRead;
|
|
230
|
+
//# sourceMappingURL=Read.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Read.js","sourceRoot":"","sources":["../../../../src/controllers/crud/Read.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,sCAA4D;AAC5D,wCAAwE;AACxE,kDAA0C;AAC1C,wCAAuF;AACvF,oCAAyC;AACzC,oDAAqG;AACrG,sCAA2C;AAC3C,yBAAuB;AAGvB,gEAA0D;AAC1D,yDAA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AAK,GAAE,CAAA;IAAmB,WAAA,IAAA,YAAK,GAAE,CAAA;IAA2B,WAAA,IAAA,gBAAI,GAAE,CAAA;;qEAAtB,gCAAa,EAAiB,WAAS;;2CA8B3L;AAGY;IADZ,IAAA,UAAG,EAAC,sBAAsB,CAAC;IACD,WAAA,IAAA,wBAAS,GAAE,CAAA;IAAuB,WAAA,IAAA,YAAK,GAAE,CAAA;IAAW,WAAA,IAAA,YAAK,GAAE,CAAA;IAAoB,WAAA,IAAA,YAAK,GAAE,CAAA;IAAqB,WAAA,IAAA,YAAK,GAAE,CAAA;IAAwB,WAAA,IAAA,YAAK,GAAE,CAAA;IAA2B,WAAA,IAAA,gBAAI,GAAE,CAAA;;6DAAnF,wBAAS,EAAoB,4BAAW,EAAqB,gCAAa,EAAiB,WAAS;;4CAiC7N;AA3KU,QAAQ;IAFpB,IAAA,eAAQ,EAAC,MAAM,CAAC;IAChB,IAAA,aAAM,EAAC,gCAAa,CAAC;GACT,QAAQ,CA4KpB;AA5KY,4BAAQ"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IModelStatic, Orm } from '@spinajs/orm';
|
|
2
|
+
import { Ok } from '@spinajs/http';
|
|
3
|
+
import { Log } from '@spinajs/log';
|
|
4
|
+
import { AccessControl } from '@spinajs/rbac';
|
|
5
|
+
import { Crud } from './Crud.js';
|
|
6
|
+
export declare class CrudUpdate extends Crud {
|
|
7
|
+
protected Log: Log;
|
|
8
|
+
protected Orm: Orm;
|
|
9
|
+
protected Ac: AccessControl;
|
|
10
|
+
update(model: IModelStatic, id: number, data: unknown): Promise<Ok>;
|
|
11
|
+
updateRelation(model: IModelStatic, id: number, relation: string, relationId: any, data: unknown): Promise<Ok>;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=Update.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Update.d.ts","sourceRoot":"","sources":["../../../../src/controllers/crud/Update.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAY,EAAE,EAAiC,MAAM,eAAe,CAAC;AAQ5E,OAAO,EAAE,GAAG,EAAU,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,qBAEa,UAAW,SAAQ,IAAI;IAElC,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC;IAGnB,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC;IAGnB,SAAS,CAAC,EAAE,EAAE,aAAa,CAAC;IAIf,MAAM,CAAc,KAAK,EAAE,YAAY,EAAW,EAAE,EAAE,MAAM,EAAe,IAAI,EAAE,OAAO;IAmBxF,cAAc,CAAc,KAAK,EAAE,YAAY,EAAW,EAAE,EAAE,MAAM,EAAW,QAAQ,EAAE,MAAM,EAAW,UAAU,EAAE,GAAG,EAAe,IAAI,EAAE,OAAO;CA4BnK"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.CrudUpdate = void 0;
|
|
16
|
+
const orm_1 = require("@spinajs/orm");
|
|
17
|
+
const http_1 = require("@spinajs/http");
|
|
18
|
+
const rbac_http_1 = require("@spinajs/rbac-http");
|
|
19
|
+
const di_1 = require("@spinajs/di");
|
|
20
|
+
const exceptions_1 = require("@spinajs/exceptions");
|
|
21
|
+
const ModelType_js_1 = require("../../route-args/ModelType.js");
|
|
22
|
+
const FindModelType_js_1 = require("../../policies/FindModelType.js");
|
|
23
|
+
const log_1 = require("@spinajs/log");
|
|
24
|
+
const rbac_1 = require("@spinajs/rbac");
|
|
25
|
+
const Crud_js_1 = require("./Crud.js");
|
|
26
|
+
let CrudUpdate = class CrudUpdate extends Crud_js_1.Crud {
|
|
27
|
+
async update(model, id, data) {
|
|
28
|
+
const descriptor = this.getModelDescriptor(model);
|
|
29
|
+
const entity = await model.where(descriptor.PrimaryKey, id).firstOrThrow(new exceptions_1.ResourceNotFound(`Record with id ${id} not found`, {
|
|
30
|
+
Resource: model.name,
|
|
31
|
+
[descriptor.PrimaryKey]: id,
|
|
32
|
+
}));
|
|
33
|
+
entity.hydrate(data);
|
|
34
|
+
await entity.update();
|
|
35
|
+
this.Log.trace('Updated entity with id ${id}');
|
|
36
|
+
return new http_1.Ok(entity.toJSON());
|
|
37
|
+
}
|
|
38
|
+
async updateRelation(model, id, relation, relationId, data) {
|
|
39
|
+
const mDescriptor = this.getModelDescriptor(model);
|
|
40
|
+
const rDescriptor = this.getRelationDescriptor(model, relation);
|
|
41
|
+
const rmDescriptor = this.getModelDescriptor(rDescriptor.TargetModel);
|
|
42
|
+
const query = rDescriptor.TargetModel.query();
|
|
43
|
+
const pExists = await model.exists(id);
|
|
44
|
+
if (!pExists) {
|
|
45
|
+
throw new exceptions_1.ResourceNotFound(`Record with id ${id} not found`, {
|
|
46
|
+
Resource: model.name,
|
|
47
|
+
[mDescriptor.PrimaryKey]: id,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
const result = await query.where(rmDescriptor.PrimaryKey, relationId).firstOrThrow(new exceptions_1.ResourceNotFound(`Record with id ${relationId} not found`, {
|
|
51
|
+
Resource: rDescriptor.TargetModel.name,
|
|
52
|
+
[rmDescriptor.PrimaryKey]: relationId,
|
|
53
|
+
}));
|
|
54
|
+
this.Log.trace(`Updating relation ${relation} with id ${relationId} for model ${model.name} with id ${id}`);
|
|
55
|
+
result.hydrate(data);
|
|
56
|
+
await result.update();
|
|
57
|
+
return new http_1.Ok(result.toJSON());
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
__decorate([
|
|
61
|
+
(0, log_1.Logger)('orm-http:api'),
|
|
62
|
+
__metadata("design:type", log_1.Log)
|
|
63
|
+
], CrudUpdate.prototype, "Log", void 0);
|
|
64
|
+
__decorate([
|
|
65
|
+
(0, di_1.Autoinject)(),
|
|
66
|
+
__metadata("design:type", orm_1.Orm)
|
|
67
|
+
], CrudUpdate.prototype, "Orm", void 0);
|
|
68
|
+
__decorate([
|
|
69
|
+
(0, di_1.Autoinject)(),
|
|
70
|
+
__metadata("design:type", rbac_1.AccessControl)
|
|
71
|
+
], CrudUpdate.prototype, "Ac", void 0);
|
|
72
|
+
__decorate([
|
|
73
|
+
(0, http_1.Put)(':model/:id'),
|
|
74
|
+
(0, rbac_http_1.Permission)('updateAny'),
|
|
75
|
+
__param(0, (0, ModelType_js_1.ModelType)()),
|
|
76
|
+
__param(1, (0, http_1.Param)()),
|
|
77
|
+
__param(2, (0, http_1.BodyField)()),
|
|
78
|
+
__metadata("design:type", Function),
|
|
79
|
+
__metadata("design:paramtypes", [Object, Number, Object]),
|
|
80
|
+
__metadata("design:returntype", Promise)
|
|
81
|
+
], CrudUpdate.prototype, "update", null);
|
|
82
|
+
__decorate([
|
|
83
|
+
(0, http_1.Put)(':model/:id/:relation/:relationId'),
|
|
84
|
+
(0, rbac_http_1.Permission)('updateAny'),
|
|
85
|
+
__param(0, (0, ModelType_js_1.ModelType)()),
|
|
86
|
+
__param(1, (0, http_1.Param)()),
|
|
87
|
+
__param(2, (0, http_1.Param)()),
|
|
88
|
+
__param(3, (0, http_1.Param)()),
|
|
89
|
+
__param(4, (0, http_1.BodyField)()),
|
|
90
|
+
__metadata("design:type", Function),
|
|
91
|
+
__metadata("design:paramtypes", [Object, Number, String, Object, Object]),
|
|
92
|
+
__metadata("design:returntype", Promise)
|
|
93
|
+
], CrudUpdate.prototype, "updateRelation", null);
|
|
94
|
+
CrudUpdate = __decorate([
|
|
95
|
+
(0, http_1.BasePath)('crud'),
|
|
96
|
+
(0, http_1.Policy)(FindModelType_js_1.FindModelType)
|
|
97
|
+
], CrudUpdate);
|
|
98
|
+
exports.CrudUpdate = CrudUpdate;
|
|
99
|
+
//# sourceMappingURL=Update.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Update.js","sourceRoot":"","sources":["../../../../src/controllers/crud/Update.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,sCAAiD;AACjD,wCAA4E;AAC5E,kDAAgD;AAChD,oCAAyC;AACzC,oDAAuD;AAGvD,gEAA0D;AAC1D,sEAAgE;AAChE,sCAA2C;AAC3C,wCAA8C;AAC9C,uCAAiC;AAI1B,IAAM,UAAU,GAAhB,MAAM,UAAW,SAAQ,cAAI;IAYrB,AAAN,KAAK,CAAC,MAAM,CAAc,KAAmB,EAAW,EAAU,EAAe,IAAa;QACnG,MAAM,UAAU,GAAG,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;QAClD,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,KAAK,CAAC,UAAU,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,YAAY,CACtE,IAAI,6BAAgB,CAAC,kBAAkB,EAAE,YAAY,EAAE;YACrD,QAAQ,EAAE,KAAK,CAAC,IAAI;YACpB,CAAC,UAAU,C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crud.js';
|
|
5
|
+
import { AccessControl, User as UserModel } from '@spinajs/rbac';
|
|
6
|
+
import { Permission } from 'accesscontrol';
|
|
7
|
+
export declare class CrudCreate extends Crud {
|
|
8
|
+
protected Log: Log;
|
|
9
|
+
protected Orm: Orm;
|
|
10
|
+
protected Ac: AccessControl;
|
|
11
|
+
protected checkAny(resource: string, user?: UserModel): Permission;
|
|
12
|
+
protected checkOwn(resource: string, user?: UserModel): Permission;
|
|
13
|
+
save(model: IModelStatic, data: unknown, user?: UserModel): Promise<Ok>;
|
|
14
|
+
insertRelation(model: IModelStatic, id: any, relation: string, data: unknown, user?: UserModel): Promise<Ok>;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=Create.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Create.d.ts","sourceRoot":"","sources":["../../../../src/controllers/crud/Create.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAY,EAAE,EAAkC,MAAM,eAAe,CAAC;AAO7E,OAAO,EAAE,GAAG,EAAU,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,aAAa,EAAwB,IAAI,IAAI,SAAS,EAAE,MAAM,eAAe,CAAC;AACvF,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,qBAEa,UAAW,SAAQ,IAAI;IAElC,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC;IAGnB,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC;IAGnB,SAAS,CAAC,EAAE,EAAE,aAAa,CAAC;IAE5B,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,SAAS,GAAG,UAAU;IAUlE,SAAS,CAAC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,SAAS,GAAG,UAAU;IAMrD,IAAI,CAAc,KAAK,EAAE,YAAY,EAAe,IAAI,EAAE,OAAO,EAAU,IAAI,CAAC,EAAE,SAAS;IA4B3F,cAAc,CAAc,KAAK,EAAE,YAAY,EAAW,EAAE,EAAE,GAAG,EAAW,QAAQ,EAAE,MAAM,EAAe,IAAI,EAAE,OAAO,EAAU,IAAI,CAAC,EAAE,SAAS;CA4ChK"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
11
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
12
|
+
};
|
|
13
|
+
import { Orm } from '@spinajs/orm';
|
|
14
|
+
import { BasePath, Ok, Post, Policy, BodyField, Param } from '@spinajs/http';
|
|
15
|
+
import { Autoinject } from '@spinajs/di';
|
|
16
|
+
import { Forbidden, ResourceNotFound, UnexpectedServerError } from '@spinajs/exceptions';
|
|
17
|
+
import { User } from '@spinajs/rbac-http';
|
|
18
|
+
import _ from 'lodash';
|
|
19
|
+
import { ModelType } from '../../route-args/ModelType.js';
|
|
20
|
+
import { FindModelType } from '../../policies/FindModelType.js';
|
|
21
|
+
import { Log, Logger } from '@spinajs/log';
|
|
22
|
+
import { Crud } from './Crud.js';
|
|
23
|
+
import { AccessControl, User as UserModel } from '@spinajs/rbac';
|
|
24
|
+
let CrudCreate = class CrudCreate extends Crud {
|
|
25
|
+
checkAny(resource, user) {
|
|
26
|
+
// if we have user
|
|
27
|
+
if (user) {
|
|
28
|
+
return this.Ac.can(user.Role).createAny(resource);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
// if not try guest account
|
|
32
|
+
return this.Ac.can('guest').createAny(resource);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
checkOwn(resource, user) {
|
|
36
|
+
const role = user ? user.Role : 'guest';
|
|
37
|
+
return this.Ac.can(role).createOwn(resource);
|
|
38
|
+
}
|
|
39
|
+
async save(model, data, user) {
|
|
40
|
+
const mDescriptor = this.getModelDescriptor(model);
|
|
41
|
+
let resource = mDescriptor.RbacResource ? mDescriptor.RbacResource : mDescriptor.Name;
|
|
42
|
+
let permission = this.checkAny(resource, user);
|
|
43
|
+
const toInsert = (Array.isArray(data) ? data : [data]).map((x) => new model(x));
|
|
44
|
+
if (!permission.granted) {
|
|
45
|
+
permission = this.checkOwn(resource, user);
|
|
46
|
+
if (permission.granted && user) {
|
|
47
|
+
if (model.checkOwnership) {
|
|
48
|
+
if (_.some(toInsert, (x) => !model.checkOwnership(x, user))) {
|
|
49
|
+
throw new Forbidden(`You can only create ${resource} that is assigned to you !`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
throw new UnexpectedServerError(`Resource ${resource} does not have checkOwnership method implemented`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (!permission.granted) {
|
|
57
|
+
throw new Forbidden(`You cannot create resource ${resource}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
await model.insert(toInsert);
|
|
61
|
+
return new Ok(toInsert.map((x) => x.dehydrate()));
|
|
62
|
+
}
|
|
63
|
+
async insertRelation(model, id, relation, data, user) {
|
|
64
|
+
const mDescriptor = this.getModelDescriptor(model);
|
|
65
|
+
const rDescriptor = this.getRelationDescriptor(model, relation);
|
|
66
|
+
let resource = mDescriptor.RbacResource ? mDescriptor.RbacResource : mDescriptor.Name;
|
|
67
|
+
let permission = this.checkAny(resource, user);
|
|
68
|
+
const toInsert = (Array.isArray(data) ? data : [data]).map((x) => permission.filter(x)).map((x) => new rDescriptor.TargetModel(x));
|
|
69
|
+
const m = await model.get(id);
|
|
70
|
+
if (!m) {
|
|
71
|
+
throw new ResourceNotFound(`Record with id ${id} not found`, {
|
|
72
|
+
Resource: model.name,
|
|
73
|
+
[mDescriptor.PrimaryKey]: id,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (!permission.granted) {
|
|
77
|
+
permission = this.checkOwn(resource, user);
|
|
78
|
+
if (permission.granted && user) {
|
|
79
|
+
if (model.checkOwnership) {
|
|
80
|
+
if (!model.checkOwnership(m, user)) {
|
|
81
|
+
throw new Forbidden(`Cannot create relations for ${resource} that you do not own`);
|
|
82
|
+
}
|
|
83
|
+
if (_.some(toInsert, (x) => !model.checkOwnership(x, user))) {
|
|
84
|
+
throw new Forbidden(`You can only create ${resource} that you own`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
throw new UnexpectedServerError(`Resource ${resource} does not have checkOwnership method implemented`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (!permission.granted) {
|
|
92
|
+
throw new Forbidden(`You do not access to ${resource}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
toInsert.forEach((x) => {
|
|
96
|
+
m.attach(x);
|
|
97
|
+
});
|
|
98
|
+
await rDescriptor.TargetModel.insert(toInsert);
|
|
99
|
+
return new Ok(toInsert.map((x) => x.dehydrate()));
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
__decorate([
|
|
103
|
+
Logger('orm-http:api'),
|
|
104
|
+
__metadata("design:type", Log)
|
|
105
|
+
], CrudCreate.prototype, "Log", void 0);
|
|
106
|
+
__decorate([
|
|
107
|
+
Autoinject(),
|
|
108
|
+
__metadata("design:type", Orm)
|
|
109
|
+
], CrudCreate.prototype, "Orm", void 0);
|
|
110
|
+
__decorate([
|
|
111
|
+
Autoinject(),
|
|
112
|
+
__metadata("design:type", AccessControl)
|
|
113
|
+
], CrudCreate.prototype, "Ac", void 0);
|
|
114
|
+
__decorate([
|
|
115
|
+
Post(':model'),
|
|
116
|
+
__param(0, ModelType()),
|
|
117
|
+
__param(1, BodyField()),
|
|
118
|
+
__param(2, User()),
|
|
119
|
+
__metadata("design:type", Function),
|
|
120
|
+
__metadata("design:paramtypes", [Object, Object, UserModel]),
|
|
121
|
+
__metadata("design:returntype", Promise)
|
|
122
|
+
], CrudCreate.prototype, "save", null);
|
|
123
|
+
__decorate([
|
|
124
|
+
Post(':model/:id/:relation'),
|
|
125
|
+
__param(0, ModelType()),
|
|
126
|
+
__param(1, Param()),
|
|
127
|
+
__param(2, Param()),
|
|
128
|
+
__param(3, BodyField()),
|
|
129
|
+
__param(4, User()),
|
|
130
|
+
__metadata("design:type", Function),
|
|
131
|
+
__metadata("design:paramtypes", [Object, Object, String, Object, UserModel]),
|
|
132
|
+
__metadata("design:returntype", Promise)
|
|
133
|
+
], CrudCreate.prototype, "insertRelation", null);
|
|
134
|
+
CrudCreate = __decorate([
|
|
135
|
+
BasePath('crud'),
|
|
136
|
+
Policy(FindModelType)
|
|
137
|
+
], CrudCreate);
|
|
138
|
+
export { CrudCreate };
|
|
139
|
+
//# sourceMappingURL=Create.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Create.js","sourceRoot":"","sources":["../../../../src/controllers/crud/Create.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAgB,GAAG,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM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A;IAAuB,WAAA,SAAS,EAAE,CAAA;IAAiB,WAAA,IAAI,EAAE,CAAA;;qDAAQ,SAAS;;sCAyBvG;AAGY;IADZ,IAAI,CAAC,sBAAsB,CAAC;IACA,WAAA,SAAS,EAAE,CAAA;IAAuB,WAAA,KAAK,EAAE,CAAA;IAAW,WAAA,KAAK,EAAE,CAAA;IAAoB,WAAA,SAAS,EAAE,CAAA;IAAiB,WAAA,IAAI,EAAE,CAAA;;qEAAQ,SAAS;;gDA2C9J;AAjGU,UAAU;IAFtB,QAAQ,CAAC,MAAM,CAAC;IAChB,MAAM,CAAC,aAAa,CAAC;GACT,UAAU,CAkGtB;SAlGY,UAAU"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IModelStatic, IOrmRelation, SelectQueryBuilder } from '@spinajs/orm';
|
|
2
|
+
import { BaseController } from '@spinajs/http';
|
|
3
|
+
/**
|
|
4
|
+
* Base class for crud operations. Provides helper functions
|
|
5
|
+
*/
|
|
6
|
+
export declare abstract class Crud extends BaseController {
|
|
7
|
+
protected prepareQuery(model: IModelStatic, relation: string, id: any, callback: (this: SelectQueryBuilder<SelectQueryBuilder<any>>, relation: IOrmRelation) => void): {
|
|
8
|
+
relation: any;
|
|
9
|
+
relationModel: any;
|
|
10
|
+
query: import("@spinajs/orm").ISelectQueryBuilder<import("@spinajs/orm").ModelBase<unknown>[]>;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=Crud.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Crud.d.ts","sourceRoot":"","sources":["../../../../src/controllers/crud/Crud.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAC9E,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAI/C;;GAEG;AACH,8BAAsB,IAAK,SAAQ,cAAc;IAC/C,SAAS,CAAC,YAAY,CAAC,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,EAAE,QAAQ,EAAE,CAAC,IAAI,EAAE,kBAAkB,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,EAAE,YAAY,KAAK,IAAI;;;;;CAYrK"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BaseController } from '@spinajs/http';
|
|
2
|
+
/**
|
|
3
|
+
* Base class for crud operations. Provides helper functions
|
|
4
|
+
*/
|
|
5
|
+
export class Crud extends BaseController {
|
|
6
|
+
prepareQuery(model, relation, id, callback) {
|
|
7
|
+
const descriptor = this.getModelDescriptor(model);
|
|
8
|
+
const rDescriptor = this.getRelationDescriptor(model, relation);
|
|
9
|
+
const tDescriptor = this.getModelDescriptor(rDescriptor.TargetModel);
|
|
10
|
+
const sQuery = model.query().where(descriptor.PrimaryKey, id).populate(relation, callback);
|
|
11
|
+
return {
|
|
12
|
+
relation: rDescriptor,
|
|
13
|
+
relationModel: tDescriptor,
|
|
14
|
+
query: sQuery,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=Crud.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Crud.js","sourceRoot":"","sources":["../../../../src/controllers/crud/Crud.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAI/C;;GAEG;AACH,MAAM,OAAgB,IAAK,SAAQ,cAAc;IACrC,YAAY,CAAC,KAAmB,EAAE,QAAgB,EAAE,EAAO,EAAE,QAA6F;QAClK,MAAM,UAAU,GAAG,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;QAClD,MAAM,WAAW,GAAG,IAAI,CAAC,qBAAqB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QAChE,MAAM,WAAW,GAAG,IAAI,CAAC,kBAAkB,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;QACrE,MAAM,MAAM,GAAG,KAAK,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QAE3F,OAAO;YACL,QAAQ,EAAE,WAAW;YACrB,aAAa,EAAE,WAAW;YAC1B,KAAK,EAAE,MAAM;SACd,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IModelStatic, Orm } from '@spinajs/orm';
|
|
2
|
+
import { Ok } from '@spinajs/http';
|
|
3
|
+
import { Log } from '@spinajs/log';
|
|
4
|
+
import { Crud } from './Crud.js';
|
|
5
|
+
import { AccessControl } from '@spinajs/rbac';
|
|
6
|
+
export declare class CrudDelete extends Crud {
|
|
7
|
+
protected Log: Log;
|
|
8
|
+
protected Orm: Orm;
|
|
9
|
+
protected Ac: AccessControl;
|
|
10
|
+
batchDelete(model: IModelStatic, ids: any[]): Promise<Ok>;
|
|
11
|
+
del(model: IModelStatic, id: any): Promise<Ok>;
|
|
12
|
+
deleteRelationBatch(model: IModelStatic, id: any, relation: string, relationIds: any[]): Promise<Ok>;
|
|
13
|
+
deleteRelation(model: IModelStatic, id: any, relation: string, relationId: number): Promise<Ok>;
|
|
14
|
+
deleteRelationAll(model: IModelStatic, id: any, relation: string): Promise<Ok>;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=Delete.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Delete.d.ts","sourceRoot":"","sources":["../../../../src/controllers/crud/Delete.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,GAAG,EAAgB,MAAM,cAAc,CAAC;AAC/D,OAAO,EAAY,EAAE,EAA8D,MAAM,eAAe,CAAC;AAQzG,OAAO,EAAE,GAAG,EAAU,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,qBAEa,UAAW,SAAQ,IAAI;IAElC,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC;IAGnB,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC;IAGnB,SAAS,CAAC,EAAE,EAAE,aAAa,CAAC;IAGf,WAAW,CAAc,KAAK,EAAE,YAAY,EAAU,GAAG,EAAE,GAAG,EAAE;IAahE,GAAG,CAAc,KAAK,EAAE,YAAY,EAAW,EAAE,EAAE,GAAG;IAatD,mBAAmB,CAAc,KAAK,EAAE,YAAY,EAAW,EAAE,EAAE,GAAG,EAAW,QAAQ,EAAE,MAAM,EAAU,WAAW,EAAE,GAAG,EAAE;IAuB7H,cAAc,CAAc,KAAK,EAAE,YAAY,EAAW,EAAE,EAAE,GAAG,EAAW,QAAQ,EAAE,MAAM,EAAW,UAAU,EAAE,MAAM;IAwBzH,iBAAiB,CAAc,KAAK,EAAE,YAAY,EAAiC,EAAE,EAAE,GAAG,EAAW,QAAQ,EAAE,MAAM;CAoBnI"}
|