Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

work with latest dynamoDB SDK #1

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.js",
"envFile": "${workspaceFolder}/.env"
}
]
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This is an example REST Api designed to be deployed on Cyclic.sh

[![Deploy to Cyclic](https://deploy.cyclic.app/button.svg)](https://deploy.cyclic.app/)

Remember to add your environment variable 'CYCLIC_DB:name_of_my_custom_dynamodb_Table' to the environments variables, under '[Variables](https://app.cyclic.sh)' Tab in the Cyclic.sh console

## Examples

Expand Down
8 changes: 6 additions & 2 deletions env.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
CYCLIC_DB=some-long-app-nameCyclicDB
BUCKET=cyclic-some-long-app-name-id-direction-1
# fill in the ... below. Go to Data/Storage Tab, on your account webpage https://app.cyclic.sh/#/app/
CYCLIC_DB=...CyclicDB
AWS_REGION="us-west-1"
AWS_ACCESS_KEY_ID="..."
AWS_SECRET_ACCESS_KEY="..."
AWS_SESSION_TOKEN="..."
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require('express')
const app = express()
const db = require('cyclic-dynamodb')
const CyclicDb = require('cyclic-dynamodb');
const db = CyclicDb(process.env.CYCLIC_DB);

app.use(express.json())
app.use(express.urlencoded({ extended: true }))
Expand All @@ -19,6 +20,13 @@ app.use(express.urlencoded({ extended: true }))
// app.use(express.static('public', options))
// #############################################################################



app.get('/user/:userId', async (req, res) => {
req.params;
res.json(req.params);
});

// Create or Update an item
app.post('/:col/:key', async (req, res) => {
console.log(req.body)
Expand All @@ -27,7 +35,7 @@ app.post('/:col/:key', async (req, res) => {
const key = req.params.key
console.log(`from collection: ${col} delete key: ${key} with params ${JSON.stringify(req.params)}`)
const item = await db.collection(col).set(key, req.body)
console.log(JSON.stringify(item, null, 2))
console.log(item)
res.json(item).end()
})

Expand All @@ -37,17 +45,17 @@ app.delete('/:col/:key', async (req, res) => {
const key = req.params.key
console.log(`from collection: ${col} delete key: ${key} with params ${JSON.stringify(req.params)}`)
const item = await db.collection(col).delete(key)
console.log(JSON.stringify(item, null, 2))
console.log(item)
res.json(item).end()
})

// Get a single item
app.get('/:col/:key', async (req, res) => {
const col = req.params.col
const key = req.params.key
console.log(`from collection: ${col} get key: ${key} with params ${JSON.stringify(req.params)}`)
console.log(`from collection: ${col} get key: ${key} with params ${JSON.stringify(req.params)}`);
const item = await db.collection(col).get(key)
console.log(JSON.stringify(item, null, 2))
console.log(item)
res.json(item).end()
})

Expand All @@ -61,9 +69,7 @@ app.get('/:col', async (req, res) => {
})

// Catch all handler for all other request.
app.use('*', (req, res) => {
res.json({ msg: 'no route handler found' }).end()
})
app.use(express.static('www'));

// Start the server
const port = process.env.PORT || 3000
Expand Down
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"nodemon": "^2.0.16"
},
"dependencies": {
"cyclic-dynamodb": "0.0.21",
"cyclic-dynamodb": "0.0.27",
"env-cmd": "^10.1.0",
"express": "^4.18.1"
}
Expand Down
27 changes: 27 additions & 0 deletions www/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<style>body {font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;}</style>
<h1>Starter REST Api</h1>

This is an example REST Api designed to be deployed on Cyclic.sh

Deploy to Cyclic

<h2>Examples</h2>

<h3>Create/Update - Insert/Upsert</h3>

curl -i https://localhost:3000/animals/rin%20tin%20tin \
--data '{"breed":"German Shepard", "gender": "male"}' \
-XPOST -H 'Content-Type: application/json'


<h3>Read All - List</h3>

curl -i <a href="https://red-cockroach-tux.cyclic.app/animals">https://localhost:3000/animals</a>

<h3>Read</h3>

curl -i <a href="https://red-cockroach-tux.cyclic.app/animals/doggie">https://localhost:3000/animals/doggie</a>

<h3>Delete</h3>

curl -i -XDELETE https://localhost:3000/animals/lassy