UNPKG

1.89 kBJavaScriptView Raw
1// Copyright IBM Corp. 2013,2019. All Rights Reserved.
2// Node module: loopback-datasource-juggler
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6'use strict';
7
8const Types = {};
9/**
10 * Schema types
11 */
12Types.Text = function Text(value) {
13 if (!(this instanceof Text)) {
14 return value;
15 }
16 this.value = value;
17}; // Text type
18
19Types.Text.prototype.toObject = Types.Text.prototype.toJSON = function() {
20 return this.value;
21};
22
23Types.JSON = function JSON(value) {
24 if (!(this instanceof JSON)) {
25 return value;
26 }
27 this.value = value;
28}; // JSON Object
29Types.JSON.prototype.toObject = Types.JSON.prototype.toJSON = function() {
30 return this.value;
31};
32
33Types.Any = function Any(value) {
34 if (!(this instanceof Any)) {
35 return value;
36 }
37 this.value = value;
38}; // Any Type
39Types.Any.prototype.toObject = Types.Any.prototype.toJSON = function() {
40 return this.value;
41};
42
43module.exports = function(modelTypes) {
44 const DateString = require('./date-string');
45 const GeoPoint = require('./geo').GeoPoint;
46
47 for (const t in Types) {
48 modelTypes[t] = Types[t];
49 }
50
51 modelTypes.schemaTypes = {};
52 modelTypes.registerType = function(type, names) {
53 names = names || [];
54 names = names.concat([type.name]);
55 for (let n = 0; n < names.length; n++) {
56 this.schemaTypes[names[n].toLowerCase()] = type;
57 }
58 };
59
60 modelTypes.registerType(Types.Text);
61 modelTypes.registerType(Types.JSON);
62 modelTypes.registerType(Types.Any);
63
64 modelTypes.registerType(String);
65 modelTypes.registerType(Number);
66 modelTypes.registerType(Boolean);
67 modelTypes.registerType(Date);
68 modelTypes.registerType(DateString);
69 modelTypes.registerType(Buffer, ['Binary']);
70 modelTypes.registerType(Array);
71 modelTypes.registerType(GeoPoint);
72 modelTypes.registerType(Object);
73};
74
75module.exports.Types = Types;