Skip to content

Latest commit

 

History

History
63 lines (46 loc) · 1.74 KB

README.md

File metadata and controls

63 lines (46 loc) · 1.74 KB

datastore-serializer

A serializer that works in conjonction with Google Cloud Datastore to serialize relationships defined as pointers. This is useful if you go the denormalization way.

Installation

go get github.com/marcgiovannoni/datastore-serializer

Usage

This serializer has been written so working with Google JSONAPI and Google Cloud Datastore is easier.

The use of jsonapi is not mandatory, it will work with simple relationships.

Requirements

ID attribute has to be an encoded datastore.Key

type Post struct {
    ID       string     `datastore:"-" jsonapi:"primary,post" serializer:"primary,post"`
    Text     string     `datastore:"text,noindex" jsonapi:"attr,text"`
    Comments []*Comment `datastore:"-" jsonapi:"relation,comments" serializer:"relation,comments"`
}

type Comment struct {
    ID   string `datastore:"-" jsonapi:"primary,comment" serializer:"primary,comment"`
    Text string `datastore:"text,noindex" jsonapi:"attr,text"`
}

func (me *Post) Load(ps []datastore.Property) error {
    return serializer.LoadEntity(me, ps)
}

func (me *Post) Save() ([]datastore.Property, error) {
    ps, err := serializer.SaveEntity(me)
    if err != nil {
        return nil, err
    }
    return ps, nil
}

func (me *Comment) Load(ps []datastore.Property) error {
    return serializer.LoadEntity(me, ps)
}

func (me *Comment) Save() ([]datastore.Property, error) {
    ps, err := serializer.SaveEntity(me)
    if err != nil {
        return nil, err
    }
    return ps, nil
}

This will be serialized in the datastore as:

text comments.id comments.text