1602 / 1602/jugglingdb

Defaults corruption

Đang mở
#393 4 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
JavaScript
Star
2k
Fork
238
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, nodejs
Lĩnh vực
backend, databases
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.