Skip to content

Get remote db model based on local model #49

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
warflash opened this issue Oct 10, 2022 · 6 comments
Closed

Get remote db model based on local model #49

warflash opened this issue Oct 10, 2022 · 6 comments
Labels
enhancement New feature or request

Comments

@warflash
Copy link

Feature request

I'm not sure this is already possible (easily) and there's no discussions tab for this repo sadly so I figured I'd open an issue.

Basically I'm looking for a way to get the Model that's stored in postgres based on a model that already exists (read from local json or whatever). There's PrimaryKey annotations which I thought would be used for this sort of usecase but can't figure out how.

Describe the solution you'd like

MyModel localModel = ; // read from somewhere

MyModel remoteModel = client.Table<MyModel>.GetByPkey(localModel);

something along those lines would be nice

@warflash warflash added the enhancement New feature or request label Oct 10, 2022
@acupofjose
Copy link
Contributor

You mean something like this?

var supabotMessages = await client.Table<Message>().Filter("id", Postgrest.Constants.Operator.Equals, id).Get();

@warflash
Copy link
Author

Well sort of yeah. Just without the verbosity of "id", Postgrest.Constants.Operator.Equals, id.
The primary keys are already annotated so I was hoping there would be a way to skip the verbose filter statements. For multi col keys in particular it's quite the hassle to create dicts and stuff.

 [Table("my_model")]
    public class MyModel: BaseModel
    {
        [PrimaryKey("id_2")]
        public string Id1 { get; set; }

        [PrimaryKey("id_2")]
        public string Id2{ get; set; }

        [PrimaryKey("id_3")]
        public string Id3 { get; set; }

        [Column("my_col")]
        public short MyCol{ get; set; }
       //....
   }

would result in a query like:

var filters = new Dictionary<string, string>()
           {
                {"id_1", localModel.Id1 },
                {"id_2", localModel.Id2 },
                {"id_3", localModel.Id3 },
            };
var dbModel = postgrestClient.Table<MyModel>().Match(filters).Get();

where to me it feels like the dict creation part should be able to be created automatically based on the [PrimaryKey(xx)] annotations.

Hope that makes sense :D

acupofjose added a commit that referenced this issue Oct 11, 2022
…del.PrimaryKeyColumn` in favor of a `PrimaryKey` dictionary with support for composite keys.

- Added `Match(T model)` in reference to #49
@acupofjose
Copy link
Contributor

acupofjose commented Oct 11, 2022

Oh yes! Very nice.

I've added the following on the master branch.

var moviePerson = new MoviePerson { Id = 1, MovieId = 1, PersonId = 1 };
var findMoviePerson = await client.Table<MoviePerson>().Match(moviePerson).Single();

Is that better? (This tangentially required better supporting composite primary keys, which is appreciated!)

@acupofjose
Copy link
Contributor

If that works I’ll push it into the next version for you!

@warflash
Copy link
Author

Awesome, yeah the syntax looks very nice, thank you👌

@acupofjose
Copy link
Contributor

Available in v2.1.0!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants