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

Relations between buckets expressed with pointers ? #236

Open
tgirod opened this issue Oct 18, 2018 · 1 comment
Open

Relations between buckets expressed with pointers ? #236

tgirod opened this issue Oct 18, 2018 · 1 comment

Comments

@tgirod
Copy link

tgirod commented Oct 18, 2018

Disclaimer: I'm throwing an idea here, no clue if it is possible / relevant... anyway:

When using a DB such as storm as a persitance layer, you inevitably find yourself in a situation where you want to have relations between your various structs.

I think it would be nice to be able to declare a field of a struct as foreign (given it is a valid storm struct), and when saving it storm would store that field in a separate bucket.

type Line struct {
	ID   int `storm:"increment"`
	Text string
}

type File struct {
	ID    int     `storm:"increment"`
	Lines []*Line `storm:"foreign"`
}

func main() {
	db, err := storm.Open("stuff.db")
	if err != nil {
		log.Fatal(err)
	}

	l1 := Line{Text: "first line"}
	l2 := Line{Text: "second line"}
	file := File{Lines: []*Line{&l1, &l2}}
	db.Save(&file)
	// would store l1 and l2 in a "Line" bucket

	var l3 Line
	db.One("ID", 1, &l3)
	// l3 == {1,"first line"}
	l3.Text = "modified first line"
	db.Save(&l3)

	var file2 File
	db.One("ID", 1, &file2)
	// file2 == {1, {"modified first line", "second line"}}
}
@tgirod
Copy link
Author

tgirod commented Nov 22, 2018

Another suggestion along that line: if a field of a struct is itself a struct and has an ID field, then automatically store that separately, and store the type + ID when serializing the database.

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

1 participant