1602 / 1602/jugglingdb

Defaults corruption

Ouverte
#393 4 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
JavaScript
Étoiles
2k
Forks
238
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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.

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript, nodejs
Domaine
backend, databases
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
À l'abandon
Clarté
Clairement spécifiée
Accessibilité débutants
45/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.