在Fluent(Swift)

时间:2017-07-01 23:40:55

标签: arrays swift3 fluent vapor

如何从循环内部向Fluents查询构建器添加子句?我只想循环遍历字典数组并根据字典内容添加谓词。例如,对于此数组:

var data: [[String: String]] = [
        [
                "name": "Joe",
                "email": "joe@example.com"
        ],
        [
                "name": "John",
                "email": "john@example.com"
        ]
]

我想要生成此查询:

var contacts = try Contact.makeQuery().or { orGroup in
        try orGroup.and { andGroup in
            try andGroup.filter("name", "Joe")
            try andGroup.filter("email", "joe@example.com")
        }
        try orGroup.and { andGroup in
            try andGroup.filter("name", "John")
            try andGroup.filter("email", "john@example.com")
        }
}.all()

感谢。

1 个答案:

答案 0 :(得分:0)

我会这样做:

var contacts = try Contact.makeQuery().or { orGroup in
    try data.forEach { inner in
        guard let name = inner["name"], let email = inner["email"] else {
            throw SomeError
        }
        try orGroup.and { andGroup in
            try andGroup.filter("name", name)
            try andGroup.filter("email", email)
        }
    }
}.all()