Skip to content
This repository was archived by the owner on Nov 27, 2025. It is now read-only.

Commit c702f6d

Browse files
committed
change _tp to _et and default alias to entity to avoid conflicts
1 parent 1a20995 commit c702f6d

16 files changed

+97
-87
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ One of the most important goals of this library is to be as **unopinionated** as
100100

101101
- **`autoExecute` and `autoParse` are enabled by default.** The original version of this library only handled limited "parameter generation", so it was necessary for you to pass the payloads to the `DocumentClient`. The library now provides support for all API options for each supported method, so by default, it will make the DynamoDB API call and parse the results, saving you redundant code. If you'd rather it didn't do this, you can disable it.
102102
- **It assumes a Single Table DynamoDB design.** Watch the Rick Houlihan videos and read [Alex DeBrie's book](https://www.dynamodbbook.com). The jury is no longer out on this: Single Table designs are what all the cool kids are doing. This library assumes that you will have multiple "Entities" associated with a single "Table", so this requires you to instantiate a `Table` and add at least one `Entity` to it. If you have multiple `Table`s and just one `Entity` type per `Table`, that's fine, it'll still make your life much easier. Also, `batchGet` and `batchWrite` support multiple tables, so we've got you covered.
103-
- **Types are added to all items.** Since this library assumes a Single Table design, it needs a way to reliably distinguish between Entity types. It does this by adding a "Type" field to each item in your table. v0.1 used `__model`, but this has been changed to `_tp` (short for Type). Don't like this? Well, you can either disable it completely (but the library won't be able to parse entities into their aliases for you), or change the attribute name to something more snappy. It is purposefully short to minimize table storage (because item storage size includes the attribute names). Also, by default, Entities will alias this field to `type` (but you can change that too).
103+
- **Types are added to all items.** Since this library assumes a Single Table design, it needs a way to reliably distinguish between Entity types. It does this by adding a "Type" field to each item in your table. v0.1 used `__model`, but this has been changed to `_et` (short for "Entity Type"). Don't like this? Well, you can either disable it completely (but the library won't be able to parse entities into their aliases for you), or change the attribute name to something more snappy. It is purposefully short to minimize table storage (because item storage size includes the attribute names). Also, by default, Entities will alias this field to `entity` (but you can change that too).
104104
- **Created and modified timestamps are enabled by default.** I can't think of many instances where created and modified timestamps aren't used in database records, so the library now automatically adds `_ct` and `_md` attributes when items are `put` or `update`d. Again, these are kept purposefully short. You can disable them, change them, or even implement them yourself if you really want. By default, Entities will alias these attributes to `created` and `modified` (customizable, of course), and will automatically apply an `if_not_exists()` on updates so that the `created` date isn't overwritten.
105105
- **Option names have been shortened using camelCase.** Nothing against long and descriptive names, but typing `ReturnConsumedCapacity` over and over again just seems like extra work. For simplification purposes, all API request parameters have been shortened to things like `capacity`, `consistent` and `metrics`. The documentation shows which parameter they map to, but they should be intuitive enough to guess.
106106
- **All configurations and options are plain JavaScript `objects`.** There are lots of JS libraries that use function chaining (like `table.query('some pk value').condition('some condition').limit(50)`). I really like this style for lots of use cases, but it **just feels wrong** to me when using DynamoDB. DynamoDB is the OG of cloud native databases. It's configured using IaC and its API is HTTP-based and uses structured JSON, so writing queries and other interactions using its native format just seems like the right thing to do. IMO, this makes your code more explicit and easier to reason about. Your `options` could actually be stored as JSON and (unless you're using functions to define defaults on Entity attributes) your Table and Entity configurations could be too.
@@ -324,7 +324,7 @@ const MyEntity = new Table({
324324
| modified | `string` | no | Override default *modified* attribute name (default: `_md`) |
325325
| createdAlias | `string` | no | Override default *created* alias name (default: `created`) |
326326
| modifiedAlias | `string` | no | Override default *modified* alias name (default: `modified`) |
327-
| typeAlias | `string` | no | Override default *entity type* alias name (default: `type`) |
327+
| typeAlias | `string` | no | Override default *entity type* alias name (default: `entity`) |
328328
| attributes | `object` | yes | Complex type that specifies the schema for the entity (see below) |
329329
| autoExecute | `boolean` | no | Enables automatic execution of the DocumentClient method (default: *inherited from Table*) |
330330
| autoParse | `boolean` | no | Enables automatic parsing of returned data when `autoExecute` evaluates to `true` (default: *inherited from Table*) |

__tests__/entity-creation.unit.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('Entity creation', ()=> {
2929
expect(TestEntity.schema.attributes).toHaveProperty('_md')
3030
expect(TestEntity.defaults).toHaveProperty('_ct')
3131
expect(TestEntity.defaults).toHaveProperty('_md')
32-
expect(TestEntity._tpAlias).toBe('type')
32+
expect(TestEntity._etAlias).toBe('entity')
3333
})
3434

3535

@@ -55,7 +55,7 @@ describe('Entity creation', ()=> {
5555
expect(TestEntity.schema.attributes).not.toHaveProperty('_md')
5656
expect(TestEntity.defaults).not.toHaveProperty('_ct')
5757
expect(TestEntity.defaults).not.toHaveProperty('_md')
58-
expect(TestEntity._tpAlias).toBe('type')
58+
expect(TestEntity._etAlias).toBe('entity')
5959
})
6060

6161

@@ -320,10 +320,10 @@ describe('Entity creation', ()=> {
320320
expect(TestTable.name).toBe('test-table')
321321
expect(TestTable.Table.partitionKey).toBe('pk')
322322
expect(TestTable.Table.sortKey).toBeNull()
323-
expect(TestTable.Table.entityField).toBe('_tp')
323+
expect(TestTable.Table.entityField).toBe('_et')
324324
expect(TestTable.Table.indexes).toEqual({})
325325
expect(TestTable.Table.attributes).toEqual({
326-
_tp: { type: 'string' },
326+
_et: { type: 'string' },
327327
pk: { type: 'string', mappings: { TestEnt: { pk: 'string' } } },
328328
_ct: { mappings: { TestEnt: { created: 'string' } } },
329329
_md: { mappings: { TestEnt: { modified: 'string' } } }
@@ -340,7 +340,7 @@ describe('Entity creation', ()=> {
340340
expect(TestEntity.schema.attributes).toHaveProperty('_md')
341341
expect(TestEntity.defaults).toHaveProperty('_ct')
342342
expect(TestEntity.defaults).toHaveProperty('_md')
343-
expect(TestEntity._tpAlias).toBe('type')
343+
expect(TestEntity._etAlias).toBe('entity')
344344
}) // creates entity w/ table
345345

346346
})

__tests__/entity.put.unit.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('put',()=>{
7777

7878
expect(Item.pk).toBe('test-pk')
7979
expect(Item.sk).toBe('test-sk')
80-
expect(Item._tp).toBe('TestEntity')
80+
expect(Item._et).toBe('TestEntity')
8181
expect(Item.test_string).toBe('test string')
8282
expect(Item).toHaveProperty('_ct')
8383
expect(Item).toHaveProperty('_md')
@@ -89,7 +89,7 @@ describe('put',()=>{
8989
expect(Item.pk).toBe('test-pk')
9090
expect(Item.sk).toBe('test-sk')
9191
expect(Item.test_number).toBe(5)
92-
expect(Item._tp).toBe('TestEntity')
92+
expect(Item._et).toBe('TestEntity')
9393
expect(Item.test_string).toBe('test string')
9494
expect(Item).toHaveProperty('_ct')
9595
expect(Item).toHaveProperty('_md')
@@ -100,7 +100,7 @@ describe('put',()=>{
100100

101101
expect(Item.pk).toBe('test-pk')
102102
expect(Item.sk).toBe('test-sk')
103-
expect(Item._tp).toBe('TestEntity')
103+
expect(Item._et).toBe('TestEntity')
104104
expect(Item.test_string).toBe('test string')
105105
expect(Item.test_float).toBe(1.234)
106106
expect(Item.test_float_coerce).toBe(1.234)
@@ -112,7 +112,7 @@ describe('put',()=>{
112112
let { Item } = TestEntity.putParams({ pk: 'test-pk', sk: 'test-sk', test_string: 'different value' })
113113
expect(Item.pk).toBe('test-pk')
114114
expect(Item.sk).toBe('test-sk')
115-
expect(Item._tp).toBe('TestEntity')
115+
expect(Item._et).toBe('TestEntity')
116116
expect(Item.test_string).toBe('different value')
117117
expect(Item).toHaveProperty('_ct')
118118
expect(Item).toHaveProperty('_md')
@@ -276,7 +276,7 @@ describe('put',()=>{
276276
expect(result).toHaveProperty('test-table.PutRequest')
277277
expect(result['test-table'].PutRequest.Item).toHaveProperty('_ct')
278278
expect(result['test-table'].PutRequest.Item).toHaveProperty('_md')
279-
expect(result['test-table'].PutRequest.Item).toHaveProperty('_tp')
279+
expect(result['test-table'].PutRequest.Item).toHaveProperty('_et')
280280
expect(result['test-table'].PutRequest.Item).toHaveProperty('pk')
281281
expect(result['test-table'].PutRequest.Item).toHaveProperty('sk')
282282
expect(result['test-table'].PutRequest.Item).toHaveProperty('test_string')

0 commit comments

Comments
 (0)