Skip to content

Commit

Permalink
zr: added dbToObject and objectToDb
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Mar 9, 2024
1 parent 9687bb6 commit 85599c7
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 21 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

- feat(lib/zone): added, with tests, fixes #22
- feat(lib/zone_record): added, with tests, fixes #23
- feat: default get sets deleted=false
- feat: default GET sets deleted=false
- group, nameserver, permission, user, zone

- sql: return indicative boolean for delete

### [3.0.0-alpha.4] - 2024-03-05

Expand Down
2 changes: 1 addition & 1 deletion lib/test/zone_record.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": 4096,
"zid": 4096,
"name": "a",
"owner": "a.example.com.",
"ttl": 86400,
"type": "A",
"address": "1.1.1.1"
Expand Down
128 changes: 110 additions & 18 deletions lib/zone_record.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { typeMap } from '@nictool/dns-resource-record'
import * as RR from '@nictool/dns-resource-record'

import Mysql from './mysql.js'
import { mapToDbColumn } from './util.js'

const zrDbMap = { id: 'nt_zone_record_id', zid: 'nt_zone_id' }
const zrDbMap = { id: 'nt_zone_record_id', zid: 'nt_zone_id', owner: 'name' }
const boolFields = ['deleted']

class ZoneRecord {
Expand All @@ -17,9 +17,15 @@ class ZoneRecord {
if (g.length === 1) return g[0].id
}

args = JSON.parse(JSON.stringify(args))
args.type_id = typeMap[args.type]
delete args.type
try {
const zr = new RR[args.type](args)
console.log(zr)
}
catch (e) {
console.error(e.message)
}

args = objectToDb(args)

return await Mysql.execute(
...Mysql.insert(`nt_zone_record`, mapToDbColumn(args, zrDbMap)),
Expand Down Expand Up @@ -54,22 +60,11 @@ class ZoneRecord {
for (const b of boolFields) {
row[b] = row[b] === 1
}
for (const f of [
'description',
'other',
'location',
'weight',
'priority',
'timestamp',
]) {
if (null === row[f]) delete row[f]
}
row.type = typeMap[row.type_id]
delete row.type_id

if (args.deleted === false) delete row.deleted
}

return rows
return dbToObject(rows)
}

async put(args) {
Expand Down Expand Up @@ -104,3 +99,100 @@ class ZoneRecord {
}

export default new ZoneRecord()

function dbToObject(rows) {
rows = JSON.parse(JSON.stringify(rows))

for (const row of rows) {
row.owner = row.name
delete row.name

row.type = RR.typeMap[row.type_id]
delete row.type_id

for (const f of [
'description',
'other',
'location',
'weight',
'priority',
'timestamp',
]) {
if (null === row[f]) delete row[f]
}
}

return rows
}

function objectToDb(obj) {
obj = JSON.parse(JSON.stringify(obj))

switch (obj.type) {
case 'CAA':
applyMap(obj, {
weight: 'flags',
priority: 'tag',
address: 'value',
})
break
case 'CNAME':
applyMap(obj, { address: 'cname' })
case 'DNAME':
applyMap(obj, { address: 'target' })
case 'DNSKEY':
applyMap(obj, {
address: 'public key',
weight: 'flags',
priority: 'protocol',
other: 'algorithm',
})
break
case 'HINFO':
obj.name = `${obj.cpu} ${obj.os}`; delete obj.cpu; delete obj.os
obj.address = obj.description; delete obj.description
break
case 'IPSECKEY':
applyMap(obj, {
address: 'gateway',
description: 'publickey',
weight: 'precedence',
priority: 'gateway type',
other: 'algorithm',
})
break
case 'NAPTR':
applyMap(obj, {
weight: 'order',
priority: 'preference',
description: 'replacement',
})
obj.address = `${obj.flags} ${obj.service} ${obj.regexp}`
delete obj.flags; delete obj.service; delete obj.regexp
break
case 'SSHFP':
applyMap(obj, {
address: 'fingerprint',
weight: 'algorithm',
priority: 'fptype',
})
break
case 'SRV':
applyMap(obj, { other: 'port' })
break
case 'URI':
applyMap(obj, { address: 'target' })
break
}

obj.type_id = RR.typeMap[obj.type]
delete obj.type

return obj
}

function applyMap (obj, map) {
for (const [key, value] of Object.entries(map)) {
obj[key] = obj[value]; delete obj[value]
}
}
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import User from './lib/user.js'
import Session from './lib/session.js'
import Permission from './lib/permission.js'
import Nameserver from './lib/nameserver.js'
import Zone from './lib/zone.js'
import ZoneRecord from './lib/zone_record.js'

import groupCase from './lib/test/group.json' with { type: 'json' }
import userCase from './lib/test/user.json' with { type: 'json' }
import zoneCase from './lib/test/zone.json' with { type: 'json' }
import zrCase from './lib/test/zone_record.json' with { type: 'json' }
import groupCaseR from './routes/test/group.json' with { type: 'json' }
import userCaseR from './routes/test/user.json' with { type: 'json' }
import nsCaseR from './routes/test/nameserver.json' with { type: 'json' }
Expand Down Expand Up @@ -47,6 +51,8 @@ async function setup() {
// }

async function teardown() {
await ZoneRecord.destroy({ id: zrCase.id })
await Zone.destroy({ id: zoneCase.id })
await Nameserver.destroy({ id: nsCaseR.id })
await Nameserver.destroy({ id: nsCaseR.id - 1 })
await Permission.destroy({ id: userCase.id })
Expand Down

0 comments on commit 85599c7

Please sign in to comment.