mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-04 02:09:37 +02:00
server/server -> server/core
This commit is contained in:
parent
114327d4ce
commit
5a3d0650c9
838 changed files with 111 additions and 111 deletions
39
server/core/models/shared/sequelize-helpers.ts
Normal file
39
server/core/models/shared/sequelize-helpers.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
import { Sequelize } from 'sequelize'
|
||||
|
||||
function isOutdated (model: { createdAt: Date, updatedAt: Date }, refreshInterval: number) {
|
||||
if (!model.createdAt || !model.updatedAt) {
|
||||
throw new Error('Miss createdAt & updatedAt attributes to model')
|
||||
}
|
||||
|
||||
const now = Date.now()
|
||||
const createdAtTime = model.createdAt.getTime()
|
||||
const updatedAtTime = model.updatedAt.getTime()
|
||||
|
||||
return (now - createdAtTime) > refreshInterval && (now - updatedAtTime) > refreshInterval
|
||||
}
|
||||
|
||||
function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value', nullable = false) {
|
||||
if (nullable && (value === null || value === undefined)) return
|
||||
|
||||
if (validator(value) === false) {
|
||||
throw new Error(`"${value}" is not a valid ${fieldName}.`)
|
||||
}
|
||||
}
|
||||
|
||||
function buildTrigramSearchIndex (indexName: string, attribute: string) {
|
||||
return {
|
||||
name: indexName,
|
||||
// FIXME: gin_trgm_ops is not taken into account in Sequelize 6, so adding it ourselves in the literal function
|
||||
fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + ')) gin_trgm_ops') as any ],
|
||||
using: 'gin',
|
||||
operator: 'gin_trgm_ops'
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
throwIfNotValid,
|
||||
buildTrigramSearchIndex,
|
||||
isOutdated
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue