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

[v1] Chore : improve type for build methods #645

Merged
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
28 changes: 3 additions & 25 deletions src/v1/entity/class.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import type { Schema } from 'v1/schema'
import type { TableV2, PrimaryKey } from 'v1/table'
import type {
PutItemCommand,
GetItemCommand,
DeleteItemCommand,
UpdateItemCommand
} from 'v1/operations'
import type { KeyInput } from 'v1/operations/types'
import type { PutItemCommandClass } from 'v1/operations/putItem/command'
import type { GetItemCommandClass } from 'v1/operations/getItem/command'
import type { DeleteItemCommandClass } from 'v1/operations/deleteItem/command'
import type { UpdateItemCommandClass } from 'v1/operations/updateItem/command'
import type { EntityOperation } from 'v1/operations/class'
import type { If } from 'v1/types/if'
import { DynamoDBToolboxError } from 'v1/errors'
Expand Down Expand Up @@ -50,21 +40,9 @@ export class EntityV2<
public computeKey?: (
keyInput: Schema extends SCHEMA ? any : KeyInput<SCHEMA>
) => PrimaryKey<TABLE>
// TODO: Maybe there's a way not to have to list all operations here
// (use OPERATION_CLASS somehow) but I haven't found it yet
public build: <OPERATION_CLASS extends typeof EntityOperation = typeof EntityOperation>(
operationClass: OPERATION_CLASS
) => string extends NAME
? any
: OPERATION_CLASS extends PutItemCommandClass
? PutItemCommand<this>
: OPERATION_CLASS extends GetItemCommandClass
? GetItemCommand<this>
: OPERATION_CLASS extends DeleteItemCommandClass
? DeleteItemCommand<this>
: OPERATION_CLASS extends UpdateItemCommandClass
? UpdateItemCommand<this>
: EntityOperation<this>
public build: <OPERATION_CLASS extends EntityOperation<this> = EntityOperation<this>>(
operationClass: new (entity: this) => OPERATION_CLASS
) => OPERATION_CLASS

