sequelize#IncludeOptions TypeScript Examples

The following examples show how to use sequelize#IncludeOptions. 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 vote down vote up
function getIncludeFilteredQuery(
  filteredValue: any,
  model: any,
  prefixName: any,
  options?: IncludeOptions
) {
  const where = getFilteredQuery(model, prefixName).build(filteredValue)

  let extraProps = {}

  if (Object.keys(where).length > 0) {
    extraProps = {
      ...extraProps,
      where,
      required: true,
    }
  }

  return {
    model,
    ...extraProps,
    ...options,
  }
}
Example #2
Source File: PluginSqlizeQuery.ts    From expresso with MIT License 6 votes vote down vote up
/**
 *
 * @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 #3
Source File: PluginSqlizeQuery.ts    From expresso with MIT License 6 votes vote down vote up
/**
 *
 * @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 #4
Source File: SqlizeQuery.ts    From expresso with MIT License 5 votes vote down vote up
/**
 *
 * @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
}