mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-10-27 11:18:14 +01:00 
			
		
		
		
	fix stringifySequelizeQuery and add tests
This commit is contained in:
		
							parent
							
								
									373551fb74
								
							
						
					
					
						commit
						8ee5646d79
					
				@ -1,34 +1,25 @@
 | 
				
			|||||||
function stringifySequelizeQuery(findOptions) {
 | 
					function stringifySequelizeQuery(findOptions) {
 | 
				
			||||||
  // Helper function to handle symbols in nested objects
 | 
					  function isClass(func) {
 | 
				
			||||||
  function handleSymbols(obj) {
 | 
					    return typeof func === 'function' && /^class\s/.test(func.toString())
 | 
				
			||||||
    if (!obj || typeof obj !== 'object') return obj
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (Array.isArray(obj)) {
 | 
					 | 
				
			||||||
      return obj.map(handleSymbols)
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const newObj = {}
 | 
					  function replacer(key, value) {
 | 
				
			||||||
    for (const [key, value] of Object.entries(obj)) {
 | 
					    if (typeof value === 'object' && value !== null) {
 | 
				
			||||||
      // Handle Symbol keys from Object.getOwnPropertySymbols
 | 
					      const symbols = Object.getOwnPropertySymbols(value).reduce((acc, sym) => {
 | 
				
			||||||
      Object.getOwnPropertySymbols(obj).forEach((sym) => {
 | 
					        acc[sym.toString()] = value[sym]
 | 
				
			||||||
        newObj[`__Op.${sym.toString()}`] = handleSymbols(obj[sym])
 | 
					        return acc
 | 
				
			||||||
      })
 | 
					      }, {})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Handle regular keys
 | 
					      return { ...value, ...symbols }
 | 
				
			||||||
      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
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const sanitizedOptions = handleSymbols(findOptions)
 | 
					    if (isClass(value)) {
 | 
				
			||||||
  return JSON.stringify(sanitizedOptions)
 | 
					      return `${value.name}`
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return value
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return JSON.stringify(findOptions, replacer)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
module.exports = stringifySequelizeQuery
 | 
					module.exports = stringifySequelizeQuery
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										52
									
								
								test/server/utils/stringifySequeslizeQuery.test.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								test/server/utils/stringifySequeslizeQuery.test.js
									
									
									
									
									
										Normal 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('{}')
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user