Skip to content

Commit

Permalink
fix: replace dynamodb with dynamodb-toolbox v0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
brettstack committed Apr 29, 2020
1 parent c5e12f0 commit 267711a
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 183 deletions.
10 changes: 3 additions & 7 deletions packages/api/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ export function createUser({
id = shortId.generate(),
name,
}) {
return User.create({
return User.put({
id,
name,
})
}

export function getUser({ id }) {
return new Promise((resolve) => User.get({ id }, (error, data) => resolve(data)))
return User.get({ id })
}

export function scanUsers() {
return new Promise((resolve, reject) => User.scan().exec((error, data) => {
console.error(error)
if (error) reject(error)
resolve(data)
}))
return User.scan()
}
5 changes: 2 additions & 3 deletions packages/api/dynamodb-init.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import DynamoDB from 'aws-sdk/clients/dynamodb'
import dynamo from 'dynamodb'

const dynamoDbConfig = {
// region: 'us-east-1',
convertEmptyValues: true,
}
const dynamoDb = new DynamoDB.DocumentClient(dynamoDbConfig)
dynamo.documentClient(dynamoDb)
export const dynamoDbDocumentClient = new DynamoDB.DocumentClient(dynamoDbConfig)
// dynamo.documentClient(dynamoDb)

// dynamo.AWS.config.loadFromPath('credentials.json')
// dynamo.AWS.config.update({
Expand Down
2 changes: 0 additions & 2 deletions packages/api/functions/cognito/post-authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,3 @@ exports.handler = async (event) => {

return event
}

// 12
1 change: 0 additions & 1 deletion packages/api/functions/express/lambda.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'source-map-support/register'
import serverless from 'serverless-http'
import app from './app'
import '../../dynamodb-init'

export const handler = serverless(app, {
request(request, event) {
Expand Down
10 changes: 8 additions & 2 deletions packages/api/functions/express/routes/users.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import express from 'express'
import wrapAsync from '../wrap-async'
import { createUser, scanUsers } from '../../../controllers/user'
import { createUser, scanUsers, getUser } from '../../../controllers/user'

const userRouter = express.Router()
userRouter.get('/', wrapAsync(async (req, res) => {
const users = await scanUsers()
res.json(users)
}))

userRouter.get('/:userId', wrapAsync(async (req, res) => {
const { userId } = req.params
const users = await getUser({ id: userId })
res.json(users)
}))

userRouter.post('/', wrapAsync(async (req, res) => {
const { name } = req.body
const user = await createUser({ name })
res.json(user)
}))

export default userRouter
// 1
25 changes: 13 additions & 12 deletions packages/api/models/User.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import dynamo from 'dynamodb'
import Joi from '@hapi/joi'
import { Table, Entity } from 'dynamodb-toolbox'
import { dynamoDbDocumentClient } from '../dynamodb-init'

const UserTable = new Table({
name: process.env.USER_TABLE,
partitionKey: 'id',
DocumentClient: dynamoDbDocumentClient,
})

const User = dynamo.define('User', {
hashKey: 'id',

// add the timestamp attributes (updatedAt, createdAt)
timestamps: true,

schema: {
id: Joi.string(),
name: Joi.string(),
const User = new Entity({
name: 'User',
attributes: {
id: { partitionKey: true },
name: { type: 'string' },
},
tableName: process.env.USER_TABLE,
table: UserTable,
})

export default User
Loading

0 comments on commit 267711a

Please sign in to comment.