corebasic 1.0.180 → 1.0.182

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 (2) hide show
  1. package/libs/elabase.js +79 -53
  2. package/package.json +1 -1
package/libs/elabase.js CHANGED
@@ -54,6 +54,63 @@ export function start(dbName) {
54
54
  }
55
55
  }
56
56
 
57
+ function validate_collection(collection, fn_name) {
58
+ if ((typeof collection === 'string' && collection.trim() === "") || (typeof collection !== 'string' && !Array.isArray(collection)) || (Array.isArray(collection) && (!collection.length || collection.some(item => typeof item !== "string" || item.trim() === "")))) {
59
+ throw {message: `Error: Invalid collection ${fn_name}`}
60
+ }
61
+ }
62
+
63
+ function validate_meta(meta, fn_name) {
64
+ if (!meta || typeof meta !== 'object') {
65
+ throw {message: `Error: Invalid meta ${fn_name}`}
66
+ }
67
+ 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() === "")))) {
68
+ throw {message: `Error: Invalid meta.company ${fn_name}`}
69
+ }
70
+ 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() === "")))) {
71
+ throw {message: `Error: Invalid meta.outlet ${fn_name}`}
72
+ }
73
+ 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() === ""))) {
74
+ throw {message: `Error: Invalid meta.suffix ${fn_name}`}
75
+ } else if (typeof meta.suffix === 'string' && meta.suffix.trim() === "")
76
+ throw {message: `Error: Invalid meta.suffix ${fn_name}`}
77
+ }
78
+
79
+ function generate_suffix(meta) {
80
+
81
+ // Assuming validate_meta() is called before reaching here. If not then this is intentional validation bypass
82
+
83
+ let company = typeof meta.company == 'string' ? [meta.company] : (meta.company?.length ? meta.company : [])
84
+ let outlet = typeof meta.outlet == 'string' ? [meta.outlet] : (meta.outlet?.length ? meta.outlet : [])
85
+ let suffix = typeof meta.suffix == 'string' ? [meta.suffix] : (meta.suffix?.length ? meta.suffix : [])
86
+
87
+ company = company.filter(item => item.trim() !== "")
88
+ outlet = outlet.filter(item => item.trim() !== "")
89
+ suffix = suffix.filter(item => item.trim() !== "")
90
+
91
+ company = company.length ? company : [""]
92
+ outlet = outlet.length ? outlet : [""]
93
+ suffix = suffix.length ? suffix : [""]
94
+
95
+ company = new Set(company)
96
+ outlet = new Set(outlet)
97
+ suffix = new Set(suffix)
98
+
99
+ // console.log(company, outlet, suffix)
100
+ let suffixes = []
101
+ for (let c of company) {
102
+ for (let o of outlet) {
103
+ for (let s of suffix) {
104
+ let pathSegments = [c, o, s].filter(segment => segment !== "");
105
+ if (pathSegments.length > 0) {
106
+ suffixes.push(pathSegments.join('/'));
107
+ }
108
+ }
109
+ }
110
+ }
111
+
112
+ return suffixes
113
+ }
57
114
 
58
115
  let isBranch = false
59
116
  let batches = []
