Skip to content

Commit

Permalink
Merge pull request #598 from jeremydaly/fix-minor-bugs
Browse files Browse the repository at this point in the history
Fix minor bugs
  • Loading branch information
ThomasAribart committed Sep 27, 2023
2 parents 662f8ed + 9aec795 commit 256e7ee
Show file tree
Hide file tree
Showing 21 changed files with 109 additions and 87 deletions.
8 changes: 2 additions & 6 deletions src/v1/commands/putItem/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,14 @@ export class PutItemCommand<
static commandType = 'put' as const

public entity: ENTITY
public _item?: PutItemInput<ENTITY, false>
public _item?: PutItemInput<ENTITY>
public item: (nextItem: PutItemInput<ENTITY>) => PutItemCommand<ENTITY, OPTIONS>
public _options: OPTIONS
public options: <NEXT_OPTIONS extends PutItemOptions<ENTITY>>(
nextOptions: NEXT_OPTIONS
) => PutItemCommand<ENTITY, NEXT_OPTIONS>

constructor(
entity: ENTITY,
item?: PutItemInput<ENTITY, false>,
options: OPTIONS = {} as OPTIONS
) {
constructor(entity: ENTITY, item?: PutItemInput<ENTITY>, options: OPTIONS = {} as OPTIONS) {
this.entity = entity
this._item = item
this._options = options
Expand Down
2 changes: 1 addition & 1 deletion src/v1/commands/putItem/putItemParams/putItemParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { parsePutItemOptions } from './parsePutItemOptions'

export const putItemParams = <ENTITY extends EntityV2, OPTIONS extends PutItemOptions<ENTITY>>(
entity: ENTITY,
input: PutItemInput<ENTITY, false>,
input: PutItemInput<ENTITY>,
putItemOptions: OPTIONS = {} as OPTIONS
): PutCommandInput => {
const validInput = parseEntityPutCommandInput(entity, input)
Expand Down
47 changes: 27 additions & 20 deletions src/v1/commands/putItem/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,33 @@ import type { OptionalizeUndefinableProperties } from 'v1/types/optionalizeUndef
import type { EntityV2 } from 'v1/entity/class'
import type { If } from 'v1/types/if'

type MustBeDefined<
export type MustBeDefined<
ATTRIBUTE extends Attribute,
REQUIRE_INDEPENDENT_DEFAULTS extends boolean = false
REQUIRED_DEFAULTS extends 'none' | 'independent' | 'all' = 'none'
> =
// Enforce Required attributes that don't have default values
ATTRIBUTE extends { required: AtLeastOnce | Always } & (
| { key: true; defaults: { key: undefined } }
| { key: false; defaults: { put: undefined } }
)
? true
: If<
REQUIRE_INDEPENDENT_DEFAULTS,
// Add attributes with independent defaults if REQUIRE_INDEPENDENT_DEFAULTS is true
ATTRIBUTE extends
: // Add attributes with independent defaults if REQUIRED_DEFAULTS is 'independent'
REQUIRED_DEFAULTS extends 'independent'
? ATTRIBUTE extends
| { key: true; defaults: { key: undefined | ComputedDefault } }
| { key: false; defaults: { put: undefined | ComputedDefault } }
? false
: true
: // Add all required attributes and those with independent defaults if REQUIRED_DEFAULTS is 'all'
REQUIRED_DEFAULTS extends 'all'
? ATTRIBUTE extends { required: AtLeastOnce | Always }
? true
: ATTRIBUTE extends
| { key: true; defaults: { key: undefined | ComputedDefault } }
| { key: false; defaults: { put: undefined | ComputedDefault } }
? false
: true,
false
>
? false
: true
: false

/**
* User input of a PUT command for a given Entity or Schema
Expand All @@ -52,7 +59,7 @@ type MustBeDefined<
*/
export type PutItemInput<
SCHEMA extends EntityV2 | Schema,
REQUIRE_INDEPENDENT_DEFAULTS extends boolean = false
REQUIRED_DEFAULTS extends 'none' | 'independent' | 'all' = 'none'
> = EntityV2 extends SCHEMA
? Item
: Schema extends SCHEMA
Expand All @@ -62,14 +69,14 @@ export type PutItemInput<
{
[KEY in keyof SCHEMA['attributes']]: AttributePutItemInput<
SCHEMA['attributes'][KEY],
REQUIRE_INDEPENDENT_DEFAULTS
REQUIRED_DEFAULTS
>
},
// Sadly we override optional AnyAttributes as 'unknown | undefined' => 'unknown' (undefined lost in the process)
O.SelectKeys<SCHEMA['attributes'], AnyAttribute & { required: Never }>
>
: SCHEMA extends EntityV2
? PutItemInput<SCHEMA['schema'], REQUIRE_INDEPENDENT_DEFAULTS>
? PutItemInput<SCHEMA['schema'], REQUIRED_DEFAULTS>
: never

/**
Expand All @@ -81,25 +88,25 @@ export type PutItemInput<
*/
export type AttributePutItemInput<
ATTRIBUTE extends Attribute,
REQUIRE_INDEPENDENT_DEFAULTS extends boolean = false
REQUIRED_DEFAULTS extends 'none' | 'independent' | 'all' = 'none'
> = Attribute extends ATTRIBUTE
? AttributeValue | undefined
:
| If<MustBeDefined<ATTRIBUTE, REQUIRE_INDEPENDENT_DEFAULTS>, never, undefined>
| If<MustBeDefined<ATTRIBUTE, REQUIRED_DEFAULTS>, never, undefined>
| (ATTRIBUTE extends AnyAttribute
? unknown
: ATTRIBUTE extends PrimitiveAttribute
? ResolvePrimitiveAttribute<ATTRIBUTE>
: ATTRIBUTE extends SetAttribute
? Set<AttributePutItemInput<ATTRIBUTE['elements'], REQUIRE_INDEPENDENT_DEFAULTS>>
? Set<AttributePutItemInput<ATTRIBUTE['elements'], REQUIRED_DEFAULTS>>
: ATTRIBUTE extends ListAttribute
? AttributePutItemInput<ATTRIBUTE['elements'], REQUIRE_INDEPENDENT_DEFAULTS>[]
? AttributePutItemInput<ATTRIBUTE['elements'], REQUIRED_DEFAULTS>[]
: ATTRIBUTE extends MapAttribute
? OptionalizeUndefinableProperties<
{
[KEY in keyof ATTRIBUTE['attributes']]: AttributePutItemInput<
ATTRIBUTE['attributes'][KEY],
REQUIRE_INDEPENDENT_DEFAULTS
REQUIRED_DEFAULTS
>
},
// Sadly we override optional AnyAttributes as 'unknown | undefined' => 'unknown' (undefined lost in the process)
Expand All @@ -109,9 +116,9 @@ export type AttributePutItemInput<
? {
[KEY in ResolvePrimitiveAttribute<ATTRIBUTE['keys']>]?: AttributePutItemInput<
ATTRIBUTE['elements'],
REQUIRE_INDEPENDENT_DEFAULTS
REQUIRED_DEFAULTS
>
}
: ATTRIBUTE extends AnyOfAttribute
? AttributePutItemInput<ATTRIBUTE['elements'][number], REQUIRE_INDEPENDENT_DEFAULTS>
? AttributePutItemInput<ATTRIBUTE['elements'][number], REQUIRED_DEFAULTS>
: never)
41 changes: 24 additions & 17 deletions src/v1/commands/types/KeyInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ import type { If } from 'v1/types/if'

type MustBeDefined<
ATTRIBUTE extends Attribute,
REQUIRE_INDEPENDENT_DEFAULTS extends boolean = false
REQUIRED_DEFAULTS extends 'none' | 'independent' | 'all' = 'none'
> =
// Enforce Required attributes that don't have default values
ATTRIBUTE extends { required: Always; defaults: { key: undefined } }
? true
: // Add attributes with independent defaults if REQUIRE_INDEPENDENT_DEFAULTS is true
If<
REQUIRE_INDEPENDENT_DEFAULTS,
ATTRIBUTE extends { defaults: { key: undefined | ComputedDefault } } ? false : true,
false
>
: // Add attributes with independent defaults if REQUIRED_DEFAULTS is 'independent'
REQUIRED_DEFAULTS extends 'independent'
? ATTRIBUTE extends { defaults: { key: undefined | ComputedDefault } }
? false
: true
: // Add all required attributes and those with independent defaults if REQUIRED_DEFAULTS is 'all'
REQUIRED_DEFAULTS extends 'all'
? ATTRIBUTE extends { required: Always }
? true
: ATTRIBUTE extends { defaults: { key: undefined | ComputedDefault } }
? false
: true
: false

/**
* Key input of a single item command (GET, DELETE ...) for an Entity or Schema
Expand All @@ -43,7 +50,7 @@ type MustBeDefined<
*/
export type KeyInput<
SCHEMA extends EntityV2 | Schema,
REQUIRE_INDEPENDENT_DEFAULTS extends boolean = false
REQUIRED_DEFAULTS extends 'none' | 'independent' | 'all' = 'none'
> = EntityV2 extends SCHEMA
? MapAttributeValue
: Schema extends SCHEMA
Expand All @@ -54,14 +61,14 @@ export type KeyInput<
// Keep only key attributes
[KEY in O.SelectKeys<SCHEMA['attributes'], { key: true }>]: AttributeKeyInput<
SCHEMA['attributes'][KEY],
REQUIRE_INDEPENDENT_DEFAULTS
REQUIRED_DEFAULTS
>
},
// Sadly we override optional AnyAttributes as 'unknown | undefined' => 'unknown' (undefined lost in the process)
O.SelectKeys<SCHEMA['attributes'], AnyAttribute & { key: true; required: Never }>
>
: SCHEMA extends EntityV2
? KeyInput<SCHEMA['schema'], REQUIRE_INDEPENDENT_DEFAULTS>
? KeyInput<SCHEMA['schema'], REQUIRED_DEFAULTS>
: never

/**
Expand All @@ -72,26 +79,26 @@ export type KeyInput<
*/
export type AttributeKeyInput<
ATTRIBUTE extends Attribute,
REQUIRE_INDEPENDENT_DEFAULTS extends boolean = false
REQUIRED_DEFAULTS extends 'none' | 'independent' | 'all' = 'none'
> = Attribute extends ATTRIBUTE
? AttributeValue | undefined
:
| If<MustBeDefined<ATTRIBUTE, REQUIRE_INDEPENDENT_DEFAULTS>, never, undefined>
| If<MustBeDefined<ATTRIBUTE, REQUIRED_DEFAULTS>, never, undefined>
| (ATTRIBUTE extends AnyAttribute
? unknown
: ATTRIBUTE extends PrimitiveAttribute
? ResolvePrimitiveAttribute<ATTRIBUTE>
: ATTRIBUTE extends SetAttribute
? Set<AttributeKeyInput<ATTRIBUTE['elements'], REQUIRE_INDEPENDENT_DEFAULTS>>
? Set<AttributeKeyInput<ATTRIBUTE['elements'], REQUIRED_DEFAULTS>>
: ATTRIBUTE extends ListAttribute
? AttributeKeyInput<ATTRIBUTE['elements'], REQUIRE_INDEPENDENT_DEFAULTS>[]
? AttributeKeyInput<ATTRIBUTE['elements'], REQUIRED_DEFAULTS>[]
: ATTRIBUTE extends MapAttribute
? OptionalizeUndefinableProperties<
{
// Keep only key attributes
[KEY in O.SelectKeys<ATTRIBUTE['attributes'], { key: true }>]: AttributeKeyInput<
ATTRIBUTE['attributes'][KEY],
REQUIRE_INDEPENDENT_DEFAULTS
REQUIRED_DEFAULTS
>
},
// Sadly we override optional AnyAttributes as 'unknown | undefined' => 'unknown' (undefined lost in the process)
Expand All @@ -101,9 +108,9 @@ export type AttributeKeyInput<
? {
[KEY in ResolvePrimitiveAttribute<ATTRIBUTE['keys']>]?: AttributeKeyInput<
ATTRIBUTE['elements'],
REQUIRE_INDEPENDENT_DEFAULTS
REQUIRED_DEFAULTS
>
}
: ATTRIBUTE extends AnyOfAttribute
? AttributeKeyInput<ATTRIBUTE['elements'][number], REQUIRE_INDEPENDENT_DEFAULTS>
? AttributeKeyInput<ATTRIBUTE['elements'][number], REQUIRED_DEFAULTS>
: never)
8 changes: 2 additions & 6 deletions src/v1/commands/updateItem/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,14 @@ export class UpdateItemCommand<
static commandType = 'put' as const

public entity: ENTITY
public _item?: UpdateItemInput<ENTITY, false>
public _item?: UpdateItemInput<ENTITY>
public item: (nextItem: UpdateItemInput<ENTITY>) => UpdateItemCommand<ENTITY, OPTIONS>
public _options: OPTIONS
public options: <NEXT_OPTIONS extends UpdateItemOptions<ENTITY>>(
nextOptions: NEXT_OPTIONS
) => UpdateItemCommand<ENTITY, NEXT_OPTIONS>

constructor(
entity: ENTITY,
item?: UpdateItemInput<ENTITY, false>,
options: OPTIONS = {} as OPTIONS
) {
constructor(entity: ENTITY, item?: UpdateItemInput<ENTITY>, options: OPTIONS = {} as OPTIONS) {
this.entity = entity
this._item = item
this._options = options
Expand Down
37 changes: 23 additions & 14 deletions src/v1/commands/updateItem/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,33 @@ export type UpdateItemInputExtension =

type MustBeDefined<
ATTRIBUTE extends Attribute,
REQUIRE_INDEPENDENT_DEFAULTS extends boolean = false
REQUIRED_DEFAULTS extends 'none' | 'independent' | 'all' = 'none'
> =
// Enforce Required attributes that don't have default values
ATTRIBUTE extends { required: Always } & (
| { key: true; defaults: { key: undefined } }
| { key: false; defaults: { update: undefined } }
)
? true
: REQUIRE_INDEPENDENT_DEFAULTS extends true
? // Add attributes with independent defaults if REQUIRE_INDEPENDENT_DEFAULTS is true
ATTRIBUTE extends
: // Add attributes with independent defaults if REQUIRED_DEFAULTS is 'independent'
REQUIRED_DEFAULTS extends 'independent'
? ATTRIBUTE extends
| { key: true; defaults: { key: undefined | ComputedDefault } }
| { key: false; defaults: { update: undefined | ComputedDefault } }
? false
: true
: // Add all required attributes and those with independent defaults if REQUIRED_DEFAULTS is 'all'
REQUIRED_DEFAULTS extends 'all'
? ATTRIBUTE extends { required: Always }
? true
: ATTRIBUTE extends
| { key: true; defaults: { key: undefined | ComputedDefault } }
| { key: false; defaults: { update: undefined | ComputedDefault } }
? false
: true
: false

type CanBeRemoved<ATTRIBUTE extends Attribute> = ATTRIBUTE extends { required: 'never' }
type CanBeRemoved<ATTRIBUTE extends Attribute> = ATTRIBUTE extends { required: Never }
? true
: false

Expand All @@ -136,7 +145,7 @@ type CanBeRemoved<ATTRIBUTE extends Attribute> = ATTRIBUTE extends { required: '
*/
export type UpdateItemInput<
SCHEMA extends EntityV2 | Schema = EntityV2,
REQUIRE_INDEPENDENT_DEFAULTS extends boolean = false
REQUIRED_DEFAULTS extends 'none' | 'independent' | 'all' = 'none'
> = EntityV2 extends SCHEMA
? Item<UpdateItemInputExtension>
: Schema extends SCHEMA
Expand All @@ -146,15 +155,15 @@ export type UpdateItemInput<
{
[KEY in keyof SCHEMA['attributes']]: AttributeUpdateItemInput<
SCHEMA['attributes'][KEY],
REQUIRE_INDEPENDENT_DEFAULTS,
REQUIRED_DEFAULTS,
SchemaAttributePath<SCHEMA>
>
},
// Sadly we override optional AnyAttributes as 'unknown | undefined' => 'unknown' (undefined lost in the process)
O.SelectKeys<SCHEMA['attributes'], AnyAttribute & { required: AtLeastOnce | Never }>
>
: SCHEMA extends EntityV2
? UpdateItemInput<SCHEMA['schema'], REQUIRE_INDEPENDENT_DEFAULTS>
? UpdateItemInput<SCHEMA['schema'], REQUIRED_DEFAULTS>
: never

export type Reference<
Expand Down Expand Up @@ -210,12 +219,12 @@ type AttributeUpdateItemCompleteInput<ATTRIBUTE extends Attribute> = Attribute e
*/
export type AttributeUpdateItemInput<
ATTRIBUTE extends Attribute = Attribute,
REQUIRE_INDEPENDENT_DEFAULTS extends boolean = false,
REQUIRED_DEFAULTS extends 'none' | 'independent' | 'all' = 'none',
SCHEMA_ATTRIBUTE_PATHS extends string = string
> = Attribute extends ATTRIBUTE
? AttributeValue<UpdateItemInputExtension> | undefined
:
| If<MustBeDefined<ATTRIBUTE, REQUIRE_INDEPENDENT_DEFAULTS>, never, undefined>
| If<MustBeDefined<ATTRIBUTE, REQUIRED_DEFAULTS>, never, undefined>
| If<CanBeRemoved<ATTRIBUTE>, $REMOVE, never>
// Not using Reference<...> for improved type display
| GET<
Expand Down Expand Up @@ -283,7 +292,7 @@ export type AttributeUpdateItemInput<
[INDEX in number]?:
| AttributeUpdateItemInput<
ATTRIBUTE['elements'],
REQUIRE_INDEPENDENT_DEFAULTS,
REQUIRED_DEFAULTS,
SCHEMA_ATTRIBUTE_PATHS
>
| $REMOVE
Expand Down Expand Up @@ -320,7 +329,7 @@ export type AttributeUpdateItemInput<
{
[KEY in keyof ATTRIBUTE['attributes']]: AttributeUpdateItemInput<
ATTRIBUTE['attributes'][KEY],
REQUIRE_INDEPENDENT_DEFAULTS,
REQUIRED_DEFAULTS,
SCHEMA_ATTRIBUTE_PATHS
>
},
Expand All @@ -339,7 +348,7 @@ export type AttributeUpdateItemInput<
[KEY in ResolvePrimitiveAttribute<ATTRIBUTE['keys']>]?:
| AttributeUpdateItemInput<
ATTRIBUTE['elements'],
REQUIRE_INDEPENDENT_DEFAULTS,
REQUIRED_DEFAULTS,
SCHEMA_ATTRIBUTE_PATHS
>
| $REMOVE
Expand All @@ -349,7 +358,7 @@ export type AttributeUpdateItemInput<
: ATTRIBUTE extends AnyOfAttribute
? AttributeUpdateItemInput<
ATTRIBUTE['elements'][number],
REQUIRE_INDEPENDENT_DEFAULTS,
REQUIRED_DEFAULTS,
SCHEMA_ATTRIBUTE_PATHS
>
: never)
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const updateItemParams = <
OPTIONS extends UpdateItemOptions<ENTITY>
>(
entity: ENTITY,
input: UpdateItemInput<ENTITY, false>,
input: UpdateItemInput<ENTITY>,
updateItemOptions: OPTIONS = {} as OPTIONS
): UpdateCommandInput => {
const validInput = parseEntityUpdateCommandInput(entity, input)
Expand Down

0 comments on commit 256e7ee

Please sign in to comment.