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.
- 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.
To install the package, run the following command:
npm i node-prisma-structure
or
yarn i node-prisma-structure
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.
Run the following Prisma command to generate the client:
npx prisma generate
Run the following Prisma command to publish database:
npx prisma migrate dev
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');
Create an instance of express
and configure it:
const app = express();
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
Map your API paths to the express
application:
// Auth API Path Mapping
app.use('/v1/', apiPath);
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);
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');
});
- 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
- 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.
- 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.
Feel free to submit issues or pull requests for new features, bug fixes, or general improvements.