sequelize-paginate-helper
v1.0.1
Published
A Sequelize pagination helper...
Maintainers
Readme
Step 1: Prepare Your Package
Create a dedicated directory: If you haven't already, ensure this code is in its own directory (e.g.,
sequelize-paginate-helper).Initialize npm: Navigate to this directory in your terminal and run:
npm initFollow the prompts. For the package name, you might want something like
sequelize-paginate-helper. Keep the entry point as the file containing yourpaginatefunction (e.g.,index.js).Create
index.js: If you haven't already, save the code you provided in a file namedindex.js(or whatever you specified as the entry point duringnpm init).Add a
package.json: Thenpm initcommand will create this file. Review it to ensure the information is correct. You might want to add keywords likesequelize,pagination,helper.Add a
LICENSEfile: Create aLICENSEfile in your project root. A common choice is the MIT license. You can find the text online.Create a
README.md: This file will explain how to use your package on npm. Include:- Your package name (
sequelize-paginate-helper). - A short description.
- Installation instructions (
npm install sequelize-paginate-helper). - Usage examples showing how to import and use the
paginatefunction with a Sequelize model. - API documentation explaining the parameters and the return value of the
paginatefunction. - License information.
Here's a basic example for your
README.md:# sequelize-paginate-helper A Sequelize pagination helper with advanced filtering and ordering. ## Installation ```bash npm install sequelize-paginate-helperUsage
const { Sequelize, DataTypes, Op } = require('sequelize'); const paginate = require('sequelize-paginate-helper'); const sequelize = new Sequelize('sqlite::memory:'); const User = sequelize.define('User', { name: DataTypes.STRING, email: DataTypes.STRING, }); async function main() { await sequelize.sync(); await User.bulkCreate([ { name: 'Alice', email: '[email protected]' }, { name: 'Bob', email: '[email protected]' }, { name: 'Charlie', email: '[email protected]' }, ]); const results = await paginate( User, 1, // pageSize 10, // pageLimit 'name', // order_by 'ASC', // order_direction ['id', 'name'], // attributes {}, // where [ // filters { filterKey: 'name', operation: 'like', value: 'A' }, ], [], // includes 'id' // distinctColumn ); console.log(results); } main();API
paginate(model, pageSize, pageLimit, order_by, order_direction, attributes, where, filters, includes, distinctColumn)Asynchronously paginates a Sequelize model query with filtering and ordering.
Parameters:
model: The Sequelize model.pageSize: The current page number (integer).pageLimit: The number of records per page (integer).order_by: The field to order by (string, default: 'id'). Can also be an object for included models.order_direction: The order direction ('ASC' or 'DESC').attributes(optional): An array of attributes to select.where(optional): A Sequelizewhereclause object.filters(optional): An array of filter objects withfilterKey,operation(Sequelize Op), andvalue.includes(optional): An array of Sequelize include options.distinctColumn(optional): The column to use forDISTINCT.
Returns:
An object containing pagination metadata and the data.
License
MIT
Author
[Vivek Gupta] ([[email protected]/IC Rudra])
- Your package name (
Step 2: Publish to npm
Sign up for npm: If you don't have an account, go to https://www.npmjs.com/signup and create one.
Log in to npm in your terminal:
npm loginEnter your npm username, password, and email.
Publish your package:
npm publishMake sure you are in the root directory of your package (the one containing
package.json).Verify: After successful publishing, you should be able to find your package on the npm website: https://www.npmjs.com/package/sequelize-paginate-helper (or whatever name you chose).
That's it! You've published your Sequelize pagination helper. Let me know if you encounter any issues or have further questions. It's Sunday afternoon here in Noida. What are your plans for the rest of the day?
