@statezero/core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adaptors/react/composables.d.ts +1 -0
- package/dist/adaptors/react/composables.js +4 -0
- package/dist/adaptors/react/index.d.ts +1 -0
- package/dist/adaptors/react/index.js +1 -0
- package/dist/adaptors/vue/composables.d.ts +2 -0
- package/dist/adaptors/vue/composables.js +36 -0
- package/dist/adaptors/vue/index.d.ts +2 -0
- package/dist/adaptors/vue/index.js +2 -0
- package/dist/adaptors/vue/reactivity.d.ts +18 -0
- package/dist/adaptors/vue/reactivity.js +125 -0
- package/dist/cli/commands/syncModels.d.ts +132 -0
- package/dist/cli/commands/syncModels.js +1040 -0
- package/dist/cli/configFileLoader.d.ts +10 -0
- package/dist/cli/configFileLoader.js +85 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +14 -0
- package/dist/config.d.ts +52 -0
- package/dist/config.js +242 -0
- package/dist/core/eventReceivers.d.ts +179 -0
- package/dist/core/eventReceivers.js +210 -0
- package/dist/core/utils.d.ts +8 -0
- package/dist/core/utils.js +62 -0
- package/dist/filtering/localFiltering.d.ts +116 -0
- package/dist/filtering/localFiltering.js +834 -0
- package/dist/flavours/django/dates.d.ts +33 -0
- package/dist/flavours/django/dates.js +99 -0
- package/dist/flavours/django/errors.d.ts +138 -0
- package/dist/flavours/django/errors.js +187 -0
- package/dist/flavours/django/f.d.ts +6 -0
- package/dist/flavours/django/f.js +91 -0
- package/dist/flavours/django/files.d.ts +76 -0
- package/dist/flavours/django/files.js +338 -0
- package/dist/flavours/django/makeApiCall.d.ts +20 -0
- package/dist/flavours/django/makeApiCall.js +169 -0
- package/dist/flavours/django/manager.d.ts +197 -0
- package/dist/flavours/django/manager.js +222 -0
- package/dist/flavours/django/model.d.ts +112 -0
- package/dist/flavours/django/model.js +253 -0
- package/dist/flavours/django/operationFactory.d.ts +65 -0
- package/dist/flavours/django/operationFactory.js +216 -0
- package/dist/flavours/django/q.d.ts +70 -0
- package/dist/flavours/django/q.js +43 -0
- package/dist/flavours/django/queryExecutor.d.ts +131 -0
- package/dist/flavours/django/queryExecutor.js +468 -0
- package/dist/flavours/django/querySet.d.ts +412 -0
- package/dist/flavours/django/querySet.js +601 -0
- package/dist/flavours/django/tempPk.d.ts +19 -0
- package/dist/flavours/django/tempPk.js +48 -0
- package/dist/flavours/django/utils.d.ts +19 -0
- package/dist/flavours/django/utils.js +29 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +38 -0
- package/dist/react-entry.d.ts +2 -0
- package/dist/react-entry.js +2 -0
- package/dist/reactiveAdaptor.d.ts +24 -0
- package/dist/reactiveAdaptor.js +38 -0
- package/dist/setup.d.ts +15 -0
- package/dist/setup.js +22 -0
- package/dist/syncEngine/cache/cache.d.ts +75 -0
- package/dist/syncEngine/cache/cache.js +341 -0
- package/dist/syncEngine/metrics/metricOptCalcs.d.ts +79 -0
- package/dist/syncEngine/metrics/metricOptCalcs.js +284 -0
- package/dist/syncEngine/registries/metricRegistry.d.ts +53 -0
- package/dist/syncEngine/registries/metricRegistry.js +162 -0
- package/dist/syncEngine/registries/modelStoreRegistry.d.ts +11 -0
- package/dist/syncEngine/registries/modelStoreRegistry.js +56 -0
- package/dist/syncEngine/registries/querysetStoreRegistry.d.ts +55 -0
- package/dist/syncEngine/registries/querysetStoreRegistry.js +244 -0
- package/dist/syncEngine/stores/metricStore.d.ts +55 -0
- package/dist/syncEngine/stores/metricStore.js +222 -0
- package/dist/syncEngine/stores/modelStore.d.ts +40 -0
- package/dist/syncEngine/stores/modelStore.js +405 -0
- package/dist/syncEngine/stores/operation.d.ts +99 -0
- package/dist/syncEngine/stores/operation.js +224 -0
- package/dist/syncEngine/stores/operationEventHandlers.d.ts +8 -0
- package/dist/syncEngine/stores/operationEventHandlers.js +239 -0
- package/dist/syncEngine/stores/querysetStore.d.ts +32 -0
- package/dist/syncEngine/stores/querysetStore.js +200 -0
- package/dist/syncEngine/stores/reactivity.d.ts +3 -0
- package/dist/syncEngine/stores/reactivity.js +4 -0
- package/dist/syncEngine/stores/utils.d.ts +14 -0
- package/dist/syncEngine/stores/utils.js +32 -0
- package/dist/syncEngine/sync.d.ts +32 -0
- package/dist/syncEngine/sync.js +169 -0
- package/dist/vue-entry.d.ts +6 -0
- package/dist/vue-entry.js +2 -0
- package/license.md +116 -0
- package/package.json +123 -0
- package/readme.md +222 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A custom data structure that behaves as an augmented array.
|
|
3
|
+
* It stores [instance, created] and also provides named properties for clarity.
|
|
4
|
+
*
|
|
5
|
+
* @class ResultTuple
|
|
6
|
+
* @extends {Array}
|
|
7
|
+
*/
|
|
8
|
+
export class ResultTuple extends Array<any> {
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new ResultTuple.
|
|
11
|
+
*
|
|
12
|
+
* @param {*} instance - The model instance.
|
|
13
|
+
* @param {boolean} created - Whether the instance was created.
|
|
14
|
+
*/
|
|
15
|
+
constructor(instance: any, created: boolean);
|
|
16
|
+
0: any;
|
|
17
|
+
1: boolean;
|
|
18
|
+
instance: any;
|
|
19
|
+
created: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Handles query execution against the backend, and parsing the response into the correct format.
|
|
23
|
+
*/
|
|
24
|
+
export class QueryExecutor {
|
|
25
|
+
/**
|
|
26
|
+
* Executes a get operation (get, first, last) with the QuerySet.
|
|
27
|
+
*
|
|
28
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
29
|
+
* @param {string} operationType - The specific get operation type.
|
|
30
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
31
|
+
* @returns {Promise<Object>} The model instance.
|
|
32
|
+
*/
|
|
33
|
+
static executeGet(querySet: QuerySet, operationType: string, args?: Object): Promise<Object>;
|
|
34
|
+
/**
|
|
35
|
+
* Execute a list-style API call for the given QuerySet, update the in‑memory store with the returned primary keys,
|
|
36
|
+
* process any included entities, and return a live‑thenable that keeps the local "live" collection in sync.
|
|
37
|
+
*
|
|
38
|
+
* @template T The model type of the QuerySet
|
|
39
|
+
* @param {QuerySet<T>} qs
|
|
40
|
+
* The QuerySet to execute.
|
|
41
|
+
* @param {string} [op="list"]
|
|
42
|
+
* The operation to perform. Defaults to `"list"`, but could be overridden for other list‑style endpoints.
|
|
43
|
+
* @param {Object} [args={}]
|
|
44
|
+
* Additional arguments to pass through to the underlying API call (e.g. filters, pagination).
|
|
45
|
+
* @returns {LiveThenable<import('./makeLiveThenable').Result<T[]>>}
|
|
46
|
+
* A live‑thenable wrapping an array of primary‑key values for the fetched models. The live part remains
|
|
47
|
+
* synchronized with the in‑memory store.
|
|
48
|
+
*/
|
|
49
|
+
static executeList<T>(qs: QuerySet<T>, op?: string, args?: Object): LiveThenable<any>;
|
|
50
|
+
/**
|
|
51
|
+
* Executes a get_or_create or update_or_create operation with the QuerySet.
|
|
52
|
+
*
|
|
53
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
54
|
+
* @param {string} operationType - The specific operation type ('get_or_create' or 'update_or_create').
|
|
55
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
56
|
+
* @returns {Promise<ResultTuple>} Tuple with instance and created flag.
|
|
57
|
+
*/
|
|
58
|
+
static executeOrCreate(querySet: QuerySet, operationType: string, args?: Object): Promise<ResultTuple>;
|
|
59
|
+
/**
|
|
60
|
+
* Executes an aggregation operation with the QuerySet.
|
|
61
|
+
*
|
|
62
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
63
|
+
* @param {string} operationType - The specific aggregation operation type.
|
|
64
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
65
|
+
* @returns {LiveMetric} The LiveMetric instance that updates optimistically.
|
|
66
|
+
*/
|
|
67
|
+
static executeAgg(querySet: QuerySet, operationType: string, args?: Object): LiveMetric;
|
|
68
|
+
/**
|
|
69
|
+
* Executes an exists operation with the QuerySet.
|
|
70
|
+
*
|
|
71
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
72
|
+
* @param {string} operationType - The operation type (always 'exists' for this method).
|
|
73
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
74
|
+
* @returns {Promise<boolean>} Whether records exist.
|
|
75
|
+
*/
|
|
76
|
+
static executeExists(querySet: QuerySet, operationType?: string, args?: Object): Promise<boolean>;
|
|
77
|
+
/**
|
|
78
|
+
* Executes an update operation with the QuerySet.
|
|
79
|
+
*
|
|
80
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
81
|
+
* @param {string} operationType - The operation type (always 'update' for this method).
|
|
82
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
83
|
+
* @returns {Promise<Array>} Tuple with count and model counts map.
|
|
84
|
+
*/
|
|
85
|
+
static executeUpdate(querySet: QuerySet, operationType?: string, args?: Object): Promise<any[]>;
|
|
86
|
+
/**
|
|
87
|
+
* Executes a delete operation with the QuerySet.
|
|
88
|
+
*
|
|
89
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
90
|
+
* @param {string} operationType - The operation type (always 'delete' for this method).
|
|
91
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
92
|
+
* @returns {Promise<Array>} Tuple with count and model counts map.
|
|
93
|
+
*/
|
|
94
|
+
static executeDelete(querySet: QuerySet, operationType?: string, args?: Object): Promise<any[]>;
|
|
95
|
+
/**
|
|
96
|
+
* Executes a create operation with the QuerySet.
|
|
97
|
+
*
|
|
98
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
99
|
+
* @param {string} operationType - The operation type (always 'create' for this method).
|
|
100
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
101
|
+
* @returns {LiveThenable<Object>} The live Model instance which resolves to the created model.
|
|
102
|
+
*/
|
|
103
|
+
static executeCreate(querySet: QuerySet, operationType?: string, args?: Object): LiveThenable<Object>;
|
|
104
|
+
/**
|
|
105
|
+
* Executes an update_instance operation with the QuerySet.
|
|
106
|
+
*
|
|
107
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
108
|
+
* @param {string} operationType - The operation type (always 'update_instance' for this method).
|
|
109
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
110
|
+
* @returns {LiveThenable<Object>} The live Model instance which resolves to the updated model.
|
|
111
|
+
*/
|
|
112
|
+
static executeUpdateInstance(querySet: QuerySet, operationType?: string, args?: Object): LiveThenable<Object>;
|
|
113
|
+
/**
|
|
114
|
+
* Executes a delete_instance operation with the QuerySet.
|
|
115
|
+
*
|
|
116
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
117
|
+
* @param {string} operationType - The operation type (always 'delete_instance' for this method).
|
|
118
|
+
* @param {Object} args - Additional arguments for the operation, including the primary key.
|
|
119
|
+
* @returns {LiveThenable<Array>} A live‑thenable resolving to [deletedCount, { modelName: deletedCount }].
|
|
120
|
+
*/
|
|
121
|
+
static executeDeleteInstance(querySet: QuerySet, operationType?: string, args?: Object): LiveThenable<any[]>;
|
|
122
|
+
/**
|
|
123
|
+
* Executes a query operation with the QuerySet.
|
|
124
|
+
*
|
|
125
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
126
|
+
* @param {string} operationType - The operation type to perform.
|
|
127
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
128
|
+
* @returns {Promise<any>} The operation result.
|
|
129
|
+
*/
|
|
130
|
+
static execute(querySet: QuerySet, operationType?: string, args?: Object): Promise<any>;
|
|
131
|
+
}
|
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { configInstance } from '../../config.js';
|
|
3
|
+
import { parseStateZeroError, MultipleObjectsReturned, DoesNotExist } from './errors.js';
|
|
4
|
+
import { modelStoreRegistry } from '../../syncEngine/registries/modelStoreRegistry.js';
|
|
5
|
+
import { querysetStoreRegistry } from '../../syncEngine/registries/querysetStoreRegistry.js';
|
|
6
|
+
import { metricRegistry } from '../../syncEngine/registries/metricRegistry.js';
|
|
7
|
+
import { Status } from '../../syncEngine/stores/operation.js';
|
|
8
|
+
import { breakThenable, makeLiveThenable } from './utils.js';
|
|
9
|
+
import { setRealPk } from './tempPk.js';
|
|
10
|
+
import { isNil } from 'lodash-es';
|
|
11
|
+
import { Model } from './model.js';
|
|
12
|
+
import { v7 as uuid7 } from 'uuid';
|
|
13
|
+
import { makeApiCall, processIncludedEntities } from './makeApiCall.js';
|
|
14
|
+
import { OperationFactory } from './operationFactory.js';
|
|
15
|
+
const getModelClass = configInstance.getModelClass;
|
|
16
|
+
/**
|
|
17
|
+
* A custom data structure that behaves as an augmented array.
|
|
18
|
+
* It stores [instance, created] and also provides named properties for clarity.
|
|
19
|
+
*
|
|
20
|
+
* @class ResultTuple
|
|
21
|
+
* @extends {Array}
|
|
22
|
+
*/
|
|
23
|
+
export class ResultTuple extends Array {
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new ResultTuple.
|
|
26
|
+
*
|
|
27
|
+
* @param {*} instance - The model instance.
|
|
28
|
+
* @param {boolean} created - Whether the instance was created.
|
|
29
|
+
*/
|
|
30
|
+
constructor(instance, created) {
|
|
31
|
+
// Create an array with length 2.
|
|
32
|
+
super(2);
|
|
33
|
+
// Set array indices directly instead of using push.
|
|
34
|
+
this[0] = instance;
|
|
35
|
+
this[1] = created;
|
|
36
|
+
// Set named properties.
|
|
37
|
+
this.instance = instance;
|
|
38
|
+
this.created = created;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Handles query execution against the backend, and parsing the response into the correct format.
|
|
43
|
+
*/
|
|
44
|
+
export class QueryExecutor {
|
|
45
|
+
/**
|
|
46
|
+
* Executes a get operation (get, first, last) with the QuerySet.
|
|
47
|
+
*
|
|
48
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
49
|
+
* @param {string} operationType - The specific get operation type.
|
|
50
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
51
|
+
* @returns {Promise<Object>} The model instance.
|
|
52
|
+
*/
|
|
53
|
+
static executeGet(querySet, operationType, args = {}) {
|
|
54
|
+
const ModelClass = querySet.ModelClass;
|
|
55
|
+
const store = querysetStoreRegistry.getStore(querySet);
|
|
56
|
+
const existing = store.render();
|
|
57
|
+
const tempPk = Array.isArray(existing) && existing.length === 1 ? existing[0] : null;
|
|
58
|
+
// always return a Model instance (pk might be null)
|
|
59
|
+
const live = ModelClass.fromPk(tempPk, querySet);
|
|
60
|
+
const promise = makeApiCall(querySet, operationType, args).then((resp) => {
|
|
61
|
+
const { data, included, model_name } = resp.data;
|
|
62
|
+
if (isNil(data) || (Array.isArray(data) && data.length === 0)) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
processIncludedEntities(modelStoreRegistry, included, ModelClass);
|
|
66
|
+
const realPk = Array.isArray(data) ? data[0] : data;
|
|
67
|
+
// swap in the real PK on the same instance, will trigger reactivity
|
|
68
|
+
live.pk = realPk;
|
|
69
|
+
breakThenable(live);
|
|
70
|
+
return live;
|
|
71
|
+
});
|
|
72
|
+
return makeLiveThenable(live, promise);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Execute a list-style API call for the given QuerySet, update the in‑memory store with the returned primary keys,
|
|
76
|
+
* process any included entities, and return a live‑thenable that keeps the local "live" collection in sync.
|
|
77
|
+
*
|
|
78
|
+
* @template T The model type of the QuerySet
|
|
79
|
+
* @param {QuerySet<T>} qs
|
|
80
|
+
* The QuerySet to execute.
|
|
81
|
+
* @param {string} [op="list"]
|
|
82
|
+
* The operation to perform. Defaults to `"list"`, but could be overridden for other list‑style endpoints.
|
|
83
|
+
* @param {Object} [args={}]
|
|
84
|
+
* Additional arguments to pass through to the underlying API call (e.g. filters, pagination).
|
|
85
|
+
* @returns {LiveThenable<import('./makeLiveThenable').Result<T[]>>}
|
|
86
|
+
* A live‑thenable wrapping an array of primary‑key values for the fetched models. The live part remains
|
|
87
|
+
* synchronized with the in‑memory store.
|
|
88
|
+
*/
|
|
89
|
+
static executeList(qs, op = "list", args = {}) {
|
|
90
|
+
const live = querysetStoreRegistry.getEntity(qs);
|
|
91
|
+
const promise = makeApiCall(qs, op, args).then((resp) => {
|
|
92
|
+
const { data, included } = resp.data;
|
|
93
|
+
processIncludedEntities(modelStoreRegistry, included, qs.ModelClass);
|
|
94
|
+
const pks = Array.isArray(data) ? data : [];
|
|
95
|
+
querysetStoreRegistry.setEntity(qs, pks);
|
|
96
|
+
return querysetStoreRegistry.getEntity(qs);
|
|
97
|
+
});
|
|
98
|
+
return makeLiveThenable(live, promise);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Executes a get_or_create or update_or_create operation with the QuerySet.
|
|
102
|
+
*
|
|
103
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
104
|
+
* @param {string} operationType - The specific operation type ('get_or_create' or 'update_or_create').
|
|
105
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
106
|
+
* @returns {Promise<ResultTuple>} Tuple with instance and created flag.
|
|
107
|
+
*/
|
|
108
|
+
static executeOrCreate(querySet, operationType, args = {}) {
|
|
109
|
+
const ModelClass = querySet.ModelClass;
|
|
110
|
+
const primaryKeyField = ModelClass.primaryKeyField;
|
|
111
|
+
const apiCallArgs = {
|
|
112
|
+
lookup: args.lookup || {},
|
|
113
|
+
defaults: args.defaults || {},
|
|
114
|
+
};
|
|
115
|
+
// Use the factory to create the operation (which includes local filtering logic)
|
|
116
|
+
const operation = operationType === 'get_or_create'
|
|
117
|
+
? OperationFactory.createGetOrCreateOperation(querySet, apiCallArgs.lookup, apiCallArgs.defaults)
|
|
118
|
+
: OperationFactory.createUpdateOrCreateOperation(querySet, apiCallArgs.lookup, apiCallArgs.defaults);
|
|
119
|
+
// Determine if we're creating new based on the operation type
|
|
120
|
+
const isCreatingNew = operation.type === 'create';
|
|
121
|
+
// Create optimistic instance and result
|
|
122
|
+
const live = isCreatingNew
|
|
123
|
+
? ModelClass.fromPk(operation.instances[0][primaryKeyField], querySet)
|
|
124
|
+
: ModelClass.fromPk(operation.instances[0][primaryKeyField], querySet);
|
|
125
|
+
let liveResult = new ResultTuple(live, isCreatingNew);
|
|
126
|
+
// Make API call with original operation type for backend
|
|
127
|
+
const promise = makeApiCall(querySet, operationType, apiCallArgs, operation.operationId)
|
|
128
|
+
.then((response) => {
|
|
129
|
+
const { data, included, model_name } = response.data;
|
|
130
|
+
const created = response.metadata.created;
|
|
131
|
+
// Process included entities
|
|
132
|
+
processIncludedEntities(modelStoreRegistry, included, ModelClass);
|
|
133
|
+
// Get the real PK
|
|
134
|
+
const pk = Array.isArray(data) ? data[0] : data;
|
|
135
|
+
// Update PK if we created a new instance
|
|
136
|
+
if (isCreatingNew) {
|
|
137
|
+
live.pk = pk;
|
|
138
|
+
}
|
|
139
|
+
// Confirm operation
|
|
140
|
+
const entityMap = included[model_name] || {};
|
|
141
|
+
const entityData = entityMap[pk];
|
|
142
|
+
if (entityData) {
|
|
143
|
+
operation.mutate({
|
|
144
|
+
instances: [entityData],
|
|
145
|
+
status: Status.CONFIRMED,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
// Update result with actual created flag
|
|
149
|
+
liveResult = new ResultTuple(live, created);
|
|
150
|
+
breakThenable(liveResult);
|
|
151
|
+
return liveResult;
|
|
152
|
+
})
|
|
153
|
+
.catch((error) => {
|
|
154
|
+
operation.updateStatus(Status.REJECTED);
|
|
155
|
+
throw error;
|
|
156
|
+
});
|
|
157
|
+
return makeLiveThenable(liveResult, promise);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Executes an aggregation operation with the QuerySet.
|
|
161
|
+
*
|
|
162
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
163
|
+
* @param {string} operationType - The specific aggregation operation type.
|
|
164
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
165
|
+
* @returns {LiveMetric} The LiveMetric instance that updates optimistically.
|
|
166
|
+
*/
|
|
167
|
+
static executeAgg(querySet, operationType, args = {}) {
|
|
168
|
+
const ModelClass = querySet.ModelClass;
|
|
169
|
+
const field = operationType === 'count'
|
|
170
|
+
? (args.field || ModelClass.primaryKeyField)
|
|
171
|
+
: args.field;
|
|
172
|
+
// Only include defined properties
|
|
173
|
+
if (operationType !== 'exists' && operationType !== 'count' && field === undefined) {
|
|
174
|
+
throw new Error(`Field parameter is required for ${operationType} operation`);
|
|
175
|
+
}
|
|
176
|
+
// Get the live metric from the registry
|
|
177
|
+
const liveMetric = metricRegistry.getEntity(operationType, querySet, field);
|
|
178
|
+
// Create the API call args
|
|
179
|
+
const apiCallArgs = {};
|
|
180
|
+
if (operationType !== 'exists') {
|
|
181
|
+
apiCallArgs.field = field;
|
|
182
|
+
}
|
|
183
|
+
// Perform the async request
|
|
184
|
+
const promise = makeApiCall(querySet, operationType, apiCallArgs).then((response) => {
|
|
185
|
+
// The aggregation result should be directly in data
|
|
186
|
+
const value = response.data;
|
|
187
|
+
// Update the metric store with the ground truth value and current queryset
|
|
188
|
+
const dataSlice = querysetStoreRegistry.getEntity(querySet);
|
|
189
|
+
metricRegistry.setEntity(operationType, querySet, field, value, dataSlice);
|
|
190
|
+
// Return the value for the promise resolution
|
|
191
|
+
return value;
|
|
192
|
+
});
|
|
193
|
+
// For consistency with other methods, make the liveMetric thenable
|
|
194
|
+
return makeLiveThenable(liveMetric, promise);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Executes an exists operation with the QuerySet.
|
|
198
|
+
*
|
|
199
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
200
|
+
* @param {string} operationType - The operation type (always 'exists' for this method).
|
|
201
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
202
|
+
* @returns {Promise<boolean>} Whether records exist.
|
|
203
|
+
*/
|
|
204
|
+
static async executeExists(querySet, operationType = "exists", args = {}) {
|
|
205
|
+
// exists
|
|
206
|
+
const apiCallArgs = {};
|
|
207
|
+
const response = await makeApiCall(querySet, operationType, apiCallArgs);
|
|
208
|
+
return response.data || false;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Executes an update operation with the QuerySet.
|
|
212
|
+
*
|
|
213
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
214
|
+
* @param {string} operationType - The operation type (always 'update' for this method).
|
|
215
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
216
|
+
* @returns {Promise<Array>} Tuple with count and model counts map.
|
|
217
|
+
*/
|
|
218
|
+
static executeUpdate(querySet, operationType = "update", args = {}) {
|
|
219
|
+
const ModelClass = querySet.ModelClass;
|
|
220
|
+
const modelName = ModelClass.modelName;
|
|
221
|
+
const apiCallArgs = {
|
|
222
|
+
filter: args.filter,
|
|
223
|
+
data: args.data || {},
|
|
224
|
+
};
|
|
225
|
+
// Use factory to create operation (includes F expression evaluation)
|
|
226
|
+
const operation = OperationFactory.createUpdateOperation(querySet, apiCallArgs.data, apiCallArgs.filter);
|
|
227
|
+
const estimatedCount = operation.instances.length;
|
|
228
|
+
let liveResult = [estimatedCount, { [modelName]: estimatedCount }];
|
|
229
|
+
const promise = makeApiCall(querySet, operationType, apiCallArgs, operation.operationId)
|
|
230
|
+
.then((response) => {
|
|
231
|
+
const { data, included } = response.data || {};
|
|
232
|
+
const fullData = included[modelName] || {};
|
|
233
|
+
const updatedObjects = Array.isArray(data)
|
|
234
|
+
? data.map((pk) => (fullData[`${pk}`]))
|
|
235
|
+
: [];
|
|
236
|
+
operation.updateStatus(Status.CONFIRMED, updatedObjects);
|
|
237
|
+
const updatedCount = response.metadata?.updated_count ?? 0;
|
|
238
|
+
liveResult = [updatedCount, { [modelName]: updatedCount }];
|
|
239
|
+
breakThenable(liveResult);
|
|
240
|
+
return liveResult;
|
|
241
|
+
})
|
|
242
|
+
.catch((err) => {
|
|
243
|
+
operation.updateStatus(Status.REJECTED);
|
|
244
|
+
throw err;
|
|
245
|
+
});
|
|
246
|
+
return makeLiveThenable(liveResult, promise);
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Executes a delete operation with the QuerySet.
|
|
250
|
+
*
|
|
251
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
252
|
+
* @param {string} operationType - The operation type (always 'delete' for this method).
|
|
253
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
254
|
+
* @returns {Promise<Array>} Tuple with count and model counts map.
|
|
255
|
+
*/
|
|
256
|
+
static executeDelete(querySet, operationType = "delete", args = {}) {
|
|
257
|
+
const ModelClass = querySet.ModelClass;
|
|
258
|
+
const modelName = ModelClass.modelName;
|
|
259
|
+
// Use factory to create operation
|
|
260
|
+
const operation = OperationFactory.createDeleteOperation(querySet);
|
|
261
|
+
// live placeholder: assume we delete all existing pks
|
|
262
|
+
const estimatedCount = operation.instances.length;
|
|
263
|
+
let liveResult = [estimatedCount, { [modelName]: estimatedCount }];
|
|
264
|
+
const promise = makeApiCall(querySet, operationType, {}, operation.operationId)
|
|
265
|
+
.then((response) => {
|
|
266
|
+
const deletedCount = response.metadata.deleted_count;
|
|
267
|
+
const deletedInstances = response.metadata.rows_deleted;
|
|
268
|
+
operation.updateStatus(Status.CONFIRMED, deletedInstances);
|
|
269
|
+
liveResult = [deletedCount, { [modelName]: deletedCount }];
|
|
270
|
+
breakThenable(liveResult);
|
|
271
|
+
return liveResult;
|
|
272
|
+
})
|
|
273
|
+
.catch((err) => {
|
|
274
|
+
operation.updateStatus(Status.REJECTED);
|
|
275
|
+
throw err;
|
|
276
|
+
});
|
|
277
|
+
return makeLiveThenable(liveResult, promise);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Executes a create operation with the QuerySet.
|
|
281
|
+
*
|
|
282
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
283
|
+
* @param {string} operationType - The operation type (always 'create' for this method).
|
|
284
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
285
|
+
* @returns {LiveThenable<Object>} The live Model instance which resolves to the created model.
|
|
286
|
+
*/
|
|
287
|
+
static executeCreate(querySet, operationType = "create", args = {}) {
|
|
288
|
+
const ModelClass = querySet.ModelClass;
|
|
289
|
+
const operationId = `${uuid7()}`;
|
|
290
|
+
const apiCallArgs = {
|
|
291
|
+
data: args.data || {},
|
|
292
|
+
};
|
|
293
|
+
// set the data so the operationId matches
|
|
294
|
+
if (isNil(args.data)) {
|
|
295
|
+
console.warn(`executeCreate was called with null data`);
|
|
296
|
+
args.data = {};
|
|
297
|
+
}
|
|
298
|
+
// Use factory to create operation
|
|
299
|
+
const operation = OperationFactory.createCreateOperation(querySet, apiCallArgs.data, operationId);
|
|
300
|
+
const tempPk = operation.instances[0][ModelClass.primaryKeyField];
|
|
301
|
+
// 1) placeholder instance
|
|
302
|
+
const live = ModelClass.fromPk(tempPk, querySet);
|
|
303
|
+
// 2) kick off the async call
|
|
304
|
+
const promise = makeApiCall(querySet, operationType, apiCallArgs, operationId, async (response) => {
|
|
305
|
+
const { data } = response.data;
|
|
306
|
+
const pk = Array.isArray(data) ? data[0] : data;
|
|
307
|
+
setRealPk(operationId, pk);
|
|
308
|
+
})
|
|
309
|
+
.then((response) => {
|
|
310
|
+
const { data, included, model_name } = response.data;
|
|
311
|
+
// Process included entities
|
|
312
|
+
processIncludedEntities(modelStoreRegistry, included, ModelClass);
|
|
313
|
+
// Get the real PK
|
|
314
|
+
const pk = Array.isArray(data) ? data[0] : data;
|
|
315
|
+
live.pk = pk;
|
|
316
|
+
// Two‑line lookup
|
|
317
|
+
const entityMap = included[model_name] || {};
|
|
318
|
+
const entityData = entityMap[pk];
|
|
319
|
+
if (!entityData) {
|
|
320
|
+
throw new Error(`Entity data not found for ${model_name} with pk ${pk}`);
|
|
321
|
+
}
|
|
322
|
+
// Confirm operation with full entity data
|
|
323
|
+
operation.mutate({
|
|
324
|
+
instances: [entityData],
|
|
325
|
+
status: Status.CONFIRMED,
|
|
326
|
+
});
|
|
327
|
+
// Freeze the live instance
|
|
328
|
+
breakThenable(live);
|
|
329
|
+
return live;
|
|
330
|
+
})
|
|
331
|
+
.catch((error) => {
|
|
332
|
+
operation.updateStatus(Status.REJECTED);
|
|
333
|
+
throw error;
|
|
334
|
+
});
|
|
335
|
+
// 3) return the live‑thenable
|
|
336
|
+
return makeLiveThenable(live, promise);
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Executes an update_instance operation with the QuerySet.
|
|
340
|
+
*
|
|
341
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
342
|
+
* @param {string} operationType - The operation type (always 'update_instance' for this method).
|
|
343
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
344
|
+
* @returns {LiveThenable<Object>} The live Model instance which resolves to the updated model.
|
|
345
|
+
*/
|
|
346
|
+
static executeUpdateInstance(querySet, operationType = "update_instance", args = {}) {
|
|
347
|
+
const ModelClass = querySet.ModelClass;
|
|
348
|
+
const primaryKeyField = ModelClass.primaryKeyField;
|
|
349
|
+
const store = querysetStoreRegistry.getStore(querySet);
|
|
350
|
+
const querysetPks = store.render();
|
|
351
|
+
const data = args.data || {};
|
|
352
|
+
// Use factory to create operation
|
|
353
|
+
const operation = OperationFactory.createUpdateInstanceOperation(querySet, data);
|
|
354
|
+
// 1) placeholder instance
|
|
355
|
+
const initialPk = Array.isArray(querysetPks) ? querysetPks[0] : null;
|
|
356
|
+
const live = ModelClass.fromPk(initialPk, querySet);
|
|
357
|
+
// 2) async call
|
|
358
|
+
const promise = makeApiCall(querySet, operationType, { data }, operation.operationId)
|
|
359
|
+
.then((response) => {
|
|
360
|
+
const { data: raw, included, model_name } = response.data;
|
|
361
|
+
// Process included entities
|
|
362
|
+
processIncludedEntities(modelStoreRegistry, included, ModelClass);
|
|
363
|
+
// Swap in the real PK
|
|
364
|
+
const pk = Array.isArray(raw) ? raw[0] : raw;
|
|
365
|
+
live.pk = pk;
|
|
366
|
+
// Two‑line lookup
|
|
367
|
+
const entityMap = included[model_name] || {};
|
|
368
|
+
const entityData = entityMap[pk];
|
|
369
|
+
if (!entityData) {
|
|
370
|
+
throw new Error(`Entity data not found for ${model_name} with pk ${pk}`);
|
|
371
|
+
}
|
|
372
|
+
// Confirm operation with full entity data
|
|
373
|
+
operation.updateStatus(Status.CONFIRMED, [entityData]);
|
|
374
|
+
// Freeze the live instance
|
|
375
|
+
breakThenable(live);
|
|
376
|
+
return live;
|
|
377
|
+
})
|
|
378
|
+
.catch((error) => {
|
|
379
|
+
operation.updateStatus(Status.REJECTED);
|
|
380
|
+
throw error;
|
|
381
|
+
});
|
|
382
|
+
// 3) return the live‑thenable
|
|
383
|
+
return makeLiveThenable(live, promise);
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Executes a delete_instance operation with the QuerySet.
|
|
387
|
+
*
|
|
388
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
389
|
+
* @param {string} operationType - The operation type (always 'delete_instance' for this method).
|
|
390
|
+
* @param {Object} args - Additional arguments for the operation, including the primary key.
|
|
391
|
+
* @returns {LiveThenable<Array>} A live‑thenable resolving to [deletedCount, { modelName: deletedCount }].
|
|
392
|
+
*/
|
|
393
|
+
static executeDeleteInstance(querySet, operationType = "delete_instance", args = {}) {
|
|
394
|
+
const ModelClass = querySet.ModelClass;
|
|
395
|
+
const modelName = ModelClass.modelName;
|
|
396
|
+
const primaryKeyField = ModelClass.primaryKeyField;
|
|
397
|
+
// Validate that the primary key is provided
|
|
398
|
+
if (args[primaryKeyField] === undefined) {
|
|
399
|
+
throw new Error(`Primary key '${primaryKeyField}' must be provided in args for delete_instance operation`);
|
|
400
|
+
}
|
|
401
|
+
// Use factory to create operation
|
|
402
|
+
const operation = OperationFactory.createDeleteInstanceOperation(querySet, args);
|
|
403
|
+
// 1) placeholder result
|
|
404
|
+
let liveResult = [1, { [modelName]: 1 }];
|
|
405
|
+
// 2) async call
|
|
406
|
+
const promise = makeApiCall(querySet, operationType, args, operation.operationId)
|
|
407
|
+
.then((response) => {
|
|
408
|
+
// response.data is the count for delete_instance
|
|
409
|
+
let deletedCount = 1;
|
|
410
|
+
if (typeof response.data === "number") {
|
|
411
|
+
deletedCount = response.data;
|
|
412
|
+
}
|
|
413
|
+
// Confirm operation
|
|
414
|
+
operation.updateStatus(Status.CONFIRMED, [args]);
|
|
415
|
+
// Swap in real result and freeze
|
|
416
|
+
liveResult = [deletedCount, { [modelName]: deletedCount }];
|
|
417
|
+
breakThenable(liveResult);
|
|
418
|
+
return liveResult;
|
|
419
|
+
})
|
|
420
|
+
.catch((err) => {
|
|
421
|
+
operation.updateStatus(Status.REJECTED);
|
|
422
|
+
throw err;
|
|
423
|
+
});
|
|
424
|
+
// 3) return live‑thenable
|
|
425
|
+
return makeLiveThenable(liveResult, promise);
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Executes a query operation with the QuerySet.
|
|
429
|
+
*
|
|
430
|
+
* @param {QuerySet} querySet - The QuerySet to execute.
|
|
431
|
+
* @param {string} operationType - The operation type to perform.
|
|
432
|
+
* @param {Object} args - Additional arguments for the operation.
|
|
433
|
+
* @returns {Promise<any>} The operation result.
|
|
434
|
+
*/
|
|
435
|
+
static execute(querySet, operationType = "list", args = {}) {
|
|
436
|
+
// execute the query and return the result
|
|
437
|
+
switch (operationType) {
|
|
438
|
+
case "get":
|
|
439
|
+
case "first":
|
|
440
|
+
case "last":
|
|
441
|
+
return this.executeGet(querySet, operationType, args);
|
|
442
|
+
case "update_instance":
|
|
443
|
+
return this.executeUpdateInstance(querySet, operationType, args);
|
|
444
|
+
case "delete_instance":
|
|
445
|
+
return this.executeDeleteInstance(querySet, operationType, args);
|
|
446
|
+
case "update":
|
|
447
|
+
return this.executeUpdate(querySet, operationType, args);
|
|
448
|
+
case "delete":
|
|
449
|
+
return this.executeDelete(querySet, operationType, args);
|
|
450
|
+
case "create":
|
|
451
|
+
return this.executeCreate(querySet, operationType, args);
|
|
452
|
+
case "get_or_create":
|
|
453
|
+
case "update_or_create":
|
|
454
|
+
return this.executeOrCreate(querySet, operationType, args);
|
|
455
|
+
case "min":
|
|
456
|
+
case "max":
|
|
457
|
+
case "avg":
|
|
458
|
+
case "sum":
|
|
459
|
+
case "count":
|
|
460
|
+
return this.executeAgg(querySet, operationType, args);
|
|
461
|
+
case "exists":
|
|
462
|
+
return this.executeExists(querySet, operationType, args);
|
|
463
|
+
case "list":
|
|
464
|
+
return this.executeList(querySet, operationType, args);
|
|
465
|
+
}
|
|
466
|
+
throw new Error(`Invalid operation type: ${operationType}`);
|
|
467
|
+
}
|
|
468
|
+
}
|