/**
* Define an Entity for a given table
Expand Down
8 changes: 1 addition & 7 deletions src/v1/operations/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ export class TableCommand<
public _table: TABLE
public _entities: ENTITIES

constructor({
table,
entities = ([] as unknown) as ENTITIES
}: {
table: TABLE
entities?: ENTITIES
}) {
constructor(table: TABLE, entities = ([] as unknown) as ENTITIES) {
this._table = table
this._entities = entities
}
Expand Down
17 changes: 8 additions & 9 deletions src/v1/operations/query/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ export class QueryCommand<
) => QueryCommand<TABLE, ENTITIES, QUERY, NEXT_OPTIONS>

constructor(
args: { table: TABLE; entities?: ENTITIES },
table: TABLE,
entities = ([] as unknown) as ENTITIES,
query?: QUERY,
options: OPTIONS = {} as OPTIONS
) {
super(args)
super(table, entities)
this._query = query
this._options = options

Expand All @@ -102,19 +103,17 @@ export class QueryCommand<
? OPTIONS
: QueryOptions<TABLE, NEXT_ENTITIES>
>(
{
table: this._table,
entities: nextEntities
},
this._table,
nextEntities,
this._query,
this._options as OPTIONS extends QueryOptions<TABLE, NEXT_ENTITIES>
? OPTIONS
: QueryOptions<TABLE, NEXT_ENTITIES>
)
this.query = nextQuery =>
new QueryCommand({ table: this._table, entities: this._entities }, nextQuery, this._options)
new QueryCommand(this._table, this._entities, nextQuery, this._options)
this.options = nextOptions =>
new QueryCommand({ table: this._table, entities: this._entities }, this._query, nextOptions)
new QueryCommand(this._table, this._entities, this._query, nextOptions)
}

params = (): QueryCommandInput => {
Expand All @@ -124,7 +123,7 @@ export class QueryCommand<
})
}

return queryParams({ table: this._table, entities: this._entities }, this._query, this._options)
return queryParams(this._table, this._entities, this._query, this._options)
}

send = async (): Promise<QueryResponse<TABLE, ENTITIES, QUERY, OPTIONS>> => {
Expand Down
3 changes: 2 additions & 1 deletion src/v1/operations/query/queryParams/queryParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const queryParams = <
QUERY extends Query<TABLE>,
OPTIONS extends QueryOptions<TABLE, ENTITIES, QUERY>
>(
{ table, entities = ([] as unknown) as ENTITIES }: { table: TABLE; entities?: ENTITIES },
table: TABLE,
entities = ([] as unknown) as ENTITIES,
query: QUERY,
scanOptions: OPTIONS = {} as OPTIONS
): QueryCommandInput => {
Expand Down
20 changes: 10 additions & 10 deletions src/v1/operations/scan/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,25 @@ export class ScanCommand<
nextOptions: NEXT_OPTIONS
) => ScanCommand<TABLE, ENTITIES, NEXT_OPTIONS>

constructor(args: { table: TABLE; entities?: ENTITIES }, options: OPTIONS = {} as OPTIONS) {
super(args)
constructor(
table: TABLE,
entities = ([] as unknown) as ENTITIES,
options: OPTIONS = {} as OPTIONS
) {
super(table, entities)
this._options = options

this.entities = <NEXT_ENTITIES extends EntityV2[]>(...nextEntities: NEXT_ENTITIES) =>
new ScanCommand<TABLE, NEXT_ENTITIES, ScanOptions<TABLE, NEXT_ENTITIES>>(
{
table: this._table,
entities: nextEntities
},
this._table,
nextEntities,
// For some reason we can't do the same as Query (cast OPTIONS) as it triggers an infinite type compute
this._options as ScanOptions<TABLE, NEXT_ENTITIES>
)
this.options = nextOptions =>
new ScanCommand({ table: this._table, entities: this._entities }, nextOptions)
this.options = nextOptions => new ScanCommand(this._table, this._entities, nextOptions)
}

params = (): ScanCommandInput =>
scanParams({ table: this._table, entities: this._entities }, this._options)
params = (): ScanCommandInput => scanParams(this._table, this._entities, this._options)

send = async (): Promise<ScanResponse<TABLE, ENTITIES, OPTIONS>> => {
const scanParams = this.params()
Expand Down
3 changes: 2 additions & 1 deletion src/v1/operations/scan/scanParams/scanParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export const scanParams = <
ENTITIES extends EntityV2[],
OPTIONS extends ScanOptions<TABLE, ENTITIES>
>(
{ table, entities = ([] as unknown) as ENTITIES }: { table: TABLE; entities?: ENTITIES },
table: TABLE,
entities = ([] as unknown) as ENTITIES,
scanOptions: OPTIONS = {} as OPTIONS
): ScanCommandInput => {
const {
Expand Down
20 changes: 4 additions & 16 deletions src/v1/table/class.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'

import type { TableCommand } from 'v1/operations/class'
import type { ScanCommand } from 'v1/operations'
import type { ScanCommandClass } from 'v1/operations/scan/command'
import type { QueryCommand } from 'v1/operations'
import type { QueryCommandClass } from 'v1/operations/query/command'
import type { NarrowObject, NarrowObjectRec } from 'v1/types/narrowObject'
import { isString } from 'v1/utils/validation/isString'

Expand All @@ -24,17 +20,9 @@ export class TableV2<
public entityAttributeSavedAs: ENTITY_ATTRIBUTE_SAVED_AS

public getName: () => string
// TODO: Maybe there's a way not to have to list all commands here
// (use TABLE_COMMAND_CLASS somehow) but I haven't found it yet
public build: <TABLE_COMMAND_CLASS extends typeof TableCommand = typeof TableCommand>(
tableCommandClass: TABLE_COMMAND_CLASS
) => Key extends PARTITION_KEY
? any
: TABLE_COMMAND_CLASS extends ScanCommandClass
? ScanCommand<this>
: TABLE_COMMAND_CLASS extends QueryCommandClass
? QueryCommand<this>
: TableCommand<this>
public build: <TABLE_COMMAND_CLASS extends TableCommand<this> = TableCommand<this>>(
tableCommandClass: new (table: this) => TABLE_COMMAND_CLASS
) => TABLE_COMMAND_CLASS

/**
* Define a Table
Expand Down Expand Up @@ -77,6 +65,6 @@ export class TableV2<
}
}

this.build = commandClass => new commandClass({ table: this }) as any
this.build = commandClass => new commandClass(this) as any
}
}