1.0.19 • Published 5 months ago
dynamodb-eloquent v1.0.19
DynamoDB Eloquent
DynamoDB Eloquent is an ORM that can run in NodeJS, and can be used with TypeScript and JavaScript (ES5, ES6, ES7, ES8).
Features
Quick Start
npm i dynamodb-eloquent
Config AWS credentials
Using environment variables
# .env
DDB_ENDPOINT=
AWS_REGION=
## way 1: using aws profile
AWS_PROFILE=
## way 2: using aws access key
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
Dynamically set credentials
ddbRepo.setConfig({
accessKeyId: 'AKIAxxx',
secretAccessKey: 'xxxxxx',
region: 'ap-northeast-1',
});
ddbRepo.setConfig({
profile: 'your-aws-profile',
region: 'ap-northeast-1',
});
Usage
Basic query
import { DynamoDBRepository } from "dynamodb-eloquent";
export class UserRepository extends DynamoDBRepository {
protected table = `Users`;
// Please put all indexes you have here
protected mappingIndex = {
email: "email",
};
}
const userRepository = new UserRepository();
// Create
await userRepository.create({
id: 1,
name: "Bill",
});
// Update
await userRepository.update({
id: 1,
name: "John",
});
// List
await userRepository.findAll();
// Show
await userRepository.findOrFail(userId);
// Delete
await userRepository.delete(userId);
// Delete
await userRepository.delete(userId);
Filter
findBy
findBy
must use with index
// Filter
const params = { email: "abc@example.com" };
const indexName = "email";
const posts = await userRepository.findBy(params, indexName);
findBy
findBy
must includes the index
const params = { email: "abc@example.com" };
const indexName = "email";
const users = await userRepository.findBy(params, indexName);
console.log(users.length)
findOneBy
findOneBy
must includes the index
const params = { email: "bill001@example.com" };
const indexName = "email";
const user = await userRepository.findOneBy(params, indexName);
console.log(user.id)
ScanData
const where = {
category: 'Clothes',
// additional conditions
};
const page1 = await postRepository.scanDataV2({ filter: where });
const page2 = await postRepository.scanDataV2({ filter: where, nextKey: page1.nextKey });
paginate
const nextKey = undefined; // put nextKey here
const limit = 10;
const params = {
limit,
nextKey,
scanIndexForward: false,
};
const posts = await postRepository.paginate(params);
// result
{
data: [
{ id: 'aa03e3a0-a9c8-439f-9b22-362ea59fc0ec', email: 'bill001@example.com'},
{ id: '527a850f-0bfc-4da5-84d3-8a03c451e868', email: 'bill002@example.com'},
...
{ id: '527a850f-0bfc-4da5-84d3-8a03c451e868', email: 'bill010@example.com'},
],
pagination: {
nextKey: 'eyJpZCI6ImFlMWRmYTRjLTM1NTUtNDM0ZC05NWU1LWFkZWVhMjllZDE1OCJ9',
count: 10,
perPage: 10
}
}
paginateV2
const page1 = await postRepository.paginateV2({ limit: 10 });
const page2 = await postRepository.paginateV2({ limit: 10, nextKey: page1.pagination.nextKey });
const params = {
limit,
scanIndexForward: false,
where: {
shortDescription: `shortDescription7`,
},
};
const posts = await postRepository.paginateV2(params);
Migrations
Update package.json
"scripts": {
"migration:create": "dynamodb_eloquent migration:create",
"migration:run": "dynamodb_eloquent migration:run",
"migration:revert": "dynamodb_eloquent migration:revert",
}
Run commands
# For local
# export AWS_REGION="ap-northeast-1"
# export DDB_ENDPOINT="http://localhost:8000"
# Generate new migration
yarn migration:create --table Posts
# Run migrations
yarn migration:run
# Or yarn migration:run --tableSuffix test
# revert migrations
yarn migration:revert
# or yarn migration:revert --tableSuffix test
1.0.19
5 months ago
1.0.18
5 months ago
1.0.17
5 months ago
1.0.16
6 months ago
1.0.15
6 months ago
1.0.14
6 months ago
1.0.13
7 months ago
1.0.12
7 months ago
1.0.9
7 months ago
1.0.11
7 months ago
1.0.10
7 months ago
1.0.8
9 months ago
1.0.7
11 months ago
1.0.6
11 months ago
1.0.5
11 months ago
1.0.4
11 months ago
1.0.3
11 months ago
1.0.2
11 months ago
1.0.1
11 months ago