@ruiapp/rapid-core 0.2.11 → 0.2.13

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.
@@ -20,6 +20,7 @@ declare const _default: {
20
20
  relation?: undefined;
21
21
  targetSingularCode?: undefined;
22
22
  selfIdColumnName?: undefined;
23
+ defaultValue?: undefined;
23
24
  } | {
24
25
  name: string;
25
26
  code: string;
@@ -30,6 +31,7 @@ declare const _default: {
30
31
  relation?: undefined;
31
32
  targetSingularCode?: undefined;
32
33
  selfIdColumnName?: undefined;
34
+ defaultValue?: undefined;
33
35
  } | {
34
36
  name: string;
35
37
  code: string;
@@ -40,6 +42,7 @@ declare const _default: {
40
42
  relation?: undefined;
41
43
  targetSingularCode?: undefined;
42
44
  selfIdColumnName?: undefined;
45
+ defaultValue?: undefined;
43
46
  } | {
44
47
  name: string;
45
48
  code: string;
@@ -50,6 +53,7 @@ declare const _default: {
50
53
  columnName?: undefined;
51
54
  required?: undefined;
52
55
  autoIncrement?: undefined;
56
+ defaultValue?: undefined;
53
57
  } | {
54
58
  name: string;
55
59
  code: string;
@@ -60,6 +64,18 @@ declare const _default: {
60
64
  relation?: undefined;
61
65
  targetSingularCode?: undefined;
62
66
  selfIdColumnName?: undefined;
67
+ defaultValue?: undefined;
68
+ } | {
69
+ name: string;
70
+ code: string;
71
+ columnName: string;
72
+ type: "boolean";
73
+ required: true;
74
+ defaultValue: string;
75
+ autoIncrement?: undefined;
76
+ relation?: undefined;
77
+ targetSingularCode?: undefined;
78
+ selfIdColumnName?: undefined;
63
79
  })[];
64
80
  indexes: ({
65
81
  name: string;
package/dist/index.js CHANGED
@@ -1539,6 +1539,14 @@ var bootstrapApplicationConfig = {
1539
1539
  type: "json",
1540
1540
  required: false,
1541
1541
  },
1542
+ {
1543
+ name: "softDelete",
1544
+ code: "softDelete",
1545
+ columnName: "soft_delete",
1546
+ type: "boolean",
1547
+ required: true,
1548
+ defaultValue: "false",
1549
+ },
1542
1550
  ],
1543
1551
  indexes: [
1544
1552
  {
@@ -3656,12 +3664,26 @@ class EntityManager {
3656
3664
  sender: plugin,
3657
3665
  routeContext,
3658
3666
  });
3659
- await this.#dataAccessor.deleteById(id);
3660
- if (model.base) {
3661
- const baseDataAccessor = this.#server.getDataAccessor({
3662
- singularCode: model.base,
3667
+ if (model.softDelete) {
3668
+ let dataAccessor = model.base
3669
+ ? this.#server.getDataAccessor({
3670
+ singularCode: model.base,
3671
+ })
3672
+ : this.#dataAccessor;
3673
+ const currentUserId = routeContext?.state?.userId;
3674
+ await dataAccessor.updateById(id, {
3675
+ deleted_at: getNowStringWithTimezone(),
3676
+ deleter_id: currentUserId,
3663
3677
  });
3664
- await baseDataAccessor.deleteById(id);
3678
+ }
3679
+ else {
3680
+ await this.#dataAccessor.deleteById(id);
3681
+ if (model.base) {
3682
+ const baseDataAccessor = this.#server.getDataAccessor({
3683
+ singularCode: model.base,
3684
+ });
3685
+ await baseDataAccessor.deleteById(id);
3686
+ }
3665
3687
  }
3666
3688
  await this.#server.emitEvent({
3667
3689
  eventName: "entity.delete",
@@ -6311,6 +6333,11 @@ async function appendFile(path, data) {
6311
6333
  });
6312
6334
  }
6313
6335
 
6336
+ function getFileBaseName(pathname) {
6337
+ const extName = path__default["default"].extname(pathname);
6338
+ return path__default["default"].basename(pathname, extName);
6339
+ }
6340
+
6314
6341
  const code$8 = "downloadDocument";
