Skip to content

Commit

Permalink
chore: fix prettier issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultRuby authored and MarioArnt committed Nov 8, 2023
1 parent bbfa305 commit c39109e
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"parser": "@typescript-eslint/parser",
"extends": [
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended"
],
"plugins": ["prettier"],
"parserOptions": {
Expand Down
27 changes: 21 additions & 6 deletions src/base-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export default abstract class Model<T> {
}
const skValue = (item as Record<string, unknown>)[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');
Expand All @@ -87,7 +89,9 @@ export default abstract class Model<T> {
}

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 {
Expand Down Expand Up @@ -145,7 +149,9 @@ export default abstract class Model<T> {
);
}
const putOptions: Partial<PutCommandInput> | undefined =
item_options != null && this.isItem(item_options) ? options : item_options as Partial<PutCommandInput> | undefined;
item_options != null && this.isItem(item_options)
? options
: (item_options as Partial<PutCommandInput> | undefined);
// Extract keys
const pk = this.pkValue(toCreate);
const sk: KeyValue | undefined = this.sk != null ? this.skValue(toCreate) : undefined;
Expand Down Expand Up @@ -183,7 +189,9 @@ export default abstract class Model<T> {
const toSave: T | undefined =
item_options != null && this.isItem(item_options) ? item_options : this.item;
const putOptions: Partial<PutCommandInput> | undefined =
item_options != null && this.isItem(item_options) ? options : item_options as Partial<PutCommandInput> | undefined;
item_options != null && this.isItem(item_options)
? options
: (item_options as Partial<PutCommandInput> | undefined);
// Validate item to put
if (!toSave) {
throw new Error(
Expand Down Expand Up @@ -252,7 +260,10 @@ export default abstract class Model<T> {
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}`);
}
Expand Down Expand Up @@ -323,7 +334,11 @@ export default abstract class Model<T> {
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
Expand Down
2 changes: 1 addition & 1 deletion test/batch-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
15 changes: 11 additions & 4 deletions test/factories/index.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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),
Expand Down Expand Up @@ -47,7 +50,9 @@ export default async (
nbEntries: number,
numericalKeys?: boolean,
): Promise<Array<CommonFields & { rangekey: string | number } & { hashkey: string | number }>> => {
const promises: Array<Promise<CommonFields & { rangekey: string | number } & { hashkey: string | number }>> = [];
const promises: Array<
Promise<CommonFields & { rangekey: string | number } & { hashkey: string | number }>
> = [];
for (let i = 0; i < Math.floor(nbEntries / 2); i += 1) {
promises.push(save(model, 1, i, numericalKeys));
}
Expand All @@ -63,7 +68,9 @@ export const hashOnly = async (
nbEntries: number,
numericalKeys?: boolean,
): Promise<Array<CommonFields & { rangekey: string | number } & { hashkey: string | number }>> => {
const promises: Array<Promise<CommonFields & { rangekey: string | number } & { hashkey: string | number }>> = [];
const promises: Array<
Promise<CommonFields & { rangekey: string | number } & { hashkey: string | number }>
> = [];
for (let i = 0; i < Math.floor(nbEntries / 2); i += 1) {
promises.push(save(model, i, i, numericalKeys));
}
Expand Down
1 change: 0 additions & 1 deletion test/hooks/teardown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ export default async (): Promise<void> => {
console.error('Fatal: Error cleaning database', e);
process.exit(1);
}

};
4 changes: 3 additions & 1 deletion test/models/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } },
);
Expand Down
52 changes: 31 additions & 21 deletions test/query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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);
});
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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);
});
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
});
Expand All @@ -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);
});
});
Expand All @@ -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);
});
});
Expand Down Expand Up @@ -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);
});
});
Expand Down
Loading

0 comments on commit c39109e

Please sign in to comment.