@gusto/create-baerly-storage 0.2.0 → 0.4.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.
Files changed (29) hide show
  1. package/LICENSE +202 -0
  2. package/dist/index.js +54 -28
  3. package/dist/index.js.map +1 -1
  4. package/dist/templates/addons/docker/Dockerfile +8 -2
  5. package/dist/templates/minimal-cloudflare/AGENTS.md +140 -101
  6. package/dist/templates/minimal-cloudflare/README.md +43 -33
  7. package/dist/templates/minimal-cloudflare/package.json +7 -7
  8. package/dist/templates/minimal-cloudflare/wrangler.jsonc +1 -1
  9. package/dist/templates/minimal-node/.baerly/scaffold.json +1 -1
  10. package/dist/templates/minimal-node/.env.example +12 -5
  11. package/dist/templates/minimal-node/AGENTS.md +135 -107
  12. package/dist/templates/minimal-node/README.md +77 -44
  13. package/dist/templates/minimal-node/package.json +5 -5
  14. package/dist/templates/minimal-node/src/server/index.ts +22 -30
  15. package/dist/templates/minimal-node/src/server/resolve-storage.ts +18 -0
  16. package/dist/templates/minimal-node/vite.config.ts +0 -2
  17. package/dist/templates/react-cloudflare/AGENTS.md +77 -52
  18. package/dist/templates/react-cloudflare/README.md +25 -16
  19. package/dist/templates/react-cloudflare/index.html +1 -1
  20. package/dist/templates/react-cloudflare/package.json +12 -12
  21. package/dist/templates/react-cloudflare/wrangler.jsonc +1 -1
  22. package/dist/templates/react-node/.env.example +12 -5
  23. package/dist/templates/react-node/AGENTS.md +117 -81
  24. package/dist/templates/react-node/README.md +67 -37
  25. package/dist/templates/react-node/package.json +10 -10
  26. package/dist/templates/react-node/src/server/index.ts +18 -29
  27. package/dist/templates/react-node/src/server/resolve-storage.ts +18 -0
  28. package/dist/templates/react-node/vite.config.ts +0 -2
  29. package/package.json +16 -11
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Gusto, Inc.
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/dist/index.js CHANGED
@@ -381,20 +381,19 @@ const substitutePackageJson = (text, ctx) => {
381
381
  //#endregion
382
382
  //#region src/scaffold.ts
383
383
  /**
384
- * Optional add-ons that can be layered on top of a base template
385
- * (`docker`) or on top of an existing wrangler project in bolt-on
386
- * mode (`agent-rules`). Adding a new entry here is what makes
387
- * `--with=<name>` recognised by both the flag parser and the wizard.
384
+ * Optional add-ons and compatibility tokens recognised by the CLI.
385
+ * `docker` is a scaffold template overlay; `agent-rules` is the legacy
386
+ * bolt-on `--with` token for the default-on agent-rules writer. Adding
387
+ * a new entry here is what makes `--with=<name>` recognised by both the
388
+ * flag parser and the wizard.
388
389
  *
389
390
  * - `docker` (scaffold-only, `--target=node`): copies the Dockerfile
390
391
  * add-on under `templates/addons/docker/` on top of the scaffolded
391
392
  * project.
392
- * - `agent-rules` (bolt-on-only): drops a delimited block telling AI
393
- * agents that `node_modules/@gusto/baerly-storage/dist/API.md` is the
394
- * canonical API surface. Scaffolded apps already ship that
395
- * guidance via the per-template `AGENTS.md`; this closes the gap
396
- * for users adopting baerly into a repo that didn't start from a
397
- * `create-baerly-storage` template.
393
+ * - `agent-rules` (bolt-on compatibility token): the CLI now writes
394
+ * this by default in bolt-on mode and accepts `--no-agent-rules` to
395
+ * opt out. Scaffolded apps already ship the same guidance via the
396
+ * per-template `AGENTS.md`.
398
397
  */
399
398
  const KNOWN_ADDONS = ["docker", "agent-rules"];
400
399
  /**
@@ -407,7 +406,7 @@ const KNOWN_ADDONS = ["docker", "agent-rules"];
407
406
  * `mise.toml` / `.tool-versions` (or any other top-level metadata
408
407
  * the user already wrote) coexists with the scaffold.
409
408
  */
410
- const SCAFFOLD_HERE_ALLOWLIST = new Set([
409
+ const SCAFFOLD_HERE_ALLOWLIST = /* @__PURE__ */ new Set([
411
410
  ".git",
412
411
  ".gitignore",
413
412
  ".gitattributes",
@@ -450,7 +449,7 @@ const topLevelOutputs = (templateDir, manifest, addonDirs) => {
450
449
  }
451
450
  return out;
452
451
  };
453
- const TEXT_EXTS = new Set([
452
+ const TEXT_EXTS = /* @__PURE__ */ new Set([
454
453
  ".ts",
455
454
  ".tsx",
456
455
  ".js",
@@ -663,7 +662,15 @@ const scaffold = async (opts) => {
663
662
  //#endregion
664
663
  //#region ../protocol/src/errors.ts
665
664
  /**
666
- * The single error class thrown by Baerly. Discriminate by `code`, not
665
+ * Codes whose default failure class is resolved by retrying with a fresh read.
666
+ * Throw sites may override this when the same code is context-sensitive
667
+ * (notably caller-supplied `_id` collisions, which also use `Conflict`).
668
+ */
669
+ const RETRIABLE_CODES = /* @__PURE__ */ new Set(["NetworkError", "Conflict"]);
670
+ /** True iff this error code's default failure class is retriable. */
671
+ const isRetriableCode = (code) => RETRIABLE_CODES.has(code);
672
+ /**
673
+ * The single error class thrown by baerly-storage. Discriminate by `code`, not
667
674
  * `instanceof`: the code string survives `JSON.stringify` round-trips
668
675
  * across Worker / iframe / postMessage realm boundaries, where each
669
676
  * realm has its own `BaerlyError` constructor identity. IDB-restored
@@ -676,7 +683,7 @@ const scaffold = async (opts) => {
676
683
  * await db.collection("tickets").insert({ title: "hi" });
677
684
  * } catch (err) {
678
685
  * if (err instanceof BaerlyError && err.code === "Conflict") {
679
- * // ... CAS lost; retry
686
+ * // Check err.retriable; CAS can retry, duplicate _id cannot.
680
687
  * }
681
688
  * throw err;
682
689
  * }
@@ -706,13 +713,25 @@ var BaerlyError = class extends Error {
706
713
  * you genuinely need the wire value.
707
714
  */
708
715
  status;
709
- constructor(code, message, cause, issues, status) {
716
+ /** Canonical corrective action; crosses the wire via `HttpErrorEnvelope.error.resolution`. */
717
+ resolution;
718
+ retriableOverride;
719
+ /**
720
+ * Whether this error instance is retriable. Defaults from `code`, but
721
+ * context-sensitive throw sites can override it.
722
+ */
723
+ get retriable() {
724
+ return this.retriableOverride ?? isRetriableCode(this.code);
725
+ }
726
+ constructor(code, message, cause, issues, status, resolution, retriableOverride) {
710
727
  super(message);
711
728
  this.name = "BaerlyError";
712
729
  this.code = code;
713
730
  this.cause = cause;
731
+ this.retriableOverride = retriableOverride;
714
732
  if (issues !== void 0) this.issues = issues;
715
733
  if (status !== void 0) this.status = status;
734
+ if (resolution !== void 0) this.resolution = resolution;
716
735
  }
717
736
  };
718
737
  //#endregion
@@ -2128,17 +2147,18 @@ const defaultInstaller = { run: (pm, cwd) => new Promise((resolve, reject) => {
2128
2147
  //#endregion
2129
2148
  //#region src/agent-rules.ts
2130
2149
  /**
2131
- * The `--with=agent-rules` bolt-on add-on. Drops a delimited block
2132
- * that tells the user's AI agent (Claude / Cursor / Copilot) that
2150
+ * Bolt-on agent-rules writer. The CLI enables this by default for
2151
+ * existing wrangler projects and lets users opt out with
2152
+ * `--no-agent-rules`. It drops a delimited block that tells the user's
2153
+ * AI agent (Claude / Cursor / Copilot) that
2133
2154
  * `node_modules/@gusto/baerly-storage/dist/API.md` is the canonical API
2134
- * surface and that the patterns those agents' training data is
2135
- * densest with (Postgres/Prisma/Drizzle, Mongo, Firebase) don't
2136
- * apply.
2155
+ * surface and that the patterns those agents' training data is densest
2156
+ * with (Postgres/Prisma/Drizzle, Mongo, Firebase) don't apply.
2137
2157
  *
2138
2158
  * Scaffolded apps already get this via the per-template `AGENTS.md`
2139
2159
  * shipped by `scaffold()`. This module is for the bolt-on path —
2140
- * `pnpm create baerly .` in an existing wrangler project — where no
2141
- * template file is copied.
2160
+ * `pnpm create @gusto/baerly-storage@latest .` in an existing wrangler
2161
+ * project — where no template file is copied.
2142
2162
  *
2143
2163
  * Target detection (first match wins):
2144
2164
  * 1. `.claude/rules/` exists → write `.claude/rules/baerly.md`.
@@ -2168,6 +2188,8 @@ This repo uses @gusto/baerly-storage. Before writing or modifying storage code:
2168
2188
  - Read **\`node_modules/@gusto/baerly-storage/dist/API.md\`** — public-API
2169
2189
  quickref. Every method, every error code, every example. If a
2170
2190
  pattern you want to use is not here, it does not exist in baerly.
2191
+ - Common mistakes keyed by the exact error string live in
2192
+ \`node_modules/@gusto/baerly-storage/dist/RECIPES.md\`.
2171
2193
  - Type contracts live in \`node_modules/@gusto/baerly-storage/dist/*.d.ts\`.
2172
2194
  The whole API is \`Db\`, \`Collection<T>\`, \`Query<T>\`, and \`Predicate<T>\`.
2173
2195
 
@@ -2292,7 +2314,7 @@ const writeAgentRulesBlock = async (outDir) => {
2292
2314
  * written.
2293
2315
  */
2294
2316
  const DEV_SHARED_SECRET = "dev-shared-secret";
2295
- const GITIGNORE_DEV_VARS_ALIASES = new Set([
2317
+ const GITIGNORE_DEV_VARS_ALIASES = /* @__PURE__ */ new Set([
2296
2318
  ".dev.vars",
2297
2319
  ".dev.vars*",
2298
2320
  ".env*.local",
@@ -2469,8 +2491,12 @@ const CREATE_BAERLY_ARGS = {
2469
2491
  },
2470
2492
  with: {
2471
2493
  type: "string",
2472
- description: "Comma-separated add-ons. Scaffold mode: \"docker\" (requires --target=node). Bolt-on mode (`create baerly .` in an existing wrangler project): \"agent-rules\" drops an AGENTS.md block pointing at the baerly API surface.",
2473
- valueHint: "docker|agent-rules"
2494
+ description: "Comma-separated add-ons. Scaffold mode: \"docker\" (requires --target=node). The bolt-on AGENTS.md block is its own default-on flag — see --agent-rules.",
2495
+ valueHint: "docker"
2496
+ },
2497
+ "agent-rules": {
2498
+ type: "boolean",
2499
+ description: "Bolt-on mode: write/refresh the AGENTS.md router pointing at the baerly API (default true; negate with --no-agent-rules). Ignored in scaffold mode."
2474
2500
  },
2475
2501
  pm: {
2476
2502
  type: "string",
@@ -2574,7 +2600,7 @@ const handleCreateBaerly = async (args, opts = {}) => {
2574
2600
  json: jsonMode,
2575
2601
  isInteractive,
2576
2602
  installer: opts.installer,
2577
- agentRules: withAddonsFromFlag?.includes("agent-rules") === true
2603
+ agentRules: args["agent-rules"] !== false
2578
2604
  });
2579
2605
  projectName = w.projectName;
2580
2606
  target = w.target;
@@ -2596,7 +2622,7 @@ const handleCreateBaerly = async (args, opts = {}) => {
2596
2622
  json: jsonMode,
2597
2623
  isInteractive,
2598
2624
  installer: opts.installer,
2599
- agentRules: withAddonsFromFlag?.includes("agent-rules") === true
2625
+ agentRules: args["agent-rules"] !== false
2600
2626
  });
2601
2627
  if (args.target === void 0) throw new Error(`--target is required when not running in wizard mode (got undefined)`);
2602
2628
  target = args.target;
@@ -2606,7 +2632,7 @@ const handleCreateBaerly = async (args, opts = {}) => {
2606
2632
  git = args.git === true;
2607
2633
  }
2608
2634
  if (withAddons.includes("docker") && target !== "node") throw new Error(`--with=docker only applies to --target=node. The Docker add-on adds a Dockerfile to the Node template. Use --target=node --with=docker, or drop --with=docker to scaffold for --target=${target}.`);
2609
- if (withAddons.includes("agent-rules")) throw new Error("--with=agent-rules only applies to bolt-on mode (pnpm create baerly . inside an existing wrangler project). Scaffolded apps already include an AGENTS.md preamble — drop --with=agent-rules.");
2635
+ if (withAddons.includes("agent-rules")) throw new Error("--with=agent-rules is unnecessary in scaffold mode. Bolt-on mode writes agent rules by default and supports --no-agent-rules to opt out. Scaffolded apps already include an AGENTS.md preamble — drop --with=agent-rules.");
2610
2636
  const result = await scaffold({
2611
2637
  projectName,
2612
2638
  target,