rev-dal 0.0.0-stage → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/AGENTS.md +234 -0
  2. package/CONTRIBUTING.md +83 -0
  3. package/LICENSE +121 -0
  4. package/README.md +67 -2
  5. package/dist/index.d.ts +48 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +26 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/lib/clone.d.ts +24 -0
  10. package/dist/lib/clone.d.ts.map +1 -0
  11. package/dist/lib/clone.js +83 -0
  12. package/dist/lib/clone.js.map +1 -0
  13. package/dist/lib/create-model.d.ts +207 -0
  14. package/dist/lib/create-model.d.ts.map +1 -0
  15. package/dist/lib/create-model.js +230 -0
  16. package/dist/lib/create-model.js.map +1 -0
  17. package/dist/lib/data-access-layer.d.ts +140 -0
  18. package/dist/lib/data-access-layer.d.ts.map +1 -0
  19. package/dist/lib/data-access-layer.js +389 -0
  20. package/dist/lib/data-access-layer.js.map +1 -0
  21. package/dist/lib/errors.d.ts +139 -0
  22. package/dist/lib/errors.d.ts.map +1 -0
  23. package/dist/lib/errors.js +206 -0
  24. package/dist/lib/errors.js.map +1 -0
  25. package/dist/lib/filter-where.d.ts +199 -0
  26. package/dist/lib/filter-where.d.ts.map +1 -0
  27. package/dist/lib/filter-where.js +563 -0
  28. package/dist/lib/filter-where.js.map +1 -0
  29. package/dist/lib/ml-string.d.ts +63 -0
  30. package/dist/lib/ml-string.d.ts.map +1 -0
  31. package/dist/lib/ml-string.js +331 -0
  32. package/dist/lib/ml-string.js.map +1 -0
  33. package/dist/lib/model-factory.d.ts +25 -0
  34. package/dist/lib/model-factory.d.ts.map +1 -0
  35. package/dist/lib/model-factory.js +57 -0
  36. package/dist/lib/model-factory.js.map +1 -0
  37. package/dist/lib/model-handle.d.ts +67 -0
  38. package/dist/lib/model-handle.d.ts.map +1 -0
  39. package/dist/lib/model-handle.js +182 -0
  40. package/dist/lib/model-handle.js.map +1 -0
  41. package/dist/lib/model-initializer.d.ts +96 -0
  42. package/dist/lib/model-initializer.d.ts.map +1 -0
  43. package/dist/lib/model-initializer.js +182 -0
  44. package/dist/lib/model-initializer.js.map +1 -0
  45. package/dist/lib/model-manifest.d.ts +112 -0
  46. package/dist/lib/model-manifest.d.ts.map +1 -0
  47. package/dist/lib/model-manifest.js +2 -0
  48. package/dist/lib/model-manifest.js.map +1 -0
  49. package/dist/lib/model-registry.d.ts +81 -0
  50. package/dist/lib/model-registry.d.ts.map +1 -0
  51. package/dist/lib/model-registry.js +139 -0
  52. package/dist/lib/model-registry.js.map +1 -0
  53. package/dist/lib/model-types.d.ts +380 -0
  54. package/dist/lib/model-types.d.ts.map +1 -0
  55. package/dist/lib/model-types.js +2 -0
  56. package/dist/lib/model-types.js.map +1 -0
  57. package/dist/lib/model.d.ts +503 -0
  58. package/dist/lib/model.d.ts.map +1 -0
  59. package/dist/lib/model.js +1423 -0
  60. package/dist/lib/model.js.map +1 -0
  61. package/dist/lib/postgres-config.d.ts +3 -0
  62. package/dist/lib/postgres-config.d.ts.map +1 -0
  63. package/dist/lib/postgres-config.js +2 -0
  64. package/dist/lib/postgres-config.js.map +1 -0
  65. package/dist/lib/query-builder.d.ts +607 -0
  66. package/dist/lib/query-builder.d.ts.map +1 -0
  67. package/dist/lib/query-builder.js +1734 -0
  68. package/dist/lib/query-builder.js.map +1 -0
  69. package/dist/lib/revision.d.ts +55 -0
  70. package/dist/lib/revision.d.ts.map +1 -0
  71. package/dist/lib/revision.js +307 -0
  72. package/dist/lib/revision.js.map +1 -0
  73. package/dist/lib/runtime.d.ts +18 -0
  74. package/dist/lib/runtime.d.ts.map +1 -0
  75. package/dist/lib/runtime.js +134 -0
  76. package/dist/lib/runtime.js.map +1 -0
  77. package/dist/lib/transaction.d.ts +17 -0
  78. package/dist/lib/transaction.d.ts.map +1 -0
  79. package/dist/lib/transaction.js +30 -0
  80. package/dist/lib/transaction.js.map +1 -0
  81. package/dist/lib/type.d.ts +130 -0
  82. package/dist/lib/type.d.ts.map +1 -0
  83. package/dist/lib/type.js +376 -0
  84. package/dist/lib/type.js.map +1 -0
  85. package/docs/models.md +406 -0
  86. package/docs/multilingual-text.md +142 -0
  87. package/docs/queries.md +374 -0
  88. package/docs/revisions.md +61 -0
  89. package/package.json +60 -4
  90. package/releases/0.2.0.md +18 -0
  91. package/releases/0.2.1.md +22 -0
  92. package/releases/README.md +7 -0
  93. package/setup-db-grants.sql +51 -0
