corebasic 1.0.182 → 1.0.184

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/libs/dip.js CHANGED
@@ -8,10 +8,10 @@ export const query = Elabase.query
8
8
  export const update = Elabase.update
9
9
  export const remove = Elabase.remove
10
10
 
11
- export const transactionBegin = Elabase.transactionBegin
12
- export const transactionAbort = Elabase.transactionAbort
13
- export const transactionReset = Elabase.transactionReset
14
- export const transactionCommit = Elabase.transactionCommit
11
+ export const batchBegin = Elabase.batchBegin
12
+ export const batchAbort = Elabase.batchAbort
13
+ export const batchSubmit = Elabase.batchSubmit
14
+ export const batch = Elabase.batch
15
15
 
16
16
  export const shard_stats = Elabase.shard_stats
17
17
  export let config = Elabase.config
package/libs/elabase.js CHANGED
@@ -112,25 +112,36 @@ function generate_suffix(meta) {
112
112
  return suffixes
113
113
  }
114
114
 
115
- let isBranch = false
116
- let batches = []
115
+ let BATCH_COUNTER = 0n;
116
+ let batches = {}
117
117
 
118
- export function batchBegin() {
119
- isBranch = true
120
- batches = []
118
+ export function batchBegin() { // Manual Lifecycle. If you forget to batchSubmit() or batchAbort(), then memory leak due to dangling batch
119
+ const uid = ++BATCH_COUNTER; // Safe. No skew in async ++BATCH_COUNTER as single thread and ++COUNTER is atomic in event loop sense. ⚠️ Only breaks on worker_threads or cluster (multi-process)
120
+ batches[uid] = []
121
+ return uid
121
122
  }
122
- export function batchAbort() {
123
- isBranch = false
124
- batches = []
123
+ export function batchAbort(batch) { // Mandatory to prevent meory leak due to dangling batch if batchBegin() is called without batchSubmit()
124
+ delete batches[batch]
125
125
  }
126
- export function batchReset() {
127
- batchAbort()
126
+
127
+ export const batch = async (callback) => { // Convenience function with auto lifecyle management and auto submit when callback returns. abort is called appropriately.
128
+ const batch = Dip.batchBegin()
129
+
130
+ try {
131
+ await callback(batch)
132
+ return await batchSubmit(batch)
133
+ } catch (err) {
134
+ Dip.batchAbort(batch)
135
+ throw err
136
+ }
128
137
  }
129
- export function batchSubmit(arg) {
130
- let txns = batches
131
- let mode = batches.some(txn => txn.insert || txn.update || txn.delete) ? "command" : "query"
132
- isBranch = false
133
- batches = []
138
+
139
+ export function batchSubmit(batch, arg) {
140
+ if (!batch || !batches[batch] || !batches[batch].length)
141
+ throw {message: `Error: Invalid batch in Dip.batchSubmit()`}
142
+ let txns = batches[batch]
143
+ let mode = batches[batch].some(txn => txn.insert || txn.update || txn.delete) ? "command" : "query"
144
+ delete batches[batch]
134
145
  return new Promise((resolve, reject) => resolve())
135
146
  .then(() => execute({ "batch": txns, mode, version: arg?.version, compression: arg?.compression }))
136
147
  .then (result => {
@@ -167,8 +178,10 @@ export const insert = async (meta, collection, value, _id, extras) => {
167
178
  throw {message: "Error: invalid _id in Dip.insert()"}
168
179
 
169
180
 
170
- if (isBranch) {
171
- batches.push(arg)
181
+ if (meta.batch) {
182
+ if (!batches[meta.batch])
183
+ throw {message: 'Error: Invalid batch in Dip.insert()'}
184
+ batches[meta.batch].push(arg)
172
185
  return
173
186
  }
174
187
  arg.mode = "command"
@@ -195,8 +208,10 @@ export const query = async (meta, collection, query, options, extras) => {
195
208
  ...(meta.DIP_DB ? {db: meta.DIP_DB} : {}),
196
209
  }
197
210
  arg = Object.assign(arg, extras)
198
- if (isBranch) {
199
- batches.push(arg)
211
+ if (meta.batch) {
212
+ if (!batches[meta.batch])
213
+ throw {message: 'Error: Invalid batch in Dip.query()'}
214
+ batches[meta.batch].push(arg)
200
215
  return
201
216
  }
202
217
  arg.mode = "query"
@@ -238,8 +253,10 @@ export const update = async (meta, collection, query, update, options, extras) =
238
253
  ...(meta.DIP_DB ? {db: meta.DIP_DB} : {}),
239
254
  }
240
255
  arg = Object.assign(arg, extras)
241
- if (isBranch) {
242
- batches.push(arg)
256
+ if (meta.batch) {
257
+ if (!batches[meta.batch])
258
+ throw {message: 'Error: Invalid batch in Dip.update()'}
259
+ batches[meta.batch].push(arg)
243
260
  return
244
261
  }
245
262
  arg.mode = "command"
@@ -270,8 +287,10 @@ export const remove = async (meta, collection, query, options, extras) => {
270
287
  ...(meta.DIP_DB ? {db: meta.DIP_DB} : {}),
271
288
  }
272
289
  arg = Object.assign(arg, extras)
273
- if (isBranch) {
274
- batches.push(arg)
290
+ if (meta.batch) {
291
+ if (!batches[meta.batch])
292
+ throw {message: 'Error: Invalid batch in Dip.query()'}
293
+ batches[meta.batch].push(arg)
275
294
  return
276
295
  }
277
296
  arg.mode = "command"
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.182",
4
+ "version": "1.0.184",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "scripts": {