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

87 lines
2.3 KiB
JavaScript
Raw Normal View History

// Copyright IBM Corp. 2013,2019. All Rights Reserved.
2016-04-01 22:25:16 +00:00
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2016-08-22 19:55:22 +00:00
'use strict';
2018-12-07 14:54:29 +00:00
const DataSource = require('../../loopback-datasource-juggler').DataSource;
const ModelBuilder = require('../../loopback-datasource-juggler').ModelBuilder;
const introspectType = require('../lib/introspection')(ModelBuilder);
2013-07-26 21:15:07 +00:00
2018-12-07 14:54:29 +00:00
const ds = new DataSource('memory');
2013-07-26 21:15:07 +00:00
// Create a open model that doesn't require a schema
2018-12-07 14:54:29 +00:00
const Application = ds.createModel('Schemaless', {}, {strict: false});
2013-07-26 21:15:07 +00:00
2018-12-07 14:54:29 +00:00
const application = {
2014-01-24 17:09:53 +00:00
owner: 'rfeng',
name: 'MyApp1',
description: 'My first app',
pushSettings: [
2016-08-19 17:46:59 +00:00
{'platform': 'apns',
2016-04-01 11:48:17 +00:00
'apns': {
'pushOptions': {
'gateway': 'gateway.sandbox.push.apple.com',
'cert': 'credentials/apns_cert_dev.pem',
2016-04-01 13:23:42 +00:00
'key': 'credentials/apns_key_dev.pem',
2014-01-24 17:09:53 +00:00
},
2013-07-26 21:15:07 +00:00
2016-04-01 11:48:17 +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,
},
}},
2016-08-19 17:46:59 +00:00
]};
2013-07-26 21:15:07 +00:00
console.log(new Application(application).toObject());
2016-04-01 11:48:17 +00:00
Application.create(application, function(err, app1) {
2014-01-24 17:09:53 +00:00
console.log('Created: ', app1.toObject());
2016-04-01 11:48:17 +00:00
Application.findById(app1.id, function(err, app2) {
2014-01-24 17:09:53 +00:00
console.log('Found: ', app2.toObject());
});
2013-07-26 21:15:07 +00:00
});
// Instance JSON document
2018-12-07 14:54:29 +00:00
const 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',
2016-04-01 11:48:17 +00:00
country: 'US',
2014-01-24 17:09:53 +00:00
},
friends: ['John', 'Mary'],
emails: [
2016-08-19 17:46:59 +00:00
{label: 'work', id: 'x@sample.com'},
{label: 'home', id: 'x@home.com'},
2014-01-24 17:09:53 +00:00
],
2016-04-01 11:48:17 +00:00
tags: [],
2013-07-26 21:15:07 +00:00
};
// Introspect the JSON document to generate a schema
2018-12-07 14:54:29 +00:00
const schema = introspectType(user);
2013-07-26 21:15:07 +00:00
// Create a model for the generated schema
2018-12-07 14:54:29 +00:00
const User = ds.createModel('User', schema, {idInjection: true});
2013-07-26 21:15:07 +00:00
// Use the model for CRUD
2018-12-07 14:54:29 +00:00
const obj = new User(user);
2013-07-26 21:15:07 +00:00
console.log(obj.toObject());
2016-04-01 11:48:17 +00:00
User.create(user, function(err, u1) {
2014-01-24 17:09:53 +00:00
console.log('Created: ', u1.toObject());
2016-04-01 11:48:17 +00:00
User.findById(u1.id, function(err, u2) {
2014-01-24 17:09:53 +00:00
console.log('Found: ', u2.toObject());
});
2013-07-26 21:15:07 +00:00
});