|
| 1 | +import Mysql from './mysql.js' |
| 2 | +import { mapToDbColumn } from './util.js' |
| 3 | + |
| 4 | +const nsDbMap = { id: 'nt_nameserver_id', gid: 'nt_group_id' } |
| 5 | +const boolFields = ['deleted', 'export_serials'] |
| 6 | + |
| 7 | +class Nameserver { |
| 8 | + constructor() { |
| 9 | + this.mysql = Mysql |
| 10 | + } |
| 11 | + |
| 12 | + async create(args) { |
| 13 | + if (args.id) { |
| 14 | + const g = await this.get({ id: args.id }) |
| 15 | + if (g.length === 1) return g[0].id |
| 16 | + } |
| 17 | + |
| 18 | + if (args.export.type) { |
| 19 | + args = JSON.parse(JSON.stringify(args)) |
| 20 | + const rows = await Mysql.execute( |
| 21 | + ...Mysql.select('SELECT id FROM nt_nameserver_export_type', { |
| 22 | + name: args.export.type, |
| 23 | + }), |
| 24 | + ) |
| 25 | + args.export_type_id = rows[0].id |
| 26 | + delete args.export.type |
| 27 | + } |
| 28 | + |
| 29 | + return await Mysql.execute( |
| 30 | + ...Mysql.insert( |
| 31 | + `nt_nameserver`, |
| 32 | + mapToDbColumn(objectToDb(args), nsDbMap), |
| 33 | + ), |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + async get(args) { |
| 38 | + if (args.name !== undefined) { |
| 39 | + args['ns.name'] = args.name |
| 40 | + delete args.name |
| 41 | + } |
| 42 | + const rows = await Mysql.execute( |
| 43 | + ...Mysql.select( |
| 44 | + `SELECT ns.nt_nameserver_id AS id |
| 45 | + , ns.nt_group_id AS gid |
| 46 | + , ns.name |
| 47 | + , ns.ttl |
| 48 | + , ns.description |
| 49 | + , ns.address |
| 50 | + , ns.address6 |
| 51 | + , ns.remote_login |
| 52 | + , ns.logdir |
| 53 | + , ns.datadir |
| 54 | + , ns.export_interval |
| 55 | + , ns.export_serials |
| 56 | + , ns.export_status |
| 57 | + , ns.deleted |
| 58 | + , t.name AS export_type |
| 59 | + FROM nt_nameserver ns |
| 60 | + JOIN nt_nameserver_export_type t ON ns.export_type_id=t.id`, |
| 61 | + mapToDbColumn(args, nsDbMap), |
| 62 | + ), |
| 63 | + ) |
| 64 | + for (const r of rows) { |
| 65 | + for (const b of boolFields) { |
| 66 | + r[b] = r[b] === 1 |
| 67 | + } |
| 68 | + } |
| 69 | + return dbToObject(rows) |
| 70 | + } |
| 71 | + |
| 72 | + async put(args) { |
| 73 | + if (!args.id) return false |
| 74 | + const id = args.id |
| 75 | + delete args.id |
| 76 | + // Mysql.debug(1) |
| 77 | + const r = await Mysql.execute( |
| 78 | + ...Mysql.update( |
| 79 | + `nt_nameserver`, |
| 80 | + `nt_nameserver_id=${id}`, |
| 81 | + mapToDbColumn(args, nsDbMap), |
| 82 | + ), |
| 83 | + ) |
| 84 | + // console.log(r) |
| 85 | + return r.changedRows === 1 |
| 86 | + } |
| 87 | + |
| 88 | + async delete(args) { |
| 89 | + await Mysql.execute( |
| 90 | + `UPDATE nt_nameserver SET deleted=? WHERE nt_nameserver_id=?`, |
| 91 | + [args.deleted ?? 1, args.id], |
| 92 | + ) |
| 93 | + return true |
| 94 | + } |
| 95 | + |
| 96 | + async destroy(args) { |
| 97 | + return await Mysql.execute( |
| 98 | + ...Mysql.delete(`nt_nameserver`, { nt_nameserver_id: args.id }), |
| 99 | + ) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +export default new Nameserver() |
| 104 | + |
| 105 | +function dbToObject(rows) { |
| 106 | + for (const row of rows) { |
| 107 | + for (const f of [ |
| 108 | + 'description', |
| 109 | + 'address6', |
| 110 | + 'remote_login', |
| 111 | + 'datadir', |
| 112 | + 'logdir', |
| 113 | + 'export_status', |
| 114 | + ]) { |
| 115 | + if ([undefined, null].includes(row[f])) row[f] = '' |
| 116 | + } |
| 117 | + for (const f of ['export']) { |
| 118 | + for (const p of ['type', 'interval', 'serials', 'status']) { |
| 119 | + if (row[`${f}_${p}`] !== undefined) { |
| 120 | + if (row[f] === undefined) row[f] = {} |
| 121 | + row[f][p] = row[`${f}_${p}`] |
| 122 | + delete row[`${f}_${p}`] |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + return rows |
| 128 | +} |
| 129 | + |
| 130 | +function objectToDb(row) { |
| 131 | + row = JSON.parse(JSON.stringify(row)) // don't mutate the original |
| 132 | + |
| 133 | + for (const f of ['export']) { |
| 134 | + for (const p of ['interval', 'serials', 'status']) { |
| 135 | + if (row[f] === undefined) continue |
| 136 | + if (row[f][p] === undefined) continue |
| 137 | + row[`${f}_${p}`] = row[f][p] |
| 138 | + delete row[f][p] |
| 139 | + } |
| 140 | + delete row[f] |
| 141 | + } |
| 142 | + return row |
| 143 | +} |
0 commit comments