Giter Club home page Giter Club logo

Comments (4)

Comdex avatar Comdex commented on August 25, 2024 1

I think a update function for one field in a structure will be great, now I must pass all info with a structure is not so good.

from storm.

asdine avatar asdine commented on August 25, 2024

You don't need to delete in db to update your structure.

type User struct {
  ID int
  Name string
  Age int
}

db.Save(&User{
  ID: 1,
  Name: "John",
  Age: 10,
})

...

// now if you want to update the age field
// First fetch your user
var u User
db.One("ID", 1, &u)

// then update the field
u.Age = 20

// then save
db.Save(&u)

Of course, a function that would avoid fetching the object first would be great

from storm.

asdine avatar asdine commented on August 25, 2024

To update, we need:

  • an instance of the type, so we can decode the record in it
  • the bucket name
  • the ID
  • the fields to update
  • the new values to set

So I was thinking about something like this:

err := db.Update(&User{ID: 1, Age: 20})

Basically, you pass an object with the values that need to change (plus the ID).
The good side is that you can update several fields at a time.
The downside is that if you want to set a field to a zero value you can't.

So we can add another function

err := db.UpdateField(&User{ID: 1}, "Age", 0)

You can only update one field
But you can set it to a zero value

@hardcoar @Comdex what do you guys think?

from storm.

Comdex avatar Comdex commented on August 25, 2024

@asdine I think so

from storm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.