@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.
Files changed (53) hide show
  1. package/lib/cjs/config/orm-http.js +1 -1
  2. package/lib/cjs/controllers/crud/Create.d.ts +16 -0
  3. package/lib/cjs/controllers/crud/Create.d.ts.map +1 -0
  4. package/lib/cjs/controllers/crud/Create.js +145 -0
  5. package/lib/cjs/controllers/crud/Create.js.map +1 -0
  6. package/lib/cjs/controllers/crud/Crud.d.ts +13 -0
  7. package/lib/cjs/controllers/crud/Crud.d.ts.map +1 -0
  8. package/lib/cjs/controllers/crud/Crud.js +22 -0
  9. package/lib/cjs/controllers/crud/Crud.js.map +1 -0
  10. package/lib/cjs/controllers/crud/Delete.d.ts +16 -0
  11. package/lib/cjs/controllers/crud/Delete.d.ts.map +1 -0
  12. package/lib/cjs/controllers/crud/Delete.js +158 -0
  13. package/lib/cjs/controllers/crud/Delete.js.map +1 -0
  14. package/lib/cjs/controllers/crud/Read.d.ts +35 -0
  15. package/lib/cjs/controllers/crud/Read.d.ts.map +1 -0
  16. package/lib/cjs/controllers/crud/Read.js +230 -0
  17. package/lib/cjs/controllers/crud/Read.js.map +1 -0
  18. package/lib/cjs/controllers/crud/Update.d.ts +13 -0
  19. package/lib/cjs/controllers/crud/Update.d.ts.map +1 -0
  20. package/lib/cjs/controllers/crud/Update.js +99 -0
  21. package/lib/cjs/controllers/crud/Update.js.map +1 -0
  22. package/lib/mjs/config/orm-http.js +1 -1
  23. package/lib/mjs/controllers/crud/Create.d.ts +16 -0
  24. package/lib/mjs/controllers/crud/Create.d.ts.map +1 -0
  25. package/lib/mjs/controllers/crud/Create.js +139 -0
  26. package/lib/mjs/controllers/crud/Create.js.map +1 -0
  27. package/lib/mjs/controllers/crud/Crud.d.ts +13 -0
  28. package/lib/mjs/controllers/crud/Crud.d.ts.map +1 -0
  29. package/lib/mjs/controllers/crud/Crud.js +18 -0
  30. package/lib/mjs/controllers/crud/Crud.js.map +1 -0
  31. package/lib/mjs/controllers/crud/Delete.d.ts +16 -0
  32. package/lib/mjs/controllers/crud/Delete.d.ts.map +1 -0
  33. package/lib/mjs/controllers/crud/Delete.js +155 -0
  34. package/lib/mjs/controllers/crud/Delete.js.map +1 -0
  35. package/lib/mjs/controllers/crud/Read.d.ts +35 -0
  36. package/lib/mjs/controllers/crud/Read.d.ts.map +1 -0
  37. package/lib/mjs/controllers/crud/Read.js +227 -0
  38. package/lib/mjs/controllers/crud/Read.js.map +1 -0
  39. package/lib/mjs/controllers/crud/Update.d.ts +13 -0
  40. package/lib/mjs/controllers/crud/Update.d.ts.map +1 -0
  41. package/lib/mjs/controllers/crud/Update.js +96 -0
  42. package/lib/mjs/controllers/crud/Update.js.map +1 -0
  43. package/lib/tsconfig.cjs.tsbuildinfo +1 -1
  44. package/lib/tsconfig.mjs.tsbuildinfo +1 -1
  45. package/package.json +17 -18
  46. package/lib/cjs/controllers/Collections.d.ts +0 -33
  47. package/lib/cjs/controllers/Collections.d.ts.map +0 -1
  48. package/lib/cjs/controllers/Collections.js +0 -417
  49. package/lib/cjs/controllers/Collections.js.map +0 -1
  50. package/lib/mjs/controllers/Collections.d.ts +0 -33
  51. package/lib/mjs/controllers/Collections.d.ts.map +0 -1
  52. package/lib/mjs/controllers/Collections.js +0 -414
  53. package/lib/mjs/controllers/Collections.js.map +0 -1
