Socket
Socket
Sign inDemoInstall

ryzen

Package Overview
Dependencies
505
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ryzen

First: initial database connection ```js import { Model } from 'ryzens' import knex from 'knex' import knexConfig from 'path/to/knexfile.js' Model.knex(knex(knexConfig)) ``` Second: Model defination ```js import { Model } from 'ryzens' import password fro


Version published
Maintainers
1
Created

Readme

Source

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 false
     * don't auto update date column
     */
    /**
     * return true
     * equals to return ['created_at', 'updated_at']
     */
    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() is shorthand
  posts.read.list()
  posts.read.item()
  posts.update()
  posts.destroy()
})

Application

import { Application, middlewares } from 'ryzens'
import {router} from 'path/to/router'
// Application inherits from Koa, so app is an instance of Koa
export const app = new Application() 
// regular koa middleware
// app.use(middleware)
app.use(middlewares.basic()) // bodyParser, cors, jsonError & logger
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 }) // equals to beforeAll
  const id = () => post.id
  create()
  read.list()
  read.item(id)
  update(id, {title: 'updated'})
  destroy(id)
})

FAQs

Last updated on 13 Jul 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc