Skip to content

Commit 5dceea3

Browse files
committed
FIX #596 #597 Default value not applied when the stored value is undefined
1 parent 6ed5579 commit 5dceea3

File tree

3 files changed

+80
-32
lines changed

3 files changed

+80
-32
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Bugfixes:
66
- Sort by sub object is not working [#585](https://github.com/pubkey/rxdb/issues/585)
77
- Encrypted attachments not working inside of electron-renderer [#587](https://github.com/pubkey/rxdb/issues/587)
88
- Schema fails with sub-sub-index [#590](https://github.com/pubkey/rxdb/issues/590)
9+
- Default value not applied when the stored value is `undefined` [#596](https://github.com/pubkey/rxdb/issues/596)
10+
911
### 7.4.2 (March 22, 2018)
1012

1113
Bugfixes:

src/rx-schema.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,8 @@ export class RxSchema {
133133
obj = util.clone(obj);
134134
Object
135135
.entries(this.defaultValues)
136-
.filter(entry => !obj.hasOwnProperty(entry[0]))
137-
.forEach(entry => {
138-
obj[entry[0]] = entry[1];
139-
});
136+
.filter(([k]) => !obj.hasOwnProperty(k) || typeof obj[k] === 'undefined')
137+
.forEach(([k, v]) => obj[k] = v);
140138
return obj;
141139
}
142140

test/unit/rx-collection.test.js

+76-28
Original file line numberDiff line numberDiff line change
@@ -1444,36 +1444,84 @@ config.parallel('rx-collection.test.js', () => {
14441444
});
14451445
});
14461446
describe('issues', () => {
1447-
describe('#528 default value ignored when 0', () => {
1448-
it('should use value when default=0', async () => {
1449-
const schema = {
1450-
version: 0,
1451-
type: 'object',
1452-
properties: {
1453-
passportId: {
1454-
type: 'string',
1455-
primary: true
1456-
},
1457-
weight: {
1458-
type: 'number',
1459-
default: 0
1460-
}
1447+
it('#528 default value ignored when 0', async () => {
1448+
const schema = {
1449+
version: 0,
1450+
type: 'object',
1451+
properties: {
1452+
passportId: {
1453+
type: 'string',
1454+
primary: true
1455+
},
1456+
weight: {
1457+
type: 'number',
1458+
default: 0
14611459
}
1462-
};
1463-
const db = await RxDatabase.create({
1464-
name: util.randomCouchString(10),
1465-
adapter: 'memory'
1466-
});
1467-
const collection = await db.collection({
1468-
name: 'humanx',
1469-
schema
1470-
});
1471-
const doc = await collection.insert({
1472-
passportId: util.randomCouchString(10)
1473-
});
1474-
assert.equal(doc.weight, 0);
1475-
db.destroy();
1460+
}
1461+
};
1462+
const db = await RxDatabase.create({
1463+
name: util.randomCouchString(10),
1464+
adapter: 'memory'
1465+
});
1466+
const collection = await db.collection({
1467+
name: 'humanx',
1468+
schema
1469+
});
1470+
const doc = await collection.insert({
1471+
passportId: util.randomCouchString(10)
1472+
});
1473+
assert.equal(doc.weight, 0);
1474+
db.destroy();
1475+
});
1476+
it('#596 Default value not applied when value is undefined', async () => {
1477+
const schema = {
1478+
version: 0,
1479+
type: 'object',
1480+
properties: {
1481+
passportId: {
1482+
type: 'string',
1483+
primary: true
1484+
},
1485+
firstName: {
1486+
type: 'string'
1487+
},
1488+
lastName: {
1489+
type: 'string'
1490+
},
1491+
age: {
1492+
type: 'integer',
1493+
minimum: 0,
1494+
maximum: 150
1495+
},
1496+
score: {
1497+
type: 'integer',
1498+
default: 100
1499+
}
1500+
}
1501+
};
1502+
const db = await RxDatabase.create({
1503+
name: util.randomCouchString(10),
1504+
adapter: 'memory'
1505+
});
1506+
const collection = await db.collection({
1507+
name: 'humanx',
1508+
schema
1509+
});
1510+
// insert a document
1511+
await collection.insert({
1512+
passportId: 'foobar',
1513+
firstName: 'Bob',
1514+
lastName: 'Kelso',
1515+
age: 56,
1516+
score: undefined
14761517
});
1518+
const myDocument = await collection
1519+
.findOne()
1520+
.where('firstName')
1521+
.eq('Bob')
1522+
.exec();
1523+
assert.equal(myDocument.score, 100);
1524+
db.destroy();
14771525
});
14781526
});
14791527
describe('wait a bit', () => {

0 commit comments

Comments
 (0)