@live-change/db-server 0.4.86 → 0.5.5

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/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM node:15
1
+ FROM node:16
2
2
  USER root
3
3
 
4
4
  RUN npm -g config set user root
package/lib/Server.js CHANGED
@@ -17,6 +17,14 @@ const ReactiveDao = require("@live-change/dao")
17
17
 
18
18
  const Database = require('@live-change/db').Database
19
19
 
20
+ const debug = require('debug')('db-server')
21
+
22
+ const {
23
+ SsrServer,
24
+ createLoopbackDao
25
+ } = require("@live-change/server")
26
+ const packageInfo = require("@live-change/db-server/package.json");
27
+
20
28
  class DatabaseStore {
21
29
  constructor(path, backend, options) {
22
30
  this.path = path
@@ -91,71 +99,111 @@ class Server {
91
99
  }
92
100
  }
93
101
  createDao(session) {
94
- const scriptContext = new ScriptContext({
95
- /// TODO: script available routines
96
- console
97
- })
98
- if(this.config.master) {
99
- return new ReactiveDao(session, {
100
- remoteUrl: this.config.master,
101
- database: {
102
- type: 'local',
102
+ const packageInfo = require('@live-change/db-server/package.json')
103
+
104
+ const store = { /// Low level data access
105
+ type: 'local',
103
106
  source: new ReactiveDao.SimpleDao({
104
- methods: {
105
- ...(profileLog.started
106
- ? profileLog.profileFunctions(dbDao.remoteRequests(this))
107
- : dbDao.remoteRequests(this))
108
- },
109
- values: {
110
- ...dbDao.localReads(this, scriptContext)
111
- }
112
- })
107
+ methods: {
108
+ ...(profileLog.started
109
+ ? profileLog.profileFunctions(storeDao.localRequests(this))
110
+ : storeDao.localRequests(this))
113
111
  },
114
- /*store: { /// Low level data access
115
- type: 'remote',
116
- generator: ReactiveDao.ObservableList
117
- }*/
118
- store: { /// Low level data access
119
- type: 'local',
120
- source: new ReactiveDao.SimpleDao({
121
- methods: { // No write access to replica store
122
- },
123
- values: {
124
- ...storeDao.localReads(this)
125
- }
126
- })
112
+ values: {
113
+ ...storeDao.localReads(this)
127
114
  }
128
115
  })
129
- } else {
130
- return new ReactiveDao(session, {
131
- database: {
132
- type: 'local',
133
- source: new ReactiveDao.SimpleDao({
134
- methods: {
135
- ...(profileLog.started
136
- ? profileLog.profileFunctions(dbDao.localRequests(this, scriptContext))
137
- : dbDao.localRequests(this, scriptContext))
116
+ }
117
+
118
+ const version = {
119
+ type: 'local',
120
+ source: new ReactiveDao.SimpleDao({
121
+ methods: {},
122
+ values: {
123
+ version: {
124
+ observable() {
125
+ return new ReactiveDao.ObservableValue(packageInfo.version)
138
126
  },
139
- values: {
140
- ...dbDao.localReads(this, scriptContext)
127
+ async get() {
128
+ return packageInfo.version
141
129
  }
142
- })
143
- },
144
- store: { /// Low level data access
145
- type: 'local',
146
- source: new ReactiveDao.SimpleDao({
147
- methods: {
148
- ...(profileLog.started
149
- ? profileLog.profileFunctions(storeDao.localRequests(this))
150
- : storeDao.localRequests(this))
130
+ }
131
+ }
132
+ })
133
+ }
134
+
135
+ const emptyServices = {
136
+ observable(parameters) {
137
+ return ReactiveDao.ObservableList([])
138
+ },
139
+ async get(parameters) {
140
+ return []
141
+ }
142
+ }
143
+ const sessionInfo = {
144
+ client: { session: 'dbRoot' },
145
+ services: []
146
+ }
147
+ const metadata = {
148
+ type: "local",
149
+ source: new ReactiveDao.SimpleDao({
150
+ methods: {},
151
+ values: {
152
+ serviceNames: emptyServices,
153
+ serviceDefinitions: emptyServices,
154
+ api: {
155
+ observable(parameters) {
156
+ ReactiveDao.ObservableValue(sessionInfo)
151
157
  },
152
- values: {
153
- ...storeDao.localReads(this)
158
+ async get(parameters) {
159
+ return sessionInfo
154
160
  }
155
- })
161
+ }
156
162
  }
157
163
  })
158
164
  }
165
+
166
+ const scriptContext = new ScriptContext({
167
+ /// TODO: script available routines
168
+ console
169
+ })
170
+ let database
171
+ if(this.config.master) {
172
+ database = {
173
+ type: 'local',
174
+ source: new ReactiveDao.SimpleDao({
175
+ methods: {
176
+ ...(profileLog.started
177
+ ? profileLog.profileFunctions(dbDao.remoteRequests(this))
178
+ : dbDao.remoteRequests(this))
179
+ },
180
+ values: {
181
+ ...dbDao.localReads(this, scriptContext)
182
+ }
183
+ })
184
+ }
185
+
186
+ } else {
187
+ database = {
188
+ type: 'local',
189
+ source: new ReactiveDao.SimpleDao({
190
+ methods: {
191
+ ...(profileLog.started
192
+ ? profileLog.profileFunctions(dbDao.localRequests(this, scriptContext))
193
+ : dbDao.localRequests(this, scriptContext))
194
+ },
195
+ values: {
196
+ ...dbDao.localReads(this, scriptContext)
197
+ }
198
+ })
199
+ }
200
+ }
201
+ return new ReactiveDao(session, {
202
+ remoteUrl: this.config.master,
203
+ database,
204
+ serverDatabase: database,
205
+ store, version, metadata
206
+ })
159
207
  }
160
208
  async initialize(initOptions = {}) {
161
209
  if(!this.config.temporary) {
@@ -224,7 +272,7 @@ class Server {
224
272
  const dbPath = this.config.temporary ? 'memory' : path.resolve(this.config.dbRoot, dbName+'.db')
225
273
  let dbStore = this.databaseStores.get(dbName)
226
274
  if(!dbStore) {
227
- console.log("CREATE DB", dbPath, dbConfig.storage)
275
+ debug("CREATE DB", dbPath, dbConfig.storage)
228
276
  dbStore = new DatabaseStore(dbPath, this.backend, dbConfig.storage)
229
277
  this.databaseStores.set(dbName, dbStore)
230
278
  }
@@ -263,35 +311,55 @@ class Server {
263
311
  await this.metadataSavePromise
264
312
  }
265
313
 
266
- getHttp() {
267
- if(!this.http) {
314
+ async getHttp() {
315
+ if(this.http) return this.http
316
+ if(this.httpPromise) return this.httpPromise
317
+ this.httpPromise = (async () => {
268
318
  const app = express()
269
319
  const sockJsServer = sockjs.createServer({ prefix: '/api/sockjs' })
270
320
  sockJsServer.on('connection', (conn) => {
271
- console.log("SOCKJS connection")
321
+ debug("SOCKJS connection")
272
322
  this.apiServer.handleConnection(conn)
273
323
  })
274
324
  const server = http.createServer(app)
275
325
  let wsServer = new WebSocketServer({ httpServer: server, autoAcceptConnections: false })
276
326
  wsServer.on("request",(request) => {
277
- console.log("WS URI", request.httpRequest.url, "FROM", request.remoteAddress)
327
+ debug("WS URI", request.httpRequest.url, "FROM", request.remoteAddress)
278
328
  if(request.httpRequest.url != "/api/ws") return request.reject()
279
329
  let serverConnection = new ReactiveDaoWebsocketServer(request)
280
330
  this.apiServer.handleConnection(serverConnection)
281
331
  })
282
332
  sockJsServer.attach(server)
333
+
334
+ const ssrRoot = path.dirname(require.resolve("@live-change/db-admin/front/vite.config.js"))
335
+ const dev = await fs.promises.access(path.resolve(ssrRoot, './dist'), fs.constants.R_OK)
336
+ .then(r => false).catch(r => true)
337
+ if(dev) console.log("STARTING ADMIN IN DEV MODE!")
338
+ const manifest = dev ? null : require(path.resolve(ssrRoot, 'dist/client/ssr-manifest.json'))
339
+ const admin = new SsrServer(app, manifest, {
340
+ dev,
341
+ fastAuth: true,
342
+ root: ssrRoot,
343
+ daoFactory: async (credentials, ip) => {
344
+ return await createLoopbackDao(credentials, () => this.apiServer.daoFactory(credentials, ip))
345
+ }
346
+ })
347
+ admin.start()
348
+
283
349
  this.http = {
284
350
  app,
285
351
  sockJsServer,
286
352
  wsServer,
287
- server
353
+ server,
354
+ admin
288
355
  }
289
- }
290
- return this.http
356
+ return this.http
357
+ })()
358
+ return this.httpPromise
291
359
  }
292
360
 
293
- listen(...args) {
294
- this.getHttp().server.listen(...args)
361
+ async listen(...args) {
362
+ (await this.getHttp()).server.listen(...args)
295
363
  }
296
364
 
297
365
  async close() {
package/lib/backend.js CHANGED
@@ -108,7 +108,6 @@ function createBackend(config) {
108
108
  closeStore(store) {
109
109
  },
110
110
  async deleteStore(store) {
111
- await store.clear()
112
111
  }
113
112
  }
114
113
  } else if(config.backend == 'lmdb') {
package/lib/dbDao.js CHANGED
@@ -372,6 +372,10 @@ function localReads(server, scriptContext) {
372
372
  observable: () => server.databasesListObservable,
373
373
  get: async () => server.databasesListObservable.list
374
374
  },
375
+ databases: {
376
+ observable: () => server.databasesListObservable.next(list => list.map(dbName => ({ id: dbName }))),
377
+ get: async () => server.databasesListObservable.list.map(dbName => ({ id: dbName }))
378
+ },
375
379
  databaseConfig: {
376
380
  observable: (dbName) => {
377
381
  const db = server.databases.get(dbName)
@@ -420,6 +424,78 @@ function localReads(server, scriptContext) {
420
424
  return db.logsListObservable.list
421
425
  }
422
426
  },
427
+ tablesCount: {
428
+ observable: (dbName, tableName, id) => {
429
+ const db = server.databases.get(dbName)
430
+ if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
431
+ return db.tablesListObservable.next(tables => tables.length ?? 0)
432
+ },
433
+ get: async (dbName, tableName, id) =>{
434
+ const db = server.databases.get(dbName)
435
+ if(!db) throw new Error('databaseNotFound')
436
+ return db.tablesListObservable.list.length
437
+ }
438
+ },
439
+ indexesCount: {
440
+ observable: (dbName, tableName, id) => {
441
+ const db = server.databases.get(dbName)
442
+ if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
443
+ return db.indexesListObservable.next(tables => tables.length ?? 0)
444
+ },
445
+ get: async (dbName, tableName, id) =>{
446
+ const db = server.databases.get(dbName)
447
+ if(!db) throw new Error('databaseNotFound')
448
+ return db.indexesListObservable.list.length
449
+ }
450
+ },
451
+ logsCount: {
452
+ observable: (dbName, tableName, id) => {
453
+ const db = server.databases.get(dbName)
454
+ if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
455
+ return db.logsListObservable.next(tables => tables.length ?? 0)
456
+ },
457
+ get: async (dbName, tableName, id) =>{
458
+ const db = server.databases.get(dbName)
459
+ if(!db) throw new Error('databaseNotFound')
460
+ return db.logsListObservable.list.length
461
+ }
462
+ },
463
+ tables: {
464
+ observable: (dbName, tableName, id) => {
465
+ const db = server.databases.get(dbName)
466
+ if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
467
+ return db.tablesListObservable.next(list => list.map(dbName => ({ id: dbName })))
468
+ },
469
+ get: async (dbName, tableName, id) =>{
470
+ const db = server.databases.get(dbName)
471
+ if(!db) throw new Error('databaseNotFound')
472
+ return db.tablesListObservable.list.map(dbName => ({ id: dbName }))
473
+ }
474
+ },
475
+ indexes: {
476
+ observable: (dbName, indexName, id) => {
477
+ const db = server.databases.get(dbName)
478
+ if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
479
+ return db.indexesListObservable.next(list => list.map(dbName => ({ id: dbName })))
480
+ },
481
+ get: async (dbName, indexName, id) =>{
482
+ const db = server.databases.get(dbName)
483
+ if(!db) throw new Error('databaseNotFound')
484
+ return db.indexesListObservable.list.map(dbName => ({ id: dbName }))
485
+ }
486
+ },
487
+ logs: {
488
+ observable: (dbName, logName, id) => {
489
+ const db = server.databases.get(dbName)
490
+ if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
491
+ return db.logsListObservable.next(list => list.map(dbName => ({ id: dbName })))
492
+ },
493
+ get: async (dbName, logName, id) => {
494
+ const db = server.databases.get(dbName)
495
+ if(!db) throw new Error('databaseNotFound')
496
+ return db.logsListObservable.list.map(dbName => ({ id: dbName }))
497
+ }
498
+ },
423
499
  tableConfig: {
424
500
  observable: (dbName, tableName, id) => {
425
501
  const db = server.databases.get(dbName)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/db-server",
3
- "version": "0.4.86",
3
+ "version": "0.5.5",
4
4
  "description": "Database with observable data for live queries",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -12,7 +12,7 @@
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
15
- "url": "git+https://github.com/live-change/db-server.git"
15
+ "url": "git+https://github.com/live-change/live-change-db.git"
16
16
  },
17
17
  "author": {
18
18
  "email": "m8@em8.pl",
@@ -21,29 +21,24 @@
21
21
  },
22
22
  "license": "MIT",
23
23
  "bugs": {
24
- "url": "https://github.com/live-change/db-server/issues"
24
+ "url": "https://github.com/live-change/live-change-db/issues"
25
25
  },
26
- "homepage": "https://github.com/live-change/db-server",
26
+ "homepage": "https://github.com/live-change/live-change-db",
27
27
  "devDependencies": {
28
- "encoding-down": "^7.0.0",
29
- "level-rocksdb": "^4.0.0",
30
- "leveldown": "^6.0.0",
31
- "levelup": "^5.0.1",
32
- "memdown": "^6.0.0",
33
- "node-lmdb": "^0.8.0",
34
- "subleveldown": "^5.0.1",
35
- "tape": "^5.2.2"
28
+ "tape": "^5.3.2"
36
29
  },
37
30
  "dependencies": {
38
- "@live-change/dao": "^0.3.12",
39
- "@live-change/dao-sockjs": "^0.2.1",
40
- "@live-change/dao-websocket": "^0.3.2",
41
- "@live-change/db": "^0.3.64",
42
- "@live-change/db-client": "^0.4.71",
43
- "@live-change/db-store-level": "^0.1.15",
44
- "@live-change/db-store-lmdb": "^0.1.22",
45
- "@live-change/db-store-observable-db": "^0.1.3",
46
- "@live-change/db-store-rbtree": "^0.1.2",
31
+ "@live-change/dao": "0.4.6",
32
+ "@live-change/dao-sockjs": "0.4.5",
33
+ "@live-change/dao-websocket": "0.4.6",
34
+ "@live-change/db": "^0.5.5",
35
+ "@live-change/db-admin": "^0.5.5",
36
+ "@live-change/db-client": "^0.5.5",
37
+ "@live-change/db-store-level": "^0.5.5",
38
+ "@live-change/db-store-lmdb": "^0.5.5",
39
+ "@live-change/db-store-observable-db": "^0.5.5",
40
+ "@live-change/db-store-rbtree": "^0.5.5",
41
+ "@live-change/sockjs": "^0.4.0-rc.1",
47
42
  "express": "^4.17.1",
48
43
  "line-reader": "^0.4.0",
49
44
  "node-interval-tree": "^1.3.3",
@@ -51,8 +46,8 @@
51
46
  "rimraf": "^3.0.2",
52
47
  "rimraf-promise": "^2.0.0",
53
48
  "segfault-handler": "^1.3.0",
54
- "@live-change/sockjs": "^0.4.0-rc.1",
55
49
  "websocket": "^1.0.34",
56
- "yargs": "^17.0.1"
57
- }
50
+ "yargs": "^17.3.0"
51
+ },
52
+ "gitHead": "bffe88c30f0f0ad2ddbf10308ab8e70b93e4581e"
58
53
  }