yamaform

2.7.2 • Public • Published

Yamaform

Install

npm i yamaform --save

Usage

A usage example can be found at https://github.com/ricardo93borges/yamaform-example

Instantiate

var Yamaform = require('yamaform')
 
let databaseConfig = {
  host     : 'localhost',
  user     : 'username',
  password : 'password',
  database : 'database'
}
 
const yamaform = new Yamaform(databaseConfig, 'database.json')
 
//Generate tables using "database.json" file
yamaform.generateTables()

JSON file example:

{
    "person": {
        "name": "varchar(45)",
        "age": "integer",
        "hasMany": "dog",
        "hasOne":"address"
    },
    "dog": {
        "name": "varchar(45)",
        "age": "integer",
        "hasMany": "person"
    },
    "address": {
        "name": "varchar(45)"
    }
}

Relationships

You can specify many to one relationships with hasOne keyword and many to many relationships with keyword hasMany, on the example above, person has one address, so a foreign key called address_id will be created on person table.

Also a person has many dogs and a dog has many persons, so a associative table called person_dog will be created with the foreign keys person_id and dog_id.

If in json file the dog object didn't have a hasMany keyword, a foreign key called person_id would be created in dog table.

In many to many relationships both tables must have a name column.

Every table created will have a column called id that will be a primary key auto incremented

If a table has more then one many to one relationship use hasOne: [table1,table2]


Generate form

let props = {
  "method":'put',
  "id":1, //Used when method is put
  "action":'/',
  'formClass':'', //Optional
  'labelClass':'', //Optional
  'inputClass':'', //Optional
  'inputWrapperClass':'', //Optional 
  'buttonClass':'', //Optional
  'buttonText':'', //Optional
}
 
const getForm = async () => {
  let form = await yamaform.generateForm('person', props)
  console.log(form)
}
 
getForm()

generateForm method expects a database table name and an object of properties that will be used in the form element


Fetch data and generate a HTML table

let props = {
  "tableClass":'', // Optional
  'viewUrl':'/your/url', // Optional, url to view record, will become /your/url/(record id)
  'deleteUrl':'/your/url', // Optional, url to delete record, will become /your/url/(record id)
  'tableClass':'', // Optional
  'viewText':'', // Optional, text for link to view, default: view
  'deleteText':'' // Optional, text for link to delete, default: delete
}
 
const fetch = async () => {
  let table = await yamaform.fetch('person', props)
  console.log(table)
}
 
fetch()

fetch method expects a database table name and an object of properties that will be used in the table element


Insert data in database

let data = {
   "tableName":[
      {"columnName":"value", "columnName":"value"},
      {"columnName":"value", "columnName":"value"}
   ],
   "otherTableName":[
      {"columnName":"value", "columnName":"value"},
      {"columnName":"value", "columnName":"value"}
   ]
}
 
const insert = async () => {
  let result = await yamaform.insert(data)
  console.log(result)
}
 
insert()

insert method expects a json object with table name and data to be inserted into database, returns an array of IDs of the inserted rows


Update data in database

let data = {
   "tableName":[
      {"id":"value", "columnName":"value"},
      {"id":"value", "columnName":"value"}
   ],
   "otherTableName":[
      {"id":"value", "columnName":"value"},
      {"id":"value", "columnName":"value"}
   ]
}
 
const update = async () => {
  let result = await yamaform.update(data)
  console.log(result)
}
 
update()

update method expects a json object with table name and data (must contain the id of the record which will be update) to be inserted into database, returns an array of IDs of the affected rows


Delete from database

let data = {
   "tableName":[
      {'where':'id = 34'}
   ],
   "otherTableName":[
      {'where':'name = "john doe" '}
   ]
}
 
const remove = async () => {
  let result = await yamaform.remove(data)
  console.log(result)
}
 
remove()

update method expects a json object with table name and a WHERE clause to specify a condition to delete records, returns an array of IDs of the affected rows

Package Sidebar

Install

npm i yamaform

Weekly Downloads

1

Version

2.7.2

License

ISC

Unpacked Size

24.2 kB

Total Files

4

Last publish

Collaborators

  • ricardo93borges