@bhoos/dynamodb
DynamoDB wrapper
Installation
$ yarn add @bhoos/dynamodb
Usage
Environment Variables:
AWS_REGION
DYNAMODB_ENDPOINT
[optional]
Example (Single partition key)
import { createCollection } from '@bhoos/dynamodb'
const User = createCollection('User', ({ key }) => {
username: key(),
}, ({ self }) => {
get: async (username) => {
const u = await self.findOne(username);
const { password, ...res } = u;
return u;
},
create: async (username, password, name) => self.insert({
username,
name,
password: hash(password),
}),
validate: async (username, password) => {
const u = await self.findOne(user);
if (!u || hash(password) !== u.password) {
throw new Error('Invalid username/password');
}
const { password, ...res } = u;
return res;
},
changePassword: async (username, password) => {
await self.update({ password: hash(password) }, username);
},
deleteUser: async (username) => {
await self.delete(username);
},
});
const email = await User.create('john@doe.com', 'SuperSecret', 'John Doe');
const user = await User.get('john@doe.com');
await User.validate('john@doe.com', 'Wrong');
await User.validate('john@doe.com', 'SuperSecret');
await User.deleteUser('john@doe.com');
await User.insert({ username: 'jane@doe.com', password: 'not-hashed', name: 'Jane Doe' });
await User.findOne('jane@doe.com');
await User.update({ password: 'not-hashed-new' }, 'jane@doe.com');
await User.delete('jane@doe.com');
Example (Multiple keys)
const Movie = createCollection('Movie', ({ key }) => ({
year: key(),
title: key().sort(),
}), ({ self, db }) => ({
get: (year, title) => self.findOne(year, title),
findAnnual: (year) => db.query(.......),
});