Skip to content

Commit

Permalink
feat(Model-119): add allowScan fieldMerge pull request #293 from Saji…
Browse files Browse the repository at this point in the history
…dHamza9/feat/119-allowScan

feat(Model): add allowScan field
  • Loading branch information
ThibaultRuby authored Jan 30, 2024
2 parents 83fa2c4 + e732374 commit 9e1fe7c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
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;

0 comments on commit 9e1fe7c

Please sign in to comment.