doix-db 0.0.35 → 0.0.37

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
@@ -12,6 +12,7 @@ module.exports = {
12
12
  DbObjectMap: require ('./lib/model/DbObjectMap.js'),
13
13
  DbObject: require ('./lib/model/DbObject.js'),
14
14
  DbModel: require ('./lib/model/DbModel.js'),
15
+ DbMigrationPlan:require ('./lib/migration/DbMigrationPlan.js'),
15
16
  DbQuery: require ('./lib/query/DbQuery.js'),
16
17
  DbQueryTable: require ('./lib/query/DbQueryTable.js'),
17
18
  DbQueryColumn: require ('./lib/query/DbQueryColumn.js'),
package/lib/DbClient.js CHANGED
@@ -1,6 +1,9 @@
1
- const EventEmitter = require ('events')
2
- const {randomUUID} = require ('crypto')
3
- const DbQuery = require ('./query/DbQuery.js')
1
+ const EventEmitter = require ('events')
2
+ const {randomUUID} = require ('crypto')
3
+
4
+ const DbQuery = require ('./query/DbQuery.js')
5
+ const DbMigrationPlan = require ('./migration/DbMigrationPlan.js')
6
+
4
7
  const NULL = Symbol.for ('NULL')
5
8
 
6
9
  class DbClient extends EventEmitter {
@@ -12,6 +15,12 @@ class DbClient extends EventEmitter {
12
15
  this.uuid = randomUUID ()
13
16
 
14
17
  }
18
+
19
+ createMigrationPlan () {
20
+
21
+ return new DbMigrationPlan (this)
22
+
23
+ }
15
24
 
16
25
  async getArrayBySql (sql, params = [], options = {}) {
17
26
 
package/lib/DbLang.js CHANGED
@@ -29,6 +29,18 @@ class DbLang {
29
29
 
30
30
  }
31
31
 
32
+ getDbObjectClassesToDiscover () {
33
+
34
+ return [DbTable]
35
+
36
+ }
37
+
38
+ getRequiredMutation (asIs, toBe) {
39
+
40
+ return null
41
+
42
+ }
43
+
32
44
  quoteName (s) {
33
45
 
34
46
  return CH_QQ + QQ_ESC.escape (s) + CH_QQ
@@ -85,13 +97,18 @@ class DbLang {
85
97
 
86
98
  getDbColumnTypeDim (col) {
87
99
 
88
- if (!('size' in col)) return col.type
100
+ const {type, size} = col; if (size == null) return type
89
101
 
90
- let s = col.type + '(' + col.size
102
+ let s = `${type}(${size}`
91
103
 
92
- if ('scale' in col) s += ',' + col.scale
104
+ const {scale} = col; if (scale != null) {
105
+ s += ','
106
+ s += scale
107
+ }
108
+
109
+ s += ')'
93
110
 
94
- return s + ')'
111
+ return s
95
112
 
96
113
  }
97
114
 
@@ -0,0 +1,88 @@
1
+ const EventEmitter = require ('events')
2
+
3
+ const ACTIONS = [
4
+ 'create',
5
+ 'alter',
6
+ 'migrate',
7
+ 'recreate',
8
+ ]
9
+
10
+ class DbMigrationPlan extends EventEmitter {
11
+
12
+ constructor (db) {
13
+
14
+ super ()
15
+
16
+ this.db = db
17
+ this.lang = db.lang
18
+ this.model = db.model
19
+ this.toBe = db.model.map
20
+
21
+ db.lang.migrationPlan = this
22
+
23
+ this.on ('existing', o => this.asIs.set (o.name, o))
24
+
25
+ this.toDo = new Map; for (const a of ACTIONS) this.on (a, o => {
26
+
27
+ if (!this.toDo.has (a)) this.toDo.set (a, [])
28
+
29
+ this.toDo.get (a).push (o)
30
+
31
+ })
32
+
33
+ }
34
+
35
+ inspectStructure () {
36
+
37
+ const {asIs} = this; if (!asIs) throw Error ('Existing database objects not discovered, call loadStructure () first')
38
+
39
+ const {lang} = this, hardClasses = lang.getDbObjectClassesToDiscover ()
40
+
41
+ for (const [name, object] of this.toBe.entries ()) {
42
+
43
+ const action =
44
+
45
+ !hardClasses.includes (object.constructor) ? 'recreate' :
46
+
47
+ !asIs.has (name) ? 'create' :
48
+
49
+ lang.getRequiredMutation (asIs.get (name), object)
50
+
51
+ if (action) this.emit (action, object)
52
+
53
+ }
54
+
55
+ }
56
+
57
+ async loadStructure () {
58
+
59
+ this.asIs = new Map ()
60
+
61
+ return Promise.all (this.lang.getDbObjectClassesToDiscover ().map (type => this.loadExistingObjects (type)))
62
+
63
+ }
64
+
65
+ async loadExistingObjects (type) {
66
+
67
+ const methodName = `getStreamOfExisting${type.name.slice (2)}s`
68
+
69
+ const {db, asIs, toBe} = this, s = await db [methodName] ()
70
+
71
+ return new Promise ((ok, fail) => {
72
+
73
+ s.on ('error', fail)
74
+
75
+ s.on ('end', ok)
76
+
77
+ s.on ('data', table => this.emit (
78
+ toBe.has (table.name) ? 'existing' : 'unknown',
79
+ table
80
+ ))
81
+
82
+ })
83
+
84
+ }
85
+
86
+ }
87
+
88
+ module.exports = DbMigrationPlan
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doix-db",
3
- "version": "0.0.35",
3
+ "version": "0.0.37",
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.42"
43
+ "doix": "^0.0.43"
44
44
  },
45
45
  "devDependencies": {
46
46
  "jest": "^29.3.1"