-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix (populate) now uses the param populate to drive populates behavio…
…ur :)
- Loading branch information
Showing
10 changed files
with
335 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.