First of all, we have to connect with the database.
const db = require('dynongo');db.connect();
Credentials
Please use IAM roles or environment variables to connect with the dynamodb database. This way, no keys have to
be embedded in your code. You can find more information on the SDK
page.
If you still want to use embedded credentials, you can by providing an accessKeyId, secretAccessKey and an optional region property.
The retry configuration can be passed during initialisation, or per individual query. The mechanism is based on p-retry and requires the same options. Configuring retry will allow the user to automatically retry the DynamoDB operation if it's a retryable error.
You can simply pass a number as well when you don't want to configure the retry strategy.
db.connect({ retries: 3})
DynamoDB Local
It is possible to connect to a local DynamoDB database
by setting the local property to true. It will use port 8000 by default, but if you want to change that port, you can provide a localPort property.
db.connect({ local: true, host: '192.168.5.5', // localhost if not provided localPort: 4444 // 8000 if not provided});
Prefixing tables
It's a good thing to prefix the tables with the name of the project and maybe the environment like production or staging. Instead
of always repeating those names every time you want to query the table, you can provide the prefix and prefix delimiter once. The
default delimiter is the ..
db.connect({ prefix: 'myapp-development', prefixDelimiter: '-' // . if not provided});
Tables
In order for the developer to execute methods on a table, you have to retrieve the table object from the database.
const Employee = db.table('Employee');
The table name will be automatically prefixed by the prefix provided in the connection object.
If you provided a prefix in the connection object but you don't want that for a specific table, you could ask for a raw table. A raw table is like a regular table without the prefix.
const Employee = db.rawTable('Employee');
Methods
Every method can override the retry options passed with the .connect() method or can customise the retry configuration for the specific method.
By default, all reads are eventually consistent which means the response migh include some stale data.
When you request a strongly consistent read, DynamoDB returns a response with the most up-to-date data, reflecting the updates from all prior write operations that were successful.
Dynongo supports strongly consistent reads by adding the .consistent() chaining operator.
The BatchWriteItem operation puts or deletes multiple items in one or more tables. A single call to BatchWrite can write up to 16 MB of data, which can comprise as many as 25 put or delete requests. Individual items to be written can be as large as 400 KB.
You can create Put and Delete request by calling the method on Table with the correct parameters.
The library also supports transactions. Transactions simplify the developer experience of making coordinated, all-or-nothing changes to multiple items both within and across tables. You can only provide up to 10 transaction requests per transaction.
The second way is by calling the createTable() method.
db.createTable(schema).exec() .then(() => { // Table is being created });
This is shorthand for the first method.
Awaiting the result
Creating a table can take a while. The previous examples do not wait for the action to be completed. But there
might be use cases where you have to wait untill the table is created entirely before continuing.
This can be done with the wait() method.
db.createTable(schema).wait().exec() .then(() => { // Table is created });
This will make sure the table is polled every 1000 milliseconds untill the status of the table is active. If you want to poll
at another speed, you can by providing the number of milliseconds in the wait method.
db.createTable(schema).wait(5000).exec();
This will poll the status of the table every 5 seconds instead of every second.
Drop a table
A table can be dropped by either calling drop() on a table instance or by calling dropTable() on the database instance.
The first way is by calling the drop() method.
const Employee = db.table('Employee');Employee.drop().exec() .then(() => { // => Table is being dropped });
The second way is by calling the dropTable() method.
db.dropTable('Employee').exec() .then(() => { // => Table is being dropped })
This method is just a shorthand method for the first example.
Awaiting the result
Dropping a table can take a while, especially when the table has a lot of data. The previous examples do not wait for the action to be completed. But there
might be use cases where you have to wait untill the table is removed entirely before continuing. This can be done with the wait() method.
db.dropTable('Employee').wait().exec() .then(() => { // => Table is dropped })
This will make sure the table is polled every 1000 milliseconds untill the table does not exist anymore. If you want to poll at another speed, you can by providing
the number of milliseconds in the wait method.
db.dropTable('Employee').wait(5000).exec();
This will poll the status of the table every 5 seconds instead of every second.
Related
dynongo-pager - Easy paging for DynamoDB with dynongo