@live-change/db 0.3.62 → 0.3.63

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/lib/Database.js CHANGED
@@ -28,7 +28,7 @@ class Database {
28
28
  this.configObservable = new ReactiveDao.ObservableValue(JSON.parse(JSON.stringify(this.config)))
29
29
  this.tablesListObservable = new ReactiveDao.ObservableList(Object.keys(this.config.tables))
30
30
  this.indexesListObservable = new ReactiveDao.ObservableList(Object.keys(this.config.indexes))
31
- this.logsListObservable = new ReactiveDao.ObservableList(Object.keys(this.config.logs))
31
+ this.logsListObservable = new ReactiveDao.ObservableList(Object.keys(this.config.logs))
32
32
  }
33
33
 
34
34
  async start(startConfig = {}) {
@@ -93,7 +93,7 @@ class Database {
93
93
  this.tablesListObservable.remove(name)
94
94
  this.tables.delete(name)
95
95
  }
96
-
96
+
97
97
  renameTable(name, newName) {
98
98
  if(this.config.tables[newName]) throw new Error(`Table ${newName} already exists`)
99
99
  const table = this.table(name)
@@ -227,25 +227,38 @@ class Database {
227
227
  return summary
228
228
  }
229
229
 
230
+ async openIndex(name) {
231
+ let config = this.config.indexes[name]
232
+ if(!config) {
233
+ console.log("INDEX", name, "NOT EXISTS - WAITING!")
234
+ await new Promise(r => setTimeout(r, 500))
235
+ config = this.config.indexes[name]
236
+ }
237
+ let index, code
238
+ if(!config) throw new Error(`Index ${name} not found`)
239
+ code = config.code
240
+ const params = config.parameters
241
+ index = new Index(this, name, code, params, config)
242
+ try {
243
+ console.log("STARTING INDEX", name)
244
+ await index.startIndex()
245
+ console.log("STARTED INDEX", name)
246
+ } catch(error) {
247
+ console.error("INDEX", name, "ERROR", error, "CODE:\n", code)
248
+ console.error("DELETING INDEX", name)
249
+ delete this.config.indexes[name]
250
+ this.indexesListObservable.remove(name)
251
+ if(this.onAutoRemoveIndex && config) this.onAutoRemoveIndex(name, config.uid)
252
+ await this.saveConfig(this.config)
253
+ throw error
254
+ }
255
+ this.indexes.set(name, index)
256
+ return index
257
+ }
230
258
  async index(name) {
231
259
  let index = this.indexes.get(name)
232
260
  if(!index) {
233
- const config = this.config.indexes[name]
234
- if(!config) throw new Error(`Index ${name} not found`)
235
- const code = config.code
236
- const params = config.parameters
237
- index = new Index(this, name, code, params, config)
238
- try {
239
- await index.startIndex()
240
- } catch(error) {
241
- console.error("INDEX", name, "ERROR", error, "CODE:\n", index.code)
242
- console.error("DELETING INDEX", name)
243
- delete this.config.indexes[name]
244
- this.indexesListObservable.remove(name)
245
- if(this.onAutoRemoveIndex) this.onAutoRemoveIndex(name, config.uid)
246
- await this.saveConfig(this.config)
247
- throw error
248
- }
261
+ index = this.openIndex(name)
249
262
  this.indexes.set(name, index)
250
263
  }
251
264
  return index
package/lib/Index.js CHANGED
@@ -63,6 +63,7 @@ class TableReader extends ChangeStream {
63
63
  this.opLogReader = opLogReader
64
64
  this.prefix = prefix
65
65
  this.table = table
66
+ Promise.resolve(this.table).then(t=> {if(!t) throw new Error("TABLE NOT FOUND!!!")})
66
67
  this.isLog = isLog
67
68
  this.objectReaders = new Map()
68
69
  this.rangeReaders = new Map()
@@ -35,6 +35,7 @@ const defaultContext = {
35
35
  return new ChangeStreamPipe()
36
36
  },
37
37
  'performance': require('perf_hooks').performance,
38
+ constructor: null
38
39
  }
39
40
 
40
41
  const filenameRE = /scriptFile:(\d+):(\d+)\)$/g
@@ -47,9 +48,9 @@ class ScriptContext {
47
48
  }
48
49
  this.context = vm.createContext({ ...defaultContext, ...context, ...userContext })
49
50
  /* vm.runInContext(`
50
- (function() {
51
- const allowed = ${JSON.stringify(nativeGlobals.concat(Object.keys(userContext)))}
52
- const keys = Object.getOwnPropertyNames(this)
51
+ (function() {
52
+ const allowed = ${JSON.stringify(nativeGlobals.concat(Object.keys(userContext)))}
53
+ const keys = Object.getOwnPropertyNames(this)
53
54
  keys.forEach((key) => {
54
55
  const item = this[key]
55
56
  if(!item) return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/db",
3
- "version": "0.3.62",
3
+ "version": "0.3.63",
4
4
  "description": "Database with observable data for live queries",
5
5
  "main": "index.js",
6
6
  "scripts": {