@spine-event-engine/storage 2.0.0-snapshot.2

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 (133) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +160 -0
  3. package/REFERENCE.md +120 -0
  4. package/dist/entity/entity-history-record-spec.d.ts +26 -0
  5. package/dist/entity/entity-history-record-spec.d.ts.map +1 -0
  6. package/dist/entity/entity-history-record-spec.js +77 -0
  7. package/dist/entity/entity-history-record-spec.js.map +1 -0
  8. package/dist/entity/entity-history-storage.d.ts +111 -0
  9. package/dist/entity/entity-history-storage.d.ts.map +1 -0
  10. package/dist/entity/entity-history-storage.js +40 -0
  11. package/dist/entity/entity-history-storage.js.map +1 -0
  12. package/dist/entity/entity-record.d.ts +31 -0
  13. package/dist/entity/entity-record.d.ts.map +1 -0
  14. package/dist/entity/entity-record.js +15 -0
  15. package/dist/entity/entity-record.js.map +1 -0
  16. package/dist/entity/history-conformance.d.ts +55 -0
  17. package/dist/entity/history-conformance.d.ts.map +1 -0
  18. package/dist/entity/history-conformance.js +165 -0
  19. package/dist/entity/history-conformance.js.map +1 -0
  20. package/dist/event/event-store.d.ts +141 -0
  21. package/dist/event/event-store.d.ts.map +1 -0
  22. package/dist/event/event-store.js +407 -0
  23. package/dist/event/event-store.js.map +1 -0
  24. package/dist/index.d.ts +23 -0
  25. package/dist/index.d.ts.map +1 -0
  26. package/dist/index.js +30 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/internal/delivery-cleanup.d.ts +99 -0
  29. package/dist/internal/delivery-cleanup.d.ts.map +1 -0
  30. package/dist/internal/delivery-cleanup.js +38 -0
  31. package/dist/internal/delivery-cleanup.js.map +1 -0
  32. package/dist/internal/entity-commit.d.ts +108 -0
  33. package/dist/internal/entity-commit.d.ts.map +1 -0
  34. package/dist/internal/entity-commit.js +32 -0
  35. package/dist/internal/entity-commit.js.map +1 -0
  36. package/dist/internal/entity-history.d.ts +12 -0
  37. package/dist/internal/entity-history.d.ts.map +1 -0
  38. package/dist/internal/entity-history.js +17 -0
  39. package/dist/internal/entity-history.js.map +1 -0
  40. package/dist/internal/event-store.d.ts +8 -0
  41. package/dist/internal/event-store.d.ts.map +1 -0
  42. package/dist/internal/event-store.js +21 -0
  43. package/dist/internal/event-store.js.map +1 -0
  44. package/dist/internal/query-values.d.ts +5 -0
  45. package/dist/internal/query-values.d.ts.map +1 -0
  46. package/dist/internal/query-values.js +18 -0
  47. package/dist/internal/query-values.js.map +1 -0
  48. package/dist/internal/tenancy.d.ts +89 -0
  49. package/dist/internal/tenancy.d.ts.map +1 -0
  50. package/dist/internal/tenancy.js +97 -0
  51. package/dist/internal/tenancy.js.map +1 -0
  52. package/dist/memory/canonical-utf8.d.ts +21 -0
  53. package/dist/memory/canonical-utf8.d.ts.map +1 -0
  54. package/dist/memory/canonical-utf8.js +65 -0
  55. package/dist/memory/canonical-utf8.js.map +1 -0
  56. package/dist/memory/in-memory-entity-commit.d.ts +35 -0
  57. package/dist/memory/in-memory-entity-commit.d.ts.map +1 -0
  58. package/dist/memory/in-memory-entity-commit.js +183 -0
  59. package/dist/memory/in-memory-entity-commit.js.map +1 -0
  60. package/dist/memory/in-memory-entity-history.d.ts +379 -0
  61. package/dist/memory/in-memory-entity-history.d.ts.map +1 -0
  62. package/dist/memory/in-memory-entity-history.js +756 -0
  63. package/dist/memory/in-memory-entity-history.js.map +1 -0
  64. package/dist/memory/in-memory-record-storage.d.ts +78 -0
  65. package/dist/memory/in-memory-record-storage.d.ts.map +1 -0
  66. package/dist/memory/in-memory-record-storage.js +127 -0
  67. package/dist/memory/in-memory-record-storage.js.map +1 -0
  68. package/dist/memory/in-memory-storage-backend.d.ts +38 -0
  69. package/dist/memory/in-memory-storage-backend.d.ts.map +1 -0
  70. package/dist/memory/in-memory-storage-backend.js +108 -0
  71. package/dist/memory/in-memory-storage-backend.js.map +1 -0
  72. package/dist/memory/in-memory-storage-factory.d.ts +58 -0
  73. package/dist/memory/in-memory-storage-factory.d.ts.map +1 -0
  74. package/dist/memory/in-memory-storage-factory.js +148 -0
  75. package/dist/memory/in-memory-storage-factory.js.map +1 -0
  76. package/dist/memory/memory-delivery-cleanup.d.ts +32 -0
  77. package/dist/memory/memory-delivery-cleanup.d.ts.map +1 -0
  78. package/dist/memory/memory-delivery-cleanup.js +61 -0
  79. package/dist/memory/memory-delivery-cleanup.js.map +1 -0
  80. package/dist/memory/tenant-records.d.ts +66 -0
  81. package/dist/memory/tenant-records.d.ts.map +1 -0
  82. package/dist/memory/tenant-records.js +573 -0
  83. package/dist/memory/tenant-records.js.map +1 -0
  84. package/dist/query/query-execution.d.ts +50 -0
  85. package/dist/query/query-execution.d.ts.map +1 -0
  86. package/dist/query/query-execution.js +222 -0
  87. package/dist/query/query-execution.js.map +1 -0
  88. package/dist/query/query-policy.d.ts +115 -0
  89. package/dist/query/query-policy.d.ts.map +1 -0
  90. package/dist/query/query-policy.js +294 -0
  91. package/dist/query/query-policy.js.map +1 -0
  92. package/dist/record/column-mapping.d.ts +41 -0
  93. package/dist/record/column-mapping.d.ts.map +1 -0
  94. package/dist/record/column-mapping.js +36 -0
  95. package/dist/record/column-mapping.js.map +1 -0
  96. package/dist/record/column-type.d.ts +75 -0
  97. package/dist/record/column-type.d.ts.map +1 -0
  98. package/dist/record/column-type.js +69 -0
  99. package/dist/record/column-type.js.map +1 -0
  100. package/dist/record/record-column.d.ts +23 -0
  101. package/dist/record/record-column.d.ts.map +1 -0
  102. package/dist/record/record-column.js +41 -0
  103. package/dist/record/record-column.js.map +1 -0
  104. package/dist/record/record-mask.d.ts +11 -0
  105. package/dist/record/record-mask.d.ts.map +1 -0
  106. package/dist/record/record-mask.js +82 -0
  107. package/dist/record/record-mask.js.map +1 -0
  108. package/dist/record/record-query.d.ts +102 -0
  109. package/dist/record/record-query.d.ts.map +1 -0
  110. package/dist/record/record-query.js +44 -0
  111. package/dist/record/record-query.js.map +1 -0
  112. package/dist/record/record-spec.d.ts +101 -0
  113. package/dist/record/record-spec.d.ts.map +1 -0
  114. package/dist/record/record-spec.js +159 -0
  115. package/dist/record/record-spec.js.map +1 -0
  116. package/dist/record/record-storage.d.ts +202 -0
  117. package/dist/record/record-storage.d.ts.map +1 -0
  118. package/dist/record/record-storage.js +234 -0
  119. package/dist/record/record-storage.js.map +1 -0
  120. package/dist/record/storage-group.d.ts +17 -0
  121. package/dist/record/storage-group.d.ts.map +1 -0
  122. package/dist/record/storage-group.js +37 -0
  123. package/dist/record/storage-group.js.map +1 -0
  124. package/dist/storage/storage-factory.d.ts +44 -0
  125. package/dist/storage/storage-factory.d.ts.map +1 -0
  126. package/dist/storage/storage-factory.js +54 -0
  127. package/dist/storage/storage-factory.js.map +1 -0
  128. package/dist/storage/storage.d.ts +62 -0
  129. package/dist/storage/storage.d.ts.map +1 -0
  130. package/dist/storage/storage.js +15 -0
  131. package/dist/storage/storage.js.map +1 -0
  132. package/dist/tsconfig.tsbuildinfo +1 -0
  133. package/package.json +56 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # Storage API and in-memory storage for Spine TS
