Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Model): add allowScan field #293

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/base-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export default abstract class Model<T> {

protected autoUpdatedAt = false;

protected allowScan = true;

constructor(item?: T, options?: DynamoDBClientConfig, translateConfig?: TranslateConfig) {
this.item = item;
const client = new DynamoDBClient(options ?? { region: process.env.AWS_REGION });
Expand Down Expand Up @@ -389,6 +391,9 @@ export default abstract class Model<T> {
* @returns The scanned items (in the 1MB single scan operation limit) and the last evaluated key
*/
public scan(options?: Partial<ScanCommandInput>): Scan<T> {
if (!this.allowScan) {
throw new Error("Model.prototype.scan: scan operations are not allowed. To enable them, consider enabling allowScan field.");
}
// Building scan parameters
if (!this.pk) {
throw new Error('Primary key is not defined on your model');
Expand Down
5 changes: 4 additions & 1 deletion test/hooks/create-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import compositeTable from '../tables/composite-key';
import hashTable from '../tables/hash-key';
import numericalTable from '../tables/numerical-keys';
import timeTrackedTable from '../tables/autoCreatedAt-autoUpdatedAt';
import allowScanTable from '../tables/allowScan';

import {
CreateTableCommand,
CreateTableCommandInput,
Expand All @@ -13,7 +15,8 @@ const tables: Record<string, CreateTableCommandInput> = {
hashTable,
compositeTable,
numericalTable,
timeTrackedTable
timeTrackedTable,
allowScanTable
};

const dynamodb = new DynamoDBClient({
Expand Down
13 changes: 13 additions & 0 deletions test/models/allowScan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Model from "../../src";
import { HashKeyEntity } from "./hashkey";
import documentClient from './common';

export default class AllowScanModel extends Model<HashKeyEntity> {
protected tableName = 'table_test_allowScan';

protected allowScan = false;

protected pk = 'hashkey';

protected documentClient = documentClient;
}
9 changes: 9 additions & 0 deletions test/scan.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import {
beginsWith,
} from '../src';
import { attr, not } from '../src/filter-conditions';
import AllowScanModel from './models/allowScan';

describe('The scan method', () => {
const model = new CompositeKeyModel();
const scanAllowedModel = new AllowScanModel();
test.skip('should return all items in the table in 1MB limit is exec called', async () => {
await clearTables();
await generateData(model, 10000);
Expand All @@ -37,6 +39,13 @@ describe('The scan method', () => {
const result = await model.scan().execAll();
expect(result.length).toBe(10);
});
test('should throw an error when allowScan is set to false', async () => {
try {
await scanAllowedModel.scan().execAll();
} catch (e) {
expect((e as Error).message.includes('Model.prototype.scan: scan operations are not allowed. To enable them, consider enabling allowScan field.')).toBe(true);
}
});
});

describe('The count method', () => {
Expand Down
21 changes: 21 additions & 0 deletions test/tables/allowScan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CreateTableCommandInput } from "@aws-sdk/client-dynamodb";

export default {
AttributeDefinitions: [
{
AttributeName: 'hashkey',
AttributeType: 'S',
},
],
KeySchema: [
{
AttributeName: 'hashkey',
KeyType: 'HASH',
},
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5,
},
TableName: 'table_test_allowScan',
} as CreateTableCommandInput;
Loading