s3db.js 3.0.1 → 3.1.0

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/dist/s3db.es.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import { nanoid } from 'nanoid';
3
3
  import { chunk, isObject as isObject$1, merge, cloneDeep, isEmpty, invert, uniq, isString as isString$1, get, set, isFunction as isFunction$1 } from 'lodash-es';
4
4
  import { PromisePool } from '@supercharge/promise-pool';
5
- import { S3Client, PutObjectCommand, GetObjectCommand, HeadObjectCommand, DeleteObjectCommand, DeleteObjectsCommand, ListObjectsV2Command } from '@aws-sdk/client-s3';
5
+ import { S3Client, PutObjectCommand, GetObjectCommand, HeadObjectCommand, CopyObjectCommand, DeleteObjectCommand, DeleteObjectsCommand, ListObjectsV2Command } from '@aws-sdk/client-s3';
6
6
  import { flatten, unflatten } from 'flat';
7
7
  import FastestValidator from 'fastest-validator';
8
8
  import { ReadableStream, TransformStream, WritableStream } from 'node:stream/web';
@@ -844,18 +844,18 @@ class Client extends EventEmitter {
844
844
  this.client = AwsS3Client || this.createClient();
845
845
  }
846
846
  createClient() {
847
- let options = {
847
+ let options2 = {
848
848
  region: this.config.region,
849
849
  endpoint: this.config.endpoint
850
850
  };
851
- if (this.config.forcePathStyle) options.forcePathStyle = true;
851
+ if (this.config.forcePathStyle) options2.forcePathStyle = true;
852
852
  if (this.config.accessKeyId) {
853
- options.credentials = {
853
+ options2.credentials = {
854
854
  accessKeyId: this.config.accessKeyId,
855
855
  secretAccessKey: this.config.secretAccessKey
856
856
  };
857
857
  }
858
- return new S3Client(options);
858
+ return new S3Client(options2);
859
859
  }
860
860
  async sendCommand(command) {
861
861
  this.emit("command.request", command.constructor.name, command.input);
@@ -890,7 +890,7 @@ class Client extends EventEmitter {
890
890
  return error;
891
891
  }
892
892
  async putObject({ key: key2, metadata, contentType, body, contentEncoding }) {
893
- const options = {
893
+ const options2 = {
894
894
  Bucket: this.config.bucket,
895
895
  Key: this.config.keyPrefix ? path.join(this.config.keyPrefix, key2) : key2,
896
896
  Metadata: { ...metadata },
@@ -899,45 +899,63 @@ class Client extends EventEmitter {
899
899
  ContentEncoding: contentEncoding
900
900
  };
901
901
  try {
902
- const response = await this.sendCommand(new PutObjectCommand(options));
903
- this.emit("putObject", response, options);
902
+ const response = await this.sendCommand(new PutObjectCommand(options2));
903
+ this.emit("putObject", response, options2);
904
904
  return response;
905
905
  } catch (error) {
906
906
  throw this.errorProxy(error, {
907
907
  key: key2,
908
- command: options
908
+ command: options2
909
909
  });
910
910
  }
911
911
  }
912
912
  async getObject(key2) {
913
- const options = {
913
+ const options2 = {
914
914
  Bucket: this.config.bucket,
915
915
  Key: path.join(this.config.keyPrefix, key2)
916
916
  };
917
917
  try {
918
- const response = await this.sendCommand(new GetObjectCommand(options));
919
- this.emit("getObject", response, options);
918
+ const response = await this.sendCommand(new GetObjectCommand(options2));
919
+ this.emit("getObject", response, options2);
920
920
  return response;
921
921
  } catch (error) {
922
922
  throw this.errorProxy(error, {
923
923
  key: key2,
924
- command: options
924
+ command: options2
925
925
  });
926
926
  }
927
927
  }
928
928
  async headObject(key2) {
929
- const options = {
929
+ const options2 = {
930
930
  Bucket: this.config.bucket,
931
931
  Key: this.config.keyPrefix ? path.join(this.config.keyPrefix, key2) : key2
932
932
  };
933
933
  try {
934
- const response = await this.client.send(new HeadObjectCommand(options));
935
- this.emit("headObject", response, options);
934
+ const response = await this.client.send(new HeadObjectCommand(options2));
935
+ this.emit("headObject", response, options2);
936
936
  return response;
937
937
  } catch (error) {
938
938
  throw this.errorProxy(error, {
939
939
  key: key2,
940
- command: options
940
+ command: options2
941
+ });
942
+ }
943
+ }
944
+ async copyObject({ from, to }) {
945
+ const options2 = {
946
+ Bucket: this.config.bucket,
947
+ Key: this.config.keyPrefix ? path.join(this.config.keyPrefix, to) : to,
948
+ CopySource: path.join(this.config.bucket, this.config.keyPrefix ? path.join(this.config.keyPrefix, from) : from)
949
+ };
950
+ try {
951
+ const response = await this.client.send(new CopyObjectCommand(options2));
952
+ this.emit("copyObject", response, options2);
953
+ return response;
954
+ } catch (error) {
955
+ throw this.errorProxy(error, {
956
+ from,
957
+ to,
958
+ command: options2
941
959
  });
942
960
  }
943
961
  }
@@ -952,25 +970,25 @@ class Client extends EventEmitter {
952
970
  }
953
971
  }
954
972
  async deleteObject(key2) {
955
- const options = {
973
+ const options2 = {
956
974
  Bucket: this.config.bucket,
957
975
  Key: this.config.keyPrefix ? path.join(this.config.keyPrefix, key2) : key2
958
976
  };
959
977
  try {
960
- const response = await this.sendCommand(new DeleteObjectCommand(options));
961
- this.emit("deleteObject", response, options);
978
+ const response = await this.sendCommand(new DeleteObjectCommand(options2));
979
+ this.emit("deleteObject", response, options2);
962
980
  return response;
963
981
  } catch (error) {
964
982
  throw this.errorProxy(error, {
965
983
  key: key2,
966
- command: options
984
+ command: options2
967
985
  });
968
986
  }
969
987
  }
970
988
  async deleteObjects(keys) {
971
989
  const packages = chunk(keys, 1e3);
972
990
  const { results, errors } = await PromisePool.for(packages).withConcurrency(this.parallelism).process(async (keys2) => {
973
- const options = {
991
+ const options2 = {
974
992
  Bucket: this.config.bucket,
975
993
  Delete: {
976
994
  Objects: keys2.map((key2) => ({
@@ -979,12 +997,12 @@ class Client extends EventEmitter {
979
997
  }
980
998
  };
981
999
  try {
982
- const response = await this.sendCommand(new DeleteObjectsCommand(options));
1000
+ const response = await this.sendCommand(new DeleteObjectsCommand(options2));
983
1001
  return response;
984
1002
  } catch (error) {
985
1003
  throw this.errorProxy(error, {
986
1004
  key,
987
- command: options
1005
+ command: options2
988
1006
  });
989
1007
  }
990
1008
  });
@@ -995,23 +1013,42 @@ class Client extends EventEmitter {
995
1013
  this.emit("deleteObjects", report, keys);
996
1014
  return report;
997
1015
  }
1016
+ async deleteAll({ prefix } = {}) {
1017
+ const keys = await this.getAllKeys({ prefix });
1018
+ const report = await this.deleteObjects(keys);
1019
+ this.emit("deleteAll", { prefix, report });
1020
+ return report;
1021
+ }
1022
+ async moveObject({ from, to }) {
1023
+ try {
1024
+ await this.copyObject({ from, to });
1025
+ await this.deleteObject(from);
1026
+ return true;
1027
+ } catch (error) {
1028
+ throw this.errorProxy(error, {
1029
+ from,
1030
+ to,
1031
+ command: options
1032
+ });
1033
+ }
1034
+ }
998
1035
  async listObjects({
999
1036
  prefix,
1000
1037
  maxKeys = 1e3,
1001
1038
  continuationToken
1002
1039
  } = {}) {
1003
- const options = {
1040
+ const options2 = {
1004
1041
  Bucket: this.config.bucket,
1005
1042
  MaxKeys: maxKeys,
1006
1043
  ContinuationToken: continuationToken,
1007
1044
  Prefix: this.config.keyPrefix ? path.join(this.config.keyPrefix, prefix || "") : prefix || ""
1008
1045
  };
1009
1046
  try {
1010
- const response = await this.sendCommand(new ListObjectsV2Command(options));
1011
- this.emit("listObjects", response, options);
1047
+ const response = await this.sendCommand(new ListObjectsV2Command(options2));
1048
+ this.emit("listObjects", response, options2);
1012
1049
  return response;
1013
1050
  } catch (error) {
1014
- throw this.errorProxy(error, { command: options });
1051
+ throw this.errorProxy(error, { command: options2 });
1015
1052
  }
1016
1053
  }
1017
1054
  async count({ prefix } = {}) {
@@ -1019,11 +1056,11 @@ class Client extends EventEmitter {
1019
1056
  let truncated = true;
1020
1057
  let continuationToken;
1021
1058
  while (truncated) {
1022
- const options = {
1059
+ const options2 = {
1023
1060
  prefix,
1024
1061
  continuationToken
1025
1062
  };
1026
- const response = await this.listObjects(options);
1063
+ const response = await this.listObjects(options2);
1027
1064
  count += response.KeyCount || 0;
1028
1065
  truncated = response.IsTruncated || false;
1029
1066
  continuationToken = response.NextContinuationToken;
@@ -1036,11 +1073,11 @@ class Client extends EventEmitter {
1036
1073
  let truncated = true;
1037
1074
  let continuationToken;
1038
1075
  while (truncated) {
1039
- const options = {
1076
+ const options2 = {
1040
1077
  prefix,
1041
1078
  continuationToken
1042
1079
  };
1043
- const response = await this.listObjects(options);
1080
+ const response = await this.listObjects(options2);
1044
1081
  if (response.Contents) {
1045
1082
  keys = keys.concat(response.Contents.map((x) => x.Key));
1046
1083
  }
@@ -1064,12 +1101,12 @@ class Client extends EventEmitter {
1064
1101
  let skipped = 0;
1065
1102
  while (truncated) {
1066
1103
  let maxKeys = offset < 1e3 ? offset : offset - skipped > 1e3 ? 1e3 : offset - skipped;
1067
- const options = {
1104
+ const options2 = {
1068
1105
  prefix,
1069
1106
  maxKeys,
1070
1107
  continuationToken
1071
1108
  };
1072
- const res = await this.listObjects(options);
1109
+ const res = await this.listObjects(options2);
1073
1110
  if (res.Contents) {
1074
1111
  skipped += res.Contents.length;
1075
1112
  }
@@ -1098,11 +1135,11 @@ class Client extends EventEmitter {
1098
1135
  });
1099
1136
  }
1100
1137
  while (truncated) {
1101
- const options = {
1138
+ const options2 = {
1102
1139
  prefix,
1103
1140
  continuationToken
1104
1141
  };
1105
- const res = await this.listObjects(options);
1142
+ const res = await this.listObjects(options2);
1106
1143
  if (res.Contents) {
1107
1144
  keys = keys.concat(res.Contents.map((x) => x.Key));
1108
1145
  }
@@ -1119,6 +1156,30 @@ class Client extends EventEmitter {
1119
1156
  this.emit("getKeysPage", keys, params);
1120
1157
  return keys;
1121
1158
  }
1159
+ async moveAllObjects({ prefixFrom, prefixTo }) {
1160
+ const keys = await this.getAllKeys({ prefix: prefixFrom });
1161
+ const { results, errors } = await PromisePool.for(keys).withConcurrency(this.parallelism).process(async (key2) => {
1162
+ const to = key2.replace(prefixFrom, prefixTo);
1163
+ try {
1164
+ await this.moveObject({
1165
+ from: key2,
1166
+ to
1167
+ });
1168
+ return to;
1169
+ } catch (error) {
1170
+ throw this.errorProxy(error, {
1171
+ from: key2,
1172
+ to
1173
+ });
1174
+ }
1175
+ });
1176
+ this.emit("moveAllObjects", { results, errors }, { prefixFrom, prefixTo });
1177
+ if (errors.length > 0) {
1178
+ console.log({ errors });
1179
+ throw new Error("Some objects could not be moved");
1180
+ }
1181
+ return results;
1182
+ }
1122
1183
  }
1123
1184
 
1124
1185
  async function dynamicCrypto() {
@@ -1800,8 +1861,8 @@ class Database extends EventEmitter {
1800
1861
  const file = { ...metadata };
1801
1862
  if (isEmpty(file.resources)) return file;
1802
1863
  for (const [name, structure] of Object.entries(file.resources)) {
1803
- for (const [attr, value] of Object.entries(structure.schema)) {
1804
- file.resources[name].schema[attr] = JSON.parse(value);
1864
+ for (const [attr, value] of Object.entries(structure.attributes)) {
1865
+ file.resources[name].attributes[attr] = JSON.parse(value);
1805
1866
  }
1806
1867
  }
1807
1868
  return file;
@@ -12864,7 +12925,6 @@ class S3Cache extends Cache {
12864
12925
  const keys = await this.client.getAllKeys({
12865
12926
  prefix: join(this.keyPrefix, dir)
12866
12927
  });
12867
- console.log({ keys });
12868
12928
  await this.client.deleteObjects(keys);
12869
12929
  }
12870
12930
  }
@@ -12987,12 +13047,10 @@ class CachePlugin extends Plugin {
12987
13047
  async stop() {
12988
13048
  }
12989
13049
  installDatabaseProxy() {
12990
- const db = this;
12991
13050
  const installResourcesProxies = this.installResourcesProxies.bind(this);
12992
13051
  this.database._createResource = this.database.createResource;
12993
13052
  this.database.createResource = async function(...args) {
12994
13053
  const resource = await this._createResource(...args);
12995
- console.log(db.driver);
12996
13054
  installResourcesProxies(resource);
12997
13055
  return resource;
12998
13056
  };