jsonbadger 0.5.0 → 0.6.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.
Files changed (123) hide show
  1. package/README.md +36 -18
  2. package/docs/api/connection.md +144 -0
  3. package/docs/api/delta-tracker.md +106 -0
  4. package/docs/api/document.md +77 -0
  5. package/docs/api/field-types.md +329 -0
  6. package/docs/api/index.md +35 -0
  7. package/docs/api/model.md +392 -0
  8. package/docs/api/query-builder.md +81 -0
  9. package/docs/api/schema.md +204 -0
  10. package/docs/architecture-flow.md +397 -0
  11. package/docs/examples.md +495 -218
  12. package/docs/jsonb-ops.md +171 -0
  13. package/docs/lifecycle/model-compilation.md +111 -0
  14. package/docs/lifecycle.md +146 -0
  15. package/docs/query-translation.md +11 -10
  16. package/package.json +10 -3
  17. package/src/connection/connect.js +12 -17
  18. package/src/connection/connection.js +128 -0
  19. package/src/connection/server-capabilities.js +60 -59
  20. package/src/constants/defaults.js +32 -19
  21. package/src/constants/{id-strategies.js → id-strategy.js} +28 -29
  22. package/src/constants/intake-mode.js +8 -0
  23. package/src/debug/debug-logger.js +17 -15
  24. package/src/errors/model-overwrite-error.js +25 -0
  25. package/src/errors/query-error.js +25 -23
  26. package/src/errors/validation-error.js +25 -23
  27. package/src/field-types/base-field-type.js +137 -140
  28. package/src/field-types/builtins/advanced.js +365 -365
  29. package/src/field-types/builtins/index.js +579 -585
  30. package/src/field-types/field-type-namespace.js +9 -0
  31. package/src/field-types/registry.js +149 -122
  32. package/src/index.js +26 -36
  33. package/src/migration/ensure-index.js +157 -154
  34. package/src/migration/ensure-schema.js +27 -15
  35. package/src/migration/ensure-table.js +44 -31
  36. package/src/migration/schema-indexes-resolver.js +8 -6
  37. package/src/model/document-instance.js +29 -540
  38. package/src/model/document.js +60 -0
  39. package/src/model/factory/constants.js +36 -0
  40. package/src/model/factory/index.js +58 -0
  41. package/src/model/model.js +875 -0
  42. package/src/model/operations/delete-one.js +39 -0
  43. package/src/model/operations/insert-one.js +35 -0
  44. package/src/model/operations/query-builder.js +132 -0
  45. package/src/model/operations/update-one.js +333 -0
  46. package/src/model/state.js +34 -0
  47. package/src/schema/field-definition-parser.js +213 -218
  48. package/src/schema/path-introspection.js +87 -82
  49. package/src/schema/schema-compiler.js +126 -212
  50. package/src/schema/schema.js +621 -138
  51. package/src/sql/index.js +17 -0
  52. package/src/sql/jsonb/ops.js +153 -0
  53. package/src/{query → sql/jsonb}/path-parser.js +54 -43
  54. package/src/sql/jsonb/read/elem-match.js +133 -0
  55. package/src/{query → sql/jsonb/read}/operators/contains.js +13 -7
  56. package/src/sql/jsonb/read/operators/elem-match.js +9 -0
  57. package/src/{query → sql/jsonb/read}/operators/has-all-keys.js +17 -11
  58. package/src/{query → sql/jsonb/read}/operators/has-any-keys.js +18 -11
  59. package/src/sql/jsonb/read/operators/has-key.js +12 -0
  60. package/src/{query → sql/jsonb/read}/operators/jsonpath-exists.js +22 -15
  61. package/src/{query → sql/jsonb/read}/operators/jsonpath-match.js +22 -15
  62. package/src/{query → sql/jsonb/read}/operators/size.js +23 -16
  63. package/src/sql/parameter-binder.js +18 -13
  64. package/src/sql/read/build-count-query.js +12 -0
  65. package/src/sql/read/build-find-query.js +25 -0
  66. package/src/sql/read/limit-skip.js +21 -0
  67. package/src/sql/read/sort.js +85 -0
  68. package/src/sql/read/where/base-fields.js +310 -0
  69. package/src/sql/read/where/casting.js +90 -0
  70. package/src/sql/read/where/context.js +79 -0
  71. package/src/sql/read/where/field-clause.js +58 -0
  72. package/src/sql/read/where/index.js +38 -0
  73. package/src/sql/read/where/operator-entries.js +29 -0
  74. package/src/{query → sql/read/where}/operators/all.js +16 -10
  75. package/src/sql/read/where/operators/eq.js +12 -0
  76. package/src/{query → sql/read/where}/operators/gt.js +23 -16
  77. package/src/{query → sql/read/where}/operators/gte.js +23 -16
  78. package/src/{query → sql/read/where}/operators/in.js +18 -12
  79. package/src/sql/read/where/operators/index.js +40 -0
  80. package/src/{query → sql/read/where}/operators/lt.js +23 -16
  81. package/src/{query → sql/read/where}/operators/lte.js +23 -16
  82. package/src/sql/read/where/operators/ne.js +12 -0
  83. package/src/{query → sql/read/where}/operators/nin.js +18 -12
  84. package/src/{query → sql/read/where}/operators/regex.js +14 -8
  85. package/src/sql/read/where/operators.js +126 -0
  86. package/src/sql/read/where/text-operators.js +83 -0
  87. package/src/sql/run.js +46 -0
  88. package/src/sql/write/build-delete-query.js +33 -0
  89. package/src/sql/write/build-insert-query.js +42 -0
  90. package/src/sql/write/build-update-query.js +65 -0
  91. package/src/utils/assert.js +34 -27
  92. package/src/utils/delta-tracker/.archive/1 tracker-redesign-codex-v2.md +250 -0
  93. package/src/utils/delta-tracker/.archive/1 tracker-redesign-gemini.md +101 -0
  94. package/src/utils/delta-tracker/.archive/2 evaluation by gemini.txt +65 -0
  95. package/src/utils/delta-tracker/.archive/2 evaluation by grok.txt +39 -0
  96. package/src/utils/delta-tracker/.archive/3 gemini evaluate grok.txt +37 -0
  97. package/src/utils/delta-tracker/.archive/3 grok evaluate gemini.txt +63 -0
  98. package/src/utils/delta-tracker/.archive/4 gemini veredict.txt +16 -0
  99. package/src/utils/delta-tracker/.archive/index.1.js +587 -0
  100. package/src/utils/delta-tracker/.archive/index.2.js +612 -0
  101. package/src/utils/delta-tracker/index.js +592 -0
  102. package/src/utils/dirty-tracker/inline.js +335 -0
  103. package/src/utils/dirty-tracker/instance.js +414 -0
  104. package/src/utils/dirty-tracker/static.js +343 -0
  105. package/src/utils/json-safe.js +13 -9
  106. package/src/utils/object-path.js +227 -33
  107. package/src/utils/object.js +408 -168
  108. package/src/utils/string.js +55 -0
  109. package/src/utils/value.js +169 -30
  110. package/docs/api.md +0 -152
  111. package/src/connection/disconnect.js +0 -16
  112. package/src/connection/pool-store.js +0 -46
  113. package/src/model/model-factory.js +0 -555
  114. package/src/query/limit-skip-compiler.js +0 -31
  115. package/src/query/operators/elem-match.js +0 -3
  116. package/src/query/operators/eq.js +0 -6
  117. package/src/query/operators/has-key.js +0 -6
  118. package/src/query/operators/index.js +0 -60
  119. package/src/query/operators/ne.js +0 -6
  120. package/src/query/query-builder.js +0 -93
  121. package/src/query/sort-compiler.js +0 -30
  122. package/src/query/where-compiler.js +0 -477
  123. package/src/sql/sql-runner.js +0 -31
