@pittica/google-cloud-storage-helpers 1.0.0 → 1.2.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/README.md CHANGED
@@ -15,7 +15,7 @@ This project uses [Cloud Storage](https://cloud.google.com/storage/docs) librari
15
15
 
16
16
  This project is licensed.
17
17
 
18
- Copyright 2024 **Pittica S.r.l.**.
18
+ Copyright 2024-2025 **Pittica S.r.l.**.
19
19
 
20
20
  - [pittica.com](https://pittica.com)
21
21
  - [info@pittica.com](mailto:info@pittica.com)
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- // Copyright 2024 Pittica S.r.l.
1
+ // Copyright 2024-2025 Pittica S.r.l.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -15,20 +15,30 @@
15
15
  const {
16
16
  copyFile,
17
17
  deleteFile,
18
- moveFiles
18
+ moveFiles,
19
+ getFiles,
20
+ copiedFile
19
21
  } = require("./storage/file");
20
22
  const {
21
- bucket,
22
- file,
23
- folder
23
+ downloadBucket,
24
+ downloadFile,
25
+ downloadFolder
24
26
  } = require("./storage/download");
25
27
  const {
26
- write
28
+ writeJson,
29
+ groupJson
27
30
  } = require("./storage/json");
31
+ const {
32
+ getStorage
33
+ } = require("./storage/storage");
28
34
  exports.copyFile = copyFile;
29
35
  exports.deleteFile = deleteFile;
30
36
  exports.moveFiles = moveFiles;
31
- exports.downloadFile = file;
32
- exports.downloadBucket = bucket;
33
- exports.downloadFolder = folder;
34
- exports.writeJson = write;
37
+ exports.getFiles = getFiles;
38
+ exports.copiedFile = copiedFile;
39
+ exports.writeJson = writeJson;
40
+ exports.groupJson = groupJson;
41
+ exports.downloadFile = downloadFile;
42
+ exports.downloadBucket = downloadBucket;
43
+ exports.downloadFolder = downloadFolder;
44
+ exports.getStorage = getStorage;
package/dist/scripts.js CHANGED
@@ -1,4 +1,4 @@
1
- // Copyright 2024 Pittica S.r.l.
1
+ // Copyright 2024-2025 Pittica S.r.l.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- // Copyright 2024 Pittica S.r.l.
1
+ // Copyright 2024-2025 Pittica S.r.l.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -26,13 +26,13 @@ const fs = require("fs");
26
26
  * @param {string} bucket Bucket name.
27
27
  * @param {string} destination Destination path.
28
28
  */
29
- exports.bucket = async (bucket, destination = __dirname) => {
29
+ exports.downloadBucket = async (bucket, destination = __dirname) => {
30
30
  const storage = new Storage();
31
31
  const source = storage.bucket(bucket);
32
32
  const [files] = await source.getFiles();
33
33
  files.forEach(({
34
34
  name
35
- }) => this.file(name, source, destination));
35
+ }) => this.downloadFile(name, source, destination));
36
36
  };
37
37
 
38
38
  /**
@@ -42,7 +42,7 @@ exports.bucket = async (bucket, destination = __dirname) => {
42
42
  * @param {Bucket} bucket Google Cloud Storage bucket object.
43
43
  * @param {string} directory Destination directory.
44
44
  */
