-
Notifications
You must be signed in to change notification settings - Fork 2
/
moleculer-db.js
159 lines (129 loc) · 5.79 KB
/
moleculer-db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* *******************************************************************************************
* MOLECULER DB CHEATSHEET
* https://github.com/moleculerjs/moleculer-db/tree/master/packages/moleculer-db#readme
*
* Version: 0.6.x
* ******************************************************************************************* */
/* *******************************************************************************************
* Install Moleculer DB
* ******************************************************************************************* */
```bash
npm i moleculer-db
```
/* *******************************************************************************************
* USAGE
* ******************************************************************************************* */
const DbService = require("moleculer-db");
module.exports = {
name: "users",
mixins: [DbService],
settings: {
fields: ["_id", "username", "name"]
},
afterConnected() {
// Seed the DB if you want
}
}
/* *******************************************************************************************
* SETTINGS
* ******************************************************************************************* */
module.exports = {
settings: {
// Name of ID field
idField: "_id",
// Field filter list
fields: ["_id", "title", "content"],
// Schema of population
populates: {
// Shorthand populate rule. Resolve the `voters` values with `users.get` action.
"voters": "users.get",
// Define the params of action call. It will receive only with username & full name of author.
"author": {
action: "users.get",
params: {
fields: "title fullName"
},
// Custom populator handler function (virtual field)
"votes"(ids, rule, ctx) {
return this.Promise.all(books.map(async post => post.votes = await ctx.call("votes.count", { query: { post: post._id } })));
}
},
},
// Default page size in list action.
pageSize: 10,
// Maximum page size in list action.
maxPageSize: 50,
// Maximum value of limit in find action. Default: -1 (no limit)
maxLimit: -1,
// Validator schema or a function to validate the incoming entity in create & 'insert' actions.
entityValidator: {
username: { type: "string", min: 6 },
content: { type: "string" }
}
},
}
/* *******************************************************************************************
* EXPOSED ACTIONS
* ******************************************************************************************* */
// Find entities with limit, offset & sort
broker.call("posts.find", { limit: 20, offset: 10, sort: ["-createdAt"] });
// Find with custom query, populate & fields
broker.call("posts.find", { query: { title: "First post" }, populate: ["author"], fields: ["title", "author.name"]});
// Find with full-text search (if adapter supports)
broker.call("posts.find", { search: "lorem" });
// Count of entities
broker.call("posts.count");
// Count with custom query
broker.call("posts.count", { query: { title: "First post" }});
// Count with full-text search (if adapter supports)
broker.call("posts.count", { search: "lorem" });
// List entities with paging
broker.call("posts.list", { page: 5, pageSize: 10, sort: ["-createdAt"] });
// List with custom query, populate & fields
broker.call("posts.list", { query: { title: "First post" }, populate: ["author"], fields: ["title", "author.name"]});
// List with full-text search (if adapter supports)
broker.call("posts.list", { search: "lorem" });
// Create an entity
broker.call("posts.create", { title: "New port", content: "My content" });
// Create multiple entities
broker.call("posts.insert", { entities: [
{ title: "First post", content: "First content" },
{ title: "Second post", content: "Second content" }
]});
// Get entity by ID
broker.call("posts.get", { id: 3, fields: ["title"] });
// Get entity with populate & fields
broker.call("posts.get", { id: [3, 5, 8], populate: ["author"], fields: ["title", "author.name"] });
// Update entity
broker.call("posts.update", { id: 5, title: "Modified title" });
// Remove entity
broker.call("posts.remove", { id: 5 });
/* *******************************************************************************************
* METHODS
* ******************************************************************************************* */
this.getById(5); // Get entity(ies) by ID(s).
this.getById("100");
this.getById([1, 3, 6, 8, 10]);
this.clearCache(); // Clear cached entities
this.encodeID(); // Encode ID of entity.
this.decodeID(); // Decode ID of entity.
this.sanitizeParams(ctx, params); // Sanitize context parameters at `find` action.
this.transformDocuments(ctx, params, docs); // Transform the fetched documents
this.validateEntity(entity); // Validate an entity by validator.
/* *******************************************************************************************
* ADAPTERS METHODS
* ******************************************************************************************* */
this.adapter.find(filters);
this.adapter.findOne(filters);
this.adapter.findById(id);
this.adapter.findByIds(ids);
this.adapter.count(filters);
this.adapter.insert(entity);
this.adapter.insertMany(entities);
this.adapter.updateMany(query, update);
this.adapter.updateById(id, update);
this.adapter.removeMany(query);
this.adapter.removeById(id);
this.adapter.clear();
this.adapter.entityToObject();
this.adapter.createCursor(params);