UNPKG

2.33 kBJavaScriptView Raw
1// Copyright IBM Corp. 2013,2019. All Rights Reserved.
2// Node module: loopback-datasource-juggler
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6'use strict';
7
8const DataSource = require('../../loopback-datasource-juggler').DataSource;
9const ModelBuilder = require('../../loopback-datasource-juggler').ModelBuilder;
10const introspectType = require('../lib/introspection')(ModelBuilder);
11
12const ds = new DataSource('memory');
13
14// Create a open model that doesn't require a schema
15const Application = ds.createModel('Schemaless', {}, {strict: false});
16
17const application = {
18 owner: 'rfeng',
19 name: 'MyApp1',
20 description: 'My first app',
21 pushSettings: [
22 {'platform': 'apns',
23 'apns': {
24 'pushOptions': {
25 'gateway': 'gateway.sandbox.push.apple.com',
26 'cert': 'credentials/apns_cert_dev.pem',
27 'key': 'credentials/apns_key_dev.pem',
28 },
29
30 'feedbackOptions': {
31 'gateway': 'feedback.sandbox.push.apple.com',
32 'cert': 'credentials/apns_cert_dev.pem',
33 'key': 'credentials/apns_key_dev.pem',
34 'batchFeedback': true,
35 'interval': 300,
36 },
37 }},
38 ]};
39
40console.log(new Application(application).toObject());
41
42Application.create(application, function(err, app1) {
43 console.log('Created: ', app1.toObject());
44 Application.findById(app1.id, function(err, app2) {
45 console.log('Found: ', app2.toObject());
46 });
47});
48
49// Instance JSON document
50const user = {
51 name: 'Joe',
52 age: 30,
53 birthday: new Date(),
54 vip: true,
55 address: {
56 street: '1 Main St',
57 city: 'San Jose',
58 state: 'CA',
59 zipcode: '95131',
60 country: 'US',
61 },
62 friends: ['John', 'Mary'],
63 emails: [
64 {label: 'work', id: 'x@sample.com'},
65 {label: 'home', id: 'x@home.com'},
66 ],
67 tags: [],
68};
69
70// Introspect the JSON document to generate a schema
71const schema = introspectType(user);
72
73// Create a model for the generated schema
74const User = ds.createModel('User', schema, {idInjection: true});
75
76// Use the model for CRUD
77const obj = new User(user);
78
79console.log(obj.toObject());
80
81User.create(user, function(err, u1) {
82 console.log('Created: ', u1.toObject());
83 User.findById(u1.id, function(err, u2) {
84 console.log('Found: ', u2.toObject());
85 });
86});