Skip to content

Commit 3429e7c

Browse files
committed
Get ruleParams to the backend
1 parent cbe43dc commit 3429e7c

File tree

5 files changed

+37
-35
lines changed

5 files changed

+37
-35
lines changed

client/packages/admin/src/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ function init<
224224
*/
225225
const init_experimental = init;
226226

227+
function steps(inputChunks) {
228+
const chunks = Array.isArray(inputChunks) ? inputChunks : [inputChunks];
229+
return chunks.flatMap(getOps);
230+
}
231+
227232
/**
228233
*
229234
* The first step: init your application!
@@ -340,7 +345,7 @@ class InstantAdmin<
340345
return jsonFetch(`${this.config.apiURI}/admin/transact`, {
341346
method: 'POST',
342347
headers: authorizedHeaders(this.config, this.impersonationOpts),
343-
body: JSON.stringify({ steps: steps }),
348+
body: JSON.stringify({ steps: steps(inputChunks) }),
344349
});
345350
};
346351

@@ -419,7 +424,7 @@ class InstantAdmin<
419424
method: 'POST',
420425
headers: authorizedHeaders(this.config, this.impersonationOpts),
421426
body: JSON.stringify({
422-
steps: steps,
427+
steps: steps(inputChunks),
423428
'rules-override': opts?.rules,
424429
// @ts-expect-error because we're using a private API (for now)
425430
'dangerously-commit-tx': opts?.__dangerouslyCommit,
@@ -866,7 +871,7 @@ class InstantAdminDatabase<Schema extends InstantSchemaDef<any, any, any>> {
866871
method: 'POST',
867872
headers: authorizedHeaders(this.config, this.impersonationOpts),
868873
body: JSON.stringify({
869-
steps: steps,
874+
steps: steps(inputChunks),
870875
'throw-on-missing-attrs?': !!this.config.schema,
871876
}),
872877
});
@@ -947,7 +952,7 @@ class InstantAdminDatabase<Schema extends InstantSchemaDef<any, any, any>> {
947952
method: 'POST',
948953
headers: authorizedHeaders(this.config, this.impersonationOpts),
949954
body: JSON.stringify({
950-
steps: steps,
955+
steps: steps(inputChunks),
951956
'rules-override': opts?.rules,
952957
// @ts-expect-error because we're using a private API (for now)
953958
'dangerously-commit-tx': opts?.__dangerouslyCommit,

client/packages/core/src/instaml.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function expandUnlink(attrs, [etype, eidA, obj]) {
175175
function expandUpdate(attrs, [etype, eid, obj]) {
176176
const lookup = extractLookup(attrs, etype, eid);
177177
// id first so that we don't clobber updates on the lookup field
178-
const attrTuples = [['id', extractLookup(attrs, etype, eid)]]
178+
const attrTuples = [['id', lookup]]
179179
.concat(Object.entries(obj))
180180
.map(([identName, value]) => {
181181
const attr = getAttrByFwdIdentName(attrs, etype, identName);
@@ -207,6 +207,12 @@ function expandDeepMerge(attrs, [etype, eid, obj]) {
207207
// id first so that we don't clobber updates on the lookup field
208208
return [idTuple].concat(attrTuples);
209209
}
210+
211+
function expandRuleParams(attrs, [etype, eid, ruleParams]) {
212+
const lookup = extractLookup(attrs, etype, eid);
213+
return [['rule-params', lookup, etype, ruleParams]];
214+
}
215+
210216
function removeIdFromArgs(step) {
211217
const [op, etype, eid, obj] = step;
212218
if (!obj) {
@@ -230,6 +236,8 @@ function toTxSteps(attrs, step) {
230236
return expandUnlink(attrs, args);
231237
case 'delete':
232238
return expandDelete(attrs, args);
239+
case 'ruleParams':
240+
return expandRuleParams(attrs, args);
233241
default:
234242
throw new Error(`unsupported action ${action}`);
235243
}
@@ -341,6 +349,7 @@ const SUPPORTS_LOOKUP_ACTIONS = new Set([
341349
'update',
342350
'merge',
343351
'delete',
352+
'ruleParams'
344353
]);
345354

346355
const lookupProps = { 'unique?': true, 'index?': true };
@@ -481,21 +490,8 @@ function createMissingAttrs({ attrs: existingAttrs, schema }, ops) {
481490

482491
export function transform(ctx, inputChunks) {
483492
const chunks = Array.isArray(inputChunks) ? inputChunks : [inputChunks];
484-
485493
const ops = chunks.flatMap((tx) => getOps(tx));
486494
const [newAttrs, addAttrTxSteps] = createMissingAttrs(ctx, ops);
487-
488-
const txSteps = [];
489-
for (const chunk of chunks) {
490-
const ops = chunk.__ops;
491-
const ruleParams = chunk.__ruleParams;
492-
for (const op of ops) {
493-
for (const txStep of toTxSteps(newAttrs, op)) {
494-
const [op, a, b, c] = txStep;
495-
txSteps.push(ruleParams ? [op, a, b, c, ruleParams] : txStep);
496-
}
497-
}
498-
}
499-
495+
const txSteps = ops.flatMap((op) => toTxSteps(newAttrs, op));
500496
return [...addAttrTxSteps, ...txSteps];
501497
}

client/packages/core/src/instatx.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export interface TransactionChunk<
1818
EntityName extends keyof Schema['entities'],
1919
> {
2020
__ops: Op[];
21-
__ruleParams: RuleParams;
2221
/**
2322
* Create and update objects:
2423
*
@@ -116,18 +115,13 @@ export type TxChunk<Schema extends IContainEntitiesAndLinks<any, any>> = {
116115
function transactionChunk(
117116
etype: EType,
118117
id: Id | LookupRef,
119-
ruleParams: RuleParams,
120118
prevOps: Op[],
121119
): TransactionChunk<any, any> {
122120
return new Proxy({} as TransactionChunk<any, any>, {
123121
get: (_target, cmd: keyof TransactionChunk<any, any>) => {
124122
if (cmd === '__ops') return prevOps;
125-
if (cmd === '__ruleParams') return ruleParams;
126-
if (cmd === 'ruleParams') {
127-
return (args: Args) => transactionChunk(etype, id, {...ruleParams, ...args}, prevOps);
128-
}
129123
return (args: Args) => {
130-
return transactionChunk(etype, id, ruleParams, [...prevOps, [cmd, etype, id, args]]);
124+
return transactionChunk(etype, id, [...prevOps, [cmd, etype, id, args]]);
131125
};
132126
},
133127
});
@@ -158,9 +152,9 @@ function etypeChunk(etype: EType): ETypeChunk<any, EType> {
158152
{
159153
get(_target, id: Id) {
160154
if (isLookup(id)) {
161-
return transactionChunk(etype, parseLookup(id), undefined, []);
155+
return transactionChunk(etype, parseLookup(id), []);
162156
}
163-
return transactionChunk(etype, id, undefined, []);
157+
return transactionChunk(etype, id, []);
164158
},
165159
},
166160
);
@@ -193,7 +187,3 @@ export const tx = txInit();
193187
export function getOps(x: TransactionChunk<any, any>): Op[] {
194188
return x.__ops;
195189
}
196-
197-
export function getRuleParams(x: TransactionChunk<any, any>): RuleParams {
198-
return x.__ruleParams;
199-
}

server/src/instant/admin/model.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@
168168
(let [lookup (extract-lookup attrs etype eid)]
169169
[[:delete-entity lookup etype]]))
170170

171+
(defn expand-rule-params [attrs [etype eid params]]
172+
(let [lookup (extract-lookup attrs etype eid)]
173+
[[:rule-params lookup etype params]]))
174+
171175
(defn expand-add-attr [_ [attr]]
172176
[[:add-attr (-> attr
173177
w/keywordize-keys
@@ -188,6 +192,7 @@
188192
"link" (expand-link attrs args)
189193
"unlink" (expand-unlink attrs args)
190194
"delete" (expand-delete attrs args)
195+
"ruleParams" (expand-rule-params attrs args)
191196
"add-attr" (expand-add-attr attrs args)
192197
"delete-attr" (expand-delete-attr attrs args)
193198
(throw (ex-info (str "unsupported action " action) {})))))
@@ -376,6 +381,9 @@
376381
(s/def ::delete-op
377382
(s/cat :op #{"delete"} :args (s/cat :etype string? :eid ::lookup :remaining-args (s/* (constantly true)))))
378383

384+
(s/def ::rule-params-op
385+
(s/cat :op #{"ruleParams"} :args (s/cat :etype string? :eid ::lookup :args map?)))
386+
379387
(s/def ::add-attr-op
380388
(s/cat :op #{"add-attr"} :attr map?))
381389

@@ -394,6 +402,7 @@
394402
:link ::link-op
395403
:unlink ::unlink-op
396404
:delete ::delete-op
405+
:rule-params ::rule-params-op
397406
:add-attr ::add-attr-op
398407
:update-attr ::update-attr-op
399408
:delete-attr ::delete-attr-op))

server/src/instant/db/transaction.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@
3131
(s/cat :op #{:add-attr} :attr ::attr-model/attr))
3232

3333
(s/def ::delete-entity-step
34-
(s/cat :op #{:delete-entity}
35-
:lookup ::triple-model/lookup
36-
:etype (s/? string?)))
34+
(s/cat :op #{:delete-entity} :lookup ::triple-model/lookup :etype (s/? string?)))
35+
36+
(s/def ::rule-params-step
37+
(s/cat :op #{:rule-params} :lookup ::triple-model/lookup :etype (s/? string?) :params (s/map-of string? any?)))
3738

3839
(s/def ::delete-attr-step
3940
(s/cat :op #{:delete-attr} :attr-id ::attr-model/id))
@@ -44,6 +45,7 @@
4445
(s/def ::tx-step (s/or :add-triple ::add-triple-step
4546
:deep-merge-triple ::deep-merge-triple-step
4647
:delete-entity ::delete-entity-step
48+
:rule-params ::rule-params-step
4749
:retract-triple ::retract-triple-step
4850
:add-attr ::add-attr-step
4951
:update-attr ::update-attr-step

0 commit comments

Comments
 (0)