dyna-record 0.0.13 → 0.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -53,6 +53,14 @@ Create a table class that extends [DynaRecord base class](https://dyna-record.co
|
|
|
53
53
|
#### Basic usage
|
|
54
54
|
|
|
55
55
|
```typescript
|
|
56
|
+
import DynaRecord, {
|
|
57
|
+
Table,
|
|
58
|
+
PartitionKeyAttribute,
|
|
59
|
+
SortKeyAttribute,
|
|
60
|
+
PartitionKey,
|
|
61
|
+
SortKey
|
|
62
|
+
} from "dyna-record";
|
|
63
|
+
|
|
56
64
|
@Table({ name: "my-table", delimiter: "#" })
|
|
57
65
|
abstract class MyTable extends DynaRecord {
|
|
58
66
|
@PartitionKeyAttribute({ alias: "PK" })
|
|
@@ -66,6 +74,14 @@ abstract class MyTable extends DynaRecord {
|
|
|
66
74
|
#### Customizing the default field table aliases
|
|
67
75
|
|
|
68
76
|
```typescript
|
|
77
|
+
import DynaRecord, {
|
|
78
|
+
Table,
|
|
79
|
+
PartitionKeyAttribute,
|
|
80
|
+
SortKeyAttribute,
|
|
81
|
+
PartitionKey,
|
|
82
|
+
SortKey
|
|
83
|
+
} from "dyna-record";
|
|
84
|
+
|
|
69
85
|
@Table({
|
|
70
86
|
name: "mock-table",
|
|
71
87
|
delimiter: "#",
|
|
@@ -103,6 +119,8 @@ By default, each entity will have [default attributes](https://dyna-record.com/t
|
|
|
103
119
|
- [updatedAt](https://dyna-record.com/classes/default.html#updatedAt) - Timestamp of when the entity was updated last
|
|
104
120
|
|
|
105
121
|
```typescript
|
|
122
|
+
import { Entity } from "dyna-record";
|
|
123
|
+
|
|
106
124
|
@Entity
|
|
107
125
|
class Student extends MyTable {
|
|
108
126
|
// ...
|
|
@@ -124,6 +142,8 @@ For [natively supported data types](https://docs.aws.amazon.com/amazondynamodb/l
|
|
|
124
142
|
- Attempting to remove a non-nullable attribute will result in a [NullConstrainViolationError](https://dyna-record.com/classes/NullConstraintViolationError.html)
|
|
125
143
|
|
|
126
144
|
```typescript
|
|
145
|
+
import { Entity, Attribute, NullableAttribute } from "dyna-record";
|
|
146
|
+
|
|
127
147
|
@Entity
|
|
128
148
|
class Student extends MyTable {
|
|
129
149
|
@Attribute({ alias: "Username" }) // Sets alias if field in Dynamo is different then on the model
|
|
@@ -146,6 +166,8 @@ Dates are not natively supported in Dynamo. To define a date attribute use [@Dat
|
|
|
146
166
|
- Attempting to remove a non-nullable attribute will result in a [NullConstrainViolationError](https://dyna-record.com/classes/NullConstraintViolationError.html)
|
|
147
167
|
|
|
148
168
|
```typescript
|
|
169
|
+
import { Entity, DateAttribute, NullableDateAttribute } from "dyna-record";
|
|
170
|
+
|
|
149
171
|
@Entity
|
|
150
172
|
class Student extends MyTable {
|
|
151
173
|
@DateAttribute()
|
|
@@ -165,6 +187,15 @@ Define foreign keys in order to support [@BelongsTo](https://dyna-record.com/fun
|
|
|
165
187
|
- Attempting to remove an entity from a non-nullable foreign key will result in a [NullConstrainViolationError](https://dyna-record.com/classes/NullConstraintViolationError.html)
|
|
166
188
|
|
|
167
189
|
```typescript
|
|
190
|
+
import {
|
|
191
|
+
Entity,
|
|
192
|
+
ForeignKeyAttribute,
|
|
193
|
+
ForeignKey,
|
|
194
|
+
NullableForeignKeyAttribute,
|
|
195
|
+
NullableForeignKey,
|
|
196
|
+
BelongsTo
|
|
197
|
+
} from "dyna-record";
|
|
198
|
+
|
|
168
199
|
@Entity
|
|
169
200
|
class Assignment extends MyTable {
|
|
170
201
|
@ForeignKeyAttribute()
|
|
@@ -199,6 +230,14 @@ A relationship can be defined as nullable or non-nullable. Non-nullable relation
|
|
|
199
230
|
[Docs](https://dyna-record.com/functions/HasOne.html)
|
|
200
231
|
|
|
201
232
|
```typescript
|
|
233
|
+
import {
|
|
234
|
+
Entity,
|
|
235
|
+
ForeignKeyAttribute,
|
|
236
|
+
ForeignKey,
|
|
237
|
+
BelongsTo,
|
|
238
|
+
HasOne
|
|
239
|
+
} from "dyna-record";
|
|
240
|
+
|
|
202
241
|
@Entity
|
|
203
242
|
class Assignment extends MyTable {
|
|
204
243
|
// 'assignmentId' must be defined on associated model
|
|
@@ -222,6 +261,14 @@ class Grade extends MyTable {
|
|
|
222
261
|
[Docs](https://dyna-record.com/functions/HasMany.html)
|
|
223
262
|
|
|
224
263
|
```typescript
|
|
264
|
+
import {
|
|
265
|
+
Entity,
|
|
266
|
+
NullableForeignKeyAttribute,
|
|
267
|
+
NullableForeignKey,
|
|
268
|
+
BelongsTo,
|
|
269
|
+
HasMany
|
|
270
|
+
} from "dyna-record";
|
|
271
|
+
|
|
225
272
|
@Entity
|
|
226
273
|
class Teacher extends MyTable {
|
|
227
274
|
// 'teacherId' must be defined on associated model
|
|
@@ -247,6 +294,13 @@ class Course extends MyTable {
|
|
|
247
294
|
HasAndBelongsToMany relationships require a [JoinTable](https://dyna-record.com/classes/JoinTable.html) class. This represents a virtual table to support the relationship
|
|
248
295
|
|
|
249
296
|
```typescript
|
|
297
|
+
import {
|
|
298
|
+
Entity,
|
|
299
|
+
JoinTable,
|
|
300
|
+
ForeignKey,
|
|
301
|
+
HasAndBelongsToMany
|
|
302
|
+
} from "dyna-record";
|
|
303
|
+
|
|
250
304
|
class StudentCourse extends JoinTable<Student, Course> {
|
|
251
305
|
public readonly studentId: ForeignKey;
|
|
252
306
|
public readonly courseId: ForeignKey;
|
|
@@ -328,7 +382,7 @@ If no record is found matching the provided ID, findById returns undefined. This
|
|
|
328
382
|
##### Find an entity by id
|
|
329
383
|
|
|
330
384
|
```typescript
|
|
331
|
-
const course
|
|
385
|
+
const course = await Course.findById("123");
|
|
332
386
|
|
|
333
387
|
// user.id; - ok for any attribute
|
|
334
388
|
// user.teacher; - Error! teacher relationship was not included in query
|
|
@@ -338,7 +392,7 @@ const course: Course = await Course.findById("123");
|
|
|
338
392
|
#### Including related entities
|
|
339
393
|
|
|
340
394
|
```typescript
|
|
341
|
-
const course
|
|
395
|
+
const course = await Course.findById("123", {
|
|
342
396
|
include: [{ association: "teacher" }, { association: "assignments" }]
|
|
343
397
|
});
|
|
344
398
|
|