Skip to content

Commit 0dd7050

Browse files
feat: Add suport for ARRAY type columns
fixes SoftwareBrothers/adminjs#353
1 parent ad7b940 commit 0dd7050

File tree

8 files changed

+46
-7
lines changed

8 files changed

+46
-7
lines changed

migrations/20181211174223-create-user.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ module.exports = {
2626
email: {
2727
type: Sequelize.STRING,
2828
},
29+
arrayed: {
30+
type: Sequelize.ARRAY(Sequelize.STRING),
31+
},
2932
createdAt: {
3033
allowNull: false,
3134
type: Sequelize.DATE,

models/user.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = (sequelize, DataTypes) => {
1111
allowNull: false,
1212
validate: { isEmail: true },
1313
},
14+
arrayed: DataTypes.ARRAY(DataTypes.STRING),
1415
}, {})
1516
User.associate = function (models) {
1617
User.hasMany(models.Post, { foreignKey: 'userId' })

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@semantic-release/git": "^9.0.0"
5050
},
5151
"dependencies": {
52-
"escape-regexp": "0.0.1"
52+
"escape-regexp": "0.0.1",
53+
"flat": "^5.0.0"
5354
}
5455
}

spec/database.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('Database', function () {
1212
firstName: Sequelize.STRING,
1313
lastName: Sequelize.STRING,
1414
email: Sequelize.STRING,
15+
arrayed: Sequelize.ARRAY(Sequelize.STRING),
1516
}, {})
1617
})
1718

spec/property.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ describe('Property', function () {
1212
db.sequelize.close()
1313
})
1414

15+
describe('#isArray', function () {
16+
it('returns false for regular (not arrayed) property', function () {
17+
const property = new Property(this.rawAttributes.email)
18+
expect(property.isArray()).to.equal(false)
19+
})
20+
21+
it('returns true for array property', function () {
22+
const property = new Property(this.rawAttributes.arrayed)
23+
expect(property.isArray()).to.equal(true)
24+
})
25+
})
26+
1527
describe('#type', function () {
1628
it('returns correct string type', function () {
1729
const property = new Property(this.rawAttributes.firstName)
@@ -27,6 +39,11 @@ describe('Property', function () {
2739
const property = new Property(this.rawAttributes.createdAt)
2840
expect(property.type()).to.equal('datetime')
2941
})
42+
43+
it('returns string when property is an array of strings', function () {
44+
const property = new Property(this.rawAttributes.arrayed)
45+
expect(property.type()).to.equal('string')
46+
})
3047
})
3148

3249
describe('#availableValues', function () {

spec/resource.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('Resource', function () {
5050

5151
describe('#properties', function () {
5252
it('returns all properties', function () {
53-
const length = 7 // there are 7 properties in the User model (4 regular + __v and _id)
53+
const length = 8 // there are 8 properties in the User model (5 regular + __v and _id)
5454
expect(this.resource.properties()).to.have.lengthOf(length)
5555
})
5656
})

src/property.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Property extends BaseProperty {
4949
}
5050

5151
reference() {
52-
return this.sequelizePath.references && this.sequelizePath.references.model
52+
return !this.isArray() && this.sequelizePath.references && this.sequelizePath.references.model
5353
}
5454

5555
availableValues() {
@@ -58,9 +58,19 @@ class Property extends BaseProperty {
5858
: null
5959
}
6060

61+
isArray() {
62+
return this.sequelizePath.type.constructor.name === 'ARRAY'
63+
}
64+
6165
type() {
66+
let sequelizeType = this.sequelizePath.type
67+
68+
if (this.isArray()) {
69+
sequelizeType = sequelizeType.type
70+
}
71+
6272
const key = TYPES_MAPPING.find(element => (
63-
this.sequelizePath.type.constructor.name === element[0]
73+
sequelizeType.constructor.name === element[0]
6474
))
6575

6676
if (this.reference()) {
@@ -71,6 +81,10 @@ class Property extends BaseProperty {
7181
return type || 'string'
7282
}
7383

84+
isSortable() {
85+
return this.type() !== 'mixed' && !this.isArray()
86+
}
87+
7488
isRequired() {
7589
return !(typeof this.sequelizePath.allowNull === 'undefined'
7690
|| this.sequelizePath.allowNull === true)

src/resource.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-param-reassign */
2-
2+
const { unflatten } = require('flat')
33
const { BaseResource, BaseRecord } = require('admin-bro')
44
const { Op } = require('sequelize')
55

@@ -116,8 +116,9 @@ class Resource extends BaseResource {
116116

117117
async create(params) {
118118
const parsedParams = this.parseParams(params)
119+
const unflattedParams = unflatten(parsedParams)
119120
try {
120-
const record = await this.SequelizeModel.create(parsedParams)
121+
const record = await this.SequelizeModel.create(unflattedParams)
121122
return record.toJSON()
122123
} catch (error) {
123124
if (error.name === SEQUELIZE_VALIDATION_ERROR) {
@@ -129,8 +130,9 @@ class Resource extends BaseResource {
129130

130131
async update(id, params) {
131132
const parsedParams = this.parseParams(params)
133+
const unflattedParams = unflatten(parsedParams)
132134
try {
133-
await this.SequelizeModel.update(parsedParams, {
135+
await this.SequelizeModel.update(unflattedParams, {
134136
where: {
135137
[this.SequelizeModel.primaryKeyField]: id,
136138
},

0 commit comments

Comments
 (0)