From 4cdd554fad04244d25f5176fe94b4b9107a5f30a Mon Sep 17 00:00:00 2001 From: wallabyway Date: Fri, 7 Oct 2022 16:22:22 -0700 Subject: [PATCH 01/10] Update index.js --- index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/index.js b/index.js index 60b01d0e5..b5198a2f4 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,11 @@ app.use(express.urlencoded({ extended: true })) // app.use(express.static('public', options)) // ############################################################################# +app.get('/user/:userId', (req, res) => { + req.params; + res.json(req.params); +}); + // Create or Update an item app.post('/:col/:key', async (req, res) => { console.log(req.body) From ee145c2b101defaa69b887bc387ace4aa2482e83 Mon Sep 17 00:00:00 2001 From: wallabyway Date: Sun, 9 Oct 2022 16:52:33 -0700 Subject: [PATCH 02/10] Update index.js --- index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index b5198a2f4..d7c8d32e0 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ const express = require('express') const app = express() -const db = require('cyclic-dynamodb') +const CyclicDb = require('cyclic-dynamodb') +const db = CyclicDb("red-cockroach-tuxCyclicDB"); app.use(express.json()) app.use(express.urlencoded({ extended: true })) @@ -31,7 +32,8 @@ app.post('/:col/:key', async (req, res) => { const col = req.params.col 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) + const coldb = db.collection(col); + const item = await coldb.set(key, req.body) console.log(JSON.stringify(item, null, 2)) res.json(item).end() }) @@ -51,7 +53,8 @@ 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)}`) - const item = await db.collection(col).get(key) + const coldb = db.collection(col); + const item = await coldb.get(key) console.log(JSON.stringify(item, null, 2)) res.json(item).end() }) From 90ba105119cf81c6a7b0c7b649195bbfb8a12ea8 Mon Sep 17 00:00:00 2001 From: wallabyway Date: Sun, 9 Oct 2022 16:56:52 -0700 Subject: [PATCH 03/10] Update index.js --- index.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d7c8d32e0..7acc4bf35 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,6 @@ const express = require('express') const app = express() const CyclicDb = require('cyclic-dynamodb') -const db = CyclicDb("red-cockroach-tuxCyclicDB"); app.use(express.json()) app.use(express.urlencoded({ extended: true })) @@ -20,9 +19,22 @@ app.use(express.urlencoded({ extended: true })) // app.use(express.static('public', options)) // ############################################################################# +async function getstuff() { + + const db = CyclicDb("red-cockroach-tuxCyclicDB") + + const animals = db.collection("animals") + + // get an item at key "leo" from collection animals + let item = await animals.get("leo") + console.log(item) + +} + app.get('/user/:userId', (req, res) => { req.params; res.json(req.params); + await getstuff(); }); // Create or Update an item @@ -52,7 +64,8 @@ app.delete('/:col/:key', async (req, res) => { 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 db = CyclicDb("red-cockroach-tuxCyclicDB"); const coldb = db.collection(col); const item = await coldb.get(key) console.log(JSON.stringify(item, null, 2)) From e479c9ba81b2f3d5d522573946157961a07b9372 Mon Sep 17 00:00:00 2001 From: wallabyway Date: Sun, 9 Oct 2022 16:57:41 -0700 Subject: [PATCH 04/10] Update index.js --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 7acc4bf35..b4013c51f 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,7 @@ app.use(express.urlencoded({ extended: true })) async function getstuff() { - const db = CyclicDb("red-cockroach-tuxCyclicDB") + const db = new CyclicDb("red-cockroach-tuxCyclicDB") const animals = db.collection("animals") @@ -65,7 +65,7 @@ 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)}`); - const db = CyclicDb("red-cockroach-tuxCyclicDB"); + const db = new CyclicDb("red-cockroach-tuxCyclicDB"); const coldb = db.collection(col); const item = await coldb.get(key) console.log(JSON.stringify(item, null, 2)) From 3bf9e09c130417b8ea737bb37b7398607099694d Mon Sep 17 00:00:00 2001 From: wallabyway Date: Sun, 9 Oct 2022 16:58:17 -0700 Subject: [PATCH 05/10] Update index.js --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index b4013c51f..165fdd320 100644 --- a/index.js +++ b/index.js @@ -31,7 +31,7 @@ async function getstuff() { } -app.get('/user/:userId', (req, res) => { +app.get('/user/:userId', async (req, res) => { req.params; res.json(req.params); await getstuff(); From 4da5bbc2e2978079a475b5c53c438787424e3a1a Mon Sep 17 00:00:00 2001 From: wallabyway Date: Sun, 9 Oct 2022 16:58:58 -0700 Subject: [PATCH 06/10] Update index.js --- index.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/index.js b/index.js index 165fdd320..cbeec94aa 100644 --- a/index.js +++ b/index.js @@ -19,22 +19,11 @@ app.use(express.urlencoded({ extended: true })) // app.use(express.static('public', options)) // ############################################################################# -async function getstuff() { - const db = new CyclicDb("red-cockroach-tuxCyclicDB") - - const animals = db.collection("animals") - - // get an item at key "leo" from collection animals - let item = await animals.get("leo") - console.log(item) - -} app.get('/user/:userId', async (req, res) => { req.params; res.json(req.params); - await getstuff(); }); // Create or Update an item From 79a0c1775b3ae298e4ea3688fd79bb7810015f00 Mon Sep 17 00:00:00 2001 From: wallabyway Date: Sun, 9 Oct 2022 18:06:51 -0700 Subject: [PATCH 07/10] fixed to work with latest dynoDB sdk and vsCode --- .vscode/launch.json | 18 ++++++++++++++++++ env.sample | 8 ++++++-- index.js | 20 ++++++++------------ package.json | 2 +- www/index.html | 27 +++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 www/index.html diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..7217ea0cd --- /dev/null +++ b/.vscode/launch.json @@ -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": [ + "/**" + ], + "program": "${workspaceFolder}/index.js", + "envFile": "${workspaceFolder}/.env" + } + ] +} \ No newline at end of file diff --git a/env.sample b/env.sample index fafbde598..b3a099894 100644 --- a/env.sample +++ b/env.sample @@ -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="..." diff --git a/index.js b/index.js index cbeec94aa..0ab5e7044 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ const express = require('express') const app = express() -const CyclicDb = 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 })) @@ -33,9 +34,8 @@ app.post('/:col/:key', async (req, res) => { const col = req.params.col const key = req.params.key console.log(`from collection: ${col} delete key: ${key} with params ${JSON.stringify(req.params)}`) - const coldb = db.collection(col); - const item = await coldb.set(key, req.body) - console.log(JSON.stringify(item, null, 2)) + const item = await db.collection(col).set(key, req.body) + console.log(item) res.json(item).end() }) @@ -45,7 +45,7 @@ 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() }) @@ -54,10 +54,8 @@ 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)}`); - const db = new CyclicDb("red-cockroach-tuxCyclicDB"); - const coldb = db.collection(col); - const item = await coldb.get(key) - console.log(JSON.stringify(item, null, 2)) + const item = await db.collection(col).get(key) + console.log(item) res.json(item).end() }) @@ -71,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 diff --git a/package.json b/package.json index ebb85f5ea..95ec5ba68 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/www/index.html b/www/index.html new file mode 100644 index 000000000..44ec8b25a --- /dev/null +++ b/www/index.html @@ -0,0 +1,27 @@ + +

