#1 Today 11:14:48

gcarreno
Member
Registered: Today
Posts: 6

Questions from a noob trying to do a RESTful API

Hey Y'All,

NOTE: I'm duplicating this thread from the Lazarus forums because, maybe, I can get a quicker answer here big_smile
https://forum.lazarus.freepascal.org/in … 476.0.html

While embarking on the task of upgrading the backend side of OPM, I've decided I'm going to FINALLY start using mORMot 2.0!!

I've started my experimentation and I got to the point where I'm serving some content from a temporary SQLite database.
The idea is to use PostgreSQL in production.

I'm a bit surprised by some of the conventions that mORMot 2.0 imposes. I was counting on it being opinionated, like any other framework, but
nonetheless, some of the opinions are rather strange from someone that is used to CakePHP, that takes the same opinionated ideas from Ruby on Rails.

All this long winded blah-blah to get to my immediate questions:

1. How do I force the table names for each model?
    I much prefer that both the tables and the fields be in lower case. I also like my models to be in the singular, but my tables to be in the plural.
    Yeah, I know, I'm a bloody whiner, but I'm old and I have my own conventions tongue

2. How do I change the case for the JSON that is served?
    I'm rather spoiled by the fact that in Go I can define the JSON name for each field, and I'm wondering if I can do the same with mORMot 2.0

3. Is there a way to do database migrations outside the main application?
    I ask this because I got, kinda, spoiled from using CakePHP's migrations. With these you can do up/down quite easily, and all those migrations
    are just another source file you add to the source control. On top of that, they are timestamped and some info is stored in the database itself, to
    track which ones have been applied.
    It would be rather nice to have something of this nature to use.

I think this is enough for now. When I have some other doubts, I'll keep adding to this thread.

Cheers,
Gus

Offline

#2 Today 12:55:52

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,240
Website

Re: Questions from a noob trying to do a RESTful API

It is difficult to understand what you really need.

Are you willing to use interface-based services?
Then, you could use existing SQL easily, returning the JSON as you require.
The use of mORMot ORM is not mandatory at all, especially if it does not match your existing DB schema.

Offline

#3 Today 13:12:14

gcarreno
Member
Registered: Today
Posts: 6

Re: Questions from a noob trying to do a RESTful API

Hey Arnaud,

ab wrote:

It is difficult to understand what you really need.

Yeah, I guess it is!!
I'm throwing at you stuff from CakePHP and Go, which is a bit confusing if I'm just mentioning it because I'm a whiner wink big_smile

ab wrote:

Are you willing to use interface-based services?
Then, you could use existing SQL easily, returning the JSON as you require.
The use of mORMot ORM is not mandatory at all, especially if it does not match your existing DB schema.

That's a bit too advanced at the moment for me. I quite like that mORMmot 2.0 does it's own database schema, and I would like it to continue like that!! I HATE writing SQL!!!
Like I said on the message, I'm coming into mORMmot 2.0 with a ton of bad habits that I picked up from Ruby on Rails based frameworks like CakePHP.
I would like to be able to use those bad habits, but if it's too much fussing about, I'll just bite the bullet and conform to the new norms that are the mORMmot 2.0 conventions.

I'm not willing to mess with the auto-magic that comes with mORMmot 2.0. That, in my humble opinion, would be contrary to a speedy code production.
I'll have to live with these new norms. In the end, it's not that much of a hassle!!

What about the question about database migration?
Anything, remotely, resembling the CakePHP ones?
Or is it done differently on this side of the fence?

Many thanks for your attention and time, Arnaud!!
It's IMMENSELY appreciated !!

Cheers,
Gus

Offline

#4 Today 13:41:24

gcarreno
Member
Registered: Today
Posts: 6

Re: Questions from a noob trying to do a RESTful API

Hey Arnaud,

Complete forgot to address this:

ab wrote:

The use of mORMot ORM is not mandatory at all, especially if it does not match your existing DB schema.

To me it doesn't make sense to embark  on creating a completely new backend for OPM, if it's not done in Object Pascal.
And the biggest boy in terms of Object Pascal and RESTful is mORMmot 2.0!!!

So, it's pretty much a no brainer big_smile

Even the frontend is being done with fp-web as the core of it.
I'm also gonna use HTMX and possibly Alpine.js. Welp, not me, but the friend that is taking part of the frontend. I'm backend material wink

Cheers,
Gus

Offline

#5 Today 13:43:19

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,240
Website

Re: Questions from a noob trying to do a RESTful API

Some remarks:

If your database starts from scratch, then you can use mORMot ORM.
Then the tables or columns names could be "mapped" using the external ORM feature.
Switching from one DB to another is just a matter of initialization at runtime.

A good practice, when using interface-based-services, is to use:
1) a "Clean" architecture with a application-level API: do not leak your domain objects
2) define some DTOs for this app API - it is easy to use records with TRttiMap from mormot.core.rtti

Offline

#6 Today 13:56:13

gcarreno
Member
Registered: Today
Posts: 6

Re: Questions from a noob trying to do a RESTful API

Hey Arnaud,

