bonsaif 1.10.41 → 1.10.43
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 +354 -44
- package/lib/hookup/redis.js +2 -2
- 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
|
|
|
@@ -320,6 +326,7 @@ const buildQuery=(field,value)=>{
|
|
|
320
326
|
//console.log('tipo:'+tipo);
|
|
321
327
|
switch (tipo){
|
|
322
328
|
case 'id':
|
|
329
|
+
case '_id':
|
|
323
330
|
eval('where.'+ltField[i]+' = new ObjectId('+val+'); ');
|
|
324
331
|
break;
|
|
325
332
|
|
|
@@ -607,7 +614,8 @@ const api=async(options,dbm, json)=>{
|
|
|
607
614
|
}
|
|
608
615
|
|
|
609
616
|
if (utl.ObjectEmpty(json, 'insert')){
|
|
610
|
-
dml = 'insert';
|
|
617
|
+
dml = 'insert';
|
|
618
|
+
col = json.insert;
|
|
611
619
|
json_api = json.documents?json.documents:[];
|
|
612
620
|
if (json_api.length==1){
|
|
613
621
|
json_api = json.documents[0];
|
|
@@ -617,7 +625,8 @@ const api=async(options,dbm, json)=>{
|
|
|
617
625
|
}
|
|
618
626
|
|
|
619
627
|
if (utl.ObjectEmpty(json, 'update')){
|
|
620
|
-
dml = 'update';
|
|
628
|
+
dml = 'update';
|
|
629
|
+
col = json.update;
|
|
621
630
|
json_api = json.documents?json.documents:[];
|
|
622
631
|
if (json_api.length==1){
|
|
623
632
|
json_api = json.documents[0];
|
|
@@ -627,7 +636,8 @@ const api=async(options,dbm, json)=>{
|
|
|
627
636
|
}
|
|
628
637
|
|
|
629
638
|
if (utl.ObjectEmpty(json, 'updateMany')){
|
|
630
|
-
dml = 'updateMany';
|
|
639
|
+
dml = 'updateMany';
|
|
640
|
+
col = json.updateMany;
|
|
631
641
|
json_api = json.documents?json.documents:[];
|
|
632
642
|
if (json_api.length==1){
|
|
633
643
|
json_api = json.documents[0];
|
|
@@ -636,7 +646,31 @@ const api=async(options,dbm, json)=>{
|
|
|
636
646
|
}
|
|
637
647
|
}
|
|
638
648
|
|
|
639
|
-
|
|
649
|
+
if (utl.ObjectEmpty(json, 'replaceOne')){
|
|
650
|
+
dml = 'replaceOne';
|
|
651
|
+
col = json.replaceOne;
|
|
652
|
+
json_api = json.documents?json.documents:[];
|
|
653
|
+
if (json_api.length==1){
|
|
654
|
+
json_api = json.documents[0];
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
if (utl.ObjectEmpty(json, 'findOneAndUpdate')){
|
|
659
|
+
dml = 'findOneAndUpdate';
|
|
660
|
+
col = json.findOneAndUpdate;
|
|
661
|
+
json_api = json.documents?json.documents:[];
|
|
662
|
+
if (json_api.length==1){
|
|
663
|
+
json_api = json.documents[0];
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
if (utl.ObjectEmpty(json, 'insertMany')){
|
|
668
|
+
dml = 'insertMany';
|
|
669
|
+
col = json.insertMany;
|
|
670
|
+
json_api = json.documents?json.documents:[];
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
let ltdml = ['find','findOne','delete','deleteOne','deleteMany','count','dbs','collections','drop','aggregate','distinct','findOneAndDelete'];
|
|
640
674
|
for (let x of ltdml){
|
|
641
675
|
if (utl.ObjectEmpty(json,x)){
|
|
642
676
|
dml = x; break;
|
|
@@ -644,20 +678,30 @@ const api=async(options,dbm, json)=>{
|
|
|
644
678
|
}
|
|
645
679
|
dml==''? dml='command':'';
|
|
646
680
|
|
|
647
|
-
|
|
681
|
+
if (!col){
|
|
682
|
+
eval(` try{ col = json.${dml}; } catch(e){ } `);
|
|
683
|
+
}
|
|
648
684
|
let r;
|
|
649
685
|
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
|
-
|
|
686
|
+
case 'insert': r = await insert(options,dbm,col, json_api); break;
|
|
687
|
+
case 'insertMany': r = await insertMany(options,dbm,col, json_api); break;
|
|
688
|
+
case 'upsert': r = await upsert(options,dbm,col, json_api, query); break;
|
|
689
|
+
case 'update': r = await update(options,dbm,col, json_api, query); break;
|
|
690
|
+
case 'updateMany': r = await updateMany(options,dbm,col, json_api, query); break;
|
|
691
|
+
case 'deleteOne': r = await deleteOne(options,dbm,col, query); break;
|
|
692
|
+
case 'deleteMany': r = await deleteMany(options,dbm,col, json); break;
|
|
693
|
+
case 'count': r = await count(options,dbm,col, json); break;
|
|
694
|
+
case 'dbs': r = await dbs(options); break;
|
|
695
|
+
case 'collections': r = await collections(options,dbm); break;
|
|
696
|
+
case 'drop': r = await drop(options,dbm,col); break;
|
|
697
|
+
case 'find': r = await findMany(options,dbm,col,json); break;
|
|
698
|
+
case 'findOne': r = await findOne(options,dbm,col, query, json.sort); break;
|
|
699
|
+
case 'aggregate': r = await aggregate(options,dbm,col, json.pipeline); break;
|
|
700
|
+
case 'distinct': r = await distinct(options,dbm,col, json.field, query); break;
|
|
701
|
+
case 'findOneAndUpdate': r = await findOneAndUpdate(options,dbm,col, query, json_api, json.returnNew); break;
|
|
702
|
+
case 'findOneAndDelete': r = await findOneAndDelete(options,dbm,col, query); break;
|
|
703
|
+
case 'replaceOne': r = await replaceOne(options,dbm,col, query, json_api); break;
|
|
704
|
+
default: r = await command(options,dbm,json);
|
|
661
705
|
}
|
|
662
706
|
|
|
663
707
|
return new Promise((resolve, reject)=>{
|
|
@@ -665,10 +709,271 @@ const api=async(options,dbm, json)=>{
|
|
|
665
709
|
})
|
|
666
710
|
}
|
|
667
711
|
|
|
712
|
+
const findOne=async(options,dbm, col, query, orderby)=>{
|
|
713
|
+
const exec = require("./exec");
|
|
714
|
+
const mongoclient = exec.mongoConn(options);
|
|
715
|
+
orderby = orderby!=null?orderby:'';
|
|
716
|
+
const start = Date.now();
|
|
717
|
+
|
|
718
|
+
return new Promise((resolve, reject)=>{
|
|
719
|
+
mongoclient.then(
|
|
720
|
+
function(client){
|
|
721
|
+
let db = client.db(dbm);
|
|
722
|
+
db.collection(col).findOne(query, {sort: orderby}, function(err, result) {
|
|
723
|
+
const end = Date.now();
|
|
724
|
+
const time = end - start;
|
|
725
|
+
|
|
726
|
+
if (err) {
|
|
727
|
+
resolve({"result":{"dml":"findOne", "headers":"", "time":utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
728
|
+
}else{
|
|
729
|
+
resolve({"result":{"dml":"findOne", "headers":getFields(result), "time":utl.milisegundosASegundos(time), code:200, error:0}, data:result ? [result] : []});
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
try{
|
|
733
|
+
exec.mongoClose(mongoclient);
|
|
734
|
+
}catch(e){
|
|
735
|
+
utl.log('err.findOne '+e);
|
|
736
|
+
}
|
|
737
|
+
});
|
|
738
|
+
}
|
|
739
|
+
).catch(err=>{
|
|
740
|
+
resolve({"result":{"dml":"findOne", "headers":"", time:0, code:404, error:1}, "results":err});
|
|
741
|
+
});
|
|
742
|
+
})
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
const deleteOne=async(options,dbm, col, query)=>{
|
|
746
|
+
const exec = require("./exec");
|
|
747
|
+
const mongoclient = exec.mongoConn(options);
|
|
748
|
+
const start = Date.now();
|
|
749
|
+
|
|
750
|
+
return new Promise((resolve, reject)=>{
|
|
751
|
+
mongoclient.then(
|
|
752
|
+
function(client){
|
|
753
|
+
let db = client.db(dbm);
|
|
754
|
+
db.collection(col).deleteOne(query, function(err, res) {
|
|
755
|
+
const end = Date.now();
|
|
756
|
+
const time = end - start;
|
|
757
|
+
let result, deletedCount=0;
|
|
758
|
+
try{ result = res.result; deletedCount = res.result.n; } catch(e){ }
|
|
759
|
+
if (err) {
|
|
760
|
+
resolve({result:{dml:"deleteOne", headers:"", time:utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
761
|
+
}else{
|
|
762
|
+
resolve({result:{dml:"deleteOne", headers:"deletedCount", time:utl.milisegundosASegundos(time), code:200, error:0}, results:{WriteResult:`${deletedCount} document deleted`, result }});
|
|
763
|
+
}
|
|
764
|
+
try{
|
|
765
|
+
exec.mongoClose(mongoclient);
|
|
766
|
+
}catch(e){
|
|
767
|
+
utl.log('err.deleteOne '+e);
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
}
|
|
771
|
+
).catch(err=>{
|
|
772
|
+
resolve({result:{dml:"deleteOne", headers:"", time:0, code:404, error:1}, results:err});
|
|
773
|
+
});
|
|
774
|
+
})
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
const insertMany=async(options,dbm, col, documents)=>{
|
|
778
|
+
const exec = require("./exec");
|
|
779
|
+
const mongoclient = exec.mongoConn(options);
|
|
780
|
+
const start = Date.now();
|
|
781
|
+
|
|
782
|
+
return new Promise((resolve, reject)=>{
|
|
783
|
+
mongoclient.then(
|
|
784
|
+
function(client){
|
|
785
|
+
let db = client.db(dbm);
|
|
786
|
+
db.collection(col).insertMany(documents, function(err, res) {
|
|
787
|
+
const end = Date.now();
|
|
788
|
+
const time = end - start;
|
|
789
|
+
if (err) {
|
|
790
|
+
resolve({result:{dml:"insertMany", headers:"", time:utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
791
|
+
}else{
|
|
792
|
+
let ops=res.ops!=null?res.ops:{};
|
|
793
|
+
resolve({result:{dml:"insertMany", headers:"insertedCount", insertedCount:res.insertedCount, time:utl.milisegundosASegundos(time), code:200, error:0}, results:{WriteResult:`${res.insertedCount} documents inserted`, ops}});
|
|
794
|
+
}
|
|
795
|
+
try{
|
|
796
|
+
exec.mongoClose(mongoclient);
|
|
797
|
+
}catch(e){
|
|
798
|
+
utl.log('err.insertMany '+e);
|
|
799
|
+
}
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
).catch(err=>{
|
|
803
|
+
resolve({"result":{"dml":"insertMany", "headers":"", time:0, code:404, error:1}, results:err});
|
|
804
|
+
});
|
|
805
|
+
})
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
const aggregate=async(options,dbm, col, pipeline)=>{
|
|
809
|
+
const exec = require("./exec");
|
|
810
|
+
const mongoclient = exec.mongoConn(options);
|
|
811
|
+
const start = Date.now();
|
|
812
|
+
|
|
813
|
+
return new Promise((resolve, reject)=>{
|
|
814
|
+
mongoclient.then(
|
|
815
|
+
function(client){
|
|
816
|
+
let db = client.db(dbm);
|
|
817
|
+
db.collection(col).aggregate(pipeline).toArray(function(err, result) {
|
|
818
|
+
const end = Date.now();
|
|
819
|
+
const time = end - start;
|
|
820
|
+
|
|
821
|
+
if (err) {
|
|
822
|
+
resolve({"result":{"dml":"aggregate", "headers":"", "time":utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
823
|
+
}else{
|
|
824
|
+
resolve({"result":{"dml":"aggregate", "headers":getFields(result[0]), "time":utl.milisegundosASegundos(time), code:200, error:0}, data:result});
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
try{
|
|
828
|
+
exec.mongoClose(mongoclient);
|
|
829
|
+
}catch(e){
|
|
830
|
+
utl.log('err.aggregate '+e);
|
|
831
|
+
}
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
).catch(err=>{
|
|
835
|
+
resolve({"result":{"dml":"aggregate", "headers":"", time:0, code:404, error:1}, "results":err});
|
|
836
|
+
});
|
|
837
|
+
})
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
const distinct=async(options,dbm, col, field, query={})=>{
|
|
841
|
+
const exec = require("./exec");
|
|
842
|
+
const mongoclient = exec.mongoConn(options);
|
|
843
|
+
const start = Date.now();
|
|
844
|
+
|
|
845
|
+
return new Promise((resolve, reject)=>{
|
|
846
|
+
mongoclient.then(
|
|
847
|
+
function(client){
|
|
848
|
+
let db = client.db(dbm);
|
|
849
|
+
db.collection(col).distinct(field, query, function(err, result) {
|
|
850
|
+
const end = Date.now();
|
|
851
|
+
const time = end - start;
|
|
852
|
+
|
|
853
|
+
if (err) {
|
|
854
|
+
resolve({"result":{"dml":"distinct", "headers":"", "time":utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
855
|
+
}else{
|
|
856
|
+
resolve({"result":{"dml":"distinct", "headers":field, "time":utl.milisegundosASegundos(time), code:200, error:0}, data:result});
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
try{
|
|
860
|
+
exec.mongoClose(mongoclient);
|
|
861
|
+
}catch(e){
|
|
862
|
+
utl.log('err.distinct '+e);
|
|
863
|
+
}
|
|
864
|
+
});
|
|
865
|
+
}
|
|
866
|
+
).catch(err=>{
|
|
867
|
+
resolve({"result":{"dml":"distinct", "headers":"", time:0, code:404, error:1}, "results":err});
|
|
868
|
+
});
|
|
869
|
+
})
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
const findOneAndUpdate=async(options,dbm, col, query, json_obj, returnNew=true)=>{
|
|
873
|
+
const exec = require("./exec");
|
|
874
|
+
const mongoclient = exec.mongoConn(options);
|
|
875
|
+
const start = Date.now();
|
|
876
|
+
|
|
877
|
+
return new Promise((resolve, reject)=>{
|
|
878
|
+
mongoclient.then(
|
|
879
|
+
function(client){
|
|
880
|
+
let db = client.db(dbm);
|
|
881
|
+
db.collection(col).findOneAndUpdate(query, { $set: json_obj }, { returnDocument: returnNew ? 'after' : 'before' }, function(err, res) {
|
|
882
|
+
const end = Date.now();
|
|
883
|
+
const time = end - start;
|
|
884
|
+
let result;
|
|
885
|
+
try{ result = res.value; } catch(e){ }
|
|
886
|
+
if (err) {
|
|
887
|
+
resolve({result:{dml:"findOneAndUpdate", headers:"", time:utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
888
|
+
}else{
|
|
889
|
+
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}});
|
|
890
|
+
}
|
|
891
|
+
try{
|
|
892
|
+
exec.mongoClose(mongoclient);
|
|
893
|
+
}catch(e){
|
|
894
|
+
utl.log('err.findOneAndUpdate '+e);
|
|
895
|
+
}
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
).catch(err=>{
|
|
899
|
+
resolve({result:{dml:"findOneAndUpdate", headers:"", time:0, code:404, error:1}, results:err});
|
|
900
|
+
});
|
|
901
|
+
})
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
const findOneAndDelete=async(options,dbm, col, query)=>{
|
|
905
|
+
const exec = require("./exec");
|
|
906
|
+
const mongoclient = exec.mongoConn(options);
|
|
907
|
+
const start = Date.now();
|
|
908
|
+
|
|
909
|
+
return new Promise((resolve, reject)=>{
|
|
910
|
+
mongoclient.then(
|
|
911
|
+
function(client){
|
|
912
|
+
let db = client.db(dbm);
|
|
913
|
+
db.collection(col).findOneAndDelete(query, function(err, res) {
|
|
914
|
+
const end = Date.now();
|
|
915
|
+
const time = end - start;
|
|
916
|
+
let result;
|
|
917
|
+
try{ result = res.value; } catch(e){ }
|
|
918
|
+
if (err) {
|
|
919
|
+
resolve({result:{dml:"findOneAndDelete", headers:"", time:utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
920
|
+
}else{
|
|
921
|
+
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}});
|
|
922
|
+
}
|
|
923
|
+
try{
|
|
924
|
+
exec.mongoClose(mongoclient);
|
|
925
|
+
}catch(e){
|
|
926
|
+
utl.log('err.findOneAndDelete '+e);
|
|
927
|
+
}
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
).catch(err=>{
|
|
931
|
+
resolve({result:{dml:"findOneAndDelete", headers:"", time:0, code:404, error:1}, results:err});
|
|
932
|
+
});
|
|
933
|
+
})
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
const replaceOne=async(options,dbm, col, query, json_obj)=>{
|
|
937
|
+
const exec = require("./exec");
|
|
938
|
+
const mongoclient = exec.mongoConn(options);
|
|
939
|
+
const start = Date.now();
|
|
940
|
+
|
|
941
|
+
return new Promise((resolve, reject)=>{
|
|
942
|
+
mongoclient.then(
|
|
943
|
+
function(client){
|
|
944
|
+
let db = client.db(dbm);
|
|
945
|
+
db.collection(col).replaceOne(query, json_obj, function(err, res) {
|
|
946
|
+
const end = Date.now();
|
|
947
|
+
const time = end - start;
|
|
948
|
+
let result, modifiedCount=0;
|
|
949
|
+
try{ result = res.result; modifiedCount = res.result.nModified; } catch(e){ }
|
|
950
|
+
if (err) {
|
|
951
|
+
resolve({result:{dml:"replaceOne", headers:"", time:utl.milisegundosASegundos(time), code:204, error:1, msg:err}, results:err});
|
|
952
|
+
}else{
|
|
953
|
+
resolve({result:{dml:"replaceOne", headers:"replaceOne", time:utl.milisegundosASegundos(time), code:200, error:0}, results:{WriteResult:`${modifiedCount} document replaced`, result }});
|
|
954
|
+
}
|
|
955
|
+
try{
|
|
956
|
+
exec.mongoClose(mongoclient);
|
|
957
|
+
}catch(e){
|
|
958
|
+
utl.log('err.replaceOne '+e);
|
|
959
|
+
}
|
|
960
|
+
});
|
|
961
|
+
}
|
|
962
|
+
).catch(err=>{
|
|
963
|
+
resolve({result:{dml:"replaceOne", headers:"", time:0, code:404, error:1}, results:err});
|
|
964
|
+
});
|
|
965
|
+
})
|
|
966
|
+
}
|
|
967
|
+
|
|
668
968
|
module.exports ={
|
|
669
969
|
find,
|
|
970
|
+
findOne,
|
|
670
971
|
insert,
|
|
972
|
+
insertMany,
|
|
671
973
|
update,
|
|
974
|
+
updateMany,
|
|
975
|
+
deleteMany,
|
|
976
|
+
deleteOne,
|
|
672
977
|
count,
|
|
673
978
|
dbs,
|
|
674
979
|
collections,
|
|
@@ -678,5 +983,10 @@ module.exports ={
|
|
|
678
983
|
command,
|
|
679
984
|
api,
|
|
680
985
|
drop,
|
|
681
|
-
findMany
|
|
986
|
+
findMany,
|
|
987
|
+
aggregate,
|
|
988
|
+
distinct,
|
|
989
|
+
findOneAndUpdate,
|
|
990
|
+
findOneAndDelete,
|
|
991
|
+
replaceOne
|
|
682
992
|
}
|
package/lib/hookup/redis.js
CHANGED
|
@@ -560,7 +560,7 @@ const del=async( options,db, key )=>{ //DEL key [key ...]
|
|
|
560
560
|
|
|
561
561
|
// Busca llaves de un hash con la combinacion de busquedas similar a un where
|
|
562
562
|
// 2022/04/26 VRSZ Se cambia la busqueda de rows
|
|
563
|
-
const hfind=async(options,db, key, filter, order, limit=
|
|
563
|
+
const hfind=async(options,db, key, filter, order, limit=1000)=>{
|
|
564
564
|
let {field:orderBy, val:orderVal} = getFirstField(order);
|
|
565
565
|
|
|
566
566
|
//2022/10/14 VRSZ Se realiza regla para combinar sentencias IN, AND, y/o OR
|
|
@@ -882,7 +882,7 @@ const api=async(options, dbm, json)=>{
|
|
|
882
882
|
let order = json.order!=null ? json.order:{};
|
|
883
883
|
let counter = json.counter!=null?json.counter:0;
|
|
884
884
|
let toJson = json.toJson!=null?json.toJson:0;
|
|
885
|
-
let limit = json.limit!=null?json.limit:
|
|
885
|
+
let limit = json.limit!=null?json.limit:1000;
|
|
886
886
|
|
|
887
887
|
let lstart = json.start!=null?json.start*1:0;
|
|
888
888
|
let lstop = json.stop!=null?json.stop*1:-1;
|
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
|
+
|