Skip to content

Commit cd3282f

Browse files
committed
Added: nosql.count().
1 parent d8f20ed commit cd3282f

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

nosql.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ Database.prototype.find = function(view) {
170170
return builder;
171171
};
172172

173-
Database.prototype.max = function(name, view) {
173+
Database.prototype.count = function(view) {
174174
var self = this;
175175
var builder = new DatabaseBuilder();
176176

177177
if (view) {
178-
self.pending_reader_view.push({ builder: builder, count: 0, view: view });
178+
self.pending_reader_view.push({ builder: builder, count: 0, view: view, type: 1 });
179179
setImmediate(() => self.next(6));
180180
} else {
181-
self.pending_reader.push({ builder: builder, count: 0, view: view });
181+
self.pending_reader.push({ builder: builder, count: 0, view: view, type: 1 });
182182
setImmediate(() => self.next(4));
183183
}
184184

@@ -519,6 +519,9 @@ Database.prototype.$reader2 = function(filename, items, callback) {
519519
continue;
520520
}
521521

522+
if (item.type)
523+
continue;
524+
522525
if (item.response) {
523526
item.response.push(output);
524527
continue;
@@ -536,6 +539,7 @@ Database.prototype.$reader2 = function(filename, items, callback) {
536539
var output;
537540

538541
if (!builder.$sort) {
542+
539543
if (builder.$first)
540544
output = item.response ? item.response[0] : undefined;
541545
else
@@ -544,13 +548,13 @@ Database.prototype.$reader2 = function(filename, items, callback) {
544548
if (builder.$cache_key)
545549
framework.cache.add(builder.$cache_key, { items: output, count: item.count }, builder.$cache_expire);
546550

547-
builder.$callback(errorhandling(null, builder, output), output, item.count);
551+
builder.$callback(errorhandling(null, builder, output), item.type === 1 ? item.count : output, item.count);
548552
continue;
549553
}
550554

551555
if (item.count) {
552556
if (builder.$sort.name)
553-
item.response.orderBy(builder.$sort.name, builder.$sort.asc);
557+
item.response.quicksort(builder.$sort.name, builder.$sort.asc);
554558
else
555559
item.response.sort(builder.$sort);
556560
if (builder.$skip && builder.$take)
@@ -569,7 +573,7 @@ Database.prototype.$reader2 = function(filename, items, callback) {
569573
if (builder.$cache_key)
570574
framework.cache.add(builder.$cache_key, { items: output, count: item.count }, builder.$cache_expire);
571575

572-
builder.$callback(errorhandling(null, builder, output), output, item.count);
576+
builder.$callback(errorhandling(null, builder, output), item.type === 1 ? item.count : output, item.count);
573577
builder.done();
574578
}
575579

@@ -624,7 +628,8 @@ Database.prototype.$views = function() {
624628
continue;
625629
}
626630

627-
response[j].response.push(output);
631+
if (!item.type)
632+
response[j].response.push(output);
628633
}
629634
}));
630635

@@ -636,10 +641,9 @@ Database.prototype.$views = function() {
636641

637642
if (builder.$sort) {
638643
if (builder.$sort.name)
639-
item.response.orderBy(builder.$sort.name, builder.$sort.asc);
644+
item.response.quicksort(builder.$sort.name, builder.$sort.asc);
640645
else
641646
item.response.sort(builder.$sort);
642-
643647
if (builder.$skip && builder.$take)
644648
item.response = item.response.splice(builder.$skip, builder.$take);
645649
else if (builder.$skip)

0 commit comments

Comments
 (0)