Instantly share code, notes, and snippets.

View hooks.js
const { hooks } = require('@adonisjs/ignitor')
hooks.after.httpServer(function () {
const Server = use('Server')
const httpInstance = Server.getInstance()
// do whatever you like
})
View setup.js
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) {
View .zshrc
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
}
View get-cat.js
// Import category model
const Category = use('App/Models/Category')
// Get all categories
const categories = await Category.all()
View models.js
const Model = use('Model')
class User extends Model {
posts () {
return this.hasMany('App/Models/Post')
}
}
class Post extends Model {
author () {
View models.js
const Model = use('Model')
class User extends Model {
}
class Post extends Model {
}
class Category extends Model {
}
View routes.js
// 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
View routes.js
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' }
View routes.js
// 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')
View routes.js
const Database = use('Database')