私は node-orm2 で遊んで、RESTful API で使用するのがどのようなものかを感じています。バックエンドで Postgres データベースを使用しています。
Person と Task の間に OneToMany 関係があり、単一のファイルで次のようなドキュメントのバリエーションを試しています。
app.js
'strict'
// Based on http://blog.modulus.io/nodejs-and-express-create-rest-api
// but with my own twists!
var express = require('express');
var app = express();
var orm = require('orm');
// Helpers
var collectionize = function(target){
if(typeof target == 'Object'){
return [target];
}
return target;
};
// Configure the app with any middleware
app.use(express.bodyParser());
app.use(orm.express("postgres://rodrigomartell:password@localhost/postgres",{
define : function(db, models, next){
// Define models
models.Person = db.define("Person",
{
name : String,
surname : String,
age : Number,
male : Boolean,
continent: ["enum1", "enum2"],
data : Object
},
{
methods : {
fullName: function(){
return this.name + this.surname;
}
},
validations : {
name: orm.enforce.unique("name already taken!"),
age : orm.enforce.ranges.number(18, undefined, "under-age")
},
autoFetch : true // global eager load
}
);
models.Task = db.define("Task",
{
description: String
}
);
// Relations
models.Task.hasOne('person', models.Person, {reverse:'tasks', required: true});
// Finally drop and sync this puppy
db.drop(function(){
db.sync(function(){
console.log('All good');
// Create one Person on load
db.models.Person.create([{
name : "Kenny",
surname : "Powers",
age : 34,
male : true,
continent: "enum1"
}], function(err, items){
var person = items[0];
db.models.Task.create([{description:'Heyo!', person_id: person.id}], function(err, tasks){
console.log(err,tasks);
});
});
// Press on?
next(); // After synching, get on with the rest of the app?
},function(err){
console.log('No good', err);
});
});
}
}));
// Configure the routes and that
app.get("/people", function(req,res){
console.log('requested');
res.type('text/plain');
// req.models is a reference to models in the middleware config step
req.models.Person.find(null, function(err, people){ // There must be a neater way to findAll?
res.json(people);
});
});
app.post("/people", function(req,res){
console.log('requested');
res.type('text/plain');
req.models.Person.create(collectionize(req.body), function(err, newPerson){
if(err){
res.json({error: err});
}
else{
res.json(newPerson);
}
});
});
app.get("/tasks", function(req, res){
res.type('text/plain');
req.models.Task.find(null, function(err, tasks){ // There must be a neater way to findAll?
res.json(tasks);
});
});
app.post("/tasks", function(req, res){
res.type('text/plain');
var task = req.body;
req.models.Task.create([req.body], function(err, task){
if(err){
res.json(err);
}
else{
res.json(task);
}
});
});
// Listen up!
app.listen(4730, function(){
console.log('Listening on http://localhost:4730');
});
次のリソースの GET および POST ルートを設定しました。
{GET,POST} /人
{GET,POST} /タスク
DB の初期ロード時に、DB に何かを入れるためだけにタスクを持つ Person インスタンスを作成します。
localhost:4730/peopleでGETを実行すると、ロード時に作成された人物とそのタスクを取得できます (すばらしい):
[{
name : "Kenny",
surname : "Powers",
age : 34,
male : true,
continent: "enum1",
data : null,
id : 1,
tasks : [
{
description: "Heyo!",
id: 1,
person_id: 1
}
]
}]
localhost:4730/tasksでGETを実行すると、期待どおりに戻ります。
[
{
description: "Heyo!",
id: 1,
person_id: 1
}
]
さて、ここに私の問題の始まりがあります:
ペイロードを使用してlocalhost:4730/tasksにPOSTを実行すると、次のようになります。
{
description: "Another task",
person_id: 1
}
そして、localhost:4730/tasksで新たにGETを実行すると、期待どおりの結果が得られます。
[{
description: "Heyo!",
id: 1,
person_id: 1
},
{
description: "Another task",
id: 2,
person_id: 1
}]
ここで、localhost:4730/peopleで新たにGETを実行すると、person_id 1 (Kenny Powers) に割り当てられた 2 つのタスクが表示されることが期待されますが、残念ながら、関連付けは多側では登録されているようですが、関係の片側では登録されていないようです。 :
[{
name : "Kenny",
surname : "Powers",
age : 34,
male : true,
continent: "enum1",
data : null,
id : 1,
tasks : [
{
description: "Heyo!",
id: 1,
person_id: 1
}
]
}]
私が間違っている可能性のあるドキュメントから解決することはできません。誰かが同様の問題を経験しましたか?
ありがとう。