@onyx.dev/onyx-database 0.3.0 → 1.0.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/index.cjs CHANGED
@@ -833,6 +833,56 @@ var QueryResults = class extends Array {
833
833
  }
834
834
  };
835
835
 
836
+ // src/helpers/condition-normalizer.ts
837
+ function isQueryBuilderLike(value) {
838
+ return !!value && typeof value.toSerializableQueryObject === "function";
839
+ }
840
+ function normalizeCriteriaValue(value) {
841
+ if (Array.isArray(value)) {
842
+ let changed = false;
843
+ const normalized = value.map((item) => {
844
+ const result = normalizeCriteriaValue(item);
845
+ if (result.changed) changed = true;
846
+ return result.value;
847
+ });
848
+ if (!changed) {
849
+ for (let i = 0; i < normalized.length; i += 1) {
850
+ if (normalized[i] !== value[i]) {
851
+ changed = true;
852
+ break;
853
+ }
854
+ }
855
+ }
856
+ return { value: changed ? normalized : value, changed };
857
+ }
858
+ if (isQueryBuilderLike(value)) {
859
+ return { value: value.toSerializableQueryObject(), changed: true };
860
+ }
861
+ return { value, changed: false };
862
+ }
863
+ function normalizeConditionInternal(condition) {
864
+ if (condition.conditionType === "SingleCondition") {
865
+ const { value, changed: changed2 } = normalizeCriteriaValue(condition.criteria.value);
866
+ if (!changed2) return condition;
867
+ return {
868
+ ...condition,
869
+ criteria: { ...condition.criteria, value }
870
+ };
871
+ }
872
+ let changed = false;
873
+ const normalizedConditions = condition.conditions.map((child) => {
874
+ const normalized = normalizeConditionInternal(child);
875
+ if (normalized !== child) changed = true;
876
+ return normalized;
877
+ });
878
+ if (!changed) return condition;
879
+ return { ...condition, conditions: normalizedConditions };
880
+ }
881
+ function normalizeCondition(condition) {
882
+ if (!condition) return condition;
883
+ return normalizeConditionInternal(condition);
884
+ }
885
+
836
886
  // src/builders/cascade-relationship-builder.ts
837
887
  var CascadeRelationshipBuilder = class {
838
888
  graphName;
@@ -960,6 +1010,10 @@ function serializeDates(value) {
960
1010
  }
961
1011
  return value;
962
1012
  }
1013
+ function stripEntityText(input) {
1014
+ const { entityText, ...rest } = input;
1015
+ return rest;
1016
+ }
963
1017
  function normalizeSecretMetadata(input) {
964
1018
  return { ...input, updatedAt: new Date(input.updatedAt) };
965
1019
  }
@@ -973,7 +1027,7 @@ function normalizeDate(value) {
973
1027
  return Number.isNaN(ts.getTime()) ? void 0 : ts;
974
1028
  }
