Guide to Fastify
Installing Fastify and NaroDB
- Ensure you have Node.js and npm installed.
- Create a new project directory and initialize it:
bash
mkdir fastify-narodb-app
cd fastify-narodb-app
npm init -y
- Install Fastify and NaroDB:
bash
npm install fastify @narodb/naro
Basic Integration
Here's how to integrate NaroDB with Fastify:
Database Configuration
ts
// db.js
import { Naro } from "@narodb/naro";
const db = new Naro("fastifyDatabase");
export default db;
js
// db.js
const { Naro } = require("@narodb/naro");
const db = new Naro("fastifyDatabase");
module.exports = db;
Fastify Server Setup
js
import Fastify from 'fastify'
import db from './db.js'
const fastify = Fastify({
logger: true
})
fastify.get('/', async (request, reply) => {
const newDoc = await db.add("test", { name: "test" })
return newDoc;
})
/**
* Run the server!
*/
const start = async () => {
try {
await fastify.listen({ port: 3000 })
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
In this guide, we explored how to set up and integrate Fastify with NaroDB to create a simple application for managing data.
Official Documentation
For more information, refer to the official Fastify documentation: Fastify
For NaroDB documentation, see the API Reference.