|
| 1 | +# @stenodb/fastify [](https://www.npmjs.org/package/@stenodb/fastify) |
| 2 | + |
| 3 | +> ✍ Easy to use local JSON database for [Fastify](https://fastify.io) |
| 4 | +
|
| 5 | +## Install |
| 6 | + |
| 7 | +```sh |
| 8 | +npm install @stenodb/fastify |
| 9 | +``` |
| 10 | + |
| 11 | +```sh |
| 12 | +yarn add @stenodb/fastify |
| 13 | +``` |
| 14 | + |
| 15 | +```sh |
| 16 | +pnpm add @stenodb/fastify |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +```ts |
| 22 | +// index.ts |
| 23 | +import Fastify from 'fastify' |
| 24 | +import { resolve } from 'node:path' |
| 25 | +import { AsyncAdapter, FastifySteno } from '@stenodb/fastify' |
| 26 | +import { Users, User } from './entities.js' |
| 27 | + |
| 28 | +const fastify = Fastify() |
| 29 | + |
| 30 | +const initialData = new Users( |
| 31 | + new User('John'), |
| 32 | + new User('Alice') |
| 33 | +) |
| 34 | + |
| 35 | +const usersAdapter = new AsyncAdapter( |
| 36 | + 'users', |
| 37 | + Users, |
| 38 | + initialData |
| 39 | +) |
| 40 | + |
| 41 | +fastify.register(FastifySteno, { |
| 42 | + path: resolve(dirname(fileURLToPath(import.meta.url)), '..', 'db'), |
| 43 | + entities: [User, Post], |
| 44 | + adapters: [usersAdapter] |
| 45 | +}) |
| 46 | + |
| 47 | +fastify.get('/users', () => { |
| 48 | + const users = fastify.steno.get<Users>('users') |
| 49 | + return users.data |
| 50 | +}) |
| 51 | + |
| 52 | +fastify.listen({ host: '0.0.0.0', port: 3000 }, (err, address) => { |
| 53 | + if (err) throw err |
| 54 | + console.log(address) |
| 55 | +}) |
| 56 | +``` |
| 57 | + |
| 58 | +## Credits |
| 59 | + |
| 60 | +- [steno](https://github.com/typicode/steno) - Specialized fast async file writer. |
| 61 | +- [fastify-plugin](https://github.com/fastify/fastify-plugin) - Plugin helper for Fastify. |
| 62 | +- [class-transformer](https://github.com/typestack/class-transformer) - Decorator-based transformation, serialization, and deserialization between objects and classes. |
| 63 | +- [class-validator](https://github.com/typestack/class-validator) - Decorator-based property validation for classes. |
| 64 | +- [class-validator-jsonschema](https://github.com/epiphone/class-validator-jsonschema) - Convert `class-validator` decorated classes into JSON schema. |
| 65 | +- [json-difference](https://github.com/lukascivil/json-difference) - A simple way to find the difference between two objects or json diff. |
| 66 | +- [tslog](https://github.com/fullstack-build/tslog) - Universal Logger for TypeScript and JavaScript. |
| 67 | + |
| 68 | +## License |
| 69 | + |
| 70 | +MIT - [crashmax](https://github.com/crashmax-dev) |
0 commit comments