975
1029
  function normalizeSchemaRevision(input, fallbackDatabaseId) {
976
- const { meta, createdAt, publishedAt, revisionId, ...rest } = input;
1030
+ const { meta, createdAt, publishedAt, revisionId, entityText, ...rest } = input;
977
1031
  const mergedMeta = {
978
1032
  revisionId: meta?.revisionId ?? revisionId,
979
1033
  createdAt: normalizeDate(meta?.createdAt ?? createdAt),
@@ -1144,15 +1198,23 @@ var OnyxDatabaseImpl = class {
1144
1198
  const params = new URLSearchParams();
1145
1199
  if (options?.publish) params.append("publish", "true");
1146
1200
  const path = `/schemas/${encodeURIComponent(databaseId)}${params.size ? `?${params.toString()}` : ""}`;
1147
- const body = { ...schema, databaseId: schema.databaseId ?? databaseId };
1148
- const res = await http.request("PUT", path, serializeDates(body));
1201
+ const body = stripEntityText({ ...schema, databaseId: schema.databaseId ?? databaseId });
1202
+ const res = await http.request(
1203
+ "PUT",
1204
+ path,
1205
+ serializeDates(body)
1206
+ );
1149
1207
  return normalizeSchemaRevision(res, databaseId);
1150
1208
  }
1151
1209
  async validateSchema(schema) {
1152
1210
  const { http, databaseId } = await this.ensureClient();
1153
1211
  const path = `/schemas/${encodeURIComponent(databaseId)}/validate`;
1154
- const body = { ...schema, databaseId: schema.databaseId ?? databaseId };
1155
- const res = await http.request("POST", path, serializeDates(body));
1212
+ const body = stripEntityText({ ...schema, databaseId: schema.databaseId ?? databaseId });
1213
+ const res = await http.request(
1214
+ "POST",
1215
+ path,
1216
+ serializeDates(body)
1217
+ );
1156
1218
  const normalizedSchema = res.schema ? normalizeSchemaRevision(res.schema, databaseId) : void 0;
1157
1219
  return {
1158
1220
  ...res,
@@ -1314,11 +1376,14 @@ var QueryBuilderImpl = class {
1314
1376
  if (!this.table) throw new Error("Table is not defined. Call from(<table>) first.");
1315
1377
  return this.table;
1316
1378
  }
1379
+ serializableConditions() {
1380
+ return normalizeCondition(this.conditions);
1381
+ }
1317
1382
  toSelectQuery() {
1318
1383
  return {
1319
1384
  type: "SelectQuery",
1320
1385
  fields: this.fields,
1321
- conditions: this.conditions,
1386
+ conditions: this.serializableConditions(),
1322
1387
  sort: this.sort,
1323
1388
  limit: this.limitValue,
1324
1389
  distinct: this.distinctValue,
@@ -1327,6 +1392,21 @@ var QueryBuilderImpl = class {
1327
1392
  resolvers: this.resolvers
1328
1393
  };
1329
1394
  }
1395
+ toUpdateQuery() {
1396
+ return {
1397
+ type: "UpdateQuery",
1398
+ conditions: this.serializableConditions(),
1399
+ updates: this.updates ?? {},
1400
+ sort: this.sort,
1401
+ limit: this.limitValue,
1402
+ partition: this.partitionValue ?? null
1403
+ };
1404
+ }
1405
+ toSerializableQueryObject() {
1406
+ const table = this.ensureTable();
1407
+ const payload = this.mode === "update" ? this.toUpdateQuery() : this.toSelectQuery();
1408
+ return { ...payload, table };
1409
+ }
1330
1410
  from(table) {
1331
1411
  this.table = table;
1332
1412
  return this;
@@ -1462,14 +1542,7 @@ var QueryBuilderImpl = class {
1462
1542
  async update() {
1463
1543
  if (this.mode !== "update") throw new Error("Call setUpdates(...) before update().");
1464
1544
  const table = this.ensureTable();
1465
- const update = {
1466
- type: "UpdateQuery",
1467
- conditions: this.conditions,
1468
- updates: this.updates ?? {},
1469
- sort: this.sort,
1470
- limit: this.limitValue,
1471
- partition: this.partitionValue ?? null
1472
- };
1545
+ const update = this.toUpdateQuery();
1473
1546
  return this.db._update(table, update, this.partitionValue);
1474
1547
  }
1475
1548
  onItemAdded(listener) {
@@ -1682,12 +1755,20 @@ var ConditionBuilderImpl = class {
1682
1755
  var c = (field, operator, value) => new ConditionBuilderImpl({ field, operator, value });
1683
1756
  var eq = (field, value) => c(field, "EQUAL", value);
1684
1757
  var neq = (field, value) => c(field, "NOT_EQUAL", value);
1685
- var inOp = (field, values) => c(
1686
- field,
1687
- "IN",
1688
- typeof values === "string" ? values.split(",").map((v) => v.trim()).filter((v) => v.length) : values
1689
- );
1690
- var notIn = (field, values) => c(field, "NOT_IN", values);
1758
+ function inOp(field, values) {
1759
+ const parsed = typeof values === "string" ? values.split(",").map((v) => v.trim()).filter((v) => v.length) : values;
1760
+ return c(field, "IN", parsed);
1761
+ }
1762
+ function within(field, values) {
1763
+ return inOp(field, values);
1764
+ }
1765
+ function notIn(field, values) {
1766
+ const parsed = typeof values === "string" ? values.split(",").map((v) => v.trim()).filter((v) => v.length) : values;
1767
+ return c(field, "NOT_IN", parsed);
1768
+ }
1769
+ function notWithin(field, values) {
1770
+ return notIn(field, values);
1771
+ }
1691
1772
  var between = (field, lower2, upper2) => c(field, "BETWEEN", [lower2, upper2]);
1692
1773
  var gt = (field, value) => c(field, "GREATER_THAN", value);
1693
1774
  var gte = (field, value) => c(field, "GREATER_THAN_EQUAL", value);
@@ -1754,6 +1835,7 @@ exports.notLike = notLike;
1754
1835
  exports.notMatches = notMatches;
1755
1836
  exports.notNull = notNull;
1756
1837
  exports.notStartsWith = notStartsWith;
1838
+ exports.notWithin = notWithin;
1757
1839
  exports.onyx = onyx;
1758
1840
  exports.percentile = percentile;
1759
1841
  exports.replace = replace;
@@ -1765,5 +1847,6 @@ exports.substring = substring;
1765
1847
  exports.sum = sum;
1766
1848
  exports.upper = upper;
1767
1849
  exports.variance = variance;
1850
+ exports.within = within;
1768
1851
  //# sourceMappingURL=index.cjs.map
1769
1852
  //# sourceMappingURL=index.cjs.map