-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzone.js
More file actions
105 lines (88 loc) · 2.65 KB
/
zone.js
File metadata and controls
105 lines (88 loc) · 2.65 KB
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
95
96
97
98
99
100
101
102
103
104
105
import Mysql from './mysql.js'
import { mapToDbColumn } from './util.js'
const zoneDbMap = { id: 'nt_zone_id', gid: 'nt_group_id' }
const boolFields = ['deleted']
class Zone {
constructor() {
this.mysql = Mysql
}
async create(args) {
if (args.id) {
const g = await this.get({ id: args.id })
if (g.length === 1) return g[0].id
}
return await Mysql.execute(...Mysql.insert(`nt_zone`, mapToDbColumn(args, zoneDbMap)))
}
async get(args) {
args = JSON.parse(JSON.stringify(args))
if (args.deleted === undefined) args.deleted = false
const limit = Number.isInteger(args.limit) ? args.limit : undefined
delete args.limit
const sqlLimit = limit === undefined ? '' : ` LIMIT ${Math.max(1, limit)}`
const [query, params] = Mysql.select(
`SELECT nt_zone_id AS id
, nt_group_id AS gid
, zone
, mailaddr
, description
, serial
, refresh
, retry
, expire
, minimum
, ttl
, last_modified
, last_publish
, deleted
FROM nt_zone`,
mapToDbColumn(args, zoneDbMap),
)
const rows = await Mysql.execute(`${query}${sqlLimit}`, params)
for (const row of rows) {
for (const b of boolFields) {
row[b] = row[b] === 1
}
for (const f of ['description', 'location']) {
if ([null].includes(row[f])) row[f] = ''
}
// Coerce legacy DB NULLs to sane defaults so responses validate
const zoneDefaults = {
minimum: 3600,
ttl: 3600,
refresh: 86400,
retry: 7200,
expire: 1209600,
}
for (const [f, val] of Object.entries(zoneDefaults)) {
if ([null, undefined].includes(row[f])) row[f] = val
}
if ([null, undefined].includes(row.serial)) row.serial = 0
if (row['last_publish'] === undefined) delete row['last_publish']
if (/00:00:00/.test(row['last_publish'])) row['last_publish'] = null
if (args.deleted === false) delete row.deleted
}
return rows
}
async put(args) {
if (!args.id) return false
const id = args.id
delete args.id
const r = await Mysql.execute(
...Mysql.update(`nt_zone`, `nt_zone_id=${id}`, mapToDbColumn(args, zoneDbMap)),
)
return r.changedRows === 1
}
async delete(args) {
const r = await Mysql.execute(
...Mysql.update(`nt_zone`, `nt_zone_id=${args.id}`, {
deleted: args.deleted ?? 1,
}),
)
return r.changedRows === 1
}
async destroy(args) {
const r = await Mysql.execute(...Mysql.delete(`nt_zone`, { nt_zone_id: args.id }))
return r.affectedRows === 1
}
}
export default new Zone()