Model
First: initial database connection
import { Model } from 'ryzens'
import knex from 'knex'
import knexConfig from 'path/to/knexfile.js'
Model.knex(knex(knexConfig))
Second: Model defination
import { Model } from 'ryzens'
import password from 'objection-password'
import {string} from 'yup'
const Password = password({
allowEmptyPassword: false,
passwordField: 'password',
rounds: 12
})
export class User extends Password(Model) {
static get tableName () { return 'users' }
static get vaildator () {
return {
username: string().min(1).max(100).required()
}
}
static get relations () {
return {
posts: this.hasMany(Post)
}
}
}
export class Post extends Model {
static get tableName () { return 'posts' }
static get timestamps () {
return ['created_at', 'updated_at']
}
static get validator () {
return {
title: string().min(3).max(256).required(),
contents: string().required()
}
}
static get relations () {
return {
user: this.belongsTo(User)
}
}
}
Router
import {router} from 'ryzens'
export const posts = router.restful(posts => {
posts.create()
posts.read.list()
posts.read.item()
posts.update()
posts.destroy()
})
Application
import { Application, middlewares } from 'ryzens'
import {router} from 'path/to/router'
export const app = new Application()
app.use(middlewares.basic())
app.use(middlewares.router(router))
app.listen(8000)
export const server = app.listen(8000)
Test
import { server } from 'path/to/app'
import { posts } from 'path/to/routers'
import casual from 'casual'
const { test } = global
test.restful(server, posts, (prepare, create, update, read, destroy) => {
let post
prepareEach(() => ({title: casual.title}), item => { post = item })
const id = () => post.id
create()
read.list()
read.item(id)
update(id, {title: 'updated'})
destroy(id)
})