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,4 +1,4 @@
var ModelBuilder = require('./model-builder').ModelBuilder;
module.exports = function getIntrospector(ModelBuilder) {
function introspectType(value) {
@ -54,6 +54,7 @@ function introspectType(value) {
return properties;
}
module.exports = introspectType;
return 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() {