@eidos.space/eidos-file 2.0.5 → 2.2.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.
@@ -2737,6 +2737,25 @@ const FILTER_OPERATORS = new Set([
2737
2737
  "is-between",
2738
2738
  "is-relative-to-today"
2739
2739
  ]);
2740
+ const RUNTIME_FILTER_OPERATORS$1 = new Set([
2741
+ "is-null",
2742
+ "is-not-null",
2743
+ "eq",
2744
+ "ne",
2745
+ "lt",
2746
+ "lte",
2747
+ "gt",
2748
+ "gte",
2749
+ "between",
2750
+ "in",
2751
+ "contains",
2752
+ "starts-with",
2753
+ "ends-with",
2754
+ "has-any",
2755
+ "has-all",
2756
+ "relation-has",
2757
+ "relative-date"
2758
+ ]);
2740
2759
  const LEVELS = {
2741
2760
  identity: 0,
2742
2761
  structural: 1,
@@ -3095,7 +3114,8 @@ function validateEidosFile(connection, options = {}) {
3095
3114
  continue;
3096
3115
  }
3097
3116
  const sql = (trigger.sql ?? "").toLowerCase();
3098
- if (trigger.tbl_name !== expected.table || sql.includes("hex(") || expected.fragments.some((fragment) => !sql.includes(fragment))) add(errors, "invalid-schema", `Portable trigger ${name} has an invalid definition`);
3117
+ const validFragments = expected.fragments.every((fragment) => fragment === "eidos_invalid_relation_value" ? sql.includes("eidos_invalid_relation_value") || sql.includes("eidos_relation_invalid") : sql.includes(fragment));
3118
+ if (trigger.tbl_name !== expected.table || sql.includes("hex(") || !validFragments) add(errors, "invalid-schema", `Portable trigger ${name} has an invalid definition`);
3099
3119
  }
3100
3120
  for (const name of triggerObjects.keys()) if (DYNAMIC_TRIGGER_NAME.test(name) && !expectedTriggers.has(name)) add(errors, "invalid-schema", `Undeclared or stale portable trigger: ${name}`);
