Its also much cheaper to host your own solution on Amazon EC2 (or Lambda) compared to other costlier alternatives.Ĭontrary to popular thinking, its not very difficult to create a database agnostic backend such as the one represented in the above diagram. Rather than using firebase, if you develop your own implementation of your back-end, not only will it help you learn and become a better programmer, but also give you a flexible solution that you can scale and change as per your needs.
But in this tutorial, we will learn how to create such a backend ourselves using Python’s flask framework and sqlalchemy, a light-weight but powerful ORM library that can access ANY database using its flexible sql expression language. Popular third-party apps like Firebase essentially provide you this same thing - A REST based front to a database that could be accessed online using simple HTTP methods.
When your application’s userbase gets quite large and the client could vary from a laptop to an android device to an iOS device, it pays to keep the backend code separate and use the server only for making RESTful calls using HTTP methods that pertain to basic OLTP transactions: SELECT, INSERT, UPDATE and DELETE. items (): row = TestTable ( key = _key, val = _val ) session. insert (),[ try : for _key, _val in data. create_all () # get user table user_t = meta. reflect () email_t = Table ( 'email_addr', meta, Column ( 'id', Integer, primary_key = True ), Column ( 'email', String ), Column ( 'name', String )) meta. execute ( select_st ) for _row in res : print ( _row ) join() - Joined Two Tables via “JOIN” Statement ¶įrom sqlalchemy import create_engine from sqlalchemy import MetaData from sqlalchemy import Table from sqlalchemy import Column from sqlalchemy import Integer from sqlalchemy import String from sqlalchemy import select db_uri = 'sqlite:///db.sqlite' engine = create_engine ( db_uri ) meta = MetaData ( engine ). execute ( select_st ) for _row in res : print ( _row ) # combine with "ORDER_BY" select_st = select ().
execute ( select_st ) for _row in res : print ( _row ) # combine with "OR" select_st = select (). execute ( select_st ) for _row in res : print ( _row ) # or equal to select_st = table. tables # select * from 'user' select_st = select (). in_ ()) insert() - Create an “INSERT” Statement ¶įrom sqlalchemy import create_engine from sqlalchemy import MetaData from sqlalchemy import Table from sqlalchemy import select from sqlalchemy import or_ db_uri = 'sqlite:///db.sqlite' engine = create_engine ( db_uri ) conn = engine. l_name + "some name" ) # in expression print ( table. id + 5 ) # or means "string concatenation" print ( table. is_ ( None )) # + means "addition" print ( table. l_name = None ) # Equal to print ( table.
id < 2 )) # compare to None produce IS NULL print ( table. id > 3 )) # or expression print (( table. f_name != 'ed' )) # comparison operator print ( repr ( table.
l_name = 'ed' )) # exhbit sql expression print ( str ( table. # Think Column as "ColumnElement" # Implement via overwrite special function from sqlalchemy import MetaData from sqlalchemy import Table from sqlalchemy import Column from sqlalchemy import Integer, String from sqlalchemy import or_ meta = MetaData () table = Table ( 'example', meta, Column ( 'id', Integer, primary_key = True ), Column ( 'l_name', String ), Column ( 'f_name', String )) # sql expression binary object print ( repr ( table. Join() - Joined Two Tables via “JOIN” Statementįastest Bulk Insert in PostgreSQL via “COPY” Statementīulk PostgreSQL Insert and Return Inserted IDsĬreate tables with dynamic columns (Table)Ĭannot use the object after close the session Print Create Table Statement with Indexes (SQL DDL) Reflection - Loading Table from Existing Database