1.0.0 • Published 6 years ago

knex-json-schema v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

Knex Json schema

Nos permite crear o alterar schema en knex, apartir de un Json de configuración.

It allows us to create or alter schema in knex, from a configuration Json.

Instalación /Installation

npm install knex-json-schema --save

Ejemplo de uso / Usage example

const database = require('./00_database.json') // JSON OBJECT DESCRIBE DATABASE
const knexJsonSchema = require('knex-json-schema')

exports.up = async function(knex, Promise) {  
  await knexJsonSchema.upSchema(knex, database)  
};

exports.down = async function(knex, Promise) {
   await knexJsonSchema.downSchema(knex, database)  
};

Configuración JSON / Configure JSON

// CREATE TABLE 

[
  {
    "name":"users",
    "type": "table",
    "description": "User table",
    "timestamps":[true,true], //"timestamps":true/ "timestamps":false // default false
    "attriubutes":[{
      "name": "userId",
      "type": "uuid",
      "primary":true,
      "unique": true,
      "notNullable":true,
      },
      {
      "name": "email",
      "type": "string",
      "unique": true,
      "notNullable":true,
      },
      {
      "name": "googleId",
      "type": "string",
      "unique": true,
      "notNullable":true,
      },
      {
        "name": "roleId",
        "type": "uuid",
        "notNullable": true,
        "references":"role.roleId"
      }
    ]
  }
]


// ALTER TABLE
[
  {
    "name":"users",
    "type": "alter",
    "dropTimestamps":true / false // default false
    "attriubutes":[{
      "name": "surname",
      "type": "string",
      "notNullable":true,
      },
      {
      "name": "email",
      "action": "dropColumn"
      },
      {
        "name": "name",
        "action":"renameColumn",
        "to":"lastName"
      }
      
    ]
  }
]

OR ***********
 {
  "name": "users",
  "type":"alter"
  "raw" : `
    ALTER TABLE table_name
    ADD column_name datatype;
   ` 
 }



// CREATE VIEW

 {
  "name": "users",
  "type":"view"
  "raw" : `
    CREATE OR REPLACE VIEW ranking
      SELECT * from users;
   ` 
 }

Historial de versiones / History Versions

  • 1.0.0
    • Create Table, Create View, Basic Alter

Meta

Distribuido bajo la licencia MIT.

https://github.com/wcamaly/github-link

1.0.0

6 years ago