loopback-datasource-juggler/lib/types.js

72 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-04-06 14:51:49 +00:00
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2014-02-14 07:42:21 +00:00
var Types = {};
/**
* Schema types
*/
Types.Text = function Text(value) {
if (!(this instanceof Text)) {
return value;
}
this.value = value;
}; // Text type
2013-07-25 14:48:31 +00:00
Types.Text.prototype.toObject = Types.Text.prototype.toJSON = function() {
2014-02-14 07:42:21 +00:00
return this.value;
};
2014-01-24 17:09:53 +00:00
2014-02-14 07:42:21 +00:00
Types.JSON = function JSON(value) {
if (!(this instanceof JSON)) {
return value;
}
this.value = value;
}; // JSON Object
Types.JSON.prototype.toObject = Types.JSON.prototype.toJSON = function() {
2014-02-14 07:42:21 +00:00
return this.value;
};
2014-01-24 17:09:53 +00:00
2014-02-14 07:42:21 +00:00
Types.Any = function Any(value) {
if (!(this instanceof Any)) {
return value;
}
this.value = value;
}; // Any Type
Types.Any.prototype.toObject = Types.Any.prototype.toJSON = function() {
2014-02-14 07:42:21 +00:00
return this.value;
};
2014-01-24 17:09:53 +00:00
module.exports = function(modelTypes) {
2014-02-14 07:42:21 +00:00
var GeoPoint = require('./geo').GeoPoint;
for (var t in Types) {
2014-02-14 07:42:21 +00:00
modelTypes[t] = Types[t];
}
2014-01-24 17:09:53 +00:00
2014-02-14 07:42:21 +00:00
modelTypes.schemaTypes = {};
modelTypes.registerType = function(type, names) {
2014-01-24 17:09:53 +00:00
names = names || [];
names = names.concat([type.name]);
for (var n = 0; n < names.length; n++) {
this.schemaTypes[names[n].toLowerCase()] = type;
}
};
2014-02-14 07:42:21 +00:00
modelTypes.registerType(Types.Text);
modelTypes.registerType(Types.JSON);
modelTypes.registerType(Types.Any);
modelTypes.registerType(String);
modelTypes.registerType(Number);
modelTypes.registerType(Boolean);
modelTypes.registerType(Date);
modelTypes.registerType(Buffer, ['Binary']);
modelTypes.registerType(Array);
modelTypes.registerType(GeoPoint);
modelTypes.registerType(Object);
};
module.exports.Types = Types;