dyna-record 0.0.12 → 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 +23 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -158,7 +158,7 @@ class Student extends MyTable {
|
|
|
158
158
|
|
|
159
159
|
### Foreign Keys
|
|
160
160
|
|
|
161
|
-
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.
|
|
162
162
|
|
|
163
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.
|
|
164
164
|
- Set nullable foreign key attributes as optional for optimal type safety
|
|
@@ -190,12 +190,14 @@ Dyna-Record supports defining relationships between entities such as [@HasOne](h
|
|
|
190
190
|
|
|
191
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)
|
|
192
192
|
|
|
193
|
-
-
|
|
194
|
-
-
|
|
195
|
-
- 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.
|
|
196
196
|
|
|
197
197
|
#### HasOne
|
|
198
198
|
|
|
199
|
+
[Docs](https://dyna-record.com/functions/HasOne.html)
|
|
200
|
+
|
|
199
201
|
```typescript
|
|
200
202
|
@Entity
|
|
201
203
|
class Assignment extends MyTable {
|
|
@@ -209,7 +211,7 @@ class Grade extends MyTable {
|
|
|
209
211
|
@ForeignKeyAttribute()
|
|
210
212
|
public readonly assignmentId: ForeignKey;
|
|
211
213
|
|
|
212
|
-
// 'assignmentId' Must be defined on self as ForeignKey
|
|
214
|
+
// 'assignmentId' Must be defined on self as ForeignKey or NullableForeignKey
|
|
213
215
|
@BelongsTo(() => Assignment, { foreignKey: "assignmentId" })
|
|
214
216
|
public readonly assignment: Assignment;
|
|
215
217
|
}
|
|
@@ -217,10 +219,12 @@ class Grade extends MyTable {
|
|
|
217
219
|
|
|
218
220
|
### HasMany
|
|
219
221
|
|
|
222
|
+
[Docs](https://dyna-record.com/functions/HasMany.html)
|
|
223
|
+
|
|
220
224
|
```typescript
|
|
221
225
|
@Entity
|
|
222
226
|
class Teacher extends MyTable {
|
|
223
|
-
// 'teacherId'
|
|
227
|
+
// 'teacherId' must be defined on associated model
|
|
224
228
|
@HasMany(() => Course, { foreignKey: "teacherId" })
|
|
225
229
|
public readonly courses: Course[];
|
|
226
230
|
}
|
|
@@ -230,6 +234,7 @@ class Course extends MyTable {
|
|
|
230
234
|
@NullableForeignKeyAttribute()
|
|
231
235
|
public readonly teacherId?: NullableForeignKey;
|
|
232
236
|
|
|
237
|
+
// 'teacherId' Must be defined on self as ForeignKey or NullableForeignKey
|
|
233
238
|
@BelongsTo(() => Teacher, { foreignKey: "teacherId" })
|
|
234
239
|
public readonly teacher?: Teacher;
|
|
235
240
|
}
|
|
@@ -237,6 +242,8 @@ class Course extends MyTable {
|
|
|
237
242
|
|
|
238
243
|
### HasAndBelongsToMany
|
|
239
244
|
|
|
245
|
+
[Docs](https://dyna-record.com/functions/HasAndBelongsToMany.html)
|
|
246
|
+
|
|
240
247
|
HasAndBelongsToMany relationships require a [JoinTable](https://dyna-record.com/classes/JoinTable.html) class. This represents a virtual table to support the relationship
|
|
241
248
|
|
|
242
249
|
```typescript
|
|
@@ -270,7 +277,7 @@ class Student extends OtherTable {
|
|
|
270
277
|
|
|
271
278
|
[Docs](https://dyna-record.com/classes/default.html#create)
|
|
272
279
|
|
|
273
|
-
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
|
|
274
281
|
|
|
275
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.
|
|
276
283
|
|
|
@@ -296,12 +303,12 @@ const grade: Grade = await Grade.create({
|
|
|
296
303
|
|
|
297
304
|
#### Error handling
|
|
298
305
|
|
|
299
|
-
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`.
|
|
300
307
|
|
|
301
308
|
#### Notes
|
|
302
309
|
|
|
303
|
-
- Automatic Timestamp Management: The createdAt and updatedAt fields are managed automatically and reflect the time of creation and the last update, respectively.
|
|
304
|
-
- 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.
|
|
305
312
|
- Relationship Management: The ORM manages entity relationships through DynamoDB's single-table design patterns, creating and maintaining the necessary links between related entities.
|
|
306
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.
|
|
307
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.
|
|
@@ -374,8 +381,11 @@ const result = await Customer.query("123", {
|
|
|
374
381
|
|
|
375
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.
|
|
376
383
|
|
|
377
|
-
```
|
|
378
|
-
const orderLinks = await Customer.query({
|
|
384
|
+
```typescript
|
|
385
|
+
const orderLinks = await Customer.query({
|
|
386
|
+
pk: "Customer#123",
|
|
387
|
+
sk: { $beginsWith: "Order" }
|
|
388
|
+
});
|
|
379
389
|
```
|
|
380
390
|
|
|
381
391
|
### Advanced usage
|
|
@@ -462,7 +472,7 @@ await PaymentMethod.update("123", {
|
|
|
462
472
|
|
|
463
473
|
#### Removing Foreign Key References
|
|
464
474
|
|
|
465
|
-
Nullable
|
|
475
|
+
Nullable foreign key references can be removed by setting them to null
|
|
466
476
|
|
|
467
477
|
Note: Attempting to remove a non nullable foreign key will result in a [NullConstraintViolationError](https://dyna-record.com/classes/NullConstraintViolationError.html)
|
|
468
478
|
|