Skip to content

Commit

Permalink
v3.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lroal committed May 8, 2024
1 parent e3424ad commit 03dfd3b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ The following operators are supported:
- max
- avg
You can also elevate associated data to the root level for easier access. In the example below, <i>balance</i> of the customer is elevated to the root level.
You can also elevate associated data to a parent level for easier access. In the example below, <i>balance</i> of the customer is elevated to the root level.
```javascript
import map from './map';
Expand Down Expand Up @@ -1766,7 +1766,7 @@ Supported functions include:
__On each row__
For each row we are counting the number of lines.
You can also elevate associated data to the root level for easier access. In the example below, <i>balance</i> of the customer is elevated to the root level.
You can also elevate associated data to the a parent level for easier access. In the example below, <i>balance</i> of the customer is elevated to the root level.
```javascript
import map from './map';
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

## Changelog
__3.9.0__
Possible to elevate associated column on a related table to a parent table when fetching. See https://github.com/alfateam/rdb/#user-content-aggregate-results
__3.8.0__
Aggregate operators: count, max, min, avg and sum.
__3.7.0__
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rdb",
"version": "3.8.0",
"version": "3.9.0",
"main": "./src/index.js",
"browser": "./src/client/index.mjs",
"bin": {
Expand Down
58 changes: 56 additions & 2 deletions src/client/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5591,6 +5591,9 @@ function rdbClient(options = {}) {
for (let name in _strategy) {
if (name === 'where' && typeof strategy[name] === 'function')
strategy.where = column(path + 'where')(strategy.where); // Assuming `column` is defined elsewhere.
else if (typeof strategy[name] === 'function') {
strategy[name] = aggregate(path, strategy[name]);
}
else
strategy[name] = where(_strategy[name], path + name + '.');
}
Expand Down Expand Up @@ -6099,6 +6102,55 @@ function tableProxy() {
return new Proxy({}, handler);
}

function aggregate(path, arg) {

const c = {
sum,
count,
avg,
max,
min
};

let handler = {
get(_target, property,) {
if (property in c)
return Reflect.get(...arguments);
else {
subColumn = column(path + 'aggregate');
return column(property);
}
}

};
let subColumn;
const proxy = new Proxy(c, handler);

const result = arg(proxy);

if (subColumn)
return subColumn(result.self());
else
return result;


function sum(fn) {
return column(path + 'aggregate')(fn(column('')).sum());
}
function avg(fn) {
return column(path + 'aggregate')(fn(column('')).avg());
}
function max(fn) {
return column(path + 'aggregate')(fn(column('')).max());
}
function min(fn) {
return column(path + 'aggregate')(fn(column('')).min());
}
function count(fn) {
return column(path + 'aggregate')(fn(column('')).count());
}
}

function column(path, ...previous) {
function c() {
let args = [];
Expand Down Expand Up @@ -6131,8 +6183,10 @@ function column(path, ...previous) {
return Reflect.get(...arguments);
else if (property === 'then')
return;
else
return column(path + '.' + property);
else {
const nextPath = path ? path + '.' : '';
return column(nextPath + property);
}
}

};
Expand Down

0 comments on commit 03dfd3b

Please sign in to comment.