mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-12 03:05:14 +00:00
remove the datastore
This commit is contained in:
2
datastore/.gitignore
vendored
2
datastore/.gitignore
vendored
@@ -1,2 +0,0 @@
|
||||
|
||||
datastore
|
||||
@@ -1,3 +0,0 @@
|
||||
FROM alpine
|
||||
ADD datastore /datastore
|
||||
ENTRYPOINT [ "/datastore" ]
|
||||
@@ -1,27 +0,0 @@
|
||||
|
||||
GOPATH:=$(shell go env GOPATH)
|
||||
.PHONY: init
|
||||
init:
|
||||
go get -u github.com/golang/protobuf/proto
|
||||
go get -u github.com/golang/protobuf/protoc-gen-go
|
||||
go get github.com/micro/micro/v3/cmd/protoc-gen-micro
|
||||
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
|
||||
.PHONY: proto
|
||||
proto:
|
||||
protoc --openapi_out=. --proto_path=. --micro_out=. --go_out=:. proto/datastore.proto
|
||||
|
||||
.PHONY: api
|
||||
api:
|
||||
protoc --openapi_out=. --proto_path=. proto/datastore.proto
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
go build -o datastore *.go
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
go test -v ./... -cover
|
||||
|
||||
.PHONY: docker
|
||||
docker:
|
||||
docker build . -t datastore:latest
|
||||
@@ -1,6 +0,0 @@
|
||||
Document data storage
|
||||
|
||||
# Datastore Service
|
||||
|
||||
The Datastore service is a document store with a simplified query model for everyone.
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
package main
|
||||
|
||||
//go:generate make proto
|
||||
@@ -1,140 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
log "github.com/micro/micro/v3/service/logger"
|
||||
"github.com/micro/micro/v3/service/model"
|
||||
|
||||
datastore "github.com/micro/services/datastore/proto"
|
||||
)
|
||||
|
||||
var indexIndex = model.Index{
|
||||
FieldName: "TypeOf",
|
||||
}
|
||||
|
||||
type IndexRecord struct {
|
||||
ID string
|
||||
TypeOf string
|
||||
Index model.Index
|
||||
}
|
||||
|
||||
type Datastore struct {
|
||||
}
|
||||
|
||||
func (e *Datastore) Create(ctx context.Context, req *datastore.CreateRequest, rsp *datastore.CreateResponse) error {
|
||||
log.Info("Received Datastore.Create request")
|
||||
m := map[string]interface{}{}
|
||||
err := json.Unmarshal([]byte(req.Value), &m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
indexes, err := e.getIndexes(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db := model.New(map[string]interface{}{}, &model.Options{
|
||||
Indexes: indexes,
|
||||
})
|
||||
return db.Context(ctx).Create(m)
|
||||
}
|
||||
|
||||
func (e *Datastore) Update(ctx context.Context, req *datastore.UpdateRequest, rsp *datastore.UpdateResponse) error {
|
||||
log.Info("Received Datastore.Update request")
|
||||
m := map[string]interface{}{}
|
||||
err := json.Unmarshal([]byte(req.Value), &m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
indexes, err := e.getIndexes(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db := model.New(map[string]interface{}{}, &model.Options{
|
||||
Indexes: indexes,
|
||||
})
|
||||
return db.Context(ctx).Update(m)
|
||||
}
|
||||
|
||||
func (e *Datastore) getIndexes(ctx context.Context) ([]model.Index, error) {
|
||||
indexDb := model.New(map[string]interface{}{}, &model.Options{
|
||||
Indexes: []model.Index{indexIndex},
|
||||
})
|
||||
result := []IndexRecord{}
|
||||
err := indexDb.Context(ctx).Read(model.QueryEquals("TypeOf", "_index"), &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
indexes := []model.Index{}
|
||||
for _, v := range result {
|
||||
indexes = append(indexes, v.Index)
|
||||
}
|
||||
return indexes, nil
|
||||
}
|
||||
|
||||
func (e *Datastore) Read(ctx context.Context, req *datastore.ReadRequest, rsp *datastore.ReadResponse) error {
|
||||
log.Info("Received Datastore.Read request")
|
||||
q := toQuery(req.Query)
|
||||
result := []map[string]interface{}{}
|
||||
indexes, err := e.getIndexes(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db := model.New(map[string]interface{}{}, &model.Options{
|
||||
Indexes: indexes,
|
||||
})
|
||||
err = db.Context(ctx).Read(q, &result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
js, err := json.Marshal(result)
|
||||
rsp.Value = string(js)
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *Datastore) CreateIndex(ctx context.Context, req *datastore.CreateIndexRequest, rsp *datastore.CreateIndexResponse) error {
|
||||
log.Info("Received Datastore.Index request")
|
||||
|
||||
index := toIndex(req.Index)
|
||||
indexRecord := IndexRecord{
|
||||
ID: index.FieldName + index.Type + index.Order.FieldName + string(index.Order.Type),
|
||||
Index: index,
|
||||
TypeOf: "_index",
|
||||
}
|
||||
db := model.New(IndexRecord{}, &model.Options{
|
||||
Indexes: []model.Index{indexIndex},
|
||||
})
|
||||
return db.Context(ctx).Create(indexRecord)
|
||||
}
|
||||
|
||||
func (e *Datastore) Delete(ctx context.Context, req *datastore.DeleteRequest, rsp *datastore.DeleteResponse) error {
|
||||
log.Info("Received Datastore.Delete request")
|
||||
q := toQuery(req.Query)
|
||||
return model.New(map[string]interface{}{}, nil).Context(ctx).Delete(q)
|
||||
}
|
||||
|
||||
func toQuery(pquery *datastore.Query) model.Query {
|
||||
q := model.QueryEquals(pquery.Index.FieldName, pquery.Value)
|
||||
if pquery.Order != nil {
|
||||
q.Order.FieldName = pquery.Order.FieldName
|
||||
q.Order.Type = model.OrderType(pquery.Order.OrderType.String())
|
||||
}
|
||||
return q
|
||||
}
|
||||
|
||||
func toIndex(pindex *datastore.Index) model.Index {
|
||||
i := model.Index{
|
||||
FieldName: pindex.FieldName,
|
||||
Type: pindex.Type,
|
||||
Unique: pindex.Unique,
|
||||
}
|
||||
if pindex.Order != nil {
|
||||
i.Order = model.Order{
|
||||
FieldName: pindex.FieldName,
|
||||
Type: model.OrderType(strings.ToLower(pindex.Order.OrderType.String())),
|
||||
}
|
||||
}
|
||||
return i
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/micro/services/datastore/handler"
|
||||
pb "github.com/micro/services/datastore/proto"
|
||||
|
||||
"github.com/micro/micro/v3/service"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create service
|
||||
srv := service.New(
|
||||
service.Name("datastore"),
|
||||
service.Version("latest"),
|
||||
)
|
||||
|
||||
// Register handler
|
||||
pb.RegisterDatastoreHandler(srv.Server(), new(handler.Datastore))
|
||||
|
||||
// Run service
|
||||
if err := srv.Run(); err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
service datastore
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,161 +0,0 @@
|
||||
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||
// source: proto/datastore.proto
|
||||
|
||||
package datastore
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
proto "github.com/golang/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
import (
|
||||
context "context"
|
||||
api "github.com/micro/micro/v3/service/api"
|
||||
client "github.com/micro/micro/v3/service/client"
|
||||
server "github.com/micro/micro/v3/service/server"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the proto package it is being compiled against.
|
||||
// A compilation error at this line likely means your copy of the
|
||||
// proto package needs to be updated.
|
||||
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ api.Endpoint
|
||||
var _ context.Context
|
||||
var _ client.Option
|
||||
var _ server.Option
|
||||
|
||||
// Api Endpoints for Datastore service
|
||||
|
||||
func NewDatastoreEndpoints() []*api.Endpoint {
|
||||
return []*api.Endpoint{}
|
||||
}
|
||||
|
||||
// Client API for Datastore service
|
||||
|
||||
type DatastoreService interface {
|
||||
Create(ctx context.Context, in *CreateRequest, opts ...client.CallOption) (*CreateResponse, error)
|
||||
Update(ctx context.Context, in *UpdateRequest, opts ...client.CallOption) (*UpdateResponse, error)
|
||||
Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error)
|
||||
Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error)
|
||||
CreateIndex(ctx context.Context, in *CreateIndexRequest, opts ...client.CallOption) (*CreateIndexResponse, error)
|
||||
}
|
||||
|
||||
type datastoreService struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewDatastoreService(name string, c client.Client) DatastoreService {
|
||||
return &datastoreService{
|
||||
c: c,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *datastoreService) Create(ctx context.Context, in *CreateRequest, opts ...client.CallOption) (*CreateResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Datastore.Create", in)
|
||||
out := new(CreateResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *datastoreService) Update(ctx context.Context, in *UpdateRequest, opts ...client.CallOption) (*UpdateResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Datastore.Update", in)
|
||||
out := new(UpdateResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *datastoreService) Read(ctx context.Context, in *ReadRequest, opts ...client.CallOption) (*ReadResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Datastore.Read", in)
|
||||
out := new(ReadResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *datastoreService) Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Datastore.Delete", in)
|
||||
out := new(DeleteResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *datastoreService) CreateIndex(ctx context.Context, in *CreateIndexRequest, opts ...client.CallOption) (*CreateIndexResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Datastore.CreateIndex", in)
|
||||
out := new(CreateIndexResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for Datastore service
|
||||
|
||||
type DatastoreHandler interface {
|
||||
Create(context.Context, *CreateRequest, *CreateResponse) error
|
||||
Update(context.Context, *UpdateRequest, *UpdateResponse) error
|
||||
Read(context.Context, *ReadRequest, *ReadResponse) error
|
||||
Delete(context.Context, *DeleteRequest, *DeleteResponse) error
|
||||
CreateIndex(context.Context, *CreateIndexRequest, *CreateIndexResponse) error
|
||||
}
|
||||
|
||||
func RegisterDatastoreHandler(s server.Server, hdlr DatastoreHandler, opts ...server.HandlerOption) error {
|
||||
type datastore interface {
|
||||
Create(ctx context.Context, in *CreateRequest, out *CreateResponse) error
|
||||
Update(ctx context.Context, in *UpdateRequest, out *UpdateResponse) error
|
||||
Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error
|
||||
Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error
|
||||
CreateIndex(ctx context.Context, in *CreateIndexRequest, out *CreateIndexResponse) error
|
||||
}
|
||||
type Datastore struct {
|
||||
datastore
|
||||
}
|
||||
h := &datastoreHandler{hdlr}
|
||||
return s.Handle(s.NewHandler(&Datastore{h}, opts...))
|
||||
}
|
||||
|
||||
type datastoreHandler struct {
|
||||
DatastoreHandler
|
||||
}
|
||||
|
||||
func (h *datastoreHandler) Create(ctx context.Context, in *CreateRequest, out *CreateResponse) error {
|
||||
return h.DatastoreHandler.Create(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *datastoreHandler) Update(ctx context.Context, in *UpdateRequest, out *UpdateResponse) error {
|
||||
return h.DatastoreHandler.Update(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *datastoreHandler) Read(ctx context.Context, in *ReadRequest, out *ReadResponse) error {
|
||||
return h.DatastoreHandler.Read(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *datastoreHandler) Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error {
|
||||
return h.DatastoreHandler.Delete(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *datastoreHandler) CreateIndex(ctx context.Context, in *CreateIndexRequest, out *CreateIndexResponse) error {
|
||||
return h.DatastoreHandler.CreateIndex(ctx, in, out)
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package datastore;
|
||||
|
||||
option go_package = "./proto;datastore";
|
||||
|
||||
// These endpoints are experimental and will likely change,
|
||||
// especially related to indexes.
|
||||
service Datastore {
|
||||
rpc Create(CreateRequest) returns (CreateResponse) {}
|
||||
rpc Update(UpdateRequest) returns (UpdateResponse) {}
|
||||
rpc Read(ReadRequest) returns (ReadResponse) {}
|
||||
rpc Delete(DeleteRequest) returns (DeleteResponse) {}
|
||||
rpc CreateIndex(CreateIndexRequest) returns (CreateIndexResponse) {}
|
||||
}
|
||||
|
||||
message Query {
|
||||
Index index = 1;
|
||||
Order order = 2;
|
||||
string value = 3;
|
||||
int64 offset = 4;
|
||||
int64 limit = 5;
|
||||
}
|
||||
|
||||
// Order is the order of the index
|
||||
message Order {
|
||||
// Field to order on
|
||||
// eg. age
|
||||
string fieldName = 1;
|
||||
// Ordered or unordered keys. Ordered keys are padded.
|
||||
// Default is true. This option only exists for strings, where ordering
|
||||
// comes at the cost of having rather long padded keys.
|
||||
enum OrderType {
|
||||
UNORDERED = 0;
|
||||
ASCENDING = 1;
|
||||
DESCENDING = 2;
|
||||
}
|
||||
// Type of the ordering
|
||||
// eg. ascending, descending, unordered
|
||||
OrderType orderType = 2;
|
||||
}
|
||||
|
||||
message Index {
|
||||
// Field to index on.
|
||||
// eg. email
|
||||
string fieldName = 1;
|
||||
// Type of index
|
||||
// eg. eq
|
||||
string type = 2;
|
||||
Order order = 3;
|
||||
|
||||
// Do not allow duplicate values of this field in the index.
|
||||
// Useful for emails, usernames, post slugs etc.
|
||||
bool unique = 4;
|
||||
|
||||
// Strings for ordering will be padded to a fix length
|
||||
// Not a useful property for Querying, please ignore this at query time.
|
||||
// Number is in bytes, not string characters. Choose a sufficiently big one.
|
||||
// Consider that each character might take 4 bytes given the
|
||||
// internals of reverse ordering. So a good rule of thumbs is expected
|
||||
// characters in a string X 4
|
||||
int64 stringOrderPadLength = 5;
|
||||
// True = base32 encode ordered strings for easier management
|
||||
// or false = keep 4 bytes long runes that might dispaly weirdly
|
||||
bool Base32Encode = 6;
|
||||
|
||||
string FloatFormat = 7;
|
||||
float Float64Max = 8;
|
||||
float Float32Max = 9;
|
||||
}
|
||||
|
||||
|
||||
message CreateRequest {
|
||||
// JSON marshalled record to save
|
||||
string value = 1;
|
||||
}
|
||||
|
||||
message CreateResponse {
|
||||
}
|
||||
|
||||
message UpdateRequest {
|
||||
// JSON marshalled record to save
|
||||
string value = 1;
|
||||
}
|
||||
|
||||
message UpdateResponse {
|
||||
|
||||
}
|
||||
|
||||
message ReadRequest {
|
||||
Query query = 1;
|
||||
}
|
||||
|
||||
message ReadResponse {
|
||||
// JSON marshalled record found
|
||||
string value = 1;
|
||||
}
|
||||
|
||||
message DeleteRequest {
|
||||
Query query = 1;
|
||||
}
|
||||
|
||||
message DeleteResponse {
|
||||
}
|
||||
|
||||
message CreateIndexRequest {
|
||||
Index index = 1;
|
||||
}
|
||||
|
||||
message CreateIndexResponse {
|
||||
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
{
|
||||
"name": "datastore",
|
||||
"icon": "📦",
|
||||
"category": "storage",
|
||||
"pricing": {
|
||||
"Datastore.Create": 100,
|
||||
"Datastore.Read": 100,
|
||||
"Datastore.Update": 100,
|
||||
"Datastore.Delete": 100,
|
||||
"Datastore.CreateIndex": 100
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user