Skip to content

Commit

Permalink
updated documentation with where instead of var filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars-Erik Roald committed May 13, 2024
1 parent 9a7145b commit cf64c28
Show file tree
Hide file tree
Showing 39 changed files with 4,026 additions and 3,370 deletions.
266 changes: 163 additions & 103 deletions README.md

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"test": "vitest run --threads=false",
"coverage": "vitest run --coverage.enabled --coverage.reporter='text-summary' --threads=false",
"testw": "vitest --threads=false update",
"tscheck": "tsc ./src/index.d.ts --module commonjs --target es6 --noEmit true --strict true --esModuleInterop true",
"tscheck": "tsc ./src/index.d.ts --module commonjs --target es2022 --noEmit true --strict true --esModuleInterop true",
"concat": "node ./src/client/merge.js",
"build": "rollup -c ./src/client/rollup.config.js && npm run concat",
"lint": "eslint ./",
Expand All @@ -53,7 +53,7 @@
"@tediousjs/connection-string": "^0.4.1",
"@types/express": "^4.17.13",
"@types/oracledb": "^6.0.4",
"@types/tedious": "^4.0.9",
"@types/tedious": "^4.0.14",
"ajv": "^6.10.2",
"axios": "^1.6.2",
"deferred": "^0.7.5",
Expand Down Expand Up @@ -117,8 +117,8 @@
"pg-query-stream": "^3.3.2",
"rollup": "^2.52.7",
"sqlite3": "^5.0.2",
"tedious": "^15.1.2",
"typescript": "^4.8.4",
"tedious": "^18.1.0",
"typescript": "^5.4.5",
"vitest": "^0.34.1"
},
"engines": {
Expand Down
140 changes: 118 additions & 22 deletions src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ function rdbClient(options = {}) {
let c = {
count,
getMany,
aggregate: groupBy,
getAll,
getOne,
getById,
Expand Down Expand Up @@ -229,6 +230,16 @@ function rdbClient(options = {}) {
return proxify(rows, strategy);
}

async function groupBy(strategy) {
let args = negotiateGroupBy(null, strategy);
let body = stringify({
path: 'aggregate',
args
});
let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
return adapter.post(body);
}

async function count(_) {
let args = [_].concat(Array.prototype.slice.call(arguments).slice(1));
let body = stringify({
Expand Down Expand Up @@ -281,30 +292,66 @@ function rdbClient(options = {}) {
return [_, where(strategy), ...rest];
else
return args;
}

function where(_strategy, path = '') {
if (typeof _strategy !== 'object' || _strategy === null)
return _strategy;
function where(_strategy, path = '') {
if (typeof _strategy !== 'object' || _strategy === null)
return _strategy;

if (Array.isArray(_strategy)) {
return _strategy.map(item => where(item, path));
}

if (Array.isArray(_strategy)) {
return _strategy.map(item => where(item, path));
const strategy = { ..._strategy };
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 + '.');
}
return strategy;
}

const strategy = { ..._strategy };
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]);
}



function negotiateGroupBy(_, strategy, ...rest) {
const args = Array.prototype.slice.call(arguments);
if (strategy)
return [_, where(strategy), ...rest];
else
return args;

function where(_strategy, path = '') {
if (typeof _strategy !== 'object' || _strategy === null)
return _strategy;

if (Array.isArray(_strategy)) {
return _strategy.map(item => where(item, path));
}
else
strategy[name] = where(_strategy[name], path + name + '.');

const strategy = { ..._strategy };
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] = groupByAggregate(path, strategy[name]);
}
else
strategy[name] = where(_strategy[name], path + name + '.');
}
return strategy;
}
return strategy;

}





async function _delete() {
let args = Array.prototype.slice.call(arguments);
let body = stringify({
Expand Down Expand Up @@ -821,7 +868,56 @@ function aggregate(path, arg) {
if (property in c)
return Reflect.get(...arguments);
else {
subColumn = column(path + 'aggregate');
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('')).groupSum());
}
function avg(fn) {
return column(path + '_aggregate')(fn(column('')).groupAvg());
}
function max(fn) {
return column(path + '_aggregate')(fn(column('')).groupMax());
}
function min(fn) {
return column(path + '_aggregate')(fn(column('')).groupMin());
}
function count(fn) {
return column(path + '_aggregate')(fn(column('')).groupCount());
}
}

function groupByAggregate(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);
}
}
Expand All @@ -830,7 +926,7 @@ function aggregate(path, arg) {
let subColumn;
const proxy = new Proxy(c, handler);

const result = arg(proxy);
const result = arg(proxy);

if (subColumn)
return subColumn(result.self());
Expand All @@ -839,19 +935,19 @@ function aggregate(path, arg) {


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

Expand Down
Loading

0 comments on commit cf64c28

Please sign in to comment.