loopback-datasource-juggler/examples/load-schemas.js

35 lines
833 B
JavaScript
Raw Normal View History

2013-05-24 05:20:20 +00:00
var path = require('path'),
2014-01-24 17:09:53 +00:00
fs = require('fs'),
DataSource = require('../lib/datasource').DataSource;
2013-05-24 05:20:20 +00:00
/**
2013-07-22 20:48:28 +00:00
* Load LDL schemas from a json doc
* @param schemaFile The dataSource json file
2013-05-24 05:20:20 +00:00
* @returns A map of schemas keyed by name
*/
function loadSchemasSync(schemaFile, dataSource) {
2014-01-24 17:09:53 +00:00
// Set up the data source
if (!dataSource) {
dataSource = new DataSource('memory');
}
2014-01-24 17:09:53 +00:00
// Read the dataSource JSON file
var schemas = JSON.parse(fs.readFileSync(schemaFile));
2013-05-24 05:20:20 +00:00
2014-01-24 17:09:53 +00:00
return dataSource.buildModels(schemas);
2013-05-24 05:20:20 +00:00
}
var models = loadSchemasSync(path.join(__dirname, 'jdb-schemas.json'));
for (var s in models) {
2014-01-24 17:09:53 +00:00
var m = models[s];
console.log(m.modelName, new m());
}
models = loadSchemasSync(path.join(__dirname, 'schemas.json'));
for (var s in models) {
2014-01-24 17:09:53 +00:00
var m = models[s];
console.log(m.modelName, new m());
}