Defaults corruption
- Vorherrschende Sprache
- JavaScript
- Sterne
- 2k
- Forks
- 238
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
Any defaults that contain a reference will be corrupted when a value is set into the corresponding attribute. Here's a short example:
``` coffeescript
{Schema} = require 'jugglingdb'
schema = new Schema require('jugglingdb/lib/adapters/memory'), {}
Test = schema.define 'Test',
attribute:
type: Schema.JSON
default: {}
test = new Test()
test.attribute['foo'] = 'bar'
test2 = new Test()
throw new Error "Corrupted default" if test2.attribute['foo'] is 'bar'
```
...and in JS in case you hate Coffee:
``` javascript
(function() {
var Schema, Test, schema, test, test2;
Schema = require('jugglingdb').Schema;
schema = new Schema(require('jugglingdb/lib/adapters/memory'), {});
Test = schema.define('Test', {
attribute: {
type: Schema.JSON,
"default": {}
}
});
test = new Test();
test.attribute['foo'] = 'bar';
test2 = new Test();
if (test2.attribute['foo'] === 'bar') {
throw new Error("Corrupted default");
}
}).call(this);
```
[You can see in model.js](https://github.com/cha0s/promised-jugglingdb/blob/master/lib/model.js#L109) that the getDefault function simply returns the default, which in the case of Schema.JSON (and possibly others) is a reference to the default object. This means that when a key is set in a property of a specific model instance, the change will propagate up to (and corrupt) the default object.
I'm not sure what the correct fix is. I am working around this in my own code by using the function version of default, and returning an empty object from a simple closure.
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Rechercherichtung
The bug is in lib/model.js line 109 where getDefault returns a reference to the default object. Look at how Schema.JSON defaults are handled and compare with other types. The fix likely involves cloning the default object or using a function to return a new object each time. Test by running the provided CoffeeScript or JavaScript snippet to reproduce the corruption.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, nodejs
- Bereich
- backend, databases
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 45/100