2020-01-21 18:12:14 +00:00
|
|
|
// 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 ModelBuilder = require('../../loopback-datasource-juggler').ModelBuilder;
|
|
|
|
const modelBuilder = new ModelBuilder();
|
2013-05-17 15:49:57 +00:00
|
|
|
// define models
|
2018-12-07 14:54:29 +00:00
|
|
|
const 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
|
2018-12-07 14:54:29 +00:00
|
|
|
const 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
|
|
|
});
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const 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
|
|
|
};
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
let 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);
|