doix-db 0.0.4 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -10,4 +10,5 @@ module.exports = {
10
10
  DbObjectMap: require ('./lib/DbObjectMap.js'),
11
11
  DbModel: require ('./lib/DbModel.js'),
12
12
  DbPool: require ('./lib/DbPool.js'),
13
+ DbLang: require ('./lib/DbLang.js'),
13
14
  }
package/lib/DbLang.js ADDED
@@ -0,0 +1,17 @@
1
+ const stringEscape = require ('string-escape-map')
2
+
3
+ const QQ_ESC = new stringEscape ([
4
+ [ '"', '""'],
5
+ ])
6
+
7
+ class DbLang {
8
+
9
+ quoteName (s) {
10
+
11
+ return '"' + QQ_ESC.escape (s) + '"'
12
+
13
+ }
14
+
15
+ }
16
+
17
+ module.exports = DbLang
package/lib/DbModel.js CHANGED
@@ -1,9 +1,17 @@
1
- const DbPool = require ('./DbPool.js')
2
- const DbObjectMap = require ('./DbObjectMap.js')
1
+ const EventEmitter = require ('events')
3
2
 
4
- class DbModel {
3
+ const DbPool = require ('./DbPool.js')
4
+ const DbObjectMap = require ('./DbObjectMap.js')
5
+ const DbObjectTypeDetector = require ('./DbObjectTypeDetector')
6
+
7
+ const EV_MODULES_LOADED = 'modules-loaded'
8
+ const EV_OBJECTS_CREATED = 'objects-created'
9
+
10
+ class DbModel extends EventEmitter {
5
11
 
6
12
  constructor (o) {
13
+
14
+ super ()
7
15
 
8
16
  let mapOptions = {}
9
17
 
@@ -12,31 +20,64 @@ class DbModel {
12
20
  case 'dir':
13
21
  case 'ext':
14
22
  case 'merger':
15
- case 'detector':
16
23
  mapOptions [k] = v
17
24
  break
18
25
 
26
+ case 'detector':
27
+ if (!(v instanceof DbObjectTypeDetector)) throw new Error ('Only DbObjectTypeDetector or its descendant can be used as detector')
28
+ this.detector = v
29
+ break
30
+
19
31
  case 'db':
20
32
  if (!(v instanceof DbPool)) throw new Error ('Only DbPool descendant can be used as db')
21
33
  this.db = v
22
- v.setModel (this)
34
+ v.model = this
23
35
  break
24
36
 
25
37
  default:
26
38
  throw new Error ('Unknown DbModel option: ' + k)
27
39
 
28
40
  }
41
+
42
+ if (!('detector' in this)) this.detector = new DbObjectTypeDetector ()
29
43
 
30
44
  this.map = new DbObjectMap (mapOptions)
31
-
32
- }
45
+
46
+ this.on (EV_MODULES_LOADED, () => this.createObjects ())
33
47
 
48
+ }
49
+
34
50
  loadModules () {
35
51
 
36
52
  this.map.load ()
53
+
54
+ this.emit ('modules-loaded')
37
55
 
38
56
  }
39
57
 
58
+ createObjects () {
59
+
60
+ const {detector} = this, map = new Map ()
61
+
62
+ for (const [name, pojo] of this.map.entries ()) {
63
+
64
+ const o = new (detector.getClass (pojo)) (pojo)
65
+
66
+ o.name = name
67
+
68
+ map.set (name, o)
69
+
70
+ }
71
+
72
+ this.map = map
73
+
74
+ this.emit ('objects-created')
75
+
76
+ }
77
+
40
78
  }
41
79
 
80
+ DbModel.EV_MODULES_LOADED = EV_MODULES_LOADED
81
+ DbModel.EV_OBJECTS_CREATED = EV_OBJECTS_CREATED
82
+
42
83
  module.exports = DbModel
@@ -1,6 +1,5 @@
1
1
  const {ModuleLoader} = require ('doix')
2
2
  const DbObjectMerger = require ('./DbObjectMerger')
3
- const DbObjectTypeDetector = require ('./DbObjectTypeDetector')
4
3
 
5
4
  class DbObjectMap extends Map {
6
5
 
@@ -22,11 +21,6 @@ class DbObjectMap extends Map {
22
21
  this.merger = v
23
22
  break
24
23
 
25
- case 'detector':
26
- if (!(v instanceof DbObjectTypeDetector)) throw new Error ('Only DbObjectTypeDetector or its descendant can be used as detector')
27
- this.detector = v
28
- break
29
-
30
24
  default:
31
25
  throw new Error ('Unknown DbObjectMap option: ' + k)
32
26
 
@@ -34,8 +28,6 @@ class DbObjectMap extends Map {
34
28
 
35
29
  this.loader = new ModuleLoader (loaderOptions)
36
30
 
37
- if (!('detector' in this)) this.detector = new DbObjectTypeDetector ()
38
-
39
31
  if (!('merger' in this)) this.merger = new DbObjectMerger ()
40
32
 
41
33
  }
@@ -46,16 +38,6 @@ class DbObjectMap extends Map {
46
38
 
47
39
  const {merger} = this
48
40
 
49
- merger.on ('complete', (pojo, name) => {
50
-
51
- const o = new (this.detector.getClass (pojo)) (pojo)
52
-
53
- o.name = name
54
-
55
- super.set (name, o)
56
-
57
- })
58
-
59
41
  for (const [k, v] of this.entries ()) merger.emit ('complete', v, k)
60
42
 
61
43
  }
package/lib/DbPool.js CHANGED
@@ -7,22 +7,15 @@ class DbPool extends ResourcePool {
7
7
 
8
8
  super ()
9
9
 
10
- this.globals = {}
11
-
10
+ this.shared.add ('model')
11
+ this.shared.add ('lang')
12
+
12
13
  this.logger = o.logger
13
14
 
14
15
  this.eventLoggerClass = o.eventLoggerClass || DbEventLogger
15
16
 
16
17
  }
17
18
 
18
- setModel (m) {
19
-
20
- this.model = m
21
-
22
- this.globals.model = m
23
-
24
- }
25
-
26
19
  }
27
20
 
28
21
  module.exports = DbPool
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doix-db",
3
- "version": "0.0.4",
3
+ "version": "0.0.7",
4
4
  "description": "Shared database related code for doix",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -40,7 +40,7 @@
40
40
  },
41
41
  "homepage": "https://github.com/do-/node-doix-db#readme",
42
42
  "peerDependencies": {
43
- "doix": "^0.0.35"
43
+ "doix": "^0.0.36"
44
44
  },
45
45
  "devDependencies": {
46
46
  "jest": "^29.3.1"