From c39109e6681d244a5d363abc06a38171f1b30a76 Mon Sep 17 00:00:00 2001 From: Thibault Ruby Date: Tue, 19 Sep 2023 11:16:21 +0200 Subject: [PATCH] chore: fix prettier issues --- .eslintrc | 2 +- src/base-model.ts | 27 +++++++++++++++----- test/batch-get.spec.ts | 2 +- test/factories/index.ts | 15 ++++++++--- test/hooks/teardown.ts | 1 - test/models/common.ts | 4 ++- test/query.spec.ts | 52 ++++++++++++++++++++++---------------- test/scan.spec.ts | 55 +++++++++++++++++++++++------------------ 8 files changed, 99 insertions(+), 59 deletions(-) diff --git a/.eslintrc b/.eslintrc index c046e9c..7766ab1 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,7 +1,7 @@ { "parser": "@typescript-eslint/parser", "extends": [ - "plugin:@typescript-eslint/recommended", + "plugin:@typescript-eslint/recommended" ], "plugins": ["prettier"], "parserOptions": { diff --git a/src/base-model.ts b/src/base-model.ts index c914cea..e4ee75e 100644 --- a/src/base-model.ts +++ b/src/base-model.ts @@ -64,7 +64,9 @@ export default abstract class Model { } const skValue = (item as Record)[key]; if (skValue === undefined) { - throw new Error(`Model error: ${type} key "${key}" is not defined on ${JSON.stringify(item)}`); + throw new Error( + `Model error: ${type} key "${key}" is not defined on ${JSON.stringify(item)}`, + ); } if (!this.isKey(skValue)) { throw new Error('Model error: ${type} key value is neither a primitive type nor Buffer'); @@ -87,7 +89,9 @@ export default abstract class Model { } static isKey(key: unknown): key is KeyValue { - return key === null || ['string', 'boolean', 'number'].includes(typeof key) || Buffer.isBuffer(key); + return ( + key === null || ['string', 'boolean', 'number'].includes(typeof key) || Buffer.isBuffer(key) + ); } public setItem(item: T): void { @@ -145,7 +149,9 @@ export default abstract class Model { ); } const putOptions: Partial | undefined = - item_options != null && this.isItem(item_options) ? options : item_options as Partial | undefined; + item_options != null && this.isItem(item_options) + ? options + : (item_options as Partial | undefined); // Extract keys const pk = this.pkValue(toCreate); const sk: KeyValue | undefined = this.sk != null ? this.skValue(toCreate) : undefined; @@ -183,7 +189,9 @@ export default abstract class Model { const toSave: T | undefined = item_options != null && this.isItem(item_options) ? item_options : this.item; const putOptions: Partial | undefined = - item_options != null && this.isItem(item_options) ? options : item_options as Partial | undefined; + item_options != null && this.isItem(item_options) + ? options + : (item_options as Partial | undefined); // Validate item to put if (!toSave) { throw new Error( @@ -252,7 +260,10 @@ export default abstract class Model { return null; } - private testKeys(pk: KeyValue | undefined, sk: KeyValue | undefined): { pk: KeyValue, sk?: KeyValue } { + private testKeys( + pk: KeyValue | undefined, + sk: KeyValue | undefined, + ): { pk: KeyValue; sk?: KeyValue } { if (!pk) { throw Error(`Missing HashKey ${this.pk}=${pk}`); } @@ -323,7 +334,11 @@ export default abstract class Model { if (!this.pk) { throw new Error('Primary key is not defined on your model'); } - const _pk: KeyValue | undefined = Model.isKey(pk_item) ? pk_item : (pk_item ? this.pkValue(pk_item) : undefined); + const _pk: KeyValue | undefined = Model.isKey(pk_item) + ? pk_item + : pk_item + ? this.pkValue(pk_item) + : undefined; const _sk: KeyValue = sk_options != null && Model.isKey(sk_options) ? sk_options : null; const deleteOptions = this.getOptions(sk_options, options); // Build delete item params diff --git a/test/batch-get.spec.ts b/test/batch-get.spec.ts index 9dc2653..1e59727 100644 --- a/test/batch-get.spec.ts +++ b/test/batch-get.spec.ts @@ -2,7 +2,7 @@ import CompositeKeyModel from './models/composite-keys'; import { clearTables } from './hooks/create-tables'; import { hashOnly } from './factories'; import HashKeyModel from './models/hashkey'; -import {KeyValue} from "../src/base-model"; +import { KeyValue } from '../src/base-model'; jest.setTimeout(20 * 1000); diff --git a/test/factories/index.ts b/test/factories/index.ts index 041a3a0..7a2f0fb 100644 --- a/test/factories/index.ts +++ b/test/factories/index.ts @@ -1,5 +1,5 @@ import Model from '../../src/base-model'; -import {CommonFields} from "../models/common"; +import { CommonFields } from '../models/common'; const generateBool = (i: number) => { if (i % 3 === 0) { @@ -8,7 +8,10 @@ const generateBool = (i: number) => { return i % 3 === 1 ? false : null; }; -const generatePartial = (i: number, numericalKeys?: boolean): CommonFields & { rangekey: string | number } => ({ +const generatePartial = ( + i: number, + numericalKeys?: boolean, +): CommonFields & { rangekey: string | number } => ({ rangekey: numericalKeys ? i : `rangekey-${i}`, number: i % 2 === 0 ? i : null, bool: generateBool(i), @@ -47,7 +50,9 @@ export default async ( nbEntries: number, numericalKeys?: boolean, ): Promise> => { - const promises: Array> = []; + const promises: Array< + Promise + > = []; for (let i = 0; i < Math.floor(nbEntries / 2); i += 1) { promises.push(save(model, 1, i, numericalKeys)); } @@ -63,7 +68,9 @@ export const hashOnly = async ( nbEntries: number, numericalKeys?: boolean, ): Promise> => { - const promises: Array> = []; + const promises: Array< + Promise + > = []; for (let i = 0; i < Math.floor(nbEntries / 2); i += 1) { promises.push(save(model, i, i, numericalKeys)); } diff --git a/test/hooks/teardown.ts b/test/hooks/teardown.ts index 34be72e..05fb691 100644 --- a/test/hooks/teardown.ts +++ b/test/hooks/teardown.ts @@ -8,5 +8,4 @@ export default async (): Promise => { console.error('Fatal: Error cleaning database', e); process.exit(1); } - }; diff --git a/test/models/common.ts b/test/models/common.ts index b0fe430..cf7a945 100644 --- a/test/models/common.ts +++ b/test/models/common.ts @@ -4,7 +4,9 @@ import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; export default DynamoDBDocumentClient.from( new DynamoDBClient({ region: 'local', - endpoint: `http://${process.env.LOCAL_DYNAMODB_HOST}:${process.env.LOCAL_DYNAMODB_PORT || 8000}`, + endpoint: `http://${process.env.LOCAL_DYNAMODB_HOST}:${ + process.env.LOCAL_DYNAMODB_PORT || 8000 + }`, }), { marshallOptions: { removeUndefinedValues: true } }, ); diff --git a/test/query.spec.ts b/test/query.spec.ts index 9f2ce69..d74f4c7 100644 --- a/test/query.spec.ts +++ b/test/query.spec.ts @@ -875,7 +875,7 @@ describe('The query method [filtering / object syntax]', () => { expect(result.count).toBe(7); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(7); - expect(result.items.every((i) => i.number! <= 12)).toBe(true); + expect(result.items.every((i) => i.number! <= 12)).toBe(true); }); }); describe('LT', () => { @@ -905,7 +905,7 @@ describe('The query method [filtering / object syntax]', () => { expect(result.count).toBe(6); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(6); - expect(result.items.every((i) => i.number! < 12)).toBe(true); + expect(result.items.every((i) => i.number! < 12)).toBe(true); }); }); describe('GE', () => { @@ -935,7 +935,7 @@ describe('The query method [filtering / object syntax]', () => { expect(result.count).toBe(4); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(4); - expect(result.items.every((i) => i.number! >= 12)).toBe(true); + expect(result.items.every((i) => i.number! >= 12)).toBe(true); }); }); describe('GT', () => { @@ -965,7 +965,7 @@ describe('The query method [filtering / object syntax]', () => { expect(result.count).toBe(3); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(3); - expect(result.items.every((i) => i.number! >= 12)).toBe(true); + expect(result.items.every((i) => i.number! >= 12)).toBe(true); }); }); describe('BETWEEN', () => { @@ -983,7 +983,8 @@ describe('The query method [filtering / object syntax]', () => { expect(result.items.length).toBe(3); expect( result.items.every( - (i) => i.string!.localeCompare('string-3') >= 0 && i.string!.localeCompare('string-8') <= 0, + (i) => + i.string!.localeCompare('string-3') >= 0 && i.string!.localeCompare('string-8') <= 0, ), ).toBe(true); }); @@ -999,7 +1000,7 @@ describe('The query method [filtering / object syntax]', () => { expect(result.count).toBe(6); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(6); - expect(result.items.every((i) => i.number! >= 6 && i.number! <= 17)).toBe(true); + expect(result.items.every((i) => i.number! >= 6 && i.number! <= 17)).toBe(true); }); }); describe('NOT_EXISTS', () => { @@ -1187,7 +1188,7 @@ describe('The query method [filtering / object syntax]', () => { expect(result.count).toBe(10); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(10); - expect(result.items.every((i) => i.number! != null)).toBe(true); + expect(result.items.every((i) => i.number! != null)).toBe(true); }); test('should return items where NOT_NULL condition is true [boolean]', async () => { const result = await model @@ -1267,7 +1268,7 @@ describe('The query method [filtering / object syntax]', () => { expect(result.count).toBe(10); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(10); - expect(result.items.every((i) => i.number! == null)).toBe(true); + expect(result.items.every((i) => i.number! == null)).toBe(true); }); test('should return items where NULL condition is true [boolean]', async () => { const result = await model @@ -1381,7 +1382,7 @@ describe('The query method [filtering / object syntax]', () => { expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(2); expect( - result.items.every((i) => i.string!.match(/^string-1/) && i.bool === true && i.number! >= 12), + result.items.every((i) => i.string!.match(/^string-1/) && i.bool === true && i.number! >= 12), ).toBe(true); }); test('should not filter if empty object is passed', async () => { @@ -1534,7 +1535,7 @@ describe('The query method [filtering / fluid syntax]', () => { expect(result.count).toBe(7); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(7); - expect(result.items.every((i) => i.number! <= 12)).toBe(true); + expect(result.items.every((i) => i.number! <= 12)).toBe(true); }); }); describe('LT', () => { @@ -1560,7 +1561,7 @@ describe('The query method [filtering / fluid syntax]', () => { expect(result.count).toBe(6); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(6); - expect(result.items.every((i) => i.number! < 12)).toBe(true); + expect(result.items.every((i) => i.number! < 12)).toBe(true); }); }); describe('GE', () => { @@ -1584,7 +1585,7 @@ describe('The query method [filtering / fluid syntax]', () => { expect(result.count).toBe(4); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(4); - expect(result.items.every((i) => i.number! >= 12)).toBe(true); + expect(result.items.every((i) => i.number! >= 12)).toBe(true); }); }); describe('GT', () => { @@ -1608,7 +1609,7 @@ describe('The query method [filtering / fluid syntax]', () => { expect(result.count).toBe(3); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(3); - expect(result.items.every((i) => i.number! >= 12)).toBe(true); + expect(result.items.every((i) => i.number! >= 12)).toBe(true); }); }); describe('BETWEEN', () => { @@ -1624,7 +1625,8 @@ describe('The query method [filtering / fluid syntax]', () => { expect(result.items.length).toBe(3); expect( result.items.every( - (i) => i.string!.localeCompare('string-3') >= 0 && i.string!.localeCompare('string-8') <= 0, + (i) => + i.string!.localeCompare('string-3') >= 0 && i.string!.localeCompare('string-8') <= 0, ), ).toBe(true); }); @@ -1639,7 +1641,7 @@ describe('The query method [filtering / fluid syntax]', () => { expect(result.count).toBe(6); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(6); - expect(result.items.every((i) => i.number! >= 6 && i.number! <= 17)).toBe(true); + expect(result.items.every((i) => i.number! >= 6 && i.number! <= 17)).toBe(true); }); }); describe('NOT_EXISTS', () => { @@ -1799,7 +1801,7 @@ describe('The query method [filtering / fluid syntax]', () => { expect(result.count).toBe(10); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(10); - expect(result.items.every((i) => i.number! != null)).toBe(true); + expect(result.items.every((i) => i.number! != null)).toBe(true); }); test('should return items where NOT_NULL condition is true [boolean]', async () => { const result = await model @@ -1867,7 +1869,7 @@ describe('The query method [filtering / fluid syntax]', () => { expect(result.count).toBe(10); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(10); - expect(result.items.every((i) => i.number! == null)).toBe(true); + expect(result.items.every((i) => i.number! == null)).toBe(true); }); test('should return items where NULL condition is true [boolean]', async () => { const result = await model @@ -1986,7 +1988,9 @@ describe('The query method [filtering / fluid syntax]', () => { expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(2); expect( - result.items.every((i) => i.string!.match(/^string-1/) && i.bool === true && i.number! >= 12), + result.items.every( + (i) => i.string!.match(/^string-1/) && i.bool === true && i.number! >= 12, + ), ).toBe(true); }); }); @@ -2004,7 +2008,9 @@ describe('The query method [filtering / fluid syntax]', () => { expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(7); expect( - result.items.every((i) => (i.bool === true && i.number! < 8) || i.string!.match(/^string-1/)), + result.items.every( + (i) => (i.bool === true && i.number! < 8) || i.string!.match(/^string-1/), + ), ).toBe(true); }); }); @@ -2026,7 +2032,9 @@ describe('The query method [filtering / fluid syntax]', () => { expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(4); expect( - result.items.every((i) => i.bool === true && (i.number! < 8 || i.string!.match(/^string-1/))), + result.items.every( + (i) => i.bool === true && (i.number! < 8 || i.string!.match(/^string-1/)), + ), ).toBe(true); }); }); @@ -2058,7 +2066,9 @@ describe('The query method [filtering / fluid syntax]', () => { expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(10); expect( - result.items.every((i) => !(i.bool === false || (i.string! && i.string!.match(/^string-1/)))), + result.items.every( + (i) => !(i.bool === false || (i.string! && i.string!.match(/^string-1/))), + ), ).toBe(true); }); }); diff --git a/test/scan.spec.ts b/test/scan.spec.ts index 7c1e442..308743e 100644 --- a/test/scan.spec.ts +++ b/test/scan.spec.ts @@ -352,7 +352,8 @@ describe('The scan method [filtering / object syntax]', () => { expect(result.items.length).toBe(3); expect( result.items.every( - (i) => i.string!.localeCompare('string-3') >= 0 && i.string!.localeCompare('string-8') <= 0, + (i) => + i.string!.localeCompare('string-3') >= 0 && i.string!.localeCompare('string-8') <= 0, ), ).toBe(true); }); @@ -796,7 +797,10 @@ describe('The scan method [filtering / fluid syntax]', () => { ); }); test('should return items where IN condition is true [number]', async () => { - const result = await model.scan().filter(attr('number').in(2, 12, 14, 0)).exec(); + const result = await model + .scan() + .filter(attr('number').in(2, 12, 14, 0)) + .exec(); expect(result.count).toBe(4); expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(4); @@ -892,7 +896,8 @@ describe('The scan method [filtering / fluid syntax]', () => { expect(result.items.length).toBe(3); expect( result.items.every( - (i) => i.string!.localeCompare('string-3') >= 0 && i.string!.localeCompare('string-8') <= 0, + (i) => + i.string!.localeCompare('string-3') >= 0 && i.string!.localeCompare('string-8') <= 0, ), ).toBe(true); }); @@ -1151,7 +1156,9 @@ describe('The scan method [filtering / fluid syntax]', () => { expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(2); expect( - result.items.every((i) => i.string!.match(/^string-1/) && i.bool === true && i.number! >= 12), + result.items.every( + (i) => i.string!.match(/^string-1/) && i.bool === true && i.number! >= 12, + ), ).toBe(true); }); }); @@ -1169,7 +1176,9 @@ describe('The scan method [filtering / fluid syntax]', () => { expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(7); expect( - result.items.every((i) => (i.bool === true && i.number! < 8) || i.string!.match(/^string-1/)), + result.items.every( + (i) => (i.bool === true && i.number! < 8) || i.string!.match(/^string-1/), + ), ).toBe(true); }); }); @@ -1190,7 +1199,9 @@ describe('The scan method [filtering / fluid syntax]', () => { expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(4); expect( - result.items.every((i) => i.bool === true && (i.number! < 8 || i.string!.match(/^string-1/))), + result.items.every( + (i) => i.bool === true && (i.number! < 8 || i.string!.match(/^string-1/)), + ), ).toBe(true); }); }); @@ -1219,7 +1230,9 @@ describe('The scan method [filtering / fluid syntax]', () => { expect(result.nextPage.lastEvaluatedKey).toBeFalsy(); expect(result.items.length).toBe(10); expect( - result.items.every((i) => !(i.bool === false || (i.string && i.string!.match(/^string-1/)))), + result.items.every( + (i) => !(i.bool === false || (i.string && i.string!.match(/^string-1/))), + ), ).toBe(true); }); }); @@ -1252,9 +1265,8 @@ describe('The scan method [projection]', () => { const result = await model.scan().projection(['string']).exec(); expect(result.count).toBe(17); expect( - result.items.every( - (i) => - attributes.filter((att) => att !== 'string').every((att) => i[att] === undefined), + result.items.every((i) => + attributes.filter((att) => att !== 'string').every((att) => i[att] === undefined), ), ).toBe(true); }); @@ -1271,9 +1283,8 @@ describe('The scan method [projection]', () => { const result = await model.scan().projection(['bool']).exec(); expect(result.count).toBe(17); expect( - result.items.every( - (i) => - attributes.filter((att) => att !== 'bool').every((att) => i[att] === undefined), + result.items.every((i) => + attributes.filter((att) => att !== 'bool').every((att) => i[att] === undefined), ), ).toBe(true); }); @@ -1281,9 +1292,8 @@ describe('The scan method [projection]', () => { const result = await model.scan().projection(['list']).exec(); expect(result.count).toBe(17); expect( - result.items.every( - (i) => - attributes.filter((att) => att !== 'list').every((att) => i[att] === undefined), + result.items.every((i) => + attributes.filter((att) => att !== 'list').every((att) => i[att] === undefined), ), ).toBe(true); }); @@ -1305,9 +1315,8 @@ describe('The scan method [projection]', () => { const result = await model.scan().projection(['stringset']).exec(); expect(result.count).toBe(17); expect( - result.items.every( - (i) => - attributes.filter((att) => att !== 'stringset').every((att) => i[att] === undefined), + result.items.every((i) => + attributes.filter((att) => att !== 'stringset').every((att) => i[att] === undefined), ), ).toBe(true); }); @@ -1329,9 +1338,8 @@ describe('The scan method [projection]', () => { const result = await model.scan().projection(['stringmap']).exec(); expect(result.count).toBe(17); expect( - result.items.every( - (i) => - attributes.filter((att) => att !== 'stringmap').every((att) => i[att] === undefined), + result.items.every((i) => + attributes.filter((att) => att !== 'stringmap').every((att) => i[att] === undefined), ), ).toBe(true); }); @@ -1346,8 +1354,7 @@ describe('The scan method [projection]', () => { (i) => attributes.filter((att) => att !== 'stringmap').every((att) => i[att] == null) && (i.stringmap == null || - (i.stringmap?.key2 != null && - !Object.keys(i.stringmap).some((k) => k !== 'key2'))), + (i.stringmap?.key2 != null && !Object.keys(i.stringmap).some((k) => k !== 'key2'))), ), ).toBe(true); });