loopback-datasource-juggler/examples/app-noschema.js

87 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-04-06 14:51:49 +00:00
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
var DataSource = require('../../loopback-datasource-juggler').DataSource;
var ModelBuilder = require('../../loopback-datasource-juggler').ModelBuilder;
2013-08-07 21:51:32 +00:00
var introspectType = require('../lib/introspection')(ModelBuilder);
2013-07-26 21:15:07 +00:00
var ds = new DataSource('memory');
// Create a open model that doesn't require a schema
var Application = ds.createModel('Schemaless', {}, {strict: false});
var application = {
2014-01-24 17:09:53 +00:00
owner: 'rfeng',
name: 'MyApp1',
description: 'My first app',
pushSettings: [
{ "platform": "apns",
"apns": {
"pushOptions": {
"gateway": "gateway.sandbox.push.apple.com",
"cert": "credentials/apns_cert_dev.pem",
"key": "credentials/apns_key_dev.pem"
},
2013-07-26 21:15:07 +00:00
2014-01-24 17:09:53 +00:00
"feedbackOptions": {
"gateway": "feedback.sandbox.push.apple.com",
"cert": "credentials/apns_cert_dev.pem",
"key": "credentials/apns_key_dev.pem",
"batchFeedback": true,
"interval": 300
}
}}
]}
2013-07-26 21:15:07 +00:00
console.log(new Application(application).toObject());
Application.create(application, function (err, app1) {
2014-01-24 17:09:53 +00:00
console.log('Created: ', app1.toObject());
Application.findById(app1.id, function (err, app2) {
console.log('Found: ', app2.toObject());
});
2013-07-26 21:15:07 +00:00
});
// Instance JSON document
var user = {
2014-01-24 17:09:53 +00:00
name: 'Joe',
age: 30,
birthday: new Date(),
vip: true,
address: {
street: '1 Main St',
city: 'San Jose',
state: 'CA',
zipcode: '95131',
country: 'US'
},
friends: ['John', 'Mary'],
emails: [
{label: 'work', id: 'x@sample.com'},
{label: 'home', id: 'x@home.com'}
],
tags: []
2013-07-26 21:15:07 +00:00
};
// Introspect the JSON document to generate a schema
var schema = introspectType(user);
// Create a model for the generated schema
var User = ds.createModel('User', schema, {idInjection: true});
// Use the model for CRUD
var obj = new User(user);
console.log(obj.toObject());
User.create(user, function (err, u1) {
2014-01-24 17:09:53 +00:00
console.log('Created: ', u1.toObject());
User.findById(u1.id, function (err, u2) {
console.log('Found: ', u2.toObject());
});
2013-07-26 21:15:07 +00:00
});