Skip to content

Instantly share code, notes, and snippets.

View thetutlage's full-sized avatar
🏠
Working from home

Harminder Virk thetutlage

🏠
Working from home
View GitHub Profile
const { hooks } = require('@adonisjs/ignitor')
hooks.after.httpServer(function () {
const Server = use('Server')
const httpInstance = Server.getInstance()
// do whatever you like
})
@thetutlage
thetutlage / setup.js
Created July 10, 2018 09:18
Adonis Lucid Setup without Adonis framework
const { registrar, ioc } = require('@adonisjs/fold')
const _ = require('lodash')
/**
* Very bare bones implementation of the config provider.
* If you run into issues, then make sure to check the
* implementation in adonis-framework repo
*/
class Config {
constructor (config) {
@thetutlage
thetutlage / .zshrc
Created June 30, 2018 05:31
Rename file extensions from a shell script
function rext () {
if [ $# -lt 3 ]; then
echo 1>&2 "$0 needs 3 arguments as DIR SOURCE_EXT DEST_EXT"
else
for file in "$1"/*.$2; do
mv "$file" "${file%.$2}.$3"
done
fi
}
@thetutlage
thetutlage / get-cat.js
Created June 27, 2018 10:16
Get categories
// Import category model
const Category = use('App/Models/Category')
// Get all categories
const categories = await Category.all()
@thetutlage
thetutlage / models.js
Created June 27, 2018 10:11
Lucid models relationships
const Model = use('Model')
class User extends Model {
posts () {
return this.hasMany('App/Models/Post')
}
}
class Post extends Model {
author () {
@thetutlage
thetutlage / models.js
Created June 27, 2018 09:57
Adonis Lucid entities
const Model = use('Model')
class User extends Model {
}
class Post extends Model {
}
class Category extends Model {
}
@thetutlage
thetutlage / routes.js
Created June 27, 2018 09:43
AdonisJs blog queries via query builder
// Display categories in the blog header
const categories = await Database.table('categories').select('*')
// Fetch posts for a category where category_id = 1
const posts = await Database
.table('posts')
.innerJoin('category_posts', 'posts.id', 'category_posts.post_id')
.where('category_posts.category_id', categoryId)
// Select author for a given post
@thetutlage
thetutlage / routes.js
Created June 27, 2018 09:00
Adonis database insert queries
await Database
.table('users')
.insert({ username: 'virk', role: 'admin', password: 'secret' })
// Bulk inserts
await Database
.table('users')
.insert([
{ username: 'virk', role: 'admin', password: 'secret' },
{ username: 'nikk', role: 'guest', password: 'secret' }
@thetutlage
thetutlage / routes.js
Created June 27, 2018 08:56
Adonis Database query builder select queries
// All users
await Database.from('users').select('*')
// Get admin users
await Database.from('users').where('role', 'admin')
// Inactive users
await Database
.from('users')
.where('status', 'inactive')
@thetutlage
thetutlage / routes.js
Created June 27, 2018 08:53
Import Adonis Database provider
const Database = use('Database')