-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ricardo Nogueira
committed
Jun 7, 2016
1 parent
de0c008
commit 97c3848
Showing
3 changed files
with
76 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,88 @@ | ||
|
||
/** | ||
* Export `bookshelf-json-columns` plugin. | ||
* Stringify JSON columns. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
function stringify() { | ||
var _this = this; | ||
|
||
this.jsonColumns.forEach(function (column) { | ||
if (_this.attributes[column]) { | ||
_this.attributes[column] = JSON.stringify(_this.attributes[column]); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Parse JSON columns. | ||
*/ | ||
|
||
function parse() { | ||
var _this2 = this; | ||
|
||
this.jsonColumns.forEach(function (column) { | ||
if (_this2.attributes[column]) { | ||
_this2.attributes[column] = JSON.parse(_this2.attributes[column]); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Export `bookshelf-json-columns` plugin. | ||
*/ | ||
|
||
exports['default'] = function (Bookshelf) { | ||
var Model = Bookshelf.Model.prototype; | ||
var client = Bookshelf.knex.client.config.client; | ||
|
||
Bookshelf.Model = Bookshelf.Model.extend({ | ||
initialize: function initialize() { | ||
var _this = this; | ||
|
||
if (!this.jsonColumns) { | ||
return Model.initialize.apply(this, arguments); | ||
} | ||
|
||
// Stringify JSON columns before saving. | ||
this.on('saving', function () { | ||
_this.jsonColumns.forEach(function (column) { | ||
if (_this.attributes[column]) { | ||
_this.attributes[column] = JSON.stringify(_this.attributes[column]); | ||
} | ||
}); | ||
}); | ||
// Stringify JSON columns before model is saved. | ||
this.on('saving', stringify.bind(this)); | ||
|
||
// Parse JSON columns after saving. | ||
this.on('saved', function () { | ||
_this.jsonColumns.forEach(function (column) { | ||
if (_this.attributes[column]) { | ||
_this.attributes[column] = JSON.parse(_this.attributes[column]); | ||
} | ||
}); | ||
}); | ||
// Parse JSON columns after model is saved. | ||
this.on('saved', parse.bind(this)); | ||
|
||
if (client === 'sqlite') { | ||
// Parse JSON columns after model is fetched. | ||
this.on('fetched', parse.bind(this)); | ||
} | ||
|
||
return Model.initialize.apply(this, arguments); | ||
} | ||
}); | ||
|
||
if (client === 'sqlite') { | ||
(function () { | ||
var Collection = Bookshelf.Collection.prototype; | ||
|
||
Bookshelf.Collection = Bookshelf.Collection.extend({ | ||
initialize: function initialize() { | ||
if (!this.model.prototype.jsonColumns) { | ||
return Collection.initialize.apply(this, arguments); | ||
} | ||
|
||
// Parse JSON columns after collection is fetched. | ||
this.on('fetched', function (collection) { | ||
collection.models.forEach(function (model) { | ||
parse.apply(model); | ||
}); | ||
}); | ||
|
||
return Collection.initialize.apply(this, arguments); | ||
} | ||
}); | ||
})(); | ||
} | ||
}; | ||
|
||
module.exports = exports['default']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters