@statezero/core 0.1.5 → 0.1.7

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.
@@ -227,22 +227,6 @@ export class {{className}} extends Model {
227
227
  });
228
228
  });
229
229
  }
230
-
231
- /**
232
- * Serialize the model's data.
233
- * Returns a plain object containing the current values of all fields,
234
- * including raw IDs for relationship fields.
235
- * @returns {Object}
236
- */
237
- serialize() {
238
- const data = {};
239
- // Simply assign the current value of each field.
240
- // No special handling or PK extraction for relationships here.
241
- {{#each properties}}
242
- data.{{name}} = this.{{name}};
243
- {{/each}}
244
- return data;
245
- }
246
230
  }
247
231
  `;
248
232
  // Updated TS_DECLARATION_TEMPLATE with improved relationship handling
@@ -1,10 +1,10 @@
1
- import { Manager } from './manager.js';
2
- import { ValidationError } from './errors.js';
3
- import { modelStoreRegistry } from '../../syncEngine/registries/modelStoreRegistry.js';
4
- import { isNil } from 'lodash-es';
5
- import { QueryExecutor } from './queryExecutor';
6
- import { wrapReactiveModel } from '../../reactiveAdaptor.js';
7
- import { DateParsingHelpers } from './dates.js';
1
+ import { Manager } from "./manager.js";
2
+ import { ValidationError } from "./errors.js";
3
+ import { modelStoreRegistry } from "../../syncEngine/registries/modelStoreRegistry.js";
4
+ import { isNil } from "lodash-es";
5
+ import { QueryExecutor } from "./queryExecutor";
6
+ import { wrapReactiveModel } from "../../reactiveAdaptor.js";
7
+ import { DateParsingHelpers } from "./dates.js";
8
8
  /**
9
9
  * A constructor for a Model.
10
10
  *
@@ -54,7 +54,7 @@ export class Model {
54
54
  * Instantiate from pk using queryset scoped singletons
55
55
  */
56
56
  static fromPk(pk, querySet) {
57
- let qsId = querySet ? querySet.__uuid : '';
57
+ let qsId = querySet ? querySet.__uuid : "";
58
58
  let key = `${qsId}__${this.configKey}__${this.modelName}__${pk}`;
59
59
  if (!this.instanceCache.has(key)) {
60
60
  const instance = new this();
@@ -90,15 +90,15 @@ export class Model {
90
90
  // footgun - fieldInfo.ModelClass() calls the arrow function that lazily gets the model class
91
91
  let relPkField = fieldInfo.ModelClass().primaryKeyField;
92
92
  switch (fieldInfo.relationshipType) {
93
- case 'many-to-many':
93
+ case "many-to-many":
94
94
  // value is an array
95
95
  if (!Array.isArray(value) && value)
96
96
  throw new Error(`Data corruption: m2m field for ${ModelClass.modelName} stored as ${value}`);
97
97
  // set each pk to the full model object for that pk
98
- value = value.map(pk => fieldInfo.ModelClass().fromPk(pk));
98
+ value = value.map((pk) => fieldInfo.ModelClass().fromPk(pk));
99
99
  break;
100
- case 'one-to-one':
101
- case 'foreign-key':
100
+ case "one-to-one":
101
+ case "foreign-key":
102
102
  // footgun - fieldInfo.ModelClass() calls the arrow function that lazily gets the model class
103
103
  if (!isNil(value))
104
104
  value = fieldInfo.ModelClass().fromPk(value);
@@ -135,13 +135,13 @@ export class Model {
135
135
  return;
136
136
  const allowedFields = this.fields;
137
137
  for (const key of Object.keys(data)) {
138
- if (key === 'repr' || key === 'type')
138
+ if (key === "repr" || key === "type")
139
139
  continue;
140
140
  // Handle nested fields by splitting on double underscore
141
141
  // and taking just the base field name
142
- const baseField = key.split('__')[0];
142
+ const baseField = key.split("__")[0];
143
143
  if (!allowedFields.includes(baseField)) {
144
- let errorMsg = `Invalid field: ${baseField}. Allowed fields are: ${allowedFields.join(', ')}`;
144
+ let errorMsg = `Invalid field: ${baseField}. Allowed fields are: ${allowedFields.join(", ")}`;
145
145
  console.error(errorMsg);
146
146
  throw new ValidationError(errorMsg);
147
147
  }
@@ -192,16 +192,20 @@ export class Model {
192
192
  async save() {
193
193
  const ModelClass = this.constructor;
194
194
  const pkField = ModelClass.primaryKeyField;
195
- const querySet = !this.pk ? ModelClass.objects.newQuerySet() : ModelClass.objects.filter({ [pkField]: this.pk });
195
+ const querySet = !this.pk
196
+ ? ModelClass.objects.newQuerySet()
197
+ : ModelClass.objects.filter({ [pkField]: this.pk });
196
198
  const data = this.serialize();
197
199
  let instance;
198
200
  if (!this.pk) {
199
201
  // Create new instance
200
- instance = await QueryExecutor.execute(querySet, 'create', { data });
202
+ instance = await QueryExecutor.execute(querySet, "create", { data });
201
203
  }
202
204
  else {
203
205
  // Update existing instance
204
- instance = await QueryExecutor.execute(querySet, 'update_instance', { data });
206
+ instance = await QueryExecutor.execute(querySet, "update_instance", {
207
+ data,
208
+ });
205
209
  }
206
210
  this._pk = instance.pk;
207
211
  this._data = {};
@@ -218,14 +222,14 @@ export class Model {
218
222
  */
219
223
  async delete() {
220
224
  if (!this.pk) {
221
- throw new Error('Cannot delete unsaved instance');
225
+ throw new Error("Cannot delete unsaved instance");
222
226
  }
223
227
  const ModelClass = this.constructor;
224
228
  const pkField = ModelClass.primaryKeyField;
225
229
  const querySet = ModelClass.objects.filter({ [pkField]: this.pk });
226
230
  // Pass the instance data with primary key as the args
227
231
  const args = { [pkField]: this.pk };
228
- const result = await QueryExecutor.execute(querySet, 'delete_instance', args);
232
+ const result = await QueryExecutor.execute(querySet, "delete_instance", args);
229
233
  // result -> [deletedCount, { [modelName]: deletedCount }];
230
234
  return result;
231
235
  }
@@ -237,10 +241,12 @@ export class Model {
237
241
  */
238
242
  async refreshFromDb() {
239
243
  if (!this.pk) {
240
- throw new Error('Cannot refresh unsaved instance');
244
+ throw new Error("Cannot refresh unsaved instance");
241
245
  }
242
246
  const ModelClass = this.constructor;
243
- const fresh = await ModelClass.objects.get({ [ModelClass.primaryKeyField]: this.pk });
247
+ const fresh = await ModelClass.objects.get({
248
+ [ModelClass.primaryKeyField]: this.pk,
249
+ });
244
250
  // clear the current data and fresh data will flow
245
251
  this._data = {};
246
252
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statezero/core",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "type": "module",
5
5
  "module": "ESNext",
6
6
  "description": "The type-safe frontend client for StateZero - connect directly to your backend models with zero boilerplate",