ocsigen / ocsigen-start

Ocsigen-start: Higher-level library to develop Web and mobile applications with users, (pre)registration, notifications, etc.
Other
74 stars 32 forks source link

Userfriendly database #367

Open tobiasBora opened 7 years ago

tobiasBora commented 7 years ago

Hello,

It would be great to have better ways to deal with databases. Here are a few things that would be very nice to have:

Everything listed above is already present in Django (python web framework). You can look at how models are used in django, but basically it's very simple: just create a class whose attributes describes the SQL database. This provides all the getters/setters to create, modify, filter, and add an object in the database. And just connect to the /admin/ part of the website, and you will be able to edit all the objects from you browser.

Here is an example:

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

then update the database using:

python manage.py migrate

and in the python code, deal with objects with something like

my_object=Person.objects.create(first_name="my_name")
myobject.save()
all_entries = Person.objects.all()
nb_entries = len(all_entries)
first_object_name = all_entries[0].first_name
all_dupont = all_entries.filter(last_name__startswith='Dupont')

You can do much more things (like describing how the objects would appear in the admin panel, you can specify a lists of values for the type fields...). You can read more example here: https://docs.djangoproject.com/fr/1.10/topics/db/queries/. Having such tools in ocaml would make it much easier to use.

Thank you.

vasilisp commented 7 years ago

It would be very nice indeed.

Note that OCaml is typed, which poses challenges. Django et al. work in an untyped setting and exploit runtime information about the structure of the data, which we can not do. Expressing complex database queries in OCaml and persuading the OCaml type checker about the type of the output (as your first bullet suggests) is notoriously difficult. It can be solved by adding preprocessing into the mix (like PGOcaml and Macaque do), but then we are effectively talking about non-OCaml syntax.

Still, there is lot we can do within the OCaml type system. For example, displaying a table and modifying one entry at a time (somewhat covering bullet 4) based on a GADT description of its schema is entirely feasible.

Contributions towards this would be more than welcome :).

Drup commented 7 years ago

Honestly, this has pretty much nothing to do with ocsigen-start, except that we would like to use it. :)

Non exhaustive previous work on the question:

Type embeddings:

Syntax-based:

And that's only in OCaml, There is also a significant Haskell and Rust literature (notably, http://diesel.rs/). A good amount of if is equally unsatisfying.

We just don't have a good solution for that in OCaml at the moment and the tasks is hard enough that nobody figured it out properly in the last few years. I don't think inventing something ad-hoc just for eliom is a good idea.

vasilisp commented 7 years ago

Honestly, this has pretty much nothing to do with ocsigen-start, except that we would like to use it. :)

@Drup, there is a user interface aspect that does have to do with Ocsigen Start (imagine a potential admin panel for managing tables). But I agree with you with respect to the core of the problem :).