Skip to content

Commit

Permalink
4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lroal committed May 28, 2024
1 parent bb7e8de commit c83b3fd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 35 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## Changelog
__4.0.0__
Changed the behaviour of `update` to accept a `where` filter and only update passed in columns and relations. The previous behaviour of `update` has moved to `replace` method.
__3.10.3__
Fix duplicate method signatures for those still using code generation
__3.10.2__
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": "orange-orm",
"version": "3.10.3",
"version": "4.0.0",
"main": "./src/index.js",
"browser": "./src/client/index.mjs",
"bin": {
Expand Down
76 changes: 42 additions & 34 deletions src/client/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5486,6 +5486,7 @@ function rdbClient(options = {}) {
getById,
proxify,
update,
replace,
updateChanges,
insert,
insertAndForget,
Expand Down Expand Up @@ -5585,31 +5586,32 @@ function rdbClient(options = {}) {
function negotiateWhere(_, strategy, ...rest) {
const args = Array.prototype.slice.call(arguments);
if (strategy)
return [_, where(strategy), ...rest];
return [_, negotiateWhereSingle(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));
}
}

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;
function negotiateWhereSingle(_strategy, path = '') {
if (typeof _strategy !== 'object' || _strategy === null)
return _strategy;

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


Expand Down Expand Up @@ -5668,28 +5670,34 @@ function rdbClient(options = {}) {
return adapter.post(body);
}

async function update(rows, ...rest) {
const concurrency = undefined;
const args = [concurrency].concat(rest);
if (Array.isArray(rows)) {
const proxy = await getMany.apply(null, [rows, ...rest]);
proxy.splice.apply(proxy, [0, proxy.length, ...rows]);
await proxy.saveChanges.apply(proxy, args);
return proxy;
}
else {
const proxy = await getMany.apply(null, [[rows], ...rest]);
proxy.splice.apply(proxy, [0, 1, rows]);
await proxy.saveChanges.apply(proxy, args);
return proxify(proxy[0], args[0]);
}
async function update(_row, _where, strategy) {
let args = [_row, negotiateWhereSingle(_where), negotiateWhereSingle(strategy)];
let body = stringify({
path: 'update',
args
});
let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
const result = await adapter.post(body);
if (strategy)
return proxify(result, strategy);
}

async function replace(_row, strategy) {
let args = [_row, negotiateWhereSingle(strategy)];
let body = stringify({
path: 'replace',
args
});
let adapter = netAdapter(url, tableName, { axios: axiosInterceptor, tableOptions });
const result = await adapter.post(body);
if (strategy)
return proxify(result, strategy);
}

async function updateChanges(rows, oldRows, ...rest) {
const concurrency = undefined;
const args = [concurrency].concat(rest);
if (Array.isArray(rows)) {
//todo
const proxy = await getMany.apply(null, [rows, ...rest]);
proxy.splice.apply(proxy, [0, proxy.length, ...rows]);
await proxy.saveChanges.apply(proxy, args);
Expand Down

0 comments on commit c83b3fd

Please sign in to comment.