Skip to content

Instantly share code, notes, and snippets.

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