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
|
|
|
|
|
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', {
|
2014-01-24 17:09:53 +00:00
|
|
|
title: { type: String, length: 255 },
|
|
|
|
content: { type: ModelBuilder.Text },
|
2016-04-14 14:41:19 +00:00
|
|
|
date: { type: Date, default: function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
return new Date();
|
|
|
|
} },
|
|
|
|
timestamp: { type: Number, default: Date.now },
|
2016-04-14 14:41:19 +00:00
|
|
|
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-14 14:41:19 +00:00
|
|
|
age: Number,
|
2013-05-17 15:49:57 +00:00
|
|
|
});
|
|
|
|
|
2016-04-14 14:41:19 +00:00
|
|
|
var Group = modelBuilder.define('Group', { group: String });
|
2013-05-17 15:49:57 +00:00
|
|
|
|
|
|
|
// define any custom method
|
2016-04-14 14:41:19 +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-04-14 14:41:19 +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);
|
2016-04-14 14:41:19 +00:00
|
|
|
var user = new User({ name: 'Ray', group: 'Admin' });
|
2013-05-28 20:50:59 +00:00
|
|
|
console.log(user);
|