Skip to content
No description, website, or topics provided.
Branch: master
Clone or download
greenkeeper and jarcodallo Update objection to the latest version 🚀 (#12)
* fix(package): update objection to version 1.6.9

* chore(package): update lockfile package-lock.json
Latest commit dc4ebcd May 21, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test Initial release (#1) May 8, 2019
.editorconfig Initial release (#1) May 8, 2019
.gitignore Initial release (#1) May 8, 2019
.npmignore Initial release (#1) May 8, 2019
.nvmrc Initial release (#1) May 8, 2019
.travis.yml Remove node@6 and fix knexConfig (#3) May 8, 2019
CHANGELOG.md Release v0.1.2 (#7) May 8, 2019
LICENSE Initial commit May 8, 2019
README.md Fix README.md (#5) May 8, 2019
package-lock.json Update objection to the latest version 🚀 (#12) May 21, 2019
package.json
plugin.js

README.md

fastify-objectionjs

Greenkeeper badge Build Status

fastify-objectionjs is a plugin for the Fastify framework that provides integration with objectionjs ORM.

Supports Fastify versions ^2.0.0.

If you don't provide the knexConfig.client by yourself (see below), the plugin use the default configuration:

const defaultKnexConfig = {
  client: 'sqlite3',
  connection: {
    filename: './default.sqlite'
  }
}

Install

npm i fastify-objectionjs --save

Usage

Define the objectionjs model

// user.model.js
'use strict'

const { Model } = require('objection')
const schema = require('./schema')

class User extends Model {
  static get tableName () {
    return 'users'
  }

  static get jsonSchema () {
    return Base.generateJsonSchema({
      type: 'object',
      required: ['username', 'password'],
      properties: {
        id: { type: 'integer' },
        username: { type: 'string', minLength: 1, maxLength: 255 },
        password: { type: 'string', minLength: 1, maxLength: 255 },
        salt: { type: 'string', minLength: 1, maxLength: 255 },
        created_at: { type: 'string' },
        updated_at: { type: 'string' }
      }
    })
  }
}

module.exports = User

Add the knex config and objectionjs model to the project with register

const fastify = require('fastify')()
const User = require('./user.model.js')

fastify.register(require('fastify-objectionjs'), {
  knexConfig: {
    client: 'sqlite3',
    connection: {
      filename: './default.sqlite'
    }
  },
  models: [User]
})

const schemas = {
  response: {
    200: {
      type: 'object',
      properties: {
        id: { type: 'integer' },
        token: { type: 'string' }
      }
    },
    401: {
      type: 'object',
      properties: {
        message: { type: 'string' }
      }
    }
  },
  body: {
    type: 'object',
    properties: {
      username: { type: 'string', minLength: 1, maxLength: 255 },
      password: { type: 'string', minLength: 1, maxLength: 255 }
    },
    required: ['username', 'password']
  }
}

fastify.post('/login', { schema }, async function (request, reply) {
  const { username, password } = request.body

  const user = await fastify.objection.models.user
    .query()
    .findOne({ username })

  if (user && fastify.password.validate(user.password, user.salt, password)) {
    const token = fastify.jwt.sign(
      { sub: user.username },
      { expiresIn: '6h' }
    )

    reply.send({ id: user.id, token })
  } else {
    reply.status(401).send({ message: 'Invalid username or password' })
  }
})

fastify.listen(3000, err => {
  if (err) throw err
})

API

Options

fastify-objectionjs accepts the options object:

{
  knexConfig: {
    client: 'sqlite3',
    connection: {
      filename: './default.sqlite'
    }
  },
  models: [User]
}
  • knexConfig (Default: sqlite3 connection): can be set any knex valid configuration.
  • models (Default: undefined): a collection of objectionjs models.

License

MIT License

You can’t perform that action at this time.