Skip to content

Commit

Permalink
add createMany, list & new settings
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Jul 7, 2017
1 parent b3580db commit b09fdde
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 19 deletions.
61 changes: 52 additions & 9 deletions databases/moleculer-db/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
<a name="0.3.0"></a>
# 0.3.0 (2017-07-07)

## New

### New `createMany` method
A new `createMany` method is created. With it you can insert many entities to the database.

```js
this.createMany(ctx, {
entities: [...]
});
```

### New `list` action with pagination
There is a new `list` action with pagination support.

```js
broker.call("posts.list", { page: 2, pageSize: 10});
```
The result is similar as
```js
{
rows: [
{ title: 'Post #26' },
{ title: 'Post #27' },
{ title: 'Post #25' },
{ title: 'Post #21' },
{ title: 'Post #28' }
],
total: 28,
page: 2,
pageSize: 10,
totalPage: 3
}
```

### New settings
- `pageSize` - Default page size in `list` action. Default: `10`
- `maxPageSize` - Maximum page size in `list` action. Default: `100`
- `maxLimit` - Maximum value of limit in `find` action. Default: `-1` (no limit)

--------------------------------------------------
<a name="0.2.0"></a>
# 0.2.0 (2017-07-06)

Expand All @@ -14,14 +57,14 @@ We removed the `clear` action from service The reason is if you don't filter it
After all if you need it:
```js
module.exports = {
name: "posts",
mixins: [DbService],

actions: {
clear(ctx) {
return this.clear(ctx);
}
}
name: "posts",
mixins: [DbService],

actions: {
clear(ctx) {
return this.clear(ctx);
}
}
}
```

