node-prisma-structure

1.0.2 • Public • Published

node-prisma-structure

The node-prisma-structure npm package is designed to simplify the setup of authentication APIs in your project. It allows you to quickly copy a predefined project structure, install all the required dependencies, and get your project up and running. This package is particularly useful for creating projects with a consistent and reusable authentication system.

npm version License

Features

  • Quickly sets up a predefined project structure for authentication APIs.
  • Automatically installs all required dependencies.
  • Supports Prisma for database handling.
  • Saves time and ensures consistency across projects.
  • Provides a ready-to-run setup.

How to Install

To install the package, run the following command:

  npm i node-prisma-structure

or

  yarn i node-prisma-structure

How to Publish

After installing the package, follow these steps:

  • Run the following command to copy the project structure and API files into your project:
  npx create-prisma-structure
  • Set up your .env file:
    • Add the necessary environment variables required for your project.
    • Ensure the DATABASE_URL for Prisma is properly configured.

How to configure

Generate Prisma client:

Run the following Prisma command to generate the client:

  npx prisma generate

Publish Prisma Database:

Run the following Prisma command to publish database:

  npx prisma migrate dev

Import necessary libraries

In your main run file (e.g., index.js, server.js, app.js, etc.), you need to import the required modules:

// Load environment variables
require("dotenv").config(); // Add this line if not present

// Import necessary libraries
const express = require('express');
const { createDoc } = require('node-api-document');
const apiDoc = require('./node_prisma.api-doc');
const apiPath = require('./modules/v1/api');

Configure Express

Create an instance of express and configure it:

const app = express();

API Configuration

Next, configure the middleware for parsing incoming requests:

// API Config
app.use(express.json()); // Parse JSON request bodies
app.use(express.text()); // Parse text request bodies

Set Up API Routes

Map your API paths to the express application:

// Auth API Path Mapping
app.use('/v1/', apiPath);

Add API Documentation (Optional)

You can also generate API documentation using node-api-document. Set it up like this:

// API Documentation mapping
createDoc(app, 'api-key, token, accept-language, z-user-ip', apiDoc);

Full Code Example

Here is the full code for the main file to use the authentication API:

require("dotenv").config(); // Add this line if not present
const express = require('express');
const { createDoc } = require('node-api-document');
const apiDoc = require('./node_prisma.api-doc');
const apiPath = require('./modules/v1/api');
const app = express();

// API Config
app.use(express.json());  // Parse JSON request bodies
app.use(express.text());  // Parse text request bodies

// Auth API Path Mapping
app.use('/v1/', apiPath);

// API Documentation mapping
createDoc(app, 'api-key, token, accept-language, z-user-ip', apiDoc);

// Start the server
app.listen(process.env.PORT || 3000, () => {
    console.log('Server running on port 3000');
});

Start your project

  • If nodemon is installed, use the following command to start the project:
  nodemon index.js
  • If nodemon is not installed, start the project with:
  node index.js

Notes

  • Ensure that your .env file is properly configured with the correct database URL and API keys for seamless integration.
  • The createDoc function automatically generates API documentation based on your API's main path. Once set up, the documentation can be accessed at the following endpoint: http://localhost:3000/api-doc/.
  • You can customize the port number in the app.listen() method to suit your project’s configuration.

Prerequisites

  • Node.js: Ensure you have Node.js installed on your system with version 20.12.0 or higher.
  • Prisma: Make sure you have Prisma configured for database handling.
  • Environment Variables: Properly configure the .env file as per your project requirements.

Contributing

Feel free to submit issues or pull requests for new features, bug fixes, or general improvements.

🔗 Author Details

portfolio linkedin

License

MIT

/node-prisma-structure/

    Package Sidebar

    Install

    npm i node-prisma-structure

    Weekly Downloads

    0

    Version

    1.0.2

    License

    MIT

    Unpacked Size

    74 kB

    Total Files

    21

    Last publish

    Collaborators

    • tirthgaudani