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

aratta-express-boilerplate

v1.0.7

Published

Aratta express backend boilerplate

Downloads

8

Readme

aratta-express-boilerplate

This is an express boilerplate which inspired from laravel framework with some simplifications.

Special thanks to Bravebinary for helping us for making this happen you can check out their website through this link:

http://bravebinary.com

Features

  • Easy jwt authentication
  • Routing system
  • MVC pattern
  • Migrations
  • Env
  • Sequelize Orm
  • mySql

Creating the project

Install the aratta-express cli if it isn't already installed

npm i -g @aratta-studios/aratta-express

Then

aratta-express projectName

Select aratta-express-boilerplate

Usage

cd projectName
npm start

Db configuration

Edit config/config.json

Edit env/index.js

Models and migrations

For models and migrations we used sequelize Read more in this link: http://docs.sequelizejs.com/manual/migrations.html Also you can generate models from database using this library https://github.com/sequelize/sequelize-auto

In the index.js in models folder, we connect sequelize with our database and configure this connection Also we link our models to sequelize and specify the associations and relations

You can take a look at this file in the models/index.js and read the comments to learn more

Folder Structure

project name
  /auth ( authentication functions, currently configured for jwt authentication )
  /config (database config for sequelize)
  /controllers (controllers)
  /env (project environment configurations)
  /migrations (sequelize migrations)
  /models (models)
  /routes (connects routes to controller functions)

Controllers

Classes that containing functions that used in routes for getting response data or etc functions are something like this:

import {Doctor} from '../models';
import QueryHelper from "../helpers/queryHelper";

/**
 *
 * @param req
 * @param res
 * @constructor
 */
export function createOrUpdateDoctor(req, res) {
  const body = req.body;
  new QueryHelper(Doctor).createOrUpdate(body, res);
}

/**
 *
 * @param req
 * @param res
 */
export function readAllDoctors(req, res) {
  new QueryHelper(Doctor).find(null, res).all();
}

// and so on ...

Routes

Something like this:

import {createExample, ... , login, signup} from "../controllers/exampleController";

module.exports = function(app){

  app.post('/login', login);
  app.post('/signup', signup);
  app.post('/create-user', createUser);
  app.post('/get-users',isJwtAuthenticated, getUsers);
  app.post('/create-example',isJwtAuthenticated, createExample);
  app.post('/get-examples',isJwtAuthenticated, getExamples);

  //other routes..
};

If you want your route protected by jwt just use isJwtAuthenticated middleware

Query Helper

A class wrote to ease querying and returning data for api process. You can use it this way:

Import (I use this class in my controller) :

import QueryHelper from "../helpers/queryHelper";

Then use it:


// create or update, body keys are your db names and if you have id so it updates otherwise creates a record
export function createOrUpdatModel(req, res) {
  const body = req.body;
  new QueryHelper(Model).createOrUpdate(body, res);
}

response in the api is like:

{
    "type": "success",
    "message": "updated!",
    "data": {
        "id": 4,
        "name": "doctor3",
        "website": "www.doctor3.com",
        "address": "doctor3 address...",
        "phone_number": "+18882223",
        "email": "[email protected]",
        "createdAt": "2019-07-25T17:15:17.000Z",
        "updatedAt": "2019-07-25T17:15:42.115Z"
    }
}

type = success or error message = message of event data = a json data structure if we needed data in our response


// reads all model table contents
/**
 *
 * @param req
 * @param res
 */
export function readAllModel(req, res) {
  const relations = [
     { model: Model1},
     { model: Model2}
  ];
  new QueryHelper(Model).find(null, res,relations).all();
}

// reads all model table contents by pagination use page and pageSize in body to get the data
/**
 * req.body: {page,pageSize}
 * @param req
 * @param res
 */
export function readPaginatedModel(req, res) {
  const body = req.body;
  const relations = [
    { model: Model1},
    { model: Model2}
  ];
  new QueryHelper(Model).find(body, res,relations).paginated();
}

// reads all model table contents by condition use where key in body to get the data
/**
 * req.body: {where}
 * @param req
 * @param res
 */
export function readConditionalModel(req, res) {
  const body = req.body;
  const relations = [
    { model: Model1},
    { model: Model2}
  ];
  new QueryHelper(Model).find(body, res,relations).conditional();
}

Example of where in request body:

{
	"where":{"id":{"$or":[2,3]} }
}

Or

{
	"where":{"id":2, "name" : "doctor"}
}

For Operators you can read in this link: https://sequelize.readthedocs.io/en/latest/docs/querying/#operators

So you can query whatever you want in your request

// read one 
/**
 * req.body: Doctor
 * @param req
 * @param res
 */
export function readOneModel(req, res) {
  const body = req.body;
  const relations = [
     { model: Model1},
     { model: Model2}
  ];
  new QueryHelper(Model).find(body, res,relations).one();
}

// delete conditional

/**
 * req.body: {where}
 * @param req
 * @param res
 */
export function deleteConditionalModel(req, res) {
  const condition = req.body.where;
  new QueryHelper(Model).delete(condition, res);
}

// create

export function createModel(req, res) {
  const body = req.body;
  new QueryHelper(Model).create(body, res);
}

// create without response

export function createWithoutResponseModel(req, res) {
  const body = req.body;
  new QueryHelper(Model).createWithoutResponse(body, res);
}


// update

export function updateModel(req, res) {
  const body = req.body;
  new QueryHelper(Model).update(body, res);
}