npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

yamaform

v2.7.2

Published

Generate form and database tables from a json file

Downloads

9

Readme

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