Documentation Tutorials

Creating (MYSQL)

The following examples are using this migration:

db.table('users',{
  id: 'id',
  name: 'string',
  created: 'datetime'
})

All examples below are using these const definitions.

import {
  storage
} from 'js-web'

const { mysql } = storage

const users = mysql.table('users')

Create(obj)

Use the create(obj) function on the return object, from the table function. In the example below, the users is const users = db.table('users')

const simonsId = await users.create({
  name: 'Simon Madsen',
  created: db.now()
})

Create() inside a route

Remember to use the await and async. The create function is returning a Promise.

route('/create-user/:name', async (input) => {
  const userId = await users.create({
    name: input.name,
    created: db.now()
  })
  console.log('user id: '+userId+' has been created');
  return redirect('/')
})