mongo

package module
v1.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2022 License: MIT Imports: 12 Imported by: 0

README

Rested MongoDB Backend

Go Reference

This storage backend currently stores data in a MongoDB cluster using the now deprecated mgo driver.

Usage

import "github.com/clarify/rested/storers/mongo"

Create a mgo master session:

session, err := mgo.Dial(url)

Create a resource storage handler with a given DB/collection:

s := mongo.NewHandler(session, "the_db", "the_collection")

Use this handler with a resource:

index.Bind("foo", foo, s, resource.DefaultConf)

You may want to create a many mongo handlers as you have resources as long as you want each resources in a different collection. You can share the same mgo session across all you handlers.

Object ID

This package also provides a REST Layer schema.Validator for MongoDB ObjectIDs. This validator ensures proper binary serialization of the Object ID in the database for space efficiency.

You may reference this validator using mongo.ObjectID as schema.Field.

A mongo.NewObjectID field hook and mongo.ObjectIDField helper are also provided.

Documentation

Overview

Package mongo is a REST Layer resource storage handler for MongoDB using mgo

Index

Constants

This section is empty.

Variables

View Source
var (
	// NewObjectID is a field hook handler that generates a new Mongo ObjectID hex if
	// value is nil to be used in schema with OnInit.
	NewObjectID = func(ctx context.Context, value interface{}) interface{} {
		if value == nil {
			value = primitive.NewObjectID().Hex()
		}
		return value
	}

	// ObjectIDField is a common schema field configuration that generate an Object ID
	// for new item id.
	ObjectIDField = schema.Field{
		Required:   true,
		ReadOnly:   true,
		OnInit:     NewObjectID,
		Filterable: true,
		Sortable:   true,
		Validator:  &ObjectID{},
	}
)
View Source
var (
	// Now is a field hook handler that returns the current time, to be used in
	// schema with OnInit and OnUpdate.
	Now = func(ctx context.Context, value interface{}) interface{} {
		return time.Now()
	}
	// CreatedField is a common schema field configuration for "created" fields.
	// It stores the creation date of the item.
	CreatedField = schema.Field{
		Description: "The time at which the item has been inserted",
		Required:    true,
		ReadOnly:    true,
		OnInit:      Now,
		Sortable:    true,
		Filterable:  true,
		Validator:   &Time{},
	}

	// UpdatedField is a common schema field configuration for "updated" fields.
	// It stores the current date each time the item is modified.
	UpdatedField = schema.Field{
		Description: "The time at which the item has been last updated",
		Required:    true,
		ReadOnly:    true,
		OnInit:      Now,
		OnUpdate:    Now,
		Sortable:    true,
		Filterable:  true,
		Validator:   &Time{},
	}
)

Functions

This section is empty.

Types

type Handler

type Handler func(ctx context.Context) (*mongo.Collection, error)

Handler handles resource storage in a MongoDB collection.

func NewHandler

func NewHandler(c *mongo.Client, db, collectionName string) Handler

NewHandler creates an new mongo handler

func (Handler) Clear

func (m Handler) Clear(ctx context.Context, q *query.Query) (int, error)

Clear clears all items from the mongo collection matching the query. Note that when q.Window != nil, the current implementation may error if the BSON encoding of all matching IDs according to the q.Window length gets close to the maximum document size in MongDB (usually 16MiB): https://docs.mongodb.com/manual/reference/limits/#bson-documents

func (Handler) Count

func (m Handler) Count(ctx context.Context, query *query.Query) (int, error)

Count counts the number items matching the lookup filter

func (Handler) Delete

func (m Handler) Delete(ctx context.Context, item *resource.Item) error

Delete deletes an item from the mongo collection.

func (Handler) Find

func (m Handler) Find(ctx context.Context, q *query.Query) (*resource.ItemList, error)

Find items from the mongo collection matching the provided query.

func (Handler) Insert

func (m Handler) Insert(ctx context.Context, items []*resource.Item) error

Insert inserts new items in the mongo collection.

func (Handler) Update

func (m Handler) Update(ctx context.Context, item *resource.Item, original *resource.Item) error

Update replace an item by a new one in the mongo collection.

type ObjectID

type ObjectID struct{}

ObjectID validates and serialize unique id

func (ObjectID) BuildJSONSchema

func (v ObjectID) BuildJSONSchema() (map[string]interface{}, error)

BuildJSONSchema implements the jsonschema.Builder interface.

func (ObjectID) Validate

func (v ObjectID) Validate(value interface{}) (interface{}, error)

Validate implements FieldValidator interface

type OptionalReference added in v1.7.0

type OptionalReference struct {
	Path string

	SchemaValidator schema.Validator
	// contains filtered or unexported fields
}

OptionalReference validates the ID of a linked resource.

func (*OptionalReference) Compile added in v1.7.0

Compile validates v.Path against rc and stores the a FieldValidator for later use by v.Validate.

func (OptionalReference) GetField added in v1.7.0

func (r OptionalReference) GetField(name string) *schema.Field

GetField implements the FieldGetter interface.

func (OptionalReference) Validate added in v1.7.0

func (r OptionalReference) Validate(value interface{}) (interface{}, error)

Validate validates and sanitizes IDs against the reference path.

type Time

type Time struct {
	TimeLayouts []string // TimeLayouts is set of time layouts we want to validate.
	// contains filtered or unexported fields
}

Time validates time based values

func (*Time) Compile

func (v *Time) Compile(rc schema.ReferenceChecker) error

Compile the time formats.

func (Time) LessFunc

func (v Time) LessFunc() schema.LessFunc

LessFunc implements the FieldComparator interface.

func (Time) Validate

func (v Time) Validate(value interface{}) (interface{}, error)

Validate validates and normalize time based value.

func (Time) ValidateQuery

func (v Time) ValidateQuery(value interface{}) (interface{}, error)

ValidateQuery implements schema.FieldQueryValidator interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL