mongoose-sort-encrypted-field
Advanced tools
Comparing version 0.4.4 to 0.9.0
@@ -18,4 +18,4 @@ "use strict"; | ||
selectSortFields: false, | ||
noOfBytesForSortId: 50, | ||
noOfBytesToIncreaseOnSaturation: 2, | ||
noOfCharsForSortId: 50, | ||
noOfCharsToIncreaseOnSaturation: 2, | ||
revaluateAllThreshold: 0.5, | ||
@@ -22,0 +22,0 @@ revaluateAllCountThreshold: 100, |
@@ -28,3 +28,3 @@ "use strict"; | ||
[field.options.sortFieldName]: { | ||
type: Buffer, | ||
type: String, | ||
default: null, | ||
@@ -31,0 +31,0 @@ select: selectSortFields, |
@@ -1,2 +0,1 @@ | ||
/// <reference types="node" /> | ||
declare class SortIdManager { | ||
@@ -8,7 +7,8 @@ model: any; | ||
ignoreCases: boolean; | ||
noOfBytesForSortId: number; | ||
noOfBytesToIncreaseOnSaturation: number; | ||
noOfCharsForSortId: number; | ||
noOfCharsToIncreaseOnSaturation: number; | ||
base: number; | ||
constructor(model: any, fieldName: any, sortFieldName: any); | ||
getDocument(skip: any): Promise<any>; | ||
getAverageSortId(predecessorSortId: Buffer, successorSortId: Buffer): any; | ||
getAverageSortId(predecessorSortId: any, successorSortId: any): any; | ||
generateSortIdUsingBinarySearch(fieldValue: any): Promise<any>; | ||
@@ -15,0 +15,0 @@ updateSortFieldsForDocument(objectId: any, fieldValue: any): Promise<void>; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SortIdManager = void 0; | ||
const { add, shift } = require("math-buffer"); | ||
const base_2_n_1 = __importDefault(require("@navpreetdevpuri/base-2-n")); | ||
class SortIdManager { | ||
constructor(model, fieldName, sortFieldName) { | ||
this.base = 15; | ||
this.model = model; | ||
this.fieldName = fieldName; | ||
this.sortFieldName = sortFieldName; | ||
const { silent, ignoreCases, noOfBytesForSortId, noOfBytesToIncreaseOnSaturation } = model.schema.options.sortEncryptedFieldsOptions; | ||
const { silent, ignoreCases, noOfCharsForSortId, noOfCharsToIncreaseOnSaturation } = model.schema.options.sortEncryptedFieldsOptions; | ||
this.silent = silent; | ||
this.ignoreCases = ignoreCases; | ||
this.noOfBytesForSortId = noOfBytesForSortId; | ||
this.noOfBytesToIncreaseOnSaturation = noOfBytesToIncreaseOnSaturation; | ||
this.noOfCharsForSortId = noOfCharsForSortId; | ||
this.noOfCharsToIncreaseOnSaturation = noOfCharsToIncreaseOnSaturation; | ||
} | ||
@@ -25,30 +29,28 @@ async getDocument(skip) { | ||
if (!predecessorSortId) { | ||
predecessorSortId = Buffer.from("".padEnd(2 * successorSortId.length, "0"), "hex"); | ||
predecessorSortId = "".padEnd(successorSortId.length, "\0"); | ||
} | ||
if (!successorSortId) { | ||
successorSortId = Buffer.from("".padEnd(2 * predecessorSortId.length, "f"), "hex"); | ||
successorSortId = "".padEnd(predecessorSortId.length, "\u7FFF"); | ||
} | ||
predecessorSortId.reverse(); | ||
successorSortId.reverse(); | ||
let predecessorNumber; | ||
let successorNumber; | ||
if (predecessorSortId.length === successorSortId.length) { | ||
if (add(predecessorSortId, Buffer.from([0x01])).equals(successorSortId)) { | ||
predecessorSortId = Buffer.concat([Buffer.from("".padEnd(2 * this.noOfBytesToIncreaseOnSaturation, "0"), "hex"), predecessorSortId]); | ||
successorSortId = Buffer.concat([Buffer.from("".padEnd(2 * this.noOfBytesToIncreaseOnSaturation, "0"), "hex"), successorSortId]); | ||
predecessorNumber = new base_2_n_1.default(predecessorSortId, this.base); | ||
successorNumber = new base_2_n_1.default(successorSortId, this.base); | ||
let averageNumber = predecessorNumber.average(successorNumber); | ||
if (averageNumber.toString() !== predecessorNumber.toString()) { | ||
return averageNumber.toString(); | ||
} | ||
let averageSortId = add(shift(predecessorSortId, -1), shift(successorSortId, -1)); | ||
if (averageSortId[averageSortId.length - 1] === 0 && averageSortId.length > predecessorSortId.length) { | ||
averageSortId = averageSortId.slice(0, averageSortId.length - 1); | ||
} | ||
averageSortId.reverse(); | ||
return averageSortId; | ||
const newSize = averageNumber.length + this.noOfCharsToIncreaseOnSaturation; | ||
predecessorNumber = new base_2_n_1.default(predecessorSortId.padEnd(newSize, "\0"), this.base); | ||
successorNumber = new base_2_n_1.default(successorSortId.padEnd(newSize, "\0"), this.base); | ||
averageNumber = predecessorNumber.average(successorNumber); | ||
return averageNumber.toString(); | ||
} | ||
const biggerSortId = predecessorSortId.length > successorSortId.length ? predecessorSortId : successorSortId; | ||
let smallerSortId = successorSortId.length > predecessorSortId.length ? predecessorSortId : successorSortId; | ||
smallerSortId = Buffer.concat([Buffer.from("".padEnd(2 * (biggerSortId.length - smallerSortId.length), "0"), "hex"), smallerSortId]); | ||
let averageSortId = add(shift(predecessorSortId, -1), shift(successorSortId, -1)); | ||
if (averageSortId[averageSortId.length - 1] === 0 && averageSortId.length > biggerSortId.length) { | ||
averageSortId = averageSortId.slice(0, averageSortId.length - 1); | ||
} | ||
averageSortId.reverse(); | ||
return averageSortId; | ||
const bigger = predecessorSortId.length > successorSortId.length ? predecessorSortId : successorSortId; | ||
const smaller = successorSortId.length > predecessorSortId.length ? predecessorSortId : successorSortId; | ||
const biggerNumber = new base_2_n_1.default(bigger, this.base); | ||
const smallerNumber = new base_2_n_1.default(smaller.padEnd(bigger.length, "\0"), this.base); | ||
const averageNumber = biggerNumber.average(smallerNumber); | ||
return averageNumber.toString(); | ||
} | ||
@@ -63,4 +65,4 @@ async generateSortIdUsingBinarySearch(fieldValue) { | ||
if (n === 0) { | ||
const predecessorSortId = Buffer.from("".padEnd(2 * this.noOfBytesForSortId, "0"), "hex"); | ||
const successorSortId = Buffer.from("".padEnd(2 * this.noOfBytesForSortId, "f"), "hex"); | ||
const predecessorSortId = "".padEnd(this.noOfCharsForSortId, "\0"); | ||
const successorSortId = "".padEnd(this.noOfCharsForSortId, "\u7FFF"); | ||
return this.getAverageSortId(predecessorSortId, successorSortId); | ||
@@ -137,15 +139,12 @@ } | ||
const log2n = Math.round(Math.log2(n)) + 1; | ||
let diff = Buffer.from("".padEnd(2 * this.noOfBytesForSortId, "f"), "hex"); | ||
let diff = new base_2_n_1.default("".padEnd(this.noOfCharsForSortId, "\u7FFF"), this.base); | ||
for (let i = 0; i < log2n; i += 1) { | ||
diff = shift(diff, -1); | ||
diff = diff.half(); | ||
} | ||
let curr = Buffer.from("".padEnd(2 * this.noOfBytesForSortId, "0"), "hex"); | ||
let curr = new base_2_n_1.default("".padEnd(this.noOfCharsForSortId, "\0"), this.base); | ||
for (let i = 0; i < n; i += 1) { | ||
if (i === 0 || documents[i - 1][this.fieldName] !== documents[i][this.fieldName]) { | ||
curr = add(curr, diff); | ||
if (curr[curr.length - 1] === 0 && curr.length > diff.length) { | ||
curr = curr.slice(0, curr.length - 1); | ||
} | ||
curr = curr.add(diff); | ||
} | ||
await this.model.updateOne({ _id: documents[i]._id }, { $set: { [this.sortFieldName]: Buffer.from(curr).reverse() } }); | ||
await this.model.updateOne({ _id: documents[i]._id }, { $set: { [this.sortFieldName]: curr.toString() } }); | ||
} | ||
@@ -152,0 +151,0 @@ if (!this.silent) { |
{ | ||
"name": "mongoose-sort-encrypted-field", | ||
"version": "0.4.4", | ||
"version": "0.9.0", | ||
"description": "Mongoose plugin to enable sorting on encrypted fields", | ||
@@ -30,4 +30,4 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@navpreetdevpuri/base-2-n": "^2.0.1", | ||
"ioredis": "^5.3.1", | ||
"math-buffer": "^0.1.1", | ||
"redis-ordered-queue": "^1.4.2" | ||
@@ -34,0 +34,0 @@ }, |
@@ -90,10 +90,10 @@ # mongoose-sort-encrypted-field | ||
2. `noOfBytesForSortId?: number` default: `50` | ||
2. `noOfCharsForSortId?: number` default: `50` | ||
Number of bytes for sort ID, bigger number is mathematically better. | ||
3. `noOfBytesToIncreaseOnSaturation?: number;` default: `2` <br> | ||
3. `noOfCharsToIncreaseOnSaturation?: number;` default: `2` <br> | ||
Number of bytes to increase on saturation, for example, | ||
for `04` and `05`, first, we can see there is no whole number between those | ||
so, It appends an extra digit at the end and it becomes `040` and `050` and the average is `045`. | ||
In the base `2^8` number system, getting a saturation like that is mathematically very unlikely. | ||
In the base `2^15` number system, getting a saturation like that is mathematically very unlikely. | ||
@@ -111,3 +111,3 @@ 4. `ignoreCases?: boolean;` default: `false` <br> | ||
If the number of documents without sort ID divided by the total number of documents is less than this threshold | ||
Then it will get all values, sort them, and generate sort ID for all at equal distances 0 to 2^8 | ||
Then it will get all values, sort them, and generate sort ID for all at equal distances 0 to 2^15 | ||
For example, if we have 3 documents and we can 00 to 20 sort ID | ||
@@ -122,6 +122,6 @@ then those documents will have 05 10 15 sort ID | ||
We create a sort order ID which is just a number in base `2^8`, which is a huge number system as compared to the 10 base number system. We search in DB using binary search. For `1 lakh` documents, it queries and decrypts only `18` documents (first+last+log(1lakh)) to generate a sort ID. It generates a sort order ID in `O(1)`. | ||
We create a sort order ID which is just a number in base `2^15`, which is a huge number system as compared to the 10 base number system. We search in DB using binary search. For `1 lakh` documents, it queries and decrypts only `18` documents (first+last+log(1lakh)) to generate a sort ID. It generates a sort order ID in `O(1)`. | ||
To generate a sort order ID it only needs to know the previous and next sort ID, and it just averages out those to get the current sort order ID, for example in the base 10 system if need to insert between `03` and `07` then `(03+07)/02` which is `05`. for `04` and `05`, first we can see there is no whole number between those so, It append extra digit at the end and it becomes `040` and `050` and the average is `045`. In the base `2^8` number system, getting a saturation like that is mathematically very unlikely. | ||
To generate a sort order ID it only needs to know the previous and next sort ID, and it just averages out those to get the current sort order ID, for example in the base 10 system if need to insert between `03` and `07` then `(03+07)/02` which is `05`. for `04` and `05`, first we can see there is no whole number between those so, It append extra digit at the end and it becomes `040` and `050` and the average is `045`. In the base `2^15` number system, getting a saturation like that is mathematically very unlikely. | ||
It uses [redis-ordered-queue](https://www.npmjs.com/package/redis-ordered-queue) to generate a sort ID. It means it only processes one document at a time as per the mathematical requirement of the sort ID generation algorithm even when we are running multiple instances of our service. |
@@ -1,41 +0,7 @@ | ||
const { encrypt, decrypt } = require("../utils/encryption"); | ||
const encryptedUserSchema = require("../schemas/encryptedUser"); | ||
const { getModelWithSortEncryptedFieldsPlugin } = require("../../lib/index"); | ||
const { getRedis } = require("../utils/databases"); | ||
const mongoose = require("mongoose"); | ||
const redis = getRedis(); | ||
const encryptedUserSchema = new mongoose.Schema({ | ||
firstName: { | ||
type: String, | ||
required: true, | ||
set: encrypt, | ||
get: decrypt, | ||
sortFieldName: "firstNameSort", | ||
}, | ||
middleName: { | ||
type: String, | ||
set: encrypt, | ||
get: decrypt, | ||
sortFieldName: "middleNameSort", | ||
}, | ||
lastName: { | ||
type: String, | ||
required: true, | ||
set: encrypt, | ||
get: decrypt, | ||
sortFieldName: "lastNameSort", | ||
}, | ||
// firstNameSort: { | ||
// type: Buffer, | ||
// }, | ||
}); | ||
encryptedUserSchema.statics.createUser = async function createUser(data) { | ||
const user = new this(data); | ||
const userData = await user.save(); | ||
return userData; | ||
}; | ||
// const EncryptedUser = mongoose.model("EncryptedUser", encryptedUserSchema); | ||
const EncryptedUser = getModelWithSortEncryptedFieldsPlugin("EncryptedUser", encryptedUserSchema, { | ||
const encryptedUser = getModelWithSortEncryptedFieldsPlugin("EncryptedUser", encryptedUserSchema, { | ||
redisQueueClientOptions: { redis }, | ||
@@ -47,2 +13,2 @@ ignoreCases: true, | ||
module.exports = EncryptedUser; | ||
module.exports = encryptedUser; |
const mongoose = require("mongoose"); | ||
const unencryptedUserSchema = require("../schemas/unencryptedUser"); | ||
const unencryptedUserSchema = new mongoose.Schema({ | ||
firstName: { | ||
type: String, | ||
required: true, | ||
sortFieldName: "firstNameSort", | ||
}, | ||
middleName: { | ||
type: String, | ||
sortFieldName: "middleNameSort", | ||
}, | ||
lastName: { | ||
type: String, | ||
required: true, | ||
sortFieldName: "lastNameSort", | ||
}, | ||
}); | ||
unencryptedUserSchema.statics.createUser = async function createUser(data) { | ||
const user = new this(data); | ||
const userData = await user.save(); | ||
return userData; | ||
}; | ||
const UnencryptedUser = mongoose.model("unencryptedUser", unencryptedUserSchema); | ||
module.exports = UnencryptedUser; | ||
const unencryptedUser = mongoose.model("unencryptedUser", unencryptedUserSchema); | ||
module.exports = unencryptedUser; |
@@ -8,6 +8,8 @@ const assert = require("assert"); | ||
describe("mongoose-sort-encrypted-field tests", async function () { | ||
const newUsers = require("./data/newUsers.json"); | ||
const users = require("./data/users.json"); | ||
const n = 90; | ||
const silent = true; | ||
const pluginSilent = true; | ||
describe(`Testing sorting after creating new documents with ${n} random users and ${newUsers.length} predefined users`, async function () { | ||
describe(`Testing sorting after creating new documents with ${n} random users and ${users.length} predefined users`, async function () { | ||
let unencryptedUserModel; | ||
@@ -20,28 +22,39 @@ let encryptedUserModel; | ||
unencryptedUserModel = require("./models/unencryptedUser"); | ||
encryptedUserModel = require("./models/encryptedUser"); | ||
const encryptedUserSchema = require("./schemas/encryptedUser"); | ||
const { getModelWithSortEncryptedFieldsPlugin } = require("../lib/index"); | ||
const { getRedis } = require("./utils/databases"); | ||
const redis = getRedis(); | ||
encryptedUserModel = getModelWithSortEncryptedFieldsPlugin("EncryptedUser", encryptedUserSchema, { | ||
redisQueueClientOptions: { redis, consumerCount: 3 }, | ||
noOfCharsForSortId: 1, | ||
silent: pluginSilent, | ||
selectSortFields: true, | ||
revaluateAllCountThreshold: -1, | ||
}); | ||
modelsQueue = getModelsQueue(); | ||
}); | ||
after(async () => { | ||
await stopDatabases(); | ||
}); | ||
it(`creating new documents with ${n} random users and ${newUsers.length} predefined users`, async function () { | ||
it(`creating new documents with ${n} random users and ${users.length} predefined users`, async function () { | ||
for (let i = 0; i < n; i += 1) { | ||
const [firstName, middleName] = generateName().raw; | ||
const [lastName] = generateName().raw; | ||
newUsers.push({ firstName, middleName, lastName }); | ||
users.push({ firstName, middleName, lastName }); | ||
} | ||
await unencryptedUserModel.deleteMany({}).exec(); | ||
await encryptedUserModel.deleteMany({}).exec(); | ||
await Promise.all(newUsers.map(async (newUser) => { | ||
await unencryptedUserModel.createUser(newUser); | ||
await encryptedUserModel.createUser(newUser); | ||
})); | ||
assert.equal(await unencryptedUserModel.find({}).count().exec(), newUsers.length); | ||
assert.equal(await encryptedUserModel.find({}).count().exec(), newUsers.length); | ||
for (const user of users) { | ||
await unencryptedUserModel.createUser(user); | ||
await encryptedUserModel.createUser(user); | ||
} | ||
assert.equal(await unencryptedUserModel.find({}).count().exec(), users.length); | ||
assert.equal(await encryptedUserModel.find({}).count().exec(), users.length); | ||
}); | ||
it("Sorting check for { firstName: 1 }", async function () { | ||
console.log("Waiting for sort ID to be generated..."); | ||
if (!silent) { | ||
console.log("Waiting for sort ID to be generated..."); | ||
} | ||
let pendingJobsCount = -1; | ||
@@ -52,7 +65,9 @@ while (pendingJobsCount !== 0) { | ||
} | ||
const unencryptedUsers = await unencryptedUserModel.find({}).sort({ firstName: 1 }).exec(); | ||
const encryptedUsers = await encryptedUserModel.find({}).sort({ firstNameSort: 1 }).exec(); | ||
const unencryptedUsers = await unencryptedUserModel.find({}).sort({ firstName: 1 }).lean().exec(); | ||
const encryptedUsers = await encryptedUserModel.find({}).sort({ firstNameSort: 1 }).lean().exec(); | ||
await Promise.all( | ||
unencryptedUsers.map(async (unencryptedUser, i) => { | ||
console.log(unencryptedUser.firstName, encryptedUsers[i].firstName); | ||
if (!silent) { | ||
console.log(unencryptedUser.firstName, " == ", encryptedUsers[i].firstName); | ||
} | ||
assert.equal(unencryptedUser.firstName, encryptedUsers[i].firstName); | ||
@@ -63,7 +78,9 @@ }) | ||
it("Sorting check for { middleName: 1 }", async function () { | ||
const unencryptedUsers = await unencryptedUserModel.find({}).sort({ middleName: 1 }).exec(); | ||
const encryptedUsers = await encryptedUserModel.find({}).sort({ middleNameSort: 1 }).exec(); | ||
const unencryptedUsers = await unencryptedUserModel.find({}).sort({ middleName: 1 }).lean().exec(); | ||
const encryptedUsers = await encryptedUserModel.find({}).sort({ middleNameSort: 1 }).lean().exec(); | ||
await Promise.all( | ||
unencryptedUsers.map(async (unencryptedUser, i) => { | ||
console.log(unencryptedUser.middleName, encryptedUsers[i].middleName); | ||
if (!silent) { | ||
console.log(unencryptedUser.middleName, " == ", encryptedUsers[i].middleName); | ||
} | ||
assert.equal(unencryptedUser.middleName, encryptedUsers[i].middleName); | ||
@@ -74,7 +91,9 @@ }) | ||
it("Sorting check for { lastName: 1 }", async function () { | ||
const unencryptedUsers = await unencryptedUserModel.find({}).sort({ lastName: 1 }).exec(); | ||
const encryptedUsers = await encryptedUserModel.find({}).sort({ lastNameSort: 1 }).exec(); | ||
const unencryptedUsers = await unencryptedUserModel.find({}).sort({ lastName: 1 }).lean().exec(); | ||
const encryptedUsers = await encryptedUserModel.find({}).sort({ lastNameSort: 1 }).lean().exec(); | ||
await Promise.all( | ||
unencryptedUsers.map(async (unencryptedUser, i) => { | ||
console.log(unencryptedUser.lastName, encryptedUsers[i].lastName); | ||
if (!silent) { | ||
console.log(unencryptedUser.lastName, " == ", encryptedUsers[i].lastName); | ||
} | ||
assert.equal(unencryptedUser.lastName, encryptedUsers[i].lastName); | ||
@@ -85,7 +104,12 @@ }) | ||
it("Sorting check for { firstName: 1, middleName: 1, lastName: 1 }", async function () { | ||
const unencryptedUsers = await unencryptedUserModel.find({}).sort({ firstName: 1, middleName: 1, lastName: 1 }).exec(); | ||
const encryptedUsers = await encryptedUserModel.find({}).sort({ firstNameSort: 1, middleNameSort: 1, lastNameSort: 1 }).exec(); | ||
const unencryptedUsers = await unencryptedUserModel.find({}).sort({ firstName: 1, middleName: 1, lastName: 1 }).lean().exec(); | ||
const encryptedUsers = await encryptedUserModel.find({}).sort({ firstNameSort: 1, middleNameSort: 1, lastNameSort: 1 }).lean().exec(); | ||
await Promise.all( | ||
unencryptedUsers.map(async (unencryptedUser, i) => { | ||
assert.equal(unencryptedUser.lastName, encryptedUsers[i].lastName); | ||
const name1 = `${unencryptedUser.firstName} ${unencryptedUser.middleName} ${unencryptedUser.lastName}`; | ||
const name2 = `${encryptedUsers[i].firstName} ${encryptedUsers[i].middleName} ${encryptedUsers[i].lastName}`; | ||
if (!silent) { | ||
console.log(name1, " == ", name2); | ||
} | ||
assert.equal(name1, name2); | ||
}) | ||
@@ -95,7 +119,12 @@ ); | ||
it("Sorting check for { lastName: 1, firstName: 1, middleName: 1 }", async function () { | ||
const unencryptedUsers = await unencryptedUserModel.find({}).sort({ lastName: 1, firstName: 1, middleName: 1 }).exec(); | ||
const encryptedUsers = await encryptedUserModel.find({}).sort({ lastNameSort: 1, firstNameSort: 1, middleNameSort: 1 }).exec(); | ||
const unencryptedUsers = await unencryptedUserModel.find({}).sort({ lastName: 1, firstName: 1, middleName: 1 }).lean().exec(); | ||
const encryptedUsers = await encryptedUserModel.find({}).sort({ lastNameSort: 1, firstNameSort: 1, middleNameSort: 1 }).lean().exec(); | ||
await Promise.all( | ||
unencryptedUsers.map(async (unencryptedUser, i) => { | ||
assert.equal(unencryptedUser.lastName, encryptedUsers[i].lastName); | ||
const name1 = `${unencryptedUser.firstName} ${unencryptedUser.middleName} ${unencryptedUser.lastName}`; | ||
const name2 = `${encryptedUsers[i].firstName} ${encryptedUsers[i].middleName} ${encryptedUsers[i].lastName}`; | ||
if (!silent) { | ||
console.log(name1, " == ", name2); | ||
} | ||
assert.equal(name1, name2); | ||
}) | ||
@@ -108,11 +137,45 @@ ); | ||
); | ||
const unencryptedUsers = await unencryptedUserModel.find({}).sort({ firstName: 1, middleName: 1, lastName: 1 }).exec(); | ||
const encryptedUsers = await encryptedUserModel.find({}).sort({ firstNameSort: 1, middleNameSort: 1, lastNameSort: 1 }).exec(); | ||
const unencryptedUsers = await unencryptedUserModel.find({}).sort({ firstName: 1, middleName: 1, lastName: 1 }).lean().exec(); | ||
const encryptedUsers = await encryptedUserModel.find({}).sort({ firstNameSort: 1, middleNameSort: 1, lastNameSort: 1 }).lean().exec(); | ||
await Promise.all( | ||
unencryptedUsers.map(async (unencryptedUser, i) => { | ||
assert.equal(unencryptedUser.lastName, encryptedUsers[i].lastName); | ||
const name1 = `${unencryptedUser.firstName} ${unencryptedUser.middleName} ${unencryptedUser.lastName}`; | ||
const name2 = `${encryptedUsers[i].firstName} ${encryptedUsers[i].middleName} ${encryptedUsers[i].lastName}`; | ||
if (!silent) { | ||
console.log(name1, " == ", name2); | ||
} | ||
assert.equal(name1, name2); | ||
}) | ||
); | ||
}); | ||
it("Sorting check for { firstName: 1, middleName: 1, lastName: 1 } after inserting users in sorted order to reach saturation", async function () { | ||
const unencryptedUsers = await unencryptedUserModel.find({}).sort({ firstName: 1, middleName: 1, lastName: 1 }).lean().exec(); | ||
await encryptedUserModel.deleteMany({}).exec(); | ||
for (const user of unencryptedUsers) { | ||
await encryptedUserModel.createUser(user); | ||
} | ||
if (!silent) { | ||
console.log("Waiting for sort ID to be generated..."); | ||
} | ||
let pendingUsers = -1; | ||
while (pendingUsers !== 0) { | ||
pendingUsers = await encryptedUserModel | ||
.find({ $or: [{ firstNameSort: null }, { middleNameSort: null }, { lastNameSort: null }] }) | ||
.count(); | ||
await new Promise((r) => setTimeout(r, 1000)); | ||
} | ||
const encryptedUsers = await encryptedUserModel.find({}).sort({ firstNameSort: 1, middleNameSort: 1, lastNameSort: 1 }).lean().exec(); | ||
await Promise.all( | ||
unencryptedUsers.map(async (unencryptedUser, i) => { | ||
const name1 = `${unencryptedUser.firstName} ${unencryptedUser.middleName} ${unencryptedUser.lastName}`; | ||
const name2 = `${encryptedUsers[i].firstName} ${encryptedUsers[i].middleName} ${encryptedUsers[i].lastName}`; | ||
if (!silent) { | ||
console.log(name1, " == ", name2); | ||
} | ||
assert.equal(name1, name2); | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |
@@ -15,4 +15,2 @@ const { RedisMemoryServer } = require("redis-memory-server"); | ||
redis = new Redis({ host, port }); | ||
await redis.call('flushall'); | ||
console.log("Redis is connected", { host, port }); | ||
} | ||
@@ -24,3 +22,2 @@ | ||
await mongoose.connect(uri); | ||
console.log("Mongoose is connected", { uri }); | ||
} | ||
@@ -27,0 +24,0 @@ |
@@ -7,3 +7,4 @@ const crypto = require("crypto"); | ||
const decrypt = function (encrypted) { | ||
function decrypt(encrypted) { | ||
return encrypted; | ||
const decipher = crypto.createDecipheriv(algorithm, key, iv); | ||
@@ -15,3 +16,4 @@ let decrypted = decipher.update(encrypted, "hex", "utf8"); | ||
const encrypt = function (plain) { | ||
function encrypt(plain) { | ||
return plain; | ||
const cipher = crypto.createCipheriv(algorithm, key, iv); | ||
@@ -24,9 +26,1 @@ let encrypted = cipher.update(plain, "utf8", "hex"); | ||
module.exports = { encrypt, decrypt }; | ||
// console.log(encrypt("yess")) | ||
// console.log(encrypt("yess")) | ||
// console.log(encrypt("yess")) | ||
// console.log(encrypt("yess")) | ||
// console.log(encrypt("yess")) | ||
// console.log(encrypt("yess")) |
81015
23
1819
+ Added@navpreetdevpuri/base-2-n@2.0.1(transitive)
- Removedmath-buffer@^0.1.1
- Removedmath-buffer@0.1.1(transitive)