dyna-record 0.0.11 → 0.0.13
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 +24 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,6 +22,7 @@ Note: ACID compliant according to DynamoDB [limitations](https://docs.aws.amazon
|
|
|
22
22
|
- [Delete](#delete)
|
|
23
23
|
- [Type Safety Features](#type-safety-features)
|
|
24
24
|
- [Best Practices](#best-practices)
|
|
25
|
+
- [Debug Logging](#debug-logging)
|
|
25
26
|
|
|
26
27
|
## Getting Started
|
|
27
28
|
|
|
@@ -157,7 +158,7 @@ class Student extends MyTable {
|
|
|
157
158
|
|
|
158
159
|
### Foreign Keys
|
|
159
160
|
|
|
160
|
-
Define foreign keys in order to support [@BelongsTo](https://dyna-record.com/functions/BelongsTo.html) relationships. A foreign key
|
|
161
|
+
Define foreign keys in order to support [@BelongsTo](https://dyna-record.com/functions/BelongsTo.html) relationships. A foreign key is required for [@HasOne](https://dyna-record.com/functions/HasOne.html) and [@HasMany](https://dyna-record.com/functions/HasMany.html) relationships.
|
|
161
162
|
|
|
162
163
|
- The [alias](https://dyna-record.com/interfaces/AttributeOptions.html#alias) option allows you to specify the attribute name as it appears in the DynamoDB table, different from your class property name.
|
|
163
164
|
- Set nullable foreign key attributes as optional for optimal type safety
|
|
@@ -189,12 +190,14 @@ Dyna-Record supports defining relationships between entities such as [@HasOne](h
|
|
|
189
190
|
|
|
190
191
|
A relationship can be defined as nullable or non-nullable. Non-nullable relationships will be enforced via transactions and violations will result in [NullConstraintViolationError](https://dyna-record.com/classes/NullConstraintViolationError.html)
|
|
191
192
|
|
|
192
|
-
-
|
|
193
|
-
-
|
|
194
|
-
- Relationship decorators (
|
|
193
|
+
- [@ForeignKeyAttribute](https://dyna-record.com/functions/ForeignKeyAttribute.html) is used to define a foreign key that links to another entity and is not nullable.
|
|
194
|
+
- [@NullableForeignKeyAttribute](https://dyna-record.com/functions/NullableForeignKeyAttribute.html) is used to define a foreign key that links to another entity and is nullable.
|
|
195
|
+
- Relationship decorators ([@HasOne](#hasone), [@HasMany](#hasmany), [@BelongsTo](https://dyna-record.com/functions/BelongsTo.html), [@HasAndBelongsToMany](#hasandbelongstomany)) define how entities relate to each other.
|
|
195
196
|
|
|
196
197
|
#### HasOne
|
|
197
198
|
|
|
199
|
+
[Docs](https://dyna-record.com/functions/HasOne.html)
|
|
200
|
+
|
|
198
201
|
```typescript
|
|
199
202
|
@Entity
|
|
200
203
|
class Assignment extends MyTable {
|
|
@@ -208,7 +211,7 @@ class Grade extends MyTable {
|
|
|
208
211
|
@ForeignKeyAttribute()
|
|
209
212
|
public readonly assignmentId: ForeignKey;
|
|
210
213
|
|
|
211
|
-
// 'assignmentId' Must be defined on self as ForeignKey
|
|
214
|
+
// 'assignmentId' Must be defined on self as ForeignKey or NullableForeignKey
|
|
212
215
|
@BelongsTo(() => Assignment, { foreignKey: "assignmentId" })
|
|
213
216
|
public readonly assignment: Assignment;
|
|
214
217
|
}
|
|
@@ -216,10 +219,12 @@ class Grade extends MyTable {
|
|
|
216
219
|
|
|
217
220
|
### HasMany
|
|
218
221
|
|
|
222
|
+
[Docs](https://dyna-record.com/functions/HasMany.html)
|
|
223
|
+
|
|
219
224
|
```typescript
|
|
220
225
|
@Entity
|
|
221
226
|
class Teacher extends MyTable {
|
|
222
|
-
// 'teacherId'
|
|
227
|
+
// 'teacherId' must be defined on associated model
|
|
223
228
|
@HasMany(() => Course, { foreignKey: "teacherId" })
|
|
224
229
|
public readonly courses: Course[];
|
|
225
230
|
}
|
|
@@ -229,6 +234,7 @@ class Course extends MyTable {
|
|
|
229
234
|
@NullableForeignKeyAttribute()
|
|
230
235
|
public readonly teacherId?: NullableForeignKey;
|
|
231
236
|
|
|
237
|
+
// 'teacherId' Must be defined on self as ForeignKey or NullableForeignKey
|
|
232
238
|
@BelongsTo(() => Teacher, { foreignKey: "teacherId" })
|
|
233
239
|
public readonly teacher?: Teacher;
|
|
234
240
|
}
|
|
@@ -236,6 +242,8 @@ class Course extends MyTable {
|
|
|
236
242
|
|
|
237
243
|
### HasAndBelongsToMany
|
|
238
244
|
|
|
245
|
+
[Docs](https://dyna-record.com/functions/HasAndBelongsToMany.html)
|
|
246
|
+
|
|
239
247
|
HasAndBelongsToMany relationships require a [JoinTable](https://dyna-record.com/classes/JoinTable.html) class. This represents a virtual table to support the relationship
|
|
240
248
|
|
|
241
249
|
```typescript
|
|
@@ -269,7 +277,7 @@ class Student extends OtherTable {
|
|
|
269
277
|
|
|
270
278
|
[Docs](https://dyna-record.com/classes/default.html#create)
|
|
271
279
|
|
|
272
|
-
The create method is used to insert a new record into a DynamoDB table. This method automatically handles key generation (using UUIDs), timestamps for
|
|
280
|
+
The create method is used to insert a new record into a DynamoDB table. This method automatically handles key generation (using UUIDs), timestamps for [createdAt](https://dyna-record.com/classes/default.html#createdAt) and [updatedAt](https://dyna-record.com/classes/default.html#updatedAt) fields, and the management of relationships between entities. It leverages AWS SDK's [TransactWriteCommand](https://www.google.com/search?q=aws+transact+write+command&oq=aws+transact+write+command&gs_lcrp=EgZjaHJvbWUyBggAEEUYOTIGCAEQRRg7MgYIAhBFGDvSAQgzMjAzajBqN6gCALACAA&sourceid=chrome&ie=UTF-8) for transactional integrity, ensuring either complete success or rollback in case of any failure. The method handles conditional checks to ensure data integrity and consistency during creation. If a foreignKey is set on create, dyna-record will de-normalize the data required in order to support the relationship
|
|
273
281
|
|
|
274
282
|
To use the create method, call it on the model class you wish to create a new record for. Pass the properties of the new record as an object argument to the method.
|
|
275
283
|
|
|
@@ -295,12 +303,12 @@ const grade: Grade = await Grade.create({
|
|
|
295
303
|
|
|
296
304
|
#### Error handling
|
|
297
305
|
|
|
298
|
-
The method is designed to throw errors under various conditions, such as transaction cancellation due to failed conditional checks. For instance, if you attempt to create a Grade for an Assignment that already has one, the method throws a TransactionWriteFailedError
|
|
306
|
+
The method is designed to throw errors under various conditions, such as transaction cancellation due to failed conditional checks. For instance, if you attempt to create a `Grade` for an `Assignment` that already has one, the method throws a `TransactionWriteFailedError`.
|
|
299
307
|
|
|
300
308
|
#### Notes
|
|
301
309
|
|
|
302
|
-
- Automatic Timestamp Management: The createdAt and updatedAt fields are managed automatically and reflect the time of creation and the last update, respectively.
|
|
303
|
-
- Automatic ID Generation: Each entity created gets a unique
|
|
310
|
+
- Automatic Timestamp Management: The [createdAt](https://dyna-record.com/classes/default.html#createdAt) and [updatedAt](https://dyna-record.com/classes/default.html#updatedAt) fields are managed automatically and reflect the time of creation and the last update, respectively.
|
|
311
|
+
- Automatic ID Generation: Each entity created gets a unique [id](https://dyna-record.com/classes/default.html#id) generated by the uuidv4 method.
|
|
304
312
|
- Relationship Management: The ORM manages entity relationships through DynamoDB's single-table design patterns, creating and maintaining the necessary links between related entities.
|
|
305
313
|
- Conditional Checks: To ensure data integrity, the create method performs various conditional checks, such as verifying the existence of entities that new records relate to.
|
|
306
314
|
- Error Handling: Errors during the creation process are handled gracefully, with specific errors thrown for different failure scenarios, such as conditional check failures or transaction cancellations.
|
|
@@ -373,8 +381,11 @@ const result = await Customer.query("123", {
|
|
|
373
381
|
|
|
374
382
|
To be more precise to the underlying data, you can specify the partition key and sort key directly. The keys here will be the partition and sort keys defined on the [table](#table) class.
|
|
375
383
|
|
|
376
|
-
```
|
|
377
|
-
const orderLinks = await Customer.query({
|
|
384
|
+
```typescript
|
|
385
|
+
const orderLinks = await Customer.query({
|
|
386
|
+
pk: "Customer#123",
|
|
387
|
+
sk: { $beginsWith: "Order" }
|
|
388
|
+
});
|
|
378
389
|
```
|
|
379
390
|
|
|
380
391
|
### Advanced usage
|
|
@@ -461,7 +472,7 @@ await PaymentMethod.update("123", {
|
|
|
461
472
|
|
|
462
473
|
#### Removing Foreign Key References
|
|
463
474
|
|
|
464
|
-
Nullable
|
|
475
|
+
Nullable foreign key references can be removed by setting them to null
|
|
465
476
|
|
|
466
477
|
Note: Attempting to remove a non nullable foreign key will result in a [NullConstraintViolationError](https://dyna-record.com/classes/NullConstraintViolationError.html)
|
|
467
478
|
|