alchemymvc 1.4.0-alpha.5 → 1.4.0-alpha.6
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.
|
@@ -131,7 +131,11 @@ Mongo.setMethod(function castToBigInt(value) {
|
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
if (typeof value == 'object') {
|
|
134
|
-
|
|
134
|
+
if (value.toBigInt) {
|
|
135
|
+
return value.toBigInt();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return null;
|
|
135
139
|
}
|
|
136
140
|
|
|
137
141
|
return BigInt(value);
|
|
@@ -487,9 +491,21 @@ Mongo.setMethod(function _create(context) {
|
|
|
487
491
|
this.collection(model.table),
|
|
488
492
|
collection => {
|
|
489
493
|
|
|
490
|
-
const
|
|
494
|
+
const converted_data = context.getConvertedData();
|
|
491
495
|
const pledge = new Swift();
|
|
492
496
|
|
|
497
|
+
let data = {};
|
|
498
|
+
|
|
499
|
+
for (let key in converted_data) {
|
|
500
|
+
let val = converted_data[key];
|
|
501
|
+
|
|
502
|
+
if (val == null) {
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
data[key] = val;
|
|
507
|
+
}
|
|
508
|
+
|
|
493
509
|
Pledge.done(collection.insertOne(data, {w: 1, fullResult: true}), function afterInsert(err, result) {
|
|
494
510
|
|
|
495
511
|
// Clear the cache
|
|
@@ -541,110 +557,125 @@ Mongo.setMethod(function _create(context) {
|
|
|
541
557
|
*/
|
|
542
558
|
Mongo.setMethod(function _update(context) {
|
|
543
559
|
|
|
544
|
-
const model = context.getModel()
|
|
545
|
-
options = context.getSaveOptions();
|
|
560
|
+
const model = context.getModel();
|
|
546
561
|
|
|
547
562
|
return Swift.waterfall(
|
|
548
563
|
this.collection(model.table),
|
|
549
564
|
collection => {
|
|
565
|
+
return performUpdate(collection, model, context);
|
|
566
|
+
}
|
|
567
|
+
);
|
|
568
|
+
});
|
|
550
569
|
|
|
551
|
-
|
|
570
|
+
/**
|
|
571
|
+
* Perform the update
|
|
572
|
+
*
|
|
573
|
+
* @author Jelle De Loecker <jelle@elevenways.be>
|
|
574
|
+
* @since 1.4.0
|
|
575
|
+
* @version 1.4.0
|
|
576
|
+
*
|
|
577
|
+
* @return {Pledge}
|
|
578
|
+
*/
|
|
579
|
+
const performUpdate = (collection, model, context) => {
|
|
552
580
|
|
|
553
|
-
|
|
554
|
-
const data = context.getConvertedData();
|
|
581
|
+
const options = context.getSaveOptions();
|
|
555
582
|
|
|
556
|
-
|
|
557
|
-
let id = data._id;
|
|
583
|
+
let key;
|
|
558
584
|
|
|
559
|
-
|
|
560
|
-
|
|
585
|
+
// Get the converted data
|
|
586
|
+
const data = context.getConvertedData();
|
|
561
587
|
|
|
562
|
-
|
|
563
|
-
|
|
588
|
+
// Get the id to update, it should always be inside the given data
|
|
589
|
+
let id = data._id;
|
|
564
590
|
|
|
565
|
-
|
|
566
|
-
|
|
591
|
+
// Clone the data object
|
|
592
|
+
let doc = {...data};
|
|
567
593
|
|
|
568
|
-
|
|
569
|
-
|
|
594
|
+
// Values that will get flattened
|
|
595
|
+
let to_flatten = {};
|
|
570
596
|
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
}
|
|
597
|
+
// Field names that won't get flattened
|
|
598
|
+
let no_flatten = {};
|
|
574
599
|
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
let field = model.getField(key);
|
|
600
|
+
// Remove the id
|
|
601
|
+
delete doc._id;
|
|
578
602
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
to_flatten[key] = doc[key];
|
|
583
|
-
}
|
|
584
|
-
}
|
|
603
|
+
if (!options.override_created) {
|
|
604
|
+
delete doc.created;
|
|
605
|
+
}
|
|
585
606
|
|
|
586
|
-
|
|
587
|
-
|
|
607
|
+
// Iterate over the fields
|
|
608
|
+
for (key in doc) {
|
|
609
|
+
let field = model.getField(key);
|
|
588
610
|
|
|
589
|
-
|
|
590
|
-
|
|
611
|
+
if (field && (field.is_self_contained || field.is_translatable || typeof doc[key] == 'object')) {
|
|
612
|
+
no_flatten[key] = doc[key];
|
|
613
|
+
} else {
|
|
614
|
+
to_flatten[key] = doc[key];
|
|
615
|
+
}
|
|
616
|
+
}
|
|
591
617
|
|
|
592
|
-
|
|
618
|
+
// Flatten the object, using periods & NOT flattening arrays
|
|
619
|
+
let flat = Object.flatten(to_flatten, '.', false);
|
|
593
620
|
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
// We can't set null, because that could interfere with dot notation updates
|
|
597
|
-
if (flat[key] == null) {
|
|
621
|
+
// Assign the no-flatten values, too
|
|
622
|
+
Object.assign(flat, no_flatten);
|
|
598
623
|
|
|
599
|
-
|
|
600
|
-
unset[key] = '';
|
|
624
|
+
let unset = {};
|
|
601
625
|
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
626
|
+
for (key in flat) {
|
|
627
|
+
// Undefined or null means we want to delete the value
|
|
628
|
+
// We can't set null, because that could interfere with dot notation updates
|
|
629
|
+
if (flat[key] == null) {
|
|
606
630
|
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
};
|
|
631
|
+
// Add the key to the unset object
|
|
632
|
+
unset[key] = '';
|
|
610
633
|
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
634
|
+
// Remove it from the flat object
|
|
635
|
+
delete flat[key];
|
|
636
|
+
}
|
|
637
|
+
}
|
|
614
638
|
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
639
|
+
let update_object = {
|
|
640
|
+
$set: flat
|
|
641
|
+
};
|
|
618
642
|
|
|
619
|
-
|
|
643
|
+
if (!Object.isEmpty(unset)) {
|
|
644
|
+
update_object.$unset = unset;
|
|
645
|
+
}
|
|
620
646
|
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
promise = collection.findAndModify({_id: id}, [['_id', 1]], update_object, {upsert: true});
|
|
625
|
-
} else {
|
|
626
|
-
// If it's not available (like nedb)
|
|
627
|
-
promise = collection.update({_id: ''+id}, update_object, {upsert: true});
|
|
628
|
-
}
|
|
647
|
+
if (options.debug) {
|
|
648
|
+
console.log('Updating with obj', id, update_object);
|
|
649
|
+
}
|
|
629
650
|
|
|
630
|
-
|
|
651
|
+
let promise;
|
|
631
652
|
|
|
632
|
-
|
|
653
|
+
if (collection.findOneAndUpdate) {
|
|
654
|
+
promise = collection.findOneAndUpdate({_id: id}, update_object, {upsert: true});
|
|
655
|
+
} else if (collection.findAndModify) {
|
|
656
|
+
promise = collection.findAndModify({_id: id}, [['_id', 1]], update_object, {upsert: true});
|
|
657
|
+
} else {
|
|
658
|
+
// If it's not available (like nedb)
|
|
659
|
+
promise = collection.update({_id: ''+id}, update_object, {upsert: true});
|
|
660
|
+
}
|
|
633
661
|
|
|
634
|
-
|
|
635
|
-
model.nukeCache();
|
|
662
|
+
let pledge = new Swift();
|
|
636
663
|
|
|
637
|
-
|
|
638
|
-
return pledge.reject(err);
|
|
639
|
-
}
|
|
664
|
+
Pledge.done(promise, function afterUpdate(err, result) {
|
|
640
665
|
|
|
641
|
-
|
|
642
|
-
|
|
666
|
+
// Clear the cache
|
|
667
|
+
model.nukeCache();
|
|
643
668
|
|
|
644
|
-
|
|
669
|
+
if (err != null) {
|
|
670
|
+
return pledge.reject(err);
|
|
645
671
|
}
|
|
646
|
-
|
|
647
|
-
});
|
|
672
|
+
|
|
673
|
+
pledge.resolve(Object.assign({}, data));
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
return pledge;
|
|
677
|
+
|
|
678
|
+
};
|
|
648
679
|
|
|
649
680
|
/**
|
|
650
681
|
* Remove a record from the database
|
package/lib/core/setting.js
CHANGED