loopback-datasource-juggler/examples/app.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

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
var ModelBuilder = require('../../loopback-datasource-juggler').ModelBuilder;
2013-05-24 05:20:20 +00:00
var modelBuilder = new ModelBuilder();
// 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-01 11:48:17 +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-01 11:48:17 +00:00
published: { type: Boolean, default: false, index: true },
});
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,
});
2016-04-01 11:48:17 +00:00
var Group = modelBuilder.define('Group', { group: String });
// 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;
};
2016-04-01 11:48:17 +00:00
var user = new User({ name: 'Joe' });
console.log(user);
2013-05-24 05:20:20 +00:00
console.log(modelBuilder.models);
console.log(modelBuilder.definitions);
2013-05-28 20:50:59 +00:00
User.mixin(Group);
2016-04-01 11:48:17 +00:00
var user = new User({ name: 'Ray', group: 'Admin' });
2013-05-28 20:50:59 +00:00
console.log(user);