@grafctory/data-store 0.16.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/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,66 @@
1
+ # @grafctory/data-store
2
+
3
+ [![npm][npm-badge]][npm] [![Grafctory][grafctory-badge]][grafctory]
4
+
5
+ [npm-badge]: https://img.shields.io/npm/v/@grafctory/data-store
6
+ [npm]: https://www.npmjs.com/package/@grafctory/data-store
7
+ [grafctory-badge]: https://img.shields.io/badge/part%20of-Grafctory-blue
8
+ [grafctory]: https://github.com/grafctory
9
+
10
+ Store processing tasks for [@grafctory/data].
11
+
12
+ Provides ready-made [@grafctory/flow] steps for working with [@grafctory/keep] stores in pipelines run by the
13
+ [@grafctory/gear] job executor.
14
+
15
+ [@grafctory/data]: https://github.com/grafctory/data
16
+ [@grafctory/flow]: https://github.com/grafctory/flow
17
+ [@grafctory/keep]: https://github.com/grafctory/keep
18
+ [@grafctory/gear]: https://github.com/grafctory/gear
19
+
20
+ # Installation
21
+
22
+ ```shell
23
+ npm install @grafctory/gear # the job executor
24
+ npm install @grafctory/data-store # this package
25
+ npm install @grafctory/keep-<connector> # store connector for the target backend
26
+ ```
27
+
28
+ > [!IMPORTANT]
29
+ >
30
+ > Node.js 22 or later is required.
31
+
32
+ > [!WARNING]
33
+ >
34
+ > TypeScript consumers must use `"moduleResolution": "nodenext"/"node16"/"bundler"` in `tsconfig.json`.
35
+ > The legacy `"node"` resolver is not supported.
36
+
37
+ # Usage
38
+
39
+ | Task | Description |
40
+ |----------------------|---------------------------|
41
+ | [`lookup()`][lookup] | Store resource retriever |
42
+ | [`retire()`][retire] | Retired resource selector |
43
+ | [`settle()`][settle] | Resource delta writer |
44
+
45
+ [lookup]: https://grafctory.github.io/data/functions/_grafctory_data-store.lookup.html
46
+
47
+ [retire]: https://grafctory.github.io/data/functions/_grafctory_data-store.retire.html
48
+
49
+ [settle]: https://grafctory.github.io/data/functions/_grafctory_data-store.settle.html
50
+
51
+ [createStore]: https://grafctory.github.io/data/functions/_grafctory_data-store.createStore.html
52
+
53
+ The store service has no default: a job binds a store connector to [`createStore()`][createStore] once, and every task
54
+ or sink in the execution then reads from or writes to the store that connector reaches.
55
+
56
+ # Support
57
+
58
+ - open an [issue](https://github.com/grafctory/data/issues) to report a problem or to suggest a new feature
59
+ - start a [discussion](https://github.com/grafctory/data/discussions) to ask a how-to question or to share an idea
60
+
61
+ # License
62
+
63
+ This project is licensed under the Apache 2.0 License –
64
+ see [LICENSE](https://github.com/grafctory/data?tab=Apache-2.0-1-ov-file) file for details.
65
+
66
+ <sub>Grafctory™ is a trademark of Metreeca srl.</sub>
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Store processing tasks.
3
+ *
4
+ * Provides ready-made {@link https://github.com/grafctory/flow @grafctory/flow} steps for working with
5
+ * {@link https://github.com/grafctory/keep @grafctory/keep} stores in pipelines run by the
6
+ * {@link https://github.com/grafctory/gear @grafctory/gear} executor.
7
+ *
8
+ * A job binds a store connector to {@link createStore} once, and every step that needs a store uses that one.
9
+ *
10
+ * @module index
11
+ */
12
+ import type { Store } from "@grafctory/keep";
13
+ /**
14
+ * Creates the default store.
15
+ *
16
+ * Identifies the store service that the steps of this package read from and write to.
17
+ *
18
+ * > [!IMPORTANT]
19
+ * >
20
+ * > No default is provided: a job must bind a store connector before creating tasks that rely on it.
21
+ *
22
+ * @returns Never: the store service has no default implementation
23
+ *
24
+ * @throws {@link !Error Error} Always, as no store connector is bound to the service in the enclosing execution
25
+ */
26
+ export declare function createStore(): Store;
27
+ export * from "./lookup.js";
28
+ export * from "./retire.js";
29
+ export * from "./settle.js";
30
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAgBA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAM7C;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,IAAI,KAAK,CAInC;AAKD,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ /*
2
+ * Copyright © 2026 Metreeca srl
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
17
+ /**
18
+ * Creates the default store.
19
+ *
20
+ * Identifies the store service that the steps of this package read from and write to.
21
+ *
22
+ * > [!IMPORTANT]
23
+ * >
24
+ * > No default is provided: a job must bind a store connector before creating tasks that rely on it.
25
+ *
26
+ * @returns Never: the store service has no default implementation
27
+ *
28
+ * @throws {@link !Error Error} Always, as no store connector is bound to the service in the enclosing execution
29
+ */
30
+ export function createStore() {
31
+ throw new Error("undefined store service");
32
+ }
33
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
34
+ export * from "./lookup.js";
35
+ export * from "./retire.js";
36
+ export * from "./settle.js";
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAiBH,wHAAwH;AAGxH;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,WAAW;IAE1B,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;AAE5C,CAAC;AAGD,wHAAwH;AAExH,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC"}
@@ -0,0 +1,52 @@
1
+ import type { ResourceShape } from "@grafctory/blue/resource";
2
+ import { type Frame, type Match, type Model, type Slice } from "@grafctory/blue/value";
3
+ import type { Lazy } from "@grafctory/core";
4
+ import type { Feed } from "@grafctory/flow";
5
+ import type { StoreLookup } from "@grafctory/keep";
6
+ /**
7
+ * Retrieves the items of a collection held by a resource in the store.
8
+ *
9
+ * The request names a resource and a {@link Slice | slice} of it. A slice is a model with a single entry: a property
10
+ * holding any number of resources, together with the members to retrieve from each of them. The feed returns the
11
+ * items one by one, each typed as a {@link Frame | frame} of the slice, rather than the resource holding them.
12
+ *
13
+ * A model naming any further member, such as `id`, retrieves the resource itself rather than the items it holds.
14
+ *
15
+ * If the store does not hold the resource, or the collection holds no items, the feed is empty.
16
+ *
17
+ * The resource is read from the store bound to the current execution.
18
+ *
19
+ * @typeParam S The shape of the resource holding the collection, possibly deferred to break definition cycles
20
+ * @typeParam T The slice naming the collection and the members to retrieve from its items
21
+ *
22
+ * @param request The resource to retrieve and the slice to retrieve it with
23
+ *
24
+ * @returns A feed of the items of the collection, each restricted to the members the slice names
25
+ *
26
+ * @throws {@link !Error Error} If no store is bound to the current execution
27
+ */
28
+ export declare function lookup<S extends Lazy<ResourceShape>, T extends Slice<S, T>>(request: StoreLookup<S, T>): Feed<Frame<S, T>>;
29
+ /**
30
+ * Retrieves a resource from the store.
31
+ *
32
+ * The request names a resource and a {@link Model | model} of the members to retrieve from it. The feed returns the
33
+ * resource once, typed as a {@link Match | match} of the model and restricted to the members it names.
34
+ *
35
+ * A model naming a single collection of resources and nothing else is a {@link Slice | slice}: the feed then returns
36
+ * the items of the collection, each typed as a {@link Frame | frame}, rather than the resource holding them.
37
+ *
38
+ * If the store does not hold the resource, the feed is empty.
39
+ *
40
+ * The resource is read from the store bound to the current execution.
41
+ *
42
+ * @typeParam S The shape of the resource, possibly deferred to break definition cycles
43
+ * @typeParam T The model naming the members to retrieve from the resource
44
+ *
45
+ * @param request The resource to retrieve and the model to retrieve it with
46
+ *
47
+ * @returns A feed of the resource, restricted to the members the model names
48
+ *
49
+ * @throws {@link !Error Error} If no store is bound to the current execution
50
+ */
51
+ export declare function lookup<S extends Lazy<ResourceShape>, T extends Model<S, T>>(request: StoreLookup<S, T>): Feed<Match<S, T>>;
52
+ //# sourceMappingURL=lookup.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lookup.d.ts","sourceRoot":"","sources":["../src/lookup.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAS,KAAK,KAAK,EAAmB,KAAK,KAAK,EAAE,KAAK,KAAK,EAAE,KAAK,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC/G,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAI5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAUnD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,MAAM,CACrB,CAAC,SAAS,IAAI,CAAC,aAAa,CAAC,EAC7B,CAAC,SAAS,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EACpB,OAAO,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAEjD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,MAAM,CACrB,CAAC,SAAS,IAAI,CAAC,aAAa,CAAC,EAC7B,CAAC,SAAS,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EACpB,OAAO,EAAE,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC"}
package/dist/lookup.js ADDED
@@ -0,0 +1,45 @@
1
+ /*
2
+ * Copyright © 2026 Metreeca srl
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { eager, items as frames } from "@grafctory/blue/value";
17
+ import { items } from "@grafctory/flow/feeds";
18
+ import { flat, map, trim } from "@grafctory/flow/tasks";
19
+ import { service } from "@grafctory/gear";
20
+ import { log, report, time } from "@grafctory/tape";
21
+ import { createStore } from "./index.js";
22
+ const logger = log(import.meta.url);
23
+ /**
24
+ * Retrieves a resource, or the items of a collection it holds, from the store.
25
+ */
26
+ export function lookup(request) {
27
+ const store = service(createStore);
28
+ return isSlice(request)
29
+ ? retrieve(request)(flat(map(match => frames(match, request.model))))
30
+ : retrieve(request);
31
+ function isSlice(request) {
32
+ const { shape, model } = request;
33
+ const fields = Object.keys(model);
34
+ const member = fields.length === 1 ? eager(shape).members[fields[0]] : undefined;
35
+ const range = member?.kind === "property" ? member.range : undefined;
36
+ const kind = range && range.maxCount !== 1 ? eager(range.shape).kind : undefined;
37
+ return kind === "reference" || kind === "resource";
38
+ }
39
+ function retrieve(request) {
40
+ return trim(items(time(() => store.lookup(request), (_, elapsed) => {
41
+ logger.info `looked up resource <${request.entry}> in <${report(elapsed)}> ms`;
42
+ })));
43
+ }
44
+ }
45
+ //# sourceMappingURL=lookup.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lookup.js","sourceRoot":"","sources":["../src/l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AAE,IAAI,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;QACrE,MAAM,IAAI,GAAG,KAAK,IAAI,KAAK,CAAC,QAAQ,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;QAEjF,OAAO,IAAI,KAAK,WAAW,IAAI,IAAI,KAAK,UAAU,CAAC;IAEpD,CAAC;IAED,SAAS,QAAQ,CAAwB,OAA0B;QAClE,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE;YAClE,MAAM,CAAC,IAAI,CAAA,uBAAuB,OAAO,CAAC,KAAK,SAAS,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;QAC/E,CAAC,CAAC,CAAC,CAAC,CAAC;IACN,CAAC;AAEF,CAAC"}
@@ -0,0 +1,39 @@
1
+ import type { IRI } from "@grafctory/core/resource";
2
+ import { type Feed, type Task } from "@grafctory/flow";
3
+ /**
4
+ * Creates a task that selects the resources to retire.
5
+ *
6
+ * The incoming feed is a scan of the source: it lists the identifiers of every resource the source still provides. The
7
+ * `existing` feed lists the identifiers currently in the store. The task returns the existing identifiers that are
8
+ * missing from the scan, each at most once. It returns nothing until the scan is complete, so a scan still in progress
9
+ * never leads to the retirement of a resource that the rest of the scan would have listed.
10
+ *
11
+ * A scan that returns too few identifiers usually means a broken source rather than a source that was really emptied. A
12
+ * scan is therefore refused if it lists fewer identifiers than the `survival` fraction of the existing ones. The check
13
+ * counts every identifier in the scan, not only those already in the store. A source that renames all its resources at
14
+ * once still passes the check, while a truncated or unreachable source is still caught.
15
+ *
16
+ * By default, a refused scan skips only the retirements and lets the other writes of the run go through. Once the
17
+ * source works again, its next scan retires the same resources. With `atomic`, a refused scan fails the whole run
18
+ * instead. This suits sources that cannot tolerate a partially applied run of the pipeline.
19
+ *
20
+ * A `survival` of `0` disables the check, so even an empty scan retires everything. A `survival` of `1` refuses any
21
+ * scan that lists fewer identifiers than the store currently holds for the source.
22
+ *
23
+ * @param options The options governing what a single run may retire
24
+ * @param options.atomic Whether a refused scan fails the whole run; defaults to `false`, skipping only the retirements
25
+ * @param options.survival The minimum fraction of the existing identifiers a scan must list, in [0, 1], rounded up
26
+ * @param options.existing A feed of the identifiers of the resources currently in the store
27
+ *
28
+ * @returns A task turning the identifiers listed by a scan into the existing identifiers to retire
29
+ *
30
+ * @throws {@link !TypeError TypeError} If `survival` is outside [0, 1]
31
+ *
32
+ * @throws {@link !RangeError RangeError} While the feed is consumed, with `atomic`, if a scan is refused
33
+ */
34
+ export declare function retire({ atomic, survival, existing }: {
35
+ readonly atomic?: boolean;
36
+ readonly survival: number;
37
+ readonly existing: Feed<IRI>;
38
+ }): Task<IRI>;
39
+ //# sourceMappingURL=retire.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retire.d.ts","sourceRoot":"","sources":["../src/retire.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,EAAE,KAAK,IAAI,EAAQ,KAAK,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAY7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,MAAM,CAAC,EAEtB,MAAc,EACd,QAAQ,EAER,QAAQ,EAER,EAAE;IAEF,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAA;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IAEzB,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC,GAAG,CAAC,CAAA;CAE5B,GAAG,IAAI,CAAC,GAAG,CAAC,CAkDZ"}
package/dist/retire.js ADDED
@@ -0,0 +1,86 @@
1
+ /*
2
+ * Copyright © 2026 Metreeca srl
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { assert } from "@grafctory/core";
17
+ import { difference } from "@grafctory/core/arrays";
18
+ import { pipe } from "@grafctory/flow";
19
+ import { items } from "@grafctory/flow/feeds";
20
+ import { toSet } from "@grafctory/flow/sinks";
21
+ import { chain } from "@grafctory/flow/tasks";
22
+ import { log } from "@grafctory/tape";
23
+ const logger = log(import.meta.url);
24
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
25
+ /**
26
+ * Creates a task that selects the resources to retire.
27
+ *
28
+ * The incoming feed is a scan of the source: it lists the identifiers of every resource the source still provides. The
29
+ * `existing` feed lists the identifiers currently in the store. The task returns the existing identifiers that are
30
+ * missing from the scan, each at most once. It returns nothing until the scan is complete, so a scan still in progress
31
+ * never leads to the retirement of a resource that the rest of the scan would have listed.
32
+ *
33
+ * A scan that returns too few identifiers usually means a broken source rather than a source that was really emptied. A
34
+ * scan is therefore refused if it lists fewer identifiers than the `survival` fraction of the existing ones. The check
35
+ * counts every identifier in the scan, not only those already in the store. A source that renames all its resources at
36
+ * once still passes the check, while a truncated or unreachable source is still caught.
37
+ *
38
+ * By default, a refused scan skips only the retirements and lets the other writes of the run go through. Once the
39
+ * source works again, its next scan retires the same resources. With `atomic`, a refused scan fails the whole run
40
+ * instead. This suits sources that cannot tolerate a partially applied run of the pipeline.
41
+ *
42
+ * A `survival` of `0` disables the check, so even an empty scan retires everything. A `survival` of `1` refuses any
43
+ * scan that lists fewer identifiers than the store currently holds for the source.
44
+ *
45
+ * @param options The options governing what a single run may retire
46
+ * @param options.atomic Whether a refused scan fails the whole run; defaults to `false`, skipping only the retirements
47
+ * @param options.survival The minimum fraction of the existing identifiers a scan must list, in [0, 1], rounded up
48
+ * @param options.existing A feed of the identifiers of the resources currently in the store
49
+ *
50
+ * @returns A task turning the identifiers listed by a scan into the existing identifiers to retire
51
+ *
52
+ * @throws {@link !TypeError TypeError} If `survival` is outside [0, 1]
53
+ *
54
+ * @throws {@link !RangeError RangeError} While the feed is consumed, with `atomic`, if a scan is refused
55
+ */
56
+ export function retire({ atomic = false, survival, existing }) {
57
+ const $survival = assert(survival, value => value >= 0 && value <= 1, `expected survival rate in [0, 1]`);
58
+ return chain(async (incoming) => {
59
+ const [$existing, $incoming] = await Promise.all([
60
+ existing(toSet()),
61
+ pipe((items(incoming))(toSet()))
62
+ ]);
63
+ const retired = difference([$existing, $incoming]);
64
+ const current = $existing.size;
65
+ const dropped = retired.length;
66
+ const minimum = current * $survival;
67
+ const scanned = $incoming.size; // every identifier the source still provides, renamed ones included
68
+ if (scanned < minimum) { // a short scan more likely means a broken source than a really emptied one
69
+ const action = atomic ? "prevented" : "skipping";
70
+ const share = Math.round($survival * 100);
71
+ const message = `${action} mass retirement leaving less than <${share}%> of <${current}> current resources`;
72
+ if (atomic) { // failing the whole run leaves the store as the last successful run left it
73
+ throw new RangeError(message);
74
+ }
75
+ else { // skipping only the retirements lets the other writes go through
76
+ logger.warn(message);
77
+ return [];
78
+ }
79
+ }
80
+ else {
81
+ logger.info `retiring <${dropped}> of <${current}> current resources`;
82
+ return retired;
83
+ }
84
+ });
85
+ }
86
+ //# sourceMappingURL=retire.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retire.js","sourceRoot":"","sources":["../src/retire.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEpD,OAA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AA2E;YAErG,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,UAAU,CAAC;YACjD,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,GAAC,GAAG,CAAC,CAAC;YAExC,MAAM,OAAO,GAAG,GAAG,MAAM,uCAAuC,KAAK,UAAU,OAAO,qBAAqB,CAAC;YAE5G,IAAK,MAAM,EAAG,CAAC,CAAC,4EAA4E;gBAE3F,MAAM,IAAI,UAAU,CAAC,OAAO,CAAC,CAAC;YAE/B,CAAC;iBAAM,CAAC,CAAC,iEAAiE;gBAEzE,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBAErB,OAAO,EAAE,CAAC;YAEX,CAAC;QAEF,CAAC;aAAM,CAAC;YAEP,MAAM,CAAC,IAAI,CAAA,aAAa,OAAO,SAAS,OAAO,qBAAqB,CAAC;YAErE,OAAO,OAAO,CAAC;QAEhB,CAAC;IAEF,CAAC,CAAC,CAAC;AAEJ,CAAC"}
@@ -0,0 +1,93 @@
1
+ import type { ResourceShape } from "@grafctory/blue/resource";
2
+ import type { Draft } from "@grafctory/blue/value";
3
+ import type { Lazy } from "@grafctory/core";
4
+ import type { Sink } from "@grafctory/flow";
5
+ import type { Reference } from "@grafctory/qest/state";
6
+ /**
7
+ * Target state of a single resource.
8
+ *
9
+ * A pipeline produces one delta for each resource it handles: either the state to store, or `null` to remove the
10
+ * resource. Each delta also names the shape of its resource, so a single feed can carry resources of different kinds.
11
+ * Pipelines that add resources and pipelines that retire them can thus all end on the same {@link settle} sink.
12
+ *
13
+ * @typeParam S The shape of the resource, against which the state is checked
14
+ */
15
+ export type Delta<S extends Lazy<ResourceShape> = Lazy<ResourceShape>> = readonly [
16
+ /**
17
+ * The resource concerned.
18
+ */
19
+ entry: Reference,
20
+ /**
21
+ * The shape of the resource.
22
+ */
23
+ shape: S,
24
+ /**
25
+ * The state to store for the resource, or `null` to remove it.
26
+ */
27
+ state: null | Draft<S>
28
+ ];
29
+ /**
30
+ * Counts of the deltas a {@link settle} run handled, by outcome.
31
+ *
32
+ * Each delta is counted exactly once, so the three counts add up to the number of deltas in the feed. A run completed
33
+ * without any failed write if, and only if, its tally holds a `reject` count of zero.
34
+ */
35
+ export interface Tally {
36
+ /**
37
+ * The number of resources stored, whether new or replacing a previous state.
38
+ */
39
+ readonly insert: number;
40
+ /**
41
+ * The number of resources removed, including those the store did not hold.
42
+ */
43
+ readonly remove: number;
44
+ /**
45
+ * The number of resources left unchanged because their write failed.
46
+ */
47
+ readonly reject: number;
48
+ }
49
+ /**
50
+ * Creates a sink that writes the deltas of a pipeline to the store.
51
+ *
52
+ * By default, each delta is committed on its own. If a run is interrupted, the resources already written stay in the
53
+ * store and the next run can carry on from there. A failed write is logged and counted as rejected: the resource keeps
54
+ * its previous state and the feed keeps running. A single failure costs only its own resource.
55
+ *
56
+ * A state rejected as invalid is handled differently, as it signals a broken run rather than a bad resource. If the
57
+ * states do not satisfy their shape, every write fails, and the run would still look clean while storing nothing. The
58
+ * error is therefore propagated, so the run fails visibly and can be retried once the states are fixed.
59
+ *
60
+ * With `atomic`, the whole feed is committed in a single transaction instead: if any write fails, the run fails and
61
+ * nothing is committed. This suits sources that cannot tolerate a partially applied run.
62
+ *
63
+ * The deltas are written to the store bound to the current execution.
64
+ *
65
+ * @param options The options governing how the deltas are committed
66
+ * @param options.atomic Whether to commit all the deltas in a single transaction; defaults to `false`
67
+ *
68
+ * @returns A sink that writes a feed of deltas and resolves to a {@link Tally} of the changes made
69
+ *
70
+ * @throws {@link !Error Error} If no store is bound to the current execution
71
+ *
72
+ * @throws {@link !RangeError RangeError} While the feed is consumed, if the store rejects a state as invalid
73
+ *
74
+ * @throws {@link !Error Error} While the feed is consumed, with `atomic`, if any write fails; nothing is committed
75
+ */
76
+ export declare function settle({ atomic }?: {
77
+ readonly atomic?: boolean;
78
+ }): Sink<Delta, Tally>;
79
+ /**
80
+ * Creates a delta for a resource.
81
+ *
82
+ * A `null` state requests the removal of the resource.
83
+ *
84
+ * @typeParam S The shape of the resource, against which the state is checked
85
+ *
86
+ * @param entry The resource concerned
87
+ * @param shape The shape of the resource
88
+ * @param state The state to store for the resource, or `null` to remove it
89
+ *
90
+ * @returns An immutable {@link Delta} combining `entry`, `shape` and `state`
91
+ */
92
+ export declare function delta<S extends Lazy<ResourceShape>>(entry: Reference, shape: S, state: null | Draft<S>): Delta<S>;
93
+ //# sourceMappingURL=settle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"settle.d.ts","sourceRoot":"","sources":["../src/settle.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAI5C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAUvD;;;;;;;;GAQG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,IAAI,CAAC,aAAa,CAAC,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,SAAS;IAEjF;;OAEG;IACH,KAAK,EAAE,SAAS;IAEhB;;OAEG;IACH,KAAK,EAAE,CAAC;IAER;;OAEG;IACH,KAAK,EAAE,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC;CAEtB,CAAA;AAED;;;;;GAKG;AACH,MAAM,WAAW,KAAK;IAErB;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAExB;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CAExB;AAKD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,MAAM,CAAC,EAEtB,MAAc,EAEd,GAAE;IAEF,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAEpB,GAAG,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAuE1B;AAKD;;;;;;;;;;;;GAYG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,IAAI,CAAC,aAAa,CAAC,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,EAAE,KAAK,EAAE,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAIjH"}
package/dist/settle.js ADDED
@@ -0,0 +1,109 @@
1
+ /*
2
+ * Copyright © 2026 Metreeca srl
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { immutable } from "@grafctory/core/values";
17
+ import { reduce } from "@grafctory/flow/sinks";
18
+ import { service } from "@grafctory/gear";
19
+ import { log, report, time } from "@grafctory/tape";
20
+ import { createStore } from "./index.js";
21
+ const logger = log(import.meta.url);
22
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
23
+ /**
24
+ * Creates a sink that writes the deltas of a pipeline to the store.
25
+ *
26
+ * By default, each delta is committed on its own. If a run is interrupted, the resources already written stay in the
27
+ * store and the next run can carry on from there. A failed write is logged and counted as rejected: the resource keeps
28
+ * its previous state and the feed keeps running. A single failure costs only its own resource.
29
+ *
30
+ * A state rejected as invalid is handled differently, as it signals a broken run rather than a bad resource. If the
31
+ * states do not satisfy their shape, every write fails, and the run would still look clean while storing nothing. The
32
+ * error is therefore propagated, so the run fails visibly and can be retried once the states are fixed.
33
+ *
34
+ * With `atomic`, the whole feed is committed in a single transaction instead: if any write fails, the run fails and
35
+ * nothing is committed. This suits sources that cannot tolerate a partially applied run.
36
+ *
37
+ * The deltas are written to the store bound to the current execution.
38
+ *
39
+ * @param options The options governing how the deltas are committed
40
+ * @param options.atomic Whether to commit all the deltas in a single transaction; defaults to `false`
41
+ *
42
+ * @returns A sink that writes a feed of deltas and resolves to a {@link Tally} of the changes made
43
+ *
44
+ * @throws {@link !Error Error} If no store is bound to the current execution
45
+ *
46
+ * @throws {@link !RangeError RangeError} While the feed is consumed, if the store rejects a state as invalid
47
+ *
48
+ * @throws {@link !Error Error} While the feed is consumed, with `atomic`, if any write fails; nothing is committed
49
+ */
50
+ export function settle({ atomic = false } = {}) {
51
+ const store = service(createStore);
52
+ return async (feed) => time(async () => immutable(atomic
53
+ ? await store.execute(client => feed(collect(client)))
54
+ : await feed(collect(store))), ({ insert, remove, reject }, elapsed) => {
55
+ const total = insert + remove + reject;
56
+ const resources = total === 1 ? "resource" : "resources";
57
+ const counts = `insert <${report(insert)}> | remove <${report(remove)}> | reject <${report(reject)}>`;
58
+ logger.info `persisted <${report(total)}> ${resources} in <${report(elapsed)}> ms | ${counts}`;
59
+ });
60
+ function collect(client) {
61
+ return reduce(async (tally, [entry, shape, state]) => {
62
+ try {
63
+ if (state === null) {
64
+ await client.remove({ entry, shape });
65
+ logger.info `removed resource <${entry}>`;
66
+ return { ...tally, remove: tally.remove + 1 };
67
+ }
68
+ else {
69
+ await client.insert({ entry, shape, state });
70
+ logger.info `inserted resource <${entry}>`;
71
+ return { ...tally, insert: tally.insert + 1 };
72
+ }
73
+ }
74
+ catch (error) {
75
+ // an atomic run must fail as a whole, and an invalid state would fail every write anyway, so both
76
+ // end the run; any other failure costs only its own resource and leaves the feed running
77
+ if (atomic || error instanceof RangeError) {
78
+ throw error;
79
+ }
80
+ else {
81
+ logger.warning `unable to mutate resource <${entry}> (${report(error)})`;
82
+ return { ...tally, reject: tally.reject + 1 };
83
+ }
84
+ }
85
+ }, {
86
+ insert: 0,
87
+ remove: 0,
88
+ reject: 0
89
+ });
90
+ }
91
+ }
92
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
93
+ /**
94
+ * Creates a delta for a resource.
95
+ *
96
+ * A `null` state requests the removal of the resource.
97
+ *
98
+ * @typeParam S The shape of the resource, against which the state is checked
99
+ *
100
+ * @param entry The resource concerned
101
+ * @param shape The shape of the resource
102
+ * @param state The state to store for the resource, or `null` to remove it
103
+ *
104
+ * @returns An immutable {@link Delta} combining `entry`, `shape` and `state`
105
+ */
106
+ export function delta(entry, shape, state) {
107
+ return Object.freeze([entry, shape, state]);
108
+ }
109
+ //# sourceMappingURL=settle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"settle.js","sourceRoot":"","sources":["../src/settle.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAKH,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAG1C,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGzC,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AA2DpC,wHAAwH;AAExH;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,MAAM,CAAC,EAEtB,MAAM,GAAG,KAAK,KAMX,EAAE;IAEL,MAAM,KAAK,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;IAGnC,OAAO,KAAK,EAAC,IAAI,EAAC,EAAE,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC,SAAS,CAAC,MAAM;QACrD,CAAC,CAAC,MAA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CAAC;oBAE7C,MAAM,CAAC,IAAI,CAAA,sBAAsB,KAAK,GAAG,CAAC;oBAE1C,OAAO,EAAE,GAAG,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,GAAC,CAAC,EAAE,CAAC;gBAE7C,CAAC;YAEF,CAAC;YAAC,OAAQ,KAAK,EAAG,CAAC;gBAElB,kGAAkG;gBAClG,yFAAyF;gBAEzF,IAAK,MAAM,IAAI,KAAK,YAAY,UAAU,EAAG,CAAC;oBAE7C,MAAM,KAAK,CAAC;gBAEb,CAAC;qBAAM,CAAC;oBAEP,MAAM,CAAC,OAAO,CAAA,8BAA8B,KAAK,MAAM,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC;oBAExE,OAAO,EAAE,GAAG,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,GAAC,CAAC,EAAE,CAAC;gBAE7C,CAAC;YAEF,CAAC;QAEF,CAAC,EAAE;YAEF,MAAM,EAAE,CAAC;YACT,MAAM,EAAE,CAAC;YACT,MAAM,EAAE,CAAC;SAET,CAAC,CAAC;IACJ,CAAC;AAEF,CAAC;AAGD,wHAAwH;AAExH;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,KAAK,CAAgC,KAAgB,EAAE,KAAQ,EAAE,KAAsB;IAEtG,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;AAE7C,CAAC"}
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@grafctory/data-store",
3
+ "version": "0.16.0",
4
+ "description": "Store processing tasks for @grafctory/data.",
5
+ "keywords": [
6
+ "typescript",
7
+ "pipeline",
8
+ "etl",
9
+ "data-integration",
10
+ "linked-data",
11
+ "storage"
12
+ ],
13
+ "homepage": "https://grafctory.github.io/data/",
14
+ "bugs": {
15
+ "url": "https://github.com/grafctory/data/issues",
16
+ "email": "info@grafctory.dev"
17
+ },
18
+ "license": "Apache-2.0",
19
+ "author": {
20
+ "name": "Metreeca",
21
+ "email": "info@metreeca.com",
22
+ "url": "https://www.metreeca.com/"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/grafctory/data.git",
27
+ "directory": "packages/data-store"
28
+ },
29
+ "type": "module",
30
+ "sideEffects": false,
31
+ "exports": {
32
+ ".": {
33
+ "@grafctory/source": "./src/index.ts",
34
+ "default": "./dist/index.js"
35
+ }
36
+ },
37
+ "files": [
38
+ "README.md",
39
+ "LICENSE",
40
+ "dist"
41
+ ],
42
+ "engines": {
43
+ "node": ">=22.22.0"
44
+ },
45
+ "scripts": {
46
+ "clean": "ush rm dist docs node_modules",
47
+ "build": "ush rm dist && tsc -p tsconfig.build.json",
48
+ "check": "npx vitest --typecheck run"
49
+ },
50
+ "dependencies": {
51
+ "@grafctory/blue": "^0.16.0",
52
+ "@grafctory/core": "^0.14.0",
53
+ "@grafctory/flow": "^0.13.0",
54
+ "@grafctory/keep": "^0.15.0",
55
+ "@grafctory/qest": "^0.15.0",
56
+ "@grafctory/tape": "^0.13.0"
57
+ },
58
+ "devDependencies": {
59
+ "@grafctory/gear": "^0.6.0",
60
+ "@types/node": "^25.9.3",
61
+ "typescript": "^6.0.3",
62
+ "vitest": "^4.1.12"
63
+ },
64
+ "peerDependencies": {
65
+ "@grafctory/gear": "^0.6.0"
66
+ }
67
+ }