Skip to content

Commit

Permalink
feat: allow creation of Dynamic attribute with only metadata
Browse files Browse the repository at this point in the history
Also add docs to cover custom attribute name.

Fixes #673
  • Loading branch information
benhutchins committed Apr 2, 2024
1 parent 994a97a commit 62d7a52
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
17 changes: 17 additions & 0 deletions docs/Attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ You can also [define custom types](#custom-attribute-types).
| [`@Dyngoose.Attribute.Any`](dyngooseattributeany) | `S` | `Object` | Stores an object as a JSON-encoded string in DynamoDB, does not allow querying or filtering of child attributes. |
| [`@Dyngoose.Attribute.Date`](dyngooseattributedate) | `S` or `N` | `Date` | Stores a Date value. By default stores values in a ISO 8601 formatted string. You can use options to store values as Unix timestamps. |

#### Attribute vs Property Name

By default, attributes will be saved to the DynamoDB using the property name you assign on your `Table` class. You can override this by providing an explicit `name` set on your attribute's configuration:

```typescript
// in the below example, the attribute name will be 'userId' in DynamoDB
@Dyngoose.Attribute.String()
userId: string

// in the below example, the attribute name will be 'user_id' in DynamoDB
@Dyngoose.Attribute.String({ name: 'user_id' })
userId: string
```

#### Alternative Decorator Style

Dyngoose also allows all the official attribute types to be defined simply as `Dyngoose.Attribute('String')` if you prefer. You can pass your options as a second argument.
Expand All @@ -43,6 +57,7 @@ utility. The additional options available from the converter are exposed on the
marshallOptions: {},
unmarshallOptions: {},
})
someProperty: string | number | any
```

##### Limitations of Dynamic Attributes
Expand Down Expand Up @@ -77,6 +92,7 @@ The `String` attribute supports additional settings:
// forces the value to uppercase
uppercase: true,
})
stringProperty: string
```

These options are also available on the `Dyngoose.Attribute.StringSet` type.
Expand Down Expand Up @@ -120,6 +136,7 @@ The `Date` attribute supports additional settings:
// defaults to current time when a record is created or updated
nowOnUpdate: true,
})
dateProperty: Date
```

#### Dyngoose.Attribute.List
Expand Down
9 changes: 6 additions & 3 deletions src/decorator/attribute-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,18 @@ export interface AttributeDefinition {
getAttribute: (record: Table, propertyName: string) => any
}

export function Attribute<T extends keyof AttributeTypeMap>(type?: T, metadata?: AttributeMetadataMap[T]): AttributeDefinition {
export function Attribute<T extends keyof AttributeTypeMap>(typeOrMetadata?: T | Metadata.AttributeType.Dynamic, metadata?: AttributeMetadataMap[T]): AttributeDefinition {
const type = typeof typeOrMetadata === 'string' ? typeOrMetadata : 'Dynamic'
metadata = metadata ?? (typeof typeOrMetadata === 'string' ? undefined : typeOrMetadata)

const define = function (record: Table, propertyName: string): void {
const AttributeTypeClass: any = AttributeTypes[type ?? 'Dynamic']
const AttributeTypeClass: any = AttributeTypes[type]
const decorator = new AttributeTypeClass(record, propertyName, metadata)
decorator.decorate()
}

define.getAttribute = function (record: Table, propertyName: string): any {
const AttributeTypeClass: any = AttributeTypes[type ?? 'Dynamic']
const AttributeTypeClass: any = AttributeTypes[type]
const decorator = new AttributeTypeClass(record, propertyName, metadata)
return decorator.attribute
}
Expand Down

0 comments on commit 62d7a52

Please sign in to comment.