6315
6342
  async function handler$8(plugin, ctx, options) {
6316
6343
  const { server, applicationConfig, routerContext, input } = ctx;
@@ -6318,24 +6345,52 @@ async function handler$8(plugin, ctx, options) {
6318
6345
  const documentDataAccessor = ctx.server.getDataAccessor({
6319
6346
  singularCode: "ecm_document",
6320
6347
  });
6348
+ const revisionDataAccessor = ctx.server.getDataAccessor({
6349
+ singularCode: "ecm_revision",
6350
+ });
6321
6351
  const storageDataAccessor = ctx.server.getDataAccessor({
6322
6352
  singularCode: "ecm_storage_object",
6323
6353
  });
6324
- const document = await documentDataAccessor.findById(input.documentId);
6325
- if (!document) {
6326
- ctx.output = { error: new Error("Document not found.") };
6354
+ let storageObjectId = 0;
6355
+ let fileName;
6356
+ let { revisionId, documentId } = input;
6357
+ if (revisionId) {
6358
+ const revision = await revisionDataAccessor.findById(revisionId);
6359
+ if (!revision) {
6360
+ ctx.output = { error: new Error(`Revision with id "${revisionId}" was not found.`) };
6361
+ return;
6362
+ }
6363
+ storageObjectId = revision.storage_object_id;
6364
+ documentId = revision.document_id;
6365
+ const document = await documentDataAccessor.findById(documentId);
6366
+ if (!document) {
6367
+ ctx.output = { error: new Error(`Document with id "${documentId}" was not found.`) };
6368
+ return;
6369
+ }
6370
+ fileName = `${getFileBaseName(document.name)}${revision.ext_name}`;
6371
+ }
6372
+ else if (documentId) {
6373
+ const document = await documentDataAccessor.findById(documentId);
6374
+ if (!document) {
6375
+ ctx.output = { error: new Error(`Document with id "${documentId}" was not found.`) };
6376
+ return;
6377
+ }
6378
+ storageObjectId = document.storage_object_id;
6379
+ fileName = document.name;
6380
+ }
6381
+ else {
6382
+ ctx.output = { error: new Error(`Parameter "revisionId" or "documentId" must be provided.`) };
6327
6383
  return;
6328
6384
  }
6329
- const storageObject = await storageDataAccessor.findById(document.storage_object_id);
6385
+ const storageObject = await storageDataAccessor.findById(storageObjectId);
6330
6386
  if (!storageObject) {
6331
- ctx.output = { error: new Error("Storage object not found.") };
6387
+ ctx.output = { error: new Error(`Storage object with id "${storageObjectId}" was not found.`) };
6332
6388
  return;
6333
6389
  }
6334
6390
  const fileKey = storageObject.key;
6335
6391
  const filePathName = path__default["default"].join(server.config.localFileStoragePath, fileKey);
6336
- const attachmentFileName = document.name;
6337
6392
  response.body = await readFile(filePathName);
6338
- response.headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(attachmentFileName)}"`);
6393
+ response.headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(fileName)}"`);
6339
6394
  }
6340
6395
 
