@cumulus/db 9.8.0-alpha.0 → 10.0.0-beta.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/LICENSE ADDED
@@ -0,0 +1,60 @@
1
+ Copyright © 2020 United States Government as represented by the Administrator of the National Aeronautics and Space Administration. All Rights Reserved.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
4
+ You may obtain a copy of the License at
5
+
6
+ http://www.apache.org/licenses/LICE NSE-2.0
7
+
8
+ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
10
+
11
+ ---
12
+
13
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
14
+
15
+ 1. Definitions.
16
+
17
+ "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
18
+
19
+ "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
20
+
21
+ "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
24
+
25
+ "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
26
+
27
+ "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
28
+
29
+ "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
30
+
31
+ "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
32
+
33
+ "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
34
+
35
+ "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
36
+
37
+ 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
38
+
39
+ 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
40
+
41
+ 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
42
+
43
+ You must give any other recipients of the Work or Derivative Works a copy of this License; and
44
+ You must cause any modified files to carry prominent notices stating that You changed the files; and
45
+ You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
46
+ If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
47
+
48
+ You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
49
+
50
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
51
+
52
+ 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
53
+
54
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
55
+
56
+ 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
57
+
58
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
59
+
60
+ END OF TERMS AND CONDITIONS
package/dist/config.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" />
1
2
  import AWS from 'aws-sdk';
2
3
  import { Knex } from 'knex';
