@8ms/helpers 1.2.17 → 1.3.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.
@@ -1,10 +1,11 @@
1
1
  type IsTableExists = {
2
2
  datasetId: string;
3
+ projectId?: string;
3
4
  tableId: string;
4
5
  };
5
6
  /**
6
7
  * Check to see whether a given BigQuery table exists.
7
8
  * https://cloud.google.com/bigquery/docs/samples/bigquery-table-exists
8
9
  */
9
- declare const isTableExists: ({ datasetId, tableId }: IsTableExists) => Promise<boolean>;
10
+ declare const isTableExists: ({ datasetId, projectId, tableId }: IsTableExists) => Promise<boolean>;
10
11
  export default isTableExists;
@@ -1,12 +1,17 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const getClient_1 = __importDefault(require("./getClient"));
3
7
  /**
4
8
  * Check to see whether a given BigQuery table exists.
5
9
  * https://cloud.google.com/bigquery/docs/samples/bigquery-table-exists
6
10
  */
7
- const isTableExists = async ({ datasetId, tableId }) => {
11
+ const isTableExists = async ({ datasetId, projectId, tableId }) => {
8
12
  let response;
9
- const dataset = global.googleBigQueryClient.dataset(datasetId);
13
+ let client = (0, getClient_1.default)({ projectId });
14
+ const dataset = client.dataset(datasetId);
10
15
  try {
11
16
  await dataset.table(tableId)
12
17
  .get();
@@ -1,5 +1,6 @@
1
1
  type CreateTable = {
2
2
  datasetId: string;
3
+ projectId?: string;
3
4
  options: object;
4
5
  tableId: string;
5
6
  };
@@ -7,5 +8,5 @@ type CreateTable = {
7
8
  * Create a Table if it doesn't already exist.
8
9
  * Returns table instance.
9
10
  */
10
- declare const createTable: ({ datasetId, options, tableId }: CreateTable) => Promise<void>;
11
+ declare const createTable: ({ datasetId, options, projectId, tableId }: CreateTable) => Promise<void>;
11
12
  export default createTable;
@@ -4,17 +4,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const IsTableExists_1 = __importDefault(require("./IsTableExists"));
7
+ const getClient_1 = __importDefault(require("./getClient"));
7
8
  /**
8
9
  * Create a Table if it doesn't already exist.
9
10
  * Returns table instance.
10
11
  */
11
- const createTable = async ({ datasetId, options, tableId }) => {
12
+ const createTable = async ({ datasetId, options, projectId, tableId }) => {
12
13
  const tableExists = await (0, IsTableExists_1.default)({
13
14
  datasetId,
15
+ projectId,
14
16
  tableId
15
17
  });
16
18
  if (!tableExists) {
17
- await global.googleBigQueryClient.dataset(datasetId)
19
+ let client = (0, getClient_1.default)({ projectId });
20
+ await client
21
+ .dataset(datasetId)
18
22
  .createTable(tableId, options);
19
23
  }
20
24
  };
@@ -0,0 +1,8 @@
1
+ type GetClient = {
2
+ projectId?: string;
3
+ };
4
+ /**
5
+ * Create a new client if the project id differs from the global.
6
+ */
7
+ declare const getClient: ({ projectId }: GetClient) => any;
8
+ export default getClient;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Create a new client if the project id differs from the global.
5
+ */
6
+ const getClient = ({ projectId }) => {
7
+ let client = global.googleBigQueryClient;
8
+ if (projectId !== global.googleBigQueryDefaultProjectId) {
9
+ const { BigQuery } = require('@google-cloud/bigquery');
10
+ client = new BigQuery({
11
+ credentials: global.googleBigQueryDefault.credentials,
12
+ projectId: projectId,
13
+ });
14
+ }
15
+ return client;
16
+ };
17
+ exports.default = getClient;
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const getConfig_1 = __importDefault(require("../getConfig"));
7
+ global.googleBigQueryDefaultProjectId = null;
7
8
  global.googleBigQueryClient = null;
8
9
  /**
9
10
  * Initialise the Big Query client.
@@ -13,9 +14,11 @@ const initClient = ({ parameter, projectId }) => {
13
14
  if (!global.googleBigQueryClient) {
14
15
  const { BigQuery } = require('@google-cloud/bigquery');
15
16
  const formattedConfig = (0, getConfig_1.default)({ parameter });
17
+ global.googleBigQueryDefault.credentials = formattedConfig;
18
+ global.googleBigQueryDefault.projectId = projectId;
16
19
  global.googleBigQueryClient = new BigQuery({
17
- credentials: formattedConfig,
18
- projectId: projectId,
20
+ credentials: global.googleBigQueryDefault.credentials,
21
+ projectId: global.googleBigQueryDefault.projectId,
19
22
  });
20
23
  }
21
24
  };
@@ -5,6 +5,7 @@ type LoadData = {
5
5
  metadata: object;
6
6
  };
7
7
  table: {
8
+ projectId?: string;
8
9
  datasetId: string;
9
10
  tableId: string;
10
11
  };
@@ -4,16 +4,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const IsTableExists_1 = __importDefault(require("./IsTableExists"));
7
+ const getClient_1 = __importDefault(require("./getClient"));
7
8
  /**
8
9
  * Load data into a given table.
9
10
  */
10
11
  const loadData = async ({ file, table }) => {
11
12
  const tableExists = await (0, IsTableExists_1.default)({
12
13
  datasetId: table.datasetId,
14
+ projectId: table.projectId,
13
15
  tableId: table.tableId,
14
16
  });
15
17
  if (tableExists) {
16
- await global.googleBigQueryClient.dataset(table.datasetId)
18
+ let client = (0, getClient_1.default)({
19
+ projectId: table.projectId
20
+ });
21
+ await client
22
+ .dataset(table.datasetId)
17
23
  .table(table.tableId)
18
24
  .load(global.googleStorageClient.bucket(file.bucket)
19
25
  .file(file.key), file.metadata);
@@ -1,8 +1,9 @@
1
1
  type Query = {
2
2
  input: string;
3
+ projectId?: string;
3
4
  };
4
5
  /**
5
6
  * Build the query and return the data.
6
7
  */
7
- declare const query: ({ input }: Query) => Promise<any>;
8
+ declare const query: ({ input, projectId }: Query) => Promise<any>;
8
9
  export default query;
@@ -1,10 +1,15 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const getClient_1 = __importDefault(require("./getClient"));
3
7
  /**
4
8
  * Build the query and return the data.
5
9
  */
6
- const query = async ({ input }) => {
7
- const apiResponse = await global.googleBigQueryClient.query(input);
10
+ const query = async ({ input, projectId }) => {
11
+ let client = (0, getClient_1.default)({ projectId });
12
+ const apiResponse = await client.query(input);
8
13
  return apiResponse;
9
14
  };
10
15
  exports.default = query;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "1.2.17",
4
+ "version": "1.3.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"