corebasic 1.0.181 → 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.
- package/libs/elabase.js +61 -87
- package/package.json +1 -1
package/libs/elabase.js
CHANGED
|
@@ -54,53 +54,62 @@ export function start(dbName) {
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
function
|
|
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
|
+
}
|
|
58
62
|
|
|
63
|
+
function validate_meta(meta, fn_name) {
|
|
59
64
|
if (!meta || typeof meta !== 'object') {
|
|
60
|
-
throw {message:
|
|
65
|
+
throw {message: `Error: Invalid meta ${fn_name}`}
|
|
61
66
|
}
|
|
62
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() === "")))) {
|
|
63
|
-
throw {message:
|
|
68
|
+
throw {message: `Error: Invalid meta.company ${fn_name}`}
|
|
64
69
|
}
|
|
65
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() === "")))) {
|
|
66
|
-
throw {message:
|
|
71
|
+
throw {message: `Error: Invalid meta.outlet ${fn_name}`}
|
|
67
72
|
}
|
|
68
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() === ""))) {
|
|
69
|
-
throw {message:
|
|
74
|
+
throw {message: `Error: Invalid meta.suffix ${fn_name}`}
|
|
70
75
|
} else if (typeof meta.suffix === 'string' && meta.suffix.trim() === "")
|
|
71
|
-
throw {message:
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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('/'));
|
|
99
107
|
}
|
|
100
108
|
}
|
|
101
109
|
}
|
|
110
|
+
}
|
|
102
111
|
|
|
103
|
-
|
|
112
|
+
return suffixes
|
|
104
113
|
}
|
|
105
114
|
|
|
106
115
|
let isBranch = false
|
|
@@ -136,16 +145,10 @@ export const operation = async (name, extras) => {
|
|
|
136
145
|
|
|
137
146
|
|
|
138
147
|
export const insert = async (meta, collection, value, _id, extras) => {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
if (meta.company !== "GLOBAL") {
|
|
144
|
-
if (typeof value === "object" && !Array.isArray(value))
|
|
145
|
-
value.company = meta.company
|
|
146
|
-
}
|
|
147
|
-
value.outlet = value.outlet ?? meta.outlet
|
|
148
|
-
// ---------------------
|
|
148
|
+
|
|
149
|
+
validate_collection(collection, 'in Dip.insert()')
|
|
150
|
+
|
|
151
|
+
validate_meta(meta, 'in Dip.insert()')
|
|
149
152
|
|
|
150
153
|
var arg = {
|
|
151
154
|
collection: collection,
|
|
@@ -178,18 +181,10 @@ export const insert = async (meta, collection, value, _id, extras) => {
|
|
|
178
181
|
}
|
|
179
182
|
|
|
180
183
|
export const query = async (meta, collection, query, options, extras) => {
|
|
181
|
-
if (typeof collection !== "string") throw {message: "Error: collection type is not string in Dip.query()"}
|
|
182
|
-
if (meta?.USE_EMPTY_COMPANY) {
|
|
183
|
-
|
|
184
|
-
} else {
|
|
185
|
-
// Multi Company Support
|
|
186
|
-
if (Utils.isEmpty(meta?.company)) throw {message: "Error: meta argument not provided in Dip.query()"}
|
|
187
|
-
collection = `${meta.company}.${collection}`
|
|
188
|
-
if (meta.company !== "GLOBAL")
|
|
189
|
-
query.company = meta.company
|
|
190
|
-
}
|
|
191
|
-
// ---------------------
|
|
192
184
|
|
|
185
|
+
validate_collection(collection, 'in Dip.query()')
|
|
186
|
+
|
|
187
|
+
validate_meta(meta, 'in Dip.query()')
|
|
193
188
|
|
|
194
189
|
var arg = {
|
|
195
190
|
collection: collection,
|
|
@@ -228,27 +223,10 @@ function collectionArray(col) {
|
|
|
228
223
|
|
|
229
224
|
|
|
230
225
|
export const update = async (meta, collection, query, update, options, extras) => {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
if (meta.company !== "GLOBAL") {
|
|
236
|
-
query.company = meta.company
|
|
237
|
-
update.$set = update.$set ?? {}
|
|
238
|
-
update.$set.company = meta.company
|
|
239
|
-
if (options?.upsert) {
|
|
240
|
-
update.$setOnInsert = update.$setOnInsert ?? {}
|
|
241
|
-
update.$setOnInsert.company = meta.company
|
|
242
|
-
update.$setOnInsert.outlet = update.$setOnInsert.outlet ?? update.$set?.outlet ?? meta.outlet
|
|
243
|
-
|
|
244
|
-
let _id = update.$setOnInsert._id ?? update.$set?._id ?? query?._id
|
|
245
|
-
if (!(["string", "number"].includes(typeof _id)))
|
|
246
|
-
throw {message: "Error: invalid _id in Dip.update() with upsert"}
|
|
247
|
-
if (typeof _id === "string" && _id.trim().length === 0)
|
|
248
|
-
throw {message: "Error: invalid _id in Dip.update() with upsert"}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
// ---------------------
|
|
226
|
+
|
|
227
|
+
validate_collection(collection, 'in Dip.update()')
|
|
228
|
+
|
|
229
|
+
validate_meta(meta, 'in Dip.update()')
|
|
252
230
|
|
|
253
231
|
var arg = {
|
|
254
232
|
collection: collection,
|
|
@@ -274,17 +252,13 @@ export const update = async (meta, collection, query, update, options, extras) =
|
|
|
274
252
|
}
|
|
275
253
|
|
|
276
254
|
export const remove = async (meta, collection, query, options, extras) => {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
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
|
-
}
|
|
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()')
|
|
288
262
|
|
|
289
263
|
var arg = {
|
|
290
264
|
collection: collection,
|