Merge pull request #3993 from mikiher/fix-stringify-sequelize-query

fix stringifySequelizeQuery and add tests
This commit is contained in:
advplyr 2025-02-15 17:24:19 -06:00 committed by GitHub
commit 8b00c16062
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 72 additions and 29 deletions

View File

@ -1,34 +1,25 @@
function stringifySequelizeQuery(findOptions) {
// Helper function to handle symbols in nested objects
function handleSymbols(obj) {
if (!obj || typeof obj !== 'object') return obj
if (Array.isArray(obj)) {
return obj.map(handleSymbols)
}
const newObj = {}
for (const [key, value] of Object.entries(obj)) {
// Handle Symbol keys from Object.getOwnPropertySymbols
Object.getOwnPropertySymbols(obj).forEach((sym) => {
newObj[`__Op.${sym.toString()}`] = handleSymbols(obj[sym])
})
// Handle regular keys
if (typeof key === 'string') {
if (value && typeof value === 'object' && Object.getPrototypeOf(value) === Symbol.prototype) {
// Handle Symbol values
newObj[key] = `__Op.${value.toString()}`
} else {
// Recursively handle nested objects
newObj[key] = handleSymbols(value)
}
}
}
return newObj
function isClass(func) {
return typeof func === 'function' && /^class\s/.test(func.toString())
}
const sanitizedOptions = handleSymbols(findOptions)
return JSON.stringify(sanitizedOptions)
function replacer(key, value) {
if (typeof value === 'object' && value !== null) {
const symbols = Object.getOwnPropertySymbols(value).reduce((acc, sym) => {
acc[sym.toString()] = value[sym]
return acc
}, {})
return { ...value, ...symbols }
}
if (isClass(value)) {
return `${value.name}`
}
return value
}
return JSON.stringify(findOptions, replacer)
}
module.exports = stringifySequelizeQuery

View File

@ -0,0 +1,52 @@
const { expect } = require('chai')
const stringifySequelizeQuery = require('../../../server/utils/stringifySequelizeQuery')
const Sequelize = require('sequelize')
class DummyClass {}
describe('stringifySequelizeQuery', () => {
it('should stringify a sequelize query containing an op', () => {
const query = {
where: {
name: 'John',
age: {
[Sequelize.Op.gt]: 20
}
}
}
const result = stringifySequelizeQuery(query)
expect(result).to.equal('{"where":{"name":"John","age":{"Symbol(gt)":20}}}')
})
it('should stringify a sequelize query containing a literal', () => {
const query = {
order: [[Sequelize.literal('libraryItem.title'), 'ASC']]
}
const result = stringifySequelizeQuery(query)
expect(result).to.equal('{"order":{"0":{"0":{"val":"libraryItem.title"},"1":"ASC"}}}')
})
it('should stringify a sequelize query containing a class', () => {
const query = {
include: [
{
model: DummyClass
}
]
}
const result = stringifySequelizeQuery(query)
expect(result).to.equal('{"include":{"0":{"model":"DummyClass"}}}')
})
it('should ignore non-class functions', () => {
const query = {
logging: (query) => console.log(query)
}
const result = stringifySequelizeQuery(query)
expect(result).to.equal('{}')
})
})