Last active
November 10, 2020 00:43
-
-
Save peterj/6d7d8c2c534aeeaaf0e3ba272cea40e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const app = express(); | |
app.use(bodyParser.json()); | |
const { Region } = require('oracle-nosqldb'); | |
const { ServiceType } = require('oracle-nosqldb/lib/constants'); | |
const NoSQLClient = require('oracle-nosqldb').NoSQLClient; | |
const port = 3000; | |
let client = new NoSQLClient({ | |
region: Region.US_ASHBURN_1, | |
serviceType: ServiceType.CLOUD, | |
compartment: | |
'ocid1.compartment.oc1..example', | |
auth: { | |
iam: { | |
tenantId: 'ocid1.tenancy.oc1..example', | |
userId: 'ocid1.user.oc1..example', | |
fingerprint: '12:34:56:78:90:ab:cd:ef:12:34:56:78:90:ab:cd:ef', | |
passphrase: '', | |
privateKeyFile: 'oci_api_key.pem', | |
}, | |
}, | |
}); | |
const TasksTableName = 'tasks'; | |
// Create a new task | |
app.post('/', async (req, res) => { | |
let { id, title, description, completed } = req.body; | |
if (completed === undefined) { | |
completed = false; | |
} | |
if (!id) { | |
res.status(400).json({ err: 'id cannot be empty' }); | |
return; | |
} | |
try { | |
const result = await client.put(TasksTableName, { | |
id, | |
title, | |
description, | |
completed, | |
}); | |
res.json({ result: result.success}); | |
} catch (err) { | |
console.error('failed to insert data', err); | |
res.status(500).json({ error: err }); | |
} | |
}); | |
// Get all tasks | |
app.get('/', async (req, res) => { | |
try { | |
const result = await client.query(`SELECT * FROM ${TasksTableName}`); | |
res.json(result.rows); | |
} catch (err) { | |
console.error('failed to get data', err); | |
res.status(500).json({ error: err }); | |
} | |
}); | |
// Get a task by id | |
app.get('/:id', async (req, res) => { | |
const { id } = req.params; | |
try { | |
const result = await client.get(TasksTableName, { id }) | |
res.json(result.row); | |
} catch (err) { | |
console.error('failed to get data', err); | |
res.status(500).json({ error: err }); | |
} | |
}); | |
// Get completed tasks | |
app.get('/completed', async (req, res) => { | |
try { | |
const result = await client.query( | |
`SELECT * FROM ${TasksTableName} WHERE completed`, | |
); | |
res.json(result.rows); | |
} catch (err) { | |
console.error('failed to get data', err); | |
res.status(500).json({ error: err }); | |
} | |
}); | |
// Get uncompleted tasks | |
app.get('/uncompleted', async (req, res) => { | |
try { | |
const result = await client.query( | |
`SELECT * FROM ${TasksTableName} WHERE completed=false`, | |
); | |
res.json(result.rows); | |
} catch (err) { | |
console.error('failed to get data', err); | |
res.status(500).json({ error: err }); | |
} | |
}); | |
// Delete a task | |
app.delete('/:id', async (req, res) => { | |
const { id } = req.params; | |
try { | |
const result = await client.delete(TasksTableName, { id }); | |
res.json({ result: result.success}); | |
} catch (err) { | |
console.error('failed to delete data', err); | |
res.status(500).json({ error: err }); | |
} | |
}); | |
app.listen(port, () => { | |
console.log(`Tasks API listening at http://localhost:${port}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment