corebasic 1.0.179 → 1.0.181

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
@@ -2,6 +2,7 @@ import * as Elabase from './elabase.js'
2
2
 
3
3
  export const start = Elabase.start
4
4
 
5
+ export const operation = Elabase.operation
5
6
  export const insert = Elabase.insert
6
7
  export const query = Elabase.query
7
8
  export const update = Elabase.update
package/libs/elabase.js CHANGED
@@ -54,35 +54,87 @@ export function start(dbName) {
54
54
  }
55
55
  }
56
56
 
57
+ function generate_suffix(meta) {
57
58
 
58
- let isTransaction = false
59
- let transactions = []
60
- let stats = false
59
+ if (!meta || typeof meta !== 'object') {
60
+ throw {message: "Dip Error. Invalid meta"}
61
+ }
62
+ if ((typeof meta.company === 'string' && meta.company.trim() === "") || (typeof meta.company !== 'string' && !Array.isArray(meta.company)) || (Array.isArray(meta.company) && (!meta.company.length || meta.company.some(item => typeof item !== "string"|| item.trim() === "")))) {
63
+ throw {message: "Dip Error. Invalid meta.company"}
64
+ }
65
+ if ((typeof meta.outlet === 'string' && meta.outlet.trim() === "") || (typeof meta.outlet !== 'string' && !Array.isArray(meta.outlet)) || (Array.isArray(meta.outlet) && (!meta.outlet.length || meta.outlet.some(item => typeof item !== "string" || item.trim() === "")))) {
66
+ throw {message: "Dip Error. Invalid meta.outlet"}
67
+ }
68
+ if ((meta.suffix !== undefined && typeof meta.suffix !== 'string' && !Array.isArray(meta.suffix)) || (Array.isArray(meta.suffix) && meta.suffix.some(item => typeof item !== "string" || item.trim() === ""))) {
69
+ throw {message: "Dip Error. Invalid meta.suffix"}
70
+ } else if (typeof meta.suffix === 'string' && meta.suffix.trim() === "")
71
+ throw {message: "Dip Error. Invalid meta.suffix"}
72
+
73
+
74
+ let company = typeof meta.company == 'string' ? [meta.company] : (meta.company?.length ? meta.company : [])
75
+ let outlet = typeof meta.outlet == 'string' ? [meta.outlet] : (meta.outlet?.length ? meta.outlet : [])
76
+ let suffix = typeof meta.suffix == 'string' ? [meta.suffix] : (meta.suffix?.length ? meta.suffix : [])
77
+
78
+ company = company.filter(item => item.trim() !== "")
79
+ outlet = outlet.filter(item => item.trim() !== "")
80
+ suffix = suffix.filter(item => item.trim() !== "")
81
+
82
+ company = company.length ? company : [""]
83
+ outlet = outlet.length ? outlet : [""]
84
+ suffix = suffix.length ? suffix : [""]
85
+
86
+ company = new Set(company)
87
+ outlet = new Set(outlet)
88
+ suffix = new Set(suffix)
89
+
90
+ // console.log(company, outlet, suffix)
91
+ let suffixes = []
92
+ for (let c of company) {
93
+ for (let o of outlet) {
94
+ for (let s of suffix) {
95
+ let pathSegments = [c, o, s].filter(segment => segment !== "");
96
+ if (pathSegments.length > 0) {
97
+ suffixes.push(pathSegments.join('/'));
98
+ }
99
+ }
100
+ }
101
+ }
102
+
103
+ return suffixes
104
+ }
105
+
106
+ let isBranch = false
107
+ let batches = []
61
108
 
62
- export function transactionBegin() {
63
- isTransaction = true
64
- transactions = []
109
+ export function batchBegin() {
110
+ isBranch = true
111
+ batches = []
65
112
  }
66
- export function transactionAbort() {
67
- isTransaction = false
68
- transactions = []
113
+ export function batchAbort() {
114
+ isBranch = false
115
+ batches = []
69
116
  }
70
- export function transactionReset() {
71
- transactionAbort()
117
+ export function batchReset() {
118
+ batchAbort()
72
119
  }
73
- export function transactionCommit(arg) {
74
- let txns = transactions
75
- let mode = transactions.some(txn => txn.insert || txn.update || txn.delete) ? "command" : "query"
76
- isTransaction = false
77
- transactions = []
120
+ export function batchSubmit(arg) {
121
+ let txns = batches
122
+ let mode = batches.some(txn => txn.insert || txn.update || txn.delete) ? "command" : "query"
123
+ isBranch = false
124
+ batches = []
78
125
  return new Promise((resolve, reject) => resolve())
79
- .then(() => execute({ "transaction": txns, mode, stats: arg?.stats ?? false, version: arg?.version, compression: arg?.compression, size: arg?.size }))
126
+ .then(() => execute({ "batch": txns, mode, version: arg?.version, compression: arg?.compression }))
80
127
  .then (result => {
81
- Events.send("Elabase.transactionCommit", { response: result })
128
+ Events.send("Elabase.batchSubmit", { response: result })
82
129
  return result
83
130
  })
84
131
  }
85
132
 
133
+ export const operation = async (name, extras) => {
134
+ return execute({ operation: name, ...extras })
135
+ }
136
+
137
+
86
138
  export const insert = async (meta, collection, value, _id, extras) => {
87
139
  if (typeof collection !== "string") throw {message: "Error: collection type is not string in Dip.insert()"}
88
140
  // Multi Company Support
@@ -97,6 +149,7 @@ export const insert = async (meta, collection, value, _id, extras) => {
97
149
 
98
150
  var arg = {
99
151
  collection: collection,
152
+ suffix: generate_suffix(meta),
100
153
  insert: value,
101
154
  ...(meta.DIP_URL ? {DIP_URL: meta.DIP_URL} : {}),
102
155
  ...(meta.DIP_DB ? {db: meta.DIP_DB} : {}),
@@ -111,8 +164,8 @@ export const insert = async (meta, collection, value, _id, extras) => {
111
164
  throw {message: "Error: invalid _id in Dip.insert()"}
112
165
 
113
166
 
114
- if (isTransaction) {
115
- transactions.push(arg)
167
+ if (isBranch) {
168
+ batches.push(arg)
116
169
  return
117
170
  }
118
171
  arg.mode = "command"
@@ -140,14 +193,15 @@ export const query = async (meta, collection, query, options, extras) => {
140
193
 
141
194
  var arg = {
142
195
  collection: collection,
196
+ suffix: generate_suffix(meta),
143
197
  query: query,
144
198
  options: options ? options : {},
145
199
  ...(meta.DIP_URL ? {DIP_URL: meta.DIP_URL} : {}),
146
200
  ...(meta.DIP_DB ? {db: meta.DIP_DB} : {}),
147
201
  }
148
202
  arg = Object.assign(arg, extras)
149
- if (isTransaction) {
150
- transactions.push(arg)
203
+ if (isBranch) {
204
+ batches.push(arg)
151
205
  return
152
206
  }
153
207
  arg.mode = "query"
@@ -198,6 +252,7 @@ export const update = async (meta, collection, query, update, options, extras) =
198
252
 
199
253
  var arg = {
200
254
  collection: collection,
255
+ suffix: generate_suffix(meta),
201
256
  query: query,
202
257
  update: update,
203
258
  options: options ? options : {},
@@ -205,8 +260,8 @@ export const update = async (meta, collection, query, update, options, extras) =
205
260
  ...(meta.DIP_DB ? {db: meta.DIP_DB} : {}),
206
261
  }
207
262
  arg = Object.assign(arg, extras)
208
- if (isTransaction) {
209
- transactions.push(arg)
263
+ if (isBranch) {
264
+ batches.push(arg)
210
265
  return
211
266
  }
212
267
  arg.mode = "command"
@@ -218,7 +273,7 @@ export const update = async (meta, collection, query, update, options, extras) =
218
273
  })
219
274
  }
220
275
 
221
- export const remove = async (meta, collection, query, extras) => {
276
+ export const remove = async (meta, collection, query, options, extras) => {
222
277
  // Multi Company Support
223
278
  if (Utils.isEmpty(meta?.company)) throw {message: "Error: meta argument not provided in Dip.remove()"}
224
279
  if (typeof collection !== "string") throw {message: "Error: collection type is not string in Dip.remove()"}
@@ -226,17 +281,23 @@ export const remove = async (meta, collection, query, extras) => {
226
281
  if (meta.company !== "GLOBAL")
227
282
  query.company = meta.company
228
283
  // ---------------------
284
+ if (Utils.isEmpty(query._id) && !meta.allowDangerousRemove) {
285
+ throw { message: "Error: Dip.remove() without _id blocked by default (Dangerous Operation). Set allowDangerousRemove key if intentional." }
286
+ return
287
+ }
229
288
 
230
289
  var arg = {
231
290
  collection: collection,
291
+ suffix: generate_suffix(meta),
232
292
  query: query,
233
293
  delete: true,
294
+ options: options ? options : {},
234
295
  ...(meta.DIP_URL ? {DIP_URL: meta.DIP_URL} : {}),
235
296
  ...(meta.DIP_DB ? {db: meta.DIP_DB} : {}),
236
297
  }
237
298
  arg = Object.assign(arg, extras)
238
- if (isTransaction) {
239
- transactions.push(arg)
299
+ if (isBranch) {
300
+ batches.push(arg)
240
301
  return
241
302
  }
242
303
  arg.mode = "command"
@@ -273,8 +334,6 @@ function execute(arg) {
273
334
  arg.engine = arg.engine ?? internal.engineName
274
335
  arg.compression = true // Compress all for now
275
336
 
276
- if (stats)
277
- arg.stats = true
278
337
 
279
338
  return config.useDipper ? dipper(arg)
280
339
  : axios
@@ -285,7 +344,9 @@ function execute(arg) {
285
344
  // console.log(JSON.stringify(res.header, null, 4));
286
345
  // console.log(JSON.stringify(res.body, null, 4));
287
346
 
288
- return result.data
347
+ let res = result.data.mode === "query" ? result.data.items : result.data
348
+ res.stats = result.data.mode === "query" ? _ => result.data.stats : res.stats
349
+ return res
289
350
  })
290
351
  // .catch(err => { console.log(err.message); console.log(err.response); return err});
291
352
  }
package/libs/features.js CHANGED
@@ -118,9 +118,10 @@ function getFeaturelessFeature(req) {
118
118
 
119
119
  const apiHandler = async (req, res) => {
120
120
  let method = req.method.toLowerCase()
121
+ let featureless
121
122
  try {
122
123
  let feature = getFeature(req.body?.feature)
123
- let featureless = feature ? undefined : (feature = getFeaturelessFeature(req))
124
+ featureless = feature ? undefined : (feature = getFeaturelessFeature(req))
124
125
 
125
126
  if (!req.body?.feature && !featureless) {
126
127
  console.warn(`Feature: feature not sent by client`)
@@ -150,7 +151,7 @@ const apiHandler = async (req, res) => {
150
151
  await feature.handler({...req, headers: req.headers, body: {...req.body, topic} }, res)
151
152
  } catch (err) {
152
153
  if (process.env.DEBUG_MODE)
153
- console.log('Error: Feature: ', req.body?.feature, err)
154
+ console.log('Error: Feature: ', req.body?.feature ?? (featureless ? `Featureless Api:${featureless.api}` : undefined), err)
154
155
  throw {status: 500, message: "Failed to GET feature", ...err}
155
156
  }
156
157
  } else if (method !== "get" && feature.bypass) {
@@ -158,14 +159,14 @@ const apiHandler = async (req, res) => {
158
159
  await feature.handler(topic, prepareMessage(req), req, res)
159
160
  } catch (err) {
160
161
  if (process.env.DEBUG_MODE)
161
- console.log('Error: Feature: ', req.body?.feature, err)
162
+ console.log('Error: Feature: ', req.body?.feature ?? (featureless ? `Featureless Api:${featureless.api}` : undefined), err)
162
163
  throw {status: 500, message: "Failed to POST feature", ...err}
163
164
  }
164
165
  } else
165
166
  await commandAction(req, res, topic)
166
167
  } catch (err) {
167
168
  if (process.env.DEBUG_MODE)
168
- console.log('Error: Feature: ', req.body?.feature, err)
169
+ console.log('Error: Feature: ', req.body?.feature ?? (featureless ? `Featureless Api:${featureless.api}` : undefined), err)
169
170
  try { // Sometimes error occurs when disconnecting stream from front end
170
171
  res.status(err.status ?? 500).json(err)
171
172
  } catch (_) { }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.179",
4
+ "version": "1.0.181",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "scripts": {