mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 10:49:28 +02:00
server/server -> server/core
This commit is contained in:
parent
114327d4ce
commit
5a3d0650c9
838 changed files with 111 additions and 111 deletions
84
server/core/models/shared/query.ts
Normal file
84
server/core/models/shared/query.ts
Normal file
|
@ -0,0 +1,84 @@
|
|||
import { BindOrReplacements, Op, QueryTypes, Sequelize } from 'sequelize'
|
||||
import { Fn } from 'sequelize/types/utils'
|
||||
import validator from 'validator'
|
||||
import { forceNumber } from '@peertube/peertube-core-utils'
|
||||
|
||||
function doesExist (sequelize: Sequelize, query: string, bind?: BindOrReplacements) {
|
||||
const options = {
|
||||
type: QueryTypes.SELECT as QueryTypes.SELECT,
|
||||
bind,
|
||||
raw: true
|
||||
}
|
||||
|
||||
return sequelize.query(query, options)
|
||||
.then(results => results.length === 1)
|
||||
}
|
||||
|
||||
// FIXME: have to specify the result type to not break peertube typings generation
|
||||
function createSimilarityAttribute (col: string, value: string): Fn {
|
||||
return Sequelize.fn(
|
||||
'similarity',
|
||||
|
||||
searchTrigramNormalizeCol(col),
|
||||
|
||||
searchTrigramNormalizeValue(value)
|
||||
)
|
||||
}
|
||||
|
||||
function buildWhereIdOrUUID (id: number | string) {
|
||||
return validator.default.isInt('' + id) ? { id } : { uuid: id }
|
||||
}
|
||||
|
||||
function parseAggregateResult (result: any) {
|
||||
if (!result) return 0
|
||||
|
||||
const total = forceNumber(result)
|
||||
if (isNaN(total)) return 0
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
function parseRowCountResult (result: any) {
|
||||
if (result.length !== 0) return result[0].total
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
function createSafeIn (sequelize: Sequelize, toEscape: (string | number)[], additionalUnescaped: string[] = []) {
|
||||
return toEscape.map(t => {
|
||||
return t === null
|
||||
? null
|
||||
: sequelize.escape('' + t)
|
||||
}).concat(additionalUnescaped).join(', ')
|
||||
}
|
||||
|
||||
function searchAttribute (sourceField?: string, targetField?: string) {
|
||||
if (!sourceField) return {}
|
||||
|
||||
return {
|
||||
[targetField]: {
|
||||
// FIXME: ts error
|
||||
[Op.iLike as any]: `%${sourceField}%`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
doesExist,
|
||||
createSimilarityAttribute,
|
||||
buildWhereIdOrUUID,
|
||||
parseAggregateResult,
|
||||
parseRowCountResult,
|
||||
createSafeIn,
|
||||
searchAttribute
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function searchTrigramNormalizeValue (value: string) {
|
||||
return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value))
|
||||
}
|
||||
|
||||
function searchTrigramNormalizeCol (col: string) {
|
||||
return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col)))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue