Skip to content
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
88 changes: 24 additions & 64 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,32 @@
const express = require('express')
const app = express()
const db = require('@cyclic.sh/dynamodb')
const { ApolloServer } = require('@apollo/server');
const { startStandaloneServer } = require('@apollo/server/standalone');

app.use(express.json())
app.use(express.urlencoded({ extended: true }))
const resolvers = require('./resolvers')
const typeDefs = require('./schema');

// #############################################################################
// This configures static hosting for files in /public that have the extensions
// listed in the array.
// var options = {
// dotfiles: 'ignore',
// etag: false,
// extensions: ['htm', 'html','css','js','ico','jpg','jpeg','png','svg'],
// index: ['index.html'],
// maxAge: '1m',
// redirect: false
// }
// app.use(express.static('public', options))
// #############################################################################
async function startApolloServer() {
const server = new ApolloServer({
typeDefs,
resolvers
});

// Create or Update an item
app.post('/:col/:key', async (req, res) => {
console.log(req.body)
const { url } = await startStandaloneServer(server, {
context: () => {
return {
dataSources : {
db: db
}
}
}
});

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)
console.log(JSON.stringify(item, null, 2))
res.json(item).end()
})
console.log(`
🚀 Server is running
📭 Query at ${url}
`);
}

// Delete an item
app.delete('/: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).delete(key)
console.log(JSON.stringify(item, null, 2))
res.json(item).end()
})
startApolloServer();

// 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)}`)
const item = await db.collection(col).get(key)
console.log(JSON.stringify(item, null, 2))
res.json(item).end()
})

// Get a full listing
app.get('/:col', async (req, res) => {
const col = req.params.col
console.log(`list collection: ${col} with params: ${JSON.stringify(req.params)}`)
const items = await db.collection(col).list()
console.log(JSON.stringify(items, null, 2))
res.json(items).end()
})

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

// Start the server
const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`index.js listening on ${port}`)
})
Loading