728x90
반응형


  • Admin 모델을 생성한다.
    $ adonis make:model Admin

    xyz/app/Models/Admin.js 파일이 생성된다.

  • 마이그레이션 생성하기
    $ adonis make:migration admins
    create: xyz/database\migrations\1507612187061_admins_schema.js
  • xyz/database\migrations\1507612187061_admins_schema.js 파일을 열어서 아래처럼 수정한다.
    'use strict'
     
    const Schema = use('Schema')
     
    class AdminsSchema extends Schema {
      up () {
        this.create('admins', (table) => {
          table.increments()
          table.string('userid').unique()
          table.string('password').notNullable()
          table.string('nick').unique()
        })
      }
     
      down () {
        this.drop('admins')
      }
    }
     
    module.exports = AdminsSchema
  • 마이그레이션을 실행하여 SQLite DB파일과 admin 테이블을 생성한다.
    $ cd xyz
    $ adonis migration:run

    이제, xyz/database/development.sqlite 파일이 생성되었다.

  • 다음과 같이 테이블이 생성된 것도 볼 수 있다.
    xyz\database> sqlite3 development.sqlite
    SQLite version 3.15.0 2016-10-14 10:20:30
    Enter ".help" for usage hints.
    sqlite> .schema
    CREATE TABLE "adonis_schema" ("id" integer not null primary key autoincrement, "name" varchar(255), "batch" integer, "migration_time" datetime default CURRENT_TIMESTAMP);
    CREATE TABLE "users" ("id" integer not null primary key autoincrement, "username" varchar(80) not null, "email" varchar(254) not null, "password" varchar(60) not null, "created_at" datetime, "updated_at" datetime);
    CREATE UNIQUE INDEX "users_username_unique" on "users" ("username");
    CREATE UNIQUE INDEX "users_email_unique" on "users" ("email");
    CREATE TABLE "tokens" ("id" integer not null primary key autoincrement, "user_id" integer, "token" varchar(40) not null, "is_revoked" boolean default '0', "created_at" datetime, "updated_at" datetime, foreign key("user_id") references "users"("id"));
    CREATE UNIQUE INDEX "tokens_token_unique" on "tokens" ("token");
    CREATE TABLE "admins" ("id" integer not null primary key autoincrement, "userid" varchar(255), "password" varchar(255) not null, "nick" varchar(255));
    CREATE UNIQUE INDEX "admins_userid_unique" on "admins" ("userid");
    CREATE UNIQUE INDEX "admins_nick_unique" on "admins" ("nick");
    sqlite> insert into admins(userid, password, nick) values('testid1', 'passwd1', 'nick1');
    sqlite> insert into admins(userid, password, nick) values('testid2', 'passwd2', 'nick2');
    sqlite> insert into admins(userid, password, nick) values('testid3', 'passwd3', 'nick3');
    sqlite> insert into admins(userid, password, nick) values('testid4', 'passwd4', 'nick4');
    sqlite> insert into admins(userid, password, nick) values('testid5', 'passwd5', 'nick5');
    sqlite> select * from admins;
    1|testid1|passwd1|nick1
    2|testid2|passwd2|nick2
    3|testid3|passwd3|nick3
    4|testid4|passwd4|nick4
    5|testid5|passwd5|nick5
    sqlite> .quit


728x90
반응형

+ Recent posts