-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnameserver.js
More file actions
130 lines (116 loc) · 3.35 KB
/
nameserver.js
File metadata and controls
130 lines (116 loc) · 3.35 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import Mysql from './mysql.js'
import { mapToDbColumn } from './util.js'
const nsDbMap = { id: 'nt_nameserver_id', gid: 'nt_group_id' }
const boolFields = ['deleted', 'export_serials']
class Nameserver {
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
}
if (args.export.type) {
args = JSON.parse(JSON.stringify(args))
const rows = await Mysql.execute(
...Mysql.select('SELECT id FROM nt_nameserver_export_type', {
name: args.export.type,
}),
)
args.export_type_id = rows[0].id
delete args.export.type
}
return await Mysql.execute(...Mysql.insert(`nt_nameserver`, mapToDbColumn(objectToDb(args), nsDbMap)))
}
async get(args) {
args = JSON.parse(JSON.stringify(args))
if (args.deleted === undefined) args.deleted = false
if (args.name !== undefined) {
args['ns.name'] = args.name
delete args.name
}
const rows = await Mysql.execute(
...Mysql.select(
`SELECT ns.nt_nameserver_id AS id
, ns.nt_group_id AS gid
, ns.name
, ns.ttl
, ns.description
, ns.address
, ns.address6
, ns.remote_login
, ns.logdir
, ns.datadir
, ns.export_interval
, ns.export_serials
, ns.export_status
, ns.deleted
, t.name AS export_type
FROM nt_nameserver ns
JOIN nt_nameserver_export_type t ON ns.export_type_id=t.id`,
mapToDbColumn(args, nsDbMap),
),
)
for (const row of rows) {
for (const b of boolFields) {
row[b] = row[b] === 1
}
if (args.deleted === false) delete row.deleted
}
return dbToObject(rows)
}
async put(args) {
if (!args.id) return false
const id = args.id
delete args.id
// Mysql.debug(1)
const r = await Mysql.execute(
...Mysql.update(`nt_nameserver`, `nt_nameserver_id=${id}`, mapToDbColumn(args, nsDbMap)),
)
return r.changedRows === 1
}
async delete(args) {
const r = await Mysql.execute(
...Mysql.update(`nt_nameserver`, `nt_nameserver_id=${args.id}`, {
deleted: args.deleted ?? 1,
}),
)
return r.changedRows === 1
}
async destroy(args) {
const r = await Mysql.execute(...Mysql.delete(`nt_nameserver`, { nt_nameserver_id: args.id }))
return r.affectedRows === 1
}
}
export default new Nameserver()
function dbToObject(rows) {
for (const row of rows) {
for (const f of ['description', 'address6', 'remote_login', 'datadir', 'logdir', 'export_status']) {
if ([undefined, null].includes(row[f])) row[f] = ''
}
for (const f of ['export']) {
for (const p of ['type', 'interval', 'serials', 'status']) {
if (row[`${f}_${p}`] !== undefined) {
if (row[f] === undefined) row[f] = {}
row[f][p] = row[`${f}_${p}`]
delete row[`${f}_${p}`]
}
}
}
}
return rows
}
function objectToDb(row) {
row = JSON.parse(JSON.stringify(row)) // don't mutate the original
for (const f of ['export']) {
for (const p of ['interval', 'serials', 'status']) {
if (row[f] === undefined) continue
if (row[f][p] === undefined) continue
row[`${f}_${p}`] = row[f][p]
delete row[f][p]
}
delete row[f]
}
return row
}