Skip to content
This repository has been archived by the owner on Jan 3, 2021. It is now read-only.

New Query Syntax Docs #23

Open
olbrichj opened this issue Aug 1, 2019 · 8 comments
Open

New Query Syntax Docs #23

olbrichj opened this issue Aug 1, 2019 · 8 comments

Comments

@olbrichj
Copy link

olbrichj commented Aug 1, 2019

I'm trying to figure out how to use Meow 2.0.2. By now I figured out how to save but I'm totally lost regarding the Queries.

class Test: Model {
    var _id = IdObject()
    var symbol: Symbol
}

class Symbol: Codable {
    var name: String
    var number: Int
}

With this setup I'm trying to find documents by name or number (or even both).

let context = manager?.makeContext()
let query: Query = "symbol.name" == "Test"
let result = try? context?.findOne(Test.self, where: query).wait()

Or trying to have name on toplevel:

class Test: Model {
    var _id = IdObject()
    var name: String
}

let context = manager?.makeContext()
let query: Query = "name" == "Test"
let result = try? context?.findOne(Test.self, where: query).wait()

All of these result in nil. I've also tried to create the corresponding syntax:

[
  "name" : [
    "$eq" : 
    "Test"
  ]
]

Which also didn't work. So I'm wondering... what is the correct way to query for documents in Meow 2.0?

I would be willing to help and write some docs, but I'd need to know how to use Meow first :D

@Joannis
Copy link
Member

Joannis commented Aug 2, 2019

There are two reasons you could be getting nil. Either manager == nil or you don't have an entity with "name" == "Test". The query is completely fine :)

@olbrichj
Copy link
Author

olbrichj commented Aug 2, 2019

I will check again, when I'm back from work. The manager is present, as I use the same one to create the entries in MongoDB (which I can also find).

Regarding the Query syntax. Is the above syntax also correct?
"symbol.name" == "Test"

@Joannis
Copy link
Member

Joannis commented Aug 2, 2019

The syntax is fine. I'm wondering, why is the manager is optional

@olbrichj
Copy link
Author

olbrichj commented Aug 2, 2019

Oh that is just a relict of my testing trying to figure out how this works :D The optional will be removed in my next try

@olbrichj
Copy link
Author

olbrichj commented Aug 2, 2019

So it seems like this is a bug in Xcode or llvm. It does return a document, but Xcode still displays it as nil. Unwrapping changes everything.

But I have another question regarding the syntax. If I want to compare a value with a codable struct, is there any way to not compare every value explicitly?

Given this structure:

class Test: Model {
  var _id = IdObject()
  var address: Address
}

struct Address: Codable {
  var street: String
  var country: Country
}

enum Country: String, Codable {
  case australia
}

I can compare it explicitly:

let query: Query = ("address.street" == testInstance.address.street) && ("address.country" == testInstance.address.country.rawValue

What I'm looking for is something like this

let query: Query = "address" = testInstance.address

Furthermore I've seen the old syntax is like this:

Test.findOne() { test in
  return test.address.strees = "foo"
}

Is there something similar? I found it for saving but didn't see anything for querying.

testInstance.save(to: context)

Last but not least, regarding the docs. Is there a specific format or a template to orientate? :)

@Joannis
Copy link
Member

Joannis commented Aug 2, 2019

The current syntax would be like this:

let query: Query = "address.street" == Country.australia.rawValue

However, I think we have code that allows you to use raw enums directly, too:

let query: Query = "address.street" == Country.australia

@Joannis
Copy link
Member

Joannis commented Aug 2, 2019

I guessed why not, so I implemented what I think is a good design of this snippet in Meow 3:

Test.findOne() { test in
  return test.address.strees = "foo"
}

@olbrichj
Copy link
Author

olbrichj commented Aug 2, 2019

That snippet for querying looks really neat :) Would also be great for .find().

Regarding the raw enum query I get the following error:

Cannot convert value of type 'Country' to expected argument type 'Primitive?'

I tried extending the enum with Primitive, which doesn't result in a compile error but instead a runtime exception:

Fatal error: Currently unsupported type []: file .build/checkouts/BSON/Sources/BSON/Document/Document+Dictionary.swift, line 175

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants