-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update(docs): Add examples and update package version
Two new example directories, basic and advanced, have been added for Adonis.js in the documentation section, along with their corresponding introductory adonis markdown files. The package.json file has been updated to reflect a minor version bump. Updates have also been made to the README and index.md file to include information on TypeScript support.
- Loading branch information
Showing
8 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# Adonis.js | ||
|
||
:button-link[Adonis.js Documentation]{icon="tabler:brand-adonis-js" href="https://adonisjs.com" blank} | ||
--- | ||
|
||
### Basic Example | ||
|
||
::code-group | ||
|
||
```ts [todos_controller.ts] | ||
import { HttpContext } from '@adonisjs/core/http' | ||
import cache from '#services/cache.service' | ||
import Todo from '#models/todo' | ||
|
||
export default class TodosController { | ||
/** | ||
* Return list of all todos | ||
*/ | ||
async index({ response }: HttpContext) { | ||
let cachedData; | ||
const fileName = 'todos' | ||
const key = 'index' | ||
|
||
try { | ||
cachedData = await cache.get({ fileName, key }) | ||
} catch (_) {} | ||
|
||
if (!cachedData) { | ||
try { | ||
cachedData = await Todo.all() | ||
await cache.set({ fileName, key, payload: cachedData }) | ||
} catch (e) { | ||
return response.badRequest({ error, message: 'Something went wrong while getting records' }) | ||
} | ||
} | ||
return response.ok({ data: cachedData }) | ||
} | ||
|
||
/** | ||
* Handle form submission to create a new todo | ||
*/ | ||
async store({ request, response }: HttpContext) { | ||
const { name } = request.only(['name']) | ||
|
||
try { | ||
await Todo.create({ | ||
name, | ||
completed: false, | ||
}) | ||
await cache.flushByRegex('todos') // Invalidates todos so that we get fresh results on next index() call | ||
return response.ok({ data: 'Successfully inserted a new record' }) | ||
} catch (error) { | ||
return response.badRequest({ error, message: 'Something went wrong while inserting a record' }) | ||
} | ||
} | ||
|
||
/** | ||
* Display a single todo by id. | ||
*/ | ||
async show({ request, response }: HttpContext) { | ||
const id = request.param('id') | ||
|
||
let cachedData; | ||
const fileName = 'todos-id' | ||
const key = id | ||
|
||
try { | ||
cachedData = await cache.get({ fileName, key }) | ||
} catch (_) {} | ||
|
||
if (!cachedData) { | ||
try { | ||
cachedData = await Todo.find(id) | ||
await cache.set({ fileName, key, payload: cachedData }) | ||
} catch (error) { | ||
return response.badRequest({ error, message: 'Something went wrong while getting a record' }) | ||
} | ||
} | ||
return response.ok({ data: cachedData }) | ||
} | ||
|
||
/** | ||
* Handle the form submission to update a specific todo by id | ||
*/ | ||
async update({ request }: HttpContext) { | ||
const id = request.param('id') | ||
const { name } = request.only(['name']) | ||
|
||
try { | ||
const todo = await Todo.findOrFail(id) | ||
todo.name = name | ||
|
||
await todo.save() | ||
await cache.flushByRegex('todos', id) // Invalidates single todo by id so that we get fresh results on next show() call | ||
return response.ok({ data: 'Successfully updated a record' }) | ||
} catch (error) { | ||
return response.badRequest({ error, message: 'Something went wrong while updating a record' }) | ||
} | ||
} | ||
} | ||
``` | ||
|
||
```ts [cache.service.ts] | ||
import { FileSysCache } from 'file-sys-cache' | ||
|
||
const cache = new FileSysCache({ | ||
basePath: './.file-sys-cache' | ||
}) | ||
|
||
export default cache | ||
``` | ||
|
||
:: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
title: 'Basic' | ||
icon: fluent:style-guide-20-regular |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Adonis.js | ||
|
||
:button-link[Adonis.js Documentation]{icon="tabler:brand-adonis-js" href="https://adonisjs.com" blank} | ||
--- | ||
|
||
### Advanced Example | ||
|
||
::alert{type="info"} | ||
Coming soon! | ||
:: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
title: 'Advanced' | ||
icon: fluent:style-guide-20-regular |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
title: 'Examples' | ||
icon: fluent:style-guide-20-regular |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters