Skip to content

Commit

Permalink
fix (populate) now uses the param populate to drive populates behavio…
Browse files Browse the repository at this point in the history
…ur :)
  • Loading branch information
pentateu committed Apr 1, 2019
1 parent 1684691 commit 48941b5
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 94 deletions.
92 changes: 92 additions & 0 deletions examples/customActions/posts-custom.service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"fmt"
"time"

db "github.com/moleculer-go/moleculer-db"

"github.com/moleculer-go/moleculer"
"github.com/moleculer-go/moleculer/broker"
)

func main() {
var bkr = broker.New(&moleculer.Config{LogLevel: "info"})
bkr.AddService(moleculer.Service{
Name: "users",
Settings: map[string]interface{}{
"fields": []string{"id", "username", "name"},
"populates": map[string]interface{}{"friends": "users.get"},
},
Mixins: []moleculer.Mixin{db.Mixin(&db.MongoAdapter{
MongoURL: "mongodb://localhost:27017",
Collection: "users",
Database: "services",
Timeout: time.Second * 10,
})},
})
bkr.AddService(moleculer.Service{
Name: "posts",
Settings: map[string]interface{}{
"populates": map[string]interface{}{
//Shorthand populate rule. Resolve the 'voters' values with the users.get action.
"voters": "users.get",
// Define the params of action call. It will receive only with username & full name of author.
"author": map[string]interface{}{
"action": "users.get",
"params": map[string]interface{}{
"fields": []string{"username", "fullname"},
},
},
},
},
Mixins: []moleculer.Mixin{db.Mixin(&db.MongoAdapter{
MongoURL: "mongodb://localhost:27017",
Collection: "posts",
Database: "services",
Timeout: time.Second * 10,
})},
})
bkr.Start()
time.Sleep(time.Millisecond * 300)

johnSnow := <-bkr.Call("users.create", map[string]interface{}{
"name": "John",
"lastname": "Snow",
"username": "jsnow",
"fullname": "John Snow",
})
marie := <-bkr.Call("users.create", map[string]interface{}{
"name": "Marie",
"lastname": "Claire",
"username": "mclaire",
"fullname": "Marie Claire",
})

post := <-bkr.Call("posts.create", map[string]interface{}{
"content": "Lorem ipsum dolor sit amet, consectetur ...",
"voters": []string{marie.Get("id").String()},
"author": johnSnow.Get("id").String(),
"status": 1,
})

// List posts with populated authors
fmt.Printf("posts with authors: ", <-bkr.Call("posts.find", map[string]interface{}{
"populate": []string{"author"},
}))

// remove post
<-bkr.Call("posts.remove", map[string]interface{}{
"id": post.Get("id").String(),
})

//remove users
<-bkr.Call("users.remove", map[string]interface{}{
"id": johnSnow.Get("id").String(),
})
<-bkr.Call("users.remove", map[string]interface{}{
"id": marie.Get("id").String(),
})

bkr.Stop()
}
Binary file added examples/populates/populates
Binary file not shown.
93 changes: 93 additions & 0 deletions examples/populates/posts.service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"fmt"
"time"

db "github.com/moleculer-go/moleculer-db"

"github.com/moleculer-go/moleculer"
"github.com/moleculer-go/moleculer/broker"
)

func main() {
var bkr = broker.New(&moleculer.Config{LogLevel: "info"})
bkr.AddService(moleculer.Service{
Name: "users",
Settings: map[string]interface{}{
"fields": []string{"id", "username", "name"},
"populates": map[string]interface{}{"friends": "users.get"},
},
Mixins: []moleculer.Mixin{db.Mixin(&db.MemoryAdapter{
Table: "users",
SearchFields: []string{"name", "username"},
})},
})
bkr.AddService(moleculer.Service{
Name: "posts",
Settings: map[string]interface{}{
"populates": map[string]interface{}{
//Shorthand populate rule. Resolve the 'voters' values with the users.get action.
"voters": "users.get",
// Define the params of action call.
//It will receive only with username of author.
"author": map[string]interface{}{
"action": "users.get",
"params": map[string]interface{}{
"fields": []string{"username"},
},
},
},
},
Mixins: []moleculer.Mixin{db.Mixin(&db.MemoryAdapter{
Table: "posts",
})},
})
bkr.Start()
time.Sleep(time.Millisecond * 300)

johnSnow := <-bkr.Call("users.create", map[string]interface{}{
"name": "John",
"lastname": "Snow",
"username": "jsnow",
"fullname": "John Snow",
})
marie := <-bkr.Call("users.create", map[string]interface{}{
"name": "Marie",
"lastname": "Claire",
"username": "mclaire",
"fullname": "Marie Claire",
})

post := <-bkr.Call("posts.create", map[string]interface{}{
"content": "Lorem ipsum dolor sit amet, consectetur ...",
"voters": []string{marie.Get("id").String()},
"author": johnSnow.Get("id").String(),
"status": 1,
})

// List posts with populated author
fmt.Printf("posts with author: ", <-bkr.Call("posts.find", map[string]interface{}{
"populate": []string{"author"},
}))

// List posts with populated voters
fmt.Printf("posts with voters: ", <-bkr.Call("posts.find", map[string]interface{}{
"populate": []string{"voters"},
}))

// remove post
<-bkr.Call("posts.remove", map[string]interface{}{
"id": post.Get("id").String(),
})

//remove users
<-bkr.Call("users.remove", map[string]interface{}{
"id": johnSnow.Get("id").String(),
})
<-bkr.Call("users.remove", map[string]interface{}{
"id": marie.Get("id").String(),
})

bkr.Stop()
}
24 changes: 9 additions & 15 deletions examples/users/users.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ import (
)

