@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,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} SerializerOptions
|
|
3
|
+
* @property {number} [depth] - How deep to serialize nested objects.
|
|
4
|
+
* @property {string[]} [fields] - List of fields to include.
|
|
5
|
+
* @property {number} [limit] - Maximum number of items to retrieve.
|
|
6
|
+
* @property {number} [offset] - Offset for pagination.
|
|
7
|
+
* @property {number} [overfetch] - Overfetch additional items.
|
|
8
|
+
* @property {string} [namespace] - Custom namespace for real-time updates.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* Manager class providing helper methods to work with QuerySets and models.
|
|
12
|
+
*
|
|
13
|
+
* @class Manager
|
|
14
|
+
*/
|
|
15
|
+
export class Manager {
|
|
16
|
+
/**
|
|
17
|
+
* Creates a new Manager.
|
|
18
|
+
*
|
|
19
|
+
* @param {Function} ModelClass - The model's constructor.
|
|
20
|
+
* @param {Function} [QuerySetClass=QuerySet] - The QuerySet class to use.
|
|
21
|
+
*/
|
|
22
|
+
constructor(ModelClass: Function, QuerySetClass?: Function);
|
|
23
|
+
ModelClass: Function;
|
|
24
|
+
QuerySetClass: Function;
|
|
25
|
+
/**
|
|
26
|
+
* Creates a new QuerySet instance.
|
|
27
|
+
*
|
|
28
|
+
* @returns {QuerySet} A new QuerySet instance for the model.
|
|
29
|
+
*/
|
|
30
|
+
newQuerySet(): QuerySet<any>;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new custom QuerySet instance with an initial name.
|
|
33
|
+
*
|
|
34
|
+
* @param {string} name - The initial queryset name.
|
|
35
|
+
* @returns {QuerySet} A new QuerySet instance for the model.
|
|
36
|
+
*/
|
|
37
|
+
customQueryset(name: string): QuerySet<any>;
|
|
38
|
+
/**
|
|
39
|
+
* Retrieves a single model instance matching the provided filters.
|
|
40
|
+
*
|
|
41
|
+
* @param {Object} [filters] - The filters to apply.
|
|
42
|
+
* @param {SerializerOptions} [serializerOptions] - Options for serialization.
|
|
43
|
+
* @returns {Promise<Model>} A promise that resolves to the model instance.
|
|
44
|
+
*/
|
|
45
|
+
get(filters?: Object, serializerOptions?: SerializerOptions): Promise<Model>;
|
|
46
|
+
/**
|
|
47
|
+
* Filters the QuerySet based on the provided conditions.
|
|
48
|
+
*
|
|
49
|
+
* @param {*} conditions - The filter conditions.
|
|
50
|
+
* @returns {QuerySet} A new QuerySet instance with the filters applied.
|
|
51
|
+
*/
|
|
52
|
+
filter(conditions: any): QuerySet<any>;
|
|
53
|
+
/**
|
|
54
|
+
* Excludes the specified conditions from the QuerySet.
|
|
55
|
+
*
|
|
56
|
+
* @param {*} conditions - The conditions to exclude.
|
|
57
|
+
* @returns {QuerySet} A new QuerySet instance with the conditions excluded.
|
|
58
|
+
*/
|
|
59
|
+
exclude(conditions: any): QuerySet<any>;
|
|
60
|
+
/**
|
|
61
|
+
* Returns a QuerySet representing all records.
|
|
62
|
+
*
|
|
63
|
+
* @returns {QuerySet} A new QuerySet instance.
|
|
64
|
+
*/
|
|
65
|
+
all(): QuerySet<any>;
|
|
66
|
+
/**
|
|
67
|
+
* Deletes records in the QuerySet.
|
|
68
|
+
*
|
|
69
|
+
* @returns {Promise<[number, Object]>} A promise that resolves to an array where the first element is
|
|
70
|
+
* the number of records deleted and the second is an object with details.
|
|
71
|
+
*/
|
|
72
|
+
delete(): Promise<[number, Object]>;
|
|
73
|
+
/**
|
|
74
|
+
* Counts the number of records matching the current query.
|
|
75
|
+
*
|
|
76
|
+
* @param {string} [field] - Optional field to count (defaults to 'pk')
|
|
77
|
+
* @returns {Promise<number>} A promise that resolves to the count
|
|
78
|
+
*/
|
|
79
|
+
count(field?: string): Promise<number>;
|
|
80
|
+
/**
|
|
81
|
+
* Computes the sum of values for the specified field.
|
|
82
|
+
*
|
|
83
|
+
* @param {string} field - The field to sum
|
|
84
|
+
* @returns {Promise<number>} A promise that resolves to the sum
|
|
85
|
+
*/
|
|
86
|
+
sum(field: string): Promise<number>;
|
|
87
|
+
/**
|
|
88
|
+
* Computes the average value for the specified field.
|
|
89
|
+
*
|
|
90
|
+
* @param {string} field - The field to average
|
|
91
|
+
* @returns {Promise<number>} A promise that resolves to the average
|
|
92
|
+
*/
|
|
93
|
+
avg(field: string): Promise<number>;
|
|
94
|
+
/**
|
|
95
|
+
* Finds the minimum value for the specified field.
|
|
96
|
+
*
|
|
97
|
+
* @param {string} field - The field to find the minimum value for
|
|
98
|
+
* @returns {Promise<any>} A promise that resolves to the minimum value
|
|
99
|
+
*/
|
|
100
|
+
min(field: string): Promise<any>;
|
|
101
|
+
/**
|
|
102
|
+
* Finds the maximum value for the specified field.
|
|
103
|
+
*
|
|
104
|
+
* @param {string} field - The field to find the maximum value for
|
|
105
|
+
* @returns {Promise<any>} A promise that resolves to the maximum value
|
|
106
|
+
*/
|
|
107
|
+
max(field: string): Promise<any>;
|
|
108
|
+
/**
|
|
109
|
+
* Checks if any records exist that match the current query.
|
|
110
|
+
*
|
|
111
|
+
* @returns {Promise<boolean>} A promise that resolves to true if records exist, otherwise false
|
|
112
|
+
*/
|
|
113
|
+
exists(): Promise<boolean>;
|
|
114
|
+
/**
|
|
115
|
+
* Orders the QuerySet by the provided fields.
|
|
116
|
+
*
|
|
117
|
+
* @param {...(string|any)} fields - The fields to order by. Supports nested paths and descending order
|
|
118
|
+
* (prefix field with '-').
|
|
119
|
+
* @returns {QuerySet} A new QuerySet instance with the order applied.
|
|
120
|
+
*/
|
|
121
|
+
orderBy(...fields: (string | any)[]): QuerySet<any>;
|
|
122
|
+
/**
|
|
123
|
+
* Creates a new model instance using the provided data, then saves it.
|
|
124
|
+
*
|
|
125
|
+
* @param {*} data - The data to create the model instance.
|
|
126
|
+
* @returns {Promise<*>} A promise that resolves to the newly created model instance.
|
|
127
|
+
*/
|
|
128
|
+
create(data: any): Promise<any>;
|
|
129
|
+
/**
|
|
130
|
+
* Fetches all records using the current QuerySet.
|
|
131
|
+
*
|
|
132
|
+
* @param {SerializerOptions} [serializerOptions] - Options for serialization.
|
|
133
|
+
* @returns {Promise<Array<*>>} A promise that resolves to an array of model instances.
|
|
134
|
+
*/
|
|
135
|
+
fetch(serializerOptions?: SerializerOptions): Promise<Array<any>>;
|
|
136
|
+
/**
|
|
137
|
+
* Retrieves or creates a model instance based on lookup fields and defaults.
|
|
138
|
+
*
|
|
139
|
+
* @param {*} lookupFields - The fields to lookup the model.
|
|
140
|
+
* @param {Object} [options={}] - Options including defaults.
|
|
141
|
+
* @param {*} [options.defaults={}] - Default values to use when creating a new instance.
|
|
142
|
+
* @returns {Promise<ResultTuple>} A promise that resolves to a ResultTuple containing the model instance
|
|
143
|
+
* and a boolean indicating whether it was created.
|
|
144
|
+
*/
|
|
145
|
+
getOrCreate(lookupFields: any, options?: {
|
|
146
|
+
defaults?: any;
|
|
147
|
+
}): Promise<ResultTuple>;
|
|
148
|
+
/**
|
|
149
|
+
* Updates or creates a model instance based on lookup fields and defaults.
|
|
150
|
+
*
|
|
151
|
+
* @param {*} lookupFields - The fields to lookup the model.
|
|
152
|
+
* @param {Object} [options={}] - Options including defaults.
|
|
153
|
+
* @param {*} [options.defaults={}] - Default values to use when updating or creating the instance.
|
|
154
|
+
* @returns {Promise<ResultTuple>} A promise that resolves to a ResultTuple containing the model instance
|
|
155
|
+
* and a boolean indicating whether it was created.
|
|
156
|
+
*/
|
|
157
|
+
updateOrCreate(lookupFields: any, options?: {
|
|
158
|
+
defaults?: any;
|
|
159
|
+
}): Promise<ResultTuple>;
|
|
160
|
+
/**
|
|
161
|
+
* Applies a search to the QuerySet using the specified search query and fields.
|
|
162
|
+
*
|
|
163
|
+
* @param {string} searchQuery - The search query.
|
|
164
|
+
* @param {string[]} [searchFields] - The fields to search in.
|
|
165
|
+
* @returns {QuerySet} A new QuerySet instance with the search applied.
|
|
166
|
+
*/
|
|
167
|
+
search(searchQuery: string, searchFields?: string[]): QuerySet<any>;
|
|
168
|
+
}
|
|
169
|
+
export type SerializerOptions = {
|
|
170
|
+
/**
|
|
171
|
+
* - How deep to serialize nested objects.
|
|
172
|
+
*/
|
|
173
|
+
depth?: number | undefined;
|
|
174
|
+
/**
|
|
175
|
+
* - List of fields to include.
|
|
176
|
+
*/
|
|
177
|
+
fields?: string[] | undefined;
|
|
178
|
+
/**
|
|
179
|
+
* - Maximum number of items to retrieve.
|
|
180
|
+
*/
|
|
181
|
+
limit?: number | undefined;
|
|
182
|
+
/**
|
|
183
|
+
* - Offset for pagination.
|
|
184
|
+
*/
|
|
185
|
+
offset?: number | undefined;
|
|
186
|
+
/**
|
|
187
|
+
* - Overfetch additional items.
|
|
188
|
+
*/
|
|
189
|
+
overfetch?: number | undefined;
|
|
190
|
+
/**
|
|
191
|
+
* - Custom namespace for real-time updates.
|
|
192
|
+
*/
|
|
193
|
+
namespace?: string | undefined;
|
|
194
|
+
};
|
|
195
|
+
import { QuerySet } from './querySet.js';
|
|
196
|
+
import { Model } from './model.js';
|
|
197
|
+
import { ResultTuple } from './queryExecutor.js';
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { QuerySet } from './querySet.js';
|
|
2
|
+
import { Model } from './model.js';
|
|
3
|
+
import { QueryExecutor, ResultTuple } from './queryExecutor.js';
|
|
4
|
+
import axios from 'axios';
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {Object} SerializerOptions
|
|
7
|
+
* @property {number} [depth] - How deep to serialize nested objects.
|
|
8
|
+
* @property {string[]} [fields] - List of fields to include.
|
|
9
|
+
* @property {number} [limit] - Maximum number of items to retrieve.
|
|
10
|
+
* @property {number} [offset] - Offset for pagination.
|
|
11
|
+
* @property {number} [overfetch] - Overfetch additional items.
|
|
12
|
+
* @property {string} [namespace] - Custom namespace for real-time updates.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Manager class providing helper methods to work with QuerySets and models.
|
|
16
|
+
*
|
|
17
|
+
* @class Manager
|
|
18
|
+
*/
|
|
19
|
+
export class Manager {
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new Manager.
|
|
22
|
+
*
|
|
23
|
+
* @param {Function} ModelClass - The model's constructor.
|
|
24
|
+
* @param {Function} [QuerySetClass=QuerySet] - The QuerySet class to use.
|
|
25
|
+
*/
|
|
26
|
+
constructor(ModelClass, QuerySetClass = QuerySet) {
|
|
27
|
+
this.ModelClass = ModelClass;
|
|
28
|
+
this.QuerySetClass = QuerySetClass;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Creates a new QuerySet instance.
|
|
32
|
+
*
|
|
33
|
+
* @returns {QuerySet} A new QuerySet instance for the model.
|
|
34
|
+
*/
|
|
35
|
+
newQuerySet() {
|
|
36
|
+
return new this.QuerySetClass(this.ModelClass);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new custom QuerySet instance with an initial name.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} name - The initial queryset name.
|
|
42
|
+
* @returns {QuerySet} A new QuerySet instance for the model.
|
|
43
|
+
*/
|
|
44
|
+
customQueryset(name) {
|
|
45
|
+
return new this.QuerySetClass(this.ModelClass, {
|
|
46
|
+
initialQueryset: name
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Retrieves a single model instance matching the provided filters.
|
|
51
|
+
*
|
|
52
|
+
* @param {Object} [filters] - The filters to apply.
|
|
53
|
+
* @param {SerializerOptions} [serializerOptions] - Options for serialization.
|
|
54
|
+
* @returns {Promise<Model>} A promise that resolves to the model instance.
|
|
55
|
+
*/
|
|
56
|
+
async get(filters, serializerOptions) {
|
|
57
|
+
let querySet = this.newQuerySet();
|
|
58
|
+
if (filters) {
|
|
59
|
+
querySet = querySet.filter(filters);
|
|
60
|
+
}
|
|
61
|
+
if (serializerOptions) {
|
|
62
|
+
querySet.setSerializerOptions(serializerOptions);
|
|
63
|
+
}
|
|
64
|
+
return await QueryExecutor.execute(querySet, 'get');
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Filters the QuerySet based on the provided conditions.
|
|
68
|
+
*
|
|
69
|
+
* @param {*} conditions - The filter conditions.
|
|
70
|
+
* @returns {QuerySet} A new QuerySet instance with the filters applied.
|
|
71
|
+
*/
|
|
72
|
+
filter(conditions) {
|
|
73
|
+
return this.newQuerySet().filter(conditions);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Excludes the specified conditions from the QuerySet.
|
|
77
|
+
*
|
|
78
|
+
* @param {*} conditions - The conditions to exclude.
|
|
79
|
+
* @returns {QuerySet} A new QuerySet instance with the conditions excluded.
|
|
80
|
+
*/
|
|
81
|
+
exclude(conditions) {
|
|
82
|
+
return this.newQuerySet().exclude(conditions);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Returns a QuerySet representing all records.
|
|
86
|
+
*
|
|
87
|
+
* @returns {QuerySet} A new QuerySet instance.
|
|
88
|
+
*/
|
|
89
|
+
all() {
|
|
90
|
+
return this.newQuerySet();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Deletes records in the QuerySet.
|
|
94
|
+
*
|
|
95
|
+
* @returns {Promise<[number, Object]>} A promise that resolves to an array where the first element is
|
|
96
|
+
* the number of records deleted and the second is an object with details.
|
|
97
|
+
*/
|
|
98
|
+
async delete() {
|
|
99
|
+
return await QueryExecutor.execute(this.newQuerySet(), 'delete');
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Counts the number of records matching the current query.
|
|
103
|
+
*
|
|
104
|
+
* @param {string} [field] - Optional field to count (defaults to 'pk')
|
|
105
|
+
* @returns {Promise<number>} A promise that resolves to the count
|
|
106
|
+
*/
|
|
107
|
+
async count(field) {
|
|
108
|
+
return await QueryExecutor.execute(this.newQuerySet(), 'count', { field });
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Computes the sum of values for the specified field.
|
|
112
|
+
*
|
|
113
|
+
* @param {string} field - The field to sum
|
|
114
|
+
* @returns {Promise<number>} A promise that resolves to the sum
|
|
115
|
+
*/
|
|
116
|
+
async sum(field) {
|
|
117
|
+
return await QueryExecutor.execute(this.newQuerySet(), 'sum', { field });
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Computes the average value for the specified field.
|
|
121
|
+
*
|
|
122
|
+
* @param {string} field - The field to average
|
|
123
|
+
* @returns {Promise<number>} A promise that resolves to the average
|
|
124
|
+
*/
|
|
125
|
+
async avg(field) {
|
|
126
|
+
return await QueryExecutor.execute(this.newQuerySet(), 'avg', { field });
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Finds the minimum value for the specified field.
|
|
130
|
+
*
|
|
131
|
+
* @param {string} field - The field to find the minimum value for
|
|
132
|
+
* @returns {Promise<any>} A promise that resolves to the minimum value
|
|
133
|
+
*/
|
|
134
|
+
async min(field) {
|
|
135
|
+
return await QueryExecutor.execute(this.newQuerySet(), 'min', { field });
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Finds the maximum value for the specified field.
|
|
139
|
+
*
|
|
140
|
+
* @param {string} field - The field to find the maximum value for
|
|
141
|
+
* @returns {Promise<any>} A promise that resolves to the maximum value
|
|
142
|
+
*/
|
|
143
|
+
async max(field) {
|
|
144
|
+
return await QueryExecutor.execute(this.newQuerySet(), 'max', { field });
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Checks if any records exist that match the current query.
|
|
148
|
+
*
|
|
149
|
+
* @returns {Promise<boolean>} A promise that resolves to true if records exist, otherwise false
|
|
150
|
+
*/
|
|
151
|
+
async exists() {
|
|
152
|
+
return await QueryExecutor.execute(this.newQuerySet(), 'exists');
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Orders the QuerySet by the provided fields.
|
|
156
|
+
*
|
|
157
|
+
* @param {...(string|any)} fields - The fields to order by. Supports nested paths and descending order
|
|
158
|
+
* (prefix field with '-').
|
|
159
|
+
* @returns {QuerySet} A new QuerySet instance with the order applied.
|
|
160
|
+
*/
|
|
161
|
+
orderBy(...fields) {
|
|
162
|
+
return this.newQuerySet().orderBy(...fields);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Creates a new model instance using the provided data, then saves it.
|
|
166
|
+
*
|
|
167
|
+
* @param {*} data - The data to create the model instance.
|
|
168
|
+
* @returns {Promise<*>} A promise that resolves to the newly created model instance.
|
|
169
|
+
*/
|
|
170
|
+
async create(data) {
|
|
171
|
+
return await QueryExecutor.execute(this.newQuerySet(), 'create', { data });
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Fetches all records using the current QuerySet.
|
|
175
|
+
*
|
|
176
|
+
* @param {SerializerOptions} [serializerOptions] - Options for serialization.
|
|
177
|
+
* @returns {Promise<Array<*>>} A promise that resolves to an array of model instances.
|
|
178
|
+
*/
|
|
179
|
+
async fetch(serializerOptions) {
|
|
180
|
+
const querySet = this.newQuerySet();
|
|
181
|
+
if (serializerOptions) {
|
|
182
|
+
querySet.setSerializerOptions(serializerOptions);
|
|
183
|
+
}
|
|
184
|
+
return await QueryExecutor.execute(querySet, 'list');
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Retrieves or creates a model instance based on lookup fields and defaults.
|
|
188
|
+
*
|
|
189
|
+
* @param {*} lookupFields - The fields to lookup the model.
|
|
190
|
+
* @param {Object} [options={}] - Options including defaults.
|
|
191
|
+
* @param {*} [options.defaults={}] - Default values to use when creating a new instance.
|
|
192
|
+
* @returns {Promise<ResultTuple>} A promise that resolves to a ResultTuple containing the model instance
|
|
193
|
+
* and a boolean indicating whether it was created.
|
|
194
|
+
*/
|
|
195
|
+
async getOrCreate(lookupFields, options = {}) {
|
|
196
|
+
const { defaults = {} } = options;
|
|
197
|
+
return await QueryExecutor.execute(this.newQuerySet(), 'get_or_create', { lookup: lookupFields, defaults });
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Updates or creates a model instance based on lookup fields and defaults.
|
|
201
|
+
*
|
|
202
|
+
* @param {*} lookupFields - The fields to lookup the model.
|
|
203
|
+
* @param {Object} [options={}] - Options including defaults.
|
|
204
|
+
* @param {*} [options.defaults={}] - Default values to use when updating or creating the instance.
|
|
205
|
+
* @returns {Promise<ResultTuple>} A promise that resolves to a ResultTuple containing the model instance
|
|
206
|
+
* and a boolean indicating whether it was created.
|
|
207
|
+
*/
|
|
208
|
+
async updateOrCreate(lookupFields, options = {}) {
|
|
209
|
+
const { defaults = {} } = options;
|
|
210
|
+
return await QueryExecutor.execute(this.newQuerySet(), 'update_or_create', { lookup: lookupFields, defaults });
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Applies a search to the QuerySet using the specified search query and fields.
|
|
214
|
+
*
|
|
215
|
+
* @param {string} searchQuery - The search query.
|
|
216
|
+
* @param {string[]} [searchFields] - The fields to search in.
|
|
217
|
+
* @returns {QuerySet} A new QuerySet instance with the search applied.
|
|
218
|
+
*/
|
|
219
|
+
search(searchQuery, searchFields) {
|
|
220
|
+
return this.newQuerySet().search(searchQuery, searchFields);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A constructor for a Model.
|
|
3
|
+
*
|
|
4
|
+
* @typedef {Function} ModelConstructor
|
|
5
|
+
* @param {any} data - Data to initialize the model.
|
|
6
|
+
* @returns {Model}
|
|
7
|
+
*
|
|
8
|
+
* @property {Manager} objects - The model's manager.
|
|
9
|
+
* @property {string} configKey - The configuration key.
|
|
10
|
+
* @property {string} modelName - The model name.
|
|
11
|
+
* @property {string} primaryKeyField - The primary key field (default 'id').
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Base Model class with integrated API implementation.
|
|
15
|
+
*
|
|
16
|
+
* @abstract
|
|
17
|
+
*/
|
|
18
|
+
export class Model {
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new Model instance.
|
|
21
|
+
*
|
|
22
|
+
* @param {any} [data={}] - The data for initialization.
|
|
23
|
+
*/
|
|
24
|
+
static instanceCache: Map<any, any>;
|
|
25
|
+
/**
|
|
26
|
+
* Instantiate from pk using queryset scoped singletons
|
|
27
|
+
*/
|
|
28
|
+
static fromPk(pk: any, querySet: any): any;
|
|
29
|
+
/**
|
|
30
|
+
* Validates that the provided data object only contains keys
|
|
31
|
+
* defined in the model's allowed fields. Supports nested fields
|
|
32
|
+
* using double underscore notation (e.g., author__name).
|
|
33
|
+
*
|
|
34
|
+
* @param {Object} data - The object to validate.
|
|
35
|
+
* @throws {ValidationError} If an unknown key is found.
|
|
36
|
+
*/
|
|
37
|
+
static validateFields(data: Object): void;
|
|
38
|
+
constructor(data?: {});
|
|
39
|
+
_data: {};
|
|
40
|
+
_pk: any;
|
|
41
|
+
__version: number;
|
|
42
|
+
touch(): void;
|
|
43
|
+
/**
|
|
44
|
+
* Sets the primary key of the model instance.
|
|
45
|
+
*
|
|
46
|
+
* @param {number|undefined} value - The new primary key value.
|
|
47
|
+
*/
|
|
48
|
+
set pk(value: number | undefined);
|
|
49
|
+
/**
|
|
50
|
+
* Returns the primary key of the model instance.
|
|
51
|
+
*
|
|
52
|
+
* @returns {number|undefined} The primary key.
|
|
53
|
+
*/
|
|
54
|
+
get pk(): number | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Gets a field value from the internal data store
|
|
57
|
+
*
|
|
58
|
+
* @param {string} field - The field name
|
|
59
|
+
* @returns {any} The field value
|
|
60
|
+
*/
|
|
61
|
+
getField(field: string): any;
|
|
62
|
+
/**
|
|
63
|
+
* Sets a field value in the internal data store
|
|
64
|
+
*
|
|
65
|
+
* @param {string} field - The field name
|
|
66
|
+
* @param {any} value - The field value to set
|
|
67
|
+
*/
|
|
68
|
+
setField(field: string, value: any): void;
|
|
69
|
+
/**
|
|
70
|
+
* Serializes a field value for API transmission
|
|
71
|
+
*
|
|
72
|
+
* @param {string} field - The field name
|
|
73
|
+
* @returns {any} The serialized field value
|
|
74
|
+
*/
|
|
75
|
+
serializeField(field: string): any;
|
|
76
|
+
/**
|
|
77
|
+
* Serializes the model instance.
|
|
78
|
+
*
|
|
79
|
+
* By default, it returns all enumerable own properties.
|
|
80
|
+
* Subclasses should override this to return specific keys.
|
|
81
|
+
*
|
|
82
|
+
* @returns {Object} The serialized model data.
|
|
83
|
+
*/
|
|
84
|
+
serialize(): Object;
|
|
85
|
+
/**
|
|
86
|
+
* Saves the model instance by either creating a new record or updating an existing one.
|
|
87
|
+
*
|
|
88
|
+
* @returns {Promise<Model>} A promise that resolves to the updated model instance.
|
|
89
|
+
*/
|
|
90
|
+
save(): Promise<Model>;
|
|
91
|
+
/**
|
|
92
|
+
* Deletes the instance from the database.
|
|
93
|
+
*
|
|
94
|
+
* Returns a tuple with the number of objects deleted and an object mapping
|
|
95
|
+
* model names to the number of objects deleted, matching Django's behavior.
|
|
96
|
+
*
|
|
97
|
+
* @returns {Promise<[number, Object]>} A promise that resolves to the deletion result.
|
|
98
|
+
* @throws {Error} If the instance has not been saved (no primary key).
|
|
99
|
+
*/
|
|
100
|
+
delete(): Promise<[number, Object]>;
|
|
101
|
+
/**
|
|
102
|
+
* Refreshes the model instance with data from the database.
|
|
103
|
+
*
|
|
104
|
+
* @returns {Promise<void>} A promise that resolves when the instance has been refreshed.
|
|
105
|
+
* @throws {Error} If the instance has not been saved (no primary key).
|
|
106
|
+
*/
|
|
107
|
+
refreshFromDb(): Promise<void>;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* A constructor for a Model.
|
|
111
|
+
*/
|
|
112
|
+
export type ModelConstructor = Function;
|