loopback-datasource-juggler/lib/types.js

75 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-04-01 22:25:16 +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
2016-08-22 19:55:22 +00:00
'use strict';
2016-04-01 22:25:16 +00:00
2018-12-07 16:13:48 +00:00
const Types = {};
2014-02-14 07:42:21 +00:00
/**
* 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
2016-04-01 11:48:17 +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
2016-04-01 11:48:17 +00:00
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
2016-04-01 11:48:17 +00:00
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
2016-04-01 11:48:17 +00:00
module.exports = function(modelTypes) {
2018-12-07 16:13:48 +00:00
const DateString = require('./date-string');
const GeoPoint = require('./geo').GeoPoint;
2014-02-14 07:42:21 +00:00
2018-12-07 16:13:48 +00:00
for (const 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 = {};
2016-04-01 11:48:17 +00:00
modelTypes.registerType = function(type, names) {
2014-01-24 17:09:53 +00:00
names = names || [];
names = names.concat([type.name]);
2018-12-07 16:13:48 +00:00
for (let n = 0; n < names.length; n++) {
2014-01-24 17:09:53 +00:00
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(DateString);
2014-02-14 07:42:21 +00:00
modelTypes.registerType(Buffer, ['Binary']);
modelTypes.registerType(Array);
modelTypes.registerType(GeoPoint);
modelTypes.registerType(Object);
};
2016-04-01 11:48:17 +00:00
module.exports.Types = Types;