Last active
August 2, 2020 09:27
-
-
Save koistya/26b6f0a5d66412f60bee1274be3920a2 to your computer and use it in GitHub Desktop.
Node.js and SQLite for Rapid Prototyping - https://medium.com/p/bc9cf1f26f10
This file contains hidden or 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
import express from 'express'; | |
import db from 'sqlite'; // <= | |
import Promise from 'bluebird'; | |
const app = express(); | |
const port = process.env.PORT || 3000; | |
app.get('/posts', async (req, res, next) => { | |
try { | |
const posts = await db.all('SELECT * FROM Post LIMIT 10'); // <= | |
res.send(posts); | |
} catch (err) { | |
next(err); | |
} | |
}); | |
Promise.resolve() | |
// First, try to open the database | |
.then(() => db.open('./database.sqlite', { Promise })) // <= | |
// Update db schema to the latest version using SQL-based migrations | |
.then(() => db.migrate({ force: 'last' })) // <= | |
// Display error message if something went wrong | |
.catch((err) => console.error(err.stack)) | |
// Finally, launch the Node.js app | |
.finally(() => app.listen(port)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment