forked from dynamodb-toolbox/dynamodb-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseEntity.ts
130 lines (106 loc) · 3.29 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
/**
* DynamoDB Toolbox: A simple set of tools for working with Amazon DynamoDB
* @author Jeremy Daly <[email protected]>
* @license MIT
*/
// Import libraries & types
import parseEntityAttributes from './parseEntityAttributes'
import { EntityConstructor } from '../classes/Entity'
// Import error handlers
import { error } from './utils'
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 = ReturnType<typeof parseEntity>
// Parse entity
export function parseEntity(entity: EntityConstructor) {
let {
name,
timestamps,
created,
createdAlias,
modified,
modifiedAlias,
typeAlias,
typeHidden,
attributes,
autoExecute,
autoParse,
table,
...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(', ')}`)
// Entity name
name = typeof name === 'string'
&& name.trim().length > 0 ? name.trim()
: error(`'name' must be defined`)
// Enable created/modified timestamps on items
timestamps = typeof timestamps === 'boolean' ? timestamps : true
// Define 'created' attribute name
created = typeof created === 'string'
&& created.trim().length > 0 ? created.trim()
: '_ct'
// Define 'createdAlias'
createdAlias = typeof createdAlias === 'string'
&& createdAlias.trim().length > 0 ? createdAlias.trim()
: 'created'
// Define 'modified' attribute anme
modified = typeof modified === 'string'
&& modified.trim().length > 0 ? modified.trim()
: '_md'
// Define 'modifiedAlias'
modifiedAlias = typeof modifiedAlias === 'string'
&& modifiedAlias.trim().length > 0 ? modifiedAlias.trim()
: 'modified'
// Define 'entityAlias'
typeAlias = typeof typeAlias === 'string'
&& typeAlias.trim().length > 0 ? typeAlias.trim()
: 'entity'
// Define 'typeHidden'
typeHidden = typeof typeHidden === "boolean" ? typeHidden : false;
// Sanity check the attributes
attributes = typeof attributes === 'object' && !Array.isArray(attributes) ?
attributes : error(`Please provide a valid 'attributes' object`)
// Add timestamps
if (timestamps) {
attributes[created] = { type: 'string', alias: createdAlias, default: ()=> new Date().toISOString() }
attributes[modified] = { type: 'string', alias: modifiedAlias, default: ()=> new Date().toISOString(), onUpdate: true }
}
// Tracking info
let track: TrackingInfo = {
fields: Object.keys(attributes), // attributes and alias list,
defaults: {}, // tracks default attributes
required: {},
linked: {},
keys: {} // tracks partition/sort/index keys
}
// Return the entity
return Object.assign({
name,
schema: parseEntityAttributes(attributes,track), // removed nested attribute?
defaults: track.defaults,
required: track.required,
linked: track.linked,
autoExecute,
autoParse,
typeHidden,
_etAlias: typeAlias
},
table ? { table } : {}
) // end mapping object
} // end parseEntity
export default parseEntity