zmn-aj
v1.0.4
Published
A library for creating customizable MongoDB models with Zod and Mongoose.
Downloads
4
Readme
ZMN-AJ
ZMN-AJ is a Node.js library for creating customizable MongoDB models with Zod and Mongoose. It provides a flexible and robust way to define and validate data models, making it easier to build and maintain Node.js applications with MongoDB.
Features
- Customizable Models: Easily create and customize MongoDB models.
- Validation with Zod: Validate data with Zod schemas to ensure data integrity.
- Mongoose Integration: Seamlessly integrates with Mongoose for MongoDB operations.
Installation
To install the library, run:
npm install zmn-aj
Usage
Importing the Library
Import the required functions from zmn-aj:
const { createUserModel, v, saveUserWithAuth } = require('zmn-aj');
Creating a User Model
Define a custom user model with validation by configuring createUserModel:
const User = createUserModel({
customZodSchema: {
age: v.number().min(18).optional() // Define custom Zod schema for age
},
customMongooseFields: {
age: { type: Number } // Define Mongoose field for age
}
});
module.exports = User;
Creating a Blog Post Model for Blogging App
const BlogPost = createBlogPostModel({
customZodSchema: {
customField: v.string().optional(),
},
customMongooseFields: {
customField: { type: String },
},
});
module.exports = BlogPost;
- with NO Fileds
const BlogPost = createBlogPostModel();
module.exports = BlogPost;
saveUserWithAuth Usage
- To create and save a new user using the User model:
- Create User and save in Database, Then Create Endpoint With saveUserWithAuth Funtion
- Jwt Implementation
- Parameters : req, res, expereinceIn = in days , secret_key from .env file, User from createUserModel Function
import User form './userModel.js'
app.post('/users', (req, res) => {
saveUserWithAuth(req, res, 7 , secret_key, User); // Pass expiresIn as days (1 day)
});
Configuration Options
- customZodSchema: Specify custom Zod schema for validation. For example, age: v.number().min(18).optional() enforces that age must be a number and at least 18 if provided.
- customMongooseFields: Define or override Mongoose fields. For example, age: { type: Number } sets the type of the age field.