6341
6396
  var downloadDocumentActionHandler = /*#__PURE__*/Object.freeze({
package/dist/types.d.ts CHANGED
@@ -186,6 +186,10 @@ export interface RpdDataModel {
186
186
  indexes?: RpdDataModelIndex[];
187
187
  extensions?: RpdDataModelExtension[];
188
188
  permissionPolicies?: RpdDataModelPermissionPolicies;
189
+ /**
190
+ * 是否使用软删除
191
+ */
192
+ softDelete?: boolean;
189
193
  }
190
194
  export interface RpdDataModelPermissionPolicies {
191
195
  find?: PermissionPolicy;
@@ -0,0 +1,3 @@
1
+ export declare function getFileExtensionName(pathname: string): string;
2
+ export declare function getFileName(pathname: string): string;
3
+ export declare function getFileBaseName(pathname: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.2.11",
3
+ "version": "0.2.13",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -121,6 +121,14 @@ export default {
121
121
  type: "json",
122
122
  required: false,
123
123
  },
124
+ {
125
+ name: "softDelete",
126
+ code: "softDelete",
127
+ columnName: "soft_delete",
128
+ type: "boolean",
129
+ required: true,
130
+ defaultValue: "false",
131
+ },
124
132
  ],
125
133
  indexes: [
126
134
  {
@@ -1580,12 +1580,25 @@ export default class EntityManager<TEntity = any> {
1580
1580
  routeContext,
1581
1581
  });
1582
1582
 
1583
- await this.#dataAccessor.deleteById(id);
1584
- if (model.base) {
1585
- const baseDataAccessor = this.#server.getDataAccessor({
1586
- singularCode: model.base,
1583
+ if (model.softDelete) {
1584
+ let dataAccessor = model.base
1585
+ ? this.#server.getDataAccessor({
1586
+ singularCode: model.base,
1587
+ })
1588
+ : this.#dataAccessor;
1589
+ const currentUserId = routeContext?.state?.userId;
1590
+ await dataAccessor.updateById(id, {
1591
+ deleted_at: getNowStringWithTimezone(),
1592
+ deleter_id: currentUserId,
1587
1593
  });
1588
- await baseDataAccessor.deleteById(id);
1594
+ } else {
1595
+ await this.#dataAccessor.deleteById(id);
1596
+ if (model.base) {
1597
+ const baseDataAccessor = this.#server.getDataAccessor({
1598
+ singularCode: model.base,
1599
+ });
1600
+ await baseDataAccessor.deleteById(id);
1601
+ }
1589
1602
  }
1590
1603
 
1591
1604
  await this.#server.emitEvent({
@@ -2,6 +2,7 @@ import path from "path";
2
2
  import { ActionHandlerContext } from "~/core/actionHandler";
3
3
  import { RapidPlugin } from "~/core/server";
4
4
  import { readFile } from "~/utilities/fsUtility";
5
+ import { getFileBaseName } from "~/utilities/pathUtility";
5
6
 
6
7
  export const code = "downloadDocument";
7
8
 
@@ -12,25 +13,53 @@ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, op
12
13
  const documentDataAccessor = ctx.server.getDataAccessor({
13
14
  singularCode: "ecm_document",
14
15
  });
16
+ const revisionDataAccessor = ctx.server.getDataAccessor({
17
+ singularCode: "ecm_revision",
18
+ });
15
19
  const storageDataAccessor = ctx.server.getDataAccessor({
16
20
  singularCode: "ecm_storage_object",
17
21
  });
18
22
 
19
- const document = await documentDataAccessor.findById(input.documentId);
20
- if (!document) {
21
- ctx.output = { error: new Error("Document not found.") };
23
+ let storageObjectId = 0;
24
+ let fileName: string;
25
+ let { revisionId, documentId } = input;
26
+ if (revisionId) {
27
+ const revision = await revisionDataAccessor.findById(revisionId);
28
+ if (!revision) {
29
+ ctx.output = { error: new Error(`Revision with id "${revisionId}" was not found.`) };
30
+ return;
31
+ }
32
+ storageObjectId = revision.storage_object_id;
33
+
34
+ documentId = revision.document_id;
35
+ const document = await documentDataAccessor.findById(documentId);
36
+ if (!document) {
37
+ ctx.output = { error: new Error(`Document with id "${documentId}" was not found.`) };
38
+ return;
39
+ }
40
+ fileName = `${getFileBaseName(document.name!)}${revision.ext_name}`;
41
+ } else if (documentId) {
42
+ const document = await documentDataAccessor.findById(documentId);
43
+ if (!document) {
44
+ ctx.output = { error: new Error(`Document with id "${documentId}" was not found.`) };
45
+ return;
46
+ }
47
+ storageObjectId = document.storage_object_id;
48
+ fileName = document.name;
49
+ } else {
50
+ ctx.output = { error: new Error(`Parameter "revisionId" or "documentId" must be provided.`) };
22
51
  return;
23
52
  }
24
- const storageObject = await storageDataAccessor.findById(document.storage_object_id);
53
+
54
+ const storageObject = await storageDataAccessor.findById(storageObjectId);
25
55
  if (!storageObject) {
26
- ctx.output = { error: new Error("Storage object not found.") };
56
+ ctx.output = { error: new Error(`Storage object with id "${storageObjectId}" was not found.`) };
27
57
  return;
28
58
  }
29
59
 
30
60
  const fileKey = storageObject.key;
31
61
  const filePathName = path.join(server.config.localFileStoragePath, fileKey);
32
- const attachmentFileName = document.name;
33
62
 
34
63
  response.body = await readFile(filePathName);
35
- response.headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(attachmentFileName)}"`);
64
+ response.headers.set("Content-Disposition", `attachment; filename="${encodeURIComponent(fileName)}"`);
36
65
  }
package/src/types.ts CHANGED
@@ -212,6 +212,10 @@ export interface RpdDataModel {
212
212
  indexes?: RpdDataModelIndex[];
213
213
  extensions?: RpdDataModelExtension[];
214
214
  permissionPolicies?: RpdDataModelPermissionPolicies;
215
+ /**
216
+ * 是否使用软删除
217
+ */
218
+ softDelete?: boolean;
215
219
  }
216
220
 
217
221
  export interface RpdDataModelPermissionPolicies {
@@ -0,0 +1,14 @@
1
+ import path from "path";
2
+
3
+ export function getFileExtensionName(pathname: string) {
4
+ return path.extname(pathname);
5
+ }
6
+
7
+ export function getFileName(pathname: string) {
8
+ return path.basename(pathname);
9
+ }
10
+
11
+ export function getFileBaseName(pathname: string) {
12
+ const extName = path.extname(pathname);
13
+ return path.basename(pathname, extName);
14
+ }