@punks/backend-entity-manager 0.0.89 → 0.0.91

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.
@@ -31,4 +31,5 @@ export type EntitiesSampleDownloadOptions = {
31
31
  };
32
32
  export type EntitiesSampleDownloadResult = {
33
33
  file: EntitiesExportFile;
34
+ downloadUrl: string;
34
35
  };
@@ -1,8 +1,12 @@
1
1
  import { IEntitiesSampleDownloadCommand } from "../abstractions/commands";
2
2
  import { EntityServiceLocator } from "../providers/services";
3
- import { EntitiesSampleDownloadOptions, EntitiesSampleDownloadResult } from "../abstractions";
3
+ import { EntitiesImportExportSettings, EntitiesSampleDownloadOptions, EntitiesSampleDownloadResult } from "../abstractions";
4
4
  export declare class EntitiesSampleDownloadCommand<TEntity> implements IEntitiesSampleDownloadCommand<TEntity> {
5
5
  private readonly services;
6
- constructor(services: EntityServiceLocator<TEntity, unknown>);
6
+ private readonly settings;
7
+ constructor(services: EntityServiceLocator<TEntity, unknown>, settings: EntitiesImportExportSettings);
7
8
  execute(input: EntitiesSampleDownloadOptions): Promise<EntitiesSampleDownloadResult>;
9
+ private uploadSampleFile;
10
+ private buildAbsoluteBucketPath;
11
+ private get bucket();
8
12
  }
@@ -1,8 +1,6 @@
1
1
  /// <reference types="multer" />
2
- import { StreamableFile } from "@nestjs/common";
3
- import type { Response } from "express";
4
2
  import { FooActions } from "./foo.actions";
5
- import { FooCreateDto, FooDto, FooExportRequest, FooExportResponse, FooImportRequest, FooSampleDownloadRequest, FooSearchRequest, FooSearchResponse, FooUpdateDto } from "./foo.dto";
3
+ import { FooCreateDto, FooDto, FooExportRequest, FooExportResponse, FooImportRequest, FooSampleDownloadRequest, FooSampleDownloadResponse, FooSearchRequest, FooSearchResponse, FooUpdateDto } from "./foo.dto";
6
4
  export declare class FooController {
7
5
  private readonly actions;
8
6
  constructor(actions: FooActions);
@@ -13,5 +11,5 @@ export declare class FooController {
13
11
  search(request: FooSearchRequest): Promise<FooSearchResponse>;
14
12
  import(file: Express.Multer.File, request: FooImportRequest): Promise<void>;
15
13
  export(request: FooExportRequest): Promise<FooExportResponse>;
16
- sampleDownload(res: Response, request: FooSampleDownloadRequest): Promise<StreamableFile>;
14
+ sampleDownload(request: FooSampleDownloadRequest): Promise<FooSampleDownloadResponse>;
17
15
  }
@@ -37,11 +37,11 @@ export declare class FooExportRequest {
37
37
  filter?: FooSearchParameters;
38
38
  }
39
39
  export declare class FooExportResponse {
40
- fileUrl: string;
40
+ downloadUrl: string;
41
41
  }
42
42
  export declare class FooSampleDownloadRequest {
43
43
  format: EntitySerializationFormat;
44
44
  }
45
45
  export declare class FooSampleDownloadResponse {
46
- fileUrl: string;
46
+ downloadUrl: string;
47
47
  }
package/dist/esm/index.js CHANGED
@@ -140,10 +140,16 @@ class EntitySerializer {
140
140
  return {
141
141
  fileName,
142
142
  contentType: "text/csv",
143
- content: Buffer.from(csvBuild([{}], this.getDefinition().columns.map((c) => ({
144
- name: c.name,
145
- value: () => c.sampleValue ?? "",
146
- }))), "utf-8"),
143
+ content: Buffer.from(csvBuild([{}], [
144
+ {
145
+ name: "_type",
146
+ value: () => this.entityName,
147
+ },
148
+ ...this.getDefinition().columns.map((c) => ({
149
+ name: c.name,
150
+ value: () => c.sampleValue ?? "",
151
+ })),
152
+ ]), "utf-8"),
147
153
  };
148
154
  case EntitySerializationFormat.Xlsx:
149
155
  return {
@@ -152,11 +158,17 @@ class EntitySerializer {
152
158
  content: Buffer.from(excelBuild({
153
159
  data: [{}],
154
160
  sheetName: this.entityName,
155
- columns: this.getDefinition().columns.map((c) => ({
156
- header: c.name,
157
- value: () => c.sampleValue ?? "",
158
- headerSize: c.colSpan,
159
- })),
161
+ columns: [
162
+ {
163
+ header: "_type",
164
+ value: () => this.entityName,
165
+ },
166
+ ...this.getDefinition().columns.map((c) => ({
167
+ header: c.name,
168
+ value: () => c.sampleValue ?? "",
169
+ headerSize: c.colSpan,
170
+ })),
171
+ ],
160
172
  })),
161
173
  };
162
174
  case EntitySerializationFormat.Json:
@@ -710,11 +722,11 @@ class EntitiesSampleDownloadAction {
710
722
  this.logger = Log.getLogger(`${services.getEntityName()} -> Import`);
711
723
  }
712
724
  async execute(input) {
713
- this.logger.debug("Import action started", { input });
725
+ this.logger.debug("Sample download action started", { input });
714
726
  const result = await this.services
715
727
  .resolveSampleDownloadCommand()
716
728
  .execute(input);
717
- this.logger.debug("Import action completed", { input });
729
+ this.logger.debug("Sample download action completed", { input });
718
730
  return result;
719
731
  }
720
732
  }
@@ -916,7 +928,7 @@ class EntitiesExportCommand {
916
928
  });
917
929
  }
