-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.js
60 lines (56 loc) · 1.49 KB
/
crud.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
const casual = require('casual')
const insertUserLoop = (db, interval) => {
setInterval(async () => {
const user = {
name: casual.full_name,
reputation: casual.integer(1, 10),
}
await db.run(
'insert into users (name, reputation) values (?, ?)',
user.name,
user.reputation
)
console.log('A new user was inserted: \x1b[32m%s\x1b[0m', user.name)
}, interval)
}
const deleteUserLoop = (db, interval) => {
setInterval(async () => {
const { id, name } = await db.get(
'SELECT * FROM users where deleted_at is null ORDER BY RANDOM() LIMIT 1'
)
await db.run(
"update users set deleted_at = strftime('%Y-%m-%dT%H:%M:%S', 'now') where id = ?",
id
)
console.log('A user was marked as removed: \x1b[31m%s\x1b[0m', name)
}, interval)
}
const updateUserLoop = (db, interval) => {
setInterval(async () => {
const { id, name, reputation } = await db.get(
'SELECT * FROM users where deleted_at is null ORDER BY RANDOM() LIMIT 1'
)
await db.run(
`update users set deleted_at = strftime('%Y-%m-%dT%H:%M:%S', 'now')
where id = ?`,
id
)
const rep = casual.integer(1, 10)
await db.run(
`insert into users (name, reputation) values (?, ?)`,
name,
rep
)
console.log(
"User's reputation was changed (%d -> %d): \x1b[33m%s\x1b[0m",
reputation,
rep,
name
)
}, interval)
}
module.exports = {
insertUserLoop,
deleteUserLoop,
updateUserLoop,
}