Skip to content

Commit

Permalink
remove bluebird from moleculer-db
Browse files Browse the repository at this point in the history
  • Loading branch information
Freezystem committed Aug 26, 2024
1 parent 7a84991 commit 081263e
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 64 deletions.
17 changes: 9 additions & 8 deletions packages/moleculer-db/examples/encode/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"use strict";

let { ServiceBroker } = require("moleculer");
let DbService = require("../../index");
let _ = require("lodash");
let ModuleChecker = require("../../test/checker");
let Promise = require("bluebird");
const { ServiceBroker } = require("moleculer");
const DbService = require("../../index");
const _ = require("lodash");
const ModuleChecker = require("../../test/checker");

const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

// Create broker
let broker = new ServiceBroker({
const broker = new ServiceBroker({
logger: console,
logLevel: "debug"
});
Expand Down Expand Up @@ -39,7 +40,7 @@ broker.createService(DbService, {
return this.adapter.count().delay(500).then(count => {
if (count == 0) {
this.logger.info("Seed products...");
let products = _.times(20, i => {
const products = _.times(20, i => {
return {
name: "Product " + i
};
Expand All @@ -58,7 +59,7 @@ const checker = new ModuleChecker(6);
// Start checks
function start() {
return Promise.resolve()
.delay(500)
.then(() => delay(500))
.then(() => checker.execute())
.catch(console.error)
.then(() => broker.stop())
Expand Down
31 changes: 15 additions & 16 deletions packages/moleculer-db/examples/pagination/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"use strict";

let _ = require("lodash");
let kleur = require("kleur");
let { ServiceBroker } = require("moleculer");
let DbService = require("../../index");
let ModuleChecker = require("../../test/checker");
let Promise = require("bluebird");
const _ = require("lodash");
const kleur = require("kleur");
const { ServiceBroker } = require("moleculer");
const DbService = require("../../index");
const ModuleChecker = require("../../test/checker");

const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

// Create broker
let broker = new ServiceBroker({
const broker = new ServiceBroker({
logger: console,
logLevel: "debug"
});
Expand Down Expand Up @@ -42,7 +43,7 @@ const checker = new ModuleChecker(14);
// Start checks
function start() {
return Promise.resolve()
.delay(500)
.then(()=> delay(500))
.then(() => checker.execute())
.catch(console.error)
.then(() => broker.stop())
Expand All @@ -51,8 +52,6 @@ function start() {

// --- TEST CASES ---

let id;

// Count of posts
checker.add("COUNT", () => broker.call("posts.count"), res => {
console.log(res);
Expand All @@ -68,7 +67,7 @@ checker.add("FIND", () => broker.call("posts.find", { sort: "title" }), res => {
// List posts
checker.add("LIST FIRST 10", () => broker.call("posts.list", { sort: "title" }), res => {
console.log(res);
let rows = res.rows;
const rows = res.rows;
return [
res.total === 28 && res.page === 1 && res.pageSize === 10 && res.totalPages === 3,
rows.length === 10 && rows[0].title == "Post #01" && rows[9].title === "Post #10"
Expand All @@ -78,7 +77,7 @@ checker.add("LIST FIRST 10", () => broker.call("posts.list", { sort: "title" }),
// List posts
checker.add("LIST LAST 10", () => broker.call("posts.list", { sort: "-title" }), res => {
console.log(res);
let rows = res.rows;
const rows = res.rows;
return [
res.total === 28 && res.page === 1 && res.pageSize === 10 && res.totalPages === 3,
rows.length === 10 && rows[0].title == "Post #28" && rows[9].title === "Post #19"
Expand All @@ -88,7 +87,7 @@ checker.add("LIST LAST 10", () => broker.call("posts.list", { sort: "-title" }),
// List posts
checker.add("LIST FIRST 25", () => broker.call("posts.list", { page: 1, pageSize: 25, sort: "title" }), res => {
console.log(res);
let rows = res.rows;
const rows = res.rows;
return [
res.total === 28 && res.page === 1 && res.pageSize === 25 && res.totalPages === 2,
rows.length === 25 && rows[0].title == "Post #01" && rows[24].title === "Post #25"
Expand All @@ -98,7 +97,7 @@ checker.add("LIST FIRST 25", () => broker.call("posts.list", { page: 1, pageSize
// List posts
checker.add("LIST NEXT 25", () => broker.call("posts.list", { page: 2, pageSize: 25, sort: "title" }), res => {
console.log(res);
let rows = res.rows;
const rows = res.rows;
return [
res.total === 28 && res.page === 2 && res.pageSize === 25 && res.totalPages === 2,
rows.length === 3 && rows[0].title == "Post #26" && rows[2].title === "Post #28"
Expand All @@ -108,7 +107,7 @@ checker.add("LIST NEXT 25", () => broker.call("posts.list", { page: 2, pageSize:
// List posts
checker.add("LIST NEXT2 25", () => broker.call("posts.list", { page: 3, pageSize: 25, sort: "title" }), res => {
console.log(res);
let rows = res.rows;
const rows = res.rows;
return [
res.total === 28 && res.page === 3 && res.pageSize === 25 && res.totalPages === 2,
rows.length === 0
Expand All @@ -118,7 +117,7 @@ checker.add("LIST NEXT2 25", () => broker.call("posts.list", { page: 3, pageSize
// List posts with search
checker.add("LIST SEARCH 5", () => broker.call("posts.list", { page: 1, pageSize: 5, search: "#2" }), res => {
console.log(res);
let rows = res.rows;
const rows = res.rows;
return [
res.total === 9 && res.page === 1 && res.pageSize === 5 && res.totalPages === 2,
rows.length === 5
Expand Down
17 changes: 9 additions & 8 deletions packages/moleculer-db/examples/populates/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"use strict";

let { ServiceBroker } = require("moleculer");
let DbService = require("../../index");
let path = require("path");
let ModuleChecker = require("../../test/checker");
let Promise = require("bluebird");
const { ServiceBroker } = require("moleculer");
const DbService = require("../../index");
const path = require("node:path");
const ModuleChecker = require("../../test/checker");

const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

// Create broker
let broker = new ServiceBroker({
const broker = new ServiceBroker({
logger: console,
logLevel: "debug"
});
Expand Down Expand Up @@ -108,7 +109,7 @@ const checker = new ModuleChecker(13);
// Start checks
function start() {
return Promise.resolve()
.delay(500)
.then(()=> delay(500))
.then(() => checker.execute())
.catch(console.error)
.then(() => broker.stop())
Expand Down Expand Up @@ -148,7 +149,7 @@ checker.add("GET POST (page: 2, pageSize: 5, sort: -votes)", () => broker.call("

checker.add("LIST POSTS (page: 2, pageSize: 5, sort: -votes)", () => broker.call("posts.list", { page: 2, pageSize: 2, sort: "-votes", populate: ["author"], fields: ["_id", "title", "votes", "author"] }), res => {
console.log(res);
let rows = res.rows;
const rows = res.rows;
return [
res.total === 5 && res.page === 2 && res.pageSize === 2 && res.totalPages === 3,
rows.length == 2,
Expand Down
7 changes: 4 additions & 3 deletions packages/moleculer-db/examples/simple/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const kleur = require("kleur");
const { ServiceBroker } = require("moleculer");
const DbService = require("../../index");
const ModuleChecker = require("../../test/checker");
const Promise = require("bluebird");

const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

// Create broker
const broker = new ServiceBroker({
Expand Down Expand Up @@ -53,7 +54,7 @@ const checker = new ModuleChecker(15);
// Start checks
function start() {
return Promise.resolve()
.delay(500)
.then(()=> delay(500))
.then(() => checker.execute())
.catch(console.error)
.then(() => broker.stop())
Expand All @@ -63,7 +64,7 @@ function start() {
// --- TEST CASES ---

let id;
let date = new Date();
const date = new Date();

// Count of posts
checker.add("COUNT", () => broker.call("posts.count"), res => {
Expand Down
1 change: 0 additions & 1 deletion packages/moleculer-db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
},
"dependencies": {
"@seald-io/nedb": "^3.0.0",
"bluebird": "^3.7.2",
"flat": "^5.0.2",
"lodash": "^4.17.21"
}
Expand Down
43 changes: 21 additions & 22 deletions packages/moleculer-db/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"use strict";

const _ = require("lodash");
const Promise = require("bluebird");
const { flatten } = require("flat");
const { MoleculerClientError, ValidationError } = require("moleculer").Errors;
const { EntityNotFoundError } = require("./errors");
const MemoryAdapter = require("./memory-adapter");
const pkg = require("../package.json");
const util = require("node:util");
const { copyFieldValueByPath } = require("./utils");
const stringToPath = require("lodash/_stringToPath");

Expand Down Expand Up @@ -129,7 +129,7 @@ module.exports = {
],
},
handler(ctx) {
let params = this.sanitizeParams(ctx, ctx.params);
const params = this.sanitizeParams(ctx, ctx.params);
return this._find(ctx, params);
}
},
Expand Down Expand Up @@ -162,7 +162,7 @@ module.exports = {
],
},
handler(ctx) {
let params = this.sanitizeParams(ctx, ctx.params);
const params = this.sanitizeParams(ctx, ctx.params);
return this._count(ctx, params);
}
},
Expand Down Expand Up @@ -217,7 +217,7 @@ module.exports = {
],
},
handler(ctx) {
let params = this.sanitizeParams(ctx, ctx.params);
const params = this.sanitizeParams(ctx, ctx.params);
return this._list(ctx, params);
}
},
Expand Down Expand Up @@ -300,7 +300,7 @@ module.exports = {
mapping: { type: "boolean", optional: true }
},
handler(ctx) {
let params = this.sanitizeParams(ctx, ctx.params);
const params = this.sanitizeParams(ctx, ctx.params);
return this._get(ctx, params);

}
Expand Down Expand Up @@ -388,7 +388,7 @@ module.exports = {
* @returns {Object}
*/
sanitizeParams(ctx, params) {
let p = Object.assign({}, params);
const p = Object.assign({}, params);

// Convert from string to number
if (typeof(p.limit) === "string")
Expand Down Expand Up @@ -647,7 +647,7 @@ module.exports = {
}

if (askedField.indexOf(".") !== -1) {
let parts = askedField.split(".");
const parts = askedField.split(".");
while (parts.length > 1) {
parts.pop();
if (this.settings.fields.indexOf(parts.join(".")) !== -1) {
Expand All @@ -657,7 +657,7 @@ module.exports = {
}
}

let nestedFields = this.settings.fields.filter(settingField => settingField.startsWith(askedField + "."));
const nestedFields = this.settings.fields.filter(settingField => settingField.startsWith(askedField + "."));
if (nestedFields.length > 0) {
allowedFields = allowedFields.concat(nestedFields);
}
Expand Down Expand Up @@ -703,7 +703,7 @@ module.exports = {
return obj;
}, {});

let promises = [];
const promises = [];
for (const populatesField of settingPopulateFields) {
let rule = this.settings.populates[populatesField];
if (groupedPopulateFields[populatesField] == null)
Expand All @@ -712,7 +712,7 @@ module.exports = {
// if the rule is a function, save as a custom handler
if (_.isFunction(rule)) {
rule = {
handler: Promise.method(rule)
handler: util.promisify(rule)
};
}

Expand All @@ -725,16 +725,16 @@ module.exports = {

if (rule.field === undefined) rule.field = populatesField;

let arr = Array.isArray(docs) ? docs : [docs];
const arr = Array.isArray(docs) ? docs : [docs];

// Collect IDs from field of docs (flatten, compact & unique list)
let idList = _.uniq(_.flattenDeep(_.compact(arr.map(doc => _.get(doc, rule.field)))));
const idList = _.uniq(_.flattenDeep(_.compact(arr.map(doc => _.get(doc, rule.field)))));
// Replace the received models according to IDs in the original docs
const resultTransform = (populatedDocs) => {
arr.forEach(doc => {
let id = _.get(doc, rule.field);
const id = _.get(doc, rule.field);
if (_.isArray(id)) {
let models = _.compact(id.map(id => populatedDocs[id]));
const models = _.compact(id.map(id => populatedDocs[id]));
_.set(doc, populatesField, models);
} else {
_.set(doc, populatesField, populatedDocs[id]);
Expand Down Expand Up @@ -777,7 +777,7 @@ module.exports = {
if (!_.isFunction(this.settings.entityValidator))
return Promise.resolve(entity);

let entities = Array.isArray(entity) ? entity : [entity];
const entities = Array.isArray(entity) ? entity : [entity];
return Promise.all(entities.map(entity => this.settings.entityValidator.call(this, entity))).then(() => entity);
},

Expand Down Expand Up @@ -848,7 +848,7 @@ module.exports = {
* @returns {Object} List of found entities and count.
*/
_list(ctx, params) {
let countParams = Object.assign({}, params);
const countParams = Object.assign({}, params);
// Remove pagination params
if (countParams && countParams.limit)
countParams.limit = null;
Expand Down Expand Up @@ -895,8 +895,7 @@ module.exports = {
* @returns {Object} Saved entity.
*/
_create(ctx, params) {
let entity = params;
return this.beforeEntityChange("create", entity, ctx)
return this.beforeEntityChange("create", params, ctx)
.then((entity)=>this.validateEntity(entity))
// Apply idField
.then(entity =>
Expand Down Expand Up @@ -963,9 +962,9 @@ module.exports = {
* @throws {EntityNotFoundError} - 404 Entity not found
*/
_get(ctx, params) {
let id = params.id;
const id = params.id;
let origDoc;
let shouldMapping = params.mapping === true;
const shouldMapping = params.mapping === true;
return this.getById(id, true)
.then(doc => {
if (!doc)
Expand All @@ -981,7 +980,7 @@ module.exports = {
.then(json => {
if (params.mapping !== true) return json;

let res = {};
const res = {};
if (_.isArray(json)) {
json.forEach((doc, i) => {
const id = this.encodeID(this.adapter.afterRetrieveTransformID(origDoc[i], this.settings.idField)[this.settings.idField]);
Expand Down Expand Up @@ -1101,7 +1100,7 @@ module.exports = {
started() {
if (this.adapter) {
return new Promise(resolve => {
let connecting = () => {
const connecting = () => {
this.connect().then(resolve).catch(err => {
this.logger.error("Connection error!", err);
setTimeout(() => {
Expand Down
Loading

0 comments on commit 081263e

Please sign in to comment.