2016-04-01 22:25:16 +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
|
|
|
|
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2013-07-30 21:10:03 +00:00
|
|
|
var ModelBuilder = require('../../loopback-datasource-juggler').ModelBuilder;
|
2013-05-24 05:20:20 +00:00
|
|
|
var modelBuilder = new ModelBuilder();
|
2013-05-17 15:49:57 +00:00
|
|
|
// define models
|
2013-05-24 05:20:20 +00:00
|
|
|
var Post = modelBuilder.define('Post', {
|
2016-08-19 17:46:59 +00:00
|
|
|
title: {type: String, length: 255},
|
|
|
|
content: {type: ModelBuilder.Text},
|
|
|
|
date: {type: Date, default: function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
return new Date();
|
2016-08-19 17:46:59 +00:00
|
|
|
}},
|
|
|
|
timestamp: {type: Number, default: Date.now},
|
|
|
|
published: {type: Boolean, default: false, index: true},
|
2013-05-17 15:49:57 +00:00
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// simpler way to describe model
|
2013-05-24 05:20:20 +00:00
|
|
|
var User = modelBuilder.define('User', {
|
2014-01-24 17:09:53 +00:00
|
|
|
name: String,
|
|
|
|
bio: ModelBuilder.Text,
|
|
|
|
approved: Boolean,
|
|
|
|
joinedAt: Date,
|
2016-04-01 11:48:17 +00:00
|
|
|
age: Number,
|
2013-05-17 15:49:57 +00:00
|
|
|
});
|
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
var Group = modelBuilder.define('Group', {group: String});
|
2013-05-17 15:49:57 +00:00
|
|
|
|
|
|
|
// define any custom method
|
2016-04-01 11:48:17 +00:00
|
|
|
User.prototype.getNameAndAge = function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
return this.name + ', ' + this.age;
|
2013-05-17 15:49:57 +00:00
|
|
|
};
|
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
var user = new User({name: 'Joe'});
|
2013-05-17 15:49:57 +00:00
|
|
|
console.log(user);
|
|
|
|
|
2013-05-24 05:20:20 +00:00
|
|
|
console.log(modelBuilder.models);
|
|
|
|
console.log(modelBuilder.definitions);
|
2013-05-17 15:49:57 +00:00
|
|
|
|
2013-05-28 20:50:59 +00:00
|
|
|
User.mixin(Group);
|
2017-01-31 15:14:53 +00:00
|
|
|
user = new User({name: 'Ray', group: 'Admin'});
|
2013-05-28 20:50:59 +00:00
|
|
|
console.log(user);
|