@@ -0,0 +1,155 @@
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, RelationType } from '@spinajs/orm';
14
+ import { BasePath, Ok, Post, Query, Del, Body, PKey, ParameterType, Policy, Param } from '@spinajs/http';
15
+ import { Permission } from '@spinajs/rbac-http';
16
+ import { Autoinject } from '@spinajs/di';
17
+ import { BadRequest, ResourceNotFound } from '@spinajs/exceptions';
18
+ import { ModelType } from '../../route-args/ModelType.js';
19
+ import { FindModelType } from '../../policies/FindModelType.js';
20
+ import { Log, Logger } from '@spinajs/log';
21
+ import { Crud } from './Crud.js';
22
+ import { AccessControl } from '@spinajs/rbac';
23
+ let CrudDelete = class CrudDelete extends Crud {
24
+ async batchDelete(model, ids) {
25
+ const tModel = this.getModelDescriptor(model);
26
+ const result = await model.destroy().whereIn(tModel.PrimaryKey, ids);
27
+ this.Log.trace(`Deleted ${result.RowsAffected} records from ${tModel.Name}`);
28
+ return new Ok({
29
+ [tModel.PrimaryKey]: ids,
30
+ });
31
+ }
32
+ async del(model, id) {
33
+ const descriptor = this.getModelDescriptor(model);
34
+ const result = await model.destroy(id);
35
+ this.Log.trace(`Deleted ${result.RowsAffected} records from ${descriptor.Name} with id ${id}`);
36
+ return new Ok({
37
+ [descriptor.PrimaryKey]: [id],
38
+ });
39
+ }
40
+ async deleteRelationBatch(model, id, relation, relationIds) {
41
+ const descriptor = this.getRelationDescriptor(model, relation);
42
+ if (descriptor.Type === RelationType.One) {
43
+ throw new BadRequest('Cannot delete batch from one to one relation');
44
+ }
45
+ const exists = await model.exists(id);
46
+ if (!exists) {
47
+ throw new ResourceNotFound(`Record with id ${id} not found`, {
48
+ Resource: model.name,
49
+ Id: id,
50
+ });
51
+ }
52
+ const result = await descriptor.TargetModel.destroy(relationIds).where(descriptor.ForeignKey, id);
53
+ this.Log.trace(`Deleted ${result.RowsAffected} records from ${descriptor.Name} with id ${id}`);
54
+ return new Ok(relationIds);
55
+ }
56
+ async deleteRelation(model, id, relation, relationId) {
57
+ const descriptor = this.getRelationDescriptor(model, relation);
58
+ const tModel = this.getModelDescriptor(descriptor.TargetModel);
59
+ const exists = await model.exists(id);
60
+ if (!exists) {
61
+ throw new ResourceNotFound(`Record with id ${id} not found`, {
62
+ Resource: model.name,
63
+ Id: id,
64
+ });
65
+ }
66
+ const result = await descriptor.TargetModel.destroy(relationId).when(descriptor.Type !== RelationType.One, function () {
67
+ this.where({ [descriptor.ForeignKey]: id });
68
+ });
69
+ this.Log.trace(`Deleted related ${result.RowsAffected} records from ${tModel.Name} with id ${id}`);
70
+ return new Ok({
71
+ [tModel.PrimaryKey]: relationId,
72
+ });
73
+ }
74
+ async deleteRelationAll(model, id, relation) {
75
+ const descriptor = this.getRelationDescriptor(model, relation);
76
+ const tModel = this.getModelDescriptor(descriptor.TargetModel);
77
+ const exists = await model.exists(id);
78
+ if (!exists) {
79
+ throw new ResourceNotFound(`Record with id ${id} not found`, {
80
+ Resource: model.name,
81
+ Id: id,
82
+ });
83
+ }
84
+ const result = await descriptor.TargetModel.destroy().when(descriptor.Type !== RelationType.One, function () {
85
+ this.where(descriptor.ForeignKey, id);
86
+ });
87
+ this.Log.trace(`Deleted related ${result.RowsAffected} records from ${tModel.Name} with id ${id}`);
88
+ return new Ok();
89
+ }
90
+ };
91
+ __decorate([
92
+ Logger('orm-http:api'),
93
+ __metadata("design:type", Log)
94
+ ], CrudDelete.prototype, "Log", void 0);
95
+ __decorate([
96
+ Autoinject(),
97
+ __metadata("design:type", Orm)
98
+ ], CrudDelete.prototype, "Orm", void 0);
99
+ __decorate([
100
+ Autoinject(),
101
+ __metadata("design:type", AccessControl)
102
+ ], CrudDelete.prototype, "Ac", void 0);
103
+ __decorate([
104
+ Post(':model/__batchDelete'),
105
+ __param(0, ModelType()),
106
+ __param(1, Body()),
107
+ __metadata("design:type", Function),
108
+ __metadata("design:paramtypes", [Object, Array]),
109
+ __metadata("design:returntype", Promise)
110
+ ], CrudDelete.prototype, "batchDelete", null);
111
+ __decorate([
112
+ Del(':model/:id'),
113
+ Permission('deleteAny'),
114
+ __param(0, ModelType()),
115
+ __param(1, Param()),
116
+ __metadata("design:type", Function),
117
+ __metadata("design:paramtypes", [Object, Object]),
118
+ __metadata("design:returntype", Promise)
119
+ ], CrudDelete.prototype, "del", null);
120
+ __decorate([
121
+ Post(':model/:id/:relation/__batchDelete'),
122
+ Permission('deleteAny'),
123
+ __param(0, ModelType()),
124
+ __param(1, Param()),
125
+ __param(2, Param()),
126
+ __param(3, Body()),
127
+ __metadata("design:type", Function),
128
+ __metadata("design:paramtypes", [Object, Object, String, Array]),
129
+ __metadata("design:returntype", Promise)
130
+ ], CrudDelete.prototype, "deleteRelationBatch", null);
131
+ __decorate([
132
+ Del(':model/:id/:relation/:relationId'),
133
+ __param(0, ModelType()),
134
+ __param(1, Param()),
135
+ __param(2, Param()),
136
+ __param(3, Param()),
137
+ __metadata("design:type", Function),
138
+ __metadata("design:paramtypes", [Object, Object, String, Number]),
139
+ __metadata("design:returntype", Promise)
140
+ ], CrudDelete.prototype, "deleteRelation", null);
141
+ __decorate([
142
+ Del(':model/:id/:relation'),
143
+ __param(0, ModelType()),
144
+ __param(1, PKey(ParameterType.FromQuery)),
145
+ __param(2, Query()),
146
+ __metadata("design:type", Function),
147
+ __metadata("design:paramtypes", [Object, Object, String]),
148
+ __metadata("design:returntype", Promise)
149
+ ], CrudDelete.prototype, "deleteRelationAll", null);
150
+ CrudDelete = __decorate([
151
+ BasePath('crud'),
152
+ Policy(FindModelType)
153
+ ], CrudDelete);
154
+ export { CrudDelete };
155
+ //# sourceMappingURL=Delete.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Delete.js","sourceRoot":"","sources":["../../../../src/controllers/crud/Delete.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAgB,GAAG,EAAE,YAA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AAC,kCAAkC,CAAC;IACX,WAAA,SAAS,EAAE,CAAA;IAAuB,WAAA,KAAK,EAAE,CAAA;IAAW,WAAA,KAAK,EAAE,CAAA;IAAoB,WAAA,KAAK,EAAE,CAAA;;;;gDAqBlH;AAGY;IADZ,GAAG,CAAC,sBAAsB,CAAC;IACI,WAAA,SAAS,EAAE,CAAA;IAAuB,WAAA,IAAI,CAAC,aAAa,CAAC,SAAS,CAAC,CAAA;IAAW,WAAA,KAAK,EAAE,CAAA;;;;mDAmBhH;AAvGU,UAAU;IAFtB,QAAQ,CAAC,MAAM,CAAC;IAChB,MAAM,CAAC,aAAa,CAAC;GACT,UAAU,CAwGtB;SAxGY,UAAU"}
@@ -0,0 +1,35 @@
1
+ import { Permission } from 'accesscontrol';
2
+ import { Orm, IModelStatic } from '@spinajs/orm';
3
+ import { Ok } from '@spinajs/http';
4
+ import { User as UserModel, AccessControl } from '@spinajs/rbac';
5
+ import { BadRequest } from '@spinajs/exceptions';
6
+ import { Log } from '@spinajs/log';
7
+ import '@spinajs/rbac';
8
+ import { QueryArgs } from '../../dto/QueryArgs.js';
9
+ import { QueryFilter } from '../../dto/QueryFilter.js';
10
+ import { QueryIncludes } from '../../dto/QueryIncludes.js';
11
+ import { Crud } from './Crud.js';
12
+ export declare class CrudRead extends Crud {
13
+ protected Log: Log;
14
+ protected Orm: Orm;
15
+ protected Ac: AccessControl;
16
+ protected checkAny(resource: string, user?: UserModel): Permission;
17
+ protected checkOwn(resource: string, user?: UserModel): Permission;
18
+ /**
19
+ *
20
+ * Creates query on model and ansures that user have proper permission to access it
21
+ *
22
+ * @param model model to query against
23
+ * @param user user for permission check
24
+ * @returns
25
+ */
26
+ protected getSafeQuery(model: IModelStatic, user?: UserModel): {
27
+ query: import("@spinajs/orm").ISelectQueryBuilder<import("@spinajs/orm").ModelBase<unknown>[]>;
28
+ permission: Permission;
29
+ };
30
+ getAll(model: IModelStatic, getParams: QueryArgs, filters: QueryFilter, includes: QueryIncludes, user?: UserModel): Promise<Ok>;
31
+ get(user: UserModel, model: IModelStatic, id: number, includes: QueryIncludes): Promise<Ok>;
32
+ getRelation(model: IModelStatic, id: any, relation: string, relationId: any, includes: QueryIncludes, user?: UserModel): Promise<Ok | BadRequest>;
33
+ getRelations(model: IModelStatic, id: any, relation: string, params: QueryArgs, filters: QueryFilter, includes: QueryIncludes, user?: UserModel): Promise<Ok | BadRequest>;
34
+ }
35
+ //# sourceMappingURL=Read.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Read.d.ts","sourceRoot":"","sources":["../../../../src/controllers/crud/Read.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAa,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAO,EAAE,EAAkC,MAAM,eAAe,CAAC;AAExE,OAAO,EAAE,IAAI,IAAI,SAAS,EAAwB,aAAa,EAAE,MAAM,eAAe,CAAC;AAEvF,OAAO,EAAE,UAAU,EAAsD,MAAM,qBAAqB,CAAC;AACrG,OAAO,EAAE,GAAG,EAAU,MAAM,cAAc,CAAC;AAC3C,OAAO,eAAe,CAAC;AAIvB,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC,qBAEa,QAAS,SAAQ,IAAI;IAEhC,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;IAUlE;;;;;;;OAOG;IACH,SAAS,CAAC,YAAY,CAAC,KAAK,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE,SAAS;;;;IA6B/C,MAAM,CAAc,KAAK,EAAE,YAAY,EAAW,SAAS,EAAE,SAAS,EAAW,OAAO,EAAE,WAAW,EAAW,QAAQ,EAAE,aAAa,EAAU,IAAI,CAAC,EAAE,SAAS;IAoBjK,GAAG,CAAS,IAAI,EAAE,SAAS,EAAe,KAAK,EAAE,YAAY,EAAW,EAAE,EAAE,MAAM,EAAW,QAAQ,EAAE,aAAa;IAkBpH,WAAW,CAAc,KAAK,EAAE,YAAY,EAAW,EAAE,EAAE,GAAG,EAAW,QAAQ,EAAE,MAAM,EAAW,UAAU,EAAE,GAAG,EAAW,QAAQ,EAAE,aAAa,EAAU,IAAI,CAAC,EAAE,SAAS;IAiC/K,YAAY,CAAc,KAAK,EAAE,YAAY,EAAW,EAAE,EAAE,GAAG,EAAW,QAAQ,EAAE,MAAM,EAAW,MAAM,EAAE,SAAS,EAAW,OAAO,EAAE,WAAW,EAAW,QAAQ,EAAE,aAAa,EAAU,IAAI,CAAC,EAAE,SAAS;CAkC/N"}
@@ -0,0 +1,227 @@
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, SortOrder } from '@spinajs/orm';
14
+ import { Get, Ok, Query, Param, Policy, BasePath } from '@spinajs/http';
15
+ import { User } from '@spinajs/rbac-http';
16
+ import { User as UserModel, AccessControl } from '@spinajs/rbac';
17
+ import { Autoinject } from '@spinajs/di';
18
+ import { BadRequest, Forbidden, ResourceNotFound, UnexpectedServerError } from '@spinajs/exceptions';
19
+ import { Log, Logger } from '@spinajs/log';
20
+ import '@spinajs/rbac';
21
+ import { ModelType } from '../../route-args/ModelType.js';
22
+ import { QueryArgs } from '../../dto/QueryArgs.js';
23
+ import { QueryFilter } from '../../dto/QueryFilter.js';
24
+ import { QueryIncludes } from '../../dto/QueryIncludes.js';
25
+ import { Crud } from './Crud.js';
26
+ import { FindModelType } from '../../policies/FindModelType.js';
27
+ let CrudRead = class CrudRead extends Crud {
28
+ checkAny(resource, user) {
29
+ // if we have user
30
+ if (user) {
31
+ return this.Ac.can(user.Role).readAny(resource);
32
+ }
33
+ else {
34
+ // if not try guest account
35
+ return this.Ac.can('guest').readAny(resource);
36
+ }
37
+ }
38
+ checkOwn(resource, user) {
39
+ // if we have user
40
+ if (user) {
41
+ return this.Ac.can(user.Role).readOwn(resource);
42
+ }
43
+ else {
44
+ // if not try guest account
45
+ return this.Ac.can('guest').readOwn(resource);
46
+ }
47
+ }
48
+ /**
49
+ *
50
+ * Creates query on model and ansures that user have proper permission to access it
51
+ *
52
+ * @param model model to query against
53
+ * @param user user for permission check
54
+ * @returns
55
+ */
56
+ getSafeQuery(model, user) {
57
+ const descriptor = this.getModelDescriptor(model);
58
+ const query = model.query();
59
+ // retrieve resource name, if model does not have @OrmResource decorator set
60
+ // model name is default resource name
61
+ let resource = descriptor.RbacResource ? descriptor.RbacResource : descriptor.Name;
62
+ let permission = this.checkAny(resource, user);
63
+ if (!permission.granted) {
64
+ // permission for readAny not granted, check for own
65
+ permission = this.checkOwn(resource, user);
66
+ if (permission.granted && user) {
67
+ if (model.ensureOwnership) {
68
+ model.ensureOwnership(query, user, descriptor);
69
+ }
70
+ else {
71
+ throw new UnexpectedServerError(`Resource ${resource} does not have ensureOwnership method implemented`);
72
+ }
73
+ }
74
+ if (!permission.granted) {
75
+ throw new Forbidden(`You do not access to ${resource}`);
76
+ }
77
+ }
78
+ return { query, permission };
79
+ }
80
+ async getAll(model, getParams, filters, includes, user) {
81
+ const { query, permission } = this.getSafeQuery(model, user);
82
+ query
83
+ .where(filters)
84
+ .select('*')
85
+ .populate(includes)
86
+ .order(getParams.order, getParams.orderDirection ?? SortOrder.ASC)
87
+ .skip(getParams.page * getParams.perPage ?? 0)
88
+ .take(getParams.perPage ?? 10);
89
+ const count = await query.clone().clearColumns().count('*', 'count').takeFirst().asRaw();
90
+ const result = await query;
91
+ return new Ok({
92
+ Data: result.map((x) => x.dehydrateWithRelations()).map((x) => permission.filter(x)),
93
+ Total: count.count,
94
+ });
95
+ }
96
+ async get(user, model, id, includes) {
97
+ const descriptor = this.getModelDescriptor(model);
98
+ const { query, permission } = this.getSafeQuery(model, user);
99
+ const result = await query
100
+ .select('*')
101
+ .where(descriptor.PrimaryKey, id)
102
+ .populate(includes)
103
+ .firstOrThrow(new ResourceNotFound(`Record with id ${id} not found`, {
104
+ Resource: model.name,
105
+ [descriptor.PrimaryKey]: id,
106
+ }));
107
+ return new Ok(permission.filter(result.dehydrateWithRelations()));
108
+ }
109
+ async getRelation(model, id, relation, relationId, includes, user) {
110
+ const mDescriptor = this.getModelDescriptor(model);
111
+ const rDescriptor = this.getRelationDescriptor(model, relation);
112
+ const rmDescriptor = this.getModelDescriptor(rDescriptor.TargetModel);
113
+ const { query, permission } = this.getSafeQuery(rDescriptor.TargetModel, user);
114
+ if (!mDescriptor.Relations.has(relation)) {
115
+ return new BadRequest(`Resource ${mDescriptor.Name} does not have relation ${relation} defined`);
116
+ }
117
+ const pExists = await model.exists(id);
118
+ if (!pExists) {
119
+ throw new ResourceNotFound(`Record with id ${id} not found`, {
120
+ Resource: model.name,
121
+ [mDescriptor.PrimaryKey]: id,
122
+ });
123
+ }
124
+ const result = await query
125
+ .select('*')
126
+ .populate(includes)
127
+ .where(rmDescriptor.PrimaryKey, relationId)
128
+ .firstOrThrow(new ResourceNotFound(`Record with id ${relationId} not found`, {
129
+ Resource: rDescriptor.TargetModel.name,
130
+ [rmDescriptor.PrimaryKey]: relationId,
131
+ }));
132
+ return new Ok(permission.filter(result.dehydrateWithRelations()));
133
+ }
134
+ async getRelations(model, id, relation, params, filters, includes, user) {
135
+ const mDescriptor = this.getModelDescriptor(model);
136
+ const rDescriptor = this.getRelationDescriptor(model, relation);
137
+ const { query, permission } = this.getSafeQuery(rDescriptor.TargetModel, user);
138
+ if (!mDescriptor.Relations.has(relation)) {
139
+ return new BadRequest(`Resource ${mDescriptor.Name} does not have relation ${relation} defined`);
140
+ }
141
+ const pExists = await model.exists(id);
142
+ if (!pExists) {
143
+ throw new ResourceNotFound(`Record with id ${id} not found`, {
144
+ Resource: model.name,
145
+ [mDescriptor.PrimaryKey]: id,
146
+ });
147
+ }
148
+ query
149
+ .where(filters)
150
+ .where(rDescriptor.ForeignKey, id)
151
+ .select('*')
152
+ .populate(includes)
153
+ .order(params.order, params.orderDirection ?? SortOrder.ASC)
154
+ .skip(params.page * params.perPage ?? 0)
155
+ .take(params.perPage ?? 10);
156
+ const count = await query.clone().clearColumns().count('*', 'count').takeFirst().asRaw();
157
+ const result = await query;
158
+ return new Ok({
159
+ Data: result.map((x) => x.dehydrateWithRelations()).map((x) => permission.filter(x)),
160
+ Total: count,
161
+ });
162
+ }
163
+ };
164
+ __decorate([
165
+ Logger('orm-http:api'),
166
+ __metadata("design:type", Log)
167
+ ], CrudRead.prototype, "Log", void 0);
168
+ __decorate([
169
+ Autoinject(),
170
+ __metadata("design:type", Orm)
171
+ ], CrudRead.prototype, "Orm", void 0);
172
+ __decorate([
173
+ Autoinject(),
174
+ __metadata("design:type", AccessControl)
175
+ ], CrudRead.prototype, "Ac", void 0);
176
+ __decorate([
177
+ Get(':model'),
178
+ __param(0, ModelType()),
179
+ __param(1, Query()),
180
+ __param(2, Query()),
181
+ __param(3, Query()),
182
+ __param(4, User()),
183
+ __metadata("design:type", Function),
184
+ __metadata("design:paramtypes", [Object, QueryArgs, QueryFilter, QueryIncludes, UserModel]),
185
+ __metadata("design:returntype", Promise)
186
+ ], CrudRead.prototype, "getAll", null);
187
+ __decorate([
188
+ Get(':model/:id'),
189
+ __param(0, User()),
190
+ __param(1, ModelType()),
191
+ __param(2, Param()),
192
+ __param(3, Query()),
193
+ __metadata("design:type", Function),
194
+ __metadata("design:paramtypes", [UserModel, Object, Number, QueryIncludes]),
195
+ __metadata("design:returntype", Promise)
196
+ ], CrudRead.prototype, "get", null);
197
+ __decorate([
198
+ Get(':model/:id/:relation/:relationId'),
199
+ __param(0, ModelType()),
200
+ __param(1, Param()),
201
+ __param(2, Param()),
202
+ __param(3, Param()),
203
+ __param(4, Query()),
204
+ __param(5, User()),
205
+ __metadata("design:type", Function),
206
+ __metadata("design:paramtypes", [Object, Object, String, Object, QueryIncludes, UserModel]),
207
+ __metadata("design:returntype", Promise)
208
+ ], CrudRead.prototype, "getRelation", null);
209
+ __decorate([
210
+ Get(':model/:id/:relation'),
211
+ __param(0, ModelType()),
212
+ __param(1, Param()),
213
+ __param(2, Param()),
214
+ __param(3, Query()),
215
+ __param(4, Query()),
216
+ __param(5, Query()),
217
+ __param(6, User()),
218
+ __metadata("design:type", Function),
219
+ __metadata("design:paramtypes", [Object, Object, String, QueryArgs, QueryFilter, QueryIncludes, UserModel]),
220
+ __metadata("design:returntype", Promise)
221
+ ], CrudRead.prototype, "getRelations", null);
222
+ CrudRead = __decorate([
223
+ BasePath('crud'),
224
+ Policy(FindModelType)
225
+ ], CrudRead);
226
+ export { CrudRead };
227
+ //# sourceMappingURL=Read.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Read.js","sourceRoot":"","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QAAQ"}
@@ -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,96 @@
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, Put, Policy, BodyField, Param } from '@spinajs/http';
15
+ import { Permission } from '@spinajs/rbac-http';
16
+ import { Autoinject } from '@spinajs/di';
17
+ import { ResourceNotFound } from '@spinajs/exceptions';
18
+ import { ModelType } from '../../route-args/ModelType.js';
19
+ import { FindModelType } from '../../policies/FindModelType.js';
20
+ import { Log, Logger } from '@spinajs/log';
21
+ import { AccessControl } from '@spinajs/rbac';
22
+ import { Crud } from './Crud.js';
23
+ let CrudUpdate = class CrudUpdate extends Crud {
24
+ async update(model, id, data) {
25
+ const descriptor = this.getModelDescriptor(model);
26
+ const entity = await model.where(descriptor.PrimaryKey, id).firstOrThrow(new ResourceNotFound(`Record with id ${id} not found`, {
27
+ Resource: model.name,
28
+ [descriptor.PrimaryKey]: id,
29
+ }));
30
+ entity.hydrate(data);
31
+ await entity.update();
32
+ this.Log.trace('Updated entity with id ${id}');
33
+ return new Ok(entity.toJSON());
34
+ }
35
+ async updateRelation(model, id, relation, relationId, data) {
36
+ const mDescriptor = this.getModelDescriptor(model);
37
+ const rDescriptor = this.getRelationDescriptor(model, relation);
38
+ const rmDescriptor = this.getModelDescriptor(rDescriptor.TargetModel);
39
+ const query = rDescriptor.TargetModel.query();
40
+ const pExists = await model.exists(id);
41
+ if (!pExists) {
42
+ throw new ResourceNotFound(`Record with id ${id} not found`, {
43
+ Resource: model.name,
44
+ [mDescriptor.PrimaryKey]: id,
45
+ });
46
+ }
47
+ const result = await query.where(rmDescriptor.PrimaryKey, relationId).firstOrThrow(new ResourceNotFound(`Record with id ${relationId} not found`, {
48
+ Resource: rDescriptor.TargetModel.name,
49
+ [rmDescriptor.PrimaryKey]: relationId,
50
+ }));
51
+ this.Log.trace(`Updating relation ${relation} with id ${relationId} for model ${model.name} with id ${id}`);
52
+ result.hydrate(data);
53
+ await result.update();
54
+ return new Ok(result.toJSON());
55
+ }
56
+ };
57
+ __decorate([
58
+ Logger('orm-http:api'),
59
+ __metadata("design:type", Log)
60
+ ], CrudUpdate.prototype, "Log", void 0);
61
+ __decorate([
62
+ Autoinject(),
63
+ __metadata("design:type", Orm)
64
+ ], CrudUpdate.prototype, "Orm", void 0);
65
+ __decorate([
66
+ Autoinject(),
67
+ __metadata("design:type", AccessControl)
68
+ ], CrudUpdate.prototype, "Ac", void 0);
69
+ __decorate([
70
+ Put(':model/:id'),
71
+ Permission('updateAny'),
72
+ __param(0, ModelType()),
73
+ __param(1, Param()),
74
+ __param(2, BodyField()),
75
+ __metadata("design:type", Function),
76
+ __metadata("design:paramtypes", [Object, Number, Object]),
77
+ __metadata("design:returntype", Promise)
78
+ ], CrudUpdate.prototype, "update", null);
79
+ __decorate([
80
+ Put(':model/:id/:relation/:relationId'),
81
+ Permission('updateAny'),
82
+ __param(0, ModelType()),
83
+ __param(1, Param()),
84
+ __param(2, Param()),
85
+ __param(3, Param()),
86
+ __param(4, BodyField()),
87
+ __metadata("design:type", Function),
88
+ __metadata("design:paramtypes", [Object, Number, String, Object, Object]),
89
+ __metadata("design:returntype", Promise)
90
+ ], CrudUpdate.prototype, "updateRelation", null);
91
+ CrudUpdate = __decorate([
92
+ BasePath('crud'),
93
+ Policy(FindModelType)
94
+ ], CrudUpdate);
95
+ export { CrudUpdate };
96
+ //# sourceMappingURL=Update.js.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