Skip to content

Commit 6c2c84a

Browse files
committed
Prettify code
1 parent f12b2e3 commit 6c2c84a

10 files changed

Lines changed: 352 additions & 287 deletions

File tree

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
A [hapi.js](https://github.com/hapijs/hapi) plugin to connect with [Sequelize ORM](https://github.com/sequelize/sequelize/).
88

99
## Support me
10+
1011
If you like this plugin, please support my work and help maintaining it.
1112

1213
<a href="https://www.buymeacoffee.com/valtlfelipe" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" width="200"></a>
@@ -51,7 +52,7 @@ server.register([
5152
A model should export a function that returns a Sequelize model definition ([http://docs.sequelizejs.com/en/latest/docs/models-definition/](http://docs.sequelizejs.com/en/latest/docs/models-definition/)).
5253

5354
```javascript
54-
module.exports = function(sequelize, DataTypes) {
55+
module.exports = function (sequelize, DataTypes) {
5556
const Category = sequelize.define('Category', {
5657
name: DataTypes.STRING,
5758
rootCategory: DataTypes.BOOLEAN,
@@ -66,13 +67,13 @@ module.exports = function(sequelize, DataTypes) {
6667
Using the sequelize model instance, define a method called `associate`, that is a function, and receives as parameter all models defined.
6768

6869
```javascript
69-
module.exports = function(sequelize, DataTypes) {
70+
module.exports = function (sequelize, DataTypes) {
7071
const Category = sequelize.define('Category', {
7172
name: DataTypes.STRING,
7273
rootCategory: DataTypes.BOOLEAN,
7374
});
7475

75-
Category.associate = function(models) {
76+
Category.associate = function (models) {
7677
models.Category.hasMany(models.Product);
7778
};
7879

lib/index.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const Joi = require('@hapi/joi');
2-
const {
3-
assert
4-
} = require('@hapi/hoek');
2+
const { assert } = require('@hapi/hoek');
53
const Schema = require('./schema');
64
const Models = require('./models');
75
const DB = require('./DB');
@@ -27,16 +25,16 @@ async function register(server, options) {
2725
setupDecorators(server);
2826

2927
server.events.on('stop', async function onStop() {
30-
const dbNames = options.map(option => option.name);
28+
const dbNames = options.map((option) => option.name);
3129
const pluginContent = server.plugins[Pkg.name];
3230

33-
await Promise.all(dbNames.map(dbName => pluginContent[dbName].sequelize.close()));
31+
await Promise.all(dbNames.map((dbName) => pluginContent[dbName].sequelize.close()));
3432
});
3533

3634
const configured = options.reduce(
3735
(acc, options) => [
3836
...acc,
39-
configure(options).then(db => {
37+
configure(options).then((db) => {
4038
server.expose(options.name, db);
4139
return db;
4240
}),
@@ -65,7 +63,7 @@ async function configure(options) {
6563

6664
if (options.sync) {
6765
await options.sequelize.sync({
68-
force: options.forceSync
66+
force: options.forceSync,
6967
});
7068
}
7169

@@ -88,7 +86,7 @@ async function configure(options) {
8886

8987
function setupDecorators(server) {
9088
const options = {
91-
apply: true
89+
apply: true,
9290
};
9391
server.decorate('request', 'getDb', () => instances.getDb, options);
9492
server.decorate('request', 'getModel', () => instances.getModel, options);

lib/instances.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ const db = {
22
register,
33
getDb,
44
getModel,
5-
getModels
5+
getModels,
66
};
77

88
let instances = [];
99

1010
Object.defineProperty(db, 'dbs', {
1111
configurable: false,
1212
enumerable: true,
13-
get: () => instances.reduce((dbs, instance) => {
14-
dbs[instance.name] = instance.db;
15-
return dbs;
16-
}, {})
13+
get: () =>
14+
instances.reduce((dbs, instance) => {
15+
dbs[instance.name] = instance.db;
16+
return dbs;
17+
}, {}),
1718
});
1819

1920
module.exports = db;
@@ -23,7 +24,7 @@ function register(name, db) {
2324
if (!instance) {
2425
instances.push({
2526
name,
26-
db
27+
db,
2728
});
2829
} else {
2930
instance.db = db;
@@ -55,7 +56,7 @@ function getModels(dbName) {
5556
}
5657

5758
function findDb(dbName) {
58-
return instances.find(db => db.name === dbName);
59+
return instances.find((db) => db.name === dbName);
5960
}
6061

6162
function getDb(dbName) {

lib/models.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async function applyRelations(models) {
5151
throw new Error(`Can't apply relationships on invalid models object`);
5252
}
5353

54-
Object.keys(models).forEach(name => {
54+
Object.keys(models).forEach((name) => {
5555
if ('associate' in models[name]) {
5656
models[name].associate(models);
5757
}

lib/schema.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ const Joi = require('@hapi/joi');
22
const Sequelize = require('sequelize');
33

44
const option = Joi.object().keys({
5-
name: Joi.string()
6-
.token()
7-
.required(),
5+
name: Joi.string().token().required(),
86
models: Joi.alternatives().try(Joi.string(), Joi.array().items(Joi.string())),
97
ignoredModels: Joi.alternatives().try(Joi.string(), Joi.array().items(Joi.string())),
10-
sequelize: Joi.object()
11-
.instance(Sequelize)
12-
.required(),
8+
sequelize: Joi.object().instance(Sequelize).required(),
139
sync: Joi.boolean().default(false),
1410
forceSync: Joi.boolean().default(false),
1511
debug: Joi.boolean(),

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111
"test-cov-html": "lab --leaks -a @hapi/code -r html -o coverage.html",
1212
"lint": "eslint lib/*.js",
1313
"coveralls": "./node_modules/lab/bin/lab -r lcov | ./node_modules/.bin/coveralls",
14-
"precommit": "lint-staged"
14+
"prettify": "prettier --print-width 100 --tab-width 4 --single-quote --trailing-comma all --write \"{{lib,test}/**/*.js,*.{js,md,json}}\""
15+
},
16+
"husky": {
17+
"hooks": {
18+
"pre-commit": "lint-staged"
19+
}
1520
},
1621
"lint-staged": {
1722
"{{lib,test}/**/*.js,*.{js,md,json}}": [
18-
"prettier --print-width 100 --tab-width 4 --single-quote --trailing-comma all --write",
19-
"git add"
23+
"prettier --print-width 100 --tab-width 4 --single-quote --trailing-comma all --write"
2024
]
2125
},
2226
"repository": {

0 commit comments

Comments
 (0)