package/AGENTS.md ADDED
@@ -0,0 +1,234 @@
1
+ # Local agent instructions
2
+
3
+ ## Documentation maintenance
4
+
5
+ Keep documentation up to date in the same change as the implementation. When
6
+ changing public APIs, behavior, configuration, or development workflows, update
7
+ the relevant documentation:
8
+
9
+ - `README.md`: overview, requirements, installation, quick start, and links.
10
+ - `docs/models.md`: bootstrap, manifests, model methods, typing, cross-model
11
+ references, logging, database grants, and runtime architecture.
12
+ - `docs/queries.md`: filtering, projections, aggregates, joins, deletion, and
13
+ relation loading and writes.
14
+ - `docs/revisions.md`: revision workflows, edit conflicts, and transactions.
15
+ - `docs/multilingual-text.md`: schemas, language providers, escaping, cleanup,
16
+ and rendering responsibilities.
17
+ - `CONTRIBUTING.md`: development setup, test database configuration, checks,
18
+ and the source directory map.
19
+
20
+ Keep the README concise and link to the topic guides for details. Give each
21
+ behavior one authoritative explanation and link to it instead of duplicating
22
+ the contract across pages. Distinguish guarantees made by rev-dal from
23
+ conventions or helpers supplied by consuming applications.
24
+
25
+ Check examples against the implementation and types. Verify affected relative
26
+ links and heading anchors after moving or renaming documentation. Keep new
27
+ documentation included in the package through `package.json`'s `files` list.
28
+ Update `TODO.md` when implementing an item listed there.
29
+
30
+ ## Releases
31
+
32
+ Release automation lives in this repository and uses our own scripts, official
33
+ `actions/*` GitHub actions, and the standard `npm`, `git`, and `gh` CLIs. Do not
34
+ introduce Release Please, semantic-release, Changesets, or third-party release
35
+ actions. Use `mise exec --` for local Node/npm commands.
36
+
37
+ ### Version and API policy
38
+
39
+ - Release stable versions only. The first npm release is `0.2.1`; `0.1.0` was
40
+ a development placeholder and `0.2.0` failed before publication.
41
+ - Before 1.0, compatible fixes and additions increment the patch version;
42
+ breaking changes increment the minor version. Choose `1.0.0` explicitly when
43
+ committing to a stable API. Afterwards use normal major/minor/patch semantics.
44
+ - Compatibility includes runtime behavior, TypeScript declarations, all exported
45
+ `rev-dal/lib/*` subpaths, the SQL setup export, and database requirements.
46
+ - Record changes and migration instructions in `releases/<version>.md`. Explain
47
+ database or grant changes explicitly; declaring models does not migrate tables.
48
+ - Versions, released notes, and `v<version>` tags are immutable. Fix an already
49
+ published package with a new release. Never force a tag or unpublish to retry.
50
+ - Keep the Git-install `prepare` build until consumers have migrated. Registry
51
+ packages contain built JavaScript and declarations and require no consumer build.
52
+
53
+ ### One-time setup
54
+
55
+ 1. Merge the workflow setup into `main` so manual dispatch is available. No npm
56
+ package is published merely by merging the setup: the version stays `0.1.0`.
57
+ 2. Enable Actions and allow GitHub Actions to create pull requests in repository
58
+ Settings → Actions → General. Workflows explicitly request their permissions;
59
+ no PAT, GitHub App, or cross-repository write token is required. Only the first
60
+ npm publication needs a temporary npm bootstrap token; routine releases use OIDC.
61
+ 3. Protect `main` against direct pushes and require the `lint`, `test`,
62
+ `consumers (lib.reviews)`, and `consumers (agpwiki)` checks. Permit the chosen
63
+ merge method (squash or merge commits both work). Confirm the check names in
64
+ the first run before adding requirements. The release PR is the review point;
65
+ do not add a recurring environment approval if unattended publishing is wanted.
66
+ 4. Establish ownership of the unscoped npm package `rev-dal` using the owner's
67
+ npm account. A registry 404 does not reserve the name. If it is unavailable,
68
+ change the package name and scripts consistently before releasing; do not
69
+ silently publish under another name.
70
+ 5. For a brand-new package, the npm account owner creates a temporary granular
71
+ access token on npmjs.com (account menu → Access Tokens → Generate New Token).
72
+ Use package permission **Read and write (publish and stage)**, enable **Bypass
73
+ two-factor authentication**, and choose the shortest practical expiry (one day
74
+ suffices). An unscoped package that does not exist cannot be selected individually;
75
+ use **All Packages** for this bootstrap token. Organization-management access
76
+ is unnecessary. The owner must complete npm's account authentication personally.
77
+ Agents must not request the token value in chat, print it, or commit it.
78
+
79
+ Store it as the repository Actions secret `NPM_BOOTSTRAP_TOKEN`, using GitHub
80
+ Settings → Secrets and variables → Actions, or this interactive command:
81
+
82
+ ```bash
83
+ gh secret set NPM_BOOTSTRAP_TOKEN --repo permacommons/rev-dal
84
+ ```
85
+
86
+ Prepare the first release (`0.2.1`), verify its PR checks, and merge when authorized.
87
+ Only its final publish step receives this secret as `NODE_AUTH_TOKEN`; neither
88
+ package tests nor consumer jobs receive it. npm attempts OIDC first and falls
89
+ back to the token before a trusted publisher exists. The workflow publishes the
90
+ exact tested tarball, with provenance, then creates its tag and GitHub release.
91
+ The secret is eligible only for version `0.2.1`; later versions cannot use it.
92
+ If publication already failed before the secret was added, use the retry procedure.
93
+ 6. After the first publication succeeds, remove the GitHub secret and revoke the
94
+ temporary token in npm. Agents can remove the repository secret without reading
95
+ its value; the account owner can revoke the npm token:
96
+
97
+ ```bash
98
+ gh secret delete NPM_BOOTSTRAP_TOKEN --repo permacommons/rev-dal
99
+ ```
100
+
101
+ 7. Shortly before the next release, configure npm's `rev-dal` package Settings →
102
+ Trusted Publisher to authorize GitHub Actions:
103
+ owner `permacommons`, repository `rev-dal`, workflow filename `publish.yml`,
104
+ no environment. Enable direct `npm publish`; staged-only authorization would
105
+ require interactive approvals. The workflow uses GitHub-hosted runners, npm 11,
106
+ and `id-token: write`. Routine OIDC publication includes provenance.
107
+ A new trusted publisher must successfully publish within two days or it expires;
108
+ configure it when the next release is ready. A retry of an existing version only
109
+ verifies registry integrity and does not validate the OIDC configuration. Later
110
+ versions publish through OIDC without the bootstrap secret.
111
+
112
+ References: [npm token setup](https://docs.npmjs.com/creating-and-viewing-access-tokens/)
113
+ and [npm trusted publishing](https://docs.npmjs.com/trusted-publishers/).
114
+
115
+ ### Prepare and release
116
+
117
+ Run from this repository, or supply `--repo permacommons/rev-dal` to `gh`:
118
+
119
+ ```bash
120
+ gh workflow run prepare-release.yml --ref main \
121
+ -f version=0.2.1 \
122
+ -f notes='First versioned release. Existing rev-dal imports remain supported.'
123
+ ```
124
+
125
+ `version` accepts `patch`, `minor`, `major`, or an explicit increasing `X.Y.Z`.
126
+ Choose the bump based on the policy above; no commit-message parser decides it.
127
+ The `notes` input accepts multiline Markdown and must describe the actual release.
128
+ Do not use the example notes without reviewing the changes since the consumer pins.
129
+
130
+ `prepare-release.yml` reads current `main`, updates package.json and package-lock.json
131
+ without lifecycle scripts, writes release notes, pushes `release/v<version>`, and
132
+ opens a PR. It explicitly dispatches `lint.yml` and `tests.yml` on that branch.
133
+ This avoids relying on PR events generated by `GITHUB_TOKEN`. The dispatched runs
134
+ attach to the branch's current commit; verify the checks refer to the PR head SHA.
135
+
136
+ Inspect the diff, notes, workflow results, and any migration instructions, then
137
+ merge the passing PR when authorized to release. `publish.yml` detects a version
138
+ change on `main` and validates the exact merged commit before publishing. Non-version
139
+ package metadata changes do not publish. Both workflows only start on `main`.
140
+
141
+ ### Validation and artifacts
142
+
143
+ `tests.yml` is both ordinary CI and a reusable release validation workflow:
144
+
145
+ 1. Run rev-dal's runtime/PostgreSQL tests, type tests, release-tool tests, lint,
146
+ typecheck, and build on Node 22.
147
+ 2. Pack once with `npm pack --ignore-scripts`. Install the tarball in a clean
148
+ temporary project with install scripts disabled; check runtime exports, SQL
149
+ resolution, and declaration imports.
150
+ 3. Upload `rev-dal-package` containing `rev-dal.tgz` (retained for 14 days), with a
151
+ SHA-512 in the job summary.
152
+ 4. Test that same tarball against lib.reviews and agpwiki `main` in disposable
153
+ runner checkouts using Node 24 and PostgreSQL 16. Record each checkout SHA,
154
+ run full typechecks and tests, and block publication if either fails.
155
+
156
+ The consumer jobs do not push changes, edit consumer repositories, or use local
157
+ consumer checkouts. Their temporary dependency install affects only the runner.
158
+ Consumer `main` is resolved per validation run; retries record any newly tested SHAs.
159
+ Their required database setup currently mirrors their own CI. Update this workflow
160
+ when consumer requirements change. A consumer CI failure is a release blocker;
161
+ do not bypass it or change consumer code as part of a release without authorization.
162
+
163
+ Resolve tarball paths to absolute paths before invoking npm; a bare relative
164
+ `directory/file.tgz` can be interpreted as a GitHub dependency.
165
+
166
+ Publication downloads the validated artifact, uses `npm publish` with OIDC (or the
167
+ temporary token for the first `0.2.1` publication) and provenance, verifies registry
168
+ integrity, then creates the immutable tag and GitHub
169
+ release from the checked-in notes. The publish job does not rebuild the package.
170
+ The summary includes version, commit, integrity, and publication URLs. No consumer
171
+ dependency update or deployment happens automatically in this initial setup.
172
+
173
+ ### Recovery
174
+
175
+ Preparation and publication each serialize their runs. Different unpublished
176
+ release PRs can still exist: merge them in version order and close stale ones.
177
+
178
+ - If preparation fails after pushing its branch or opening the PR, preserve that
179
+ work. Locate the existing `release/v<version>` branch/PR; do not force-push or
180
+ blindly rerun preparation. Dispatch any missing checks on that branch:
181
+
182
+ ```bash
183
+ gh workflow run lint.yml --ref release/v0.2.1
184
+ gh workflow run tests.yml --ref release/v0.2.1
185
+ ```
186
+
187
+ - After editing a bot-created PR, dispatch both checks again at its new head.
188
+ Never treat passing checks on an earlier SHA as sufficient.
189
+ - To retry publication, use the full SHA of the merged main commit that introduced
190
+ the version, not the release branch head or current main after later changes:
191
+
192
+ ```bash
193
+ gh workflow run publish.yml --ref main -f commit=<40-character-merged-sha>
194
+ gh run list --workflow publish.yml
195
+ gh run watch <original-push-run-id> --exit-status
196
+ ```
197
+
198
+ Manual dispatch queues a complete rerun of the original push workflow for that
199
+ commit; its summary links to that original run. Watch the original run after its
200
+ new attempt starts, rather than only watching the short dispatch run. This
201
+ preserves the original `GITHUB_SHA` for accurate npm provenance even if main
202
+ has advanced. A checkout of an older commit in a new main run would attest the
203
+ wrong source identity, so the publish script rejects such mismatches.
204
+
205
+ The target must belong to main's first-parent history, introduce an increasing
206
+ version, and have matching manifests and version-specific notes. Validation runs again. Existing
207
+ tags must point to that exact commit; an existing registry package must match
208
+ the tested tarball's SHA-512. Matching publication steps are skipped and missing
209
+ steps are completed. Conflicting bytes or tags fail rather than overwrite.
210
+ Revalidation replaces the earlier run artifact with the newly tested tarball.
211
+ A retry cannot publish an absent older version over a newer npm `latest`.
212
+ - If the tarball cannot be reproduced, investigate the build inputs; never waive
213
+ the integrity check. If publication failed before npm accepted the package and
214
+ source changes are required, prepare a new version.
215
+ - Consumer updates are deferred. When authorized, migrate each consumer to an
216
+ exact published version and commit its lockfile; keep deployment on `npm ci`.
217
+
218
+ ### Local checks
219
+
220
+ ```bash
221
+ mise exec -- npm run test:release
222
+ mise exec -- npm run lint
223
+ mise exec -- npm run typecheck
224
+ mise exec -- npm run test:types
225
+ mise exec -- npm test
226
+ mise exec -- npm run build
227
+ artifact_dir=$(mktemp -d)
228
+ mise exec -- npm pack --ignore-scripts --pack-destination "$artifact_dir"
229
+ mise exec -- node scripts/check-package.mjs "$artifact_dir/rev-dal-0.2.1.tgz"
230
+ ```
231
+
232
+ Use the current package version in the tarball filename. The package smoke check
233
+ installs runtime dependencies from npm into a temporary directory, then removes it.
234
+ The runtime tests require the test database configuration in CONTRIBUTING.md.
@@ -0,0 +1,83 @@
1
+ # Contributing
2
+
3
+ See the [README](README.md) for an introduction and the [model guide](docs/models.md)
4
+ for bootstrap and runtime architecture.
5
+
6
+ ## Development setup
7
+
8
+ Use Node.js 22 or later and PostgreSQL for integration tests. Install dependencies
9
+ and build the library:
10
+
11
+ ```bash
12
+ npm install
13
+ npm run build
14
+ ```
15
+
16
+ Before submitting changes, run:
17
+
18
+ ```bash
19
+ npm run lint
20
+ npm run typecheck
21
+ npm run test:types
22
+ npm run test:release
23
+ npm test
24
+ ```
25
+
26
+ ## Testing
27
+
28
+ rev-dal uses Node's built-in test runner with `tsx` to execute TypeScript tests.
29
+
30
+ ```bash
31
+ npm test
32
+ ```
33
+
34
+ ### Test Database Setup
35
+
36
+ Integration tests require PostgreSQL with the `pgcrypto` extension available. The test harness
37
+ creates a fresh schema per run and cleans it up afterwards.
38
+
39
+ Provide a connection string via `REV_DAL_TEST_DATABASE_URL`:
40
+
41
+ ```bash
42
+ export REV_DAL_TEST_DATABASE_URL="postgres://rev_dal_test_user:rev_dal_password@localhost:5432/rev_dal_test"
43
+ npm test
44
+ ```
45
+
46
+ If your test user cannot create extensions, run this once as a superuser:
47
+
48
+ ```bash
49
+ psql -d rev_dal_test -c 'CREATE EXTENSION IF NOT EXISTS "pgcrypto";'
50
+ ```
51
+
52
+ ### Revision Tests
53
+
54
+ The shared `revisions` fixture in `test/helpers/revision-helpers.ts` includes JSONB,
55
+ array and timestamp columns, and `createTestDocumentWithRevisions()` edits them in
56
+ place the way applications do. When changing how rows are read, written or copied,
57
+ check history with `assertArchivedRevisionMatches()`, which compares an archived
58
+ revision with the stored row from before the edit.
59
+
60
+ ### Type Tests
61
+
62
+ The source, declaration build, and type tests enable `strictNullChecks`. Other
63
+ strict-mode checks are not yet enabled.
64
+
65
+ Type-level checks live under `test/types/` and are compiled with a dedicated config:
66
+
67
+ ```bash
68
+ npm run test:types
69
+ ```
70
+
71
+ ## Directory Reference
72
+
73
+ - `src/index.ts` – Public entry point that re-exports constructors, types, and helpers.
74
+ - `src/lib/` – Core implementation (connection management, manifests, query builder, filters, revision system, schema types).
75
+ - `setup-db-grants.sql` – Grants applied to shared environments.
76
+
77
+ ## Releases
78
+
79
+ Use the repository's own GitHub Actions workflows to prepare a release PR, test
80
+ the packed library against both consumers, and publish through npm trusted
81
+ publishing. See the [release runbook](AGENTS.md#releases) for the version policy,
82
+ one-time npm setup, dispatch commands, and retry procedures. Release-tool tests
83
+ run with `npm run test:release` and do not require PostgreSQL.
package/LICENSE ADDED
@@ -0,0 +1,121 @@
1
+ Creative Commons Legal Code
2
+
3
+ CC0 1.0 Universal
4
+
5
+ CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
6
+ LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
7
+ ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
8
+ INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
9
+ REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
10
+ PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
11
+ THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
12
+ HEREUNDER.
13
+
14
+ Statement of Purpose
15
+
16
+ The laws of most jurisdictions throughout the world automatically confer
17
+ exclusive Copyright and Related Rights (defined below) upon the creator
18
+ and subsequent owner(s) (each and all, an "owner") of an original work of
19
+ authorship and/or a database (each, a "Work").
20
+
21
+ Certain owners wish to permanently relinquish those rights to a Work for
22
+ the purpose of contributing to a commons of creative, cultural and
23
+ scientific works ("Commons") that the public can reliably and without fear
24
+ of later claims of infringement build upon, modify, incorporate in other
25
+ works, reuse and redistribute as freely as possible in any form whatsoever
26
+ and for any purposes, including without limitation commercial purposes.
27
+ These owners may contribute to the Commons to promote the ideal of a free
28
+ culture and the further production of creative, cultural and scientific
29
+ works, or to gain reputation or greater distribution for their Work in
30
+ part through the use and efforts of others.
31
+
32
+ For these and/or other purposes and motivations, and without any
33
+ expectation of additional consideration or compensation, the person
34
+ associating CC0 with a Work (the "Affirmer"), to the extent that he or she
35
+ is an owner of Copyright and Related Rights in the Work, voluntarily
36
+ elects to apply CC0 to the Work and publicly distribute the Work under its
37
+ terms, with knowledge of his or her Copyright and Related Rights in the
38
+ Work and the meaning and intended legal effect of CC0 on those rights.
39
+
40
+ 1. Copyright and Related Rights. A Work made available under CC0 may be
41
+ protected by copyright and related or neighboring rights ("Copyright and
42
+ Related Rights"). Copyright and Related Rights include, but are not
43
+ limited to, the following:
44
+
45
+ i. the right to reproduce, adapt, distribute, perform, display,
46
+ communicate, and translate a Work;
47
+ ii. moral rights retained by the original author(s) and/or performer(s);
48
+ iii. publicity and privacy rights pertaining to a person's image or
49
+ likeness depicted in a Work;
50
+ iv. rights protecting against unfair competition in regards to a Work,
51
+ subject to the limitations in paragraph 4(a), below;
52
+ v. rights protecting the extraction, dissemination, use and reuse of data
53
+ in a Work;
54
+ vi. database rights (such as those arising under Directive 96/9/EC of the
55
+ European Parliament and of the Council of 11 March 1996 on the legal
56
+ protection of databases, and under any national implementation
57
+ thereof, including any amended or successor version of such
58
+ directive); and
59
+ vii. other similar, equivalent or corresponding rights throughout the
60
+ world based on applicable law or treaty, and any national
61
+ implementations thereof.
62
+
63
+ 2. Waiver. To the greatest extent permitted by, but not in contravention
64
+ of, applicable law, Affirmer hereby overtly, fully, permanently,
65
+ irrevocably and unconditionally waives, abandons, and surrenders all of
66
+ Affirmer's Copyright and Related Rights and associated claims and causes
67
+ of action, whether now known or unknown (including existing as well as
68
+ future claims and causes of action), in the Work (i) in all territories
69
+ worldwide, (ii) for the maximum duration provided by applicable law or
70
+ treaty (including future time extensions), (iii) in any current or future
71
+ medium and for any number of copies, and (iv) for any purpose whatsoever,
72
+ including without limitation commercial, advertising or promotional
73
+ purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
74
+ member of the public at large and to the detriment of Affirmer's heirs and
75
+ successors, fully intending that such Waiver shall not be subject to
76
+ revocation, rescission, cancellation, termination, or any other legal or
77
+ equitable action to disrupt the quiet enjoyment of the Work by the public
78
+ as contemplated by Affirmer's express Statement of Purpose.
79
+
80
+ 3. Public License Fallback. Should any part of the Waiver for any reason
81
+ be judged legally invalid or ineffective under applicable law, then the
82
+ Waiver shall be preserved to the maximum extent permitted taking into
83
+ account Affirmer's express Statement of Purpose. In addition, to the
84
+ extent the Waiver is so judged Affirmer hereby grants to each affected
85
+ person a royalty-free, non transferable, non sublicensable, non exclusive,
86
+ irrevocable and unconditional license to exercise Affirmer's Copyright and
87
+ Related Rights in the Work (i) in all territories worldwide, (ii) for the
88
+ maximum duration provided by applicable law or treaty (including future
89
+ time extensions), (iii) in any current or future medium and for any number
90
+ of copies, and (iv) for any purpose whatsoever, including without
91
+ limitation commercial, advertising or promotional purposes (the
92
+ "License"). The License shall be deemed effective as of the date CC0 was
93
+ applied by Affirmer to the Work. Should any part of the License for any
94
+ reason be judged legally invalid or ineffective under applicable law, such
95
+ partial invalidity or ineffectiveness shall not invalidate the remainder
96
+ of the License, and in such case Affirmer hereby affirms that he or she
97
+ will not (i) exercise any of his or her remaining Copyright and Related
98
+ Rights in the Work or (ii) assert any associated claims and causes of
99
+ action with respect to the Work, in either case contrary to Affirmer's
100
+ express Statement of Purpose.
101
+
102
+ 4. Limitations and Disclaimers.
103
+
104
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
105
+ surrendered, licensed or otherwise affected by this document.
106
+ b. Affirmer offers the Work as-is and makes no representations or
107
+ warranties of any kind concerning the Work, express, implied,
108
+ statutory or otherwise, including without limitation warranties of
109
+ title, merchantability, fitness for a particular purpose, non
110
+ infringement, or the absence of latent or other defects, accuracy, or
111
+ the present or absence of errors, whether or not discoverable, all to
112
+ the greatest extent permissible under applicable law.
113
+ c. Affirmer disclaims responsibility for clearing rights of other persons
114
+ that may apply to the Work or any use thereof, including without
115
+ limitation any person's Copyright and Related Rights in the Work.
116
+ Further, Affirmer disclaims responsibility for obtaining any necessary
117
+ consents, permissions or other rights required for any use of the
118
+ Work.
119
+ d. Affirmer understands and acknowledges that Creative Commons is not a
120
+ party to this document and has no duty or obligation with respect to
121
+ this CC0 or use of the Work.
package/README.md CHANGED
@@ -1,3 +1,68 @@
1
- # Temporary Holding Version
1
+ # rev-dal
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ rev-dal is a TypeScript-first PostgreSQL data access layer with built-in revision support and
4
+ rich model typing (including multilingual string helpers).
5
+
6
+ It is currently internally used in the [Permacommons](https://permacommons.org/)
7
+ project family for two projects:
8
+
9
+ - https://lib.reviews/ - review anything in any language
10
+ - https://agpedia.org/ - an encyclopedia and knowledge repository managed
11
+ via MCP (AI agents)
12
+
13
+ The need for consistent wiki-style revision metadata motivated the creation
14
+ of a custom DAL.
15
+
16
+ ## Requirements
17
+
18
+ Node.js 22 or later and PostgreSQL.
19
+
20
+ ## Install
21
+
22
+ ```bash
23
+ npm install --save-exact rev-dal@0.2.1
24
+ ```
25
+
26
+ Use an exact release and commit your package lockfile. For unreleased development
27
+ changes, Git installations remain supported.
28
+
29
+ ## Quick start
30
+
31
+ Create a DAL with your application's PostgreSQL connection settings:
32
+
33
+ ```ts
34
+ import createDataAccessLayer from 'rev-dal';
35
+
36
+ const dal = createDataAccessLayer({
37
+ connectionString: process.env.DATABASE_URL,
38
+ });
39
+ await dal.connect();
40
+
41
+ try {
42
+ const result = await dal.query('SELECT NOW() AS now');
43
+ console.log(result.rows[0].now);
44
+ } finally {
45
+ await dal.disconnect();
46
+ }
47
+ ```
48
+
49
+ For typed model operations, follow [Models and bootstrap](docs/models.md) to
50
+ register manifests and configure model resolution. Database tables must already
51
+ exist; declaring a model does not create them.
52
+
53
+ ## Documentation
54
+
55
+ - [Models and bootstrap](docs/models.md) — lifecycle, per-DAL model lookup,
56
+ manifests, methods, typing, cross-model references, logging, and database grants.
57
+ - [Queries and relations](docs/queries.md) — filtering, projections, aggregates,
58
+ joins, deletion, and loading or writing associations.
59
+ - [Revisions](docs/revisions.md) — revision history, edit conflicts, and transactions.
60
+ - [Multilingual text](docs/multilingual-text.md) — schemas, language providers,
61
+ escaping, cleanup, and rendering responsibilities.
62
+
63
+ ## Development
64
+
65
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for setup, test database configuration,
66
+ checks, and the source directory map.
67
+ See the [release runbook](AGENTS.md#releases) for versioning, automated releases,
68
+ and npm publisher setup.
@@ -0,0 +1,48 @@
1
+ import DataAccessLayer from './lib/data-access-layer.js';
2
+ import Errors from './lib/errors.js';
3
+ import mlString from './lib/ml-string.js';
4
+ import Model from './lib/model.js';
5
+ import type { PostgresConfig } from './lib/postgres-config.js';
6
+ import type { JoinApplyBuilder, JoinOptions, JoinRelationOptions } from './lib/query-builder.js';
7
+ import QueryBuilder from './lib/query-builder.js';
8
+ import revision from './lib/revision.js';
9
+ import type { DalDebugLogger, DalLanguageProvider } from './lib/runtime.js';
10
+ import { setDebugLogger, setLanguageProvider, setRevisionSummaryEnabled } from './lib/runtime.js';
11
+ import types from './lib/type.js';
12
+ /**
13
+ * Data Access Layer (DAL) for PostgreSQL
14
+ *
15
+ * This module provides the main entry point for the PostgreSQL DAL,
16
+ * abstracting database operations and providing a consistent interface
17
+ * for all database interactions.
18
+ */
19
+ type DataAccessLayerClass = typeof DataAccessLayer;
20
+ type DataAccessLayerInstance = InstanceType<DataAccessLayerClass>;
21
+ export type DalContext = DataAccessLayerInstance & {
22
+ cleanup?: (options?: {
23
+ dropSchema?: boolean;
24
+ }) => Promise<void>;
25
+ schemaNamespace?: string;
26
+ };
27
+ type CreateDataAccessLayer = ((config?: Partial<PostgresConfig>) => DataAccessLayerInstance) & {
28
+ DataAccessLayer: DataAccessLayerClass;
29
+ Model: typeof Model;
30
+ QueryBuilder: typeof QueryBuilder;
31
+ types: typeof types;
32
+ Errors: typeof Errors;
33
+ mlString: typeof mlString;
34
+ revision: typeof revision;
35
+ };
36
+ /**
37
+ * Create a PostgreSQL Data Access Layer (DAL) instance.
38
+ *
39
+ * @param config Optional PostgreSQL configuration overrides used to initialize the DAL instance.
40
+ * @returns A ready-to-use DAL instance bound to the provided configuration.
41
+ */
42
+ declare const createDataAccessLayer: CreateDataAccessLayer;
43
+ export { DataAccessLayer, Model, QueryBuilder, types, Errors, mlString, revision, setLanguageProvider, setDebugLogger, setRevisionSummaryEnabled, createDataAccessLayer, };
44
+ export type { JoinApplyBuilder, JoinOptions, JoinRelationOptions };
45
+ export type { DalDebugLogger, DalLanguageProvider };
46
+ export type { ProjectedQuery, SelectableData } from './lib/model-types.js';
47
+ export default createDataAccessLayer;
48
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,MAAM,4BAA4B,CAAC;AACzD,OAAO,MAAM,MAAM,iBAAiB,CAAC;AACrC,OAAO,QAAQ,MAAM,oBAAoB,CAAC;AAC1C,OAAO,KAAK,MAAM,gBAAgB,CAAC;AACnC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AACjG,OAAO,YAAY,MAAM,wBAAwB,CAAC;AAClD,OAAO,QAAQ,MAAM,mBAAmB,CAAC;AACzC,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAC5E,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,MAAM,kBAAkB,CAAC;AAClG,OAAO,KAAK,MAAM,eAAe,CAAC;AAElC;;;;;;GAMG;AAEH,KAAK,oBAAoB,GAAG,OAAO,eAAe,CAAC;AAEnD,KAAK,uBAAuB,GAAG,YAAY,CAAC,oBAAoB,CAAC,CAAC;AAElE,MAAM,MAAM,UAAU,GAAG,uBAAuB,GAAG;IACjD,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE;QAAE,UAAU,CAAC,EAAE,OAAO,CAAA;KAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAChE,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAIF,KAAK,qBAAqB,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,KAAK,uBAAuB,CAAC,GAAG;IAC7F,eAAe,EAAE,oBAAoB,CAAC;IACtC,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB,YAAY,EAAE,OAAO,YAAY,CAAC;IAClC,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB,MAAM,EAAE,OAAO,MAAM,CAAC;IACtB,QAAQ,EAAE,OAAO,QAAQ,CAAC;IAC1B,QAAQ,EAAE,OAAO,QAAQ,CAAC;CAC3B,CAAC;AAEF;;;;;GAKG;AACH,QAAA,MAAM,qBAAqB,EACW,qBAAqB,CAAC;AAU5D,OAAO,EACL,eAAe,EACf,KAAK,EACL,YAAY,EACZ,KAAK,EACL,MAAM,EACN,QAAQ,EACR,QAAQ,EACR,mBAAmB,EACnB,cAAc,EACd,yBAAyB,EACzB,qBAAqB,GACtB,CAAC;AAEF,YAAY,EAAE,gBAAgB,EAAE,WAAW,EAAE,mBAAmB,EAAE,CAAC;AACnE,YAAY,EAAE,cAAc,EAAE,mBAAmB,EAAE,CAAC;AACpD,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAE3E,eAAe,qBAAqB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,26 @@
1
+ import DataAccessLayer from './lib/data-access-layer.js';
2
+ import Errors from './lib/errors.js';
3
+ import mlString from './lib/ml-string.js';
4
+ import Model from './lib/model.js';
5
+ import QueryBuilder from './lib/query-builder.js';
6
+ import revision from './lib/revision.js';
7
+ import { setDebugLogger, setLanguageProvider, setRevisionSummaryEnabled } from './lib/runtime.js';
8
+ import types from './lib/type.js';
9
+ const DataAccessLayerCtor = DataAccessLayer;
10
+ /**
11
+ * Create a PostgreSQL Data Access Layer (DAL) instance.
12
+ *
13
+ * @param config Optional PostgreSQL configuration overrides used to initialize the DAL instance.
14
+ * @returns A ready-to-use DAL instance bound to the provided configuration.
15
+ */
16
+ const createDataAccessLayer = ((config) => new DataAccessLayerCtor(config));
17
+ createDataAccessLayer.DataAccessLayer = DataAccessLayerCtor;
18
+ createDataAccessLayer.Model = Model;
19
+ createDataAccessLayer.QueryBuilder = QueryBuilder;
20
+ createDataAccessLayer.types = types;
21
+ createDataAccessLayer.Errors = Errors;
22
+ createDataAccessLayer.mlString = mlString;
23
+ createDataAccessLayer.revision = revision;
24
+ export { DataAccessLayer, Model, QueryBuilder, types, Errors, mlString, revision, setLanguageProvider, setDebugLogger, setRevisionSummaryEnabled, createDataAccessLayer, };
25
+ export default createDataAccessLayer;
26
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,MAAM,4BAA4B,CAAC;AACzD,OAAO,MAAM,MAAM,iBAAiB,CAAC;AACrC,OAAO,QAAQ,MAAM,oBAAoB,CAAC;AAC1C,OAAO,KAAK,MAAM,gBAAgB,CAAC;AAGnC,OAAO,YAAY,MAAM,wBAAwB,CAAC;AAClD,OAAO,QAAQ,MAAM,mBAAmB,CAAC;AAEzC,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,MAAM,kBAAkB,CAAC;AAClG,OAAO,KAAK,MAAM,eAAe,CAAC;AAmBlC,MAAM,mBAAmB,GAAG,eAAkD,CAAC;AAY/E;;;;;GAKG;AACH,MAAM,qBAAqB,GAAG,CAAC,CAAC,MAAgC,EAAE,EAAE,CAClE,IAAI,mBAAmB,CAAC,MAAM,CAAC,CAA0B,CAAC;AAE5D,qBAAqB,CAAC,eAAe,GAAG,mBAAmB,CAAC;AAC5D,qBAAqB,CAAC,KAAK,GAAG,KAAK,CAAC;AACpC,qBAAqB,CAAC,YAAY,GAAG,YAAY,CAAC;AAClD,qBAAqB,CAAC,KAAK,GAAG,KAAK,CAAC;AACpC,qBAAqB,CAAC,MAAM,GAAG,MAAM,CAAC;AACtC,qBAAqB,CAAC,QAAQ,GAAG,QAAQ,CAAC;AAC1C,qBAAqB,CAAC,QAAQ,GAAG,QAAQ,CAAC;AAE1C,OAAO,EACL,eAAe,EACf,KAAK,EACL,YAAY,EACZ,KAAK,EACL,MAAM,EACN,QAAQ,EACR,QAAQ,EACR,mBAAmB,EACnB,cAAc,EACd,yBAAyB,EACzB,qBAAqB,GACtB,CAAC;AAMF,eAAe,qBAAqB,CAAC"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Deep-copy a row value so the copy shares no mutable state with the
3
+ * original, while keeping every type `pg` serializes differently:
4
+ *
5
+ * - primitives, `null`, `undefined` and functions are returned as-is
6
+ * - `Date` → new `Date`; `Buffer` → new `Buffer`; other typed arrays and
7
+ * `DataView`s → copies of the same class
8
+ * - arrays, `Map` and `Set` → new containers with copied contents
9
+ * - other objects → a copy with the same prototype and copied own
10
+ * properties, so plain JSON objects stay plain and class instances keep
11
+ * methods such as `toJSON()` / `toPostgres()` that `pg` relies on
12
+ *
13
+ * `structuredClone` is not used because it turns a `Buffer` into a plain
14
+ * `Uint8Array`, drops prototypes, and throws on functions.
15
+ *
16
+ * Cycles are preserved. Objects whose state lives in internal slots or
17
+ * `#private` fields (other than the built-ins handled above) cannot be copied
18
+ * faithfully by any generic routine; they don't come out of PostgreSQL rows.
19
+ *
20
+ * @param value Value to copy
21
+ * @returns An independent copy of `value`
22
+ */
23
+ export declare function cloneRowValue<T>(value: T): T;
24
+ //# sourceMappingURL=clone.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clone.d.ts","sourceRoot":"","sources":["../../src/lib/clone.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAE5C"}