@@ -88,19 +145,14 @@ export const operation = async (name, extras) => {
88
145
 
89
146
 
90
147
  export const insert = async (meta, collection, value, _id, extras) => {
91
- if (typeof collection !== "string") throw {message: "Error: collection type is not string in Dip.insert()"}
92
- // Multi Company Support
93
- if (Utils.isEmpty(meta?.company)) throw {message: "Error: meta argument not provided in Dip.insert()"}
94
- collection = `${meta.company}.${collection}`
95
- if (meta.company !== "GLOBAL") {
96
- if (typeof value === "object" && !Array.isArray(value))
97
- value.company = meta.company
98
- }
99
- value.outlet = value.outlet ?? meta.outlet
100
- // ---------------------
148
+
149
+ validate_collection(collection, 'in Dip.insert()')
150
+
151
+ validate_meta(meta, 'in Dip.insert()')
101
152
 
102
153
  var arg = {
103
154
  collection: collection,
155
+ suffix: generate_suffix(meta),
104
156
  insert: value,
105
157
  ...(meta.DIP_URL ? {DIP_URL: meta.DIP_URL} : {}),
106
158
  ...(meta.DIP_DB ? {db: meta.DIP_DB} : {}),
@@ -129,21 +181,14 @@ export const insert = async (meta, collection, value, _id, extras) => {
129
181
  }
130
182
 
131
183
  export const query = async (meta, collection, query, options, extras) => {
132
- if (typeof collection !== "string") throw {message: "Error: collection type is not string in Dip.query()"}
133
- if (meta?.USE_EMPTY_COMPANY) {
134
-
135
- } else {
136
- // Multi Company Support
137
- if (Utils.isEmpty(meta?.company)) throw {message: "Error: meta argument not provided in Dip.query()"}
138
- collection = `${meta.company}.${collection}`
139
- if (meta.company !== "GLOBAL")
140
- query.company = meta.company
141
- }
142
- // ---------------------
143
184
 
185
+ validate_collection(collection, 'in Dip.query()')
186
+
187
+ validate_meta(meta, 'in Dip.query()')
144
188
 
145
189
  var arg = {
146
190
  collection: collection,
191
+ suffix: generate_suffix(meta),
147
192
  query: query,
148
193
  options: options ? options : {},
149
194
  ...(meta.DIP_URL ? {DIP_URL: meta.DIP_URL} : {}),
@@ -178,30 +223,14 @@ function collectionArray(col) {
178
223
 
179
224
 
180
225
  export const update = async (meta, collection, query, update, options, extras) => {
181
- // Multi Company Support
182
- if (Utils.isEmpty(meta?.company)) throw {message: "Error: meta argument not provided in Dip.update()"}
183
- if (typeof collection !== "string") throw {message: "Error: collection type is not string in Dip.update()"}
184
- collection = `${meta.company}.${collection}`
185
- if (meta.company !== "GLOBAL") {
186
- query.company = meta.company
187
- update.$set = update.$set ?? {}
188
- update.$set.company = meta.company
189
- if (options?.upsert) {
190
- update.$setOnInsert = update.$setOnInsert ?? {}
191
- update.$setOnInsert.company = meta.company
192
- update.$setOnInsert.outlet = update.$setOnInsert.outlet ?? update.$set?.outlet ?? meta.outlet
193
-
194
- let _id = update.$setOnInsert._id ?? update.$set?._id ?? query?._id
195
- if (!(["string", "number"].includes(typeof _id)))
196
- throw {message: "Error: invalid _id in Dip.update() with upsert"}
197
- if (typeof _id === "string" && _id.trim().length === 0)
198
- throw {message: "Error: invalid _id in Dip.update() with upsert"}
199
- }
200
- }
201
- // ---------------------
226
+
227
+ validate_collection(collection, 'in Dip.update()')
228
+
229
+ validate_meta(meta, 'in Dip.update()')
202
230
 
203
231
  var arg = {
204
232
  collection: collection,
233
+ suffix: generate_suffix(meta),
205
234
  query: query,
206
235
  update: update,
207
236
  options: options ? options : {},
@@ -223,20 +252,17 @@ export const update = async (meta, collection, query, update, options, extras) =
223
252
  }
224
253
 
225
254
  export const remove = async (meta, collection, query, options, extras) => {
226
- // Multi Company Support
227
- if (Utils.isEmpty(meta?.company)) throw {message: "Error: meta argument not provided in Dip.remove()"}
228
- if (typeof collection !== "string") throw {message: "Error: collection type is not string in Dip.remove()"}
229
- collection = `${meta.company}.${collection}`
230
- if (meta.company !== "GLOBAL")
231
- query.company = meta.company
232
- // ---------------------
233
- if (Utils.isEmpty(query._id) && !meta.allowDangerousRemove) {
234
- throw { message: "Error: Dip.remove() without _id blocked by default (Dangerous Operation). Set allowDangerousRemove key if intentional." }
235
- return
236
- }
255
+
256
+ validate_collection(collection, 'in Dip.remove()')
257
+
258
+ if (Utils.isEmpty(query._id) && !meta.allowDangerousRemove)
259
+ throw { message: "Error: Dip.remove() without _id blocked by default (Dangerous Operation). Set meta.allowDangerousRemove if intentional." }
260
+
261
+ validate_meta(meta, 'in Dip.remove()')
237
262
 
238
263
  var arg = {
239
264
  collection: collection,
265
+ suffix: generate_suffix(meta),
240
266
  query: query,
241
267
  delete: true,
242
268
  options: options ? options : {},
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.180",
4
+ "version": "1.0.182",
5
5
  "description": "",
6
6
  "main": "index.js",
7
7
  "scripts": {