918
930
  buildAbsoluteBucketPath(relativePath) {
919
- return `${this.settings.exportBucket.rootFolderPath ?? ""}/${createDayPath(new Date())}/${relativePath}`;
931
+ return `${this.settings.exportBucket.rootFolderPath ?? ""}/exports/${createDayPath(new Date())}/${relativePath}`;
920
932
  }
921
933
  async getExportEntities(filters) {
922
934
  return this.services.resolveSearchQuery().execute(filters ?? {});
@@ -937,21 +949,43 @@ class EntitiesImportCommand {
937
949
  }
938
950
 
939
951
  class EntitiesSampleDownloadCommand {
940
- constructor(services) {
952
+ constructor(services, settings) {
941
953
  this.services = services;
954
+ this.settings = settings;
942
955
  }
943
956
  async execute(input) {
944
957
  const sample = await this.services
945
958
  .resolveSerializer()
946
959
  .createSample(input.format);
960
+ const downloadUrl = await this.uploadSampleFile(sample);
947
961
  return {
948
962
  file: {
949
963
  content: sample.content,
950
964
  contentType: sample.contentType,
951
965
  name: sample.fileName,
952
966
  },
967
+ downloadUrl,
953
968
  };
954
969
  }
970
+ async uploadSampleFile(file) {
971
+ await this.bucket.fileUpload({
972
+ bucket: this.settings.exportBucket.bucket,
973
+ filePath: this.buildAbsoluteBucketPath(file.fileName),
974
+ content: file.content,
975
+ contentType: file.contentType,
976
+ });
977
+ return await this.bucket.filePublicUrlCreate({
978
+ bucket: this.settings.exportBucket.bucket,
979
+ expirationMinutes: this.settings.exportBucket.publicLinksExpirationMinutes,
980
+ filePath: this.buildAbsoluteBucketPath(file.fileName),
981
+ });
982
+ }
983
+ buildAbsoluteBucketPath(relativePath) {
984
+ return `${this.settings.exportBucket.rootFolderPath ?? ""}/samples/${createDayPath(new Date())}/${relativePath}`;
985
+ }
986
+ get bucket() {
987
+ return this.services.getRootServices().resolveBucketProvider();
988
+ }
955
989
  }
956
990
 
957
991
  class EntityUpdateCommand {
@@ -2098,7 +2132,7 @@ class EntityManagerServiceCollection {
2098
2132
  this.locator.registerExportAction(this.entityName, new EntitiesExportAction(this.resolver));
2099
2133
  this.locator.registerImportCommand(this.entityName, new EntitiesImportCommand(this.resolver, settings));
2100
2134
  this.locator.registerImportAction(this.entityName, new EntitiesImportAction(this.resolver));
2101
- this.locator.registerSampleDownloadCommand(this.entityName, new EntitiesSampleDownloadCommand(this.resolver));
2135
+ this.locator.registerSampleDownloadCommand(this.entityName, new EntitiesSampleDownloadCommand(this.resolver, settings));
2102
2136
  this.locator.registerSampleDownloadAction(this.entityName, new EntitiesSampleDownloadAction(this.resolver));
2103
2137
  return this;
2104
2138
  }