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

fast-express-backend

v1.1.4

Published

```bash mkdir HelloWorld cd ./HelloWorld npm install next-leve-backend ```

Downloads

506

Readme

Next Level Backend

Quick Start Guide

Preparing The Envrionment

mkdir HelloWorld
cd ./HelloWorld
npm install next-leve-backend

Next open the the HelloWolrd folder using Visual Studio Code or the code editor you use.

Get up and Running

  1. Create a file called main.js

    Since next level backend is integrated using express js. We will use the same base as the express.

    // import express and it's base components
    // we will use the express to create the server
    // express router will be used to handle views(get,post,put,delete)
    const express = require("express")
    const {Router} = require("express")
    
    // import mongodb and connect it
    const mongoose  = require("mongoose")
    // the url depends on the mongodb instance you are gonna use
    // at the time of writing we are running it locally,
    // but you can connect it to the remote server
    // please read mongoose documentation if you want to know more  
     mongoose.connect("mongodb://localhost:27017")

    Let's stop the main.js fie there.Since this libary contains helper functions and classes to extends the experience of express let's use our first helper function.

  2. Create a file called models.js

    Notice : This file and main.js should in the same directory.It's not a must.But if you want to follow along please do it for now.you can write it in your own way after this

    So create the models.js file .

    
    // import the data class
    const {DataClass} = require("fast-express-backend")
    // a shortcut method
    const {createMongoDBField} = require('fast-express-backend/utils')
    // import some functions validate your data
    const {validators} = require('fast-express-backend/dataclasses/')
    
    
    // Create a class called MovieDataClass
    // Make sure it's extends from fast express backend data class
    // this is also the movie Schema and the model you see in mongoose
    class MovieDataClass extends DataClass {
    
        // you must override the getName functions
        // you must return a unique name for each data class you create
        getName(){
            return "movies"
        }
           
        // now we can describe what our model looks like
    
        // first it has a name
        // so we just give the class an attribute called name
        // then assign the value of createMongoDBField()
        // first argument is the type - String,Number,Boolean,Date
        // then weather the field is unique or not
        // then pass the validators
        // it will check weather the data is correct or not
        // for now don't worry just copy and pase this.
        // when we run the code you will understand everything
        name = createMongoDBField(String,true,[validators.is_required("Name is required")])
    
        // just like that we create a another attribute called date
        date = createMongoDBField(Date,false,[validators.is_required("Date is required")])
    }
    
    module.exports = {MovieDataClass}
    

    Okay that's it. Now you have created a model / schema / dataValidation class from one class.Don't worry for now. We will tell everything.As a startup code this without thinking.

  3. Now go back to the main.js file

    then add these following lines.

    
    
    // import fast express backend components
    const {applyBasicCrud} = require("fast-express-backend")
    
    
    // import the models
    const {MovieDataClass} = require("./models")
    
    // create routers
    const movieRouter = Router()
    applyBasicCrud(movieRouter,MovieDataClass)
    
    // create the base server
    const app = express()
    // make sure it can use json to send and get data
    app.use(express.json())
    // set the routers in the app
    app.use("/movies",movieRouter)
    
    // run the app
    app.listen(8000,() => {
        console.log("app is running on port 8000")
    })
       
  4. Your main.js file should look like this

    
    
    // import express and it's base components
    const express = require("express")
    const {Router} = require("express")
    
    // import mongodb and connect it
    const mongoose  = require("mongoose")
    mongoose.connect("mongodb://localhost:27017")
    
    // import fast express backend components
    const {applyBasicCrud} = require("fast-express-backend")
    
    
    // import the models
    const {MovieDataClass} = require("./models")
    
    // create routers
    const movieRouter = Router()
    applyBasicCrud(movieRouter,MovieDataClass)
    
    // create the base server
    const app = express()
    // make sure it can use json to send and get data
    app.use(express.json())
    // set the routers in the app
    app.use("/movies",movieRouter)
    
    // run the app
    app.listen(8000,() => {
    console.log("app is running on port 8000")
    })
    
       
  5. Last thing is to run this app

    go to your in visual studio code terminal

    node main.js
  6. That's it . Now you have successfully created an app that can get,save,update,and delete movies.Let's test it with postman or whatever rest api tester app you like

    when we send requests we will use the date Date() output just for now. So open the postman create a new post request.First let's test it with no data Postman request with no data

    send the request.you will see an organized error.

    Postman response with no data

    As you can say it says that name must be a string.That's because of auto type checking. When we said in our model Movie has an attribute called name, fast express backend knows that name must be a string and if any other type were to given it will throw an error to the user.

    name = createMongoDBField(String,true,[validators.is_required("Name is required")])

    Now let's pass the name but not the date.Let's see what happen when we try to do it. Postman response with only name

    As you can see it says that date must be a Date. So what's the point here.When you want to save data you don't have to worry about the validation it will be done by the libary.Just give us what the data looks like we will handle everything.

    Now enough with testing.let's create a save our first instance in the database.we will get the date from the javascript Date() function output.because it's a valid date.

    Postman save object

    as you can see it returns an object with id,created_at and ... So it means your model has been saved.

    Note: if you send the same request without changing data you will get an error Postman unique check

    If we recall when we created the name field

    name = createMongoDBField(String,true,[validators.is_required("Name is required")])

    we pass the second argument to be true. it makes the name field unique.

    try the same request with different name you can save a new object then.

    See that's the power of fast express backend.It will use express and it's own features to make things more dynamic and make sure it's safe.

    Take a 5 minute break if you want.😜😜😜😜

    So we saved data.But how we retrive them.That won't take so much time. Just create a another request called GetMovies and just like below send the requets

    Postman get all saved movies

    As you can see , you get all the models you saved via create movie request. How easy is that, only thing we did was create a data class and create a mini router. We will explain these in another sections for now keep in mind dataclass is a class which represent the mongo db model.

    All right now let's see how we can update a model.This requires two things. id of the object and payload you want to change

    for example let's change a date of a movie

    first of all copy an id of a object which we saved. then create new postman get request called GetMovieWithId add a query parameter called id and set it to the id you copied you should get a result like this

    Postman request to get object with id

    So let's change it. Now create a new request called UpdateMovie make sure it's type to be put. In order to update a model you need to pass two arguments

    1. query - base on this data we will find your object Usually it's look like this.

      "query":{_id:"some random object id"}
    2. payload - the data you want to replace

      "payload":{date:"new date"}
    3. So whole object looks like this

      {
          "query":{
              "_id":"some random id"
          },
          "payload":{
              "date":"some new date"
          }
      }

    now let's send the request see what will happen. Postman update request

    it returns nothing.But if you look at the status code it's around 200-203 that means it been saved. for now fast express does not use the general status codes , but soon it will be changed.

    you can check by running the same get movie by id request to verify the updated version of movie.

    Last but not least, let's wrap up this by deleating an object. So let's create another request called delete movie by id. pass the id as a query parameter. note for delete you should name id as id not _id

    Postman delete object with id

    you should not get any content for this either. but it must return 204 status content. which is default for delete response.

    And that's it for the introduction.Let's dive into each sections later.So let's move on to learn what happens behind the scenes.