-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
31 lines (29 loc) · 938 Bytes
/
setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//How to create a table using knex.js
const setting = require('./routes/settings');
const knex = require('knex')({
client: 'mysql',
connection: setting.database
})
const bcrypt = require('bcryptjs');
const saltRounds = 10;
knex.schema.hasTable('users').then(exists => {
if (!exists) {
return knex.schema.createTable('users', table => {
table.increments('id')
table.string('username')
table.string('password')
})
.then(() => console.log('Users table has been created'))
.catch(err => console.log(err))
} else {
const username = 'Antonio'
const password = 'password'
bcrypt.genSalt(saltRounds, (error, salt) => {
bcrypt.hash(password, salt, (err, hash) => {
return knex('users').insert({ username, password: hash})
.then(() => console.log(`${username} inserted`))
.catch(err => console.log(err));
});
});
}
}).catch(err => console.log(err));