45
- exports.file = async (file, bucket, directory = __dirname) => {
45
+ exports.downloadFile = async (file, bucket, directory = __dirname) => {
46
46
  const destination = path.join(directory, file);
47
47
  const folder = path.dirname(destination);
48
48
  if (!fs.existsSync(folder)) {
@@ -62,7 +62,7 @@ exports.file = async (file, bucket, directory = __dirname) => {
62
62
  *
63
63
  * @returns {Promise}
64
64
  */
65
- exports.folder = (folder, bucket, directory = __dirname) => {
65
+ exports.downloadFolder = (folder, bucket, directory = __dirname) => {
66
66
  const storage = new Storage();
67
67
  const manager = new TransferManager(storage.bucket(bucket));
68
68
  const destination = path.join(directory, folder);
@@ -1,4 +1,4 @@
1
- // Copyright 2024 Pittica S.r.l.
1
+ // Copyright 2024-2025 Pittica S.r.l.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -59,4 +59,29 @@ exports.copyFile = (source, destination) => source.copy(source.storage.bucket(de
59
59
  exports.deleteFile = file => file.delete().then(response => {
60
60
  const [res] = response;
61
61
  return res.statusCode >= 200 && res.statusCode < 300;
62
- }).catch(() => false);
62
+ }).catch(() => false);
63
+
64
+ /**
65
+ * Gets the files from the given bucket.
66
+ *
67
+ * @param {Storage} storage Google Cloud Storage object.
68
+ * @param {string} bucket Bucket name.
69
+ * @param {string} prefix Files prefix.
70
+ * @returns {Array} Files from the given bucket.
71
+ */
72
+ exports.getFiles = (storage, source, prefix) => storage.bucket(source).getFiles({
73
+ prefix
74
+ }).then(files => files).catch(() => []);
75
+
76
+ /**
77
+ * Process the response from copy API and extracts the response file object.
78
+ *
79
+ * @param {Array} response Response.
80
+ * @returns {File} File object or a null value.
81
+ */
82
+ exports.copiedFile = response => {
83
+ if (typeof response[1] !== "undefined" && response[1].done) {
84
+ return response[0];
85
+ }
86
+ return null;
87
+ };
@@ -1,4 +1,4 @@
1
- // Copyright 2024 Pittica S.r.l.
1
+ // Copyright 2024-2025 Pittica S.r.l.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@ const log = require("@pittica/logger-helpers");
25
25
  *
26
26
  * @returns {boolean} Write action response.
27
27
  */
28
- exports.write = async (file, body, bucket) => {
28
+ exports.writeJson = async (file, body, bucket) => {
29
29
  const storage = new Storage();
30
30
  const stream = storage.bucket(bucket).file(file).createWriteStream({
31
31
  metadata: {
@@ -36,4 +36,25 @@ exports.write = async (file, body, bucket) => {
36
36
  stream.on("finish", () => log.success(`"${file}" has been written.`));
37
37
  const buffer = Buffer.from(JSON.stringify(body));
38
38
  return stream.end(buffer);
39
+ };
40
+
41
+ /**
42
+ * Group files by table.
43
+ *
44
+ * @param {Array} files An array of files.
45
+ */
46
+ exports.groupJson = files => {
47
+ const result = {};
48
+ files.forEach(file => {
49
+ if (file.endsWith(".json")) {
50
+ const group = file.split(/^(.*)\-[\d]+\.json$/gis);
51
+ if (group.length > 1) {
52
+ if (typeof result[group[1]] === "undefined") {
53
+ result[group[1]] = [];
54
+ }
55
+ result[group[1]].push(file);
56
+ }
57
+ }
58
+ });
59
+ return result;
39
60
  };
@@ -0,0 +1,29 @@
1
+ // Copyright 2024-2025 Pittica S.r.l.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ const {
16
+ Storage
17
+ } = require("@google-cloud/storage");
18
+
19
+ /**
20
+ * Gets a Google Cloud Storage client with default settings.
21
+ *
22
+ * @returns {Storage} A Google Cloud Storage client.
23
+ */
24
+ exports.getStorage = () => new Storage({
25
+ retryOptions: {
26
+ autoRetry: true,
27
+ retryDelayMultiplier: 4
28
+ }
29
+ });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pittica/google-cloud-storage-helpers",
3
3
  "private": false,
4
- "version": "1.0.0",
4
+ "version": "1.2.0",
5
5
  "description": "Helpers for Google Cloud Storage.",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {
@@ -26,13 +26,13 @@
26
26
  },
27
27
  "homepage": "https://github.com/pittica/google-cloud-storage-helpers#README.md",
28
28
  "dependencies": {
29
- "@google-cloud/storage": "^7.14.0",
29
+ "@google-cloud/storage": "^7.15.2",
30
30
  "@pittica/logger-helpers": "^0.9.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@babel/cli": "^7.25.9",
33
+ "@babel/cli": "^7.26.4",
34
34
  "mkdirp": "^3.0.1",
35
- "prettier": "^3.3.3",
35
+ "prettier": "^3.5.3",
36
36
  "rimraf": "^6.0.1",
37
37
  "run-func": "^3.0.0"
38
38
  }