ab wrote:

If your database starts from scratch, then you can use mORMot ORM.
Then the tables or columns names could be "mapped" using the external ORM feature.
Switching from one DB to another is just a matter of initialization at runtime.

Yes, indeed I'm starting from scratch!!
And I want mORMmot to take care of all the SQL as necessary.
I want to only do ANY SQL at all, when there's an impossibility of doing it via mORMmot!!

ab wrote:

A good practice, when using interface-based-services, is to use:
1) a "Clean" architecture with a application-level API: do not leak your domain objects
2) define some DTOs for this app API - it is easy to use records with TRttiMap from mormot.core.rtti

Hummm... I'm guessing that I'll probably have to plunge into this at some time in the future, once the entire things gets a level of complexity that the basics can't handle.
At the moment this is waaayyy above my head.
Nonetheless, I'm glad to have some tips on the subject. Thank you so very much Arnaud!!

My intentions are to create your run of the mill RESTful API based on CRUD.
The website/frontend will consume it's data from the above API, and will, serve it as HTML.
In the future, if anyone wants to create a mobile app or anything that is not the main site, they can always consume the RESTful API.
This is basically what the big boys are doing, so I'm pinching their ideas wink

Cheers,
Gus

Offline

#7 Today 17:46:51

gcarreno
Member
Registered: Today
Posts: 6

Re: Questions from a noob trying to do a RESTful API

Hey Arnaud,

I've left the migrations question on the back burner of my brain and I guess I sussed it out...

In mORMmot, every time we launch a new version of the executable, it will migrate the database to reflect the current state of the TOrm* classes.
This means it will add, update, and hopefully remove, fields and field types according to the TOrm* class current fields.
If this is true, it will at least fill in my requirements of having pseudo migrations that will go up/down by themselves and are tracked via the version control.

The only thing I'm unsure is if I remove a TOrm* class all together, will it DROP that table on the database?
I guess that dropping it by default may be undesirable, due to accidental loss of data... right... something to ponder about big_smile
I started with the TAuth* tables when I had security on. Then I switch the security off, and the tables remained... I guess that answers a bit of my question...
So, yeah, dropping tables is always a no-no, at least when it's done automagically!!

I think this is kinda settled, now.
And, if I'm not mistaken, that takes care of all my initial questions!!

I'm gonna have some questions about CLI tools using the bare minimum to access the database and do cronjobs in the future, maybe.
I guess I can stop being a lazy bastard and bang my head against the wall for a change. Less answering dumb questions on your side wink big_smile

Cheers,
Gus

Offline

#8 Today 19:44:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,240
Website

Re: Questions from a noob trying to do a RESTful API

Adding fields is supported.
Update the name or remove a field is not.
If you don't put an TOrm class to the TOrmModel, it won't indeed drop the table in the database.

Note that if you define the database, also consider using variant fields, and store JSON in it using TDocVariantData.
Consider reading the "aggregate" pattern in the documentation: it fits well the mORMot ORM patterns.

All this is documented in https://synopse.info/files/html/Synopse … 01.18.html

Offline

#9 Today 20:08:16

gcarreno
Member
Registered: Today
Posts: 6

Re: Questions from a noob trying to do a RESTful API

Hey Arnaud,

ab wrote:

Adding fields is supported.
Update the name or remove a field is not.
If you don't put an TOrm class to the TOrmModel, it won't indeed drop the table in the database.

Good to know !!!
I'm now a bit sad to loose the database migration freedom from CakePHP, but it is what it is and I can't keep whining about it big_smile

Hummm... had a brain fart:
There are 2 directions on how an ORM app works:
1. The code adapts to the SCHEMA.
2. The SCHEMA adapts to the code.

Since PHP is a dynamic language, adapting the code to the SCHEMA is rather easy, so they use that direction.
But... and here's the but: In compiled land, we cannot do that, so we have to do the opposite, the SCHEMA adapts to the code.
Which means that I've been whining about something that is not even possible, or it's rather complicated, to do with the compiled approach!! DOH !!!!!!

OK, NOW it makes more sense!!!

ab wrote:

Note that if you define the database, also consider using variant fields, and store JSON in it using TDocVariantData.
Consider reading the "aggregate" pattern in the documentation: it fits well the mORMot ORM patterns.

All this is documented in https://synopse.info/files/html/Synopse … 01.18.html

OK, at this present moment, this kinda goes over my head. Nonetheless, I promise to read the relevant parts of the docs about the aggregate pattern!!
I'm also quite tired right now, so I'll leave it for after I've had some rest!!

For the time being I've been following the code suggested in my session with ChatGPT and some small bits from the ex/MVC example.
Between the two, I was able to get where I am now.
Alas, the MVC example produces HTML pages. My WEB API will only produce JSON...
Still, I need to dig deeper into that example because there might be some gems that I'm missing!!

Again, cannot be said enough: Many thanks for dedicating some of your precious time entertaining a complete noob in mORMmot!!!
I really appreciate every moment of it !!

Cheers,
Gus

Offline

Board footer

Powered by FluxBB