[loopbackjs] как сделать, где не в (выберите ххх) запрос - PullRequest
0 голосов
/ 26 сентября 2019

Я бы хотел использовать loopback 4 whereBuilder для создания запроса типа «выбрать * из файла, где идентификатор отсутствует (выберите fileId из workFile, где workId = 'xxxxx')», и я попробовал это:

const fileIdArray = await this.workFileRepository.find({
      where: {
        fileId: {
          eq: xxx
        }
      }
    });
    const ret = await this.fileRepository.find({
      where: {
        id: {
          nin: fileIdArray
        }
      }
    })

и

const ret = await this.fileRepository.find({
      where: {
        id: {
          nin: () => {
            return await this.workFileRepository.find({
              where: {
                fileId: {
                  eq: id
                }
              }
            })
          }
        }
      }
    })

но оба они не правы, что мне делать?это ошибка:

error TS2345: Argument of type '{ where: { id: { nin: () => (WorkFile & WorkFileRelations)[]; }; }; }' is not assignable to parameter of type 'Filter<File>'.
  Types of property 'where' are incompatible.
    Type '{ id: { nin: () => (WorkFile & WorkFileRelations)[]; }; }' is not assignable to type 'Condition<File> | AndClause<File> | OrClause<File> | undefined'.
      Type '{ id: { nin: () => (WorkFile & WorkFileRelations)[]; }; }' is not assignable to type 'Condition<File>'.
        Types of property 'id' are incompatible.
          Type '{ nin: () => (WorkFile & WorkFileRelations)[]; }' is not assignable to type 'string | (string & Date) | PredicateComparison<string> | undefined'.
            Type '{ nin: () => (WorkFile & WorkFileRelations)[]; }' is not assignable to type 'PredicateComparison<string>'.
              Types of property 'nin' are incompatible.
                Type '() => (WorkFile & WorkFileRelations)[]' is missing the following properties from type 'string[]': pop, push, concat, join, and 25 more.

 86     const ret = await this.fileRepository.find({
                                                   ~
 87       where: {

правильный формат выглядит следующим образом:

const ret = await this.fileRepository.find({
      where: {
        id: {
          nin: ["xxxxxx2","xxxxxx3"]
        }
      }
    })

, но если я изменю на

const ret = await this.fileRepository.find({
      where: {
        id: {
          nin: ()=>{
            return ['xxxxx2','xxxx3']
          }
        }
      }
    })

, это неправильно, я нене знаю, знает ли кто-нибудь и использует этот фреймворк

1 Ответ

0 голосов
/ 26 сентября 2019

я нашел решение, сначала я получаю включенные идентификаторы:

const inqFileCollectionss = await this.workFileRepository.find({
      where: {
        workId: {
          eq: id
        }
      }
    });
    const inqFileIds = inqFileCollectionss.map(item => item.fileId)

, а затем я получаю вот так:

 const data = await this.fileRepository.find({
      where: {
        id: {
          nin: inqFileIds
        }
      },
      limit: pageSize,
      skip: pageIndex
    })
...