@statezero/core 0.1.98 → 0.2.1

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.
@@ -80,6 +80,14 @@ export class QuerySet<T> {
80
80
  * @returns {Object} The serialized conditions.
81
81
  */
82
82
  private _serializeConditions;
83
+ /**
84
+ * Serializes a value based on its type (handles arrays, Model instances, Dates, primitives)
85
+ *
86
+ * @private
87
+ * @param {any} value - The value to serialize
88
+ * @returns {any} The serialized value
89
+ */
90
+ private _serializeValue;
83
91
  /**
84
92
  * Filters the QuerySet with the provided conditions.
85
93
  *
@@ -1,6 +1,6 @@
1
1
  import { MultipleObjectsReturned, DoesNotExist, parseStateZeroError, } from "./errors.js";
2
2
  import { Model } from "./model.js";
3
- import { ModelSerializer } from "./serializers.js"; // Import the ModelSerializer
3
+ import { ModelSerializer, relationshipFieldSerializer, dateFieldSerializer } from "./serializers.js";
4
4
  import axios from "axios";
5
5
  import { QueryExecutor } from "./queryExecutor.js";
6
6
  import { json } from "stream/consumers";
@@ -110,8 +110,57 @@ export class QuerySet {
110
110
  if (!conditions || typeof conditions !== "object") {
111
111
  return conditions;
112
112
  }
113
- // Use the model serializer to convert conditions to internal format
114
- return this._serializer.toInternal(conditions);
113
+ const serializedConditions = {};
114
+ for (const [fieldPath, value] of Object.entries(conditions)) {
115
+ serializedConditions[fieldPath] = this._serializeValue(value);
116
+ }
117
+ return serializedConditions;
118
+ }
119
+ /**
120
+ * Serializes a value based on its type (handles arrays, Model instances, Dates, primitives)
121
+ *
122
+ * @private
123
+ * @param {any} value - The value to serialize
124
+ * @returns {any} The serialized value
125
+ */
126
+ _serializeValue(value) {
127
+ // Handle arrays (for __in lookups)
128
+ if (Array.isArray(value)) {
129
+ return value.map(item => this._serializeValue(item));
130
+ }
131
+ // Handle Model instances (objects with pk, serialize method, and constructor with configKey/modelName)
132
+ // Note: Model instances are objects, not classes (typeof instance === 'object')
133
+ if (value &&
134
+ typeof value === 'object' &&
135
+ !(value instanceof Date) && // Exclude Date objects
136
+ 'pk' in value &&
137
+ 'serialize' in value &&
138
+ typeof value.serialize === 'function' &&
139
+ value.constructor &&
140
+ 'configKey' in value.constructor &&
141
+ 'modelName' in value.constructor) {
142
+ return relationshipFieldSerializer.toInternal(value);
143
+ }
144
+ // Handle Date objects
145
+ // Without field context, we default to datetime format (ISO string with time)
146
+ // Unless it's exactly midnight in UTC, which likely indicates a date-only field
147
+ if (value instanceof Date) {
148
+ // Check if it's midnight UTC (likely a date-only value)
149
+ const hours = value.getUTCHours();
150
+ const minutes = value.getUTCMinutes();
151
+ const seconds = value.getUTCSeconds();
152
+ const milliseconds = value.getUTCMilliseconds();
153
+ if (hours === 0 && minutes === 0 && seconds === 0 && milliseconds === 0) {
154
+ // It's midnight UTC - serialize as date-only (YYYY-MM-DD)
155
+ return value.toISOString().split('T')[0];
156
+ }
157
+ else {
158
+ // Has time component - serialize as full datetime
159
+ return value.toISOString();
160
+ }
161
+ }
162
+ // Everything else (strings, numbers, booleans, null) - return as-is
163
+ return value;
115
164
  }
116
165
  /**
117
166
  * Filters the QuerySet with the provided conditions.
@@ -38,7 +38,9 @@ class EventData {
38
38
  static fromOperation(operation) {
39
39
  const ModelClass = operation.queryset.ModelClass;
40
40
  const pkField = ModelClass.primaryKeyField;
41
- return operation.instances.map(instance => new EventData(ModelClass, instance[pkField]));
41
+ return operation.instances
42
+ .filter(instance => instance != null && typeof instance === 'object' && pkField in instance)
43
+ .map(instance => new EventData(ModelClass, instance[pkField]));
42
44
  }
43
45
  /**
44
46
  * One event per unique PK across multiple operations
@@ -51,7 +53,9 @@ class EventData {
51
53
  const uniquePks = new Set();
52
54
  for (const op of operations) {
53
55
  for (const inst of op.instances) {
54
- uniquePks.add(inst[pkField]);
56
+ if (inst != null && typeof inst === 'object' && pkField in inst) {
57
+ uniquePks.add(inst[pkField]);
58
+ }
55
59
  }
56
60
  }
57
61
  return Array.from(uniquePks).map(pk => new EventData(ModelClass, pk));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@statezero/core",
3
- "version": "0.1.98",
3
+ "version": "0.2.1",
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",