2
+
3
+ `@spine-event-engine/storage` defines the storage API used by Spine TS and
4
+ includes an in-memory implementation for local development and tests. Use it
5
+ when an application needs a small record store, or when an adapter needs to
6
+ implement the same storage contract for a durable provider.
7
+
8
+ For detailed query, lifecycle, and adapter notes, see
9
+ [REFERENCE documentation for agents](REFERENCE.md).
10
+
11
+ ## 💡 Why use it?
12
+
13
+ - ✅ Gives every Spine repository one storage contract.
14
+ - ✅ Includes a fast in-memory implementation for development and tests.
15
+ - ✅ Supports typed IDs, declared columns, filtering, sorting, and limits.
16
+ - ✅ Lets applications select persistence without changing domain handlers.
17
+
18
+ ## 🚀 Build it in this workspace
19
+
20
+ ```sh
21
+ pnpm typecheck:build
22
+ ```
23
+
24
+ Run this workspace-wide TypeScript build from the repository root. For an
25
+ experimental npm consumer, install
26
+ `@spine-event-engine/storage@2.0.0-snapshot.2` or the explicit
27
+ `@spine-event-engine/storage@snapshot` tag.
28
+
29
+ ## 🧪 Start with the Proto fields people may query
30
+
31
+ Persistence begins in the Proto model. Mark only the fields that need to be
32
+ filtered or sorted with `(column)`. The complete message is always kept as
33
+ authoritative bytes; an unmarked field is not silently promoted to a provider
34
+ property.
35
+
36
+ ```proto
37
+ message TaskView {
38
+ TaskId id = 1;
39
+ UserId assignee = 2 [(column) = true];
40
+ string title = 3;
41
+ }
42
+ ```
43
+
44
+ Here `assignee` is a query column. `title` stays in the serialized record. The
45
+ generated record specification carries that declaration into the selected
46
+ storage adapter.
47
+
48
+ ## 🧪 Store a Protobuf record in memory
49
+
50
+ Create a `RecordSpec` to describe the record type, identity, and searchable
51
+ columns. Its source type defaults to the record type. Pass it to an
52
+ `InMemoryStorageFactory` with the storage context.
53
+
54
+ ```ts
55
+ import { create, ScalarType } from "@bufbuild/protobuf";
56
+ import { StringValueSchema, type StringValue } from "@bufbuild/protobuf/wkt";
57
+ import {
58
+ ColumnTypes,
59
+ InMemoryStorageFactory,
60
+ RecordColumn,
61
+ RecordSpec,
62
+ } from "@spine-event-engine/storage";
63
+
64
+ const records = new InMemoryStorageFactory().createRecordStorage(
65
+ { name: "Users", multitenant: false },
66
+ new RecordSpec<string, StringValue>({
67
+ recordType: StringValueSchema,
68
+ idKind: "string",
69
+ extractId: (user) => user.value,
70
+ columns: [
71
+ new RecordColumn("value", ColumnTypes.scalar(ScalarType.STRING), (user) => user.value),
72
+ ],
73
+ }),
74
+ );
75
+
76
+ await records.write(create(StringValueSchema, { value: "ava" }));
77
+ const user = await records.read("ava");
78
+ const users = await records.query({
79
+ filters: [{ column: "value", value: "ava" }],
80
+ sort: [{ field: "value", direction: "asc" }],
81
+ limit: 20,
82
+ });
83
+ ```
84
+
85
+ `InMemoryStorageFactory` creates a fresh backend by default. Pass the same
86
+ `InMemoryStorageBackend` to separate factories only when they intentionally
87
+ need to share rows. Query named columns, sort them, and limit the result; the
88
+ column names come from the declared `RecordSpec`/Proto mapping. `RecordQuery<I>`
89
+ types its IDs. Providers apply their documented runtime mapping and validation
90
+ to filter names and values: MySQL checks declared columns, while other adapters
91
+ can support different query shapes. A provider can push down supported ID and
92
+ declared-column query parts while preserving the common result semantics.
93
+
94
+ The storage API clones data at its boundaries. For the base factory and the
95
+ in-memory implementation, closing a factory prevents new record handles while
96
+ existing handles remain usable. Closing a record handle invalidates that
97
+ handle's later operations. Other adapters can close live handles; see their
98
+ [Datastore reference](../storage-datastore/REFERENCE.md) and
99
+ [MySQL reference](../storage-rdbms/REFERENCE.md) before choosing shutdown
100
+ behavior.
101
+
102
+ ## 🧩 Keep compatible record families separate
103
+
104
+ Most applications only need a `RecordSpec`. Sometimes two record families use
105
+ the same source type and record type but must remain separate: for example,
106
+ two independently retained views of the same Task state records.
107
+ Pass a `StorageGroup` as the optional third argument to give that family its
108
+ separate physical storage group. The group is deliberately separate from the
109
+ `RecordSpec`, because the record layout has not changed.
110
+
111
+ ```ts
112
+ import { create } from "@bufbuild/protobuf";
113
+ import { StringValueSchema, type StringValue } from "@bufbuild/protobuf/wkt";
114
+ import { InMemoryStorageFactory, RecordSpec, StorageGroup } from "@spine-event-engine/storage";
115
+
116
+ const factory = new InMemoryStorageFactory();
117
+ const stateHistorySpec = new RecordSpec<string, StringValue>({
118
+ recordType: StringValueSchema,
119
+ idKind: "string",
120
+ extractId: (state) => state.value,
121
+ });
122
+
123
+ const auditStates = factory.createRecordStorage(
124
+ { name: "Tasks", multitenant: false },
125
+ stateHistorySpec,
126
+ new StorageGroup("example.tasks.TaskState.audit"),
127
+ );
128
+
129
+ await auditStates.write(create(StringValueSchema, { value: "first" }));
130
+ ```
131
+
132
+ Use a group only when otherwise compatible records must not share rows. The
133
+ ordinary Event Store is intentionally ungrouped.
134
+
135
+ ## 🗄️ Choose a durable adapter
136
+
137
+ `@spine-event-engine/storage-datastore` provides Google Cloud Datastore
138
+ storage. `@spine-event-engine/storage-rdbms` provides MySQL storage. Configure
139
+ those packages in application code and pass the resulting factory to the Spine
140
+ server; this package does not choose a database.
141
+
142
+ | Need | Adapter |
143
+ | --------------------------- | ------------------------------------------------------------------------- |
144
+ | Local development and tests | `InMemoryStorageFactory` in this package |
145
+ | Google Cloud Datastore | [`@spine-event-engine/storage-datastore`](../storage-datastore/README.md) |
146
+ | MySQL | [`@spine-event-engine/storage-rdbms`](../storage-rdbms/README.md) |
147
+
148
+ ## ⚠️ Lifecycle differences
149
+
150
+ The common API defines record behavior, not deployment policy. Provider
151
+ adapters may close live handles, impose query budgets, or require indexes and
152
+ credentials. Read the selected adapter guide before configuring production
153
+ shutdown and queries.
154
+
155
+ ## 🔗 Learn more
156
+
157
+ - [Datastore adapter](../storage-datastore/README.md)
158
+ - [MySQL adapter](../storage-rdbms/README.md)
159
+ - [Server](../server/README.md)
160
+ - [Reference for coding agents](REFERENCE.md)
package/REFERENCE.md ADDED
@@ -0,0 +1,120 @@
1
+ # @spine-event-engine/storage reference
2
+
3
+ This reference is for agents working with the Spine TS storage contract.
4
+
5
+ ## Public entry point
6
+
7
+ Import public types from `@spine-event-engine/storage`. The entry point exports
8
+ `StorageFactory`, `RecordStorage`, `RecordSpec`, `RecordSpecOptions`, `RecordColumn`, `RecordQuery`,
9
+ `RecordMask`, `StorageGroup`, `ColumnTypes`, `ColumnMappings`, the exported
10
+ column-mapping contracts, `TenantBoundary`, `TenantCatalog`,
11
+ `TenantCatalogProvider`, `InMemoryStorageFactory`,
12
+ `InMemoryStorageBackend`, event-store types, normalized query policy/evaluator
13
+ types, and entity history interfaces.
14
+ The `./internal/entity-history` subpath supplies Entity records, source-type
15
+ inputs, and current/state/event-history ports. The separate
16
+ `./internal/entity-commit` subpath supplies atomic commit input/result types,
17
+ `EntityCommitStorage`, and `EntityCommitStorageFactories`; both are
18
+ framework/provider seams, not application-facing remote APIs.
19
+
20
+ ## Record storage
21
+
22
+ `StorageFactory.createRecordStorage(context, spec, group?)` returns an
23
+ independently closeable `RecordStorage`. A single-tenant context forbids a
24
+ tenant ID. A multitenant context requires a complete generated `TenantId`.
25
+ The Bounded Context name is diagnostic only. A `RecordSpec` fixes the
26
+ source type, stored record type, identity extractor, ID schema or primitive ID
27
+ kind, and materialized columns. `sourceType` defaults to `recordType`; Entity
28
+ record specifications use the entity state type as their source type.
29
+ `RecordSpecOptions` is the public constructor-input contract for those fields.
30
+ For generated application records, Proto `(column)` declarations determine the
31
+ materialized fields. A field without that declaration remains only in the
32
+ authoritative serialized record; providers do not infer columns from every
33
+ field in the message.
34
+
35
+ `StorageGroup` is an optional external physical-family identity. It is not part
36
+ of `RecordSpec`: use it only when records with an otherwise compatible layout
37
+ must remain distinct. The in-memory provider keys physical identity by backend,
38
+ tenant boundary, source type, and either the named group or the explicit
39
+ ungrouped value. It keeps different source types and
40
+ different groups separate even when they use the same stored record type.
41
+ `idType`, `recordType`, `sourceType`, and `columns` are read-only accessors.
42
+
43
+ `RecordStorage` supports `write`, `writeAll`, `read`, `delete`, `compareAndSet`,
44
+ `index`, `query`, `queryEntries`, `queryPlan`, and `queryPlanEntries`. It clones
45
+ IDs and messages at its public boundary. `RecordQuery.ids` filters actual
46
+ storage slots, while `index()` returns logical IDs extracted from record bodies.
47
+ Its `atomicCompareAndSet` capability defaults to `false`. A provider sets it to
48
+ `true` only when `compareAndSet()` is atomic across compatible handles; code
49
+ that needs that guarantee must reject a handle that does not declare it.
50
+
51
+ ## Query behavior
52
+
53
+ Record queries validate positive limits, non-negative offsets, and
54
+ continuations that match the requested sort fields. Query plans are normalized
55
+ and checked against adapter capabilities. If a provider returns more candidates
56
+ than a plan's explicit query budget, or the exported
57
+ `defaultQueryCandidateLimit` of 10,000 when it is omitted,
58
+ `QueryCandidateLimitError` is thrown before
59
+ local materialization can return a partial semantic result.
60
+ An explicit query budget must be a positive safe integer no greater
61
+ than 10,000.
62
+
63
+ An accepted query budget is distinct from the one-row raw-provider
64
+ overflow lookahead used to detect excess: the shared default accepts 10,000
65
+ records and can fetch 10,001 raw rows; a provider may declare a lower accepted
66
+ ceiling, such as Datastore's 1,000 accepted / 1,001 raw rows.
67
+
68
+ `StorageQueryPolicy` validates normalized plans and
69
+ `StorageQueryEvaluator` applies the portable query semantics. Provider packages
70
+ can push down supported ID, declared-column, and sort parts of a plan, but must
71
+ preserve these semantics and enforce their documented bounds. `RecordQuery<I>`
72
+ statically types IDs only; filter and sort names are strings and filter values
73
+ are `unknown`. Each provider documents the runtime mapping and validation it
74
+ applies before using those inputs; callers cannot infer shared filter or sort
75
+ name validation from the common query shape.
76
+
77
+ The normalized-plan matrix is intentionally provider-specific. MySQL admits
78
+ IDs; equality and the five comparisons on mapped orderable columns; nested
79
+ `all` and `either`; declared-column ordering; positive limits; and masks.
80
+ Datastore admits only IDs, equality, one provider-legal inequality column, flat
81
+ `all`, compatible ordering, limits, and masks. Both reject unsupported shapes
82
+ before provider access. Normalized plans never include offset: the existing
83
+ `RecordQuery.offset` path is separate. MySQL executes every admitted predicate,
84
+ order, and finite bound in contained parameterized SQL; Datastore executes only
85
+ that stated overlap. See each provider reference for mappings and index needs.
86
+
87
+ ## Lifecycle
88
+
89
+ The base `StorageFactory.close()` prevents later record-storage creation. The
90
+ in-memory factory follows that behavior and leaves existing record handles open
91
+ until each handle closes. `RecordStorage.close()` rejects that handle's later
92
+ operations. Datastore follows the base factory behavior. Adapters can define a
93
+ stronger shutdown lifecycle: the MySQL factory closes live handles while it
94
+ drains its pool. Read the [Datastore reference](../storage-datastore/REFERENCE.md)
95
+ and [MySQL reference](../storage-rdbms/REFERENCE.md) before relying on shutdown
96
+ behavior. The in-memory backend is ephemeral and process-local. Passing one
97
+ `InMemoryStorageBackend` to multiple in-memory factories deliberately shares
98
+ its scoped rows.
99
+
100
+ ## Entity storage
101
+
102
+ The `./internal/entity-history` seam supplies the framework's Entity storage
103
+ ports. Current Entity state is a generated `spine.server.entity.EntityRecord`.
104
+ Retained state history stores generated `EntityStateKey`/`EntityRecord` rows in
105
+ a `StorageGroup` named after the Entity state type. Retained diagnostic event
106
+ history stores generated `EventId`/`Event` rows in that same state-type-named
107
+ group. The framework Event Store is a separate, ungrouped `EventId`/`Event`
108
+ record family.
109
+
110
+ History ports are lazy. When a history is disabled, the in-memory provider does
111
+ not open or allocate its grouped records. Current Entity loading always uses
112
+ the current record, never retained history. Event history is diagnostic data,
113
+ not a source for rebuilding current state.
114
+
115
+ The separate `./internal/entity-commit` port combines one current record with
116
+ the nonempty enabled history families and delivery events. For the in-memory
117
+ provider only, that operation stages and atomically publishes its touched
118
+ families. Its outcomes are `"committed"` and `"conflict"`; it has no receipt or
119
+ `"replayed"` outcome. Other providers define and document their atomicity
120
+ guarantees.
@@ -0,0 +1,26 @@
1
+ import { type Message } from "@bufbuild/protobuf";
2
+ import type { GenMessage } from "@bufbuild/protobuf/codegenv2";
3
+ import { type Event, type EventId } from "@spine-event-engine/proto";
4
+ import { type EntityRecord } from "@spine-event-engine/proto/generated/spine/server/entity/entity_pb.js";
5
+ import { type EntityStateKey } from "@spine-event-engine/proto/generated/spine/server/entity/state_key_pb.js";
6
+ import { RecordSpec } from "../record/record-spec.js";
7
+ import { StorageGroup } from "../record/storage-group.js";
8
+ /**
9
+ * Creates the generated state-history layout for one Entity state type.
10
+ * @param stateType The served Entity state message type.
11
+ * @returns The separate group and record specification for retained states.
12
+ */
13
+ export declare function stateHistorySpec(stateType: GenMessage<Message>): {
14
+ readonly group: StorageGroup;
15
+ readonly spec: RecordSpec<EntityStateKey, EntityRecord>;
16
+ };
17
+ /**
18
+ * Creates the generated diagnostic event-history layout for one Entity state type.
19
+ * @param stateType The served Entity state message type.
20
+ * @returns The separate group and record specification for retained events.
21
+ */
22
+ export declare function eventHistorySpec(stateType: GenMessage<Message>): {
23
+ readonly group: StorageGroup;
24
+ readonly spec: RecordSpec<EventId, Event>;
25
+ };
26
+ //# sourceMappingURL=entity-history-record-spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity-history-record-spec.d.ts","sourceRoot":"","sources":["../../src/entity/entity-history-record-spec.ts"],"names":[],"mappings":"AAcA,OAAO,EAAsB,KAAK,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAE/D,OAAO,EAA8B,KAAK,KAAK,EAAE,KAAK,OAAO,EAAE,MAAM,2BAA2B,CAAC;AACjG,OAAO,EAEL,KAAK,YAAY,EAClB,MAAM,sEAAsE,CAAC;AAC9E,OAAO,EAEL,KAAK,cAAc,EACpB,MAAM,yEAAyE,CAAC;AAIjF,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAE1D;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG;IAChE,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;IAC7B,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC,cAAc,EAAE,YAAY,CAAC,CAAC;CACzD,CA+BA;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG;IAChE,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;IAC7B,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;CAC3C,CA8BA"}
@@ -0,0 +1,77 @@
1
+ /*
2
+ * Copyright 2026, CodeMatters. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
+ * in compliance with the License. You may obtain a copy of the License at
6
+ *
7
+ * https://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
10
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
+ * or implied. See the License for the specific language governing permissions and limitations under
12
+ * the License.
13
+ */
14
+ import { create, ScalarType } from "@bufbuild/protobuf";
15
+ import { AnySchema, TimestampSchema } from "@bufbuild/protobuf/wkt";
16
+ import { EventIdSchema, EventSchema } from "@spine-event-engine/proto";
17
+ import { EntityRecordSchema, } from "@spine-event-engine/proto/generated/spine/server/entity/entity_pb.js";
18
+ import { EntityStateKeySchema, } from "@spine-event-engine/proto/generated/spine/server/entity/state_key_pb.js";
19
+ import { RecordColumn } from "../record/record-column.js";
20
+ import { ColumnTypes } from "../record/column-type.js";
21
+ import { RecordSpec } from "../record/record-spec.js";
22
+ import { StorageGroup } from "../record/storage-group.js";
23
+ /**
24
+ * Creates the generated state-history layout for one Entity state type.
25
+ * @param stateType The served Entity state message type.
26
+ * @returns The separate group and record specification for retained states.
27
+ */
28
+ export function stateHistorySpec(stateType) {
29
+ return {
30
+ group: new StorageGroup(stateType.typeName),
31
+ spec: new RecordSpec({
32
+ sourceType: stateType,
33
+ recordType: EntityRecordSchema,
34
+ idSchema: EntityStateKeySchema,
35
+ extractId: (record) => {
36
+ if (record.entityId === undefined) {
37
+ throw new Error("State history requires EntityRecord.entityId.");
38
+ }
39
+ return create(EntityStateKeySchema, {
40
+ entityId: record.entityId,
41
+ version: record.version?.number ?? 0,
42
+ });
43
+ },
44
+ columns: [
45
+ new RecordColumn("entity_id", ColumnTypes.message(AnySchema), (record) => record.entityId),
46
+ new RecordColumn("created", ColumnTypes.message(TimestampSchema), (record) => record.version?.timestamp ?? create(TimestampSchema)),
47
+ new RecordColumn("version", ColumnTypes.scalar(ScalarType.INT32), (record) => record.version?.number ?? 0),
48
+ ],
49
+ }),
50
+ };
51
+ }
52
+ /**
53
+ * Creates the generated diagnostic event-history layout for one Entity state type.
54
+ * @param stateType The served Entity state message type.
55
+ * @returns The separate group and record specification for retained events.
56
+ */
57
+ export function eventHistorySpec(stateType) {
58
+ return {
59
+ group: new StorageGroup(stateType.typeName),
60
+ spec: new RecordSpec({
61
+ sourceType: EventSchema,
62
+ recordType: EventSchema,
63
+ idSchema: EventIdSchema,
64
+ extractId: (event) => {
65
+ if (event.id === undefined)
66
+ throw new Error("Event history requires Event.id.");
67
+ return event.id;
68
+ },
69
+ columns: [
70
+ new RecordColumn("entity_id", ColumnTypes.message(AnySchema), (event) => event.context?.producerId),
71
+ new RecordColumn("created", ColumnTypes.message(TimestampSchema), (event) => event.context?.timestamp ?? create(TimestampSchema)),
72
+ new RecordColumn("version", ColumnTypes.scalar(ScalarType.INT32), (event) => event.context?.version?.number ?? 0),
73
+ ],
74
+ }),
75
+ };
76
+ }
77
+ //# sourceMappingURL=entity-history-record-spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity-history-record-spec.js","sourceRoot":"","sources":["../../src/entity/entity-history-record-spec.ts"],"names":[],"mappings":"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"}