#1 2012-11-13 00:02:05

sytis
Member
From: Milano
Registered: 2012-10-16
Posts: 11

one-to-many and REST POST Method

Hi,
I'm studying mORMot and Delphi: actualy I have some problem with Object relations and http REST.

Basically I'm trying to understand how to create a one-to-many relationship between two objects:

Type
  TSQLTableParents  = Class(TSQLRecord)
  public
    fDescription: RawUTF8;
  published
    property Description: RawUTF8 read fDescription write fDescription;
  End;

  TSQLTableChild = Class(TSQLRecord)
  public
    fChildDescription: RawUTF8;
    fParents: TSQLTableParents;
  published
    property ChildDescription: RawUTF8          read fChildDescription write fChildDescription;
    property Parents:          TSQLTableParents read fParents          write fParents;
  End;

With this Parents property it's possible to do something like this from a client via REST/http:

POST /ModelRoot/TableParents/1/TableChild -> {"ChildDescription":"This is a child"}

GET /ModelRoot/TableParents/1/TableChild  -> {"ID":13,"ID":51,...}

Thanks,
Mauro

Offline

#2 2012-11-13 07:15:12

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: one-to-many and REST POST Method

No, it is not possible as this.

You'll have to search for all the Childs with Parents=ParentID in the share WHERE clause.
There is no direct REST routing for such requests.

Do you need it for some AJAX properties?
I do not remember that such a REST routing is standard - is it?

Offline

#3 2012-11-13 11:59:02

sytis
Member
From: Milano
Registered: 2012-10-16
Posts: 11

Re: one-to-many and REST POST Method

> No, it is not possible as this.

OK!

>You'll have to search for all the Childs with Parents=ParentID in the share WHERE clause.
>There is no direct REST routing for such requests.

It's possible to have just a little example about this (MainDemo doesn't explain exactly this situation)?

Sorry, I don't understand! Parents=ParentID is on client or server?

When I request a Parent from the client:

  GET /ModelRoot/TableParents    ->    {"ID":1,"ID":2,...}

  GET /ModelRoot/TableParents/1    ->     {"ParentDescription":"This is a parent",{???}}

How mORMot expose multiple TableChild ID in the body of the response?

To add a new Child with a reference to the Parent what I need to do:

  POST /ModelRoot/TableChilds&ParentID=1    -> {"ChildDescription":"This is a child"}

  POST /ModelRoot/TableChilds        -> {"ParentID":1,"ChildDescription":"This is a child"}

Do you need it for some AJAX properties?

I'm just trying to simplyfing the client logic: as I don't understand the server side too! smile
I don't need a Delphi client, just remote apps (Javascript/PHP/Python/Ruby/etc.)  that talks with a mORMot server and viceversa.

> I do not remember that such a REST routing is standard - is it?

I've read about this in a lot of docs, this is an example:

REST API Design Rulebook - O'Reilly:
http://books.google.it/books?id=eABpzyT … &q&f=false

Ruby on Rails implement this.

But I just want to go in the mORMot way, if I'll find it! smile

Thanks for your help!
Mauro

Offline

#4 2012-11-14 15:57:18

sytis
Member
From: Milano
Registered: 2012-10-16
Posts: 11

Re: one-to-many and REST POST Method

if resource only REST methods are not allowed (/ModelRoot/TableName1/ID/TableName2/ID) I guessing that mORMot way is tables/methods mixed style (/ModelRoot/TableName1/ID/MethodTableName2), is it RESTful anyway?

Do you think mORMot could support resource only REST methods in a future version or do you belive it's useless?

Thanks,
Mauro

Offline

#5 2012-11-14 16:42:32

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: one-to-many and REST POST Method

I suspect I cannot fully understand what you are asking for...
What do you mean by "MethodTableName2" for instance?
Could you be a bit more explicit, please?

Offline

#6 2012-11-14 18:56:00

sytis
Member
From: Milano
Registered: 2012-10-16
Posts: 11

Re: one-to-many and REST POST Method

:) I beg your pardon for my english!

I try to simplify my request:

TSQLTableName1 = Class(TSQLRecord)
TSQLTableName2 = Class(TSQLRecord)
...
TMethodName1   = Class(TInterfacedObject,...)
TMethodName2   = Class(TInterfacedObject,...)
...

aServer.ServiceRegister(TMethodName2,...)

POST /ModelRoot/TableName1/ID/MethodName2 <- {"create a new TableName2 record with TableName1 ID reference"}

It's correct!? I'm trying to "simulate" a resource only REST URL.

Thanks,
Mauro

Offline

#7 2012-11-15 09:57:21

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: one-to-many and REST POST Method

There is no link between interface-based services and ORM classes, at REST URL routing level.
I suspect it will be confusing:
1. For interface-based services, just put the TSQLRecord instance as a parameter.
2. Which method of the interface is to be executed?

You have such a routing only for method-based services.

Offline

#8 2012-11-15 17:42:10

sytis
Member
From: Milano
Registered: 2012-10-16
Posts: 11

Re: one-to-many and REST POST Method

For method-based services do you mean ModelRoot/TableName/ID/MethodName? This is what I'm trying to do. right now...

My goal is a http only REST API server exposing resources to remote clients (iOS,Android,html/js,external web services) with all business logic residing on the server. Do you think that is feasible?

It would be possible to develop a resource only mORMot server (/ModelRoot/TableName/ID/AnotherTableName/ID...)?

Thanks,
Mauro

Offline

#9 2012-11-27 08:17:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: one-to-many and REST POST Method

sytis wrote:

For method-based services do you mean ModelRoot/TableName/ID/MethodName? This is what I'm trying to do. right now...

Yes, it can work like this.
See TSQLRestServerCallBackParams.Context.Table and TSQLRestServerCallBackParams.Context.ID and TSQLRestServerCallBackParams.URI.

sytis wrote:

My goal is a http only REST API server exposing resources to remote clients (iOS,Android,html/js,external web services) with all business logic residing on the server. Do you think that is feasible?

Of course it is.
Method-based services can do it directly.

sytis wrote:

It would be possible to develop a resource only mORMot server (/ModelRoot/TableName/ID/AnotherTableName/ID...)?

It would be possible, but it is not on the roadmap.
mORMot provides basic RESTful API, not such over-sophisticated URI routing.

Offline

Board footer

Powered by FluxBB