Documentation Tutorials

Reading (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')

Reading functions

Following reading functions are available on the object returned by the table(tablename) function.

{
  select: [Function: select],
  find: [Function: find],
  selectFields: [Function: selectFields]
}

The select function

select(where,orderBy,limit)
The select function returns an array of rows.

await users.select()
[
   {
      "id":1,
      "name":"Amanda",
      "created":"2017-08-20T19:23:43.000Z"
   },
   {
      "id":2,
      "name":"Simon",
      "created":"2017-08-20T19:23:43.000Z"
   }
]

Where paramter

await users.select({name:'Amanda'})
[
   {
      "id":1,
      "name":"Amanda",
      "created":"2017-08-20T19:23:43.000Z"
   }
]

Order By paramter

await users.select(null,'id desc')
[
   {
      "id":2,
      "name":"Simon",
      "created":"2017-08-20T19:23:43.000Z"
   },
   {
      "id":1,
      "name":"Amanda",
      "created":"2017-08-20T19:23:43.000Z"
   }
]

Limit paramter

await users.select(null,'id desc',1)
[
   {
      "id":2,
      "name":"Simon",
      "created":"2017-08-20T19:23:43.000Z"
   }
]

Routing and Reading

Remember to use the await and async, because the select function is returning a Promise.

web.route('/', async (input) => {
  return await users.select()
})

The find function

find(where)
The select function returns a object or null if not found

await users.find({id:1})
{
   "id":1,
   "name":"Amanda",
   "created":"2017-08-20T19:23:43.000Z"
}

The selectFields function

select(fields,where,orderBy,limit)
The select function returns an array of rows.

await users.selectFields('id,name as person')
[
   {
      "id":1,
      "person":"Amanda"
   },
   {
      "id":2,
      "person":"Simon"
   }
]