Skip to content

Commit 8561a86

Browse files
authored
routes file organization (#46)
1 parent eec83cc commit 8561a86

64 files changed

Lines changed: 156 additions & 51 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/group/store/mysql.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Mysql from '../../mysql.js'
22
import GroupBase from './base.js'
3-
import Permission from '../../permission.js'
3+
import Permission from '../../permission/index.js'
44
import { mapToDbColumn } from '../../util.js'
55

66
const groupDbMap = { id: 'nt_group_id', parent_gid: 'parent_group_id' }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from 'node:assert/strict'
22
import { describe, it, after, before } from 'node:test'
33

4-
import Group from './index.js'
4+
import Group from '../index.js'
55

66
import testCase from '../test/group.json' with { type: 'json' }
77

lib/nameserver/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const storeType = process.env.NICTOOL_DATA_STORE ?? 'mysql'
2+
3+
let RepoClass
4+
switch (storeType) {
5+
case 'toml':
6+
RepoClass = (await import('./store/toml.js')).default
7+
break
8+
case 'mongodb':
9+
RepoClass = (await import('./store/mongodb.js')).default
10+
break
11+
case 'elasticsearch':
12+
RepoClass = (await import('./store/elasticsearch.js')).default
13+
break
14+
default:
15+
RepoClass = (await import('./store/mysql.js')).default
16+
}
17+
18+
export default new RepoClass()

lib/nameserver/store/base.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Nameserver store base class – pure attributes and business logic.
3+
*
4+
* Has zero knowledge of how nameservers are persisted. All nameserver repository classes
5+
* must extend this class and implement the repo contract methods.
6+
*
7+
* Repo contract:
8+
* get(args) → object[]
9+
* create(args) → number (zoneId)
10+
* put(args) → boolean
11+
* delete(args) → boolean
12+
* destroy(args) → boolean
13+
*/
14+
class NameserverBase {
15+
constructor(args = {}) {
16+
this.debug = args?.debug ?? false
17+
}
18+
19+
// -------------------------------------------------------------------------
20+
// Repo contract – subclasses must implement these
21+
// -------------------------------------------------------------------------
22+
23+
async get(_args) {
24+
throw new Error('get() not implemented by this repo')
25+
}
26+
27+
async create(_args) {
28+
throw new Error('create() not implemented by this repo')
29+
}
30+
31+
async put(_args) {
32+
throw new Error('put() not implemented by this repo')
33+
}
34+
35+
async delete(_args) {
36+
throw new Error('delete() not implemented by this repo')
37+
}
38+
39+
async destroy(_args) {
40+
throw new Error('destroy() not implemented by this repo')
41+
}
42+
}
43+
44+
export default NameserverBase
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import Mysql from './mysql.js'
2-
import { mapToDbColumn } from './util.js'
1+
import Mysql from '../../mysql.js'
2+
import NameserverBase from './base.js'
3+
import { mapToDbColumn } from '../../util.js'
34

45
const nsDbMap = { id: 'nt_nameserver_id', gid: 'nt_group_id' }
56
const boolFields = ['deleted', 'export_serials']
67

7-
class Nameserver {
8+
class Nameserver extends NameserverBase {
89
constructor() {
10+
super()
911
this.mysql = Mysql
1012
}
1113

@@ -94,7 +96,7 @@ class Nameserver {
9496
}
9597
}
9698

97-
export default new Nameserver()
99+
export default Nameserver
98100

99101
function dbToObject(rows) {
100102
for (const row of rows) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import assert from 'node:assert/strict'
22
import { describe, it, after, before } from 'node:test'
33

4-
import Nameserver from './nameserver.js'
4+
import Nameserver from '../index.js'
55

6-
import testCase from './test/nameserver.json' with { type: 'json' }
6+
import testCase from './nameserver.json' with { type: 'json' }
77

88
before(async () => {
99
await Nameserver.destroy({ id: testCase.id })
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Mysql from './mysql.js'
2-
import { mapToDbColumn } from './util.js'
1+
import Mysql from '../mysql.js'
2+
import { mapToDbColumn } from '../util.js'
33

44
const permDbMap = {
55
id: 'nt_perm_id',
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import assert from 'node:assert/strict'
22
import { describe, it, after, before } from 'node:test'
33

4-
import Group from './group/index.js'
5-
import User from './user/index.js'
6-
import Permission from './permission.js'
4+
import Group from '../../group/index.js'
5+
import User from '../../user/index.js'
6+
import Permission from '../index.js'
77

8-
import groupTestCase from './test/group.json' with { type: 'json' }
9-
import userTestCase from './test/user.json' with { type: 'json' }
10-
import permTestCase from './test/permission.json' with { type: 'json' }
8+
import groupTestCase from '../../group/test/group.json' with { type: 'json' }
9+
import userTestCase from '../../user/test/user.json' with { type: 'json' }
10+
import permTestCase from './permission.json' with { type: 'json' }
1111

1212
before(async () => {
1313
await Group.create(groupTestCase)

0 commit comments

Comments
 (0)