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

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
  }
@@ -120,12 +120,13 @@ exports.getSafeFilenameFromBucket = ({
120
120
  * @param {string} output Path to save files.
121
121
  * @param {string} bucket Bucket name.
122
122
  * @param {string} dateFormat Date format string.
123
+ * @param {string} projectId Optional project ID.
123
124
  */
124
- exports.implode = async (date, output, bucket, dateFormat = "YYYY-MM-DD") => {
125
+ exports.implode = async (date, output, bucket, dateFormat = "YYYY-MM-DD", projectId = null) => {
125
126
  const folder = format(parse(date, dateFormat), dateFormat);
126
127
  const destination = path.join(output, ".tmp");
127
128
  const temporary = path.join(destination, folder);
128
- await downloadFolder(folder, bucket, destination).then(() => log.info(`Downloaded "gs://${bucket}/${folder}" to "${destination}".`)).catch(log.error);
129
+ await downloadFolder(folder, bucket, destination, projectId).then(() => log.info(`Downloaded "gs://${bucket}/${folder}" to "${destination}".`)).catch(log.error);
129
130
  const files = groupJson(fs.readdirSync(temporary));
130
131
  Object.keys(files).forEach(table => {
131
132
  const json = [];
@@ -155,10 +156,11 @@ exports.implode = async (date, output, bucket, dateFormat = "YYYY-MM-DD") => {
155
156
  *
156
157
  * @param {string} bucket Bucket name.
157
158
  * @param {number} days Number of days to keep.
159
+ * @param {string} projectId Optional project ID.
158
160
  * @returns {boolean} A value indicating whehter the job has been done.
159
161
  */
160
- exports.erase = async (bucket, days = process.env.BACKUP_KEEP_DAYS || 3) => {
161
- const b = getBucket(bucket);
162
+ exports.erase = async (bucket, days = process.env.BACKUP_KEEP_DAYS || 3, projectId = null) => {
163
+ const b = getBucket(bucket, projectId);
162
164
  const [files] = await b.getFiles();
163
165
  const result = {};
164
166
  files.forEach(({
@@ -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.3",
5
5
  "description": "Helpers for business intelligence for Google Cloud.",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {