Refactor introspection to ModelBuilder

This commit is contained in:
Raymond Feng 2013-08-07 14:51:32 -07:00
parent f3443959a1
commit 0a5c0ff77d
4 changed files with 70 additions and 51 deletions

View File

@ -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');

View File

@ -1,6 +1,6 @@
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) {
@ -48,12 +48,13 @@ function introspectType(value) {
properties[p] = itemType;
}
}
if(Object.keys(properties).length === 0) {
if (Object.keys(properties).length === 0) {
return 'object';
}
return properties;
}
return introspectType;
}
module.exports = introspectType;

View File

@ -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);
}

View File

@ -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() {