@cocreate/crud-server 1.14.5 → 1.15.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.15.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.14.5...v1.15.0) (2022-12-02)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * bump dependencies ([e9921ac](https://github.com/CoCreate-app/CoCreate-crud-server/commit/e9921acc5404d9bc7b04718e1cd6520949ceccd9))
7
+ * data.user to data.user_id ([645493a](https://github.com/CoCreate-app/CoCreate-crud-server/commit/645493ab7795559ddaa7e04b040777b6288ead37))
8
+ * renamed $contain to $include ([42d351e](https://github.com/CoCreate-app/CoCreate-crud-server/commit/42d351e435ebe5f2328682c8c836092f41eea2b7))
9
+
10
+
11
+ ### Features
12
+
13
+ * supports index and limit. improved search ([2ce163a](https://github.com/CoCreate-app/CoCreate-crud-server/commit/2ce163a60a9503f276ffb3ac1ffedf6016ba4d92))
14
+
1
15
  ## [1.14.5](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.14.4...v1.14.5) (2022-11-28)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/crud-server",
3
- "version": "1.14.5",
3
+ "version": "1.15.0",
4
4
  "description": "CoCreate-crud-server",
5
5
  "keywords": [
6
6
  "cocreate-crud",
@@ -40,8 +40,8 @@
40
40
  },
41
41
  "homepage": "https://cocreate.app/docs/CoCreate-crud-server",
42
42
  "dependencies": {
43
- "@cocreate/docs": "^1.4.8",
44
- "@cocreate/utils": "^1.12.6",
43
+ "@cocreate/docs": "^1.4.10",
44
+ "@cocreate/utils": "^1.13.0",
45
45
  "csvtojson": "^2.0.10",
46
46
  "json-2-csv": "^3.10.3",
47
47
  "mongodb": "^4.4.0"
@@ -322,7 +322,7 @@ function document(action, data){
322
322
  data[type][i]._id = ObjectId()
323
323
  else
324
324
  data[type][i]._id = ObjectId(data[type][i]._id)
325
- data[type][i]['created'] = {on: data.timeStamp, by: data.user || data.clientId}
325
+ data[type][i]['created'] = {on: data.timeStamp, by: data.user_id || data.clientId}
326
326
  }
327
327
  if (action == 'readDocument') {
328
328
  if (data[type][i]._id)
@@ -335,7 +335,7 @@ function document(action, data){
335
335
  if (!data[type][i]._id)
336
336
  updateData = createUpdate({document: [data[type][i]]}, type)
337
337
 
338
- data[type][i]['modified'] = {on: data.timeStamp, by: data.user || data.clientId}
338
+ data[type][i]['modified'] = {on: data.timeStamp, by: data.user_id || data.clientId}
339
339
 
340
340
  }
341
341
  if (action =='deleteDocument') {
@@ -375,8 +375,20 @@ function document(action, data){
375
375
  }
376
376
 
377
377
  if (action == 'readDocument') {
378
-
379
- collectionObj.find(query).sort(sort).toArray(function(error, result) {
378
+ let index = 0, limit = 0
379
+ if (data.filter) {
380
+ const count = await collectionObj.estimatedDocumentCount()
381
+ data.filter.count = count
382
+
383
+ if (data.filter.startIndex)
384
+ index = data.filter.startIndex
385
+ if (data.filter.limit)
386
+ limit = data.filter.limit
387
+ if (limit)
388
+ limit = index + limit;
389
+ }
390
+
391
+ collectionObj.find(query).limit(limit).sort(sort).toArray(function(error, result) {
380
392
  if (error) {
381
393
  error.database = database
382
394
  error.collection = collection
@@ -386,10 +398,10 @@ function document(action, data){
386
398
  if (result) {
387
399
  // ToDo: forEach at cursor
388
400
  for (let doc of result) {
389
- let isFilter = true
401
+ let isMatch = true
390
402
  if (data.filter && data.filter['search'])
391
- isFilter = searchData(doc, data.filter['search'])
392
- if (isFilter) {
403
+ isMatch = searchData(doc, data.filter['search'])
404
+ if (isMatch) {
393
405
  doc.db = 'mongodb'
394
406
  doc.database = database
395
407
  doc.collection = collection
@@ -397,6 +409,10 @@ function document(action, data){
397
409
  }
398
410
  }
399
411
 
412
+ if (index && limit) {
413
+ documents = documents.slice(index, limit)
414
+ }
415
+
400
416
  if (data.returnDocument == false) {
401
417
 
402
418
  for (let item of data['data']) {
@@ -431,9 +447,17 @@ function document(action, data){
431
447
  error.collection = collection
432
448
  errorHandler(error)
433
449
  }
434
-
435
- let searchResult = searchData(result, data.filter)
436
- resolve(searchResult)
450
+ if (data.filter && data.filter.search) {
451
+ let searchResult = []
452
+
453
+ for (let doc of result) {
454
+ let isMatch = searchData(doc, data.filter.search)
455
+ if (isMatch)
456
+ searchResult.push(doc)
457
+ }
458
+ result = searchResult
459
+ }
460
+ resolve(result)
437
461
  })
438
462
  }, (err) => {
439
463
  console.log(err);
@@ -451,7 +475,7 @@ function document(action, data){
451
475
  if (action == 'deleteDocument')
452
476
  documents.push({_id: doc._id, db: 'mongodb', database, collection})
453
477
  else
454
- doc['modified'] = {on: data.timeStamp, by: data.user || data.clientId}
478
+ doc['modified'] = {on: data.timeStamp, by: data.user_id || data.clientId}
455
479
 
456
480
  _ids.push(doc._id)
457
481
  }
@@ -630,9 +654,8 @@ function getFilters(data) {
630
654
 
631
655
 
632
656
  if (filter.sort)
633
- filter.sort.forEach((order) => {
634
- sort[order.name] = order.type
635
- });
657
+ for (let i = 0; i < filter.sort.length; i++)
658
+ sort[filter.sort[i].name] = filter.sort[i].direction
636
659
 
637
660
  return {query, sort}
638
661
  }
@@ -654,7 +677,7 @@ function createQuery(filters) {
654
677
  item.value = ObjectId(item.value)
655
678
 
656
679
  switch (item.operator) {
657
- case '$contain':
680
+ case '$includes':
658
681
  query[key]['$regex'] = item.value;
659
682
  break;
660
683