Skip to content

Commit

Permalink
Add some examples from presentation slides
Browse files Browse the repository at this point in the history
  • Loading branch information
Gael committed Dec 7, 2018
1 parent b1bc499 commit faf79d7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
33 changes: 33 additions & 0 deletions examples/activeRecord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const UserSchema = new Schema({
name: { type: String, default: '' },
email: { type: String, default: '' },
role: { type: String, enum: ['USER', 'ADMIN'] },
});

UserSchema.pre('save', function(next) {
if (!this.isNew) return next();

if (this.role != 'ADMIN' && this.email.endsWith('@admins.com')) {
next(new Error('Only admins can use @admins.com emails'));
} else {
next();
}
});

UserSchema.methods = {
isAdmin: function() {
return this.role === 'ADMIN';
},
};

UserSchema.statics = {
findByEmail: function(email) {
return this.findOne({ email });
},
};

mongoose.model('User', UserSchema);
20 changes: 20 additions & 0 deletions examples/framework.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/src
/controllers
/user.js
/company.js
/product.js
/cart.js
/views
/user.js
/company.js
/product.js
/cart.js
/models
/user.js
/company.js
/product.js
/cart.js




16 changes: 16 additions & 0 deletions examples/inversionOfControl/bad.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const dbClient = require('dbClient');
const notificationService = require('notificationService');

async function someUseCase() {
const account = await dbClient.collection('accounts').findOne({
_id: 'someId',
});

if (account.type === 'USER') {
notificationService.sendNotification(account);
}
}

module.exports = {
someUseCase,
};
18 changes: 18 additions & 0 deletions examples/inversionOfControl/good.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = ({
accountCollection: { findAccount },
notificationService: { sendNotification },
}) => {
async function someUseCase() {
const account = await findAccount({
_id: 'someId',
});

if (account.type === 'USER') {
sendNotification(account);
}
}

return {
someUseCase,
};
};

0 comments on commit faf79d7

Please sign in to comment.