apin
fast lightweight rest-api builder
Apin Scripts
Add these to your package.json
file.
"scripts": {
"dev": "nodemon core/index.js",
"dbd": "knex migrate:rollback --knexfile core/db/knexfile.js",
"dbu": "knex migrate:latest --knexfile core/db/knexfile.js",
"create_table": "knex migrate:make create_${npm_config_name} --knexfile core/db/knexfile.js",
"create_seed": "knex seed:make ${npm_config_name} --knexfile core/db/knexfile.js",
"seed": "knex seed:run --knexfile core/db/knexfile.js"
},
Knexfile
module.exports = {
development: {
client: 'pg',
connection: {
host: 'some-host',
port: 'some-port',
user: 'some-username',
password: 'some-password',
database: 'some-database'
},
migrations: {
tableName: 'knex_migrations',
directory: 'migrations'
},
seeds: {
directory: 'seeds'
},
}
}
Apin Scripts
npm run create_table --name=users &&
npm run create_seed --name=01-users
Examples Migration
const tableName = 'users'
const schema = (table) => {
table.increments()
table.string('public_id').notNullable().unique()
table.string('username').notNullable().unique()
table.string('email').notNullable().unique()
table.string('password').notNullable()
table.boolean('is_verified').defaultTo(false)
table.boolean('is_suspended').defaultTo(false)
table.boolean('is_deactivated').defaultTo(false)
table.timestamps(true, true)
}
exports.up = (knex) => knex.schema.createTable(tableName, schema)
exports.down = (knex) => knex.schema.dropTable(tableName)
Index File
global.apin = require('apin')()
const app = new (require('koa'))
const server = require('http').createServer(app.callback())
const bodyparser = require('koa-bodyparser')
const cors = require('kcors')
apin.io = require('socket.io')(server, {cors: {origin: '*'}})
app.use(apin.plugins.errorHandler)
app.use(bodyparser())
app.use(cors())
app.use(apin.router.routes())
app.use(apin.plugins.notFoundHandler)
server.listen(apin.config.server.port, () => {
const host = require('url').format(apin.config.server)
print('running at: %s', host)
})
Config File
module.exports = {
server: {
protocol: 'http',
hostname: 'localhost',
port:7000
},
jwt:{
access:{
key: 'some-scecret',
options:{
expiresIn: '10m',
}
},
refresh:{
key: 'some-other-scecret',
options:{
expiresIn: '30d',
}
},
}
}