Skip to content
This repository was archived by the owner on May 15, 2019. It is now read-only.

Adding new database table

Paul C edited this page Jul 17, 2018 · 1 revision

Assuming we want to add a table Cats, which has columns name and color with auto-increment id

  1. Define DB interface in spec/db.ts, which will be used to info typescript of database query's type
export interface DBCats {
  id: number
  name: string
  color: string
}
  1. Create a migration file. In terminal under /server, enter commands:
$> knex migrate:create cats
  1. Edit migration file to specify the column details of cats table, which is in /server/src/db/<datetime>_cats.ts
import * as Knex from 'knex'

exports.up = async function(knex: Knex): Promise<any> {
  return knex.schema.createTable('community_managers', table => {
    table.increments('id').primary()
    table.string('name').notNullable()
    table.string('color')
  })
}

exports.down = async function(knex: Knex): Promise<any> {
  return knex.schema.dropTable('community_managers')
}
  1. Apply migration file to the dev database
$> knex migrate:latest --env development

Clone this wiki locally