diff --git a/examples/app-noschema.js b/examples/app-noschema.js index 528bf636..9187159f 100644 --- a/examples/app-noschema.js +++ b/examples/app-noschema.js @@ -1,6 +1,6 @@ var DataSource = require('../../loopback-datasource-juggler').DataSource; var ModelBuilder = require('../../loopback-datasource-juggler').ModelBuilder; -var introspectType = require('../lib/introspection'); +var introspectType = require('../lib/introspection')(ModelBuilder); var ds = new DataSource('memory'); diff --git a/lib/introspection.js b/lib/introspection.js index 80e29f8a..5dcca91c 100644 --- a/lib/introspection.js +++ b/lib/introspection.js @@ -1,59 +1,60 @@ -var ModelBuilder = require('./model-builder').ModelBuilder; +module.exports = function getIntrospector(ModelBuilder) { -function introspectType(value) { + function introspectType(value) { - // Unknown type, using Any - if (value === null || value === undefined) { - return ModelBuilder.Any; - } - - // Check registered schemaTypes - for (var t in ModelBuilder.schemaTypes) { - var st = ModelBuilder.schemaTypes[t]; - if (st !== Object && st !== Array && (value instanceof st)) { - return t; + // Unknown type, using Any + if (value === null || value === undefined) { + return ModelBuilder.Any; } - } - var type = typeof value; - if (type === 'string' || type === 'number' || type === 'boolean') { - return type; - } - - if (value instanceof Date) { - return 'date'; - } - - if (Array.isArray(value)) { - for (var i = 0; i < value.length; i++) { - if (value[i] === null || value[i] === undefined) { - continue; + // Check registered schemaTypes + for (var t in ModelBuilder.schemaTypes) { + var st = ModelBuilder.schemaTypes[t]; + if (st !== Object && st !== Array && (value instanceof st)) { + return t; } - var itemType = introspectType(value[i]); + } + + var type = typeof value; + if (type === 'string' || type === 'number' || type === 'boolean') { + return type; + } + + if (value instanceof Date) { + return 'date'; + } + + if (Array.isArray(value)) { + for (var i = 0; i < value.length; i++) { + if (value[i] === null || value[i] === undefined) { + continue; + } + var itemType = introspectType(value[i]); + if (itemType) { + return [itemType]; + } + } + return 'array'; + } + + if (type === 'function') { + return value.constructor.name; + } + + var properties = {}; + for (var p in value) { + var itemType = introspectType(value[p]); if (itemType) { - return [itemType]; + properties[p] = itemType; } } - return 'array'; - } - - if (type === 'function') { - return value.constructor.name; - } - - var properties = {}; - for (var p in value) { - var itemType = introspectType(value[p]); - if (itemType) { - properties[p] = itemType; + if (Object.keys(properties).length === 0) { + return 'object'; } + return properties; } - if(Object.keys(properties).length === 0) { - return 'object'; - } - return properties; + + return introspectType; } -module.exports = introspectType; - diff --git a/lib/model-builder.js b/lib/model-builder.js index b1ce9263..f205648c 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -8,6 +8,11 @@ var List = require('./list.js'); var EventEmitter = require('events').EventEmitter; var util = require('util'); +// Set up types +require('./types')(ModelBuilder); + +var introspect = require('./introspection')(ModelBuilder); + /** * Export public API */ @@ -31,9 +36,6 @@ function ModelBuilder() { util.inherits(ModelBuilder, EventEmitter); -// Set up types -require('./types')(ModelBuilder); - /** * Define class * @@ -516,5 +518,21 @@ ModelBuilder.prototype.buildModels = function (schemas) { return models; } +/** + * Introspect the json document to build a corresponding model + * @param name + * @param json + * @param options + * @returns {} + */ +ModelBuilder.prototype.buildModelFromInstance = function(name, json, options) { + + // Introspect the JSON document to generate a schema + var schema = introspect(json); + + // Create a model for the generated schema + return this.define(name, schema, options); +} + diff --git a/test/introspection.test.js b/test/introspection.test.js index ac3a0e17..8004e93a 100644 --- a/test/introspection.test.js +++ b/test/introspection.test.js @@ -1,6 +1,6 @@ var assert = require('assert'); var ModelBuilder = require('../lib/model-builder').ModelBuilder; -var introspectType = require('../lib/introspection'); +var introspectType = require('../lib/introspection')(ModelBuilder); var traverse = require('traverse'); describe('Introspection of model definitions from JSON', function() {