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