Skip to content

Commit d816c51

Browse files
fix empty string errors
1 parent fa4ce7f commit d816c51

File tree

8 files changed

+113
-20
lines changed

8 files changed

+113
-20
lines changed

config/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = {
44
password: process.env.POSTGRES_PASSWORD,
55
port: process.env.POSTGRES_PORT || 5432,
66
database: process.env.POSTGRES_DATABASE || 'database_test',
7-
host: process.env.POSTGRES_HOST || 'postgres',
7+
host: process.env.POSTGRES_HOST || 'localhost',
88
dialect: 'postgres',
99
},
1010
}

index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
*
44
* @description
55
* ### A Sequelize database adapter for AdminBro.
6-
*
6+
*
77
* #### Installation
8-
*
8+
*
99
* To install the adapter run
10-
*
10+
*
1111
* ```
1212
* yarn add admin-bro-sequelizejs
1313
* ```
14-
*
14+
*
1515
* ### Usage
16-
*
16+
*
1717
* In order to use it in your project register the adapter first:
1818
*
1919
* ```javascript
@@ -22,12 +22,12 @@
2222
*
2323
* AdminBro.registerAdapter(AdminBroSequelize)
2424
* ```
25-
*
25+
*
2626
* ### Passing an entire database
27-
*
27+
*
2828
* Sequelize generates folder in your app called `./models` and there is an `index.js` file.
2929
* You can require it and pass to {@link AdminBroOptions} like this:
30-
*
30+
*
3131
* ```javascript
3232
* const db = require('../models');
3333
* const AdminBro = new AdminBro({
@@ -36,20 +36,20 @@
3636
* })
3737
* //...
3838
* ```
39-
*
39+
*
4040
* ### Passing each resource
41-
*
41+
*
4242
* Also you can pass a single resource and adjust it to your needs via {@link ResourceOptions}.
4343
*
4444
* So let say you have a model called `vendor` and there is a `vendor.js` file in your `./models`.
4545
* Within this file there is
46-
*
46+
*
4747
* ```javascript
4848
* //...
4949
* sequelize.define('vendor', //...
5050
* //...
5151
* ```
52-
*
52+
*
5353
* In order to pass it directly, run this code:
5454
*
5555
* ```javascript
@@ -65,13 +65,13 @@
6565
* })
6666
* //...
6767
* ```
68-
*
69-
*
68+
*
69+
*
7070
*/
7171

7272
/**
7373
* Implementation of {@link BaseDatabase} for Sequelize Adapter
74-
*
74+
*
7575
* @type {typeof BaseDatabase}
7676
* @static
7777
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
3+
module.exports = {
4+
up: (queryInterface, Sequelize) => queryInterface.createTable('Posts', {
5+
id: {
6+
allowNull: false,
7+
autoIncrement: true,
8+
primaryKey: true,
9+
type: Sequelize.INTEGER,
10+
},
11+
title: {
12+
type: Sequelize.STRING,
13+
},
14+
description: {
15+
type: Sequelize.STRING,
16+
},
17+
publishedAt: {
18+
type: Sequelize.DATE,
19+
},
20+
createdAt: {
21+
allowNull: false,
22+
type: Sequelize.DATE,
23+
},
24+
userId: {
25+
type: Sequelize.INTEGER,
26+
references: {
27+
model: 'Users',
28+
key: 'id',
29+
},
30+
onUpdate: 'CASCADE',
31+
onDelete: 'SET NULL',
32+
},
33+
updatedAt: {
34+
allowNull: false,
35+
type: Sequelize.DATE,
36+
},
37+
}),
38+
down: (queryInterface, Sequelize) => queryInterface.dropTable('Posts'),
39+
}

models/post.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = (sequelize, DataTypes) => {
2+
const Post = sequelize.define('Post', {
3+
title: DataTypes.STRING,
4+
description: DataTypes.STRING,
5+
publishedAt: DataTypes.DATE,
6+
}, {})
7+
Post.associate = function (models) {
8+
Post.belongsTo(models.User, { foreignKey: 'userId' })
9+
}
10+
return Post
11+
}

models/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = (sequelize, DataTypes) => {
1212
},
1313
}, {})
1414
User.associate = function (models) {
15-
// associations can be defined here
15+
User.hasMany(models.Post, { foreignKey: 'userId' })
1616
}
1717
return User
1818
}

spec/database.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('Database', function () {
2828
describe('#resources', function () {
2929
it('fetches all resources when entire db is given', function () {
3030
const database = new Database(db)
31-
expect(database.resources()).to.have.lengthOf(1)
31+
expect(database.resources()).to.have.lengthOf(2)
3232
expect(database.resources()[0]).to.be.an.instanceof(Resource)
3333
})
3434

spec/resource.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,24 @@ describe('Resource', function () {
130130
}
131131
})
132132
})
133+
134+
context('record with empty id field', function () {
135+
beforeEach(function () {
136+
this.SequelizeModel = db.sequelize.models.Post
137+
this.resource = new Resource(this.SequelizeModel)
138+
})
139+
140+
it('creates record without an error', async function () {
141+
this.params = {
142+
title: 'some title',
143+
description: 'doe',
144+
publishedAt: '2019-12-10 12:00',
145+
userId: '',
146+
}
147+
this.recordParams = await this.resource.create(this.params)
148+
expect(this.recordParams.userId).to.be.null
149+
})
150+
})
133151
})
134152

135153
describe('#update', function () {

src/resource.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ class Resource extends BaseResource {
106106
}
107107

108108
async create(params) {
109+
const parsedParams = this.parseParams(params)
109110
try {
110-
const record = await this.SequelizeModel.create(params)
111+
const record = await this.SequelizeModel.create(parsedParams)
111112
return record.toJSON()
112113
} catch (error) {
113114
if (error.name === SEQUELIZE_VALIDATION_ERROR) {
@@ -118,8 +119,9 @@ class Resource extends BaseResource {
118119
}
119120

120121
async update(id, params) {
122+
const parsedParams = this.parseParams(params)
121123
try {
122-
await this.SequelizeModel.update(params, {
124+
await this.SequelizeModel.update(parsedParams, {
123125
where: { id },
124126
})
125127
const record = await this.findById(id)
@@ -144,6 +146,29 @@ class Resource extends BaseResource {
144146
}, {})
145147
return new ValidationError(`${this.name()} validation failed`, errors)
146148
}
149+
150+
/**
151+
* Check all params against values they hold. In case of wrong value it corrects it.
152+
*
153+
* What it does esactly:
154+
* - removes keys with empty strings for the `number`, `float` and 'reference' properties.
155+
*
156+
* @param {Object} params received from AdminBro form
157+
*
158+
* @return {Object} converted params
159+
*/
160+
parseParams(params) {
161+
const parasedParams = { ...params }
162+
this.properties().forEach((property) => {
163+
const value = parasedParams[property.name()]
164+
if (['number', 'float', 'reference'].includes(property.type())) {
165+
if (value === '') {
166+
delete parasedParams[property.name()]
167+
}
168+
}
169+
})
170+
return parasedParams
171+
}
147172
}
148173

149174
module.exports = Resource

0 commit comments

Comments
 (0)