3101
3121
  if (LEVELS[level] === LEVELS.structural) return {
@@ -3258,17 +3278,128 @@ function validateEidosFile(connection, options = {}) {
3258
3278
  if (!query || Array.isArray(query) || typeof query !== "object") throw new Error("View query_json must be an object");
3259
3279
  if (!layout || Array.isArray(layout) || typeof layout !== "object") throw new Error("View layout_json must be an object");
3260
3280
  let unsupportedQuery = Object.keys(query).some((key) => key !== "filter" && key !== "sort");
3281
+ const hasOnlyKeys = (value, keys) => Object.keys(value).every((key) => keys.includes(key));
3261
3282
  const checkFilter = (value) => {
3262
3283
  if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("View filter nodes must be objects");
3263
3284
  const node = value;
3264
- if (Array.isArray(node.args)) {
3265
- if (typeof node.op !== "string") throw new Error("View filter group has an invalid op");
3266
- if (node.op !== "and" && node.op !== "or" || Object.keys(node).some((key) => key !== "op" && key !== "args")) unsupportedQuery = true;
3285
+ if (node.op === "and" || node.op === "or") {
3286
+ if (!Array.isArray(node.args)) throw new Error("View filter group must contain args");
3287
+ if (!hasOnlyKeys(node, ["op", "args"])) unsupportedQuery = true;
3267
3288
  node.args.forEach(checkFilter);
3268
3289
  return;
3269
3290
  }
3270
- if (typeof node.field !== "string" || !fieldIds.has(node.field)) throw new Error("View filter references an unknown Field ID");
3291
+ if (node.op === "not") {
3292
+ if (!("arg" in node)) throw new Error("View filter negation must contain an arg");
3293
+ if (!hasOnlyKeys(node, ["op", "arg"])) unsupportedQuery = true;
3294
+ checkFilter(node.arg);
3295
+ return;
3296
+ }
3271
3297
  if (typeof node.op !== "string") throw new Error("View filter rule has an invalid operator");
3298
+ if ("fieldId" in node) {
3299
+ if (typeof node.fieldId !== "string" || !fieldIds.has(node.fieldId)) throw new Error("View filter references an unknown Field ID");
3300
+ if (!RUNTIME_FILTER_OPERATORS$1.has(node.op)) {
3301
+ unsupportedQuery = true;
3302
+ return;
3303
+ }
3304
+ const field = fieldsById.get(node.fieldId);
3305
+ if ((field.storageCodec === "json_array" || field.storageCodec === "relation") && [
3306
+ "lt",
3307
+ "lte",
3308
+ "gt",
3309
+ "gte",
3310
+ "starts-with",
3311
+ "ends-with"
3312
+ ].includes(node.op)) throw new Error("View filter uses a scalar comparison on a list Field");
3313
+ if (node.op === "is-null" || node.op === "is-not-null") {
3314
+ if (!hasOnlyKeys(node, ["op", "fieldId"])) unsupportedQuery = true;
3315
+ return;
3316
+ }
3317
+ if ([
3318
+ "eq",
3319
+ "ne",
3320
+ "lt",
3321
+ "lte",
3322
+ "gt",
3323
+ "gte"
3324
+ ].includes(node.op)) {
3325
+ if (!("value" in node) || node.value === null) throw new Error("View filter operand must be non-null");
3326
+ if (!hasOnlyKeys(node, [
3327
+ "op",
3328
+ "fieldId",
3329
+ "value"
3330
+ ])) unsupportedQuery = true;
3331
+ if (field.type === "relation" && (!Array.isArray(node.value) || !node.value.every(isEidosFileUuid))) throw new Error("View Relation filter values must be canonical Row IDs");
3332
+ return;
3333
+ }
3334
+ if (node.op === "between") {
3335
+ if (!("lower" in node) || !("upper" in node)) throw new Error("View between filter is missing an operand");
3336
+ if (node.lower === null || node.upper === null) throw new Error("View between operands must be non-null");
3337
+ if (!hasOnlyKeys(node, [
3338
+ "op",
3339
+ "fieldId",
3340
+ "lower",
3341
+ "upper"
3342
+ ])) unsupportedQuery = true;
3343
+ return;
3344
+ }
3345
+ if ([
3346
+ "in",
3347
+ "has-any",
3348
+ "has-all"
3349
+ ].includes(node.op)) {
3350
+ if (!Array.isArray(node.values) || node.values.some((entry) => entry === null)) throw new Error("View membership filter values must be a non-null array");
3351
+ if (!hasOnlyKeys(node, [
3352
+ "op",
3353
+ "fieldId",
3354
+ "values"
3355
+ ])) unsupportedQuery = true;
3356
+ if (field.type === "relation" && !(node.op === "in" ? node.values.every((value) => Array.isArray(value) && value.every(isEidosFileUuid)) : node.values.every(isEidosFileUuid))) throw new Error("View Relation filter values must be canonical Row IDs");
3357
+ return;
3358
+ }
3359
+ if ([
3360
+ "contains",
3361
+ "starts-with",
3362
+ "ends-with"
3363
+ ].includes(node.op)) {
3364
+ if (typeof node.value !== "string") throw new Error("View text filter value must be a string");
3365
+ if (!hasOnlyKeys(node, [
3366
+ "op",
3367
+ "fieldId",
3368
+ "value"
3369
+ ])) unsupportedQuery = true;
3370
+ return;
3371
+ }
3372
+ if (node.op === "relation-has") {
3373
+ if (field.type !== "relation" || !isEidosFileUuid(node.rowId)) throw new Error("View Relation filter must reference a canonical Row ID");
3374
+ if (!hasOnlyKeys(node, [
3375
+ "op",
3376
+ "fieldId",
3377
+ "rowId"
3378
+ ])) unsupportedQuery = true;
3379
+ return;
3380
+ }
3381
+ if (node.op === "relative-date") {
3382
+ if (field.type !== "date" && field.type !== "datetime" && field.type !== "created-time" && field.type !== "last-edited-time") throw new Error("View relative filter requires a Date or Datetime Field");
3383
+ if (![
3384
+ "past",
3385
+ "next",
3386
+ "this"
3387
+ ].includes(String(node.direction)) || ![
3388
+ "day",
3389
+ "week",
3390
+ "month",
3391
+ "year"
3392
+ ].includes(String(node.unit))) throw new Error("View relative filter value is invalid");
3393
+ if (!hasOnlyKeys(node, [
3394
+ "op",
3395
+ "fieldId",
3396
+ "direction",
3397
+ "unit"
3398
+ ])) unsupportedQuery = true;
3399
+ return;
3400
+ }
3401
+ }
3402
+ if (typeof node.field !== "string" || !fieldIds.has(node.field)) throw new Error("View filter references an unknown Field ID");
3272
3403
  if (!FILTER_OPERATORS.has(node.op)) {
3273
3404
  unsupportedQuery = true;
3274
3405
  return;
@@ -3311,8 +3442,9 @@ function validateEidosFile(connection, options = {}) {
3311
3442
  if ("sort" in query) {
3312
3443
  if (!Array.isArray(query.sort)) throw new Error("View sort must be an array");
3313
3444
  for (const item of query.sort) {
3314
- if (!item || Array.isArray(item) || typeof item !== "object" || typeof item.field !== "string" || !fieldIds.has(item.field)) throw new Error("View sort contains an invalid Field reference or direction");
3315
- if (item.direction !== "asc" && item.direction !== "desc" || item.nulls !== void 0 && item.nulls !== "first" && item.nulls !== "last" || Object.keys(item).some((key) => key !== "field" && key !== "direction" && key !== "nulls")) unsupportedQuery = true;
3445
+ const fieldId = item && !Array.isArray(item) && typeof item === "object" ? typeof item.fieldId === "string" ? item.fieldId : item.field : void 0;
3446
+ if (!item || Array.isArray(item) || typeof item !== "object" || typeof fieldId !== "string" || !fieldIds.has(fieldId)) throw new Error("View sort contains an invalid Field reference or direction");
3447
+ if (item.direction !== "asc" && item.direction !== "desc" || item.nulls !== void 0 && item.nulls !== "first" && item.nulls !== "last" || Object.keys(item).some((key) => key !== "field" && key !== "fieldId" && key !== "direction" && key !== "nulls")) unsupportedQuery = true;
3316
3448
  }
3317
3449
  }
3318
3450
  if (unsupportedQuery) add(warnings, "view-query-unsupported", `View ${view.id} uses query semantics from a newer Eidos version`);
@@ -3528,13 +3660,14 @@ function presentationSettingsObject(source) {
3528
3660
  ]) delete property[key];
3529
3661
  return property;
3530
3662
  }
3663
+ function assertedRawSelectOptions(property) {
3664
+ assertEidosFileSelectOptions(property);
3665
+ return Array.isArray(property?.options) ? property.options.map((option) => ({ ...option })) : [];
3666
+ }
3531
3667
  function presentationSettings(input) {
3532
3668
  const property = presentationSettingsObject("property" in input ? input.property : void 0);
3533
3669
  if (input.type === "select" || input.type === "multi-select") {
3534
- property.options = assertEidosFileSelectOptions("property" in input ? input.property : void 0).map((option) => ({
3535
- color: option.color,
3536
- name: option.name
3537
- }));
3670
+ property.options = assertedRawSelectOptions("property" in input ? input.property : void 0);
3538
3671
  if (input.type === "select") {
3539
3672
  const defaultOption = assertEidosFileSelectDefaultOption(property);
3540
3673
  if (defaultOption === null) delete property.defaultOption;
@@ -3547,8 +3680,9 @@ function transformOptionValue(value, fieldId, changes) {
3547
3680
  if (Array.isArray(value)) return value.map((entry) => transformOptionValue(entry, fieldId, changes));
3548
3681
  if (!value || typeof value !== "object") return value;
3549
3682
  const object = value;
3683
+ const referencesField = object.fieldId === fieldId || object.field === fieldId;
3550
3684
  const next = {};
3551
- for (const [key, entry] of Object.entries(object)) if (object.field === fieldId && key === "value") next[key] = Array.isArray(entry) ? entry.map((item) => typeof item === "string" ? changes.get(item) ?? item : item) : typeof entry === "string" ? changes.get(entry) ?? entry : entry;
3685
+ for (const [key, entry] of Object.entries(object)) if (referencesField && (key === "value" || key === "values" || key === "lower" || key === "upper")) next[key] = Array.isArray(entry) ? entry.map((item) => typeof item === "string" ? changes.get(item) ?? item : item) : typeof entry === "string" ? changes.get(entry) ?? entry : entry;
3552
3686
  else next[key] = transformOptionValue(entry, fieldId, changes);
3553
3687
  return next;
3554
3688
  }
@@ -3694,27 +3828,165 @@ function filterToStorage(filter, fields) {
3694
3828
  if (!normalized) return null;
3695
3829
  const byKey = /* @__PURE__ */ new Map();
3696
3830
  for (const field of fields) if (field.id) {
3697
- byKey.set(field.id, field.id);
3698
- byKey.set(field.tableColumnName, field.id);
3699
- byKey.set(field.name, field.id);
3831
+ byKey.set(field.id, field);
3832
+ byKey.set(field.tableColumnName, field);
3833
+ byKey.set(field.name, field);
3700
3834
  }
3701
- const convert = (group) => {
3702
- const args = [];
3703
- for (const child of group.children) if (child.type === "group") args.push(convert(child));
3704
- else {
3705
- const field = byKey.get(child.field);
3706
- if (field) args.push({
3707
- field,
3708
- op: child.operator,
3709
- ..."value" in child ? { value: child.value } : {}
3710
- });
3835
+ const convertRule = (rule) => {
3836
+ const field = byKey.get(rule.field);
3837
+ if (!field?.id) return null;
3838
+ const fieldId = field.id;
3839
+ const list = field.storageCodec === "json_array" || field.storageCodec === "relation";
3840
+ const values = Array.isArray(rule.value) ? rule.value : [rule.value ?? null];
3841
+ const scalar = values[0] ?? null;
3842
+ const equalityValue = list && Array.isArray(rule.value) ? rule.value : scalar;
3843
+ const requireValue = () => {
3844
+ if (scalar === null) throw new EidosFileError("invalid-query", `Saved View filter ${rule.operator} requires an operand`);
3845
+ return scalar;
3846
+ };
3847
+ const requireValues = () => {
3848
+ if (values.some((value) => value === null)) throw new EidosFileError("invalid-query", `Saved View filter ${rule.operator} requires non-null operands`);
3849
+ return values;
3850
+ };
3851
+ const membership = () => list ? {
3852
+ fieldId,
3853
+ op: "has-any",
3854
+ values: requireValues()
3855
+ } : {
3856
+ fieldId,
3857
+ op: "in",
3858
+ values: requireValues()
3859
+ };
3860
+ if (rule.operator === "is-relative-to-today") {
3861
+ const relative = rule.value;
3862
+ if (!relative || typeof relative !== "object" || Array.isArray(relative) || !("direction" in relative) || !("unit" in relative) || ![
3863
+ "past",
3864
+ "next",
3865
+ "this"
3866
+ ].includes(String(relative.direction)) || ![
3867
+ "day",
3868
+ "week",
3869
+ "month",
3870
+ "year"
3871
+ ].includes(String(relative.unit))) throw new EidosFileError("invalid-query", "Saved View relative date filter is invalid");
3872
+ return {
3873
+ direction: relative.direction,
3874
+ fieldId,
3875
+ op: "relative-date",
3876
+ unit: relative.unit
3877
+ };
3711
3878
  }
3712
- return {
3713
- args,
3879
+ if (rule.operator === "is-between") {
3880
+ if (!Array.isArray(rule.value) || rule.value.length !== 2 || rule.value.some((value) => value === null)) throw new EidosFileError("invalid-query", "Saved View between filter is invalid");
3881
+ return {
3882
+ fieldId,
3883
+ lower: rule.value[0],
3884
+ op: "between",
3885
+ upper: rule.value[1]
3886
+ };
3887
+ }
3888
+ switch (rule.operator) {
3889
+ case "is-empty": return list ? {
3890
+ fieldId,
3891
+ op: "eq",
3892
+ value: []
3893
+ } : {
3894
+ fieldId,
3895
+ op: "is-null"
3896
+ };
3897
+ case "is-not-empty": return list ? {
3898
+ fieldId,
3899
+ op: "ne",
3900
+ value: []
3901
+ } : {
3902
+ fieldId,
3903
+ op: "is-not-null"
3904
+ };
3905
+ case "equals": return equalityValue === null ? {
3906
+ fieldId,
3907
+ op: "is-null"
3908
+ } : {
3909
+ fieldId,
3910
+ op: "eq",
3911
+ value: equalityValue
3912
+ };
3913
+ case "not-equals": return equalityValue === null ? {
3914
+ fieldId,
3915
+ op: "is-not-null"
3916
+ } : {
3917
+ fieldId,
3918
+ op: "ne",
3919
+ value: equalityValue
3920
+ };
3921
+ case "less-than": return {
3922
+ fieldId,
3923
+ op: "lt",
3924
+ value: requireValue()
3925
+ };
3926
+ case "less-than-or-equal": return {
3927
+ fieldId,
3928
+ op: "lte",
3929
+ value: requireValue()
3930
+ };
3931
+ case "greater-than": return {
3932
+ fieldId,
3933
+ op: "gt",
3934
+ value: requireValue()
3935
+ };
3936
+ case "greater-than-or-equal": return {
3937
+ fieldId,
3938
+ op: "gte",
3939
+ value: requireValue()
3940
+ };
3941
+ case "contains": return list ? membership() : {
3942
+ fieldId,
3943
+ op: "contains",
3944
+ value: String(requireValue())
3945
+ };
3946
+ case "not-contains": return {
3947
+ arg: list ? membership() : {
3948
+ fieldId,
3949
+ op: "contains",
3950
+ value: String(requireValue())
3951
+ },
3952
+ op: "not"
3953
+ };
3954
+ case "starts-with": return {
3955
+ fieldId,
3956
+ op: "starts-with",
3957
+ value: String(requireValue())
3958
+ };
3959
+ case "ends-with": return {
3960
+ fieldId,
3961
+ op: "ends-with",
3962
+ value: String(requireValue())
3963
+ };
3964
+ case "is-any-of": return membership();
3965
+ case "is-all-of": return {
3966
+ fieldId,
3967
+ op: "has-all",
3968
+ values: requireValues()
3969
+ };
3970
+ case "is-none-of": return {
3971
+ arg: membership(),
3972
+ op: "not"
3973
+ };
3974
+ }
3975
+ };
3976
+ const convertGroup = (group) => {
3977
+ const node = {
3978
+ args: group.children.flatMap((child) => {
3979
+ const converted = child.type === "group" ? convertGroup(child) : convertRule(child);
3980
+ return converted ? [converted] : [];
3981
+ }),
3714
3982
  op: group.conjunction
3715
3983
  };
3984
+ return group.negated ? {
3985
+ arg: node,
3986
+ op: "not"
3987
+ } : node;
3716
3988
  };
3717
- return convert(normalized);
3989
+ return convertGroup(normalized);
3718
3990
  }
3719
3991
  function filterFromStorage(value, fields) {
3720
3992
  if (!value || typeof value !== "object" || Array.isArray(value)) return null;
@@ -3745,7 +4017,133 @@ function filterFromStorage(value, fields) {
3745
4017
  children
3746
4018
  };
3747
4019
  };
3748
- return normalizeEidosFileFilter(convert(value));
4020
+ const runtimeGroup = (conjunction, children) => ({
4021
+ type: "group",
4022
+ conjunction,
4023
+ children
4024
+ });
4025
+ const convertRuntime = (input) => {
4026
+ if (!input || typeof input !== "object" || Array.isArray(input)) return null;
4027
+ const node = input;
4028
+ if ((node.op === "and" || node.op === "or") && Array.isArray(node.args)) return runtimeGroup(node.op, node.args.flatMap((child) => {
4029
+ const converted = convertRuntime(child);
4030
+ return converted ? [converted] : [];
4031
+ }));
4032
+ if (node.op === "not" && node.arg !== void 0) {
4033
+ const child = convertRuntime(node.arg);
4034
+ if (!child) return null;
4035
+ const nested = child.type === "group" ? child : runtimeGroup("and", [child]);
4036
+ return {
4037
+ ...nested,
4038
+ negated: nested.negated !== true
4039
+ };
4040
+ }
4041
+ if (typeof node.fieldId !== "string" || !ids.has(node.fieldId) || typeof node.op !== "string") return null;
4042
+ const field = node.fieldId;
4043
+ if (node.op === "between") return {
4044
+ type: "rule",
4045
+ field,
4046
+ operator: "is-between",
4047
+ value: [node.lower, node.upper]
4048
+ };
4049
+ if (node.op === "relative-date") return {
4050
+ type: "rule",
4051
+ field,
4052
+ operator: "is-relative-to-today",
4053
+ value: {
4054
+ direction: node.direction,
4055
+ unit: node.unit
4056
+ }
4057
+ };
4058
+ if (node.op === "has-all") return {
4059
+ type: "rule",
4060
+ field,
4061
+ operator: "is-all-of",
4062
+ value: node.values
4063
+ };
4064
+ if (node.op === "in") return runtimeGroup("or", node.values.map((entry) => ({
4065
+ type: "rule",
4066
+ field,
4067
+ operator: "equals",
4068
+ value: entry
4069
+ })));
4070
+ if (node.op === "is-null" || node.op === "is-not-null") return {
4071
+ type: "rule",
4072
+ field,
4073
+ operator: node.op === "is-null" ? "equals" : "not-equals",
4074
+ value: null
4075
+ };
4076
+ const operator = RUNTIME_TO_COMPATIBILITY_OPERATOR[node.op];
4077
+ if (!operator) return null;
4078
+ return {
4079
+ type: "rule",
4080
+ field,
4081
+ operator,
4082
+ ...node.op === "has-any" ? { value: node.values } : node.op === "relation-has" ? { value: [node.rowId] } : "value" in node ? { value: node.value } : {}
4083
+ };
4084
+ };
4085
+ const containsRuntimeField = (input) => {
4086
+ if (!input || typeof input !== "object") return false;
4087
+ if (Array.isArray(input)) return input.some(containsRuntimeField);
4088
+ const node = input;
4089
+ return typeof node.fieldId === "string" || Object.values(node).some(containsRuntimeField);
4090
+ };
4091
+ if (!containsRuntimeField(value)) return normalizeEidosFileFilter(convert(value));
4092
+ const converted = convertRuntime(value);
4093
+ return normalizeEidosFileFilter(converted?.type === "group" ? converted : converted ? runtimeGroup("and", [converted]) : null);
4094
+ }
4095
+ const RUNTIME_TO_COMPATIBILITY_OPERATOR = {
4096
+ eq: "equals",
4097
+ ne: "not-equals",
4098
+ lt: "less-than",
4099
+ lte: "less-than-or-equal",
4100
+ gt: "greater-than",
4101
+ gte: "greater-than-or-equal",
4102
+ contains: "contains",
4103
+ "starts-with": "starts-with",
4104
+ "ends-with": "ends-with",
4105
+ "has-any": "is-any-of",
4106
+ "relation-has": "is-any-of"
4107
+ };
4108
+ const RUNTIME_FILTER_OPERATORS = new Set([
4109
+ "is-null",
4110
+ "is-not-null",
4111
+ "eq",
4112
+ "ne",
4113
+ "lt",
4114
+ "lte",
4115
+ "gt",
4116
+ "gte",
4117
+ "between",
4118
+ "in",
4119
+ "contains",
4120
+ "starts-with",
4121
+ "ends-with",
4122
+ "has-any",
4123
+ "has-all",
4124
+ "relation-has",
4125
+ "relative-date"
4126
+ ]);
4127
+ function sortsFromStorage(value) {
4128
+ if (!Array.isArray(value)) return [];
4129
+ return value.flatMap((entry) => {
4130
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) return [];
4131
+ const sort = entry;
4132
+ const field = typeof sort.fieldId === "string" ? sort.fieldId : typeof sort.field === "string" ? sort.field : null;
4133
+ if (!field) return [];
4134
+ return [{
4135
+ field,
4136
+ direction: sort.direction === "desc" ? "desc" : "asc",
4137
+ nulls: sort.nulls === "first" ? "first" : "last"
4138
+ }];
4139
+ });
4140
+ }
4141
+ function sortsToStorage(sorts) {
4142
+ return sorts.map((sort) => ({
4143
+ direction: sort.direction,
4144
+ fieldId: sort.field,
4145
+ ...sort.nulls === "first" ? { nulls: "first" } : {}
4146
+ }));
3749
4147
  }
3750
4148
  function storedViewQueryStatus(query, fields) {
3751
4149
  const fieldIds = new Set(fields.flatMap((field) => field.id ? [field.id] : []));
@@ -3772,7 +4170,54 @@ function storedViewQueryStatus(query, fields) {
3772
4170
  const filterSupported = (value) => {
3773
4171
  if (!value || typeof value !== "object" || Array.isArray(value)) return false;
3774
4172
  const node = value;
3775
- if (Array.isArray(node.args)) return (node.op === "and" || node.op === "or") && Object.keys(node).every((key) => key === "op" || key === "args") && node.args.every(filterSupported);
4173
+ if ((node.op === "and" || node.op === "or") && Array.isArray(node.args)) return Object.keys(node).every((key) => key === "op" || key === "args") && node.args.every(filterSupported);
4174
+ if (node.op === "not") return Object.keys(node).every((key) => key === "op" || key === "arg") && filterSupported(node.arg);
4175
+ if (typeof node.fieldId === "string") {
4176
+ if (!fieldIds.has(node.fieldId) || typeof node.op !== "string" || !RUNTIME_FILTER_OPERATORS.has(node.op)) return false;
4177
+ const allowed = new Set(["fieldId", "op"]);
4178
+ if ([
4179
+ "eq",
4180
+ "ne",
4181
+ "lt",
4182
+ "lte",
4183
+ "gt",
4184
+ "gte",
4185
+ "contains",
4186
+ "starts-with",
4187
+ "ends-with"
4188
+ ].includes(node.op)) {
4189
+ allowed.add("value");
4190
+ if (!("value" in node) || node.value === null) return false;
4191
+ } else if ([
4192
+ "in",
4193
+ "has-any",
4194
+ "has-all"
4195
+ ].includes(node.op)) {
4196
+ allowed.add("values");
4197
+ if (!Array.isArray(node.values)) return false;
4198
+ } else if (node.op === "between") {
4199
+ allowed.add("lower");
4200
+ allowed.add("upper");
4201
+ if (!("lower" in node) || !("upper" in node) || node.lower === null || node.upper === null) return false;
4202
+ } else if (node.op === "relation-has") {
4203
+ allowed.add("rowId");
4204
+ if (typeof node.rowId !== "string") return false;
4205
+ } else if (node.op === "relative-date") {
4206
+ allowed.add("direction");
4207
+ allowed.add("unit");
4208
+ if (![
4209
+ "past",
4210
+ "next",
4211
+ "this"
4212
+ ].includes(String(node.direction)) || ![
4213
+ "day",
4214
+ "week",
4215
+ "month",
4216
+ "year"
4217
+ ].includes(String(node.unit))) return false;
4218
+ }
4219
+ return Object.keys(node).every((key) => allowed.has(key));
4220
+ }
3776
4221
  if (typeof node.field !== "string" || !fieldIds.has(node.field) || !isEidosFileFilterOperator(node.op) || Object.keys(node).some((key) => key !== "field" && key !== "op" && key !== "value")) return false;
3777
4222
  return supportedRuleValue(node.op, node.value);
3778
4223
  };
@@ -3782,7 +4227,9 @@ function storedViewQueryStatus(query, fields) {
3782
4227
  for (const value of query.sort) {
3783
4228
  if (!value || typeof value !== "object" || Array.isArray(value)) return "unsupported";
3784
4229
  const sort = value;
3785
- if (typeof sort.field !== "string" || !fieldIds.has(sort.field) || sort.direction !== "asc" && sort.direction !== "desc" || sort.nulls !== void 0 && sort.nulls !== "first" && sort.nulls !== "last" || Object.keys(sort).some((key) => key !== "field" && key !== "direction" && key !== "nulls")) return "unsupported";
4230
+ const field = typeof sort.fieldId === "string" ? sort.fieldId : typeof sort.field === "string" ? sort.field : null;
4231
+ const fieldKey = typeof sort.fieldId === "string" ? "fieldId" : "field";
4232
+ if (!field || !fieldIds.has(field) || sort.direction !== "asc" && sort.direction !== "desc" || sort.nulls !== void 0 && sort.nulls !== "first" && sort.nulls !== "last" || Object.keys(sort).some((key) => key !== fieldKey && key !== "direction" && key !== "nulls")) return "unsupported";
3786
4233
  }
3787
4234
  }
3788
4235
  return "supported";
@@ -4459,7 +4906,7 @@ var EidosFileRuntime = class {
4459
4906
  if (changes.optionValueChanges && changes.optionValueChanges.length > 0) {
4460
4907
  if (field.type !== "select" && field.type !== "multi-select") throw new EidosFileError("invalid-schema", "Only Select Fields have option values");
4461
4908
  const replacements = new Map(changes.optionValueChanges.map((change) => [change.from, change.to]));
4462
- const options = assertEidosFileSelectOptions(settings);
4909
+ const options = assertedRawSelectOptions(settings);
4463
4910
  const existingNames = new Set(options.map((option) => option.name));
4464
4911
  settings.options = options.flatMap((option) => {
4465
4912
  const replacement = replacements.get(option.name);
@@ -4473,10 +4920,7 @@ var EidosFileRuntime = class {
4473
4920
  if (field.type === "select" && typeof settings.defaultOption === "string") settings.defaultOption = replacements.get(settings.defaultOption) ?? settings.defaultOption;
4474
4921
  }
4475
4922
  if (field.type === "select" || field.type === "multi-select") {
4476
- settings.options = assertEidosFileSelectOptions(settings).map((option) => ({
4477
- color: option.color,
4478
- name: option.name
4479
- }));
4923
+ settings.options = assertedRawSelectOptions(settings);
4480
4924
  if (field.type === "select") {
4481
4925
  const defaultOption = assertEidosFileSelectDefaultOption(settings);
4482
4926
  if (defaultOption === null) delete settings.defaultOption;
@@ -4826,7 +5270,7 @@ var EidosFileRuntime = class {
4826
5270
  const query = jsonObject(row.query_json);
4827
5271
  const layout = jsonObject(row.layout_json);
4828
5272
  const queryStatus = storedViewQueryStatus(query, fields);
4829
- const storedSorts = normalizeEidosFileSorts(query.sort);
5273
+ const storedSorts = sortsFromStorage(query.sort);
4830
5274
  const fieldIds = new Set(fields.flatMap((field) => field.id ? [field.id] : []));
4831
5275
  const fieldOrder = Array.isArray(layout.fieldOrder) ? layout.fieldOrder.flatMap((value) => typeof value === "string" && fieldIds.has(value) ? [value] : []) : [];
4832
5276
  const properties = { ...layout };
@@ -4900,7 +5344,7 @@ var EidosFileRuntime = class {
4900
5344
  input.type,
4901
5345
  canonicalizeEidosFileJson({
4902
5346
  ...filterToStorage(input.filter, fields) ? { filter: filterToStorage(input.filter, fields) } : {},
4903
- ...sorts.length > 0 ? { sort: sorts } : {}
5347
+ ...sorts.length > 0 ? { sort: sortsToStorage(sorts) } : {}
4904
5348
  }),
4905
5349
  canonicalizeEidosFileJson({
4906
5350
  ...properties,
@@ -4916,7 +5360,9 @@ var EidosFileRuntime = class {
4916
5360
  now,
4917
5361
  now
4918
5362
  ]);
4919
- return this.mapView(this.viewRow(id));
5363
+ const view = this.mapView(this.viewRow(id));
5364
+ this.optimizeViewQueries();
5365
+ return view;
4920
5366
  });
4921
5367
  }
4922
5368
  updateView(viewId, changes) {
@@ -4967,14 +5413,16 @@ var EidosFileRuntime = class {
4967
5413
  next.type,
4968
5414
  queryChanged ? canonicalizeEidosFileJson({
4969
5415
  ...filterToStorage(next.filter, fields) ? { filter: filterToStorage(next.filter, fields) } : {},
4970
- ...sorts.length ? { sort: sorts } : {}
5416
+ ...sorts.length ? { sort: sortsToStorage(sorts) } : {}
4971
5417
  }) : currentRow.query_json,
4972
5418
  layoutChanged ? canonicalizeEidosFileJson(layout) : currentRow.layout_json,
4973
5419
  stablePosition(next.position, current.position ?? 0),
4974
5420
  this.operationInstant(),
4975
5421
  viewId
4976
5422
  ]);
4977
- return this.mapView(this.viewRow(viewId));
5423
+ const view = this.mapView(this.viewRow(viewId));
5424
+ this.optimizeViewQueries();
5425
+ return view;
4978
5426
  });
4979
5427
  }
4980
5428
  duplicateView(viewId, name) {
@@ -6015,7 +6463,29 @@ var EidosFileRuntime = class {
6015
6463
  }))
6016
6464
  };
6017
6465
  }
6018
- optimizeViewQueries() {}
6466
+ optimizeViewQueries() {
6467
+ const indexedFields = /* @__PURE__ */ new Set();
6468
+ for (const table of this.listTables()) {
6469
+ const fields = this.listFields(table.id);
6470
+ for (const view of this.listViews(table.id)) {
6471
+ if (view.type !== "calendar") continue;
6472
+ const dateField = view.properties?.dateField;
6473
+ if (typeof dateField !== "string") continue;
6474
+ const field = fields.find((candidate) => candidate.id === dateField || candidate.tableColumnName === dateField);
6475
+ if (!field?.id || !field.physicalName || field.isDerived || field.storageCodec !== "scalar" || ![
6476
+ "date",
6477
+ "datetime",
6478
+ "created-time",
6479
+ "last-edited-time"
6480
+ ].includes(field.type) || indexedFields.has(field.id)) continue;
6481
+ indexedFields.add(field.id);
6482
+ const indexName = `idx_eidos_calendar_${field.id.replace(/-/g, "")}`;
6483
+ this.connection.exec(`CREATE INDEX IF NOT EXISTS ${quoteIdentifier(indexName)}
6484
+ ON ${quoteIdentifier(table.physicalName ?? table.rawTableName)}
6485
+ (${quoteIdentifier(field.physicalName)} COLLATE BINARY)`);
6486
+ }
6487
+ }
6488
+ }
6019
6489
  schema(tableId) {
6020
6490
  return (tableId ? [this.getTable(tableId)] : this.listTables()).map((table) => ({
6021
6491
  table,
@@ -6191,4 +6661,4 @@ function fieldValueTypeForScan(field) {
6191
6661
  //#endregion
6192
6662
  export { EIDOS_FILE_REQUIRED_TABLES as $, eidosFileLookupElementType as A, eidosFileFormulaReferences as B, normalizeEidosFileFilter as C, eidosFileFieldStoresJsonArray as D, removeEidosFileFilterField as E, EIDOS_FILE_FORMULA_FIELD_FUNCTION_NAMES as F, EIDOS_FILE_FIELDS_TABLE as G, EIDOS_FILE_APPLICATION_ID as H, EIDOS_FILE_FORMULA_FUNCTION_NAMES as I, EIDOS_FILE_FORMULA_FIELDS_TABLE as J, EIDOS_FILE_FORMAT as K, compileEidosFileFormula as L, eidosFileLookupTargetDisplayType as M, eidosFileLookupTargetValueType as N, eidosFileLookupAggregateSupportsTarget as O, eidosFileLookupValueType as P, EIDOS_FILE_RELATION_FIELDS_TABLE as Q, compileEidosFileFormulaFields as R, isEidosFileFilterOperator as S, normalizeEidosFileSorts as T, EIDOS_FILE_EXTENSION as U, rewriteEidosFileFormulaFieldReferences as V, EIDOS_FILE_FEATURES_TABLE as W, EIDOS_FILE_META_TABLE as X, EIDOS_FILE_LOOKUP_FIELDS_TABLE as Y, EIDOS_FILE_MIME_TYPE as Z, assertEidosFileRowQuery as _, EIDOS_FILE_FORMAT_VERSION_TEXT as a, hasEidosFileSqliteHeader as at, eidosFileRowQueryPredicateColumns as b, incrementEidosFileRevision as c, ConnectionPortEidosFileConnection as ct, decodeEidosFileRelationDisplay as d, assertInt64Decimal as dt, EIDOS_FILE_SCHEMA_VERSION as et, decodeEidosFileRelationIds as f, assertUnicodeString as ft, parseNonNegativeInt64 as g, cancellationPortFromSignal as h, sqlValueToNative as ht, scanStableRowPages as i, SQLITE_HEADER as it, eidosFileLookupStorageCodec as j, eidosFileLookupDisplayType as k, initializeEidosFileSchema as l, EidosAdapterError as lt, MemoryByteSource as m, nativeToSqlValue as mt, validateEidosFile as n, EIDOS_FILE_TABLES_TABLE as nt, EIDOS_FILE_SCHEMA_SQL as o, eidosFileColumnStatLabel as ot, encodeEidosFileRelationIds as p, exactConnectionBindingCount as pt, EIDOS_FILE_FORMAT_VERSION as q, smallestDependencyCycle as r, EIDOS_FILE_VIEWS_TABLE as rt, configureEidosFileConnection as s, eidosFileColumnStatTypesForField as st, EidosFileRuntime as t, EIDOS_FILE_SYSTEM_COLUMNS as tt, setEidosFileMetadata as u, assertExactConnectionBindings as ut, compileEidosFileRowQuery as v, normalizeEidosFileRowQuery as w, eidosFileSortExpression as x, eidosFileRowQueryAffectedByFieldChanges as y, compileEidosFileFormulaSource as z };
6193
6663
 
6194
- //# sourceMappingURL=runtime-ApHTYmlB.mjs.map
6664
+ //# sourceMappingURL=runtime-D9Vp-lxn.mjs.map