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

@agrippa-io/node-generator

v1.0.5

Published

Plop is a scaffolding tool used to build out a file structure that would otherwise take a lot of your time just creating files at the right location with the right naming conventions.

Downloads

22

Readme

Using Plop for Rapid Prototyping

Plop is a scaffolding tool used to build out a file structure that would otherwise take a lot of your time just creating files at the right location with the right naming conventions.

Manually doing so would likely lead to an inconsistent file structure and file naming conventions...and that is just the start.

Here at Agrippa.io, we have setup a number of generators to get you going quickly.

The node-generator assists in creating, naming and placing the appropriate files into the project. They are also used to dynamically generate your routes using snake-case.

The power of Plop doesn't stop there. When the generators execute after all prompts in the CLI have been completed, Plop reads from a series of configuration files to create the appropriate files at the right path with the right name. Plop references each configurition and selects a registered template to use for the generating the contents of the file.

Plop Templates are just handlebar (.hbs) files. These templates are passed the values provided from the series of CLI Prompts and plug them into the templates to dynamically create the content of the output file.

This allows us to not only build out a lot of the boilerplate code, but also create the minimal implementation of almost all the files you will need to create basic CRUD operations.

Our Plop generators can do most of the work to get full CRUD Operations wired up and functioning, but there are things it cannot do.

Plop cannot write your models for you. Here comes your magnificant skills as a Developer. Unleash your awesomeness!

Generator ExpressRest

Creates end-to-end RESTful CRUD Operations for a Model

Let's get started by using Plop to generate a Model and all of its REST endpoints.

In your CLI, navigate to the root of the project directory you are working on and follow the steps below to create our example model Plan:

# Run Plop
npm run generate

# Select the `ExpressRest` generator and hit enter
> ExpressRest

# Enter the model name in PascalCase
> Plan

# Watch the magic of Plops create a series of files. Take note that Test files are 
# also scaffolded :)

All of the routes for CRUD operations will be generated, HOWEVER, they commented out by default to prevent you from exposing an endpoint before it is production ready.

YOU MUST MANUALLY REGISTER THE MODEL ROUTER (src/routes/v1/<model-name>) INTO THE VERSION ROUTER FILE (/src/routes/v1/index.ts)

/src/routes/v1/index.ts

// vvv IMPORT OUR NEW PLAN ROUTER vvv
import PlanRouter from './Plan'


// Authenticated Routes - Models
// vvv REGISTER OUR PLAN ROUTES WITH THE V1 ROUTER TO ACTIVATE ROUTES vvv
RouterV3Models.use('/plans', PlanRouter)

// Leave the folling alone as it registers middleware used for Authentication
// Register v<N> Routes
const RouterV<N> = express.Router()
RouterV1.use('/v1/health', HealthChecker)
RouterV1.use('/v1/auth', AuthenticationRouter)
RouterV1.use(
  '/v1',
  AuthenticateUser,
  RejectUnauthorizeRequest,
  RouterV1Models,
  CatchAllErrors
)

export default RouterV1

Once you have attached the Model Router to the Root Version Router, you can actually start using the CRUD endpoints as the schema is created with a single property name of type string so that you can immediately test that things are wired up.

Try hitting the following endpoints using Postman:

  • POST /v1/plans - with body { name: 'test' }, Creates a new Plan
  • GET /v1/plans - List Plans
  • PUT /v1/plans/:id - Update Plan
  • DELETE /v1/plans/:id - DELETE Plan

NOTE: I have not added the PATCH endpoints to ExpressRest yet...maybe give it a shot?

Now that we have tested that our routes work out of the box, we can start implementing our model-specific logic. Most of this will be done in the appropriate src/model/<model-name directory.

Required File Updates:

  • /src/constants/model.ts - Add the new Model Name to the Models Constants file
  • /src/model/<model-name>/schemaDefinition.js - Write the Model Schema (By default has name prop)
  • /src/model/<model-name>/interfaces/props.ts - Declare props a simple POJO Model Object contains
  • /src/model/<model-name>/interfaces/document.ts - Declare props MongoDB needs to know
  • /src/model/<model-name>/serializer.js - Register the properties that will be returned for Model Requests

Optional File Updates:

  • /src/model/<model-name>/interfaces/model.ts - Declare custom methods created for Mongoose Model
  • /src/model/<model-name>/private.js - Add custom public methods attached to Mongoose Model
  • /src/model/<model-name>/private.js - Add custom private methods attached to Mongoose Model

Leverage the Power-User Features of Mongoose:

  • /src/model/<model-name>/middleware/<custom-middleware>.js - Add custom Mongoose Middleware
  • /src/model/<model-name>/quries/<custom-query>.js - Add custom Mongoose Query Methods
  • /src/model/<model-name>/validators/<validator-name>.js - Add custom Mongoose Validators
  • /src/model/<model-name>/quries/<query-name>.js - Add custom Mongoose Query Methods
  • /src/model/<model-name>/virtuals/<virtual-name>.js - Add Mongoose Model Virtual Methods