Documentation Tutorials

Getting started (MYSQL)

Fill out the variables in the .env file. You can read about the variables here.

mysql_host=localhost
mysql_user=root
mysql_password=
mysql_database=demo
mysql_soft_delete=false
mysql_soft_delete_field=deleted
mysql_query_debug=true
mysql_port=3306

Create a table

Create a file db.js file.

const db = require('js-web').migration.mysql

const migrate = async _ => {
  await db.table('users',{
    id: 'id',
    name: 'string',
    created: 'datetime'
  })
  await db.table('pets',{
    id: 'id',
    name: 'string',
    user_id: 'int',
    created: 'datetime'
  })
  process.exit()
}

migrate()

Run the db.js file.

node db.js

Using MYSQL

Define tables and db const.

import {
  storage
} from 'js-web'

const { mysql } = storage

const users = mysql.table('users')

Table(tablename)

The table function takes a table name as a parameter, and it returns an object with the following functions.

// console.log(mysql.table('users'))
{
  delete: [Function: delete],
  find: [Function: find],
  select: [Function: select],
  selectFields: [Function: selectFields],
  selectCols: [Function: selectCols],
  update: [Function: update],
  create: [Function: create]
}

Storage and routing

All MYSQL calls are returning a Promise, so you need to use an async callback function. Now it's possible to utilize the await keyword.

route('/', async (input) => {
  return await users.select()
})
postRoute('/create-simon', async (input) => {
  const simonsId = await users.create({name:'Simon'})
  console.log(simonsId)
  return back()
})