Expand All @@ -30,4 +73,4 @@ module.exports = {
- `update` renamed to `updateMany`
- `remove` renamed to `removeMany`

--------------------------------------------------
--------------------------------------------------
5 changes: 4 additions & 1 deletion databases/moleculer-db/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ broker.start()
| ---- | ------ | ------ | ----------- |
| `find` | `limit`, `offset`, `sort`, `search`, `searchFields` | `Array` | Find matched entities. |
| `count` | `search`, `searchFields` | `Number` | Count of matched entities. |
| `create` | `entity` | `Object` | Save a new entity. |
| `create` | `entity` | `Object` | Create a new entity. |
| `get` | `id` | `Object` | Get an entity by ID. |
| `model` | `id`, `populate`, `fields`, `resultAsObject` | `Object` | Get entities by ID/IDs. **For internal use only!** |
| `update` | `id`, `update` | `Object` | Update an entity by ID. |
Expand All @@ -90,6 +90,9 @@ Get count of find entities by filters. The `params` will be passed to the adapte
### `this.create(ctx, params)`
Create a new entity. The `params.entity` will be passed to the adapter.

### `this.createMany(ctx, params)`
Create many new entities. The `params.entities` will be passed to the adapter.

### `this.get(ctx, params)`
Get an entities by ID. The `params.id` will be passed to the adapter.

Expand Down
83 changes: 83 additions & 0 deletions databases/moleculer-db/examples/pagination/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"use strict";

let _ = require("lodash");
let chalk = require("chalk");
let { ServiceBroker } = require("moleculer");
let DbService = require("../../index");

// Create broker
let broker = new ServiceBroker({
logger: console,
logLevel: "debug"
});

// Load my service
broker.createService(DbService, {
name: "posts",
settings: {
fields: "title"
},

methods: {
seedDB() {
return this.createMany(null, { entities: _.times(28, i => {
return {
title: `Post #${_.padStart(i + 1, 2, "0")}`
};
})});
}
},

afterConnected() {
this.logger.info(chalk.green.bold("Connected successfully"));
this.clear();

this.seedDB();
}
});

// Start server
broker.start().delay(500).then(() => {
let id;
Promise.resolve()
// Count of posts
.then(() => console.log(chalk.yellow.bold("\n--- COUNT ---")))
.then(() => broker.call("posts.count").then(console.log))

// Find posts
.then(() => console.log(chalk.yellow.bold("\n--- FIND ---")))
.then(() => broker.call("posts.find", { sort: "title" }).then(console.log))

// List posts
.then(() => console.log(chalk.yellow.bold("\n--- LIST FIRST 10 ---")))
.then(() => broker.call("posts.list", { sort: "title" }).then(console.log))

// List posts
.then(() => console.log(chalk.yellow.bold("\n--- LIST LAST 10 ---")))
.then(() => broker.call("posts.list", { sort: "-title" }).then(console.log))

// List posts
.then(() => console.log(chalk.yellow.bold("\n--- LIST FIRST 25 ---")))
.then(() => broker.call("posts.list", { page: 1, pageSize: 25, sort: "title" }).then(console.log))

// List posts
.then(() => console.log(chalk.yellow.bold("\n--- LIST NEXT 25 ---")))
.then(() => broker.call("posts.list", { page: 2, pageSize: 25, sort: "title" }).then(console.log))

// List posts
.then(() => console.log(chalk.yellow.bold("\n--- LIST NEXT2 25 ---")))
.then(() => broker.call("posts.list", { page: 3, pageSize: 25, sort: "title" }).then(console.log))

// List posts with search
.then(() => console.log(chalk.yellow.bold("\n--- LIST SEARCH 5 ---")))
.then(() => broker.call("posts.list", { page: 1, pageSize: 5, search: "#2" }).then(console.log))


// Error handling
.catch(console.error)

// Stop
.then(() => broker.stop());


});
111 changes: 102 additions & 9 deletions databases/moleculer-db/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ module.exports = {

// Validator schema or a function to validate the incoming entity in "users.create" action
entityValidator: null,

// Default page size
pageSize: 10,

// Maximum page size
maxPageSize: 100,

// Maximum value of limit in `find` action
maxLimit: -1
},

/**
Expand All @@ -54,12 +63,19 @@ module.exports = {
searchFields: { type: "array", optional: true }
},
handler(ctx) {
if (typeof(ctx.params.limit) === "string")
ctx.params.limit = Number(ctx.params.limit);
if (typeof(ctx.params.offset) === "string")
ctx.params.offset = Number(ctx.params.offset);

return this.find(ctx, ctx.params);
let params = Object.assign({}, ctx.params);

// Convert from string to number
if (typeof(params.limit) === "string")
params.limit = Number(params.limit);
if (typeof(params.offset) === "string")
params.offset = Number(params.offset);

// Limit the `limit`
if (this.settings.maxLimit > 0 && params.limit > this.settings.maxLimit)
params.limit = this.settings.maxLimit;

return this.find(ctx, params);
}
},

Expand All @@ -81,6 +97,64 @@ module.exports = {
}
},

list: {
cache: {
keys: ["page", "pageSize", "sort", "search", "searchFields"]
},
params: {
page: { type: "number", integer: true, min: 1, optional: true, convert: true },
pageSize: { type: "number", integer: true, min: 0, optional: true, convert: true },
sort: { type: "string", optional: true },
search: { type: "string", optional: true },
searchFields: { type: "array", optional: true }
},
handler(ctx) {
let params = Object.assign({}, ctx.params);
// Convert from string to number
if (typeof(params.page) === "string")
params.page = Number(params.page);
if (typeof(params.pageSize) === "string")
params.pageSize = Number(params.pageSize);

// Default `pageSize`
if (!params.pageSize)
params.pageSize = this.settings.pageSize;

// Default `page`
if (!params.page)
params.page = 1;

// Limit the `pageSize`
if (this.settings.maxPageSize > 0 && params.pageSize > this.settings.maxPageSize)
params.pageSize = this.settings.maxPageSize;

// Calculate the limit & offset from page & pageSize
params.limit = params.pageSize;
params.offset = (params.page - 1) * params.pageSize;

return this.Promise.all([
// Get rows
this.find(ctx, params),

// Get count of all rows
this.count(ctx, params)
]).then(res => {
return {
// Rows
rows: res[0],
// Total rows
total: res[1],
// Page
page: params.page,
// Page size
pageSize: params.pageSize,
// Total pages
totalPage: Math.floor((res[1] + params.pageSize - 1) / params.pageSize)
};
});
}
},

/**
* Create a new entity
*/
Expand Down Expand Up @@ -206,14 +280,19 @@ module.exports = {
* @returns
*/
count(ctx, params) {
if (params.limit)
params.limit = null;
if (params.offset)
params.offset = null;

return this.adapter.count(params);
},

/**
* Create a new entity
*
* @param {Context} ctx
* @param {Object} entity
* @param {Object} params
* @returns
*/
create(ctx, params) {
Expand All @@ -223,6 +302,20 @@ module.exports = {
.then(json => this.clearCache().then(() => json));
},

/**
* Create many new entities
*
* @param {Context} ctx
* @param {Object} params
* @returns
*/
createMany(ctx, params) {
return this.validateEntity(params.entities)
.then(entities => this.adapter.insertMany(entities))
.then(docs => this.transformDocuments(ctx, docs))
.then(json => this.clearCache().then(() => json));
},

/**
* Get an entity by ID
*
Expand Down Expand Up @@ -353,11 +446,11 @@ module.exports = {
transformDocuments(ctx, docs) {
return this.Promise.resolve(docs)
.then(json => {
if (ctx.params.populate !== false)
if (ctx && ctx.params.populate !== false)
return this.populateDocs(ctx, json);
return json;
})
.then(docs => this.filterFields(docs, ctx.params.fields));
.then(docs => this.filterFields(docs, ctx ? ctx.params.fields : null));
},

/**
Expand Down
Loading

0 comments on commit b09fdde

Please sign in to comment.