bonsaif 1.10.41 → 1.10.42
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/lib/hookup/mongodb.js +353 -44
- package/package.json +1 -1
- package/tests/docker-restart.sh +0 -0
- package/tests/docker-start.sh +0 -0
- package/tests/test-mongodb.js +205 -84
package/lib/hookup/mongodb.js
CHANGED
|
@@ -163,34 +163,40 @@ const update=async(options,dbm, col, json_obj, query)=>{
|
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
const upsert=async(options,dbm, col, json_obj, query)=>{
|
|
166
|
+
const exec = require("./exec");
|
|
167
|
+
const mongoclient = exec.mongoConn(options);
|
|
168
|
+
const start = Date.now();
|
|
169
|
+
|
|
166
170
|
return new Promise((resolve, reject)=>{
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
171
|
+
mongoclient.then(
|
|
172
|
+
function(client){
|
|
173
|
+
let db = client.db(dbm);
|
|
174
|
+
db.collection(col).updateOne(query, { $set: json_obj }, { upsert: true }, function(err, res) {
|
|
175
|
+
const end = Date.now();
|
|
176
|
+
const time = end - start;
|
|
177
|
+
let result, upsertedCount=0, modifiedCount=0, upsertedId=null;
|
|
178
|
+
try{
|
|
179
|
+
result = res.result;
|
|
180
|
+
upsertedCount = res.upsertedCount || 0;
|
|
181
|
+
modifiedCount = res.modifiedCount || 0;
|
|
182
|
+
upsertedId = res.upsertedId ? res.upsertedId._id : null;
|
|
183
|
+
} catch(e){ }
|
|
184
|
+
if (err) {
|
|
185
|
+
resolve({result:{dml:"upsert", headers:"", time:utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
186
|
+
}else{
|
|
187
|
+
let action = upsertedCount > 0 ? 'inserted' : 'updated';
|
|
188
|
+
resolve({result:{dml:"upsert", headers:"upsert", insertId:upsertedId, time:utl.milisegundosASegundos(time), code:200, error:0}, results:{WriteResult:`1 document ${action}`, result }});
|
|
189
|
+
}
|
|
190
|
+
try{
|
|
191
|
+
exec.mongoClose(mongoclient);
|
|
192
|
+
}catch(e){
|
|
193
|
+
utl.log('err.upsert '+e);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
).catch(err=>{
|
|
198
|
+
resolve({result:{dml:"upsert", headers:"", time:0, code:404, error:1}, results:err});
|
|
199
|
+
});
|
|
194
200
|
})
|
|
195
201
|
}
|
|
196
202
|
|
|
@@ -607,7 +613,8 @@ const api=async(options,dbm, json)=>{
|
|
|
607
613
|
}
|
|
608
614
|
|
|
609
615
|
if (utl.ObjectEmpty(json, 'insert')){
|
|
610
|
-
dml = 'insert';
|
|
616
|
+
dml = 'insert';
|
|
617
|
+
col = json.insert;
|
|
611
618
|
json_api = json.documents?json.documents:[];
|
|
612
619
|
if (json_api.length==1){
|
|
613
620
|
json_api = json.documents[0];
|
|
@@ -617,7 +624,8 @@ const api=async(options,dbm, json)=>{
|
|
|
617
624
|
}
|
|
618
625
|
|
|
619
626
|
if (utl.ObjectEmpty(json, 'update')){
|
|
620
|
-
dml = 'update';
|
|
627
|
+
dml = 'update';
|
|
628
|
+
col = json.update;
|
|
621
629
|
json_api = json.documents?json.documents:[];
|
|
622
630
|
if (json_api.length==1){
|
|
623
631
|
json_api = json.documents[0];
|
|
@@ -627,7 +635,8 @@ const api=async(options,dbm, json)=>{
|
|
|
627
635
|
}
|
|
628
636
|
|
|
629
637
|
if (utl.ObjectEmpty(json, 'updateMany')){
|
|
630
|
-
dml = 'updateMany';
|
|
638
|
+
dml = 'updateMany';
|
|
639
|
+
col = json.updateMany;
|
|
631
640
|
json_api = json.documents?json.documents:[];
|
|
632
641
|
if (json_api.length==1){
|
|
633
642
|
json_api = json.documents[0];
|
|
@@ -636,7 +645,31 @@ const api=async(options,dbm, json)=>{
|
|
|
636
645
|
}
|
|
637
646
|
}
|
|
638
647
|
|
|
639
|
-
|
|
648
|
+
if (utl.ObjectEmpty(json, 'replaceOne')){
|
|
649
|
+
dml = 'replaceOne';
|
|
650
|
+
col = json.replaceOne;
|
|
651
|
+
json_api = json.documents?json.documents:[];
|
|
652
|
+
if (json_api.length==1){
|
|
653
|
+
json_api = json.documents[0];
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
if (utl.ObjectEmpty(json, 'findOneAndUpdate')){
|
|
658
|
+
dml = 'findOneAndUpdate';
|
|
659
|
+
col = json.findOneAndUpdate;
|
|
660
|
+
json_api = json.documents?json.documents:[];
|
|
661
|
+
if (json_api.length==1){
|
|
662
|
+
json_api = json.documents[0];
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
if (utl.ObjectEmpty(json, 'insertMany')){
|
|
667
|
+
dml = 'insertMany';
|
|
668
|
+
col = json.insertMany;
|
|
669
|
+
json_api = json.documents?json.documents:[];
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
let ltdml = ['find','findOne','delete','deleteOne','deleteMany','count','dbs','collections','drop','aggregate','distinct','findOneAndDelete'];
|
|
640
673
|
for (let x of ltdml){
|
|
641
674
|
if (utl.ObjectEmpty(json,x)){
|
|
642
675
|
dml = x; break;
|
|
@@ -644,20 +677,30 @@ const api=async(options,dbm, json)=>{
|
|
|
644
677
|
}
|
|
645
678
|
dml==''? dml='command':'';
|
|
646
679
|
|
|
647
|
-
|
|
680
|
+
if (!col){
|
|
681
|
+
eval(` try{ col = json.${dml}; } catch(e){ } `);
|
|
682
|
+
}
|
|
648
683
|
let r;
|
|
649
684
|
switch (dml){
|
|
650
|
-
case 'insert':
|
|
651
|
-
case '
|
|
652
|
-
case '
|
|
653
|
-
case '
|
|
654
|
-
case '
|
|
655
|
-
case '
|
|
656
|
-
case 'deleteMany':
|
|
657
|
-
case '
|
|
658
|
-
case '
|
|
659
|
-
case '
|
|
660
|
-
|
|
685
|
+
case 'insert': r = await insert(options,dbm,col, json_api); break;
|
|
686
|
+
case 'insertMany': r = await insertMany(options,dbm,col, json_api); break;
|
|
687
|
+
case 'upsert': r = await upsert(options,dbm,col, json_api, query); break;
|
|
688
|
+
case 'update': r = await update(options,dbm,col, json_api, query); break;
|
|
689
|
+
case 'updateMany': r = await updateMany(options,dbm,col, json_api, query); break;
|
|
690
|
+
case 'deleteOne': r = await deleteOne(options,dbm,col, query); break;
|
|
691
|
+
case 'deleteMany': r = await deleteMany(options,dbm,col, json); break;
|
|
692
|
+
case 'count': r = await count(options,dbm,col, json); break;
|
|
693
|
+
case 'dbs': r = await dbs(options); break;
|
|
694
|
+
case 'collections': r = await collections(options,dbm); break;
|
|
695
|
+
case 'drop': r = await drop(options,dbm,col); break;
|
|
696
|
+
case 'find': r = await findMany(options,dbm,col,json); break;
|
|
697
|
+
case 'findOne': r = await findOne(options,dbm,col, query, json.sort); break;
|
|
698
|
+
case 'aggregate': r = await aggregate(options,dbm,col, json.pipeline); break;
|
|
699
|
+
case 'distinct': r = await distinct(options,dbm,col, json.field, query); break;
|
|
700
|
+
case 'findOneAndUpdate': r = await findOneAndUpdate(options,dbm,col, query, json_api, json.returnNew); break;
|
|
701
|
+
case 'findOneAndDelete': r = await findOneAndDelete(options,dbm,col, query); break;
|
|
702
|
+
case 'replaceOne': r = await replaceOne(options,dbm,col, query, json_api); break;
|
|
703
|
+
default: r = await command(options,dbm,json);
|
|
661
704
|
}
|
|
662
705
|
|
|
663
706
|
return new Promise((resolve, reject)=>{
|
|
@@ -665,10 +708,271 @@ const api=async(options,dbm, json)=>{
|
|
|
665
708
|
})
|
|
666
709
|
}
|
|
667
710
|
|
|
711
|
+
const findOne=async(options,dbm, col, query, orderby)=>{
|
|
712
|
+
const exec = require("./exec");
|
|
713
|
+
const mongoclient = exec.mongoConn(options);
|
|
714
|
+
orderby = orderby!=null?orderby:'';
|
|
715
|
+
const start = Date.now();
|
|
716
|
+
|
|
717
|
+
return new Promise((resolve, reject)=>{
|
|
718
|
+
mongoclient.then(
|
|
719
|
+
function(client){
|
|
720
|
+
let db = client.db(dbm);
|
|
721
|
+
db.collection(col).findOne(query, {sort: orderby}, function(err, result) {
|
|
722
|
+
const end = Date.now();
|
|
723
|
+
const time = end - start;
|
|
724
|
+
|
|
725
|
+
if (err) {
|
|
726
|
+
resolve({"result":{"dml":"findOne", "headers":"", "time":utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
727
|
+
}else{
|
|
728
|
+
resolve({"result":{"dml":"findOne", "headers":getFields(result), "time":utl.milisegundosASegundos(time), code:200, error:0}, data:result ? [result] : []});
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
try{
|
|
732
|
+
exec.mongoClose(mongoclient);
|
|
733
|
+
}catch(e){
|
|
734
|
+
utl.log('err.findOne '+e);
|
|
735
|
+
}
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
).catch(err=>{
|
|
739
|
+
resolve({"result":{"dml":"findOne", "headers":"", time:0, code:404, error:1}, "results":err});
|
|
740
|
+
});
|
|
741
|
+
})
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const deleteOne=async(options,dbm, col, query)=>{
|
|
745
|
+
const exec = require("./exec");
|
|
746
|
+
const mongoclient = exec.mongoConn(options);
|
|
747
|
+
const start = Date.now();
|
|
748
|
+
|
|
749
|
+
return new Promise((resolve, reject)=>{
|
|
750
|
+
mongoclient.then(
|
|
751
|
+
function(client){
|
|
752
|
+
let db = client.db(dbm);
|
|
753
|
+
db.collection(col).deleteOne(query, function(err, res) {
|
|
754
|
+
const end = Date.now();
|
|
755
|
+
const time = end - start;
|
|
756
|
+
let result, deletedCount=0;
|
|
757
|
+
try{ result = res.result; deletedCount = res.result.n; } catch(e){ }
|
|
758
|
+
if (err) {
|
|
759
|
+
resolve({result:{dml:"deleteOne", headers:"", time:utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
760
|
+
}else{
|
|
761
|
+
resolve({result:{dml:"deleteOne", headers:"deletedCount", time:utl.milisegundosASegundos(time), code:200, error:0}, results:{WriteResult:`${deletedCount} document deleted`, result }});
|
|
762
|
+
}
|
|
763
|
+
try{
|
|
764
|
+
exec.mongoClose(mongoclient);
|
|
765
|
+
}catch(e){
|
|
766
|
+
utl.log('err.deleteOne '+e);
|
|
767
|
+
}
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
).catch(err=>{
|
|
771
|
+
resolve({result:{dml:"deleteOne", headers:"", time:0, code:404, error:1}, results:err});
|
|
772
|
+
});
|
|
773
|
+
})
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
const insertMany=async(options,dbm, col, documents)=>{
|
|
777
|
+
const exec = require("./exec");
|
|
778
|
+
const mongoclient = exec.mongoConn(options);
|
|
779
|
+
const start = Date.now();
|
|
780
|
+
|
|
781
|
+
return new Promise((resolve, reject)=>{
|
|
782
|
+
mongoclient.then(
|
|
783
|
+
function(client){
|
|
784
|
+
let db = client.db(dbm);
|
|
785
|
+
db.collection(col).insertMany(documents, function(err, res) {
|
|
786
|
+
const end = Date.now();
|
|
787
|
+
const time = end - start;
|
|
788
|
+
if (err) {
|
|
789
|
+
resolve({result:{dml:"insertMany", headers:"", time:utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
790
|
+
}else{
|
|
791
|
+
let ops=res.ops!=null?res.ops:{};
|
|
792
|
+
resolve({result:{dml:"insertMany", headers:"insertedCount", insertedCount:res.insertedCount, time:utl.milisegundosASegundos(time), code:200, error:0}, results:{WriteResult:`${res.insertedCount} documents inserted`, ops}});
|
|
793
|
+
}
|
|
794
|
+
try{
|
|
795
|
+
exec.mongoClose(mongoclient);
|
|
796
|
+
}catch(e){
|
|
797
|
+
utl.log('err.insertMany '+e);
|
|
798
|
+
}
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
).catch(err=>{
|
|
802
|
+
resolve({"result":{"dml":"insertMany", "headers":"", time:0, code:404, error:1}, results:err});
|
|
803
|
+
});
|
|
804
|
+
})
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
const aggregate=async(options,dbm, col, pipeline)=>{
|
|
808
|
+
const exec = require("./exec");
|
|
809
|
+
const mongoclient = exec.mongoConn(options);
|
|
810
|
+
const start = Date.now();
|
|
811
|
+
|
|
812
|
+
return new Promise((resolve, reject)=>{
|
|
813
|
+
mongoclient.then(
|
|
814
|
+
function(client){
|
|
815
|
+
let db = client.db(dbm);
|
|
816
|
+
db.collection(col).aggregate(pipeline).toArray(function(err, result) {
|
|
817
|
+
const end = Date.now();
|
|
818
|
+
const time = end - start;
|
|
819
|
+
|
|
820
|
+
if (err) {
|
|
821
|
+
resolve({"result":{"dml":"aggregate", "headers":"", "time":utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
822
|
+
}else{
|
|
823
|
+
resolve({"result":{"dml":"aggregate", "headers":getFields(result[0]), "time":utl.milisegundosASegundos(time), code:200, error:0}, data:result});
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
try{
|
|
827
|
+
exec.mongoClose(mongoclient);
|
|
828
|
+
}catch(e){
|
|
829
|
+
utl.log('err.aggregate '+e);
|
|
830
|
+
}
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
).catch(err=>{
|
|
834
|
+
resolve({"result":{"dml":"aggregate", "headers":"", time:0, code:404, error:1}, "results":err});
|
|
835
|
+
});
|
|
836
|
+
})
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
const distinct=async(options,dbm, col, field, query={})=>{
|
|
840
|
+
const exec = require("./exec");
|
|
841
|
+
const mongoclient = exec.mongoConn(options);
|
|
842
|
+
const start = Date.now();
|
|
843
|
+
|
|
844
|
+
return new Promise((resolve, reject)=>{
|
|
845
|
+
mongoclient.then(
|
|
846
|
+
function(client){
|
|
847
|
+
let db = client.db(dbm);
|
|
848
|
+
db.collection(col).distinct(field, query, function(err, result) {
|
|
849
|
+
const end = Date.now();
|
|
850
|
+
const time = end - start;
|
|
851
|
+
|
|
852
|
+
if (err) {
|
|
853
|
+
resolve({"result":{"dml":"distinct", "headers":"", "time":utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
854
|
+
}else{
|
|
855
|
+
resolve({"result":{"dml":"distinct", "headers":field, "time":utl.milisegundosASegundos(time), code:200, error:0}, data:result});
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
try{
|
|
859
|
+
exec.mongoClose(mongoclient);
|
|
860
|
+
}catch(e){
|
|
861
|
+
utl.log('err.distinct '+e);
|
|
862
|
+
}
|
|
863
|
+
});
|
|
864
|
+
}
|
|
865
|
+
).catch(err=>{
|
|
866
|
+
resolve({"result":{"dml":"distinct", "headers":"", time:0, code:404, error:1}, "results":err});
|
|
867
|
+
});
|
|
868
|
+
})
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
const findOneAndUpdate=async(options,dbm, col, query, json_obj, returnNew=true)=>{
|
|
872
|
+
const exec = require("./exec");
|
|
873
|
+
const mongoclient = exec.mongoConn(options);
|
|
874
|
+
const start = Date.now();
|
|
875
|
+
|
|
876
|
+
return new Promise((resolve, reject)=>{
|
|
877
|
+
mongoclient.then(
|
|
878
|
+
function(client){
|
|
879
|
+
let db = client.db(dbm);
|
|
880
|
+
db.collection(col).findOneAndUpdate(query, { $set: json_obj }, { returnDocument: returnNew ? 'after' : 'before' }, function(err, res) {
|
|
881
|
+
const end = Date.now();
|
|
882
|
+
const time = end - start;
|
|
883
|
+
let result;
|
|
884
|
+
try{ result = res.value; } catch(e){ }
|
|
885
|
+
if (err) {
|
|
886
|
+
resolve({result:{dml:"findOneAndUpdate", headers:"", time:utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
887
|
+
}else{
|
|
888
|
+
resolve({result:{dml:"findOneAndUpdate", headers:getFields(result), time:utl.milisegundosASegundos(time), code:200, error:0}, data:result ? [result] : [], results:{ok: res.ok, lastErrorObject: res.lastErrorObject}});
|
|
889
|
+
}
|
|
890
|
+
try{
|
|
891
|
+
exec.mongoClose(mongoclient);
|
|
892
|
+
}catch(e){
|
|
893
|
+
utl.log('err.findOneAndUpdate '+e);
|
|
894
|
+
}
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
).catch(err=>{
|
|
898
|
+
resolve({result:{dml:"findOneAndUpdate", headers:"", time:0, code:404, error:1}, results:err});
|
|
899
|
+
});
|
|
900
|
+
})
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
const findOneAndDelete=async(options,dbm, col, query)=>{
|
|
904
|
+
const exec = require("./exec");
|
|
905
|
+
const mongoclient = exec.mongoConn(options);
|
|
906
|
+
const start = Date.now();
|
|
907
|
+
|
|
908
|
+
return new Promise((resolve, reject)=>{
|
|
909
|
+
mongoclient.then(
|
|
910
|
+
function(client){
|
|
911
|
+
let db = client.db(dbm);
|
|
912
|
+
db.collection(col).findOneAndDelete(query, function(err, res) {
|
|
913
|
+
const end = Date.now();
|
|
914
|
+
const time = end - start;
|
|
915
|
+
let result;
|
|
916
|
+
try{ result = res.value; } catch(e){ }
|
|
917
|
+
if (err) {
|
|
918
|
+
resolve({result:{dml:"findOneAndDelete", headers:"", time:utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
919
|
+
}else{
|
|
920
|
+
resolve({result:{dml:"findOneAndDelete", headers:getFields(result), time:utl.milisegundosASegundos(time), code:200, error:0}, data:result ? [result] : [], results:{ok: res.ok, lastErrorObject: res.lastErrorObject}});
|
|
921
|
+
}
|
|
922
|
+
try{
|
|
923
|
+
exec.mongoClose(mongoclient);
|
|
924
|
+
}catch(e){
|
|
925
|
+
utl.log('err.findOneAndDelete '+e);
|
|
926
|
+
}
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
).catch(err=>{
|
|
930
|
+
resolve({result:{dml:"findOneAndDelete", headers:"", time:0, code:404, error:1}, results:err});
|
|
931
|
+
});
|
|
932
|
+
})
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
const replaceOne=async(options,dbm, col, query, json_obj)=>{
|
|
936
|
+
const exec = require("./exec");
|
|
937
|
+
const mongoclient = exec.mongoConn(options);
|
|
938
|
+
const start = Date.now();
|
|
939
|
+
|
|
940
|
+
return new Promise((resolve, reject)=>{
|
|
941
|
+
mongoclient.then(
|
|
942
|
+
function(client){
|
|
943
|
+
let db = client.db(dbm);
|
|
944
|
+
db.collection(col).replaceOne(query, json_obj, function(err, res) {
|
|
945
|
+
const end = Date.now();
|
|
946
|
+
const time = end - start;
|
|
947
|
+
let result, modifiedCount=0;
|
|
948
|
+
try{ result = res.result; modifiedCount = res.result.nModified; } catch(e){ }
|
|
949
|
+
if (err) {
|
|
950
|
+
resolve({result:{dml:"replaceOne", headers:"", time:utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
951
|
+
}else{
|
|
952
|
+
resolve({result:{dml:"replaceOne", headers:"replaceOne", time:utl.milisegundosASegundos(time), code:200, error:0}, results:{WriteResult:`${modifiedCount} document replaced`, result }});
|
|
953
|
+
}
|
|
954
|
+
try{
|
|
955
|
+
exec.mongoClose(mongoclient);
|
|
956
|
+
}catch(e){
|
|
957
|
+
utl.log('err.replaceOne '+e);
|
|
958
|
+
}
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
).catch(err=>{
|
|
962
|
+
resolve({result:{dml:"replaceOne", headers:"", time:0, code:404, error:1}, results:err});
|
|
963
|
+
});
|
|
964
|
+
})
|
|
965
|
+
}
|
|
966
|
+
|
|
668
967
|
module.exports ={
|
|
669
968
|
find,
|
|
969
|
+
findOne,
|
|
670
970
|
insert,
|
|
971
|
+
insertMany,
|
|
671
972
|
update,
|
|
973
|
+
updateMany,
|
|
974
|
+
deleteMany,
|
|
975
|
+
deleteOne,
|
|
672
976
|
count,
|
|
673
977
|
dbs,
|
|
674
978
|
collections,
|
|
@@ -678,5 +982,10 @@ module.exports ={
|
|
|
678
982
|
command,
|
|
679
983
|
api,
|
|
680
984
|
drop,
|
|
681
|
-
findMany
|
|
985
|
+
findMany,
|
|
986
|
+
aggregate,
|
|
987
|
+
distinct,
|
|
988
|
+
findOneAndUpdate,
|
|
989
|
+
findOneAndDelete,
|
|
990
|
+
replaceOne
|
|
682
991
|
}
|
package/package.json
CHANGED
package/tests/docker-restart.sh
CHANGED
|
File without changes
|
package/tests/docker-start.sh
CHANGED
|
File without changes
|
package/tests/test-mongodb.js
CHANGED
|
@@ -1,133 +1,254 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Pruebas de
|
|
2
|
+
* Pruebas y Ejemplos de MongoDB
|
|
3
3
|
* Ejecutar: node tests/test-mongodb.js
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
const config = require('./config');
|
|
7
7
|
const mongodb = require('../lib/hookup/mongodb');
|
|
8
8
|
|
|
9
|
-
console.log('===
|
|
9
|
+
console.log('=== EJEMPLOS Y PRUEBAS MONGODB ===\n');
|
|
10
10
|
|
|
11
|
-
async function
|
|
11
|
+
async function ejemplosYPruebas() {
|
|
12
12
|
const dbName = 'test_bonsaif';
|
|
13
13
|
const collection = 'test_collection';
|
|
14
14
|
|
|
15
15
|
try {
|
|
16
|
-
|
|
17
|
-
console.log('
|
|
18
|
-
|
|
16
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
17
|
+
console.log('📝 EJEMPLOS DE INSERT');
|
|
18
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
|
|
19
|
+
|
|
20
|
+
// EJEMPLO 1: INSERT con 1 documento usando api()
|
|
21
|
+
console.log('1️⃣ INSERT con documents[] - UN documento (usa insert internamente):');
|
|
22
|
+
console.log(' Código:');
|
|
23
|
+
console.log(' await mongodb.api(config.mongodb, dbName, {');
|
|
24
|
+
console.log(' insert: collection,');
|
|
25
|
+
console.log(' documents: [{name: "Juan", email: "juan@example.com"}]');
|
|
26
|
+
console.log(' })\n');
|
|
27
|
+
const insertResult = await mongodb.api(
|
|
19
28
|
config.mongodb,
|
|
20
29
|
dbName,
|
|
21
|
-
|
|
22
|
-
|
|
30
|
+
{
|
|
31
|
+
insert: collection,
|
|
32
|
+
documents: [{name: 'Juan Pérez', email: 'juan@example.com', age: 30, created: new Date()}]
|
|
33
|
+
}
|
|
23
34
|
);
|
|
24
|
-
console.log('
|
|
35
|
+
console.log(' ✅ Resultado:', insertResult.result);
|
|
25
36
|
const insertId = insertResult.result.insertId;
|
|
26
|
-
|
|
27
|
-
//
|
|
28
|
-
console.log('\n2
|
|
29
|
-
|
|
37
|
+
|
|
38
|
+
// EJEMPLO 2: INSERT con múltiples documentos usando api()
|
|
39
|
+
console.log('\n2️⃣ INSERT con documents[] - MÚLTIPLES documentos (usa insertMany automáticamente):');
|
|
40
|
+
console.log(' Código:');
|
|
41
|
+
console.log(' await mongodb.api(config.mongodb, dbName, {');
|
|
42
|
+
console.log(' insert: collection,');
|
|
43
|
+
console.log(' documents: [');
|
|
44
|
+
console.log(' {name: "María", email: "maria@example.com"},');
|
|
45
|
+
console.log(' {name: "Pedro", email: "pedro@example.com"},');
|
|
46
|
+
console.log(' {name: "Ana", email: "ana@example.com"}');
|
|
47
|
+
console.log(' ]');
|
|
48
|
+
console.log(' })\n');
|
|
49
|
+
const insertManyResult = await mongodb.api(
|
|
30
50
|
config.mongodb,
|
|
31
51
|
dbName,
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
52
|
+
{
|
|
53
|
+
insert: collection,
|
|
54
|
+
documents: [
|
|
55
|
+
{name: 'María López', email: 'maria@example.com', age: 25, status: 'active'},
|
|
56
|
+
{name: 'Pedro Ramírez', email: 'pedro@example.com', age: 35, status: 'active'},
|
|
57
|
+
{name: 'Ana García', email: 'ana@example.com', age: 28, status: 'inactive'}
|
|
58
|
+
]
|
|
59
|
+
}
|
|
36
60
|
);
|
|
37
|
-
console.log('
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
61
|
+
console.log(' ✅ Resultado:', insertManyResult.result);
|
|
62
|
+
console.log(' ✅ Documentos insertados:', insertManyResult.result.insertedCount);
|
|
63
|
+
|
|
64
|
+
console.log('\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
65
|
+
console.log('📝 EJEMPLOS DE UPDATE');
|
|
66
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
|
|
67
|
+
|
|
68
|
+
// EJEMPLO 3: UPDATE con 1 documento
|
|
69
|
+
console.log('3️⃣ UPDATE con documents[] - UN documento (usa update/updateOne):');
|
|
70
|
+
console.log(' Código:');
|
|
71
|
+
console.log(' await mongodb.api(config.mongodb, dbName, {');
|
|
72
|
+
console.log(' update: collection,');
|
|
73
|
+
console.log(' filter: {name: "Juan Pérez"},');
|
|
74
|
+
console.log(' documents: [{email: "juannuevo@example.com", updated: new Date()}]');
|
|
75
|
+
console.log(' })\n');
|
|
76
|
+
const updateResult = await mongodb.api(
|
|
45
77
|
config.mongodb,
|
|
46
78
|
dbName,
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
79
|
+
{
|
|
80
|
+
update: collection,
|
|
81
|
+
filter: {name: 'Juan Pérez'},
|
|
82
|
+
documents: [{email: 'juannuevo@example.com', updated: new Date()}]
|
|
83
|
+
}
|
|
50
84
|
);
|
|
51
|
-
console.log('
|
|
52
|
-
|
|
53
|
-
//
|
|
54
|
-
console.log('\n4
|
|
55
|
-
|
|
85
|
+
console.log(' ✅ Resultado:', updateResult.result);
|
|
86
|
+
|
|
87
|
+
// EJEMPLO 4: UPDATEMANY con múltiples documentos
|
|
88
|
+
console.log('\n4️⃣ UPDATE con documents[] - MÚLTIPLES documentos (usa updateMany):');
|
|
89
|
+
console.log(' Código:');
|
|
90
|
+
console.log(' await mongodb.api(config.mongodb, dbName, {');
|
|
91
|
+
console.log(' updateMany: collection,');
|
|
92
|
+
console.log(' filter: {status: "active"},');
|
|
93
|
+
console.log(' documents: [{status: "verified", verifiedAt: new Date()}]');
|
|
94
|
+
console.log(' })\n');
|
|
95
|
+
const updateManyResult = await mongodb.api(
|
|
56
96
|
config.mongodb,
|
|
57
97
|
dbName,
|
|
58
|
-
|
|
59
|
-
|
|
98
|
+
{
|
|
99
|
+
updateMany: collection,
|
|
100
|
+
filter: {status: 'active'},
|
|
101
|
+
documents: [{status: 'verified', verifiedAt: new Date()}]
|
|
102
|
+
}
|
|
60
103
|
);
|
|
61
|
-
console.log('
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
console.log('
|
|
65
|
-
|
|
104
|
+
console.log(' ✅ Resultado:', updateManyResult.result);
|
|
105
|
+
|
|
106
|
+
console.log('\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
107
|
+
console.log('📝 EJEMPLOS DE OTROS COMANDOS');
|
|
108
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
|
|
109
|
+
|
|
110
|
+
// EJEMPLO 5: FINDONE
|
|
111
|
+
console.log('5️⃣ FINDONE - Buscar un documento:');
|
|
112
|
+
console.log(' Código:');
|
|
113
|
+
console.log(' await mongodb.api(config.mongodb, dbName, {');
|
|
114
|
+
console.log(' findOne: collection,');
|
|
115
|
+
console.log(' filter: {name: "Juan Pérez"}');
|
|
116
|
+
console.log(' })\n');
|
|
117
|
+
const findOneResult = await mongodb.api(
|
|
66
118
|
config.mongodb,
|
|
67
119
|
dbName,
|
|
68
|
-
{
|
|
120
|
+
{
|
|
121
|
+
findOne: collection,
|
|
122
|
+
filter: {name: 'Juan Pérez'}
|
|
123
|
+
}
|
|
69
124
|
);
|
|
70
|
-
console.log('
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
console.log('
|
|
76
|
-
|
|
125
|
+
console.log(' ✅ Encontrado:', findOneResult.data?.[0]?.name);
|
|
126
|
+
|
|
127
|
+
// EJEMPLO 6: AGGREGATE
|
|
128
|
+
console.log('\n6️⃣ AGGREGATE - Pipeline de agregación:');
|
|
129
|
+
console.log(' Código:');
|
|
130
|
+
console.log(' await mongodb.api(config.mongodb, dbName, {');
|
|
131
|
+
console.log(' aggregate: collection,');
|
|
132
|
+
console.log(' pipeline: [');
|
|
133
|
+
console.log(' {$match: {status: "verified"}},');
|
|
134
|
+
console.log(' {$group: {_id: "$status", count: {$sum: 1}}}');
|
|
135
|
+
console.log(' ]');
|
|
136
|
+
console.log(' })\n');
|
|
137
|
+
const aggregateResult = await mongodb.api(
|
|
77
138
|
config.mongodb,
|
|
78
139
|
dbName,
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
140
|
+
{
|
|
141
|
+
aggregate: collection,
|
|
142
|
+
pipeline: [
|
|
143
|
+
{$match: {status: 'verified'}},
|
|
144
|
+
{$group: {_id: '$status', total: {$sum: 1}, avgAge: {$avg: '$age'}}}
|
|
145
|
+
]
|
|
146
|
+
}
|
|
82
147
|
);
|
|
83
|
-
console.log('
|
|
84
|
-
|
|
85
|
-
//
|
|
86
|
-
console.log('\n7
|
|
87
|
-
|
|
148
|
+
console.log(' ✅ Resultado:', aggregateResult.data);
|
|
149
|
+
|
|
150
|
+
// EJEMPLO 7: DISTINCT
|
|
151
|
+
console.log('\n7️⃣ DISTINCT - Valores únicos:');
|
|
152
|
+
console.log(' Código:');
|
|
153
|
+
console.log(' await mongodb.api(config.mongodb, dbName, {');
|
|
154
|
+
console.log(' distinct: collection,');
|
|
155
|
+
console.log(' field: "status",');
|
|
156
|
+
console.log(' filter: {}');
|
|
157
|
+
console.log(' })\n');
|
|
158
|
+
const distinctResult = await mongodb.api(
|
|
88
159
|
config.mongodb,
|
|
89
160
|
dbName,
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
161
|
+
{
|
|
162
|
+
distinct: collection,
|
|
163
|
+
field: 'status',
|
|
164
|
+
filter: {}
|
|
165
|
+
}
|
|
93
166
|
);
|
|
94
|
-
console.log('
|
|
95
|
-
|
|
96
|
-
//
|
|
97
|
-
console.log('\n8
|
|
98
|
-
|
|
99
|
-
console.log('
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
console.log('
|
|
167
|
+
console.log(' ✅ Valores únicos de status:', distinctResult.data);
|
|
168
|
+
|
|
169
|
+
// EJEMPLO 8: REPLACEONE
|
|
170
|
+
console.log('\n8️⃣ REPLACEONE - Reemplazar documento completo:');
|
|
171
|
+
console.log(' Código:');
|
|
172
|
+
console.log(' await mongodb.api(config.mongodb, dbName, {');
|
|
173
|
+
console.log(' replaceOne: collection,');
|
|
174
|
+
console.log(' filter: {name: "Ana García"},');
|
|
175
|
+
console.log(' documents: [{name: "Ana García Updated", email: "ananueva@example.com", replaced: true}]');
|
|
176
|
+
console.log(' })\n');
|
|
177
|
+
const replaceOneResult = await mongodb.api(
|
|
178
|
+
config.mongodb,
|
|
179
|
+
dbName,
|
|
180
|
+
{
|
|
181
|
+
replaceOne: collection,
|
|
182
|
+
filter: {name: 'Ana García'},
|
|
183
|
+
documents: [{name: 'Ana García Updated', email: 'ananueva@example.com', replaced: true}]
|
|
184
|
+
}
|
|
185
|
+
);
|
|
186
|
+
console.log(' ✅ Resultado:', replaceOneResult.result);
|
|
187
|
+
|
|
188
|
+
// EJEMPLO 9: FINDONEANDUPDATE
|
|
189
|
+
console.log('\n9️⃣ FINDONEANDUPDATE - Buscar y actualizar (retorna el documento):');
|
|
190
|
+
console.log(' Código:');
|
|
191
|
+
console.log(' await mongodb.api(config.mongodb, dbName, {');
|
|
192
|
+
console.log(' findOneAndUpdate: collection,');
|
|
193
|
+
console.log(' filter: {name: "María López"},');
|
|
194
|
+
console.log(' documents: [{score: 100}],');
|
|
195
|
+
console.log(' returnNew: true // true = retorna actualizado, false = retorna anterior');
|
|
196
|
+
console.log(' })\n');
|
|
197
|
+
const findOneAndUpdateResult = await mongodb.api(
|
|
198
|
+
config.mongodb,
|
|
199
|
+
dbName,
|
|
200
|
+
{
|
|
201
|
+
findOneAndUpdate: collection,
|
|
202
|
+
filter: {name: 'María López'},
|
|
203
|
+
documents: [{score: 100, modified: true}],
|
|
204
|
+
returnNew: true
|
|
205
|
+
}
|
|
206
|
+
);
|
|
207
|
+
console.log(' ✅ Documento actualizado:', findOneAndUpdateResult.data?.[0]);
|
|
208
|
+
|
|
209
|
+
// EJEMPLO 10: UPSERT
|
|
210
|
+
console.log('\n🔟 UPSERT - Actualizar si existe, insertar si no existe:');
|
|
211
|
+
console.log(' Código:');
|
|
212
|
+
console.log(' await mongodb.api(config.mongodb, dbName, {');
|
|
213
|
+
console.log(' upsert: collection,');
|
|
214
|
+
console.log(' filter: {name: "Usuario Nuevo"},');
|
|
215
|
+
console.log(' document: {name: "Usuario Nuevo", email: "nuevo@example.com"}');
|
|
216
|
+
console.log(' })\n');
|
|
217
|
+
const upsertResult = await mongodb.api(
|
|
218
|
+
config.mongodb,
|
|
219
|
+
dbName,
|
|
220
|
+
{
|
|
221
|
+
upsert: collection,
|
|
222
|
+
filter: {name: 'Usuario Nuevo'},
|
|
223
|
+
document: {name: 'Usuario Nuevo', email: 'nuevo@example.com', created: new Date()}
|
|
224
|
+
}
|
|
225
|
+
);
|
|
226
|
+
console.log(' ✅ Resultado:', upsertResult.result);
|
|
227
|
+
|
|
228
|
+
// Limpieza
|
|
229
|
+
console.log('\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
230
|
+
console.log('🧹 Limpiando datos de prueba...');
|
|
114
231
|
const deleteResult = await mongodb.api(
|
|
115
232
|
config.mongodb,
|
|
116
233
|
dbName,
|
|
117
234
|
{
|
|
118
235
|
deleteMany: collection,
|
|
119
|
-
filter: {
|
|
236
|
+
filter: {}
|
|
120
237
|
}
|
|
121
238
|
);
|
|
122
|
-
console.log('
|
|
239
|
+
console.log('✅ Limpieza completada\n');
|
|
123
240
|
|
|
124
|
-
console.log('
|
|
241
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
242
|
+
console.log('✅ TODOS LOS EJEMPLOS Y PRUEBAS COMPLETADOS');
|
|
243
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
|
|
244
|
+
process.exit(0);
|
|
125
245
|
|
|
126
246
|
} catch (error) {
|
|
127
|
-
console.error('\n=== ✗ ERROR EN
|
|
247
|
+
console.error('\n=== ✗ ERROR EN EJEMPLOS ===');
|
|
128
248
|
console.error(error);
|
|
129
249
|
process.exit(1);
|
|
130
250
|
}
|
|
131
251
|
}
|
|
132
252
|
|
|
133
|
-
|
|
253
|
+
ejemplosYPruebas();
|
|
254
|
+
|