How to use the select function from knex

Find comprehensive JavaScript knex.select code examples handpicked from public code repositorys.

30
31
32
33
34
35
36
37
38
39

console.log('See Transaction...')
await knex.transaction(async trx => {  
  await knex('test_table').insert({ name: 'aa' }).transacting(trx)
  // await knex.schema.raw('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;')
  let rv = await knex.select().from('test_table')
  console.log(rv)

  await knex('test_table').insert({ name: 'bb' }).transacting(trx)
  await knex.schema.raw('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;')
fork icon116
star icon447
watch icon29

+ 19 other calls in file

127
128
129
130
131
132
133
134
135
136
 * Get a server's data from it's ID
 * @param {ServerID} serverID The server's Snowflake ID
 * * @returns {Promise<Array>}
 */
getServer: function getServer(serverID) {
    return knex.select().from(SERVERS_TABLE).where({server: serverID}).limit(1);
},
searchServer: function searchServer(serverName){
    return knex.select().from(SERVERS_TABLE).where("name", "LIKE", serverName);
},
fork icon7
star icon35
watch icon0

+ 449 other calls in file

252
253
254
255
256
257
258
259
260
261
262
263
        .orderBy(`${USER_CALCULATIONS_TABLE}.id`, 'desc')
        .groupBy(`${USER_CALCULATIONS_TABLE}.id`)
        .limit(limit || total)
        .offset(offset || 0);


    const types = await db.select().from(COLLECTIONS_TYPES_TABLE);


    return { data, total, types };
}

fork icon3
star icon5
watch icon0

+ 3 other calls in file

31
32
33
34
35
36
37
38
39
40

//const data = await knex.select('id', 'login', 'email').from('users');
//const data = await knex.select('id', 'login', 'email').table('users');

//const data = await knex.select('id as identifier').from('users');
//const data = await knex.select(knex.ref('id').as('identifier')).from('users');

// const data = await knex
//   .select(knex.ref('id'), knex.ref('login'))
//   .from('users');
fork icon0
star icon0
watch icon1

+ 56 other calls in file

119
120
121
122
123
124
125
126
127
128
129
    }
})


app.post('/dfac_name', (req, res) => {
    if ((req.body.name != undefined) && (req.body.location != undefined)) {
        knex.select('location').from('military_base').where('location', '=', req.body.location).then(
            data => {
                if (data.length == 0) {
                    res.status(404).send("ERROR: Location does not exist inside of military_base database")
                } else {
fork icon0
star icon0
watch icon0

+ 3 other calls in file

49
50
51
52
53
54
55
56
57
58
59
// Creating a path to the index.html file
app.get("/", (req, res) =>
{
    // res.send('[STOP] Human Trafficking');


    knex.select().from("trafficking_data").orderBy("case").then(
        // when knex.select runs, "result" will be an array with all of the results in the table
        (result) =>
        {
            res.render("index", { aCases: result }); // the "result" array is now the data's array in the ejs file
fork icon0
star icon0
watch icon0

+ 3 other calls in file

41
42
43
44
45
46
47
48
49
50

let sqlQuery = "SELECT * FROM amazon INNER JOIN (SELECT ASIN, GROUP_CONCAT(searchIndex) AS searchIndex FROM (SELECT DISTINCT ASIN, searchIndex FROM amazonImport WHERE searchIndex IS NOT NULL ORDER BY searchIndex) AS searchIndexDistinct GROUP BY ASIN) AS amazonSearchIndex ON amazon.ASIN = amazonSearchIndex.ASIN WHERE active = 1 AND merchant = 'Amazon' ORDER BY viewed, createDate, authorName, titleName";

// db.raw(sqlQuery).toSQL();

// db.select(select)
//   .from(tableName)
//   // .limit(limit)
//   // .where(activeWhere)
//   .where(viewedWhere)
fork icon0
star icon0
watch icon1

+ 5 other calls in file

44
45
46
47
48
49
50
51
52
53
54
    }
*/


// get 路由获取数据库内 demo_info 表中的数据
router.get('/getInfo', async (ctx) => {
    const queryBuilder = await knex.select().from('demo_info');
    ctx.response.body = queryBuilder;
    ctx.response.status = 200;
})

fork icon0
star icon0
watch icon0

126
127
128
129
130
131
132
133
134
135
router.get('/get-states-and-cities', async (req, res) => {
  try {
    console.log('getting states and cities');
    // knex.raw('json_agg(cities.*) as cities')   -> if we want the full data of 2nd table 
    // knex.raw('json_agg(json_build_object(\'id\', cities.id, \'name\', cities.name))  ->if we want some fields of 2nd table data
    await knex.select('states.*', knex.raw('json_agg(json_build_object(\'id\', cities.id, \'name\', cities.name)) as cities'))
      .withSchema('bookmyshow')
      .table('states')
      .join('cities', 'states.id', 'cities.stateid')
      .groupBy('states.id')
fork icon0
star icon0
watch icon0

33
34
35
36
37
38
39
40
41
42
43
44
45
    })
})


//TODO: do not fetch the whole table, just what's needed
app.get('/fetchedData', (req, res) => {
    knex.select().from('user').then(user => res.send([...user]))
})




app.listen(7555, () => {
fork icon0
star icon0
watch icon0

189
190
191
192
193
194
195
196
197
198
    last_name: last_name,
    username: username,
    password: password,
})
.then((data) =>
    knex.select('*')
        .from('user')
        .where({ username: username, password: password, first_name: first_name, last_name: last_name, })

).then(data => { console.log(data); return res.status(202).json(data) })
fork icon0
star icon0
watch icon0

+ 11 other calls in file

58
59
60
61
62
63
64
65
66
67
68
69
 ******************************/
// router.get("/", (request, response) => {


//   const where = { active: true };


//   db.select(select)
//     .from(tableName)
//     .where(where)
//     .orderBy(orderBy)
//     .then((results) => {
fork icon0
star icon0
watch icon0

+ 3 other calls in file

37
38
39
40
41
42
43
44
45
46
47
48
49






// *********** Server ************
app.get('/load$', (req, res)=> {
    knex.select("*").from("posts").limit(20).then(data => {
        //console.log(data);
        res.json(data);
    });
})
fork icon0
star icon0
watch icon0

+ 3 other calls in file