@@ -1,140 +1,137 @@
1
- /*
2
- Assumptions and trade-offs:
3
- - Universal options are normalized here, while runtime-only behaviors are stored for later phases.
4
- - Validation/casting fails fast per path and defers aggregation to schema_compiler.
5
- */
6
- function BaseFieldType(path_value, options) {
7
- this.path = path_value;
8
- this.options = options || {};
9
- this.instance = 'Mixed';
10
- this.validators = [];
11
- this.regExp = null;
12
- this.enum_values = null;
13
- this.register_universal_validators();
14
- }
15
-
16
- BaseFieldType.prototype.register_universal_validators = function () {
17
- if(this.options.required !== undefined) {
18
- this.validators.push({
19
- kind: 'required'
20
- });
21
- }
22
-
23
- if(typeof this.options.validate === 'function') {
24
- this.validators.push({
25
- kind: 'custom'
26
- });
27
- }
28
- };
29
-
30
- BaseFieldType.prototype.create_field_error = function (code_value, message, value) {
31
- const field_error = new Error(message);
32
- field_error.code = code_value;
33
- field_error.path = this.path;
34
- field_error.value = value;
35
- return field_error;
36
- };
37
-
38
- BaseFieldType.prototype.is_required = function (context_value) {
39
- const required_option = this.options.required;
40
-
41
- if(typeof required_option === 'function') {
42
- return Boolean(required_option.call(null, context_value || {}));
43
- }
44
-
45
- return required_option === true;
46
- };
47
-
48
- BaseFieldType.prototype.resolve_default = function (context_value) {
49
- if(this.options.default === undefined) {
50
- return undefined;
51
- }
52
-
53
- if(typeof this.options.default === 'function') {
54
- return this.options.default.call(null, context_value || {});
55
- }
56
-
57
- return this.options.default;
58
- };
59
-
60
- BaseFieldType.prototype.apply_set = function (value, context_value) {
61
- if(typeof this.options.set !== 'function') {
62
- return value;
63
- }
64
-
65
- return this.options.set.call(null, value, context_value || {});
66
- };
67
-
68
- BaseFieldType.prototype.apply_get = function (value, context_value) {
69
- if(typeof this.options.get !== 'function') {
70
- return value;
71
- }
72
-
73
- return this.options.get.call(null, value, context_value || {});
74
- };
75
-
76
- BaseFieldType.prototype.cast = function (value) {
77
- return value;
78
- };
79
-
80
- BaseFieldType.prototype.run_custom_validator = function (value, context_value) {
81
- if(typeof this.options.validate !== 'function') {
82
- return;
83
- }
84
-
85
- let validator_result = false;
86
-
87
- try {
88
- validator_result = this.options.validate.call(null, value, context_value || {});
89
- } catch(error) {
90
- const message_value = error && error.message ? error.message : 'Custom validator failed for path "' + this.path + '"';
91
- throw this.create_field_error('validator_error', message_value, value);
92
- }
93
-
94
- if(!validator_result) {
95
- throw this.create_field_error('validator_error', 'Custom validator failed for path "' + this.path + '"', value);
96
- }
97
- };
98
-
99
- BaseFieldType.prototype.run_type_validators = function () {
100
- return;
101
- };
102
-
103
- BaseFieldType.prototype.validate = function (value, context_value) {
104
- this.run_custom_validator(value, context_value || {});
105
- this.run_type_validators(value, context_value || {});
106
- };
107
-
108
- BaseFieldType.prototype.normalize = function (value, context_value) {
109
- const normalized_context = context_value || {};
110
- let current_value = value;
111
-
112
- if(current_value === undefined) {
113
- current_value = this.resolve_default(normalized_context);
114
- }
115
-
116
- if(current_value === undefined || current_value === null) {
117
- if(this.is_required(normalized_context)) {
118
- throw this.create_field_error('required_error', 'Path "' + this.path + '" is required', current_value);
119
- }
120
-
121
- return current_value;
122
- }
123
-
124
- current_value = this.apply_set(current_value, normalized_context);
125
- current_value = this.cast(current_value, normalized_context);
126
- this.validate(current_value, normalized_context);
127
- return current_value;
128
- };
129
-
130
- BaseFieldType.prototype.to_introspection = function () {
131
- return {
132
- path: this.path,
133
- instance: this.instance,
134
- validators: this.validators,
135
- regExp: this.regExp || null,
136
- enum_values: this.enum_values || null
137
- };
138
- };
139
-
140
- export default BaseFieldType;
1
+ import {is_function} from '#src/utils/value.js';
2
+
3
+ function BaseFieldType(path_value, options) {
4
+ this.path = path_value;
5
+ this.options = options || {};
6
+ this.instance = 'Mixed';
7
+ this.validators = [];
8
+ this.regExp = null;
9
+ this.enum_values = null;
10
+ this.register_universal_validators();
11
+ }
12
+
13
+ BaseFieldType.prototype.register_universal_validators = function () {
14
+ if(this.options.required !== undefined) {
15
+ this.validators.push({
16
+ kind: 'required'
17
+ });
18
+ }
19
+
20
+ if(is_function(this.options.validate)) {
21
+ this.validators.push({
22
+ kind: 'custom'
23
+ });
24
+ }
25
+ };
26
+
27
+ BaseFieldType.prototype.create_field_error = function (code_value, message, value) {
28
+ const field_error = new Error(message);
29
+ field_error.code = code_value;
30
+ field_error.path = this.path;
31
+ field_error.value = value;
32
+ return field_error;
33
+ };
34
+
35
+ BaseFieldType.prototype.is_required = function (context_value) {
36
+ const required_option = this.options.required;
37
+
38
+ if(is_function(required_option)) {
39
+ return Boolean(required_option.call(null, context_value || {}));
40
+ }
41
+
42
+ return required_option === true;
43
+ };
44
+
45
+ BaseFieldType.prototype.resolve_default = function (context_value) {
46
+ if(this.options.default === undefined) {
47
+ return undefined;
48
+ }
49
+
50
+ if(is_function(this.options.default)) {
51
+ return this.options.default.call(null, context_value || {});
52
+ }
53
+
54
+ return this.options.default;
55
+ };
56
+
57
+ BaseFieldType.prototype.apply_set = function (value, context_value) {
58
+ if(!is_function(this.options.set)) {
59
+ return value;
60
+ }
61
+
62
+ return this.options.set.call(null, value, context_value || {});
63
+ };
64
+
65
+ BaseFieldType.prototype.apply_get = function (value, context_value) {
66
+ if(!is_function(this.options.get)) {
67
+ return value;
68
+ }
69
+
70
+ return this.options.get.call(null, value, context_value || {});
71
+ };
72
+
73
+ BaseFieldType.prototype.cast = function (value) {
74
+ return value;
75
+ };
76
+
77
+ BaseFieldType.prototype.run_custom_validator = function (value, context_value) {
78
+ if(!is_function(this.options.validate)) {
79
+ return;
80
+ }
81
+
82
+ let validator_result = false;
83
+
84
+ try {
85
+ validator_result = this.options.validate.call(null, value, context_value || {});
86
+ } catch(error) {
87
+ const message_value = error && error.message ? error.message : 'Custom validator failed for path "' + this.path + '"';
88
+ throw this.create_field_error('validator_error', message_value, value);
89
+ }
90
+
91
+ if(!validator_result) {
92
+ throw this.create_field_error('validator_error', 'Custom validator failed for path "' + this.path + '"', value);
93
+ }
94
+ };
95
+
96
+ BaseFieldType.prototype.run_type_validators = function () {
97
+ return;
98
+ };
99
+
100
+ BaseFieldType.prototype.validate = function (value, context_value) {
101
+ this.run_custom_validator(value, context_value || {});
102
+ this.run_type_validators(value, context_value || {});
103
+ };
104
+
105
+ BaseFieldType.prototype.normalize = function (value, context_value) {
106
+ const normalized_context = context_value || {};
107
+ let current_value = value;
108
+
109
+ if(current_value === undefined) {
110
+ current_value = this.resolve_default(normalized_context);
111
+ }
112
+
113
+ if(current_value === undefined || current_value === null) {
114
+ if(this.is_required(normalized_context)) {
115
+ throw this.create_field_error('required_error', 'Path "' + this.path + '" is required', current_value);
116
+ }
117
+
118
+ return current_value;
119
+ }
120
+
121
+ current_value = this.apply_set(current_value, normalized_context);
122
+ current_value = this.cast(current_value, normalized_context);
123
+ this.validate(current_value, normalized_context);
124
+ return current_value;
125
+ };
126
+
127
+ BaseFieldType.prototype.to_introspection = function () {
128
+ return {
129
+ path: this.path,
130
+ instance: this.instance,
131
+ validators: this.validators,
132
+ regExp: this.regExp || null,
133
+ enum_values: this.enum_values || null
134
+ };
135
+ };
136
+
137
+ export default BaseFieldType;