loopback-datasource-juggler/examples/app.js

44 lines
1.2 KiB
JavaScript
Raw Normal View History

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
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 },
date: { type: Date, default: function() {
2014-01-24 17:09:53 +00:00
return new Date();
} },
timestamp: { type: Number, default: Date.now },
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,
age: Number,
});
var Group = modelBuilder.define('Group', { group: String });
// define any custom method
User.prototype.getNameAndAge = function() {
2014-01-24 17:09:53 +00:00
return this.name + ', ' + this.age;
};
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);
var user = new User({ name: 'Ray', group: 'Admin' });
2013-05-28 20:50:59 +00:00
console.log(user);