🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

mongoose-sort-encrypted-field

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-sort-encrypted-field

Mongoose plugin to enable sorting on encrypted fields

0.0.9
Source
npm
Version published
Weekly downloads
12
-72.09%
Maintainers
1
Weekly downloads
 
Created
Source

mongoose-sort-encrypted-field

Mongoose plugin to enable sorting on encrypted fields

Install

npm i mongoose-sort-encrypted-field

Example

We are having user with encrypted email, We just need to add sortFieldName option to that field

const { encrypt, decrypt } = require('./encryption.js');
const {
  sortEncryptedFields,
  evaluateMissedSortFields,
} = require('mongoose-sort-encrypted-field');

const userSchema = new mongoose.Schema({
  email: {
    type: String,
    required: true,
    unique: true,
    set: encrypt,
    get: decrypt,
    sortFieldName: 'emailSort',
  },
});

userSchema.plugin(sortEncryptedFields);
const User = mongoose.model('User', userSchema);

// Sometimes some field could be missed 
// Due to server crash or anything 
// So, handling those.
evaluateMissedSortFields(User);

module.exports = User;

Then we can sort all records by email from 'emailSort' field. For proformace we can create mongoDB index for that field.

const sortedUsers = await User.find({}).sort({ emailSort: 1 }).exec();

How does it work?

We create a sort order ID which is just a number in base 2^16, which is huge number system as compared to 10 base number system. We search in DB similar way to binary search but dividing the records into 100 parts intead of 2 parts in binary search. It generate sort order ID in O(1).

Sort order generation is asynchronous using mongoose post middlewares.

Keywords

mongoose

FAQs

Package last updated on 20 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts