moebius icon indicating copy to clipboard operation
moebius copied to clipboard

Moebius.DocumentQuery return only one entry?

Open madasebrof opened this issue 7 years ago • 3 comments

Not sure if this is a bug or a feature request, but I'm wondering how to return a single entry. For example, I have a series of entries that are similar but just have different ids and "created_at" fields (e.g. like a log file). I only want to get the most recent one. I thought it would work like this:

db(:my_db)
   |> search(for: "/users/accounts", in: [:name])
   |> sort(:id, :desc)
   |> limit(1)
   |> Polo.Db.run

but this returns all entries that match "/users/accounts".

When I inserted it, I used:

db(:my_db)
   |> searchable([:name])
   |> Polo.Db.save(name: name, value: body)

Any help is most appreciated!

madasebrof avatar Jun 07 '17 15:06 madasebrof

I think you should be able to tack on first:

db(:my_db)
   |> search(for: "/users/accounts", in: [:name])
   |> sort(:id, :desc)
   |> Polo.Db.first

This is missing from the docs... which I really need to update.

robconery avatar Jun 07 '17 16:06 robconery

Thanks for the speedy reply!

Looks like it doesn't work:

** (ArgumentError) parameters must be of length 0 for query %Postgrex.Query{columns: ["id", "body", "created_at", "updated_at"], name: "", param_formats: [], param_oids: [], param_types: [], ref: #Reference<0.0.1.10021>, result_formats: [:binary, :binary, :binary, :binary], result_oids: [23, 25, 1184, 1184], result_types: [Postgrex.Extensions.Int4, Postgrex.Extensions.Raw, Postgrex.Extensions.TimestampTZ, Postgrex.Extensions.TimestampTZ], statement: "select id, body::text, created_at, updated_at\nfrom quovo\n\n order by body -> 'id' desc\n\n;\n", types: {Moebius.PostgrexTypes, 639035}}
         (postgrex) lib/postgrex/query.ex:49: DBConnection.Query.Postgrex.Query.encode/3
    (db_connection) lib/db_connection.ex:1071: DBConnection.describe_run/5
    (db_connection) lib/db_connection.ex:1199: DBConnection.run_begin/3
    (db_connection) lib/db_connection.ex:1133: DBConnection.run_meter/3
    (db_connection) lib/db_connection.ex:584: DBConnection.prepare_execute/4
         (postgrex) lib/postgrex.ex:134: Postgrex.query/4
          (moebius) lib/moebius/database.ex:234: Moebius.Database.execute/1
             (polo) lib/moebius/database.ex:185: Polo.Db.execute/1
             (polo) lib/moebius/database.ex:73: Polo.Db.first/1
             (polo) lib/polo/quovo/user_mgmt.ex:13: Polo.Quovo.UserMgmt.show_accounts/1

Also, it looks like the sort function translates to body -> 'id' as opposed to just plain id. Thus, it looks like you wouldn't be able to sort based on id created_at or updated_at anyway.

Oh well! :)

Thanks anyway, maybe I can figure out a workaround that doesn't involve returning all entries of a given key and sorting it after the fact...

Ideally, what I'd want is a Db.tail function that returned the last entry by either getting the maximum id created_at or updated_at.

db(:my_db)
   |> search(for: "/users/accounts", in: [:name])
   |> tail(:id)
   |> Polo.Db.tail

I would imagine this would be a useful thing to have!

Best,

madasebrof avatar Jun 07 '17 17:06 madasebrof

Sorry for the delay on this for some reason I wasn't notified of your response :(. Yes, definitely useful and I don't have time to dig in currently but would welcome any help!

robconery avatar Jul 06 '17 17:07 robconery