forked from TimelordUK/node-sqlserver-v8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequelize.js
94 lines (85 loc) · 1.94 KB
/
sequelize.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const Sequelize = require('sequelize')
const { TestEnv } = require('../../test/env/test-env')
const env = new TestEnv()
const connectionString = env.connectionString
const sequelize = new Sequelize({
dialect: 'mssql',
dialectModulePath: 'msnodesqlv8/lib/sequelize',
dialectOptions: {
user: '',
password: '',
database: 'node',
options: {
driver: '',
connectionString,
trustedConnection: true,
instanceName: ''
}
},
pool: {
min: 0,
max: 5,
idle: 10000
}
})
function createUserModel () {
return sequelize.define('user', {
username: {
type: Sequelize.STRING
},
job: {
type: Sequelize.STRING
}
})
}
async function userModel () {
const user = createUserModel()
// force: true will drop the table if it already exists
await user.sync({ force: true })
await Promise.all([
user.create({
username: 'techno01',
job: 'Programmer'
}),
user.create({
username: 'techno02',
job: 'Head Programmer'
}),
user.create({
username: 'techno03',
job: 'Agile Leader'
})
])
const id1 = await user.findByPk(3)
console.log(JSON.stringify(id1, null, 4))
const agile = await user.findOne({
where: { job: 'Agile Leader' }
})
console.log(JSON.stringify(agile, null, 4))
const all = await user.findAll()
console.log(JSON.stringify(all, null, 4))
const programmers = await user
.findAndCountAll({
where: {
job: {
[Sequelize.Op.like]: '%Programmer'
}
},
limit: 2
})
console.log(programmers.count)
const dataValues = programmers.rows.reduce((aggregate, latest) => {
aggregate.push(latest.dataValues)
return aggregate
}, [])
console.log(dataValues)
const updated = await user.update(
{ job: 'Scrum Master' },
{ where: { id: 3 } })
console.log(updated)
}
userModel().then(() => {
sequelize.close().then(() => {
console.log('done')
})
})