Skip to content

Commit fe8bbc5

Browse files
Merge pull request #35 from SoftwareBrothers/beta
Beta
2 parents ad7b940 + 76b2317 commit fe8bbc5

9 files changed

+103
-49
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: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "admin-bro-sequelizejs",
3-
"version": "0.4.4",
3+
"version": "0.5.0-beta.2",
44
"description": "Sequelize adapter for AdminBro",
55
"main": "index.js",
66
"scripts": {
@@ -26,29 +26,31 @@
2626
}
2727
},
2828
"devDependencies": {
29+
"@commitlint/cli": "^8.3.5",
30+
"@commitlint/config-conventional": "^8.3.4",
31+
"@semantic-release/git": "^9.0.0",
32+
"@types/mocha": "^7.0.2",
2933
"admin-bro": "^2.2.0",
3034
"chai": "^4.2.0",
3135
"eslint": "^5.10.0",
3236
"eslint-config-airbnb-base": "^13.1.0",
3337
"eslint-plugin-import": "^2.14.0",
3438
"factory-girl": "^5.0.4",
39+
"husky": "^4.2.5",
3540
"lodash": "^4.17.11",
3641
"mocha": "^5.2.0",
3742
"mongoose": "^5.3.15",
3843
"nyc": "^12.0.2",
3944
"pg": "^7.7.1",
45+
"semantic-release": "^17.0.7",
46+
"semantic-release-jira-releases-sb": "^0.7.2",
4047
"sequelize": "^4.42.0",
4148
"sequelize-cli": "^5.4.0",
4249
"sinon": "^7.1.1",
43-
"sinon-chai": "^3.3.0",
44-
"husky": "^4.2.5",
45-
"@commitlint/cli": "^8.3.5",
46-
"@commitlint/config-conventional": "^8.3.4",
47-
"semantic-release": "^17.0.7",
48-
"semantic-release-jira-releases-sb": "^0.7.2",
49-
"@semantic-release/git": "^9.0.0"
50+
"sinon-chai": "^3.3.0"
5051
},
5152
"dependencies": {
52-
"escape-regexp": "0.0.1"
53+
"escape-regexp": "0.0.1",
54+
"flat": "^5.0.0"
5355
}
5456
}

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: 12 additions & 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
})
@@ -59,6 +59,17 @@ describe('Resource', function () {
5959
it('returns given property', function () {
6060
expect(this.resource.property('email')).to.be.an.instanceOf(Property)
6161
})
62+
63+
it('returns null when property doesn\'t exit', function () {
64+
expect(this.resource.property('some.imagine.property')).to.be.null
65+
})
66+
67+
it('returns nested property for array field', function () {
68+
const property = this.resource.property('arrayed.1')
69+
70+
expect(property).to.be.an.instanceOf(Property)
71+
expect(property.type()).to.equal('string')
72+
})
6273
})
6374

6475
describe('#findMany', function () {

src/property.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const { BaseProperty } = require('admin-bro')
22

3+
/**
4+
* @type {Array<[string, import('admin-bro').PropertyType]>}
5+
*
6+
* @var {[type]}
7+
*/
38
const TYPES_MAPPING = [
49
['STRING', 'string'],
510
['TEXT', 'string'],
@@ -49,7 +54,7 @@ class Property extends BaseProperty {
4954
}
5055

5156
reference() {
52-
return this.sequelizePath.references && this.sequelizePath.references.model
57+
return !this.isArray() && this.sequelizePath.references && this.sequelizePath.references.model
5358
}
5459

5560
availableValues() {
@@ -58,9 +63,22 @@ class Property extends BaseProperty {
5863
: null
5964
}
6065

66+
isArray() {
67+
return this.sequelizePath.type.constructor.name === 'ARRAY'
68+
}
69+
70+
/**
71+
* @returns {import('admin-bro').PropertyType}
72+
*/
6173
type() {
74+
let sequelizeType = this.sequelizePath.type
75+
76+
if (this.isArray()) {
77+
sequelizeType = sequelizeType.type
78+
}
79+
6280
const key = TYPES_MAPPING.find(element => (
63-
this.sequelizePath.type.constructor.name === element[0]
81+
sequelizeType.constructor.name === element[0]
6482
))
6583

6684
if (this.reference()) {
@@ -71,6 +89,10 @@ class Property extends BaseProperty {
7189
return type || 'string'
7290
}
7391

92+
isSortable() {
93+
return this.type() !== 'mixed' && !this.isArray()
94+
}
95+
7496
isRequired() {
7597
return !(typeof this.sequelizePath.allowNull === 'undefined'
7698
|| this.sequelizePath.allowNull === true)

src/resource.js

Lines changed: 15 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

@@ -50,6 +50,16 @@ class Resource extends BaseResource {
5050
}
5151

5252
property(path) {
53+
const nested = path.split('.')
54+
55+
// if property is an array return the array property
56+
if (nested.length > 1 && this.rawAttributes()[nested[0]]) {
57+
return new Property(this.rawAttributes()[nested[0]])
58+
}
59+
60+
if (!this.rawAttributes()[path]) {
61+
return null
62+
}
5363
return new Property(this.rawAttributes()[path])
5464
}
5565

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

117127
async create(params) {
118128
const parsedParams = this.parseParams(params)
129+
const unflattedParams = unflatten(parsedParams)
119130
try {
120-
const record = await this.SequelizeModel.create(parsedParams)
131+
const record = await this.SequelizeModel.create(unflattedParams)
121132
return record.toJSON()
122133
} catch (error) {
123134
if (error.name === SEQUELIZE_VALIDATION_ERROR) {
@@ -129,8 +140,9 @@ class Resource extends BaseResource {
129140

130141
async update(id, params) {
131142
const parsedParams = this.parseParams(params)
143+
const unflattedParams = unflatten(parsedParams)
132144
try {
133-
await this.SequelizeModel.update(parsedParams, {
145+
await this.SequelizeModel.update(unflattedParams, {
134146
where: {
135147
[this.SequelizeModel.primaryKeyField]: id,
136148
},

yarn.lock

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,6 +1658,11 @@
16581658
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6"
16591659
integrity sha1-aaI6OtKcrwCX8G7aWbNh7i8GOfY=
16601660

1661+
"@types/mocha@^7.0.2":
1662+
version "7.0.2"
1663+
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-7.0.2.tgz#b17f16cf933597e10d6d78eae3251e692ce8b0ce"
1664+
integrity sha512-ZvO2tAcjmMi8V/5Z3JsyofMe3hasRcaw88cto5etSVMwVQfeivGAlEYmaQgceUSVYFofVjT+ioHsATjdWcFt1w==
1665+
16611666
"@types/node@*", "@types/node@^10.11.7":
16621667
version "10.12.15"
16631668
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.15.tgz#20e85651b62fd86656e57c9c9bc771ab1570bc59"
@@ -3086,7 +3091,7 @@ debug@^4.0.1:
30863091
dependencies:
30873092
ms "^2.1.1"
30883093

3089-
debuglog@*, debuglog@^1.0.1:
3094+
debuglog@^1.0.1:
30903095
version "1.0.1"
30913096
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
30923097
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
@@ -3910,6 +3915,13 @@ flat@^4.1.0:
39103915
dependencies:
39113916
is-buffer "~2.0.3"
39123917

3918+
flat@^5.0.0:
3919+
version "5.0.0"
3920+
resolved "https://registry.yarnpkg.com/flat/-/flat-5.0.0.tgz#dab7d71d60413becb0ac2de9bf4304495e3af6af"
3921+
integrity sha512-6KSMM+cHHzXC/hpldXApL2S8Uz+QZv+tq5o/L0KQYleoG+GcwrnIJhTWC7tCOiKQp8D/fIvryINU1OZCCwevjA==
3922+
dependencies:
3923+
is-buffer "~2.0.4"
3924+
39133925
flush-write-stream@^1.0.0:
39143926
version "1.1.1"
39153927
resolved "https://registry.yarnpkg.com/flush-write-stream/-/flush-write-stream-1.1.1.tgz#8dd7d873a1babc207d94ead0c2e0e44276ebf2e8"
@@ -4558,7 +4570,7 @@ import-lazy@^2.1.0:
45584570
resolved "https://registry.yarnpkg.com/import-lazy/-/import-lazy-2.1.0.tgz#05698e3d45c88e8d7e9d92cb0584e77f096f3e43"
45594571
integrity sha1-BWmOPUXIjo1+nZLLBYTnfwlvPkM=
45604572

4561-
imurmurhash@*, imurmurhash@^0.1.4:
4573+
imurmurhash@^0.1.4:
45624574
version "0.1.4"
45634575
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
45644576

@@ -4698,6 +4710,11 @@ is-buffer@^2.0.2, is-buffer@~2.0.3:
46984710
version "2.0.3"
46994711
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.3.tgz#4ecf3fcf749cbd1e472689e109ac66261a25e725"
47004712

4713+
is-buffer@~2.0.4:
4714+
version "2.0.4"
4715+
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.4.tgz#3e572f23c8411a5cfd9557c849e3665e0b290623"
4716+
integrity sha512-Kq1rokWXOPXWuaMAqZiJW4XxsmD9zGx9q4aePabbn3qCRGedtH7Cm+zV8WETitMfu1wdh+Rvd6w5egwSngUX2A==
4717+
47014718
is-builtin-module@^1.0.0:
47024719
version "1.0.0"
47034720
resolved "https://registry.yarnpkg.com/is-builtin-module/-/is-builtin-module-1.0.0.tgz#540572d34f7ac3119f8f76c30cbc1b1e037affbe"
@@ -5479,11 +5496,6 @@ lockfile@^1.0.4:
54795496
dependencies:
54805497
signal-exit "^3.0.2"
54815498

5482-
lodash._baseindexof@*:
5483-
version "3.1.0"
5484-
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
5485-
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
5486-
54875499
lodash._baseuniq@~4.6.0:
54885500
version "4.6.0"
54895501
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
@@ -5492,33 +5504,11 @@ lodash._baseuniq@~4.6.0:
54925504
lodash._createset "~4.0.0"
54935505
lodash._root "~3.0.0"
54945506

5495-
lodash._bindcallback@*:
5496-
version "3.0.1"
5497-
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
5498-
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=
5499-
5500-
lodash._cacheindexof@*:
5501-
version "3.0.2"
5502-
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
5503-
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
5504-
5505-
lodash._createcache@*:
5506-
version "3.1.2"
5507-
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
5508-
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
5509-
dependencies:
5510-
lodash._getnative "^3.0.0"
5511-
55125507
lodash._createset@~4.0.0:
55135508
version "4.0.3"
55145509
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
55155510
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=
55165511

5517-
lodash._getnative@*, lodash._getnative@^3.0.0:
5518-
version "3.9.1"
5519-
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
5520-
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
5521-
55225512
lodash._reinterpolate@^3.0.0:
55235513
version "3.0.0"
55245514
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
@@ -5563,11 +5553,6 @@ lodash.isstring@^4.0.1:
55635553
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
55645554
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=
55655555

5566-
lodash.restparam@*:
5567-
version "3.6.1"
5568-
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
5569-
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
5570-
55715556
lodash.template@^4.0.2:
55725557
version "4.5.0"
55735558
resolved "https://registry.yarnpkg.com/lodash.template/-/lodash.template-4.5.0.tgz#f976195cf3f347d0d5f52483569fe8031ccce8ab"

0 commit comments

Comments
 (0)