Package: pg4nosql
A simple psycopg2 based wrapper for nosql like database interaction with
python.Why another wrapper?The wrapper was developed to work with JSON postgres storage like a real
NoSQL DB (e.g. MongoDB). After a long research with google there was no
library found which helps to work with JSON and PostgreSQL so I decided
to develop one.The strength of the wrapper is that you still can have multiple
relational colums in your table.InstallationUsingPython Package Index(PIP)Just run the command:pip install pg4nosqlDuring alpha stage the api will change with each build. So try to stay
with one version if you want to use it.The hacky waydownloador
clone this repositoryrun the commandpython setup.py installChangelogVersion0.4.2fixed python 3.3 iterator returnVersion0.4.1Query from viewfixed some bugsVersion0.4.0adds join query statementsadds update methodadds save method on result itemVersion0.3.7adds the support for non-json database tablesVersion0.3.6id datatype can be set on table creationVersion0.3.3project cleanupVersion0.3.1auto-commit for very operation as defaultsave does not affect saving object anymorebug fixesVersion0.2.4a lot of bug fixesVersion0.2.0added port argumentreplaced dictionary argument with **keyword syntaxswitched result.relational with result.jsonadd bracket syntax to get database or tablerenamed table.get_or_create to match codestyleExampleThese examples show the funcionality of the wrapper. There are some
functions which are not covered by the examples (like removing of a
table) but the importent ones are explained.Dataschema CreationTo create the dataschema you can use normal database tools if you want.
A document table has two fields:idwith data typeserialjsonwith data typeJSONwhich represents the documentBut pg4nosql also provides methods to create your database schema on the
fly. This is useful to create tables and databases software controlled.This example shows how to create a database and their tables. The cities
table is special because it also contains relational data like a normal
table would:# create pg4nosql clientpg4nosql=PostgresNoSQLClient(host='localhost')# create demo databasedemo_db=pg4nosql['demo']# create document only tableusers=demo_db['users']# create document & relational tablecities=demo_db.get_or_create_table('cities',size='real NOT NULL')Row Identifier TypeBy default theidrow type isSERIALbut in some cases it is
necessary to define the type yourself. This is possible with therow_identifier_typeargument.# create document table with bigserialbig_users=demo_db.create_table('big_users',row_identifier_type='BIGSERIAL')Insert DataTo insert data into the table you just hand over a dictionary or an
object which is json serializable. If there are relational columns
defined you can set those by the table name as keyword and the value:# store data into users tableusers.put({'name':'Florian','age':24})users.put({'name':'Markus','age':24})users.put({'name':'Sara','age':22})users.put({'name':'Thomas','age':25})# store data into cities tablecities.put({'name':'Zurich'},size=87.88)cities.put({'name':'Berlin'},size=891.8)cities.put({'name':'Bern'},size=51.6)cities.put({'name':'London'},size=1572)If you workwithout json documents, there is just a normalinsertmethod to store new records into a table.# store data into cities tableusers.insert(age=25,name="Florian")Lazy CommitIf you want to store or save multiple entries you can set theauto_commitargument toFalseand commit it yourself.# store data with lazy commitforiinrange(0,255):users.put({'name':'Test','age':i},auto_commit=False)# lazy commit datausers.commit()Query DataTo get your data back you can run a query over it. This works like
normal SQL WHERE queries. ForJSONdata you have to use thejsoncolumn:# query all users which are 24 years oldusers_24=users.query("json->>'age'='24'")And here the result of the user query:["{'json': {u'age': 24, u'name': u'Florian'}, 'id': 1}","{'json': {u'age': 24, u'name': u'Markus'}, 'id': 2}"]You can also combine relational and JSON queries together like this:# query all cities which start with be and are bigger than 100 kmbig_ber_cities=cities.query("json->>'name' LIKE 'Ber%'""AND size > 100")Here the result of this query:["{'json': {u'name': u'Berlin'}, 'id': 2, 'size': 891.8}"]Query with JoinIt is also possible to create simplejoinedqueries with the functionquery_join. Consider a datamodel with anusertable and anaddresstable. This two tables are connected through aforeign keyfk_user.The called table get’s the identifieraand the joined table the
identifierb.# get all users with their addressusers=user.query_join('address','a.id = b.fk_user')Query from ViewTo query data from a view you have to get the view from the database and
then it has the same query method like a table.# get viewself.user_view=self.database.get_view('user_view')# query datarecords=self.user_view.query()Query Data AccessToaccesstheJSONfields of the result there is an attribute
calledjson:# get first city of the result arrayfirst_city=big_ber_cities[0]# read JSON attributecity_name=first_city.json['name']Toaccesstherelationalfields of the result you have to usesquare brackets([]) on the result:# read relational attributecity_size=first_city['size']There is also a default field calledidwhich contains the default
row identifier for easy access:# get id of rowcity_id=first_city.idUpdate DataWith those access methods you can also write into the result and change
the values of the fields. To save it just callsave(obj)on the
table object.# change florian's ageflorian=users_24[0]florian.json['age']=25users.save(florian)The same works also with therelationalfields:# make zurich a bit biggerzurich=cities.query_one("data->>'name'='Zurich'")zurich['size']=90cities.save(zurich)With the release0.4.0it is also possible to save the database
object directly:# make zurich a bit biggerzurich=cities.query_one("data->>'name'='Zurich'")zurich['size']=90zurich.save()Without json documents, there is just a normalupdatemethod to
update new records into a table.# store data into cities tableflorian=users_24[0]florian['name']='Markus'users.update(florian)Direct ExecutionIt is also possible to directly execute sql statements as you are used
to. The execute function is declared on the database object and on the
table object.# run simple sql querymy_data=demo_db.execute('SELECT * FROM cities')Close ConnectionFinally don’t forget to close the connection to the database.# close dbdemo_db.close()AboutThe wrapper has been written for a science project and is still an early
beta version! Idea and implementation by Florian (cansik)MIT License Copyright (c) 2015