@live-change/db-server 0.5.25 → 0.6.1

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 +46 -13
  2. package/lib/backend.js +11 -11
  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,26 @@ 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
+ this.backends[backend.name] = createBackend(backend)
91
+ }
92
+ if(!this.backends.default) {
93
+ throw new Error("No default backend configured")
94
+ }
95
+ if(!this.backends.memory) {
96
+ this.backends.memory = createBackend({
97
+ name: "mem"
98
+ })
99
+ }
70
100
 
71
101
  if(this.config.master) {
72
102
  this.masterDao = new ReactiveDao('app', {
@@ -269,7 +299,10 @@ class Server {
269
299
  let dbStore = this.databaseStores.get(dbName)
270
300
  if(!dbStore) {
271
301
  debug("CREATE DB", dbPath, dbConfig.storage)
272
- dbStore = new DatabaseStore(dbPath, this.backend, dbConfig.storage)
302
+ const backend = this.backends[dbConfig.backend?.name ?? dbConfig.backend ?? 'default']
303
+ dbStore = new DatabaseStore(dbPath, { ...this.backends, default: backend },
304
+ typeof dbConfig.backend == 'object' ? dbConfig.backend : dbConfig.storage
305
+ )
273
306
  this.databaseStores.set(dbName, dbStore)
274
307
  }
275
308
  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') {
94
94
  return {
95
95
  Store: require('@live-change/db-store-rbtree'),
96
96
  createDb(path, options) {
@@ -110,7 +110,7 @@ function createBackend(config) {
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)
@@ -149,9 +149,9 @@ function createBackend(config) {
149
149
  store.lmdb.drop()
150
150
  }
151
151
  }
152
- } else if(config.backend == 'observabledb') {
152
+ } else if(name == 'observabledb') {
153
153
  const Store = require('@live-change/db-store-observable-db')
154
- const connection = new Store.Connection(config.backendUrl || 'ws://localhost:3530/api/ws')
154
+ const connection = new Store.Connection(url || 'ws://localhost:3530/api/ws')
155
155
  return {
156
156
  Store,
157
157
  connection,
@@ -190,7 +190,7 @@ function createBackend(config) {
190
190
  return connection.deleteStore(store.databaseName, store.storeName)
191
191
  }
192
192
  }
193
- } else throw new Error("Unknown backend " + config.backend)
193
+ } else throw new Error("Unknown backend " + name)
194
194
  }
195
195
 
196
196
  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.1",
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.1",
35
+ "@live-change/db-admin": "^0.6.1",
36
+ "@live-change/db-client": "^0.6.1",
37
+ "@live-change/db-store-level": "^0.6.1",
38
+ "@live-change/db-store-lmdb": "^0.6.1",
39
+ "@live-change/db-store-observable-db": "^0.6.1",
40
+ "@live-change/db-store-rbtree": "^0.6.1",
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": "f458425c609deeb9d1f38c15a3aa37759629a67e"
53
53
  }