forked from jeremydaly/dynamodb-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseEntity.ts
199 lines (177 loc) · 5.82 KB
/
parseEntity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import parseEntityAttributes from './parseEntityAttributes.js'
import type { TableDef } from '../classes/Table/types.js'
import type {
AttributeDefinitions,
EntityConstructor,
PureAttributeDefinition,
Readonly
} from '../classes/Entity/types.js'
import { error } from './utils.js'
export interface TrackingInfo {
fields: string[]
defaults: any
required: any
linked: Linked
keys: any
}
export interface Linked {
[key: string]: string[]
}
export interface TrackingInfoKeys {
partitionKey?: string
sortKey?: string
}
export type ParsedEntity<
EntityTable extends TableDef | undefined = TableDef | undefined,
Name extends string = string,
AutoExecute extends boolean = boolean,
AutoParse extends boolean = boolean,
TypeAlias extends string = string,
TypeHidden extends boolean = boolean
> = {
name: Name
schema: {
keys: any
attributes: Record<string, PureAttributeDefinition>
}
_etAlias: TypeAlias
typeHidden: TypeHidden
autoParse: AutoParse | undefined
autoExecute: AutoExecute | undefined
linked: Linked
defaults: any
required: any
table?: EntityTable | undefined,
setTable?: <NextTable extends EntityTable | undefined>(table: NextTable) => ParsedEntity<NextTable, Name, AutoExecute, AutoParse, TypeAlias, TypeHidden>
}
// Parse entity
export function parseEntity<
EntityTable extends TableDef | undefined,
Name extends string,
AutoExecute extends boolean,
AutoParse extends boolean,
Timestamps extends boolean,
CreatedAlias extends string,
ModifiedAlias extends string,
TypeAlias extends string,
TypeHidden extends boolean,
ReadonlyAttributeDefinitions extends Readonly<AttributeDefinitions> = Readonly<AttributeDefinitions>
>(
entity: EntityConstructor<
EntityTable,
Name,
AutoExecute,
AutoParse,
Timestamps,
CreatedAlias,
ModifiedAlias,
TypeAlias,
TypeHidden,
ReadonlyAttributeDefinitions
>
): ParsedEntity<EntityTable, Name, AutoExecute, AutoParse, TypeAlias> {
let {
name,
timestamps,
created,
createdAlias,
modified,
modifiedAlias,
typeAlias,
typeHidden,
attributes,
// eslint-disable-next-line prefer-const
autoExecute,
// eslint-disable-next-line prefer-const
autoParse,
// eslint-disable-next-line prefer-const
table,
// eslint-disable-next-line prefer-const
...args // extraneous config
} = entity
// TODO: verify string types (e.g. created)
// Error on extraneous arguments
if (Object.keys(args).length > 0)
error(`Invalid Entity configuration options: ${Object.keys(args).join(', ')}`)
// 🔨 TOIMPROVE: Not triming would be better for type safety (no need to cast)
// Entity name
name = (typeof name === 'string' && name.trim().length > 0
? name.trim()
: error(`'name' must be defined`)) as Name
// 🔨 TOIMPROVE: Use default option & simply throw if type is incorrect
// Enable created/modified timestamps on items
timestamps = (typeof timestamps === 'boolean' ? timestamps : true) as Timestamps
// Define 'created' attribute name
created = typeof created === 'string' && created.trim().length > 0 ? created.trim() : '_ct'
// 🔨 TOIMPROVE: Not triming would be better for type safety (no need to cast)
// Define 'createdAlias'
createdAlias = (typeof createdAlias === 'string' && createdAlias.trim().length > 0
? createdAlias.trim()
: 'created') as CreatedAlias
// Define 'modified' attribute anme
modified = typeof modified === 'string' && modified.trim().length > 0 ? modified.trim() : '_md'
// 🔨 TOIMPROVE: Not triming would be better for type safety (no need to cast)
// Define 'modifiedAlias'
modifiedAlias = (typeof modifiedAlias === 'string' && modifiedAlias.trim().length > 0
? modifiedAlias.trim()
: 'modified') as ModifiedAlias
// 🔨 TOIMPROVE: Not triming would be better for type safety (no need to cast)
// Define 'entityAlias'
typeAlias = (typeof typeAlias === 'string' && typeAlias.trim().length > 0
? typeAlias.trim()
: 'entity') as TypeAlias
typeHidden = (typeof typeHidden === 'boolean' ? typeHidden : false) as TypeHidden
// Sanity check the attributes
attributes =
attributes?.constructor === Object
? attributes
: error(`Please provide a valid 'attributes' object`)
// Add timestamps
if (timestamps) {
(attributes as AttributeDefinitions)[created] = {
type: 'string',
alias: createdAlias,
default: () => new Date().toISOString()
}
;(attributes as AttributeDefinitions)[modified] = {
type: 'string',
alias: modifiedAlias,
default: () => new Date().toISOString(),
onUpdate: true
}
}
// Tracking info
const track: TrackingInfo = {
fields: Object.keys(attributes), // attributes and alias list,
defaults: {}, // tracks default attributes
required: {},
linked: {},
keys: {} // tracks partition/sort/index keys
}
const schema = parseEntityAttributes<ReadonlyAttributeDefinitions>(attributes, track) // removed nested attribute?
// Safety check for bigint users, to avoid losing precision
if (table && table.DocumentClient) {
Object.keys(schema.attributes).forEach((field) => {
const config = schema.attributes[field]
if (config.type && config.type === 'bigint' || config.setType && config.setType === 'bigint') {
// Verify DocumentClient has wrapNumbers set to true
if (table?.DocumentClient?.config?.translateConfig?.unmarshallOptions?.wrapNumbers !== true) {
error('Please set `wrapNumbers: true` in your DocumentClient to avoid losing precision with bigint fields')
}
}
})
}
return {
name,
schema,
defaults: track.defaults,
required: track.required,
linked: track.linked,
autoExecute,
autoParse,
typeHidden,
_etAlias: typeAlias,
...(table ? { table } : {})
}
}
export default parseEntity