Skip to content
Snippets Groups Projects
Select Git revision
  • master default protected
  • wip/memdb-dbal
  • wip-old
  • old-history
  • testing
  • v0.10.6
  • v0.10.5
  • v0.10.4
  • v0.10.3
  • v0.10.2
  • v0.10.1
  • v0.10.0
  • v0.9.16
  • v0.9.15
  • v0.9.14
  • v0.9.13
  • v0.9.12
  • v0.9.11
  • v0.9.10
  • v0.9.9
  • v0.9.8
  • v0.9.7
  • v0.9.6
  • v0.9.5
  • v0.9.4
25 results

adapter.go

adapter.go 3.56 KiB
package database

import (
	"reflect"

	"github.com/graphql-go/graphql"
)

type SchemaSyncOptions struct {
	// Whether to remove columns from the database
	// that can't be found in models.
	//
	// *This is a potentially destructive option, only
	// enable if you're absolutely sure you want to remove
	// old columns*
	//
	// Note that this may not have an effect on document
	// or graph databases.
	RemoveOrphanedColumns bool

	// Model names (in case the collections have a different name)
	ModelNames []string
}

type InnerTransactionFunc func(MutationAdapter) error
type TransactionFunc func(txDb *Collection) error

type MutationAdapter interface {
	CreateCollectionFromModel(name string, model interface{}, ifNotExists bool) error
	Insert(collectionName string, obj interface{}) error
	Update(collectionName string, obj interface{}) error
	Delete(collectionName string, obj interface{}) error
}

// Use this interface to implement a database interface
// This will be used by the abstraction layer
type Adapter interface {
	SyncSchema(options SchemaSyncOptions, models ...interface{}) error
	Close() error
	Select(collectionName string, args interface{}) (interface{}, error)
	SelectInto(collectionName string, args interface{}, model interface{}) error
	SelectAdvanced(collectionName string, model interface{}, query *SelectQuery) error
	MutationAdapter
	DataFromDatabaseUsingResolveParams(objArr interface{}, modelType reflect.Type, p graphql.ResolveParams) error
	RunInTransaction(InnerTransactionFunc) error
}

// MutationAdapterOverlay overlays the methods of the
// mutation adapter on top of the Adapter ones.
// This is mainly used to provide the abstracted RunInTransaction
// functionality while keeping the database API the same
type MutationAdapterOverlay struct {
	fullAdapter     Adapter
	mutationAdapter MutationAdapter
}

func (overlay MutationAdapterOverlay) SyncSchema(options SchemaSyncOptions, models ...interface{}) error {
	return overlay.fullAdapter.SyncSchema(options, models)
}

func (overlay MutationAdapterOverlay) Close() error {
	return overlay.fullAdapter.Close()
}

func (overlay MutationAdapterOverlay) Select(collectionName string, args interface{}) (interface{}, error) {
	return overlay.fullAdapter.Select(collectionName, args)
}

func (overlay MutationAdapterOverlay) SelectInto(
	collectionName string, args interface{}, model interface{}) error {
	return overlay.fullAdapter.SelectInto(collectionName, args, model)
}

func (overlay MutationAdapterOverlay) SelectAdvanced(
	collectionName string, model interface{}, query *SelectQuery) error {
	return overlay.fullAdapter.SelectAdvanced(collectionName, model, query)
}

func (overlay MutationAdapterOverlay) CreateCollectionFromModel(name string, model interface{}, ifNotExists bool) error {
	return overlay.mutationAdapter.CreateCollectionFromModel(name, model, ifNotExists)
}

func (overlay MutationAdapterOverlay) Insert(cn string, obj interface{}) error {
	return overlay.mutationAdapter.Insert(cn, obj)
}

func (overlay MutationAdapterOverlay) Update(cn string, obj interface{}) error {
	return overlay.mutationAdapter.Update(cn, obj)
}

func (overlay MutationAdapterOverlay) Delete(cn string, obj interface{}) error {
	return overlay.mutationAdapter.Delete(cn, obj)
}

func (overlay MutationAdapterOverlay) DataFromDatabaseUsingResolveParams(objArr interface{}, modelType reflect.Type, p graphql.ResolveParams) error {
	return overlay.fullAdapter.DataFromDatabaseUsingResolveParams(objArr, modelType, p)
}

func (overlay MutationAdapterOverlay) RunInTransaction(fn InnerTransactionFunc) error {
	return overlay.fullAdapter.RunInTransaction(fn)
}