npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fastifox

v1.0.15

Published

**Fastifox** is a mini framework for Fastify to facilitate the creation of APIs and CRUD operations with just one file.

Downloads

40

Readme

🦊FastiFox

Fastifox is a mini framework for Fastify to facilitate the creation of APIs and CRUD operations with just one file.

🦊FastiFox

Prerequisites

To use Fastifox, you need to have TypeORM for database operations and Zod for data validation.

Install the necessary dependencies:

npm install fastify typeorm zod

🚀 Installation

Install the package via npm:

npm install fastifox

🛠️ Usage

Setup

Start Fastifox by passing the correct parameters in your application's bootstrap.

  import { FastiFox } from 'fastifox'

  const fastifox = new FastiFox()
  fastifox.initialize({
    directory: './src/**/*.module{.ts,.js}',
    context: {
      datasource: typeormSouce,
      server: fastifyInstance
    }
  })

CRUD Generator

To automatically generate a CRUD, you need to have the TypeORM entity configured and Zod schemas in place if you want to add validations to the routes. Import the CrudGenerator from Fastifox and make the necessary definitions.

import { FastifyInstance } from 'fastify';
import { DataSource } from 'typeorm';
import { CrudGenerator } from 'fastifox'
import { YourEntity } from './entities/YourEntity';
import { z } from 'zod';

class MyCrudGenerator extends CrudGenerator {
  init(){
    this.generate({
      entity: YourEntity,
      route: {
        create: {
          findableFields: ['email'],
          schema: z.object({
            name: z.string(),
            // other fields...
          }),
        },
        update: {
          schema: updateUserSchema,
        },
        listMany: {
          withPagination: true,
          findableFields: ['email', 'name']
        },
        listOne: {
          inputPlugins // plugins to execute on input
        },
        delete: {
          inputPlugins
        }
    }})
  }
}

Using the CrudGenerator and correctly mapping the file at the start of the application will automatically generate the routes.

| Field | Description | | ------------- | ------------- | | inputPlugins | An array of plugins to be executed at the start of the request (route). | | findableFields | The entity fields that should be used to search or validate data. | | withPagination | Enables pagination for routes that list multiple data entries. | | schema | It's the Zod schema object. |

Additional details | Field | Description | | ------------- | ------------- | | searchTerm | Use in GET routes to fetch data. | | fields | Use to select the fields that should be returned in the request. |

Auth JWT (Plugin)

This plugin generates JWT token authentication as well as the Sign in and Sign up routes.

Usage

Instantiate the plugin and add the necessary data for authentication configuration.

  import { JWTAuthPlugin } from "fastify";

  const foxFlowAuth = new JWTAuthPlugin({
    datasource: this.datasource,
    server: this.server,
    entity: User,
  })
  
  foxFlowAuth.register({
    secret: enviroment.jwt.secret
  })

  foxFlowAuth.execute({
    findableField: 'email',
    hashedField: 'password',
    signUp: {
      schema: signUpSchema,
      defaultValues: {
        roleSlug: 'superuser',
      }
    },
    signIn: {
      schema: signInSchema,
      fieldsToken: ['id', 'email', 'roleSlug'],
      returnFields: ['id', 'email', 'roleSlug', 'name'],
    },
    defaultValues: {
      roleSlug: 'superuser'
    }
  })

After the configuration, the /sign-in and /sign-up routes will be generated.

Using the authentication middleware

  // ... other code
    update: {
    schema: updateUserSchema,
    inputPlugins: [
      JWTAuthPlugin.verifyToken()
    ]
  },
  // ... other code

📚 Detailed Explanation

  1. Create an Entity: Define your TypeORM entity.
  2. Generate CRUD Routes: Use CrudGenerator to generate CRUD routes for your entity.
  3. Run the Server: Initialize Fastify and TypeORM, then start the server.

🤝 Contributing

We welcome contributions! Please follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/my-feature).
  3. Make your changes and commit them (git commit -m 'Add some feature').
  4. Push to the branch (git push origin feature/my-feature).
  5. Open a Pull Request.

Setting Up for Development

  1. Clone the repository:

    git clone https://github.com/dotbruu/fastifox
    cd fastifox
  2. Install dependencies:

    npm install
  3. Build the project:

    npm run build
  4. Run tests (if applicable):

    npm test

🙌 Acknowledgments

  • TypeORM: A powerful ORM for TypeScript and JavaScript.
  • Zod: A TypeScript-first schema declaration and validation library.
  • Fastify: A fast and low overhead web framework for Node.js.