@live-change/db-server 0.5.25 → 0.6.2

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.
Files changed (3) hide show
  1. package/lib/Server.js +52 -13
  2. package/lib/backend.js +14 -15
  3. package/package.json +12 -12
package/lib/Server.js CHANGED
@@ -19,37 +19,48 @@ const Database = require('@live-change/db').Database
19
19
 
20
20
  const debug = require('debug')('db-server')
21
21
 
22
- const packageInfo = require("@live-change/db-server/package.json");
23
-
24
22
  class DatabaseStore {
25
- constructor(path, backend, options) {
23
+ constructor(path, backends, options) {
26
24
  this.path = path
27
- this.backend = backend
28
- this.options = options
25
+ this.backends = backends
29
26
  this.stores = new Map()
30
- this.db = backend.createDb(path, options)
27
+
28
+ this.dbs = {}
29
+ this.dbs.default = this.backends.default.createDb(path, options)
31
30
  }
32
31
  close() {
33
- return this.backend.closeDb(this.db)
32
+ for(let key in this.dbs) {
33
+ return this.backends[key].closeDb(this.dbs[key])
34
+ }
34
35
  }
35
36
  delete() {
36
- return this.backend.deleteDb(this.db)
37
+ for(let key in this.dbs) {
38
+ return this.backends[key].deleteDb(this.dbs[key])
39
+ }
37
40
  }
38
41
  getStore(name, options = {}) {
39
42
  let store = this.stores.get(name)
40
43
  if(store) return store
41
- store = this.backend.createStore(this.db, name, options)
44
+ const backendName = options.backend ?? (options.memory ? 'memory' : 'default')
45
+ if(!this.backends[backendName]) {
46
+ throw new Error(`db ${path} backend ${backendName} not configured`)
47
+ }
48
+ if(!this.dbs[backendName]) {
49
+ this.dbs[backendName] = this.backends[backendName].createDb(this.path, options)
50
+ }
51
+ store = this.backends[backendName].createStore(this.dbs[backendName], name, options)
52
+ store.backendName = backendName
42
53
  this.stores.set(name, store)
43
54
  return store
44
55
  }
45
56
  closeStore(name) {
46
57
  let store = this.stores.get(name)
47
58
  if(!store) return;
48
- return this.backend.closeStore(store)
59
+ return this.backends[store.backendName].closeStore(store)
49
60
  }
50
61
  deleteStore(name) {
51
62
  let store = this.getStore(name)
52
- return this.backend.deleteStore(store)
63
+ return this.backends[store.backendName].deleteStore(store)
53
64
  }
54
65
  }
55
66
 
@@ -66,7 +77,32 @@ class Server {
66
77
 
67
78
  this.apiServer = new ReactiveDao.ReactiveServer((sessionId) => this.createDao(sessionId))
68
79
 
69
- this.backend = createBackend(config)
80
+ this.backends = {}
81
+ if(config.backend && !this.backends.default) { // backward compatibility
82
+ this.backends.default = createBackend({
83
+ name: config.backend,
84
+ url: config.backendUrl,
85
+ maxDbs: config.maxDbs,
86
+ maxDbSize: config.maxDbSize,
87
+ })
88
+ }
89
+ for(let backend of config.backends || []) {
90
+ if(typeof backend == 'string') {
91
+ backend = { name: backend }
92
+ }
93
+ this.backends[backend.name] = createBackend(backend)
94
+ if(!this.backends.default) {
95
+ this.backends.default = this.backends[backend.name]
96
+ }
97
+ }
98
+ if(!this.backends.default) {
99
+ throw new Error("No default backend configured")
100
+ }
101
+ if(!this.backends.memory) {
102
+ this.backends.memory = createBackend({
103
+ name: "memory"
104
+ })
105
+ }
70
106
 
71
107
  if(this.config.master) {
72
108
  this.masterDao = new ReactiveDao('app', {
@@ -269,7 +305,10 @@ class Server {
269
305
  let dbStore = this.databaseStores.get(dbName)
270
306
  if(!dbStore) {
271
307
  debug("CREATE DB", dbPath, dbConfig.storage)
272
- dbStore = new DatabaseStore(dbPath, this.backend, dbConfig.storage)
308
+ const backend = this.backends[dbConfig.backend?.name ?? dbConfig.backend ?? 'default']
309
+ dbStore = new DatabaseStore(dbPath, { ...this.backends, default: backend },
310
+ typeof dbConfig.backend == 'object' ? dbConfig.backend : dbConfig.storage
311
+ )
273
312
  this.databaseStores.set(dbName, dbStore)
274
313
  }
275
314
  const database = new Database(
package/lib/backend.js CHANGED
@@ -2,8 +2,8 @@ const fs = require('fs')
2
2
  const path = require('path')
3
3
  const rimraf = require("rimraf-promise")
4
4
 
5
- function createBackend(config) {
6
- if(config.backend == 'leveldb') {
5
+ function createBackend({ name, url, maxDbs, mapSize }) {
6
+ if(name == 'leveldb') {
7
7
  return {
8
8
  levelup: require('levelup'),
9
9
  leveldown: require('leveldown'),
@@ -32,7 +32,7 @@ function createBackend(config) {
32
32
  await store.clear()
33
33
  }
34
34
  }
35
- } else if(config.backend == 'rocksdb') {
35
+ } else if(name == 'rocksdb') {
36
36
  return {
37
37
  levelup: require('levelup'),
38
38
  rocksdb: require('level-rocksdb'),
@@ -61,7 +61,7 @@ function createBackend(config) {
61
61
  await store.clear()
62
62
  }
63
63
  }
64
- } else if(config.backend == 'memdown') {
64
+ } else if(name == 'memdown') {
65
65
  return {
66
66
  levelup: require('levelup'),
67
67
  memdown: require('memdown'),
@@ -90,7 +90,7 @@ function createBackend(config) {
90
90
  await store.clear()
91
91
  }
92
92
  }
93
- } else if(config.backend == 'mem') {
93
+ } else if(name == 'mem' || name == 'memory') {
94
94
  return {
95
95
  Store: require('@live-change/db-store-rbtree'),
96
96
  createDb(path, options) {
@@ -103,14 +103,14 @@ function createBackend(config) {
103
103
  async deleteDb(db) {
104
104
  },
105
105
  createStore(db, name, options) {
106
- return new this.Store()
106
+ return new this.Store(options)
107
107
  },
108
108
  closeStore(store) {
109
109
  },
110
110
  async deleteStore(store) {
111
111
  }
112
112
  }
113
- } else if(config.backend == 'lmdb') {
113
+ } else if(name == 'lmdb') {
114
114
  return {
115
115
  lmdb: require('node-lmdb'),
116
116
  Store: require('@live-change/db-store-lmdb'),
@@ -119,8 +119,8 @@ function createBackend(config) {
119
119
  const env = new this.lmdb.Env()
120
120
  const envConfig = {
121
121
  path: path,
122
- maxDbs: config.maxDbs || 1000,
123
- mapSize: config.mapSize || (10 * 1024 * 1024 * 1024),
122
+ maxDbs: maxDbs || 1000,
123
+ mapSize: mapSize || (10 * 1024 * 1024 * 1024),
124
124
  ...options
125
125
  }
126
126
  env.open(envConfig)
@@ -139,8 +139,7 @@ function createBackend(config) {
139
139
  db.openDbi({
140
140
  name,
141
141
  create: true
142
- })
143
- )
142
+ }), options)
144
143
  },
145
144
  closeStore(store) {
146
145
  store.lmdb.close()
@@ -149,9 +148,9 @@ function createBackend(config) {
149
148
  store.lmdb.drop()
150
149
  }
151
150
  }
152
- } else if(config.backend == 'observabledb') {
151
+ } else if(name == 'observabledb') {
153
152
  const Store = require('@live-change/db-store-observable-db')
154
- const connection = new Store.Connection(config.backendUrl || 'ws://localhost:3530/api/ws')
153
+ const connection = new Store.Connection(url || 'ws://localhost:3530/api/ws')
155
154
  return {
156
155
  Store,
157
156
  connection,
@@ -181,7 +180,7 @@ function createBackend(config) {
181
180
  if(err == 'exists') console.log("database", db, "store", name, "already exists")
182
181
  else console.error("CREATE STORE ERROR", err)
183
182
  })
184
- return new Store(connection, db, name)
183
+ return new Store(connection, db, name, options)
185
184
  },
186
185
  closeStore(store) {
187
186
  return store.close()
@@ -190,7 +189,7 @@ function createBackend(config) {
190
189
  return connection.deleteStore(store.databaseName, store.storeName)
191
190
  }
192
191
  }
193
- } else throw new Error("Unknown backend " + config.backend)
192
+ } else throw new Error("Unknown backend " + name)
194
193
  }
195
194
 
196
195
  module.exports = createBackend
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/db-server",
3
- "version": "0.5.25",
3
+ "version": "0.6.2",
4
4
  "description": "Database with observable data for live queries",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -28,16 +28,16 @@
28
28
  "tape": "^5.3.2"
29
29
  },
30
30
  "dependencies": {
31
- "@live-change/dao": "^0.5.10",
32
- "@live-change/dao-sockjs": "^0.5.10",
33
- "@live-change/dao-websocket": "0.5.10",
34
- "@live-change/db": "^0.5.25",
35
- "@live-change/db-admin": "^0.5.25",
36
- "@live-change/db-client": "^0.5.25",
37
- "@live-change/db-store-level": "^0.5.25",
38
- "@live-change/db-store-lmdb": "^0.5.25",
39
- "@live-change/db-store-observable-db": "^0.5.25",
40
- "@live-change/db-store-rbtree": "^0.5.25",
31
+ "@live-change/dao": "0.5.14",
32
+ "@live-change/dao-sockjs": "0.5.14",
33
+ "@live-change/dao-websocket": "0.5.14",
34
+ "@live-change/db": "^0.6.2",
35
+ "@live-change/db-admin": "^0.6.2",
36
+ "@live-change/db-client": "^0.6.2",
37
+ "@live-change/db-store-level": "^0.6.2",
38
+ "@live-change/db-store-lmdb": "^0.6.2",
39
+ "@live-change/db-store-observable-db": "^0.6.2",
40
+ "@live-change/db-store-rbtree": "^0.6.2",
41
41
  "@live-change/sockjs": "0.4.1",
42
42
  "express": "^4.18.1",
43
43
  "line-reader": "^0.4.0",
@@ -49,5 +49,5 @@
49
49
  "websocket": "^1.0.34",
50
50
  "yargs": "^17.5.1"
51
51
  },
52
- "gitHead": "dac1ac23bd1a4e1ba83e674d5493ab6c832f4462"
52
+ "gitHead": "9a1b104864c08f3e35b009f191889e3308e3eeb0"
53
53
  }