I want to create RESTful webservice that uses database tables as resource entities.
PUT myservice/table/new creates a new table in database
DELETE myservice/table/1 deletes a table in database and so on...
How do I design a model for this? usually the models are created for tables in which resource entities reside but my resources are not present in any table but the database itself!
Is that possible or am I not making any sense? Please help me!
Thanks in Advance.
Yes, it's possible, because REST relies on a very abstract and flexible concept of resources. As long as the interface obeys the uniform interface (standard methods, using URI's, etc) you can do anything you like behind the scenes. One of the best reasons to make an HTTP interface to something is to hide ugly implementation details and present just such a uniform interface.
Exactly how you do that in your language or environment of choice is a far more detailed question, but the basics would be something like this CherryPy pseudo-code:
class Table:
exposed = True
def __init__(self, tablename):
self.tablename = tablename
def PUT(self):
fields = parse_fields(cherrypy.request.body.read())
db.execute("CREATE TABLE %s (%s)" % (self.tablename, fields))
def DELETE(self):
db.execute("DROP TABLE %s" % self.tablename)
class Tables:
def _cp_dispatch(self, vpath):
return Table(vpath.pop(0))
You'll have to wire up the 'db' connection logic yourself, and figure out how much control the client should have over the field definitions (and what media type is best to communicate that, and how to parse that media type into valid SQL for the CREATE TABLE statement). You'll also want much better security than "DROP TABLE {user input}" ;) But hopefully this gets you on the right track.