https://github.com/kdcio/ddb
Lightweight DynamoDB ODM
https://github.com/kdcio/ddb
aws ddb dynamodb kdc kdcio nosql odm
Last synced: about 1 month ago
JSON representation
Lightweight DynamoDB ODM
- Host: GitHub
- URL: https://github.com/kdcio/ddb
- Owner: kdcio
- License: mit
- Created: 2020-08-23T14:52:59.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-08-31T14:01:19.000Z (almost 2 years ago)
- Last Synced: 2025-10-18T23:58:10.760Z (8 months ago)
- Topics: aws, ddb, dynamodb, kdc, kdcio, nosql, odm
- Language: JavaScript
- Homepage:
- Size: 406 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# KDC DDB
A lightweight object modeling for [DynamoDB](https://aws.amazon.com/dynamodb/) inspired by [Mongoose](https://mongoosejs.com/).
The default database operations of this package assumes [single table design](https://www.alexdebrie.com/posts/dynamodb-single-table/) but all can be overridden by writing your own.
[](https://www.npmjs.com/package/@kdcio/ddb) [](https://bundlephobia.com/result?p=@kdcio/ddb) [](https://github.com/kdcio/ddb/actions?query=workflow%3Abuild) [](https://snyk.io/test/github/kdcio/ddb?targetFile=package.json) [](https://sonarcloud.io/dashboard?id=kdcio_ddb) [](https://sonarcloud.io/dashboard?id=kdcio_ddb) [](https://sonarcloud.io/dashboard?id=kdcio_ddb) [](https://github.com/kdcio/ddb/blob/master/LICENSE)
## Installation
```bash
npm i @kdcio/ddb
```
## Quick Start
1. Define database table name in environment variable.
In code:
```js
process.env.DDB_TABLE = 'my-dynamodb-table';
```
or in serverless.yml:
```yaml
provider:
environment:
DDB_TABLE: 'my-dynamodb-table'
```
or in package.json:
```json
"scripts": {
"start": "DDB_TABLE='my-dynamodb-table' node index.js"
}
```
Note that table creation needs to be done outside this package to allow flexibility.
2. Define the fields of the document.
```js
const fields = {
id: { required: true },
name: { required: true },
type: { required: true },
};
```
3. Define primary and secondary keys
```js
const pKey = { pk: '{type}#{id}', sk: 'ANIMAL#{type}' };
const sKey = { pk2: 'ANIMAL', sk2: '{type}#{id}' };
```
4. Create schema
```js
const schema = new DDB.Schema(fields, pKey, sKey);
```
5. Create class
```js
const Animal = DDB.model('Animal', schema);
```
6. Create an instance document from data object
```js
const dog = new Animal({ id: 1, name: 'sparkle', type: 'dog' });
await dog.save();
```
7. Check the tests folder for more examples.
### Debugging
This package uses [debug](https://www.npmjs.com/package/debug).
To enable:
```bash
DEBUG=ddb:* node app.js
```
## Sonar Cloud Badges
[](https://sonarcloud.io/dashboard?id=kdcio_ddb) [](https://sonarcloud.io/dashboard?id=kdcio_ddb) [](https://sonarcloud.io/dashboard?id=kdcio_ddb) [](https://sonarcloud.io/dashboard?id=kdcio_ddb) [](https://sonarcloud.io/dashboard?id=kdcio_ddb) [](https://sonarcloud.io/dashboard?id=kdcio_ddb) [](https://sonarcloud.io/dashboard?id=kdcio_ddb) [](https://sonarcloud.io/dashboard?id=kdcio_ddb) [](https://sonarcloud.io/dashboard?id=kdcio_ddb) [](https://sonarcloud.io/dashboard?id=kdcio_ddb) [](https://sonarcloud.io/dashboard?id=kdcio_ddb)