func main() {
var bkr = broker.New(&moleculer.Config{LogLevel: "debug"})
var bkr = broker.New(&moleculer.Config{LogLevel: "info"})
bkr.AddService(moleculer.Service{
Name: "users",
Settings: map[string]interface{}{
"fields": []string{"_id", "username", "name"},
"populates": map[string]interface{}{"friends": "users.get"},
},
Mixins: []moleculer.Mixin{db.Mixin(&db.MongoAdapter{
MongoURL: "mongodb://localhost:27017",
Collection: "user",
Database: "services",
Timeout: time.Second * 10,
Mixins: []moleculer.Mixin{db.Mixin(&db.MemoryAdapter{
Table: "users",
SearchFields: []string{"name", "username"},
})},
})
bkr.Start()
Expand All @@ -43,10 +41,10 @@ func main() {
"pageSize": 10,
}))

idParam := map[string]interface{}{"id": id}

// Get a user
fmt.Printf("get user: ", <-bkr.Call("users.get", map[string]interface{}{
"id": id,
}))
fmt.Printf("get user: ", <-bkr.Call("users.get", idParam))

// Update a user
fmt.Printf("update user: ", <-bkr.Call("users.update", map[string]interface{}{
Expand All @@ -55,14 +53,10 @@ func main() {
}))

// Print user after update
fmt.Printf("get user: ", <-bkr.Call("users.get", map[string]interface{}{
"id": id,
}))
fmt.Printf("get user: ", <-bkr.Call("users.get", idParam))

// Delete a user
fmt.Printf("remove user: ", <-bkr.Call("users.remove", map[string]interface{}{
"id": id,
}))
fmt.Printf("remove user: ", <-bkr.Call("users.remove", idParam))

bkr.Stop()
}
43 changes: 39 additions & 4 deletions memory_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,48 @@ import (

//MemoryAdapter stores data in memory!
type MemoryAdapter struct {
Schema *memdb.DBSchema
Table string
db *memdb.MemDB
//Schema *memdb.DBSchema
SearchFields []string
Table string
db *memdb.MemDB
}

func (adapter *MemoryAdapter) generateSchema() *memdb.DBSchema {

Indexes := map[string]*memdb.IndexSchema{
"id": &memdb.IndexSchema{
Name: "id",
Unique: true,
Indexer: &PayloadIndex{Field: "id"},
},
"all": &memdb.IndexSchema{
Name: "all",
Unique: false,
Indexer: &PayloadIndex{Field: "all"},
},
}
if adapter.SearchFields != nil {
for _, field := range adapter.SearchFields {
Indexes[field] = &memdb.IndexSchema{
Name: field,
Unique: false,
Indexer: &PayloadIndex{Field: field},
}
}
}
return &memdb.DBSchema{
Tables: map[string]*memdb.TableSchema{
adapter.Table: &memdb.TableSchema{
Name: adapter.Table,
Indexes: Indexes,
},
},
}
}

func (adapter *MemoryAdapter) Connect() error {
db, err := memdb.NewMemDB(adapter.Schema)
schema := adapter.generateSchema()
db, err := memdb.NewMemDB(schema)
if err != nil {
return err
}
Expand Down
9 changes: 6 additions & 3 deletions memory_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (

var _ = Describe("MemoryAdapter", func() {

adapter := memoryAdapter("user", userDbSchema)
adapter := &MemoryAdapter{
Table: "user",
SearchFields: []string{"name"},
}

var johnSnow, johnTravolta moleculer.Payload
BeforeEach(func() {
Expand All @@ -30,7 +33,7 @@ var _ = Describe("MemoryAdapter", func() {
}))
Expect(r.IsError()).Should(BeFalse())
Expect(r.Len()).Should(Equal(2))
Expect(snap.SnapshotMulti("Find()", r.Remove("id", "friends"))).Should(Succeed())
Expect(snap.SnapshotMulti("Find()", r.Remove("id", "friends", "master"))).Should(Succeed())
})

It("FindById() should return one matching records by ID", func() {
Expand All @@ -43,7 +46,7 @@ var _ = Describe("MemoryAdapter", func() {
r := adapter.FindByIds(payload.EmptyList().AddItem(johnSnow.Get("id")).AddItem(johnTravolta.Get("id")))
Expect(r.IsError()).Should(BeFalse())
Expect(r.Len()).Should(Equal(2))
Expect(snap.SnapshotMulti("FindByIds()", r.Remove("id", "friends"))).Should(Succeed())
Expect(snap.SnapshotMulti("FindByIds()", r.Remove("id", "friends", "master"))).Should(Succeed())
})

It("Count() should return matching records", func() {
Expand Down
1 change: 1 addition & 0 deletions mocks/test_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func ConnectAndLoadUsers(adapter Adapter) (moleculer.Payload, moleculer.Payload,
"name": "John",
"lastname": "Travolta",
"age": 65,
"master": johnSnow.Get("id").String(),
"friends": []string{johnSnow.Get("id").String(), marie.Get("id").String()},
}))

Expand Down
Loading

0 comments on commit 48941b5

Please sign in to comment.