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

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.
@@ -35,13 +35,13 @@ const {
35
35
  * @param {string} dateFormat Date format string.
36
36
  * @returns {boolean} A value indicating whehter the job has been done.
37
37
  */
38
- exports.backup = async (id, bucket, projectId, dateFormat = "YYYY-MM-DD") => {
38
+ exports.backup = async (id, bucket, projectId = null, dateFormat = "YYYY-MM-DD") => {
39
39
  const dataset = await getDataset(id, {
40
40
  projectId
41
41
  });
42
42
  const [tables] = await dataset.getTables();
43
43
  const date = format(new Date(), dateFormat);
44
- return await exports.extract(bucket, date, tables, 0, dataset.metadata.location);
44
+ return await exports.extract(bucket, date, tables, 0, dataset.metadata.location, projectId);
45
45
  };
46
46
 
47
47
  /**
@@ -52,12 +52,13 @@ exports.backup = async (id, bucket, projectId, dateFormat = "YYYY-MM-DD") => {
52
52
  * @param {Array} tables An array of BigQuery tables.
53
53
  * @param {number} index Array index.
54
54
  * @param {string} location Location of the BigQuery dataset.
55
+ * @param {string} projectId Google Cloud project ID.
55
56
  * @returns {boolean} A value indicating whehter the job has been done.
56
57
  */
57
- exports.extract = async (bucket, date, tables, index = 0, location = "us-central1") => {
58
+ exports.extract = async (bucket, date, tables, index = 0, location = "us-central1", projectId = null) => {
58
59
  if (index < tables.length) {
59
60
  const table = tables[index];
60
- return await table.extract(getBucket(bucket).file(`${date}/${table.id}-*.json`), {
61
+ return await table.extract(getBucket(bucket, projectId).file(`${date}/${table.id}-*.json`), {
61
62
  location,
62
63
  format: "json",
63
64
  gzip: false
@@ -65,7 +66,7 @@ exports.extract = async (bucket, date, tables, index = 0, location = "us-central
65
66
  if (jobDone(response)) {
66
67
  const [job] = response;
67
68
  log.success(`Job backup "${job.id}" for table "${table.id}" done.`);
68
- return await exports.extract(bucket, date, tables, index + 1, location);
69
+ return await exports.extract(bucket, date, tables, index + 1, location, projectId);
69
70
  } else {
70
71
  return false;
71
72
  }
@@ -93,12 +93,20 @@ exports.extractStorageResponse = (response, schemas = "", dateFormat = "YYYY-MM-
93
93
  * @param {string} folder Google Storage folder path.
94
94
  * @returns {object} A safe name of a file in the given folder in the given bucket.
95
95
  */
96
- exports.getSafeFilename = (storage, bucket, filename, folder = "") => getFiles(storage, bucket, folder ? `${folder}/${filename}` : filename).then(response => {
97
- const file = response.flat(3).filter(value => value && typeof value.name !== "undefined").map(({
98
- name
99
- }) => folder ? name.replace(new RegExp(`^${folder}\/`, "gs"), "") : name).slice(-1).pop();
100
- return typeof file !== "undefined" && file ? incrementFilenameVersion(splitName(file)) : splitName(filename);
101
- });
96
+ exports.getSafeFilename = (storage, bucket, filename, folder = "") => {
97
+ const {
98
+ name,
99
+ ext
100
+ } = path.parse(filename);
101
+ const regex = new RegExp(`${name}(_\\d+)?\\${ext}$`, "gs");
102
+ return getFiles(storage, bucket, folder ? `${folder}/${name}` : name).then(response => {
103
+ const [files] = response;
104
+ const file = files.flat(3).filter(value => value && typeof value.name !== "undefined" && regex.test(value.name)).map(({
105
+ name
106
+ }) => folder ? name.replace(new RegExp(`^${folder}\/`, "gs"), "") : name).slice(-1).pop();
107
+ return typeof file !== "undefined" && file ? incrementFilenameVersion(splitName(file)) : splitName(filename);
108
+ });
109
+ };
102
110
 
103
111
  /**
104
112
  * Ritrieves a safe name of a file in the given folder in the given bucket.
@@ -120,12 +128,13 @@ exports.getSafeFilenameFromBucket = ({
120
128
  * @param {string} output Path to save files.
121
129
  * @param {string} bucket Bucket name.
122
130
  * @param {string} dateFormat Date format string.
131
+ * @param {string} projectId Optional project ID.
123
132
  */
124
- exports.implode = async (date, output, bucket, dateFormat = "YYYY-MM-DD") => {
133
+ exports.implode = async (date, output, bucket, dateFormat = "YYYY-MM-DD", projectId = null) => {
125
134
  const folder = format(parse(date, dateFormat), dateFormat);
126
135
  const destination = path.join(output, ".tmp");
127
136
  const temporary = path.join(destination, folder);
128
- await downloadFolder(folder, bucket, destination).then(() => log.info(`Downloaded "gs://${bucket}/${folder}" to "${destination}".`)).catch(log.error);
137
+ await downloadFolder(folder, bucket, destination, projectId).then(() => log.info(`Downloaded "gs://${bucket}/${folder}" to "${destination}".`)).catch(log.error);
129
138
  const files = groupJson(fs.readdirSync(temporary));
130
139
  Object.keys(files).forEach(table => {
131
140
  const json = [];
@@ -155,10 +164,11 @@ exports.implode = async (date, output, bucket, dateFormat = "YYYY-MM-DD") => {
155
164
  *
156
165
  * @param {string} bucket Bucket name.
157
166
  * @param {number} days Number of days to keep.
167
+ * @param {string} projectId Optional project ID.
158
168
  * @returns {boolean} A value indicating whehter the job has been done.
159
169
  */
160
- exports.erase = async (bucket, days = process.env.BACKUP_KEEP_DAYS || 3) => {
161
- const b = getBucket(bucket);
170
+ exports.erase = async (bucket, days = process.env.BACKUP_KEEP_DAYS || 3, projectId = null) => {
171
+ const b = getBucket(bucket, projectId);
162
172
  const [files] = await b.getFiles();
163
173
  const result = {};
164
174
  files.forEach(({
@@ -16,7 +16,7 @@ const {
16
16
  getFilenameVersion
17
17
  } = require("./file");
18
18
  const {
19
- partsToDate
19
+ partsToDateUTC
20
20
  } = require("./date");
21
21
 
22
22
  /**
@@ -30,7 +30,7 @@ exports.splitName = name => {
30
30
  const split = name.split(/^(\d{4})\-(\d{2})\-(\d{2})\-(([\w_-]+)(?:\_[0-9]+)|([\w_-]+))(?:\.(\w+))$/gis);
31
31
  if (split.length >= 8) {
32
32
  return {
33
- date: partsToDate(split),
33
+ date: partsToDateUTC(split),
34
34
  name: typeof split[5] !== "undefined" ? split[5] : split[6],
35
35
  fullname: typeof split[4] !== "undefined" ? split[4] : name,
36
36
  extension: split[7],
@@ -41,7 +41,7 @@ exports.scriptBackup = backup;
41
41
  * @param {string} dateFormat Date format string.
42
42
  * @returns {boolean} A value indicating whehter the job has been done.
43
43
  */
44
- exports.scriptBackupErase = async (id, bucket, projectId, days, dateFormat = "YYYY-MM-DD") => await backup(id, bucket, projectId, dateFormat).then(async () => await erase(bucket, 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, projectId));
45
45
 
46
46
  /**
47
47
  * Rollback data using backup.
@@ -28,14 +28,15 @@ const {
28
28
  *
29
29
  * @param {string} bucket Bucket name.
30
30
  * @param {string} date Date to compare against.
31
+ * @param {string} projectId Google Cloud project ID.
31
32
  * @returns {Array} Array of files that are in the file but not in the bucket.
32
33
  */
33
- exports.scriptComparison = async (bucket, date = null) => {
34
+ exports.scriptComparison = async (bucket, date = null, projectId = null) => {
34
35
  if (bucket) {
35
36
  const day = date ? stringToDateUTC(date) : new Date();
36
37
  const filename = mergeFilename(day, "filelist", "txt");
37
- const content = await txtContentToArray(filename, bucket);
38
- const files = await listFiles(bucket);
38
+ const content = await txtContentToArray(filename, bucket, projectId);
39
+ const files = await listFiles(bucket, null, projectId);
39
40
  return content.filter(file => !files.includes(file));
40
41
  }
41
42
  };
@@ -24,6 +24,7 @@ const {
24
24
  * @param {string} output Path to save files.
25
25
  * @param {string} bucket Bucket name.
26
26
  * @param {string} dateFormat Date format string.
27
+ * @param {string} projectId Optional project ID.
27
28
  */
28
29
  exports.scriptImplode = implode;
29
30
 
@@ -32,6 +33,7 @@ exports.scriptImplode = implode;
32
33
  *
33
34
  * @param {string} bucket Bucket name.
34
35
  * @param {number} days Number of days to keep.
36
+ * @param {string} projectId Optional project ID.
35
37
  * @returns {boolean} A value indicating whehter the job has been done.
36
38
  */
37
39
  exports.scriptErase = erase;
@@ -29,11 +29,11 @@ const {
29
29
  *
30
30
  * @param {string} bucket Bucket name.
31
31
  * @param {string} folder Path of the local folder to scan for schemas.
32
- * @returns {void}
32
+ * @param {string} projectId Optional project ID.
33
33
  */
34
- exports.scriptUnlistened = async (bucket, folder) => {
34
+ exports.scriptUnlistened = async (bucket, folder, projectId = null) => {
35
35
  if (bucket) {
36
- const storage = getStorage();
36
+ const storage = getStorage(projectId);
37
37
  const [files] = await getFiles(storage, bucket);
38
38
  if (files) {
39
39
  files.forEach(async file => {
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.2",
4
+ "version": "1.15.4",
5
5
  "description": "Helpers for business intelligence for Google Cloud.",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {