Skip to content

Commit 8e672b4

Browse files
fix: isEditable for some primaryKeys
where property had defaultValue: sequelize.UUIDV4 - it was editable
1 parent f7bd90d commit 8e672b4

File tree

5 files changed

+72
-28
lines changed

5 files changed

+72
-28
lines changed

models/comment.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = (sequelize, DataTypes) => {
2+
const Comment = sequelize.define('Comment', {
3+
id: {
4+
primaryKey: true,
5+
type: DataTypes.UUID,
6+
defaultValue: sequelize.UUIDV4,
7+
},
8+
title: DataTypes.STRING,
9+
description: DataTypes.STRING,
10+
publishedAt: DataTypes.DATE,
11+
}, {})
12+
return Comment
13+
}

src/database.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('Database', () => {
3535
describe('#resources', () => {
3636
it('fetches all resources when entire db is given', () => {
3737
const database = new Database(db)
38-
expect(database.resources()).to.have.lengthOf(2)
38+
expect(database.resources()).to.have.lengthOf(Object.keys(db.sequelize.models).length)
3939
expect(database.resources()[0]).to.be.an.instanceof(Resource)
4040
})
4141

src/property.spec.ts

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,94 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
22
import chai, { expect } from 'chai'
33
import sinonChai from 'sinon-chai'
4+
import { AvailableTestModels } from './utils/available-test-models'
45

56
import Property from './property'
67

78
chai.use(sinonChai)
89

910
const db = require('../models/index.js')
1011

11-
describe('Property', () => {
12-
before(function () {
13-
this.SequelizeModel = db.sequelize.models.User
14-
this.rawAttributes = this.SequelizeModel.attributes || this.SequelizeModel.rawAttributes
15-
})
12+
const getRawProperty = (modelName: AvailableTestModels, propertyName) => {
13+
const model = db.sequelize.models[modelName]
14+
const propertyAttrs = model.attributes || model.rawAttributes
15+
return propertyAttrs[propertyName]
16+
}
1617

18+
describe('Property', () => {
1719
describe('#isArray', () => {
18-
it('returns false for regular (not arrayed) property', function () {
19-
const property = new Property(this.rawAttributes.email)
20+
it('returns false for regular (not arrayed) property', () => {
21+
const property = new Property(getRawProperty('User', 'email'))
2022
expect(property.isArray()).to.equal(false)
2123
})
2224

23-
it('returns true for array property', function () {
24-
const property = new Property(this.rawAttributes.arrayed)
25+
it('returns true for array property', () => {
26+
const property = new Property(getRawProperty('User', 'arrayed'))
2527
expect(property.isArray()).to.equal(true)
2628
})
2729
})
2830

2931
describe('#type', () => {
30-
it('returns correct string type', function () {
31-
const property = new Property(this.rawAttributes.firstName)
32+
it('returns correct string type', () => {
33+
const property = new Property(getRawProperty('User', 'firstName'))
3234
expect(property.type()).to.equal('string')
3335
})
3436

35-
it('returns correct integer type', function () {
36-
const property = new Property(this.rawAttributes.id)
37+
it('returns correct integer type', () => {
38+
const property = new Property(getRawProperty('User', 'id'))
3739
expect(property.type()).to.equal('number')
3840
})
3941

40-
it('returns correct date type', function () {
41-
const property = new Property(this.rawAttributes.createdAt)
42+
it('returns correct date type', () => {
43+
const property = new Property(getRawProperty('User', 'createdAt'))
4244
expect(property.type()).to.equal('datetime')
4345
})
4446

45-
it('returns string when property is an array of strings', function () {
46-
const property = new Property(this.rawAttributes.arrayed)
47+
it('returns string when property is an array of strings', () => {
48+
const property = new Property(getRawProperty('User', 'arrayed'))
4749
expect(property.type()).to.equal('string')
4850
})
4951
})
5052

5153
describe('#availableValues', () => {
52-
it('returns null for all standard (Non enum) values', function () {
53-
const property = new Property(this.rawAttributes.email)
54+
it('returns null for all standard (Non enum) values', () => {
55+
const property = new Property(getRawProperty('User', 'email'))
5456
expect(property.availableValues()).to.equal(null)
5557
})
5658

57-
it('returns array of values for the enum field', function () {
58-
const property = new Property(this.rawAttributes.gender)
59+
it('returns array of values for the enum field', () => {
60+
const property = new Property(getRawProperty('User', 'gender'))
5961
expect(property.availableValues()).to.deep.equal(['male', 'female'])
6062
})
6163
})
6264

65+
describe('#isEditable', () => {
66+
it('returns false for UUID property', () => {
67+
const property = new Property(getRawProperty('Comment', 'id'))
68+
expect(property.isEditable()).to.equal(false)
69+
})
70+
})
71+
72+
describe('#isId', () => {
73+
it('returns true for id when its default', () => {
74+
const property = new Property(getRawProperty('User', 'id'))
75+
expect(property.isId()).to.eq(true)
76+
})
77+
78+
it('returns true for id when its uuid', () => {
79+
const property = new Property(getRawProperty('Comment', 'id'))
80+
expect(property.isId()).to.eq(true)
81+
})
82+
})
83+
6384
describe('isRequired', () => {
64-
it('returns true for required fields', function () {
65-
const property = new Property(this.rawAttributes.email)
85+
it('returns true for required fields', () => {
86+
const property = new Property(getRawProperty('User', 'email'))
6687
expect(property.isRequired()).to.equal(true)
6788
})
6889

69-
it('returns false for not required fields', function () {
70-
const property = new Property(this.rawAttributes.gender)
90+
it('returns false for not required fields', () => {
91+
const property = new Property(getRawProperty('User', 'gender'))
7192
expect(property.isRequired()).to.eq(false)
7293
})
7394
})

src/property.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-underscore-dangle */
12
import { BaseProperty, PropertyType } from 'admin-bro'
23
import { ModelAttributeColumnOptions } from 'sequelize/types'
34

@@ -43,8 +44,16 @@ class Property extends BaseProperty {
4344
}
4445

4546
isEditable(): boolean {
46-
// eslint-disable-next-line no-underscore-dangle
47-
return !(this.sequelizePath as any)._autoGenerated && !this.sequelizePath.autoIncrement
47+
if ((this.sequelizePath as any)._autoGenerated) {
48+
return false
49+
}
50+
if (this.sequelizePath.autoIncrement) {
51+
return false
52+
}
53+
if (this.isId()) {
54+
return false
55+
}
56+
return true
4857
}
4958

5059
isVisible(): boolean {

src/utils/available-test-models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type AvailableTestModels = 'User' | 'Post' | 'Comment'

0 commit comments

Comments
 (0)