Skip to content

Commit 1e4725f

Browse files
committed
NAS-0: remove bluebird
1 parent 06b4d50 commit 1e4725f

File tree

7 files changed

+92
-114
lines changed

7 files changed

+92
-114
lines changed

package-lock.json

Lines changed: 1 addition & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nodejs-api-showcase",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "",
55
"main": "node src/server.js",
66
"scripts": {
@@ -22,7 +22,6 @@
2222
"dependencies": {
2323
"@hapi/joi": "^17.1.1",
2424
"bcryptjs": "^2.4.3",
25-
"bluebird": "^3.5.0",
2625
"body-parser": "^1.18.2",
2726
"compression": "^1.7.1",
2827
"cors": "^2.8.5",
@@ -36,7 +35,7 @@
3635
"moment": "^2.18.1",
3736
"mongodb": "^4.2.2",
3837
"mongoose": "^6.1.4",
39-
"mongoose-paginate": "5.0.3",
38+
"mongoose-paginate": "^5.0.3",
4039
"mongoose-unique-validator": "^3.0.0",
4140
"morgan": "^1.9.0",
4241
"rate-limiter-flexible": "^2.1.13",

src/data/infrastructure/db/index.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,43 @@
22
It can be published as private npm module shared among all team's projects.
33
*/
44
const mongoose = require('mongoose');
5-
const schemasFactory = require('./schemas');
5+
const schemas = require('./schemas');
66
const logging = require('../../../common/logging');
7-
mongoose.Promise = require('bluebird');
87

9-
module.exports = ({ dbConnectionString }) => {
8+
module.exports.init = (dbConnectionString) => {
109
if (!dbConnectionString) {
1110
throw new Error('add correct format of config with dbConnectionString');
1211
}
13-
const options = {
14-
promiseLibrary: require('bluebird'),
15-
};
16-
1712
// Check for errors on connecting to Mongo DB
1813
mongoose.connection.on('error', (err) => {
1914
logging.error(`Error! DB Connection failed. Error: ${err}`);
2015
return err;
2116
});
22-
2317
// Connection opened successfully
2418
mongoose.connection.once('open', () => {
2519
logging.info('Connection to MongoDB established');
2620
// mongoose.connection.db.dropDatabase()
2721
});
28-
2922
mongoose.connection.on('disconnected', () => {
3023
logging.info('Connection to MongoDB closed');
3124
logging.info('-------------------');
3225
});
3326

34-
const schemas = schemasFactory.create(mongoose);
3527
return Object.assign(
3628
{
3729
getConnection() {
3830
return mongoose.connection;
3931
},
40-
4132
connect() {
4233
// Open Connection to Mongo DB
43-
return mongoose.connect(dbConnectionString, options);
34+
return mongoose.connect(dbConnectionString);
4435
},
4536
close() {
4637
return mongoose.connection.close();
4738
},
4839
},
4940
{
50-
schemas,
41+
schemas: schemas.create(),
5142
},
5243
);
5344
};
Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
const moment = require('moment');
22
const mongoosePaginate = require('mongoose-paginate');
3+
const mongoose = require('mongoose');
34

4-
function create(mongoose) {
5-
const postSchema = mongoose.Schema({
6-
userId: {
7-
type: mongoose.Schema.Types.ObjectId,
8-
ref: 'User',
9-
},
10-
imageUrl: {
11-
type: String,
12-
required: true,
13-
},
14-
description: String,
15-
publisher: {
16-
type: String,
17-
required: true,
18-
},
19-
created: Date,
20-
});
5+
const PostSchema = new mongoose.Schema({
6+
userId: {
7+
type: mongoose.Schema.Types.ObjectId,
8+
ref: 'User',
9+
},
10+
imageUrl: {
11+
type: String,
12+
required: true,
13+
},
14+
description: String,
15+
publisher: {
16+
type: String,
17+
required: true,
18+
},
19+
created: Date,
20+
});
2121

22-
postSchema.pre('save', (next) => {
23-
this.created = moment().toJSON();
24-
return next();
25-
});
22+
PostSchema.index({ userId: 1 });
2623

27-
postSchema.index({ created: -1 });
24+
PostSchema.index({ created: -1 });
2825

29-
postSchema.plugin(mongoosePaginate);
26+
PostSchema.plugin(mongoosePaginate);
3027

31-
return mongoose.model('Post', postSchema);
32-
}
28+
PostSchema.pre('save', function (next) {
29+
this.created = moment().toJSON();
30+
return next();
31+
});
3332

34-
module.exports = create;
33+
const PostDao = mongoose.model('Post', PostSchema);
34+
35+
module.exports = { PostDao };
Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,58 @@
11
const moment = require('moment');
2-
const mongoosePaginate = require('mongoose-paginate');
32
const bcrypt = require('bcryptjs');
43
const uniqueValidator = require('mongoose-unique-validator');
5-
6-
function create(mongoose) {
7-
const userSchema = mongoose.Schema({
8-
name: {
9-
type: String,
10-
required: true,
11-
},
12-
surname: {
13-
type: String,
14-
required: true,
15-
},
16-
username: {
17-
type: String,
18-
required: true,
19-
},
20-
password: {
21-
type: String,
22-
required: true,
23-
},
24-
email: {
25-
type: String,
26-
required: true,
27-
unique: true,
28-
},
29-
created: Date,
30-
});
31-
32-
userSchema.pre('save', function (next) {
33-
bcrypt.genSalt(10, (err, salt) => {
34-
if (err) {
35-
return next(err);
4+
const {
5+
model,
6+
Schema,
7+
} = require('mongoose');
8+
9+
const UserSchema = new Schema({
10+
name: {
11+
type: String,
12+
required: true,
13+
},
14+
surname: {
15+
type: String,
16+
required: true,
17+
},
18+
username: {
19+
type: String,
20+
required: true,
21+
},
22+
password: {
23+
type: String,
24+
required: true,
25+
},
26+
email: {
27+
type: String,
28+
required: true,
29+
unique: true,
30+
},
31+
created: Date,
32+
});
33+
34+
UserSchema.index({ name: 1 });
35+
36+
UserSchema.index({ name: 1, created: -1 });
37+
38+
UserSchema.plugin(uniqueValidator);
39+
40+
UserSchema.pre('save', function (next) {
41+
bcrypt.genSalt(10, (err, salt) => {
42+
if (err) {
43+
return next(err);
44+
}
45+
bcrypt.hash(this.password, salt, (error, hash) => {
46+
if (error) {
47+
return next(error);
3648
}
37-
bcrypt.hash(this.password, salt, (err, hash) => {
38-
if (err) {
39-
return next(err);
40-
}
41-
this.password = hash;
42-
this.created = moment().toJSON();
43-
return next();
44-
});
49+
this.password = hash;
50+
this.created = moment().toJSON();
51+
return next();
4552
});
4653
});
54+
});
4755

48-
userSchema.index({ name: 1 });
49-
50-
userSchema.index({ name: 1, created: -1 });
51-
52-
userSchema.plugin(mongoosePaginate);
53-
54-
userSchema.plugin(uniqueValidator);
55-
56-
return mongoose.model('User', userSchema);
57-
}
58-
56+
const UserDao = model('User', UserSchema);
5957

60-
module.exports = create;
58+
module.exports = { UserDao };
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const postSchema = require('./Post');
2-
const userSchema = require('./User');
1+
const { PostDao } = require('./Post');
2+
const { UserDao } = require('./User');
33

4-
module.exports.create = (mongoose) => ({
5-
Post: postSchema(mongoose),
6-
User: userSchema(mongoose),
4+
module.exports.create = () => ({
5+
Post: PostDao,
6+
User: UserDao,
77
});

src/server.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const {
66
const setupWorkerProcesses = require('./common/utils/workerProcesses');
77
const logging = require('./common/logging');
88
const signals = require('./signals');
9-
const db = require('./data/infrastructure/db')({ dbConnectionString });
9+
const dbContainer = require('./data/infrastructure/db');
1010
const postsRepositoryContainer = require('./data/repositories/posts');
1111
const usersRepositoryContainer = require('./data/repositories/users');
1212
const authenticationRepositoryContainer = require('./data/repositories/authenticationRepository');
@@ -17,6 +17,7 @@ const usersServiceContainer = require('./domain/users/service');
1717
const appContainer = require('./presentation/http/app');
1818
const websocketsContainer = require('./presentation/websockets');
1919

20+
const db = dbContainer.init(dbConnectionString);
2021
const authenticationRepository = authenticationRepositoryContainer.init();
2122
const postsRepository = postsRepositoryContainer.init(db.schemas);
2223
const usersRepository = usersRepositoryContainer.init(db.schemas);
@@ -38,7 +39,7 @@ const app = appContainer.init({
3839
postsService,
3940
usersService,
4041
});
41-
const websockets = websocketsContainer.init(app);
42+
websocketsContainer.init(app);
4243

4344
let server;
4445

@@ -54,7 +55,6 @@ let server;
5455
}
5556
})(true);
5657

57-
5858
const shutdown = signals.init(async () => {
5959
await db.close();
6060
await server.close();

0 commit comments

Comments
 (0)