@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 @@
|
|
|
1
|
+
export function useQueryset(querysetFactory: any): any;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useQueryset } from "./composables.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useQueryset } from './composables.js';
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { computed, getCurrentInstance, onBeforeUnmount } from 'vue';
|
|
2
|
+
import { querysetStoreRegistry } from '../../syncEngine/registries/querysetStoreRegistry';
|
|
3
|
+
import { metricRegistry } from '../../syncEngine/registries/metricRegistry';
|
|
4
|
+
import { syncManager } from '../../syncEngine/sync';
|
|
5
|
+
import { v7 } from 'uuid';
|
|
6
|
+
syncManager.followAllQuerysets = false;
|
|
7
|
+
export const querysets = new Map(); // Map of composableId -> queryset
|
|
8
|
+
function updateSyncManager() {
|
|
9
|
+
// Get unique querysets from all active composables
|
|
10
|
+
const uniqueQuerysets = new Set(querysets.values());
|
|
11
|
+
syncManager.followedQuerysets = uniqueQuerysets;
|
|
12
|
+
}
|
|
13
|
+
export function useQueryset(querysetFactory) {
|
|
14
|
+
const instance = getCurrentInstance();
|
|
15
|
+
if (!instance) {
|
|
16
|
+
throw new Error('useQueryset must be called within a component setup function');
|
|
17
|
+
}
|
|
18
|
+
const composableId = v7();
|
|
19
|
+
let lastQueryset = null;
|
|
20
|
+
onBeforeUnmount(() => {
|
|
21
|
+
querysets.delete(composableId);
|
|
22
|
+
updateSyncManager();
|
|
23
|
+
});
|
|
24
|
+
return computed(() => {
|
|
25
|
+
const result = querysetFactory();
|
|
26
|
+
const original = result?.original || result;
|
|
27
|
+
const queryset = original?.queryset || original;
|
|
28
|
+
// Only update if queryset actually changed
|
|
29
|
+
if (lastQueryset !== queryset) {
|
|
30
|
+
querysets.set(composableId, queryset);
|
|
31
|
+
updateSyncManager();
|
|
32
|
+
lastQueryset = queryset;
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapts a model instance to a Vue reactive object by directly wrapping
|
|
3
|
+
* the instance and incrementing an internal version on relevant events.
|
|
4
|
+
*
|
|
5
|
+
* @param {Object} modelInstance - An instance of a model class with static modelName and primaryKeyField
|
|
6
|
+
* @param {Function} [reactivityFn=reactive] - Which Vue reactivity function to use (reactive or ref)
|
|
7
|
+
* @returns {import('vue').Reactive|import('vue').Ref} The reactive model instance, augmented with __version
|
|
8
|
+
*/
|
|
9
|
+
export function ModelAdaptor(modelInstance: Object, reactivityFn?: Function): any | import("vue").Ref;
|
|
10
|
+
/**
|
|
11
|
+
* Adapts a queryset to a Vue reactive object and sets up event handling for queryset updates
|
|
12
|
+
*
|
|
13
|
+
* @param {Object} liveQuerySet - A LiveQueryset instance
|
|
14
|
+
* @param {Function} [reactivityFn=reactive] - Which Vue reactivity function to use (reactive or ref)
|
|
15
|
+
* @returns {import('vue').Reactive|import('vue').Ref} The reactive queryset
|
|
16
|
+
*/
|
|
17
|
+
export function QuerySetAdaptor(liveQuerySet: Object, reactivityFn?: Function): any | import("vue").Ref;
|
|
18
|
+
export function MetricAdaptor(metric: any): any;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { reactive, ref, nextTick } from "vue";
|
|
2
|
+
import { modelEventEmitter, querysetEventEmitter, metricEventEmitter } from "../../syncEngine/stores/reactivity.js";
|
|
3
|
+
import { initEventHandler } from "../../syncEngine/stores/operationEventHandlers.js";
|
|
4
|
+
import { isEqual, isNil } from 'lodash-es';
|
|
5
|
+
import hash from 'object-hash';
|
|
6
|
+
initEventHandler(); // Initialize event handler for model events
|
|
7
|
+
const wrappedQuerysetCache = new Map();
|
|
8
|
+
const wrappedMetricCache = new Map();
|
|
9
|
+
/**
|
|
10
|
+
* Adapts a model instance to a Vue reactive object by directly wrapping
|
|
11
|
+
* the instance and incrementing an internal version on relevant events.
|
|
12
|
+
*
|
|
13
|
+
* @param {Object} modelInstance - An instance of a model class with static modelName and primaryKeyField
|
|
14
|
+
* @param {Function} [reactivityFn=reactive] - Which Vue reactivity function to use (reactive or ref)
|
|
15
|
+
* @returns {import('vue').Reactive|import('vue').Ref} The reactive model instance, augmented with __version
|
|
16
|
+
*/
|
|
17
|
+
export function ModelAdaptor(modelInstance, reactivityFn = reactive) {
|
|
18
|
+
const modelClass = modelInstance.constructor;
|
|
19
|
+
const modelName = modelClass.modelName;
|
|
20
|
+
const configKey = modelClass.configKey;
|
|
21
|
+
const pkField = modelClass.primaryKeyField;
|
|
22
|
+
// Make the model instance reactive using the specified function
|
|
23
|
+
const wrapper = reactivityFn(modelInstance);
|
|
24
|
+
const eventName = `${configKey}::${modelName}::render`;
|
|
25
|
+
// Handler bumps version to trigger Vue reactivity when this instance updates
|
|
26
|
+
const renderHandler = (eventData) => {
|
|
27
|
+
const isRef = reactivityFn === ref;
|
|
28
|
+
const model = isRef ? wrapper.value : wrapper;
|
|
29
|
+
if (eventData.pk === model[pkField]) {
|
|
30
|
+
if (isRef) {
|
|
31
|
+
wrapper.value.touch();
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
wrapper.touch();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
// Subscribe to model events indefinitely
|
|
39
|
+
modelEventEmitter.on(eventName, renderHandler);
|
|
40
|
+
return wrapper;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Adapts a queryset to a Vue reactive object and sets up event handling for queryset updates
|
|
44
|
+
*
|
|
45
|
+
* @param {Object} liveQuerySet - A LiveQueryset instance
|
|
46
|
+
* @param {Function} [reactivityFn=reactive] - Which Vue reactivity function to use (reactive or ref)
|
|
47
|
+
* @returns {import('vue').Reactive|import('vue').Ref} The reactive queryset
|
|
48
|
+
*/
|
|
49
|
+
export function QuerySetAdaptor(liveQuerySet, reactivityFn = reactive) {
|
|
50
|
+
const queryset = liveQuerySet?.queryset;
|
|
51
|
+
const modelName = queryset?.ModelClass?.modelName;
|
|
52
|
+
const configKey = queryset?.ModelClass?.configKey;
|
|
53
|
+
if (isNil(queryset) || isNil(modelName)) {
|
|
54
|
+
throw new Error(`liveQuerySet ${JSON.stringify(liveQuerySet)} had null qs: ${queryset} or model: ${modelName}`);
|
|
55
|
+
}
|
|
56
|
+
// Use the semantic key if available
|
|
57
|
+
const cacheKey = queryset.semanticKey;
|
|
58
|
+
// Check the cache first
|
|
59
|
+
if (cacheKey && wrappedQuerysetCache.has(cacheKey)) {
|
|
60
|
+
return wrappedQuerysetCache.get(cacheKey);
|
|
61
|
+
}
|
|
62
|
+
const querysetAst = queryset.build();
|
|
63
|
+
// Make the queryset reactive using the specified function
|
|
64
|
+
const wrapper = reactivityFn([...liveQuerySet]);
|
|
65
|
+
wrapper.original = liveQuerySet;
|
|
66
|
+
const eventName = `${configKey}::${modelName}::queryset::render`;
|
|
67
|
+
// Handler bumps version to trigger Vue reactivity when this queryset updates
|
|
68
|
+
const renderHandler = (eventData) => {
|
|
69
|
+
if (eventData && eventData.ast && isEqual(querysetAst, eventData.ast)) {
|
|
70
|
+
if (reactivityFn === ref) {
|
|
71
|
+
wrapper.value = [...liveQuerySet];
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
wrapper.splice(0, wrapper.length);
|
|
75
|
+
wrapper.push(...liveQuerySet);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
// Subscribe to queryset events indefinitely
|
|
80
|
+
querysetEventEmitter.on(eventName, renderHandler);
|
|
81
|
+
/* Dont delete the innocuous looking queryset.length check. There is some weird interaction
|
|
82
|
+
with vue, where when we load an empty queryset from the cache, the reactivity completely breaks.
|
|
83
|
+
I wasted over 2 days on this bug, and it won't show up in the e2e tests because it just impacts the
|
|
84
|
+
vue reactivity. If this causes performance issues, and needs to be refactored. Make sure you understand
|
|
85
|
+
the vue reactivity interaction correctly and find a different way to fix the broken reactivity for empty querysets */
|
|
86
|
+
if (cacheKey && liveQuerySet && liveQuerySet.length > 0) {
|
|
87
|
+
wrappedQuerysetCache.set(cacheKey, wrapper);
|
|
88
|
+
}
|
|
89
|
+
return wrapper;
|
|
90
|
+
}
|
|
91
|
+
export function MetricAdaptor(metric) {
|
|
92
|
+
const queryset = metric.queryset;
|
|
93
|
+
const modelName = queryset?.ModelClass?.modelName;
|
|
94
|
+
const configKey = queryset?.ModelClass?.configKey;
|
|
95
|
+
const querysetAst = queryset.build();
|
|
96
|
+
// Create a cache key based on metric properties
|
|
97
|
+
// This combines model, metric type, field, and the queryset AST hash to ensure uniqueness
|
|
98
|
+
const cacheKey = `${configKey}::${modelName}::${metric.metricType}::${metric.field}::${hash(querysetAst)}`;
|
|
99
|
+
// Check the cache first
|
|
100
|
+
if (cacheKey && wrappedMetricCache.has(cacheKey)) {
|
|
101
|
+
return wrappedMetricCache.get(cacheKey);
|
|
102
|
+
}
|
|
103
|
+
// Create a reactive reference with the initial value
|
|
104
|
+
const wrapper = ref(metric.value);
|
|
105
|
+
wrapper.original = metric;
|
|
106
|
+
// Single handler for metric render events
|
|
107
|
+
const metricRenderHandler = (eventData) => {
|
|
108
|
+
// Only update if this event is for our metric
|
|
109
|
+
if (eventData.metricType === metric.metricType &&
|
|
110
|
+
eventData.ModelClass === metric.queryset.ModelClass &&
|
|
111
|
+
eventData.field === metric.field &&
|
|
112
|
+
eventData.ast === hash(querysetAst) &&
|
|
113
|
+
eventData.valueChanged === true) {
|
|
114
|
+
// Update the wrapper value with the latest metric value
|
|
115
|
+
wrapper.value = metric.value;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
// Only listen for metric render events
|
|
119
|
+
metricEventEmitter.on('metric::render', metricRenderHandler);
|
|
120
|
+
// Store in cache
|
|
121
|
+
if (cacheKey) {
|
|
122
|
+
wrappedMetricCache.set(cacheKey, wrapper);
|
|
123
|
+
}
|
|
124
|
+
return wrapper;
|
|
125
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Main exported function to generate schema.
|
|
3
|
+
* @param {GenerateArgs} args
|
|
4
|
+
* @returns {Promise<void>}
|
|
5
|
+
*/
|
|
6
|
+
export function generateSchema(args: GenerateArgs): Promise<void>;
|
|
7
|
+
/**
|
|
8
|
+
* // Additional arguments for generation if needed.
|
|
9
|
+
*/
|
|
10
|
+
export type GenerateArgs = Object;
|
|
11
|
+
export type SchemaProperty = {
|
|
12
|
+
type: string;
|
|
13
|
+
format?: string | undefined;
|
|
14
|
+
items?: SchemaProperty | undefined;
|
|
15
|
+
properties?: {
|
|
16
|
+
[x: string]: SchemaProperty;
|
|
17
|
+
} | undefined;
|
|
18
|
+
required?: string[] | undefined;
|
|
19
|
+
enum?: string[] | undefined;
|
|
20
|
+
description?: string | undefined;
|
|
21
|
+
nullable?: boolean | undefined;
|
|
22
|
+
default?: any;
|
|
23
|
+
ref?: string | undefined;
|
|
24
|
+
};
|
|
25
|
+
export type RelationshipField = {
|
|
26
|
+
/**
|
|
27
|
+
* - Name of the relationship field
|
|
28
|
+
*/
|
|
29
|
+
field: string;
|
|
30
|
+
/**
|
|
31
|
+
* - The class name of the related model
|
|
32
|
+
*/
|
|
33
|
+
ModelClass: string;
|
|
34
|
+
/**
|
|
35
|
+
* - Type of relationship (e.g., "many-to-many", "foreign-key")
|
|
36
|
+
*/
|
|
37
|
+
relationshipType: string;
|
|
38
|
+
};
|
|
39
|
+
export type RelationshipData = {
|
|
40
|
+
type: string;
|
|
41
|
+
/**
|
|
42
|
+
* - e.g. "django_app.deepmodellevel1"
|
|
43
|
+
*/
|
|
44
|
+
model: string;
|
|
45
|
+
/**
|
|
46
|
+
* - e.g. "DeepModelLevel1"
|
|
47
|
+
*/
|
|
48
|
+
class_name: string;
|
|
49
|
+
primary_key_field: string;
|
|
50
|
+
};
|
|
51
|
+
export type SchemaDefinition = {
|
|
52
|
+
type: string;
|
|
53
|
+
properties: {
|
|
54
|
+
[x: string]: SchemaProperty;
|
|
55
|
+
};
|
|
56
|
+
required?: string[] | undefined;
|
|
57
|
+
description?: string | undefined;
|
|
58
|
+
definitions?: {
|
|
59
|
+
[x: string]: SchemaDefinition;
|
|
60
|
+
} | undefined;
|
|
61
|
+
model_name: string;
|
|
62
|
+
class_name: string;
|
|
63
|
+
primary_key_field?: string | undefined;
|
|
64
|
+
relationships?: {
|
|
65
|
+
[x: string]: RelationshipData;
|
|
66
|
+
} | undefined;
|
|
67
|
+
};
|
|
68
|
+
export type PropertyDefinition = {
|
|
69
|
+
name: string;
|
|
70
|
+
type: string;
|
|
71
|
+
required: boolean;
|
|
72
|
+
defaultValue: string;
|
|
73
|
+
isRelationship?: boolean | undefined;
|
|
74
|
+
relationshipClassName?: string | undefined;
|
|
75
|
+
isArrayRelationship?: boolean | undefined;
|
|
76
|
+
relationshipPrimaryKeyField?: string | undefined;
|
|
77
|
+
isString?: boolean | undefined;
|
|
78
|
+
isNumber?: boolean | undefined;
|
|
79
|
+
isBoolean?: boolean | undefined;
|
|
80
|
+
isDate?: boolean | undefined;
|
|
81
|
+
isPrimaryKey?: boolean | undefined;
|
|
82
|
+
};
|
|
83
|
+
export type TemplateData = {
|
|
84
|
+
/**
|
|
85
|
+
* - Dynamic module path for imports.
|
|
86
|
+
*/
|
|
87
|
+
modulePath: string;
|
|
88
|
+
/**
|
|
89
|
+
* - Exported full model class name (from schema.class_name).
|
|
90
|
+
*/
|
|
91
|
+
className: string;
|
|
92
|
+
/**
|
|
93
|
+
* - Full model fields interface name (e.g. DeepModelLevel1Fields).
|
|
94
|
+
*/
|
|
95
|
+
interfaceName: string;
|
|
96
|
+
/**
|
|
97
|
+
* - Raw schema.model_name (including app label path).
|
|
98
|
+
*/
|
|
99
|
+
modelName: string;
|
|
100
|
+
properties: PropertyDefinition[];
|
|
101
|
+
/**
|
|
102
|
+
* - List of relationship fields for the model
|
|
103
|
+
*/
|
|
104
|
+
relationshipFields: RelationshipField[];
|
|
105
|
+
description?: string | undefined;
|
|
106
|
+
definitions?: string[] | undefined;
|
|
107
|
+
/**
|
|
108
|
+
* - For JS generation: full class imports.
|
|
109
|
+
*/
|
|
110
|
+
jsImports?: string[] | undefined;
|
|
111
|
+
/**
|
|
112
|
+
* - For TS generation: type imports (Fields).
|
|
113
|
+
*/
|
|
114
|
+
tsImports?: string[] | undefined;
|
|
115
|
+
/**
|
|
116
|
+
* - The backend config key.
|
|
117
|
+
*/
|
|
118
|
+
configKey: string;
|
|
119
|
+
/**
|
|
120
|
+
* - Primary key field from schema.
|
|
121
|
+
*/
|
|
122
|
+
primaryKeyField: string;
|
|
123
|
+
};
|
|
124
|
+
export type BackendConfig = {
|
|
125
|
+
NAME: string;
|
|
126
|
+
API_URL: string;
|
|
127
|
+
GENERATED_TYPES_DIR: string;
|
|
128
|
+
};
|
|
129
|
+
export type SelectedModel = {
|
|
130
|
+
backend: BackendConfig;
|
|
131
|
+
model: string;
|
|
132
|
+
};
|