Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Can you insert a struct without using a command? #29

Closed
josebalius opened this issue Oct 2, 2015 · 12 comments
Closed

Can you insert a struct without using a command? #29

josebalius opened this issue Oct 2, 2015 · 12 comments

Comments

@josebalius
Copy link

I tried playing around with the Document type but could not figure out a way to simply insert a struct without writing an insert statement.

I realize this is more "ORM" but I was wondering if it was currently possible?

Thanks!

Jose

@dennwc
Copy link
Collaborator

dennwc commented Oct 2, 2015

Hi, @josebalius!

This is related to #27. I intend to implement a way to convert a structs to Documents shortly, and this will allow to write something like this:

var a MyStruct
doc := orient.ToDocument(a)
db.CreateRecord(doc)

Is that a behavior you asking about?

The long way in example above is intentional, as it allows to set a RID on Document (this changes default cluster of a record). After some time I will also implement a way to register Go structs as a class for OrientDB, using fields to set type constraint, allowing to just insert a struct to certain cluster. This will be more ORM-like :)

@josebalius
Copy link
Author

@dennwc yep exactly what I mean, that would be awesome!

@dennwc dennwc closed this as completed in d96f087 Oct 3, 2015
@dennwc
Copy link
Collaborator

dennwc commented Oct 3, 2015

Implemented. First step to #6. Document.From function can be used in this way:

var a MyStruct
doc := orient.NewDocument("Structs") // cluster or class name
_ = doc.From(a) // can return conversion error
err := db.CreateRecord(doc) // doc will be updated with new record RID and version

a.Data = "some changes"
doc.From(a) // fill doc with changes
err = db.UpdateRecord(doc) // version will be updated

@josebalius feel free to reopen this issue if you have any related questions or proposals

@josebalius
Copy link
Author

@dennwc So it looks like it can't convert structs that have consts with iota on them (go enums) :/ What do you recommend I do? Need an example or you know what I mean?

@ympons
Copy link
Contributor

ympons commented Oct 8, 2015

@josebalius I suspect the problem is not the struct. You was using a custom type SourceType (#37), so the type was unknown for the serializer.

@josebalius
Copy link
Author

@ympons yes, its the custom type but is there anyway to support this? otherwise the enum pattern recommended by Go won't ever be compatible with this. I guess i can just use "int" as my type but that makes me sad :(

@josebalius
Copy link
Author

Maybe we could use a struct tag to specify how to convert the custom type? It's just an int at the end of the day.

@dennwc
Copy link
Collaborator

dennwc commented Oct 8, 2015

This is not a big problem. It only needs a small tweak in a type converter. I will check it tomorrow. Also, please open a new issue for that, or reopen issue #37.

@josebalius
Copy link
Author

@dennwc Can you please provide an example on how to use the UpdateRecord API without doing a CreateRecord first? All of the examples I found in the code were after a CreateRecord has been done and I am having some trouble updating a record that i got from a query.

@josebalius
Copy link
Author

@dennwc I keep getting record is not persistent

doc := orient.NewDocument("DataSources")
            doc.From(ds)
            ds.Columns = columns
            //doc.SetField("columns", columns)
            err := m.UpdateRecord(doc)

            log.Println(ds)
log.Println(ds.RID.IsPersistent()) // true

            if err != nil {
                panic(err)
            }

ds is a struct that got filled out from a query

@josebalius
Copy link
Author

So I added doc.SetRID(ds.RID) to the code and that seemed to do it

@dennwc
Copy link
Collaborator

dennwc commented Oct 28, 2015

Right, Document.RID is the only way for now to determine which record to update. Related to #32 .

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

No branches or pull requests

3 participants