sql model template

This commit is contained in:
Asim Aslam 2020-08-23 21:30:46 +01:00
parent d28f0670d6
commit 1a6652fe6b
3 changed files with 145 additions and 2 deletions

View File

@ -34,8 +34,6 @@ type Entity interface {
Value() interface{}
// Attributes of the entity
Attributes() map[string]interface{}
// Read a value as a concrete type
Read(v interface{}) error
}
type Options struct {

41
model/sql/entity.go Normal file
View File

@ -0,0 +1,41 @@
package sql
import (
"github.com/google/uuid"
"github.com/micro/go-micro/v3/codec"
"github.com/micro/go-micro/v3/model"
)
type sqlEntity struct {
id string
name string
value interface{}
codec codec.Marshaler
attributes map[string]interface{}
}
func (m *sqlEntity) Attributes() map[string]interface{} {
return m.attributes
}
func (m *sqlEntity) Id() string {
return m.id
}
func (m *sqlEntity) Name() string {
return m.name
}
func (m *sqlEntity) Value() interface{} {
return m.value
}
func newEntity(name string, value interface{}, codec codec.Marshaler) model.Entity {
return &sqlEntity{
id: uuid.New().String(),
name: name,
value: value,
codec: codec,
attributes: make(map[string]interface{}),
}
}

104
model/sql/sql.go Normal file
View File

@ -0,0 +1,104 @@
// Package sql is the micro data model implementation
package sql
import (
"github.com/micro/go-micro/v3/codec/json"
"github.com/micro/go-micro/v3/model"
"github.com/micro/go-micro/v3/store"
"github.com/micro/go-micro/v3/store/memory"
memsync "github.com/micro/go-micro/v3/sync/memory"
)
type sqlModel struct {
options model.Options
}
func (m *sqlModel) Init(opts ...model.Option) error {
for _, o := range opts {
o(&m.options)
}
return nil
}
func (m *sqlModel) NewEntity(name string, value interface{}) model.Entity {
// TODO: potentially pluralise name for tables
return newEntity(name, value, m.options.Codec)
}
func (m *sqlModel) Create(e model.Entity) error {
// lock on the name of entity
if err := m.options.Sync.Lock(e.Name()); err != nil {
return err
}
// TODO: deal with the error
defer m.options.Sync.Unlock(e.Name())
// TODO: potentially add encode to entity?
v, err := m.options.Codec.Marshal(e.Value())
if err != nil {
return err
}
// TODO: include metadata and set database
return m.options.Store.Write(&store.Record{
Key: e.Id(),
Value: v,
}, store.WriteTo(m.options.Database, e.Name()))
}
func (m *sqlModel) Read(opts ...model.ReadOption) ([]model.Entity, error) {
var options model.ReadOptions
for _, o := range opts {
o(&options)
}
// TODO: implement the options that allow querying
return nil, nil
}
func (m *sqlModel) Update(e model.Entity) error {
// TODO: read out the record first, update the fields and store
// lock on the name of entity
if err := m.options.Sync.Lock(e.Name()); err != nil {
return err
}
// TODO: deal with the error
defer m.options.Sync.Unlock(e.Name())
// TODO: potentially add encode to entity?
v, err := m.options.Codec.Marshal(e.Value())
if err != nil {
return err
}
// TODO: include metadata and set database
return m.options.Store.Write(&store.Record{
Key: e.Id(),
Value: v,
}, store.WriteTo(m.options.Database, e.Name()))
}
func (m *sqlModel) Delete(opts ...model.DeleteOption) error {
var options model.DeleteOptions
for _, o := range opts {
o(&options)
}
// TODO: implement the options that allow deleting
return nil
}
func (m *sqlModel) String() string {
return "sql"
}
func NewModel(opts ...model.Option) model.Model {
options := model.Options{
Codec: new(json.Marshaler),
Sync: memsync.NewSync(),
Store: memory.NewStore(),
}
return &sqlModel{
options: options,
}
}