From f999502c0d4eeab4f05ba05dbd667f094dd2e125 Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Sat, 20 Jun 2020 13:04:58 -0600 Subject: [PATCH 1/3] Cleaning up duplicated code for Condition & DocumentRetriever --- lib/Condition.ts | 23 ++++++++++++++++++++++- lib/DocumentRetriever.ts | 33 ++++----------------------------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/lib/Condition.ts b/lib/Condition.ts index a23d02fd6..455d21cf2 100644 --- a/lib/Condition.ts +++ b/lib/Condition.ts @@ -62,7 +62,28 @@ const types: ConditionComparisonType[] = [ ]; export type ConditionInitalizer = Condition | ObjectType | string; -export class Condition { +export interface BasicOperators { + and: () => T; + or: () => T; + not: () => T; + parenthesis: (value: T | ConditionFunction) => T; + group: (value: T | ConditionFunction) => T; + where: (key: string) => T; + filter: (key: string) => T; + attribute: (key: string) => T; + eq: (value: any) => T; + lt: (value: Number) => T; + le: (value: Number) => T; + gt: (value: Number) => T; + ge: (value: Number) => T; + beginsWith: (value: any) => T; + contains: (value: any) => T; + exists: (value: any) => T; + in: (value: any) => T; + between: (...values: any[]) => T; +} + +export class Condition implements BasicOperators { settings: { // TODO: fix this below, it should be a reference to `OR` not Symbol, you are only allowed to pass in OR here, not any other Symbol. conditions: ConditionStorageSettingsConditions; diff --git a/lib/DocumentRetriever.ts b/lib/DocumentRetriever.ts index 01c49c0ae..8d08ff9ee 100644 --- a/lib/DocumentRetriever.ts +++ b/lib/DocumentRetriever.ts @@ -1,7 +1,7 @@ import ddb = require("./aws/ddb/internal"); import CustomError = require("./Error"); import utils = require("./utils"); -import {Condition, ConditionInitalizer, ConditionFunction} from "./Condition"; +import {Condition, ConditionInitalizer, ConditionFunction, BasicOperators} from "./Condition"; import {Model} from "./Model"; import {Document} from "./Document"; import {CallbackType, ObjectType, DocumentArray, SortOrder} from "./General"; @@ -16,8 +16,10 @@ interface DocumentRetrieverTypeInformation { type: DocumentRetrieverTypes; pastTense: string; } + // DocumentRetriever is used for both Scan and Query since a lot of the code is shared between the two -abstract class DocumentRetriever { +interface DocumentRetriever extends BasicOperators {} +abstract class DocumentRetriever implements BasicOperators { internalSettings?: { model: Model; typeInformation: DocumentRetrieverTypeInformation; @@ -116,33 +118,6 @@ abstract class DocumentRetriever { } } - - - // TODO: this was all copied from Condition.ts, we need to figure out a better way to handle this -------------------------------------------------- - and: () => Condition; - or: () => Condition; - not: () => Condition; - parenthesis: (value: Condition | ConditionFunction) => Condition; - group: (value: Condition | ConditionFunction) => Condition; - where: (key: string) => Condition; - filter: (key: string) => Condition; - attribute: (key: string) => Condition; - eq: (value: any) => Condition; - lt: (value: number) => Condition; - le: (value: number) => Condition; - gt: (value: number) => Condition; - ge: (value: number) => Condition; - beginsWith: (value: any) => Condition; - contains: (value: any) => Condition; - exists: (value: any) => Condition; - in: (value: any) => Condition; - between: (...values: any[]) => Condition; - // ------------------------------------------------------------------------------------------------------------------------------------------------- - - - - - constructor (model: Model, typeInformation: DocumentRetrieverTypeInformation, object?: ConditionInitalizer) { this.internalSettings = {model, typeInformation}; From 8052e2aa677316dfb1694e0fbf03b0b169a8b801 Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Sat, 20 Jun 2020 13:10:25 -0600 Subject: [PATCH 2/3] Fixing lint errors --- lib/Condition.ts | 8 ++++---- lib/DocumentRetriever.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Condition.ts b/lib/Condition.ts index 455d21cf2..17ee61400 100644 --- a/lib/Condition.ts +++ b/lib/Condition.ts @@ -72,10 +72,10 @@ export interface BasicOperators { filter: (key: string) => T; attribute: (key: string) => T; eq: (value: any) => T; - lt: (value: Number) => T; - le: (value: Number) => T; - gt: (value: Number) => T; - ge: (value: Number) => T; + lt: (value: number) => T; + le: (value: number) => T; + gt: (value: number) => T; + ge: (value: number) => T; beginsWith: (value: any) => T; contains: (value: any) => T; exists: (value: any) => T; diff --git a/lib/DocumentRetriever.ts b/lib/DocumentRetriever.ts index 8d08ff9ee..a9f11d923 100644 --- a/lib/DocumentRetriever.ts +++ b/lib/DocumentRetriever.ts @@ -1,7 +1,7 @@ import ddb = require("./aws/ddb/internal"); import CustomError = require("./Error"); import utils = require("./utils"); -import {Condition, ConditionInitalizer, ConditionFunction, BasicOperators} from "./Condition"; +import {Condition, ConditionInitalizer, BasicOperators} from "./Condition"; import {Model} from "./Model"; import {Document} from "./Document"; import {CallbackType, ObjectType, DocumentArray, SortOrder} from "./General"; @@ -18,7 +18,7 @@ interface DocumentRetrieverTypeInformation { } // DocumentRetriever is used for both Scan and Query since a lot of the code is shared between the two -interface DocumentRetriever extends BasicOperators {} +type DocumentRetriever = BasicOperators; abstract class DocumentRetriever implements BasicOperators { internalSettings?: { model: Model; From 2b557d69c6ca97dd4ce76acee6c6188acb7670af Mon Sep 17 00:00:00 2001 From: Charlie Fish Date: Sat, 20 Jun 2020 13:13:49 -0600 Subject: [PATCH 3/3] Fixing lint error --- lib/DocumentRetriever.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/DocumentRetriever.ts b/lib/DocumentRetriever.ts index a9f11d923..109928615 100644 --- a/lib/DocumentRetriever.ts +++ b/lib/DocumentRetriever.ts @@ -18,8 +18,9 @@ interface DocumentRetrieverTypeInformation { } // DocumentRetriever is used for both Scan and Query since a lot of the code is shared between the two -type DocumentRetriever = BasicOperators; -abstract class DocumentRetriever implements BasicOperators { +// type DocumentRetriever = BasicOperators; +interface DocumentRetriever extends BasicOperators {} // eslint-disable-line @typescript-eslint/no-empty-interface +abstract class DocumentRetriever { internalSettings?: { model: Model; typeInformation: DocumentRetrieverTypeInformation;