3
4
  export declare const localStackConnectionEnv: {
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" />
1
2
  import AWS from 'aws-sdk';
2
3
  import { Knex } from 'knex';
3
4
  import Logger from '@cumulus/logger';
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ export { createTestDatabase, deleteTestDatabase, destroyLocalTestDb, fakeAsyncOp
3
3
  export { getKnexClient } from './connection';
4
4
  export { getKnexConfig, localStackConnectionEnv } from './config';
5
5
  export { createRejectableTransaction } from './database';
6
- export { tableNames } from './tables';
6
+ export { TableNames } from './tables';
7
7
  export declare const migrationDir: string;
8
8
  export { validateProviderHost, nullifyUndefinedProviderValues, } from './provider';
9
9
  export { PostgresAsyncOperation, PostgresAsyncOperationRecord, } from './types/async_operation';
@@ -15,14 +15,14 @@ export { PostgresGranule, PostgresGranuleRecord, } from './types/granule';
15
15
  export { PostgresPdr, PostgresPdrRecord, } from './types/pdr';
16
16
  export { PostgresFile, PostgresFileRecord, } from './types/file';
17
17
  export { translateApiAsyncOperationToPostgresAsyncOperation } from './translate/async_operations';
18
- export { translateApiFiletoPostgresFile, } from './translate/file';
18
+ export { translateApiFiletoPostgresFile, translatePostgresFileToApiFile, } from './translate/file';
19
19
  export { translateApiCollectionToPostgresCollection } from './translate/collections';
20
20
  export { translateApiProviderToPostgresProvider, } from './translate/providers';
21
21
  export { translateApiRuleToPostgresRule } from './translate/rules';
22
22
  export { translateApiExecutionToPostgresExecution, translatePostgresExecutionToApiExecution, } from './translate/executions';
23
- export { translateApiGranuleToPostgresGranule } from './translate/granules';
23
+ export { translateApiGranuleToPostgresGranule, translatePostgresGranuleToApiGranule, } from './translate/granules';
24
24
  export { translateApiPdrToPostgresPdr } from './translate/pdrs';
25
- export { executionArnsFromGranuleIdsAndWorkflowNames, newestExecutionArnFromGranuleIdWorkflowName, getWorkflowNameIntersectFromGranuleIds, } from './lib/execution';
25
+ export { executionArnsFromGranuleIdsAndWorkflowNames, newestExecutionArnFromGranuleIdWorkflowName, getWorkflowNameIntersectFromGranuleIds, getApiExecutionCumulusIds, getApiGranuleExecutionCumulusIdsByExecution, getExecutionArnsByGranuleCumulusId, } from './lib/execution';
26
26
  export { getApiGranuleCumulusIds, getApiGranuleExecutionCumulusIds, upsertGranuleWithExecutionJoinRecord, } from './lib/granule';
27
27
  export { AsyncOperationPgModel } from './models/async_operation';
28
28
  export { BasePgModel } from './models/base';
package/dist/index.js CHANGED
@@ -19,7 +19,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
19
19
  return result;
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.RulePgModel = exports.ProviderPgModel = exports.PdrPgModel = exports.GranulesExecutionsPgModel = exports.GranulePgModel = exports.FilePgModel = exports.ExecutionPgModel = exports.CollectionPgModel = exports.BasePgModel = exports.AsyncOperationPgModel = exports.upsertGranuleWithExecutionJoinRecord = exports.getApiGranuleExecutionCumulusIds = exports.getApiGranuleCumulusIds = exports.getWorkflowNameIntersectFromGranuleIds = exports.newestExecutionArnFromGranuleIdWorkflowName = exports.executionArnsFromGranuleIdsAndWorkflowNames = exports.translateApiPdrToPostgresPdr = exports.translateApiGranuleToPostgresGranule = exports.translatePostgresExecutionToApiExecution = exports.translateApiExecutionToPostgresExecution = exports.translateApiRuleToPostgresRule = exports.translateApiProviderToPostgresProvider = exports.translateApiCollectionToPostgresCollection = exports.translateApiFiletoPostgresFile = exports.translateApiAsyncOperationToPostgresAsyncOperation = exports.nullifyUndefinedProviderValues = exports.validateProviderHost = exports.migrationDir = exports.tableNames = exports.createRejectableTransaction = exports.localStackConnectionEnv = exports.getKnexConfig = exports.getKnexClient = exports.generateLocalTestDb = exports.fakeRuleRecordFactory = exports.fakeProviderRecordFactory = exports.fakePdrRecordFactory = exports.fakeGranuleRecordFactory = exports.fakeFileRecordFactory = exports.fakeExecutionRecordFactory = exports.fakeCollectionRecordFactory = exports.fakeAsyncOperationRecordFactory = exports.destroyLocalTestDb = exports.deleteTestDatabase = exports.createTestDatabase = exports.Knex = void 0;
22
+ exports.ProviderPgModel = exports.PdrPgModel = exports.GranulesExecutionsPgModel = exports.GranulePgModel = exports.FilePgModel = exports.ExecutionPgModel = exports.CollectionPgModel = exports.BasePgModel = exports.AsyncOperationPgModel = exports.upsertGranuleWithExecutionJoinRecord = exports.getApiGranuleExecutionCumulusIds = exports.getApiGranuleCumulusIds = exports.getExecutionArnsByGranuleCumulusId = exports.getApiGranuleExecutionCumulusIdsByExecution = exports.getApiExecutionCumulusIds = exports.getWorkflowNameIntersectFromGranuleIds = exports.newestExecutionArnFromGranuleIdWorkflowName = exports.executionArnsFromGranuleIdsAndWorkflowNames = exports.translateApiPdrToPostgresPdr = exports.translatePostgresGranuleToApiGranule = exports.translateApiGranuleToPostgresGranule = exports.translatePostgresExecutionToApiExecution = exports.translateApiExecutionToPostgresExecution = exports.translateApiRuleToPostgresRule = exports.translateApiProviderToPostgresProvider = exports.translateApiCollectionToPostgresCollection = exports.translatePostgresFileToApiFile = exports.translateApiFiletoPostgresFile = exports.translateApiAsyncOperationToPostgresAsyncOperation = exports.nullifyUndefinedProviderValues = exports.validateProviderHost = exports.migrationDir = exports.TableNames = exports.createRejectableTransaction = exports.localStackConnectionEnv = exports.getKnexConfig = exports.getKnexClient = exports.generateLocalTestDb = exports.fakeRuleRecordFactory = exports.fakeProviderRecordFactory = exports.fakePdrRecordFactory = exports.fakeGranuleRecordFactory = exports.fakeFileRecordFactory = exports.fakeExecutionRecordFactory = exports.fakeCollectionRecordFactory = exports.fakeAsyncOperationRecordFactory = exports.destroyLocalTestDb = exports.deleteTestDatabase = exports.createTestDatabase = exports.Knex = void 0;
23
+ exports.RulePgModel = void 0;
23
24
  const path = __importStar(require("path"));
24
25
  var knex_1 = require("knex");
25
26
  Object.defineProperty(exports, "Knex", { enumerable: true, get: function () { return knex_1.Knex; } });
@@ -44,7 +45,7 @@ Object.defineProperty(exports, "localStackConnectionEnv", { enumerable: true, ge
44
45
  var database_1 = require("./database");
45
46
  Object.defineProperty(exports, "createRejectableTransaction", { enumerable: true, get: function () { return database_1.createRejectableTransaction; } });
46
47
  var tables_1 = require("./tables");
47
- Object.defineProperty(exports, "tableNames", { enumerable: true, get: function () { return tables_1.tableNames; } });
48
+ Object.defineProperty(exports, "TableNames", { enumerable: true, get: function () { return tables_1.TableNames; } });
48
49
  exports.migrationDir = path.join(__dirname, 'migrations');
49
50
  var provider_1 = require("./provider");
50
51
  Object.defineProperty(exports, "validateProviderHost", { enumerable: true, get: function () { return provider_1.validateProviderHost; } });
@@ -53,6 +54,7 @@ var async_operations_1 = require("./translate/async_operations");
53
54
  Object.defineProperty(exports, "translateApiAsyncOperationToPostgresAsyncOperation", { enumerable: true, get: function () { return async_operations_1.translateApiAsyncOperationToPostgresAsyncOperation; } });
54
55
  var file_1 = require("./translate/file");
55
56
  Object.defineProperty(exports, "translateApiFiletoPostgresFile", { enumerable: true, get: function () { return file_1.translateApiFiletoPostgresFile; } });
57
+ Object.defineProperty(exports, "translatePostgresFileToApiFile", { enumerable: true, get: function () { return file_1.translatePostgresFileToApiFile; } });
56
58
  var collections_1 = require("./translate/collections");
57
59
  Object.defineProperty(exports, "translateApiCollectionToPostgresCollection", { enumerable: true, get: function () { return collections_1.translateApiCollectionToPostgresCollection; } });
58
60
  var providers_1 = require("./translate/providers");
@@ -64,12 +66,16 @@ Object.defineProperty(exports, "translateApiExecutionToPostgresExecution", { enu
64
66
  Object.defineProperty(exports, "translatePostgresExecutionToApiExecution", { enumerable: true, get: function () { return executions_1.translatePostgresExecutionToApiExecution; } });
65
67
  var granules_1 = require("./translate/granules");
66
68
  Object.defineProperty(exports, "translateApiGranuleToPostgresGranule", { enumerable: true, get: function () { return granules_1.translateApiGranuleToPostgresGranule; } });
69
+ Object.defineProperty(exports, "translatePostgresGranuleToApiGranule", { enumerable: true, get: function () { return granules_1.translatePostgresGranuleToApiGranule; } });
67
70
  var pdrs_1 = require("./translate/pdrs");
68
71
  Object.defineProperty(exports, "translateApiPdrToPostgresPdr", { enumerable: true, get: function () { return pdrs_1.translateApiPdrToPostgresPdr; } });
69
72
  var execution_1 = require("./lib/execution");
70
73
  Object.defineProperty(exports, "executionArnsFromGranuleIdsAndWorkflowNames", { enumerable: true, get: function () { return execution_1.executionArnsFromGranuleIdsAndWorkflowNames; } });
71
74
  Object.defineProperty(exports, "newestExecutionArnFromGranuleIdWorkflowName", { enumerable: true, get: function () { return execution_1.newestExecutionArnFromGranuleIdWorkflowName; } });
72
75
  Object.defineProperty(exports, "getWorkflowNameIntersectFromGranuleIds", { enumerable: true, get: function () { return execution_1.getWorkflowNameIntersectFromGranuleIds; } });
76
+ Object.defineProperty(exports, "getApiExecutionCumulusIds", { enumerable: true, get: function () { return execution_1.getApiExecutionCumulusIds; } });
77
+ Object.defineProperty(exports, "getApiGranuleExecutionCumulusIdsByExecution", { enumerable: true, get: function () { return execution_1.getApiGranuleExecutionCumulusIdsByExecution; } });
78
+ Object.defineProperty(exports, "getExecutionArnsByGranuleCumulusId", { enumerable: true, get: function () { return execution_1.getExecutionArnsByGranuleCumulusId; } });
73
79
  var granule_1 = require("./lib/granule");
74
80
  Object.defineProperty(exports, "getApiGranuleCumulusIds", { enumerable: true, get: function () { return granule_1.getApiGranuleCumulusIds; } });
75
81
  Object.defineProperty(exports, "getApiGranuleExecutionCumulusIds", { enumerable: true, get: function () { return granule_1.getApiGranuleExecutionCumulusIds; } });
@@ -1,7 +1,20 @@
1
1
  import { Knex } from 'knex';
2
- export interface arnRecord {
2
+ import { ExecutionPgModel } from '../models/execution';
3
+ import { GranulesExecutionsPgModel } from '../models/granules-executions';
4
+ export interface ArnRecord {
3
5
  arn: string;
4
6
  }
7
+ /**
8
+ * Returns a list of executionArns sorted by most recent first, for an input
9
+ * Granule Cumulus ID.
10
+ *
11
+ * @param {Knex | Knex.Transaction} knexOrTransaction
12
+ * Knex client for reading from RDS database
13
+ * @param {number} granuleCumulusId - The primary ID for a Granule
14
+ * @param {number} limit - limit to number of executions to query
15
+ * @returns {Promise<ArnRecord[]>} - Array of arn objects with the most recent first.
16
+ */
17
+ export declare const getExecutionArnsByGranuleCumulusId: (knexOrTransaction: Knex | Knex.Transaction, granuleCumulusId: Number, limit?: number | undefined) => Promise<ArnRecord[]>;
5
18
  /**
6
19
  * Returns a list of executionArns sorted by most recent first, for an input
7
20
  * list of granuleIds and workflowNames.
@@ -9,9 +22,9 @@ export interface arnRecord {
9
22
  * @param {Knex} knex - DB Client
10
23
  * @param {string[]} granuleIds - Array of granuleIds
11
24
  * @param {string[]} workflowNames - Array of workflow names
12
- * @returns {Promise<arnRecord[]>} - Array of arn objects with the most recent first.
25
+ * @returns {Promise<ArnRecord[]>} - Array of arn objects with the most recent first.
13
26
  */
14
- export declare const executionArnsFromGranuleIdsAndWorkflowNames: (knex: Knex, granuleIds: string[], workflowNames: string[]) => Promise<arnRecord[]>;
27
+ export declare const executionArnsFromGranuleIdsAndWorkflowNames: (knex: Knex, granuleIds: string[], workflowNames: string[]) => Promise<ArnRecord[]>;
15
28
  /**
16
29
  * convenience function to return a single executionArn string for a intput
17
30
  * granuleId and workflowName.
@@ -33,4 +46,29 @@ export declare const newestExecutionArnFromGranuleIdWorkflowName: (granuleId: st
33
46
  * @throws {RecordNotFound}
34
47
  */
35
48
  export declare const getWorkflowNameIntersectFromGranuleIds: (knexOrTransaction: Knex | Knex.Transaction, granuleCumulusIds: Array<number> | number) => Promise<Array<string>>;
49
+ /**
50
+ * Get cumulus IDs for list of executions
51
+ *
52
+ * @param {Knex | Knex.Transaction} knexOrTransaction -
53
+ * DB client or transaction
54
+ * @param {Array<Object>} executions - array of executions
55
+ * @param {Object} [executionPgModel] - Execution PG model class instance
56
+ * @returns {Promise<number[]>}
57
+ */
58
+ export declare const getApiExecutionCumulusIds: (knexOrTransaction: Knex | Knex.Transaction, executions: Array<{
59
+ arn: string;
60
+ }>, executionPgModel?: ExecutionPgModel) => Promise<number[]>;
61
+ /**
62
+ * Get cumulus IDs for all granules associated to a set of executions
63
+ *
64
+ * @param {Knex | Knex.Transaction} knexOrTransaction -
65
+ * DB client or transaction
66
+ * @param {Array<Object>} executions - array of executions
67
+ * @param {Object} [executionsPgModel]
68
+ * Executions PG model class instance
69
+ * @returns {Promise<number[]>}
70
+ */
71
+ export declare const getApiGranuleExecutionCumulusIdsByExecution: (knexOrTransaction: Knex | Knex.Transaction, executions: Array<{
72
+ arn: string;
73
+ }>, executionPgModel?: ExecutionPgModel, granulesExecutionsPgModel?: GranulesExecutionsPgModel) => Promise<Array<number>>;
36
74
  //# sourceMappingURL=execution.d.ts.map
@@ -1,11 +1,36 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getWorkflowNameIntersectFromGranuleIds = exports.newestExecutionArnFromGranuleIdWorkflowName = exports.executionArnsFromGranuleIdsAndWorkflowNames = void 0;
3
+ exports.getApiGranuleExecutionCumulusIdsByExecution = exports.getApiExecutionCumulusIds = exports.getWorkflowNameIntersectFromGranuleIds = exports.newestExecutionArnFromGranuleIdWorkflowName = exports.executionArnsFromGranuleIdsAndWorkflowNames = exports.getExecutionArnsByGranuleCumulusId = void 0;
4
4
  const errors_1 = require("@cumulus/errors");
5
+ const execution_1 = require("../models/execution");
6
+ const granules_executions_1 = require("../models/granules-executions");
5
7
  const tables_1 = require("../tables");
6
8
  const Logger = require('@cumulus/logger');
7
9
  const { getKnexClient } = require('../connection');
8
10
  const log = new Logger({ sender: '@cumulus/db/lib/execution' });
11
+ /**
12
+ * Returns a list of executionArns sorted by most recent first, for an input
13
+ * Granule Cumulus ID.
14
+ *
15
+ * @param {Knex | Knex.Transaction} knexOrTransaction
16
+ * Knex client for reading from RDS database
17
+ * @param {number} granuleCumulusId - The primary ID for a Granule
18
+ * @param {number} limit - limit to number of executions to query
19
+ * @returns {Promise<ArnRecord[]>} - Array of arn objects with the most recent first.
20
+ */
21
+ const getExecutionArnsByGranuleCumulusId = async (knexOrTransaction, granuleCumulusId, limit) => {
22
+ const knexQuery = knexOrTransaction(tables_1.TableNames.executions)
23
+ .select(`${tables_1.TableNames.executions}.arn`)
24
+ .where(`${tables_1.TableNames.granules}.cumulus_id`, granuleCumulusId)
25
+ .join(tables_1.TableNames.granulesExecutions, `${tables_1.TableNames.executions}.cumulus_id`, `${tables_1.TableNames.granulesExecutions}.execution_cumulus_id`)
26
+ .join(tables_1.TableNames.granules, `${tables_1.TableNames.granules}.cumulus_id`, `${tables_1.TableNames.granulesExecutions}.granule_cumulus_id`)
27
+ .orderBy(`${tables_1.TableNames.executions}.timestamp`, 'desc');
28
+ if (limit) {
29
+ knexQuery.limit(limit);
30
+ }
31
+ return await knexQuery;
32
+ };
33
+ exports.getExecutionArnsByGranuleCumulusId = getExecutionArnsByGranuleCumulusId;
9
34
  /**
10
35
  * Returns a list of executionArns sorted by most recent first, for an input
11
36
  * list of granuleIds and workflowNames.
@@ -13,16 +38,16 @@ const log = new Logger({ sender: '@cumulus/db/lib/execution' });
13
38
  * @param {Knex} knex - DB Client
14
39
  * @param {string[]} granuleIds - Array of granuleIds
15
40
  * @param {string[]} workflowNames - Array of workflow names
16
- * @returns {Promise<arnRecord[]>} - Array of arn objects with the most recent first.
41
+ * @returns {Promise<ArnRecord[]>} - Array of arn objects with the most recent first.
17
42
  */
18
43
  const executionArnsFromGranuleIdsAndWorkflowNames = (knex, granuleIds, workflowNames) => knex
19
- .select(`${tables_1.tableNames.executions}.arn`)
20
- .from(tables_1.tableNames.executions)
21
- .join(tables_1.tableNames.granulesExecutions, `${tables_1.tableNames.executions}.cumulus_id`, `${tables_1.tableNames.granulesExecutions}.execution_cumulus_id`)
22
- .join(tables_1.tableNames.granules, `${tables_1.tableNames.granules}.cumulus_id`, `${tables_1.tableNames.granulesExecutions}.granule_cumulus_id`)
23
- .whereIn(`${tables_1.tableNames.granules}.granule_id`, granuleIds)
24
- .whereIn(`${tables_1.tableNames.executions}.workflow_name`, workflowNames)
25
- .orderBy(`${tables_1.tableNames.executions}.timestamp`, 'desc');
44
+ .select(`${tables_1.TableNames.executions}.arn`)
45
+ .from(tables_1.TableNames.executions)
46
+ .join(tables_1.TableNames.granulesExecutions, `${tables_1.TableNames.executions}.cumulus_id`, `${tables_1.TableNames.granulesExecutions}.execution_cumulus_id`)
47
+ .join(tables_1.TableNames.granules, `${tables_1.TableNames.granules}.cumulus_id`, `${tables_1.TableNames.granulesExecutions}.granule_cumulus_id`)
48
+ .whereIn(`${tables_1.TableNames.granules}.granule_id`, granuleIds)
49
+ .whereIn(`${tables_1.TableNames.executions}.workflow_name`, workflowNames)
50
+ .orderBy(`${tables_1.TableNames.executions}.timestamp`, 'desc');
26
51
  exports.executionArnsFromGranuleIdsAndWorkflowNames = executionArnsFromGranuleIdsAndWorkflowNames;
27
52
  /**
28
53
  * convenience function to return a single executionArn string for a intput
@@ -61,7 +86,7 @@ exports.newestExecutionArnFromGranuleIdWorkflowName = newestExecutionArnFromGran
61
86
  const getWorkflowNameIntersectFromGranuleIds = async (knexOrTransaction, granuleCumulusIds) => {
62
87
  const granuleCumulusIdsArray = [granuleCumulusIds].flat();
63
88
  const numberOfGranules = granuleCumulusIdsArray.length;
64
- const { executions: executionsTable, granulesExecutions: granulesExecutionsTable } = tables_1.tableNames;
89
+ const { executions: executionsTable, granulesExecutions: granulesExecutionsTable } = tables_1.TableNames;
65
90
  const aggregatedWorkflowCounts = await knexOrTransaction(executionsTable)
66
91
  .select('workflow_name')
67
92
  .countDistinct('granule_cumulus_id')
@@ -79,4 +104,37 @@ const getWorkflowNameIntersectFromGranuleIds = async (knexOrTransaction, granule
79
104
  .map((workflowCounts) => workflowCounts.workflow_name);
80
105
  };
81
106
  exports.getWorkflowNameIntersectFromGranuleIds = getWorkflowNameIntersectFromGranuleIds;
107
+ /**
108
+ * Get cumulus IDs for list of executions
109
+ *
110
+ * @param {Knex | Knex.Transaction} knexOrTransaction -
111
+ * DB client or transaction
112
+ * @param {Array<Object>} executions - array of executions
113
+ * @param {Object} [executionPgModel] - Execution PG model class instance
114
+ * @returns {Promise<number[]>}
115
+ */
116
+ const getApiExecutionCumulusIds = async (knexOrTransaction, executions, executionPgModel = new execution_1.ExecutionPgModel()) => {
117
+ const executionCumulusIds = await Promise.all(executions.map(async (execution) => await executionPgModel.getRecordCumulusId(knexOrTransaction, {
118
+ arn: execution.arn,
119
+ })));
120
+ return [...new Set(executionCumulusIds)];
121
+ };
122
+ exports.getApiExecutionCumulusIds = getApiExecutionCumulusIds;
123
+ /**
124
+ * Get cumulus IDs for all granules associated to a set of executions
125
+ *
126
+ * @param {Knex | Knex.Transaction} knexOrTransaction -
127
+ * DB client or transaction
128
+ * @param {Array<Object>} executions - array of executions
129
+ * @param {Object} [executionsPgModel]
130
+ * Executions PG model class instance
131
+ * @returns {Promise<number[]>}
132
+ */
133
+ const getApiGranuleExecutionCumulusIdsByExecution = async (knexOrTransaction, executions, executionPgModel = new execution_1.ExecutionPgModel(), granulesExecutionsPgModel = new granules_executions_1.GranulesExecutionsPgModel()) => {
134
+ const executionCumulusIds = await (0, exports.getApiExecutionCumulusIds)(knexOrTransaction, executions, executionPgModel);
135
+ const granuleCumulusIds = await granulesExecutionsPgModel
136
+ .searchByExecutionCumulusIds(knexOrTransaction, executionCumulusIds);
137
+ return granuleCumulusIds;
138
+ };
139
+ exports.getApiGranuleExecutionCumulusIdsByExecution = getApiGranuleExecutionCumulusIdsByExecution;
82
140
  //# sourceMappingURL=execution.js.map
@@ -7,7 +7,7 @@ const tables_1 = require("../tables");
7
7
  class AsyncOperationPgModel extends base_1.BasePgModel {
8
8
  constructor() {
9
9
  super({
10
- tableName: tables_1.tableNames.asyncOperations,
10
+ tableName: tables_1.TableNames.asyncOperations,
11
11
  });
12
12
  }
13
13
  async upsert(knexOrTrx, asyncOperation) {
@@ -1,11 +1,11 @@
1
1
  import { Knex } from 'knex';
2
- import { tableNames } from '../tables';
2
+ import { TableNames } from '../tables';
3
3
  declare class BasePgModel<ItemType, RecordType extends {
4
4
  cumulus_id: number;
5
5
  }> {
6
- readonly tableName: tableNames;
6
+ readonly tableName: TableNames;
7
7
  constructor({ tableName, }: {
8
- tableName: tableNames;
8
+ tableName: TableNames;
9
9
  });
10
10
  count(knexOrTransaction: Knex | Knex.Transaction, params: ([string, string, string] | [Partial<RecordType>])[]): Promise<{
11
11
  [k: string]: string | number;
@@ -6,7 +6,7 @@ const tables_1 = require("../tables");
6
6
  class CollectionPgModel extends base_1.BasePgModel {
7
7
  constructor() {
8
8
  super({
9
- tableName: tables_1.tableNames.collections,
9
+ tableName: tables_1.TableNames.collections,
10
10
  });
11
11
  }
12
12
  upsert(knexOrTransaction, collection) {
@@ -7,7 +7,7 @@ const sort_1 = require("../lib/sort");
7
7
  class ExecutionPgModel extends base_1.BasePgModel {
8
8
  constructor() {
9
9
  super({
10
- tableName: tables_1.tableNames.executions,
10
+ tableName: tables_1.TableNames.executions,
11
11
  });
12
12
  }
13
13
  async upsert(knexOrTrx, execution) {
@@ -6,7 +6,7 @@ declare class FilePgModel extends BasePgModel<PostgresFile, PostgresFileRecord>
6
6
  upsert(knexOrTrx: Knex | Knex.Transaction, file: PostgresFile): Knex.QueryBuilder<any, {
7
7
  _base: any;
8
8
  _hasSelection: true;
9
- _keys: "bucket" | "key";
9
+ _keys: "key" | "bucket";
10
10
  _aliases: {};
11
11
  _single: false;
12
12
  _intersectProps: {};
@@ -6,7 +6,7 @@ const tables_1 = require("../tables");
6
6
  class FilePgModel extends base_1.BasePgModel {
7
7
  constructor() {
8
8
  super({
9
- tableName: tables_1.tableNames.files,
9
+ tableName: tables_1.TableNames.files,
10
10
  });
11
11
  }
12
12
  upsert(knexOrTrx, file) {
@@ -36,6 +36,22 @@ export default class GranulePgModel extends BasePgModel<PostgresGranule, Postgre
36
36
  */
37
37
  get(knexOrTransaction: Knex | Knex.Transaction, params: PostgresGranuleUniqueColumns | RecordSelect): Promise<PostgresGranuleRecord>;
38
38
  upsert(knexOrTrx: Knex | Knex.Transaction, granule: PostgresGranule, executionCumulusId?: number, granulesExecutionsPgModel?: GranulesExecutionsPgModel): Promise<any[]>;
39
+ /**
40
+ * Get granules from the granule cumulus_id
41
+ *
42
+ * @param {Knex | Knex.Transaction} knexOrTrx -
43
+ * DB client or transaction
44
+ * @param {Array<number>} granuleCumulusIds -
45
+ * single granule cumulus_id or array of granule cumulus_ids
46
+ * @param {Object} [params] - Optional object with addition params for query
47
+ * @param {number} [params.limit] - number of records to be returned
48
+ * @param {number} [params.offset] - record offset
49
+ * @returns {Promise<Array<PostgresGranuleRecord>>} An array of granules
50
+ */
51
+ searchByCumulusIds(knexOrTrx: Knex | Knex.Transaction, granuleCumulusIds: Array<number> | number, params: {
52
+ limit: number;
53
+ offset: number;
54
+ }): Promise<Array<PostgresGranuleRecord>>;
39
55
  }
40
56
  export { GranulePgModel };
41
57
  //# sourceMappingURL=granule.d.ts.map
@@ -6,13 +6,14 @@ const tables_1 = require("../tables");
6
6
  const base_1 = require("./base");
7
7
  const granules_executions_1 = require("./granules-executions");
8
8
  const timestamp_1 = require("../lib/timestamp");
9
+ const sort_1 = require("../lib/sort");
9
10
  function isRecordSelect(param) {
10
11
  return param.cumulus_id !== undefined;
11
12
  }
12
13
  class GranulePgModel extends base_1.BasePgModel {
13
14
  constructor() {
14
15
  super({
15
- tableName: tables_1.tableNames.granules,
16
+ tableName: tables_1.TableNames.granules,
16
17
  });
17
18
  }
18
19
  /**
@@ -97,6 +98,39 @@ class GranulePgModel extends base_1.BasePgModel {
97
98
  .where(knexOrTrx.raw(`${this.tableName}.created_at <= to_timestamp(${(0, timestamp_1.translateDateToUTC)(granule.created_at)})`))
98
99
  .returning('cumulus_id');
99
100
  }
101
+ /**
102
+ * Get granules from the granule cumulus_id
103
+ *
104
+ * @param {Knex | Knex.Transaction} knexOrTrx -
105
+ * DB client or transaction
106
+ * @param {Array<number>} granuleCumulusIds -
107
+ * single granule cumulus_id or array of granule cumulus_ids
108
+ * @param {Object} [params] - Optional object with addition params for query
109
+ * @param {number} [params.limit] - number of records to be returned
110
+ * @param {number} [params.offset] - record offset
111
+ * @returns {Promise<Array<PostgresGranuleRecord>>} An array of granules
112
+ */
113
+ async searchByCumulusIds(knexOrTrx, granuleCumulusIds, params) {
114
+ const { limit, offset, ...sortQueries } = params || {};
115
+ const sortFields = (0, sort_1.getSortFields)(sortQueries);
116
+ const granuleCumulusIdsArray = [granuleCumulusIds].flat();
117
+ const granules = await knexOrTrx(this.tableName)
118
+ .whereIn('cumulus_id', granuleCumulusIdsArray)
119
+ .modify((queryBuilder) => {
120
+ if (limit)
121
+ queryBuilder.limit(limit);
122
+ if (offset)
123
+ queryBuilder.offset(offset);
124
+ if (sortFields.length >= 1) {
125
+ sortFields.forEach((sortObject) => {
126
+ const sortField = Object.keys(sortObject)[0];
127
+ const { order } = sortObject[sortField];
128
+ queryBuilder.orderBy(sortField, order);
129
+ });
130
+ }
131
+ });
132
+ return granules;
133
+ }
100
134
  }
101
135
  exports.default = GranulePgModel;
102
136
  exports.GranulePgModel = GranulePgModel;
@@ -1,8 +1,8 @@
1
1
  import { Knex } from 'knex';
2
- import { tableNames } from '../tables';
2
+ import { TableNames } from '../tables';
3
3
  import { PostgresGranuleExecution } from '../types/granule-execution';
4
4
  export default class GranulesExecutionsPgModel {
5
- readonly tableName: tableNames;
5
+ readonly tableName: TableNames;
6
6
  constructor();
7
7
  create(knexTransaction: Knex.Transaction, item: PostgresGranuleExecution): Promise<number[]>;
8
8
  exists(knexTransaction: Knex.Transaction, item: PostgresGranuleExecution): Promise<boolean>;
@@ -17,6 +17,16 @@ export default class GranulesExecutionsPgModel {
17
17
  * @returns {Promise<Array<number>>} An array of execution_cumulus_ids
18
18
  */
19
19
  searchByGranuleCumulusIds(knexOrTransaction: Knex | Knex.Transaction, granuleCumulusIds: Array<number> | number): Promise<Array<number>>;
20
+ /**
21
+ * Get granule_cumulus_id column values from the execution_cumulus_id
22
+ *
23
+ * @param {Knex | Knex.Transaction} knexOrTransaction -
24
+ * DB client or transaction
25
+ * @param {number | Array<number>} executionCumulusIds -
26
+ * single execution_cumulus_id or array of execution_cumulus_ids
27
+ * @returns {Promise<Array<number>>} An array of granule_cumulus_ids
28
+ */
29
+ searchByExecutionCumulusIds(knexOrTransaction: Knex | Knex.Transaction, executionCumulusIds: Array<number> | number): Promise<Array<number>>;
20
30
  delete(knexTransaction: Knex.Transaction, params: Partial<PostgresGranuleExecution>): Promise<number>;
21
31
  search(knexTransaction: Knex | Knex.Transaction, query: Partial<PostgresGranuleExecution>): Knex.QueryBuilder<PostgresGranuleExecution, {
22
32
  _base: PostgresGranuleExecution;
@@ -7,7 +7,7 @@ class GranulesExecutionsPgModel {
7
7
  // can't extend base class because type for this data doesn't contain
8
8
  // a cumulus_id property
9
9
  constructor() {
10
- this.tableName = tables_1.tableNames.granulesExecutions;
10
+ this.tableName = tables_1.TableNames.granulesExecutions;
11
11
  }
12
12
  async create(knexTransaction, item) {
13
13
  return await knexTransaction(this.tableName).insert(item);
@@ -38,6 +38,23 @@ class GranulesExecutionsPgModel {
38
38
  .groupBy('execution_cumulus_id');
39
39
  return granuleExecutions.map((granuleExecution) => granuleExecution.execution_cumulus_id);
40
40
  }
41
+ /**
42
+ * Get granule_cumulus_id column values from the execution_cumulus_id
43
+ *
44
+ * @param {Knex | Knex.Transaction} knexOrTransaction -
45
+ * DB client or transaction
46
+ * @param {number | Array<number>} executionCumulusIds -
47
+ * single execution_cumulus_id or array of execution_cumulus_ids
48
+ * @returns {Promise<Array<number>>} An array of granule_cumulus_ids
49
+ */
50
+ async searchByExecutionCumulusIds(knexOrTransaction, executionCumulusIds) {
51
+ const executionCumulusIdsArray = [executionCumulusIds].flat();
52
+ const granuleExecutions = await knexOrTransaction(this.tableName)
53
+ .select('granule_cumulus_id')
54
+ .whereIn('execution_cumulus_id', executionCumulusIdsArray)
55
+ .groupBy('granule_cumulus_id');
56
+ return granuleExecutions.map((granuleExecution) => granuleExecution.granule_cumulus_id);
57
+ }
41
58
  async delete(knexTransaction, params) {
42
59
  return await knexTransaction(this.tableName)
43
60
  .where(params)
@@ -7,7 +7,7 @@ const timestamp_1 = require("../lib/timestamp");
7
7
  class PdrPgModel extends base_1.BasePgModel {
8
8
  constructor() {
9
9
  super({
10
- tableName: tables_1.tableNames.pdrs,
10
+ tableName: tables_1.TableNames.pdrs,
11
11
  });
12
12
  }
13
13
  async upsert(knexOrTrx, pdr) {
@@ -6,7 +6,7 @@ const tables_1 = require("../tables");
6
6
  class ProviderPgModel extends base_1.BasePgModel {
7
7
  constructor() {
8
8
  super({
9
- tableName: tables_1.tableNames.providers,
9
+ tableName: tables_1.TableNames.providers,
10
10
  });
11
11
  }
12
12
  upsert(knexOrTransaction, provider) {
@@ -6,7 +6,7 @@ const tables_1 = require("../tables");
6
6
  class RulePgModel extends base_1.BasePgModel {
7
7
  constructor() {
8
8
  super({
9
- tableName: tables_1.tableNames.rules,
9
+ tableName: tables_1.TableNames.rules,
10
10
  });
11
11
  }
12
12
  upsert(knexOrTransaction, rule) {
package/dist/tables.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export declare enum tableNames {
1
+ export declare enum TableNames {
2
2
  asyncOperations = "async_operations",
3
3
  collections = "collections",
4
4
  executions = "executions",
package/dist/tables.js CHANGED
@@ -1,16 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.tableNames = void 0;
4
- var tableNames;
5
- (function (tableNames) {
6
- tableNames["asyncOperations"] = "async_operations";
7
- tableNames["collections"] = "collections";
8
- tableNames["executions"] = "executions";
9
- tableNames["files"] = "files";
10
- tableNames["granules"] = "granules";
11
- tableNames["granulesExecutions"] = "granules_executions";
12
- tableNames["pdrs"] = "pdrs";
13
- tableNames["providers"] = "providers";
14
- tableNames["rules"] = "rules";
15
- })(tableNames = exports.tableNames || (exports.tableNames = {}));
3
+ exports.TableNames = void 0;
4
+ var TableNames;
5
+ (function (TableNames) {
6
+ TableNames["asyncOperations"] = "async_operations";
7
+ TableNames["collections"] = "collections";
8
+ TableNames["executions"] = "executions";
9
+ TableNames["files"] = "files";
10
+ TableNames["granules"] = "granules";
11
+ TableNames["granulesExecutions"] = "granules_executions";
12
+ TableNames["pdrs"] = "pdrs";
13
+ TableNames["providers"] = "providers";
14
+ TableNames["rules"] = "rules";
15
+ })(TableNames = exports.TableNames || (exports.TableNames = {}));
16
16
  //# sourceMappingURL=tables.js.map
@@ -1,4 +1,5 @@
1
1
  import { ApiFile } from '@cumulus/types/api/files';
2
- import { PostgresFile } from '../types/file';
2
+ import { PostgresFile, PostgresFileRecord } from '../types/file';
3
+ export declare const translatePostgresFileToApiFile: (filePgRecord: PostgresFileRecord) => Omit<ApiFile, 'granuleId'>;
3
4
  export declare const translateApiFiletoPostgresFile: (file: ApiFile) => Omit<PostgresFile, 'granule_cumulus_id'>;
4
5
  //# sourceMappingURL=file.d.ts.map
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.translateApiFiletoPostgresFile = void 0;
3
+ exports.translateApiFiletoPostgresFile = exports.translatePostgresFileToApiFile = void 0;
4
4
  const { parseS3Uri } = require('@cumulus/aws-client/S3');
5
+ const { removeNilProperties } = require('@cumulus/common/util');
5
6
  const getBucket = (file) => {
6
7
  if (file.bucket)
7
8
  return file.bucket;
@@ -16,6 +17,16 @@ const getKey = (file) => {
16
17
  return parseS3Uri(file.filename).Key;
17
18
  return undefined;
18
19
  };
20
+ const translatePostgresFileToApiFile = (filePgRecord) => removeNilProperties({
21
+ bucket: filePgRecord.bucket,
22
+ checksum: filePgRecord.checksum_value,
23
+ checksumType: filePgRecord.checksum_type,
24
+ fileName: filePgRecord.file_name,
25
+ key: filePgRecord.key,
26
+ size: filePgRecord.file_size ? filePgRecord.file_size : undefined,
27
+ source: filePgRecord.source,
28
+ });
29
+ exports.translatePostgresFileToApiFile = translatePostgresFileToApiFile;
19
30
  const translateApiFiletoPostgresFile = (file) => {
20
31
  const bucket = getBucket(file);
21
32
  const key = getKey(file);
@@ -1,8 +1,34 @@
1
1
  import { Knex } from 'knex';
2
+ import { ApiGranule } from '@cumulus/types/api/granules';
2
3
  import { CollectionPgModel } from '../models/collection';
3
4
  import { PdrPgModel } from '../models/pdr';
4
- import { PostgresGranule } from '../types/granule';
5
+ import { PostgresGranule, PostgresGranuleRecord } from '../types/granule';
5
6
  import { ProviderPgModel } from '../models/provider';
7
+ import { FilePgModel } from '../models/file';
8
+ import { PostgresCollectionRecord } from '../types/collection';
9
+ /**
10
+ * Generate an API Granule object from a Postgres Granule with associated Files.
11
+ *
12
+ * @param {Object} params
13
+ * @param {PostgresGranuleRecord} params.granulePgRecord - Granule from Postgres
14
+ * @param {PostgresCollectionRecord} params.collectionPgRecord - Collection from Postgres
15
+ * @param {Knex | Knex.Transaction} params.knexOrTransaction
16
+ * Knex client for reading from RDS database
17
+ * @param {Object} [params.collectionPgModel] - Instance of the collection database model
18
+ * @param {Object} [params.pdrPgModel] - Instance of the pdr database model
19
+ * @param {Object} [params.providerPgModel] - Instance of the provider database model
20
+ * @param {Object} [params.filePgModel] - Instance of the file database model
21
+ * @returns {Object} An API Granule with associated Files
22
+ */
23
+ export declare const translatePostgresGranuleToApiGranule: ({ granulePgRecord, collectionPgRecord, knexOrTransaction, collectionPgModel, pdrPgModel, providerPgModel, filePgModel, }: {
24
+ granulePgRecord: PostgresGranuleRecord;
25
+ collectionPgRecord: PostgresCollectionRecord;
26
+ knexOrTransaction: Knex | Knex.Transaction;
27
+ collectionPgModel?: CollectionPgModel | undefined;
28
+ pdrPgModel?: PdrPgModel | undefined;
29
+ providerPgModel?: ProviderPgModel | undefined;
30
+ filePgModel?: FilePgModel | undefined;
31
+ }) => Promise<ApiGranule>;
6
32
  /**
7
33
  * Generate a Postgres granule record from a DynamoDB record.
8
34
  *
@@ -1,10 +1,78 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.translateApiGranuleToPostgresGranule = void 0;
3
+ exports.translateApiGranuleToPostgresGranule = exports.translatePostgresGranuleToApiGranule = void 0;
4
+ const Collections_1 = require("@cumulus/message/Collections");
5
+ const Executions_1 = require("@cumulus/message/Executions");
6
+ const util_1 = require("@cumulus/common/util");
7
+ const errors_1 = require("@cumulus/errors");
4
8
  const collection_1 = require("../models/collection");
5
9
  const pdr_1 = require("../models/pdr");
6
10
  const provider_1 = require("../models/provider");
7
- const { deconstructCollectionId } = require('@cumulus/message/Collections');
11
+ const file_1 = require("../models/file");
12
+ const file_2 = require("./file");
13
+ const execution_1 = require("../lib/execution");
14
+ /**
15
+ * Generate an API Granule object from a Postgres Granule with associated Files.
16
+ *
17
+ * @param {Object} params
18
+ * @param {PostgresGranuleRecord} params.granulePgRecord - Granule from Postgres
19
+ * @param {PostgresCollectionRecord} params.collectionPgRecord - Collection from Postgres
20
+ * @param {Knex | Knex.Transaction} params.knexOrTransaction
21
+ * Knex client for reading from RDS database
22
+ * @param {Object} [params.collectionPgModel] - Instance of the collection database model
23
+ * @param {Object} [params.pdrPgModel] - Instance of the pdr database model
24
+ * @param {Object} [params.providerPgModel] - Instance of the provider database model
25
+ * @param {Object} [params.filePgModel] - Instance of the file database model
26
+ * @returns {Object} An API Granule with associated Files
27
+ */
28
+ const translatePostgresGranuleToApiGranule = async ({ granulePgRecord, collectionPgRecord, knexOrTransaction, collectionPgModel = new collection_1.CollectionPgModel(), pdrPgModel = new pdr_1.PdrPgModel(), providerPgModel = new provider_1.ProviderPgModel(), filePgModel = new file_1.FilePgModel(), }) => {
29
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
30
+ const collection = collectionPgRecord || await collectionPgModel.get(knexOrTransaction, { cumulus_id: granulePgRecord.collection_cumulus_id });
31
+ if (granulePgRecord.collection_cumulus_id !== collection.cumulus_id) {
32
+ throw new errors_1.ValidationError(`Collection ${collection.cumulus_id} does not match the Granule's Collection ${granulePgRecord.collection_cumulus_id}`);
33
+ }
34
+ const files = await filePgModel.search(knexOrTransaction, { granule_cumulus_id: granulePgRecord.cumulus_id });
35
+ const executionArns = await (0, execution_1.getExecutionArnsByGranuleCumulusId)(knexOrTransaction, granulePgRecord.cumulus_id, 1);
36
+ let pdr;
37
+ if (granulePgRecord.pdr_cumulus_id) {
38
+ pdr = await pdrPgModel.get(knexOrTransaction, { cumulus_id: granulePgRecord.pdr_cumulus_id });
39
+ }
40
+ let provider;
41
+ if (granulePgRecord.provider_cumulus_id) {
42
+ provider = await providerPgModel.get(knexOrTransaction, { cumulus_id: granulePgRecord.provider_cumulus_id });
43
+ }
44
+ const apiGranule = (0, util_1.removeNilProperties)({
45
+ beginningDateTime: (_a = granulePgRecord.beginning_date_time) === null || _a === void 0 ? void 0 : _a.toISOString(),
46
+ cmrLink: granulePgRecord.cmr_link,
47
+ collectionId: (0, Collections_1.constructCollectionId)(collection.name, collection.version),
48
+ createdAt: (_b = granulePgRecord.created_at) === null || _b === void 0 ? void 0 : _b.getTime(),
49
+ duration: granulePgRecord.duration,
50
+ endingDateTime: (_c = granulePgRecord.ending_date_time) === null || _c === void 0 ? void 0 : _c.toISOString(),
51
+ error: granulePgRecord.error,
52
+ execution: executionArns[0] ? (0, Executions_1.getExecutionUrlFromArn)(executionArns[0].arn) : undefined,
53
+ files: files.map((file) => ({
54
+ ...(0, file_2.translatePostgresFileToApiFile)(file),
55
+ })),
56
+ granuleId: granulePgRecord.granule_id,
57
+ lastUpdateDateTime: (_d = granulePgRecord.last_update_date_time) === null || _d === void 0 ? void 0 : _d.toISOString(),
58
+ pdrName: pdr ? pdr.name : undefined,
59
+ processingEndDateTime: (_e = granulePgRecord.processing_end_date_time) === null || _e === void 0 ? void 0 : _e.toISOString(),
60
+ processingStartDateTime: (_f = granulePgRecord.processing_start_date_time) === null || _f === void 0 ? void 0 : _f.toISOString(),
61
+ productionDateTime: (_g = granulePgRecord.production_date_time) === null || _g === void 0 ? void 0 : _g.toISOString(),
62
+ productVolume: granulePgRecord.product_volume
63
+ ? granulePgRecord.product_volume : undefined,
64
+ provider: provider ? provider.name : undefined,
65
+ published: granulePgRecord.published,
66
+ queryFields: granulePgRecord.query_fields,
67
+ status: granulePgRecord.status,
68
+ timestamp: (_h = granulePgRecord.timestamp) === null || _h === void 0 ? void 0 : _h.getTime(),
69
+ timeToArchive: granulePgRecord.time_to_archive,
70
+ timeToPreprocess: granulePgRecord.time_to_process,
71
+ updatedAt: (_j = granulePgRecord.updated_at) === null || _j === void 0 ? void 0 : _j.getTime(),
72
+ });
73
+ return apiGranule;
74
+ };
75
+ exports.translatePostgresGranuleToApiGranule = translatePostgresGranuleToApiGranule;
8
76
  /**
9
77
  * Generate a Postgres granule record from a DynamoDB record.
10
78
  *
@@ -18,7 +86,7 @@ const { deconstructCollectionId } = require('@cumulus/message/Collections');
18
86
  * @returns {Object} A granule PG record
19
87
  */
20
88
  const translateApiGranuleToPostgresGranule = async (dynamoRecord, knexOrTransaction, collectionPgModel = new collection_1.CollectionPgModel(), pdrPgModel = new pdr_1.PdrPgModel(), providerPgModel = new provider_1.ProviderPgModel()) => {
21
- const { name, version } = deconstructCollectionId(dynamoRecord.collectionId);
89
+ const { name, version } = (0, Collections_1.deconstructCollectionId)(dynamoRecord.collectionId);
22
90
  const granuleRecord = {
23
91
  granule_id: dynamoRecord.granuleId,
24
92
  status: dynamoRecord.status,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cumulus/db",
3
- "version": "9.8.0-alpha.0",
3
+ "version": "10.0.0-beta.0",
4
4
  "description": "Utilities for working with the Cumulus DB",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.js",
@@ -29,21 +29,22 @@
29
29
  "node": ">=12.18.0"
30
30
  },
31
31
  "dependencies": {
32
- "@cumulus/aws-client": "9.7.0",
33
- "@cumulus/common": "9.7.0",
34
- "@cumulus/errors": "9.7.0",
35
- "@cumulus/logger": "9.7.0",
36
- "@cumulus/message": "9.7.0",
37
- "@cumulus/types": "9.7.0",
32
+ "@cumulus/aws-client": "10.0.0-beta.0",
33
+ "@cumulus/common": "10.0.0-beta.0",
34
+ "@cumulus/errors": "10.0.0-beta.0",
35
+ "@cumulus/logger": "10.0.0-beta.0",
36
+ "@cumulus/message": "10.0.0-beta.0",
37
+ "@cumulus/types": "10.0.0-beta.0",
38
38
  "crypto-random-string": "^3.2.0",
39
39
  "is-valid-hostname": "0.0.1",
40
40
  "knex": "0.95.11",
41
41
  "lodash": "^4.17.20",
42
42
  "pg": "^8.3.0",
43
43
  "snake-camel": "^1.0.6",
44
- "uuid": "8.3.1"
44
+ "uuid": "8.3.2"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@types/uuid": "^8.0.0"
48
- }
48
+ },
49
+ "gitHead": "bc283986be627ba06a4084cabd4e01d1540d14c7"
49
50
  }