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

nodejs-typescript-express-prisma

v1.6.0

Published

A template for Node.js and Express with Prisma setup

Downloads

772

Readme

Node.js Express CRUD Template

This project template provides a Node.js TypeScript setup with Express, Prisma, and jsonwebtoken (JWT) for authentication. It includes an out-of-the-box CRUD setup and allows the generation of additional routes, controllers, and services for new database tables.

Getting Started

To start a new project using this template, run:

npx nodejs-typescript-express-prisma

Setup Instructions

Upon initialization, you will be prompted to provide specific project configurations:

  1. Roles: Specify any custom roles you'd like to add, in addition to the default ADMIN role already configured in the schema. List roles in a comma-separated format, e.g., ROLE1,ROLE2,....

  2. Environment Variables: You will be prompted to set up the following environment variables:

    • PORT: The port number to run the Express server.
    • JWT_SECRET: Secret key for JWT-based authentication.
    • DB_STRING: PostgreSQL connection URI for Prisma.
    • HOST: The root URL for the API.

Default Prisma Schema

The default Prisma schema includes a User model with predefined fields, including a role setup with ADMIN as the default. You can extend this schema according to your project needs.

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DB_STRING")
}

model User {
  id            String         @id @default(cuid())
  name          String?        @db.VarChar(255)
  password      String?        @db.VarChar(255)
  email         String?        @unique @db.VarChar(255)
  phoneNumber   String?        @default("0600000000") @map("phone_number") @db.VarChar(255)
  role          UserRolesEnum? @default(ADMIN)
  createdAt     DateTime       @default(now()) @map("created_at")
  updatedAt     DateTime       @updatedAt @map("updated_at")
  @@map("users")
}

enum UserRolesEnum {
  ADMIN
}

CRUD Generation for New Tables

You can create new tables with corresponding routes, controllers, and services using the command:

yarn crud:generate [table_name] [field:type1,field:type2,...]

After running this command:

  1. Define the schema for the new table in schema.prisma.
  2. Generate the Prisma client with:
    yarn prisma:generate
  3. Apply the new schema changes to your database in development mode:
    yarn prisma:migrate dev

Field Types

The following Joi types are supported for validation:

  • string: string()
  • int: number()
  • date: date()
  • double: number()
  • boolean: boolean()
  • id: string()
  • fid: string()
  • array: array()
  • text: string()
  • object: object()

Route Access

By default, all generated routes require the ADMIN role for access. You can customize access permissions as needed.

Authentication

This template uses JWT for authentication. Ensure you set a strong JWT_SECRET value in the environment variables to secure token generation.

Generating a JWT_SECRET

To generate a secure JWT_SECRET for your environment, use the following command in your terminal:

node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

This will generate a unique hash that can be used as your JWT_SECRET. Copy the output and paste it into your environment variable file under JWT_SECRET.

Additional Resources

For more information, refer to: