sequelize#Includeable TypeScript Examples
The following examples show how to use
sequelize#Includeable.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PluginSqlizeQuery.ts From expresso with MIT License | 6 votes |
/**
*
* @param include
* @returns
*/
function injectRequireInclude(include: Includeable[]): Includeable[] {
function test(dataInclude: Includeable[]): boolean {
for (let i = 0; i < (dataInclude?.length || 0); i += 1) {
const optionInclude = dataInclude[i] as IncludeOptions
let data
if (optionInclude.include) {
data = test(optionInclude.include)
}
if (optionInclude.required) return true
if (data && optionInclude.required === undefined) {
optionInclude.required = true
return true
}
}
return false
}
test(include)
return include
}
Example #2
Source File: PluginSqlizeQuery.ts From expresso with MIT License | 6 votes |
/**
*
* @param filteredValue
* @param includes
* @returns
*/
function makeIncludeQueryable(filteredValue: any, includes: Includeable[]) {
return transfromIncludeToQueryable(includes, (value) => {
const { model, key, ...restValue } = value
return getIncludeFilteredQuery(filteredValue, model, value.key, {
key,
...restValue,
} as IncludeOptions)
})
}
Example #3
Source File: PluginSqlizeQuery.ts From expresso with MIT License | 5 votes |
/**
*
* @param reqQuery
* @param model
* @param includeRule
* @param options
* @returns
*/
function generate(
reqQuery: ReqGenerate,
model: any,
includeRule?: Includeable | Includeable[],
options?: GenerateOptions
) {
const { onBeforeBuild } = options ?? {}
const paginationQuery = getPaginationQuery()
const filteredQuery = getFilteredQuery(model)
const sortedQuery = getSortedQuery()
const includeCountRule = filterIncludeHandledOnly({
include: includeRule,
})
const include = injectRequireInclude(
_.cloneDeep(includeRule) as Includeable[]
)
const includeCount = injectRequireInclude(
_.cloneDeep(includeCountRule) as Includeable[]
)
if (onBeforeBuild) {
onBeforeBuild({
filteredQuery,
paginationQuery,
sortedQuery,
})
}
const pagination = paginationQuery.build(reqQuery)
const filter = filteredQuery.build(reqQuery.filtered)
const sort = sortedQuery.build(reqQuery.sorted)
return {
include,
includeCount,
where: filter,
order: sort,
offset: pagination.offset,
limit: pagination.limit,
}
}
Example #4
Source File: SqlizeQuery.ts From expresso with MIT License | 5 votes |
/**
*
* @param includes
* @param onBuildInclude
* @returns
*/
export function transfromIncludeToQueryable(
includes: Includeable[],
onBuildInclude?: onBuildInclude
): CustomIncludeOptions[] {
const result = [] as CustomIncludeOptions[]
const _onBuildInclude =
onBuildInclude ??
function (value: CustomIncludeOptions) {
return value
}
/**
*
* @param includes
* @param parent
*/
function wrapFiltered(
includes: Includeable[],
parent?: IncludeOptions
): void {
for (let i = 0; i < includes.length; i += 1) {
const include = includes[i] as CustomIncludeOptions
const { model, key, include: oriInclude, ...restInclude } = include
// TODO: fix compare isTypeModel for better check typing
const isTypeModel = typeof Model === typeof include
const curModel = (isTypeModel ? include : model) as typeof Model
const defaultName = curModel.options.name?.singular
const data = _onBuildInclude({
...(isTypeModel ? {} : restInclude),
key: key ?? defaultName,
model: curModel,
} as unknown as IncludeOptions)
if (parent) {
// eslint-disable-next-line no-param-reassign
parent.include = parent.include ?? []
parent.include.push(data)
} else {
result.push(data)
}
if (include.include) {
wrapFiltered(include.include, data)
}
}
}
wrapFiltered(includes)
return result
}