Starter REST Api

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

Examples

+ +

Create/Update - Insert/Upsert

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

Read All - List

+ +curl -i https://localhost:3000/animals + +

Read

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

Delete

+ +curl -i -XDELETE https://localhost:3000/animals/lassy \ No newline at end of file From ad46f5192b9a652d93b4b7d31c828bc4981797ec Mon Sep 17 00:00:00 2001 From: wallabyway Date: Sun, 9 Oct 2022 18:12:39 -0700 Subject: [PATCH 08/10] fix deploy on cyclic - package lock file bad --- package-lock.json | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 67d8031f4..00d1a1bfd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,10 +5,11 @@ "requires": true, "packages": { "": { + "name": "starter-rest-api", "version": "1.0.0", "license": "ISC", "dependencies": { - "cyclic-dynamodb": "0.0.21", + "cyclic-dynamodb": "0.0.27", "env-cmd": "^10.1.0", "express": "^4.18.1" }, @@ -1401,9 +1402,9 @@ } }, "node_modules/cyclic-dynamodb": { - "version": "0.0.21", - "resolved": "https://registry.npmjs.org/cyclic-dynamodb/-/cyclic-dynamodb-0.0.21.tgz", - "integrity": "sha512-dVfQj4qICFdSjcOrBtku2gQHA645CSz38nEBFI5JNo8Qb5wuyALpHtyG8Yyh6T3lJAAyQ7CPN39dKp4cgjA8uw==", + "version": "0.0.27", + "resolved": "https://registry.npmjs.org/cyclic-dynamodb/-/cyclic-dynamodb-0.0.27.tgz", + "integrity": "sha512-8AqHqaYVzjWn1S/joQf29oDewVCd1PRGdxzMATe6DcL/AH79TOCDbGR/9Bhh8Ljx6RUDMkXcORNT4Dfm+T/QhA==", "dependencies": { "@aws-sdk/client-dynamodb": "3.82.0", "@aws-sdk/lib-dynamodb": "3.82.0" @@ -4071,9 +4072,9 @@ "dev": true }, "cyclic-dynamodb": { - "version": "0.0.21", - "resolved": "https://registry.npmjs.org/cyclic-dynamodb/-/cyclic-dynamodb-0.0.21.tgz", - "integrity": "sha512-dVfQj4qICFdSjcOrBtku2gQHA645CSz38nEBFI5JNo8Qb5wuyALpHtyG8Yyh6T3lJAAyQ7CPN39dKp4cgjA8uw==", + "version": "0.0.27", + "resolved": "https://registry.npmjs.org/cyclic-dynamodb/-/cyclic-dynamodb-0.0.27.tgz", + "integrity": "sha512-8AqHqaYVzjWn1S/joQf29oDewVCd1PRGdxzMATe6DcL/AH79TOCDbGR/9Bhh8Ljx6RUDMkXcORNT4Dfm+T/QhA==", "requires": { "@aws-sdk/client-dynamodb": "3.82.0", "@aws-sdk/lib-dynamodb": "3.82.0" From 205d2f8653ee29741cc6eba49488ecad691664e0 Mon Sep 17 00:00:00 2001 From: wallabyway Date: Sun, 9 Oct 2022 18:18:25 -0700 Subject: [PATCH 09/10] updated index.html with examples --- www/index.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/www/index.html b/www/index.html index 44ec8b25a..ab9205a56 100644 --- a/www/index.html +++ b/www/index.html @@ -16,11 +16,11 @@

Create/Update - Insert/Upsert

Read All - List

-curl -i https://localhost:3000/animals +curl -i https://localhost:3000/animals

Read

-curl -i https://localhost:3000/animals/lassy +curl -i https://localhost:3000/animals/doggie

Delete

From 7e896d82c7e2b2d6f92599b48b496289fc2360f0 Mon Sep 17 00:00:00 2001 From: wallabyway Date: Sun, 9 Oct 2022 18:23:18 -0700 Subject: [PATCH 10/10] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1651d8d99..96aab03bc 100644 --- a/README.md +++ b/README.md @@ -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