@live-change/db-web 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.
- package/lib/Server.js +55 -14
- package/lib/backend.js +78 -4
- package/lib/dbDao.js +271 -29
- package/package.json +7 -5
package/lib/Server.js
CHANGED
|
@@ -8,34 +8,47 @@ const ReactiveDao = require("@live-change/dao")
|
|
|
8
8
|
const Database = require('@live-change/db').Database
|
|
9
9
|
|
|
10
10
|
class DatabaseStore {
|
|
11
|
-
constructor(path,
|
|
11
|
+
constructor(path, backends, options) {
|
|
12
12
|
this.path = path
|
|
13
|
-
this.
|
|
13
|
+
this.backends = backends
|
|
14
14
|
this.stores = new Map()
|
|
15
15
|
|
|
16
|
-
this.
|
|
16
|
+
this.dbs = {}
|
|
17
|
+
this.dbs.default = this.backends.default.createDb(path, options)
|
|
17
18
|
}
|
|
18
19
|
close() {
|
|
19
|
-
|
|
20
|
+
for(let key in this.dbs) {
|
|
21
|
+
return this.backends[key].closeDb(this.dbs[key])
|
|
22
|
+
}
|
|
20
23
|
}
|
|
21
24
|
delete() {
|
|
22
|
-
|
|
25
|
+
for(let key in this.dbs) {
|
|
26
|
+
return this.backends[key].deleteDb(this.dbs[key])
|
|
27
|
+
}
|
|
23
28
|
}
|
|
24
29
|
getStore(name, options = {}) {
|
|
25
30
|
let store = this.stores.get(name)
|
|
26
31
|
if(store) return store
|
|
27
|
-
|
|
32
|
+
const backendName = options.backend ?? (options.memory ? 'memory' : 'default')
|
|
33
|
+
if(!this.backends[backendName]) {
|
|
34
|
+
throw new Error(`db ${path} backend ${backendName} not configured`)
|
|
35
|
+
}
|
|
36
|
+
if(!this.dbs[backendName]) {
|
|
37
|
+
this.dbs[backendName] = this.backends[backendName].createDb(this.path, options)
|
|
38
|
+
}
|
|
39
|
+
store = this.backends[backendName].createStore(this.dbs[backendName], name, options)
|
|
40
|
+
store.backendName = backendName
|
|
28
41
|
this.stores.set(name, store)
|
|
29
42
|
return store
|
|
30
43
|
}
|
|
31
44
|
closeStore(name) {
|
|
32
45
|
let store = this.stores.get(name)
|
|
33
46
|
if(!store) return;
|
|
34
|
-
return this.
|
|
47
|
+
return this.backends[store.backendName].closeStore(store)
|
|
35
48
|
}
|
|
36
49
|
deleteStore(name) {
|
|
37
50
|
let store = this.getStore(name)
|
|
38
|
-
return this.
|
|
51
|
+
return this.backends[store.backendName].deleteStore(store)
|
|
39
52
|
}
|
|
40
53
|
}
|
|
41
54
|
|
|
@@ -48,9 +61,35 @@ class Server {
|
|
|
48
61
|
|
|
49
62
|
this.databasesListObservable = new ReactiveDao.ObservableList([])
|
|
50
63
|
|
|
51
|
-
this.
|
|
64
|
+
this.backends = {}
|
|
65
|
+
if(config.backend && !this.backends.default) { // backward compatibility
|
|
66
|
+
this.backends.default = createBackend({
|
|
67
|
+
name: config.backend,
|
|
68
|
+
url: config.backendUrl,
|
|
69
|
+
maxDbs: config.maxDbs,
|
|
70
|
+
maxDbSize: config.maxDbSize,
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
for(let backend of config.backends || []) {
|
|
74
|
+
if(typeof backend == 'string') {
|
|
75
|
+
backend = { name: backend }
|
|
76
|
+
}
|
|
77
|
+
this.backends[backend.name] = createBackend(backend)
|
|
78
|
+
if(!this.backends.default) {
|
|
79
|
+
this.backends.default = this.backends[backend.name]
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if(!this.backends.default) {
|
|
83
|
+
throw new Error("No default backend configured")
|
|
84
|
+
}
|
|
85
|
+
if(!this.backends.memory) {
|
|
86
|
+
this.backends.memory = createBackend({
|
|
87
|
+
name: "memory"
|
|
88
|
+
})
|
|
89
|
+
}
|
|
52
90
|
|
|
53
|
-
this.
|
|
91
|
+
this.metadataDatabase = this.backends.default.createDb('_db_metadata', {})
|
|
92
|
+
this.metadataStore = this.backends.default.createStore(this.metadataDatabase, 'metadata', {})
|
|
54
93
|
}
|
|
55
94
|
createDao(session) {
|
|
56
95
|
return new ReactiveDao(session, {
|
|
@@ -92,8 +131,7 @@ class Server {
|
|
|
92
131
|
if(initOptions.metadata) {
|
|
93
132
|
this.metadata = initOptions.metadata
|
|
94
133
|
} else {
|
|
95
|
-
|
|
96
|
-
this.metadata = jsonStr && JSON.parse(jsonStr)
|
|
134
|
+
this.metadata = await this.metadataStore.objectGet('metadata')
|
|
97
135
|
}
|
|
98
136
|
if(!this.metadata) {
|
|
99
137
|
this.metadata = {
|
|
@@ -116,7 +154,10 @@ class Server {
|
|
|
116
154
|
const dbPath = `${this.config.dbPrefix || ''}_lcdb`
|
|
117
155
|
let dbStore = this.databaseStores.get(dbName)
|
|
118
156
|
if(!dbStore) {
|
|
119
|
-
|
|
157
|
+
const backend = this.backends[dbConfig.backend?.name ?? dbConfig.backend ?? 'default']
|
|
158
|
+
dbStore = new DatabaseStore(dbPath, { ...this.backends, default: backend },
|
|
159
|
+
typeof dbConfig.backend == 'object' ? dbConfig.backend : dbConfig.storage
|
|
160
|
+
)
|
|
120
161
|
this.databaseStores.set(dbName, dbStore)
|
|
121
162
|
}
|
|
122
163
|
const database = new Database(
|
|
@@ -138,7 +179,7 @@ class Server {
|
|
|
138
179
|
}
|
|
139
180
|
|
|
140
181
|
async saveMetadata() {
|
|
141
|
-
|
|
182
|
+
await this.metadataStore.put({ ...this.metadata, id: 'metadata' })
|
|
142
183
|
}
|
|
143
184
|
|
|
144
185
|
async close() {
|
package/lib/backend.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
function createBackend(config) {
|
|
2
|
-
|
|
2
|
+
console.log("CREATE BACKEND", config)
|
|
3
|
+
if(config.name == 'mem' || config.name == 'memory') {
|
|
3
4
|
return {
|
|
4
5
|
Store: require('@live-change/db-store-rbtree'),
|
|
5
6
|
createDb(path, options) {
|
|
@@ -12,10 +13,9 @@ function createBackend(config) {
|
|
|
12
13
|
},
|
|
13
14
|
async deleteDb(db) {
|
|
14
15
|
db.close()
|
|
15
|
-
await rimraf(db.path)
|
|
16
16
|
},
|
|
17
17
|
createStore(db, name, options) {
|
|
18
|
-
return new this.Store()
|
|
18
|
+
return new this.Store(options)
|
|
19
19
|
},
|
|
20
20
|
closeStore(store) {
|
|
21
21
|
},
|
|
@@ -23,7 +23,81 @@ function createBackend(config) {
|
|
|
23
23
|
await store.clear()
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
|
-
}
|
|
26
|
+
} if(config.name == 'indexeddb') {
|
|
27
|
+
return {
|
|
28
|
+
Store: require('@live-change/db-store-indexeddb'),
|
|
29
|
+
createDb(path, options) {
|
|
30
|
+
const db = {}
|
|
31
|
+
db.path = path
|
|
32
|
+
return db
|
|
33
|
+
},
|
|
34
|
+
closeDb(db) {
|
|
35
|
+
db.close()
|
|
36
|
+
},
|
|
37
|
+
async deleteDb(db) {
|
|
38
|
+
db.close()
|
|
39
|
+
db.deleteDb()
|
|
40
|
+
},
|
|
41
|
+
createStore(db, name, options) {
|
|
42
|
+
return new this.Store(db.path, name, options)
|
|
43
|
+
},
|
|
44
|
+
closeStore(store) {
|
|
45
|
+
},
|
|
46
|
+
async deleteStore(store) {
|
|
47
|
+
await store.clear()
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} if(config.name == 'local') {
|
|
51
|
+
return {
|
|
52
|
+
Store: require('@live-change/db-store-localstorage'),
|
|
53
|
+
createDb(path, options) {
|
|
54
|
+
const db = {}
|
|
55
|
+
db.path = path
|
|
56
|
+
return db
|
|
57
|
+
},
|
|
58
|
+
closeDb(db) {
|
|
59
|
+
db.close()
|
|
60
|
+
},
|
|
61
|
+
async deleteDb(db) {
|
|
62
|
+
db.close()
|
|
63
|
+
db.deleteDb()
|
|
64
|
+
},
|
|
65
|
+
createStore(db, name, options) {
|
|
66
|
+
return new this.Store(db.path, name, 'local', options)
|
|
67
|
+
},
|
|
68
|
+
closeStore(store) {
|
|
69
|
+
},
|
|
70
|
+
async deleteStore(store) {
|
|
71
|
+
await store.clear()
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
} if(config.name == 'session') {
|
|
75
|
+
return {
|
|
76
|
+
Store: require('@live-change/db-store-localstorage'),
|
|
77
|
+
createDb(path, options) {
|
|
78
|
+
const db = {}
|
|
79
|
+
db.path = path
|
|
80
|
+
return db
|
|
81
|
+
},
|
|
82
|
+
closeDb(db) {
|
|
83
|
+
db.close()
|
|
84
|
+
},
|
|
85
|
+
async deleteDb(db) {
|
|
86
|
+
db.close()
|
|
87
|
+
db.deleteDb()
|
|
88
|
+
},
|
|
89
|
+
createStore(db, name, options) {
|
|
90
|
+
return new this.Store(db.path, name, 'session', options)
|
|
91
|
+
},
|
|
92
|
+
closeStore(store) {
|
|
93
|
+
},
|
|
94
|
+
async deleteStore(store) {
|
|
95
|
+
await store.clear()
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
throw new Error("Unknown backend " + config.name)
|
|
100
|
+
}
|
|
27
101
|
}
|
|
28
102
|
|
|
29
103
|
module.exports = createBackend
|
package/lib/dbDao.js
CHANGED
|
@@ -71,11 +71,25 @@ function localRequests(server, scriptContext) {
|
|
|
71
71
|
if(dbName == 'system') throw new Error("system database is not writable")
|
|
72
72
|
const db = server.databases.get(dbName)
|
|
73
73
|
if(!db) throw new Error('databaseNotFound')
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
74
|
+
if(tableName[tableName.length - 1] == "*") {
|
|
75
|
+
const list = db.tablesListObservable.list.slice()
|
|
76
|
+
for(const foundTableName of list) {
|
|
77
|
+
const base = tableName.slice(0, -1)
|
|
78
|
+
if(foundTableName.slice(0, base.length) != base) continue
|
|
79
|
+
const table = db.table(foundTableName)
|
|
80
|
+
if(!table) throw new Error("tableNotFound")
|
|
81
|
+
const uid = table.configObservable.value.uid
|
|
82
|
+
await db.deleteTable(foundTableName)
|
|
83
|
+
await server.databases.get('system').table(dbName + '_tables').delete(uid)
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
const table = db.table(tableName)
|
|
87
|
+
if(!table) throw new Error("tableNotFound")
|
|
88
|
+
const uid = table.configObservable.value.uid
|
|
89
|
+
await db.deleteTable(tableName)
|
|
90
|
+
await server.databases.get('system').table(dbName + '_tables').delete(uid)
|
|
91
|
+
}
|
|
92
|
+
return 'ok'
|
|
79
93
|
},
|
|
80
94
|
renameTable: async (dbName, tableName, newTableName) => {
|
|
81
95
|
if(dbName == 'system') throw new Error("system database is not writable")
|
|
@@ -105,11 +119,24 @@ function localRequests(server, scriptContext) {
|
|
|
105
119
|
if(dbName == 'system') throw new Error("system database is not writable")
|
|
106
120
|
const db = server.databases.get(dbName)
|
|
107
121
|
if(!db) throw new Error('databaseNotFound')
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
122
|
+
if(indexName[indexName.length - 1] == "*") {
|
|
123
|
+
const base = indexName.slice(0, -1)
|
|
124
|
+
const list = db.indexesListObservable.list.slice()
|
|
125
|
+
for(const foundIndexName of list) {
|
|
126
|
+
if(foundIndexName.slice(0, base.length) != base) continue
|
|
127
|
+
const index = await db.index(foundIndexName)
|
|
128
|
+
if(!index) throw new Error("indexNotFound")
|
|
129
|
+
const uid = index.configObservable.value.uid
|
|
130
|
+
await db.deleteIndex(foundIndexName)
|
|
131
|
+
await server.databases.get('system').table(dbName + '_indexes').delete(uid)
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
const index = await db.index(indexName)
|
|
135
|
+
if(!index) throw new Error("indexNotFound")
|
|
136
|
+
const uid = index.configObservable.value.uid
|
|
137
|
+
await db.deleteIndex(indexName)
|
|
138
|
+
await server.databases.get('system').table(dbName + '_indexes').delete(uid)
|
|
139
|
+
}
|
|
113
140
|
return 'ok'
|
|
114
141
|
},
|
|
115
142
|
renameIndex: async (dbName, indexName, newIndexName) => {
|
|
@@ -191,6 +218,14 @@ function localRequests(server, scriptContext) {
|
|
|
191
218
|
if(!log) throw new Error("logNotFound")
|
|
192
219
|
return log.put(object)
|
|
193
220
|
},
|
|
221
|
+
putOldLog: (dbName, logName, object) => {
|
|
222
|
+
if(dbName == 'system') throw new Error("system database is not writable")
|
|
223
|
+
const db = server.databases.get(dbName)
|
|
224
|
+
if(!db) throw new Error('databaseNotFound')
|
|
225
|
+
const log = db.log(logName)
|
|
226
|
+
if(!log) throw new Error("logNotFound")
|
|
227
|
+
return log.putOld(object)
|
|
228
|
+
},
|
|
194
229
|
query: (dbName, code, params) => {
|
|
195
230
|
if(dbName == 'system') throw new Error("system database is not writable")
|
|
196
231
|
if(!dbName) throw new Error("databaseNameRequired")
|
|
@@ -202,6 +237,133 @@ function localRequests(server, scriptContext) {
|
|
|
202
237
|
}
|
|
203
238
|
}
|
|
204
239
|
|
|
240
|
+
function remoteRequests(server) {
|
|
241
|
+
return {
|
|
242
|
+
createDatabase: async (dbName, options = {}) => {
|
|
243
|
+
if(server.metadata.databases[dbName]) throw new Error("databaseAlreadyExists")
|
|
244
|
+
return server.masterDao.request(['database', 'createDatabase'], dbName, options)
|
|
245
|
+
},
|
|
246
|
+
deleteDatabase: async (dbName) => {
|
|
247
|
+
if(!server.metadata.databases[dbName]) throw new Error("databaseNotFound")
|
|
248
|
+
return server.masterDao.request(['database', 'deleteDatabase'], dbName, options)
|
|
249
|
+
},
|
|
250
|
+
clearDatabaseOpLogs: async (dbName, lastTimestamp, limit) => {
|
|
251
|
+
const db = server.databases.get(dbName)
|
|
252
|
+
if(!db) throw new Error('databaseNotFound')
|
|
253
|
+
if(typeof lastTimestamp == 'string') lastTimestamp = new Date(lastTimestamp)
|
|
254
|
+
return server.masterDao.request(['database', 'clearDatabaseOpLogs'], dbName, lastTimestamp, limit)
|
|
255
|
+
},
|
|
256
|
+
clearTableOpLog: async (dbName, tableName, lastTimestamp, limit) => {
|
|
257
|
+
const db = server.databases.get(dbName)
|
|
258
|
+
if(!db) throw new Error('databaseNotFound')
|
|
259
|
+
const table = db.table(tableName)
|
|
260
|
+
if(!table) throw new Error("tableNotFound")
|
|
261
|
+
if(typeof lastTimestamp == 'string') lastTimestamp = new Date(lastTimestamp)
|
|
262
|
+
return server.masterDao.request(['database', 'clearTableOpLog'], dbName, tableName, lastTimestamp, limit)
|
|
263
|
+
},
|
|
264
|
+
clearIndexOpLog: async (dbName, indexName, lastTimestamp, limit) => {
|
|
265
|
+
const db = server.databases.get(dbName)
|
|
266
|
+
if(!db) throw new Error('databaseNotFound')
|
|
267
|
+
const index = db.table(indexName)
|
|
268
|
+
if(!index) throw new Error("indexNotFound")
|
|
269
|
+
if(typeof lastTimestamp == 'string') lastTimestamp = new Date(lastTimestamp)
|
|
270
|
+
return server.masterDao.request(['database', 'clearIndexOpLog'], dbName, indexName, lastTimestamp, limit)
|
|
271
|
+
},
|
|
272
|
+
createTable: async (dbName, tableName, options = {}) => {
|
|
273
|
+
const db = server.databases.get(dbName)
|
|
274
|
+
if(!db) throw new Error('databaseNotFound')
|
|
275
|
+
return server.masterDao.request(['database', 'createTable'], dbName, tableName, options)
|
|
276
|
+
},
|
|
277
|
+
deleteTable: async (dbName, tableName, options) => {
|
|
278
|
+
const db = server.databases.get(dbName)
|
|
279
|
+
if(!db) throw new Error('databaseNotFound')
|
|
280
|
+
return server.masterDao.request(['database', 'deleteTable'], dbName, tableName, options)
|
|
281
|
+
},
|
|
282
|
+
renameTable: async (dbName, tableName, newTableName) => {
|
|
283
|
+
const db = server.databases.get(dbName)
|
|
284
|
+
if(!db) throw new Error('databaseNotFound')
|
|
285
|
+
const table = db.table(tableName)
|
|
286
|
+
if(!table) throw new Error("tableNotFound")
|
|
287
|
+
return server.masterDao.request(['database', 'renameTable'], dbName, tableName, newTableName)
|
|
288
|
+
},
|
|
289
|
+
createIndex: async (dbName, indexName, code, params, options = {}) => {
|
|
290
|
+
const db = server.databases.get(dbName)
|
|
291
|
+
if(!db) throw new Error('databaseNotFound')
|
|
292
|
+
return server.masterDao.request(['database', 'createIndex'], dbName, indexName, code, params, options )
|
|
293
|
+
},
|
|
294
|
+
deleteIndex: async (dbName, indexName, options) => {
|
|
295
|
+
const db = server.databases.get(dbName)
|
|
296
|
+
if(!db) throw new Error('databaseNotFound')
|
|
297
|
+
return server.masterDao.request(['database', 'deleteIndex'], dbName, indexName, options )
|
|
298
|
+
},
|
|
299
|
+
renameIndex: async (dbName, indexName, newIndexName) => {
|
|
300
|
+
const db = server.databases.get(dbName)
|
|
301
|
+
if(!db) throw new Error('databaseNotFound')
|
|
302
|
+
const index = db.index(indexName)
|
|
303
|
+
if(!index) throw new Error("indexNotFound")
|
|
304
|
+
return server.masterDao.request(['database', 'renameIndex'], dbName, indexName, newIndexName )
|
|
305
|
+
},
|
|
306
|
+
createLog: async (dbName, logName, options = {}) => {
|
|
307
|
+
const db = server.databases.get(dbName)
|
|
308
|
+
if(!db) throw new Error('databaseNotFound')
|
|
309
|
+
return server.masterDao.request(['database', 'createLog'], dbName, logName, options )
|
|
310
|
+
},
|
|
311
|
+
deleteLog: async (dbName, logName, options) => {
|
|
312
|
+
const db = server.databases.get(dbName)
|
|
313
|
+
if(!db) throw new Error('databaseNotFound')
|
|
314
|
+
await db.deleteLog(logName)
|
|
315
|
+
return server.masterDao.request(['database', 'deleteLog'], dbName, logName, options )
|
|
316
|
+
},
|
|
317
|
+
renameLog: async (dbName, logName, newLogName) => {
|
|
318
|
+
const db = server.databases.get(dbName)
|
|
319
|
+
if(!db) throw new Error('databaseNotFound')
|
|
320
|
+
const log = db.log(logName)
|
|
321
|
+
if(!log) throw new Error("logNotFound")
|
|
322
|
+
return server.masterDao.request(['database', 'renameLog'], dbName, logName, newLogName )
|
|
323
|
+
},
|
|
324
|
+
put: (dbName, tableName, object) => {
|
|
325
|
+
const db = server.databases.get(dbName)
|
|
326
|
+
if(!db) throw new Error('databaseNotFound')
|
|
327
|
+
const table = db.table(tableName)
|
|
328
|
+
if(!table) throw new Error("tableNotFound")
|
|
329
|
+
return server.masterDao.request(['database', 'put'], dbName, tableName, object )
|
|
330
|
+
},
|
|
331
|
+
delete: (dbName, tableName, id) => {
|
|
332
|
+
const db = server.databases.get(dbName)
|
|
333
|
+
if(!db) throw new Error('databaseNotFound')
|
|
334
|
+
const table = db.table(tableName)
|
|
335
|
+
if(!table) throw new Error("tableNotFound")
|
|
336
|
+
return server.masterDao.request(['database', 'delete'], dbName, tableName, id )
|
|
337
|
+
},
|
|
338
|
+
update: (dbName, tableName, id, operations, options) => {
|
|
339
|
+
const db = server.databases.get(dbName)
|
|
340
|
+
if(!db) throw new Error('databaseNotFound')
|
|
341
|
+
const table = db.table(tableName)
|
|
342
|
+
if(!table) throw new Error("tableNotFound")
|
|
343
|
+
return server.masterDao.request(['database', 'update'], dbName, tableName, id, operations, options )
|
|
344
|
+
},
|
|
345
|
+
putLog: (dbName, logName, object) => {
|
|
346
|
+
const db = server.databases.get(dbName)
|
|
347
|
+
if(!db) throw new Error('databaseNotFound')
|
|
348
|
+
const log = db.log(logName)
|
|
349
|
+
if(!log) throw new Error("logNotFound")
|
|
350
|
+
return server.masterDao.request(['database', 'putLog'], dbName, logName, object )
|
|
351
|
+
},
|
|
352
|
+
putOldLog: (dbName, logName, object) => {
|
|
353
|
+
const db = server.databases.get(dbName)
|
|
354
|
+
if(!db) throw new Error('databaseNotFound')
|
|
355
|
+
const log = db.log(logName)
|
|
356
|
+
if(!log) throw new Error("logNotFound")
|
|
357
|
+
return server.masterDao.request(['database', 'putOldLog'], dbName, logName, object )
|
|
358
|
+
},
|
|
359
|
+
query: (dbName, code, params) => {
|
|
360
|
+
if(!dbName) throw new Error("databaseNameRequired")
|
|
361
|
+
const db = server.databases.get(dbName)
|
|
362
|
+
if(!db) throw new Error('databaseNotFound')
|
|
363
|
+
return server.masterDao.request(['database', 'query'], dbName, code, params )
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
205
367
|
|
|
206
368
|
function localReads(server, scriptContext) {
|
|
207
369
|
return {
|
|
@@ -350,33 +512,33 @@ function localReads(server, scriptContext) {
|
|
|
350
512
|
}
|
|
351
513
|
},
|
|
352
514
|
indexConfig: {
|
|
353
|
-
observable: (dbName, indexName, id) => {
|
|
515
|
+
observable: async (dbName, indexName, id) => {
|
|
354
516
|
const db = server.databases.get(dbName)
|
|
355
517
|
if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
|
|
356
|
-
const index = db.index(indexName)
|
|
518
|
+
const index = await db.index(indexName)
|
|
357
519
|
if(!index) return new ReactiveDao.ObservableError('indexNotFound')
|
|
358
520
|
return index.configObservable
|
|
359
521
|
},
|
|
360
522
|
get: async (dbName, indexName, id) =>{
|
|
361
523
|
const db = server.databases.get(dbName)
|
|
362
524
|
if(!db) throw new Error('databaseNotFound')
|
|
363
|
-
const index = db.index(indexName)
|
|
525
|
+
const index = await db.index(indexName)
|
|
364
526
|
if(!index) throw new Error("indexNotFound")
|
|
365
527
|
return index.configObservable.value
|
|
366
528
|
}
|
|
367
529
|
},
|
|
368
530
|
indexCode: {
|
|
369
|
-
observable: (dbName, indexName, id) => {
|
|
531
|
+
observable: async (dbName, indexName, id) => {
|
|
370
532
|
const db = server.databases.get(dbName)
|
|
371
533
|
if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
|
|
372
|
-
const index = db.index(indexName)
|
|
534
|
+
const index = await db.index(indexName)
|
|
373
535
|
if(!index) return new ReactiveDao.ObservableError('indexNotFound')
|
|
374
536
|
return index.codeObservable
|
|
375
537
|
},
|
|
376
538
|
get: async (dbName, indexName, id) =>{
|
|
377
539
|
const db = server.databases.get(dbName)
|
|
378
540
|
if(!db) throw new Error('databaseNotFound')
|
|
379
|
-
const index = db.index(indexName)
|
|
541
|
+
const index = await db.index(indexName)
|
|
380
542
|
if(!index) throw new Error("indexNotFound")
|
|
381
543
|
return index.codeObservable.value
|
|
382
544
|
}
|
|
@@ -431,6 +593,22 @@ function localReads(server, scriptContext) {
|
|
|
431
593
|
return table.rangeGet(range)
|
|
432
594
|
}
|
|
433
595
|
},
|
|
596
|
+
tableCount: {
|
|
597
|
+
observable: (dbName, tableName, range) => {
|
|
598
|
+
const db = server.databases.get(dbName)
|
|
599
|
+
if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
|
|
600
|
+
const table = db.table(tableName)
|
|
601
|
+
if(!table) return new ReactiveDao.ObservableError('tableNotFound')
|
|
602
|
+
return table.countObservable(range)
|
|
603
|
+
},
|
|
604
|
+
get: async (dbName, tableName, range) => {
|
|
605
|
+
const db = server.databases.get(dbName)
|
|
606
|
+
if(!db) throw new Error('databaseNotFound')
|
|
607
|
+
const table = db.table(tableName)
|
|
608
|
+
if(!table) throw new Error("tableNotFound")
|
|
609
|
+
return table.countGet(range)
|
|
610
|
+
}
|
|
611
|
+
},
|
|
434
612
|
tableOpLogObject: {
|
|
435
613
|
observable: (dbName, tableName, id) => {
|
|
436
614
|
const db = server.databases.get(dbName)
|
|
@@ -463,6 +641,22 @@ function localReads(server, scriptContext) {
|
|
|
463
641
|
return table.opLog.rangeGet(range)
|
|
464
642
|
}
|
|
465
643
|
},
|
|
644
|
+
tableOpLogCount: {
|
|
645
|
+
observable: (dbName, tableName, range) => {
|
|
646
|
+
const db = server.databases.get(dbName)
|
|
647
|
+
if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
|
|
648
|
+
const table = db.table(tableName)
|
|
649
|
+
if(!table) return new ReactiveDao.ObservableError('tableNotFound')
|
|
650
|
+
return table.opLog.countObservable(range)
|
|
651
|
+
},
|
|
652
|
+
get: async (dbName, tableName, range) => {
|
|
653
|
+
const db = server.databases.get(dbName)
|
|
654
|
+
if(!db) throw new Error('databaseNotFound')
|
|
655
|
+
const table = db.table(tableName)
|
|
656
|
+
if(!table) throw new Error("tableNotFound")
|
|
657
|
+
return table.opLog.countGet(range)
|
|
658
|
+
}
|
|
659
|
+
},
|
|
466
660
|
indexObject: {
|
|
467
661
|
observable: async (dbName, indexName, id) => {
|
|
468
662
|
if(!id) return new ReactiveDao.ObservableError("id is required")
|
|
@@ -497,6 +691,22 @@ function localReads(server, scriptContext) {
|
|
|
497
691
|
return index.rangeGet(range)
|
|
498
692
|
}
|
|
499
693
|
},
|
|
694
|
+
indexCount: {
|
|
695
|
+
observable: async (dbName, indexName, range) => {
|
|
696
|
+
const db = server.databases.get(dbName)
|
|
697
|
+
if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
|
|
698
|
+
const index = await db.index(indexName)
|
|
699
|
+
if(!index) return new ReactiveDao.ObservableError('indexNotFound')
|
|
700
|
+
return index.countObservable(range)
|
|
701
|
+
},
|
|
702
|
+
get: async (dbName, indexName, range) => {
|
|
703
|
+
const db = server.databases.get(dbName)
|
|
704
|
+
if(!db) throw new Error('databaseNotFound')
|
|
705
|
+
const index = await db.index(indexName)
|
|
706
|
+
if(!index) throw new Error("indexNotFound")
|
|
707
|
+
return index.countGet(range)
|
|
708
|
+
}
|
|
709
|
+
},
|
|
500
710
|
indexOpLogObject: {
|
|
501
711
|
observable: (dbName, indexName, id) => {
|
|
502
712
|
const db = server.databases.get(dbName)
|
|
@@ -531,6 +741,24 @@ function localReads(server, scriptContext) {
|
|
|
531
741
|
return index.opLog.rangeGet(range)
|
|
532
742
|
}
|
|
533
743
|
},
|
|
744
|
+
indexOpLogCount: {
|
|
745
|
+
observable: (dbName, indexName, range) => {
|
|
746
|
+
if(!id) return new ReactiveDao.ObservableError("id is required")
|
|
747
|
+
const db = server.databases.get(dbName)
|
|
748
|
+
if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
|
|
749
|
+
const index = db.index(indexName)
|
|
750
|
+
if(!index) return new ReactiveDao.ObservableError('indexNotFound')
|
|
751
|
+
return index.opLog.countObservable(range)
|
|
752
|
+
},
|
|
753
|
+
get: async (dbName, indexName, range) => {
|
|
754
|
+
if(!id) throw new Error("id is required")
|
|
755
|
+
const db = server.databases.get(dbName)
|
|
756
|
+
if(!db) throw new Error('databaseNotFound')
|
|
757
|
+
const index = db.index(indexName)
|
|
758
|
+
if(!index) throw new Error("indexNotFound")
|
|
759
|
+
return index.opLog.countGet(range)
|
|
760
|
+
}
|
|
761
|
+
},
|
|
534
762
|
logObject: {
|
|
535
763
|
observable: (dbName, logName, id) => {
|
|
536
764
|
|
|
@@ -564,42 +792,55 @@ function localReads(server, scriptContext) {
|
|
|
564
792
|
return log.rangeGet(range)
|
|
565
793
|
}
|
|
566
794
|
},
|
|
795
|
+
logCount: {
|
|
796
|
+
observable: (dbName, logName, range) => {
|
|
797
|
+
const db = server.databases.get(dbName)
|
|
798
|
+
if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
|
|
799
|
+
const log = db.log(logName)
|
|
800
|
+
if(!log) return new ReactiveDao.ObservableError('logNotFound')
|
|
801
|
+
return log.countObservable(range)
|
|
802
|
+
},
|
|
803
|
+
get: async (dbName, logName, range) => {
|
|
804
|
+
const db = server.databases.get(dbName)
|
|
805
|
+
if(!db) throw new Error('databaseNotFound')
|
|
806
|
+
const log = db.log(logName)
|
|
807
|
+
if(!log) throw new Error("logNotFound")
|
|
808
|
+
return log.countGet(range)
|
|
809
|
+
}
|
|
810
|
+
},
|
|
567
811
|
query: {
|
|
568
|
-
observable: (dbName, code, params = {}) => {
|
|
812
|
+
observable: (dbName, code, params = {}, sourceName = 'query/query.js') => {
|
|
569
813
|
if(!dbName) return new ReactiveDao.ObservableError("databaseNameRequired")
|
|
570
814
|
const db = server.databases.get(dbName)
|
|
571
815
|
if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
|
|
572
|
-
const queryFunction = scriptContext.run(code, '
|
|
573
|
-
|
|
816
|
+
const queryFunction = scriptContext.run(code, 'queryCode:' + sourceName) /// TODO: log queries, more info here
|
|
574
817
|
return db.queryObservable(async (input, output) => {
|
|
575
818
|
return queryFunction(input, output, params)
|
|
576
819
|
})
|
|
577
820
|
},
|
|
578
|
-
get: async (dbName, code, params = {}) => {
|
|
821
|
+
get: async (dbName, code, params = {}, sourceName = 'query/query.js') => {
|
|
579
822
|
if(!dbName) throw new Error("databaseNameRequired")
|
|
580
823
|
const db = server.databases.get(dbName)
|
|
581
824
|
if(!db) throw new Error('databaseNotFound')
|
|
582
|
-
const queryFunction = scriptContext.run(code, '
|
|
583
|
-
return db.queryGet((input, output) =>
|
|
584
|
-
return queryFunction(input, output, params)
|
|
585
|
-
})
|
|
825
|
+
const queryFunction = scriptContext.run(code, 'queryCode:' + sourceName)
|
|
826
|
+
return db.queryGet((input, output) => queryFunction(input, output, params))
|
|
586
827
|
}
|
|
587
828
|
},
|
|
588
829
|
queryObject: {
|
|
589
|
-
observable: (dbName, code, params = {}) => {
|
|
830
|
+
observable: (dbName, code, params = {}, sourceName = 'queryObject/query.js') => {
|
|
590
831
|
if(!dbName) return new ReactiveDao.ObservableError("databaseNameRequired")
|
|
591
832
|
const db = server.databases.get(dbName)
|
|
592
833
|
if(!db) return new ReactiveDao.ObservableError('databaseNotFound')
|
|
593
|
-
const queryFunction = scriptContext.run(code, '
|
|
834
|
+
const queryFunction = scriptContext.run(code, 'queryCode:' + sourceName) /// TODO: log queries, more info here
|
|
594
835
|
return db.queryObjectObservable(async (input, output) => {
|
|
595
836
|
return queryFunction(input, output, params)
|
|
596
837
|
})
|
|
597
838
|
},
|
|
598
|
-
get: async (dbName, code, params = {}) => {
|
|
839
|
+
get: async (dbName, code, params = {}, sourceName = 'queryObject/query.js') => {
|
|
599
840
|
if(!dbName) throw new Error("databaseNameRequired")
|
|
600
841
|
const db = server.databases.get(dbName)
|
|
601
842
|
if(!db) throw new Error('databaseNotFound')
|
|
602
|
-
const queryFunction = scriptContext.run(code, '
|
|
843
|
+
const queryFunction = scriptContext.run(code, 'queryCode:' + sourceName)
|
|
603
844
|
return db.queryObjectGet((input, output) => queryFunction(input, output, params))
|
|
604
845
|
}
|
|
605
846
|
}
|
|
@@ -608,5 +849,6 @@ function localReads(server, scriptContext) {
|
|
|
608
849
|
|
|
609
850
|
module.exports = {
|
|
610
851
|
localRequests,
|
|
852
|
+
remoteRequests,
|
|
611
853
|
localReads
|
|
612
|
-
}
|
|
854
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/db-web",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"author": {
|
|
5
5
|
"email": "michal@laszczewski.com",
|
|
6
6
|
"name": "Michał Łaszczewski",
|
|
@@ -29,10 +29,12 @@
|
|
|
29
29
|
"tape": "^5.3.2"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@live-change/dao": "
|
|
33
|
-
"@live-change/db": "^0.
|
|
34
|
-
"@live-change/db-store-
|
|
32
|
+
"@live-change/dao": "0.5.14",
|
|
33
|
+
"@live-change/db": "^0.6.2",
|
|
34
|
+
"@live-change/db-store-indexeddb": "^0.6.2",
|
|
35
|
+
"@live-change/db-store-localstorage": "^0.6.2",
|
|
36
|
+
"@live-change/db-store-rbtree": "^0.6.2",
|
|
35
37
|
"debug": "^4.3.4"
|
|
36
38
|
},
|
|
37
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "9a1b104864c08f3e35b009f191889e3308e3eeb0"
|
|
38
40
|
}
|