@pittica/google-business-intelligence-helpers 1.15.0 → 1.15.2

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.
@@ -13,11 +13,13 @@
13
13
  // limitations under the License.
14
14
 
15
15
  const {
16
- getBucket
16
+ getBucket,
17
+ groupJson
17
18
  } = require("@pittica/google-cloud-storage-helpers");
18
19
  const {
19
20
  getDataset,
20
- jobDone
21
+ jobDone,
22
+ getTable
21
23
  } = require("@pittica/google-bigquery-helpers");
22
24
  const log = require("@pittica/logger-helpers");
23
25
  const {
@@ -39,7 +41,7 @@ exports.backup = async (id, bucket, projectId, dateFormat = "YYYY-MM-DD") => {
39
41
  });
40
42
  const [tables] = await dataset.getTables();
41
43
  const date = format(new Date(), dateFormat);
42
- return await exports.extract(bucket, date, tables, 0);
44
+ return await exports.extract(bucket, date, tables, 0, dataset.metadata.location);
43
45
  };
44
46
 
45
47
  /**
@@ -49,20 +51,21 @@ exports.backup = async (id, bucket, projectId, dateFormat = "YYYY-MM-DD") => {
49
51
  * @param {string} date Date in format YYYY-MM-DD.
50
52
  * @param {Array} tables An array of BigQuery tables.
51
53
  * @param {number} index Array index.
54
+ * @param {string} location Location of the BigQuery dataset.
52
55
  * @returns {boolean} A value indicating whehter the job has been done.
53
56
  */
54
- exports.extract = async (bucket, date, tables, index) => {
57
+ exports.extract = async (bucket, date, tables, index = 0, location = "us-central1") => {
55
58
  if (index < tables.length) {
56
59
  const table = tables[index];
57
60
  return await table.extract(getBucket(bucket).file(`${date}/${table.id}-*.json`), {
58
- location: "us-central1",
61
+ location,
59
62
  format: "json",
60
63
  gzip: false
61
64
  }).then(async response => {
62
65
  if (jobDone(response)) {
63
66
  const [job] = response;
64
67
  log.success(`Job backup "${job.id}" for table "${table.id}" done.`);
65
- return await exports.extract(date, tables, index + 1);
68
+ return await exports.extract(bucket, date, tables, index + 1, location);
66
69
  } else {
67
70
  return false;
68
71
  }
@@ -88,7 +91,7 @@ exports.rollback = async (date, id, projectId, sourceBucket, destinationDataset)
88
91
  const dataset = await getDataset(id, {
89
92
  projectId
90
93
  });
91
- const bucket = getBucket(sourceBucket);
94
+ const bucket = getBucket(sourceBucket, projectId);
92
95
  const [response] = await bucket.getFiles({
93
96
  prefix: `${date}/`
94
97
  });
@@ -96,16 +99,19 @@ exports.rollback = async (date, id, projectId, sourceBucket, destinationDataset)
96
99
  log.info(`Begin rollback "${date}".`);
97
100
  Object.keys(files).forEach(async key => {
98
101
  const [table] = await dataset.table(key).get();
99
- files[key].forEach(filename => bigquery.dataset(destinationDataset, {
100
- location: dataset.metadata.location,
101
- projectId
102
- }).table(key).load(bucket.file(`${date}/${filename}`), {
103
- sourceFormat: "NEWLINE_DELIMITED_JSON",
104
- schema: table.metadata.schema,
105
- timePartitioning: table.metadata.timePartitioning,
106
- location: table.metadata.location,
107
- tableConstraints: table.metadata.tableConstraints,
108
- partitionDefinition: table.metadata.partitionDefinition
109
- }));
102
+ files[key].forEach(async filename => {
103
+ const destination = await getTable(destinationDataset, key, {
104
+ projectId,
105
+ location: dataset.metadata.location
106
+ });
107
+ await destination.load(bucket.file(`${date}/${filename}`), {
108
+ sourceFormat: "NEWLINE_DELIMITED_JSON",
109
+ schema: table.metadata.schema,
110
+ timePartitioning: table.metadata.timePartitioning,
111
+ location: table.metadata.location,
112
+ tableConstraints: table.metadata.tableConstraints,
113
+ partitionDefinition: table.metadata.partitionDefinition
114
+ });
115
+ });
110
116
  });
111
117
  };
@@ -169,9 +169,9 @@ exports.erase = async (bucket, days = process.env.BACKUP_KEEP_DAYS || 3) => {
169
169
  });
170
170
  const folders = Object.keys(result).sort();
171
171
  const blacklist = folders.slice(0, folders.length - parseInt(days));
172
- blacklist.forEach(async prefix => await b.deleteFiles({
172
+ await Promise.all(blacklist.map(async prefix => await b.deleteFiles({
173
173
  prefix
174
- }));
174
+ })));
175
175
  log.success("Backup bucket erased.");
176
176
  return true;
177
177
  };
@@ -34,10 +34,14 @@ exports.scriptBackup = backup;
34
34
  /**
35
35
  * Creates a backup and erases the bucket.
36
36
  *
37
+ * @param {string} id BigQuery dataset ID.
38
+ * @param {string} bucket Bucket name.
39
+ * @param {string} projectId Google Cloud project ID.
37
40
  * @param {number} days Number of days to keep.
41
+ * @param {string} dateFormat Date format string.
38
42
  * @returns {boolean} A value indicating whehter the job has been done.
39
43
  */
40
- exports.scriptBackupErase = async days => await backup().then(async () => await erase(days));
44
+ exports.scriptBackupErase = async (id, bucket, projectId, days, dateFormat = "YYYY-MM-DD") => await backup(id, bucket, projectId, dateFormat).then(async () => await erase(bucket, days));
41
45
 
42
46
  /**
43
47
  * Rollback data using backup.
@@ -26,6 +26,7 @@ const {
26
26
  * @param {string} dateFormat Date format string.
27
27
  */
28
28
  exports.scriptImplode = implode;
29
+
29
30
  /**
30
31
  * Erases the bucket.
31
32
  *
@@ -36,12 +36,12 @@ exports.scriptUnlistened = async (bucket, folder) => {
36
36
  const storage = getStorage();
37
37
  const [files] = await getFiles(storage, bucket);
38
38
  if (files) {
39
- files.forEach(file => {
39
+ files.forEach(async file => {
40
40
  const {
41
41
  name
42
42
  } = splitName(file.id);
43
43
  if (!hasSchema(name, folder)) {
44
- deleteFile(file);
44
+ await deleteFile(file);
45
45
  }
46
46
  });
47
47
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pittica/google-business-intelligence-helpers",
3
3
  "private": false,
4
- "version": "1.15.0",
4
+ "version": "1.15.2",
5
5
  "description": "Helpers for business intelligence for Google Cloud.",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {
@@ -36,8 +36,8 @@
36
36
  "node": ">=24.11.0"
37
37
  },
38
38
  "dependencies": {
39
- "@pittica/google-bigquery-helpers": "^1.6.1",
40
- "@pittica/google-cloud-storage-helpers": "^1.5.0",
39
+ "@pittica/google-bigquery-helpers": "^1.7.0",
40
+ "@pittica/google-cloud-storage-helpers": "^1.6.0",
41
41
  "@pittica/logger-helpers": "^1.2.2",
42
42
  "date-and-time": "^4.7.0"
43
43
  },