-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat-legacy-models-import
- Loading branch information
Showing
17 changed files
with
304 additions
and
635 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file modified
0
packages/spacecat-shared-data-access/test/it/util/tableOperations.js
100644 → 100755
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
import { performance } from 'perf_hooks'; | ||
|
||
/** | ||
* Scans DynamoDB and automatically handles pagination to retrieve all items. | ||
* | ||
* @param {DynamoDBDocumentClient} docClient - The AWS SDK DynamoDB Document client instance. | ||
* @param {Object} originalParams - The parameters for the DynamoDB scan. | ||
* @param {Logger} log - The logging object, defaults to console. | ||
* @returns {Promise<Array>} A promise that resolves to an array of items retrieved from DynamoDB. | ||
* @throws {Error} Throws an error if the DynamoDB scan operation fails. | ||
*/ | ||
async function scan(docClient, originalParams, log = console) { | ||
let items = []; | ||
const params = { ...originalParams }; | ||
|
||
let totalTime = 0; | ||
let paginationCount = 0; | ||
|
||
try { | ||
let data; | ||
if (params.Limit && params.Limit <= 1) { | ||
const result = await docClient.scan(params); | ||
return result.Items; | ||
} | ||
do { | ||
const startTime = performance.now(); | ||
|
||
// eslint-disable-next-line no-await-in-loop | ||
data = await docClient.scan(params); | ||
|
||
const endTime = performance.now(); // End timing | ||
const duration = endTime - startTime; | ||
totalTime += duration; | ||
paginationCount += 1; | ||
|
||
log.info(`Pagination ${paginationCount} scan time: ${duration.toFixed(2)} ms`); | ||
|
||
items = items.concat(data.Items); | ||
params.ExclusiveStartKey = data.LastEvaluatedKey; | ||
} while (data.LastEvaluatedKey); | ||
} catch (error) { | ||
log.error('DB Scan Error:', error); | ||
throw error; | ||
} | ||
|
||
log.info(`Total scan time: ${totalTime.toFixed(2)} ms with ${paginationCount} paginations for scan: ${JSON.stringify(params)}`); | ||
|
||
return items; | ||
} | ||
|
||
export default scan; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2024 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
/* eslint-env mocha */ | ||
|
||
import { expect } from 'chai'; | ||
import { createClient } from '../../src/index.js'; | ||
|
||
describe('scan', () => { | ||
const scanParams = { | ||
TableName: 'TestTable', | ||
}; | ||
const scanParamsWithLimit = { | ||
TableName: 'TestTable', | ||
Limit: 1, | ||
}; | ||
|
||
let dynamoDbClient; | ||
let mockDocClient; | ||
|
||
beforeEach(() => { | ||
mockDocClient = { | ||
scan: async (params) => { | ||
if (params.Limit) { | ||
return { Items: ['item1'] }; | ||
} | ||
// Check if LastEvaluatedKey is provided and simulate pagination | ||
if (params.ExclusiveStartKey === 'key2') { | ||
return { Items: ['item3'], LastEvaluatedKey: undefined }; | ||
} else { | ||
return { Items: ['item1', 'item2'], LastEvaluatedKey: 'key2' }; | ||
} | ||
}, | ||
}; | ||
|
||
dynamoDbClient = createClient(console, undefined, mockDocClient); | ||
}); | ||
|
||
it('scans items from the database', async () => { | ||
const result = await dynamoDbClient.scan(scanParams); | ||
expect(result).to.be.an('array'); | ||
}); | ||
|
||
it('scans items from the database with pagination', async () => { | ||
const result = await dynamoDbClient.scan(scanParams); | ||
expect(result).to.have.lengthOf(3); | ||
expect(result).to.deep.equal(['item1', 'item2', 'item3']); | ||
}); | ||
|
||
it('scans items from the database with limit', async () => { | ||
const result = await dynamoDbClient.scan(scanParamsWithLimit); | ||
expect(result).to.have.lengthOf(1); | ||
expect(result).to.deep.equal(['item1']); | ||
}); | ||
|
||
it('handles errors in scan', async () => { | ||
mockDocClient.scan = async () => { | ||
throw new Error('Scan failed'); | ||
}; | ||
|
||
try { | ||
await dynamoDbClient.scan(scanParams); | ||
expect.fail('scanDb did not throw as expected'); | ||
} catch (error) { | ||
expect(error.message).to.equal('Scan failed'); | ||
} | ||
}); | ||
}); |