Skip to content

Instantly share code, notes, and snippets.

@claretnnamocha
Last active February 29, 2020 22:46
Show Gist options
  • Save claretnnamocha/95e27bb3cf05b43246fad07b3dfa410d to your computer and use it in GitHub Desktop.
Save claretnnamocha/95e27bb3cf05b43246fad07b3dfa410d to your computer and use it in GitHub Desktop.
import alphaorm.AlphaORM as DB
# Create new Record
product = DB.create('product')
product.name = 'Running shoes'
product.price = 5000
DB.store(product)
# Create new Record with Foreign key
author = DB.create('author')
author.name = 'Chimamanda Adichie'
book = DB.create('book')
book.title = 'Purple Hibiscus'
book.author = author
DB.store(book)
# Retrieve only one row from table
book = DB.find('book','id = :bid', { 'bid' : 1 })
print(f'{book.title} by {book.author.name}')
# Retrieve all records in a table
books = DB.getAll('book')
for book in books:
print(f'{book.title} by {book.author.name}')
# Retrieve all records with filter
book = DB.findAll('book','pub_year > :year', { 'year' : 1999 })
for book in books:
print(f'{book.title} by {book.author.name}')
# Update existing record in database
product = DB.find('product', 'id = :pid', { 'pid': 1 })
product.price = 500
DB.store(product)
# Update existing record and foreign key reference in database
book = DB.find('book','id = :bid', { 'bid' : 1 })
book.author.name = 'New author' # Changing data of foreign key table
book.pub_year= '2003'
DB.store(book) # Updates the record
# Delete a single record from database
book = DB.find('book','id = :bid', { 'bid' : 1 })
DB.drop(book)
# Delete a single record from database
DB.dropAll('book')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment