@fabrika/db 0.9.0-dev.2 → 0.9.0-dev.20

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 (80) hide show
  1. package/package.json +35 -4
  2. package/src/admissions.ts +78 -12
  3. package/src/application-rows.ts +108 -0
  4. package/src/authority.ts +560 -0
  5. package/src/builtins.ts +21 -0
  6. package/src/capability.ts +56 -2
  7. package/src/codegen-sqlite.ts +194 -0
  8. package/src/codegen.ts +229 -40
  9. package/src/condition-nodes.ts +119 -0
  10. package/src/conditions.ts +683 -0
  11. package/src/database.ts +227 -61
  12. package/src/description.ts +37 -8
  13. package/src/errors.ts +6 -4
  14. package/src/file-columns.ts +56 -11
  15. package/src/framework-tables.ts +28 -8
  16. package/src/framework.ts +14 -5
  17. package/src/guard.ts +373 -15
  18. package/src/index.ts +13 -1
  19. package/src/job-leases.ts +212 -55
  20. package/src/job-wakes.ts +450 -0
  21. package/src/json.ts +124 -0
  22. package/src/links.ts +8 -1
  23. package/src/migrations-sqlite.ts +163 -0
  24. package/src/migrations.ts +25 -0
  25. package/src/nodes.ts +84 -14
  26. package/src/orm-types.ts +124 -45
  27. package/src/orm.ts +283 -133
  28. package/src/policy.ts +1851 -119
  29. package/src/postgres.ts +26 -0
  30. package/src/registry.ts +52 -3
  31. package/src/sql.ts +247 -476
  32. package/src/sqlite-bun.ts +149 -0
  33. package/src/sqlite-migrate.ts +242 -0
  34. package/src/sqlite.ts +269 -0
  35. package/src/store.ts +2290 -239
  36. package/src/stored-files.ts +188 -52
  37. package/src/tables.ts +164 -3
  38. package/src/temporal.ts +122 -9
  39. package/src/testing-shared.ts +1348 -0
  40. package/src/testing-sqlite.ts +685 -0
  41. package/src/testing.ts +310 -494
  42. package/src/vocabulary-tables.ts +124 -18
  43. package/types/src/application-rows.d.ts +58 -0
  44. package/types/src/authority.d.ts +147 -0
  45. package/types/src/builtins.d.ts +2 -0
  46. package/types/src/capability.d.ts +55 -2
  47. package/types/src/codegen-sqlite.d.ts +13 -0
  48. package/types/src/codegen.d.ts +79 -5
  49. package/types/src/condition-nodes.d.ts +9 -0
  50. package/types/src/conditions.d.ts +151 -0
  51. package/types/src/database.d.ts +81 -20
  52. package/types/src/description.d.ts +26 -8
  53. package/types/src/errors.d.ts +5 -4
  54. package/types/src/file-columns.d.ts +6 -4
  55. package/types/src/framework.d.ts +11 -5
  56. package/types/src/guard.d.ts +24 -0
  57. package/types/src/index.d.ts +4 -3
  58. package/types/src/job-leases.d.ts +34 -5
  59. package/types/src/job-wakes.d.ts +101 -0
  60. package/types/src/json.d.ts +18 -0
  61. package/types/src/migrations-sqlite.d.ts +4 -0
  62. package/types/src/migrations.d.ts +1 -0
  63. package/types/src/nodes.d.ts +17 -2
  64. package/types/src/orm-types.d.ts +91 -42
  65. package/types/src/orm.d.ts +10 -0
  66. package/types/src/policy.d.ts +332 -6
  67. package/types/src/postgres.d.ts +12 -0
  68. package/types/src/registry.d.ts +37 -3
  69. package/types/src/sql.d.ts +29 -21
  70. package/types/src/sqlite-bun.d.ts +21 -0
  71. package/types/src/sqlite-migrate.d.ts +28 -0
  72. package/types/src/sqlite.d.ts +24 -0
  73. package/types/src/store.d.ts +143 -34
  74. package/types/src/stored-files.d.ts +6 -2
  75. package/types/src/tables.d.ts +17 -1
  76. package/types/src/temporal.d.ts +24 -0
  77. package/types/src/testing-shared.d.ts +290 -0
  78. package/types/src/testing-sqlite.d.ts +37 -0
  79. package/types/src/testing.d.ts +2 -93
  80. package/types/src/vocabulary-tables.d.ts +141 -17
@@ -0,0 +1,26 @@
1
+ import { SQL } from 'bun'
2
+ import { PostgresJSDialect } from 'kysely-postgres-js'
3
+ import { Database } from './database'
4
+ import { QueryRefusedError } from './errors'
5
+ import { isPlainIdentifier } from './identifier'
6
+
7
+ export interface PostgresOptions {
8
+ readonly url: string
9
+ /** The application's schema (`app_<id>`), set as the connection's `search_path`. */
10
+ readonly schema: string
11
+ readonly max?: number
12
+ }
13
+
14
+ /**
15
+ * Over `Bun.SQL` with `prepare: false`: Bun otherwise keeps one prepared statement per distinct SQL text per
16
+ * connection, and `in` lists of different lengths make that grow without bound.
17
+ */
18
+ export function openPostgres(options: PostgresOptions): Database {
19
+ if (!isPlainIdentifier(options.schema)) {
20
+ throw new QueryRefusedError('invalid_name', 'the schema must match ^[a-z_][a-z0-9_]*$')
21
+ }
22
+ const pool = new SQL({ url: options.url, adapter: 'postgres', prepare: false, max: options.max ?? 10, connection: { search_path: options.schema } })
23
+ return Database.fromDialect(new PostgresJSDialect({ postgres: pool }), { schema: options.schema })
24
+ }
25
+ // Application modules load after this one: a replaced opener would receive the database URL.
26
+ Object.freeze(openPostgres)
package/src/registry.ts CHANGED
@@ -3,9 +3,11 @@
3
3
  // files may import.
4
4
 
5
5
  import type { Admissions } from './admissions'
6
+ import type { ContainerGrant, RelationHold } from './capability'
6
7
  import type { Session } from './database'
7
8
  import type { WebhookTables } from './framework-tables'
8
9
  import type { JobLeases } from './job-leases'
10
+ import type { JobWakes } from './job-wakes'
9
11
  import type { DataAccess } from './orm'
10
12
  import type { DataLayerRuntime, TableContext, TableProbe } from './store'
11
13
  import type { StoredFiles } from './stored-files'
@@ -16,21 +18,68 @@ import type { StoredFiles } from './stored-files'
16
18
  */
17
19
  export type Locator = (dimension: string, id: string, using?: Session) => Promise<ReadonlyMap<string, string> | null>
18
20
 
21
+ /** Whether an active row of `relation`'s source names `principal` at `node`: what a Ref held through it is minted on. */
22
+ export type RelationCheck = (relation: string, node: string, principal: string) => Promise<boolean>
23
+
24
+ /**
25
+ * Whether a row `relation` reaches for `principal` reveals `node` of `dimension` now: what a Ref held through a reveal
26
+ * is minted on.
27
+ */
28
+ export type RevealCheck = (relation: string, dimension: string, node: string, principal: string, action: string) => Promise<boolean>
29
+
30
+ /**
31
+ * Of `relations` and `paths`, the caller's relations and grants of `action` that reach the object `id` of `dimension`
32
+ * through a secondary path that holds the action for a Ref (`writeThrough`), or through a relation row or reveal: what a
33
+ * Ref held through a path is minted on, and carries.
34
+ */
35
+ export type PathCheck = (
36
+ dimension: string,
37
+ id: string,
38
+ action: string,
39
+ principal: string,
40
+ relations: readonly RelationHold[],
41
+ paths: readonly ContainerGrant[],
42
+ ) => Promise<{ readonly relations: readonly RelationHold[]; readonly paths: readonly ContainerGrant[] }>
43
+
44
+ /**
45
+ * Of `values`, the caller's grants of `action` at nodes of `revealing`, those whose row reveals `node` of `dimension` to
46
+ * its members now: what a Ref held through member reveals is minted on, and carries.
47
+ */
48
+ export type MemberRevealCheck = (
49
+ revealing: string,
50
+ values: readonly string[],
51
+ dimension: string,
52
+ node: string,
53
+ action: string,
54
+ ) => Promise<readonly string[]>
55
+
19
56
  /** What the framework writes itself: its own `fabrika_*` tables. Usable once bound. */
20
57
  export interface FrameworkTables {
21
58
  readonly webhooks: WebhookTables
22
59
  readonly jobs: JobLeases
60
+ /** A job's durable pending work (`fabrika_wakes`), which follows its lease, and the scheduler's next wake over both. */
61
+ readonly wakes: JobWakes
23
62
  /** A procedure's admission buckets, the challenge tokens it accepted and its byte budget. */
24
63
  readonly admissions: Admissions
25
64
  /** Every stored file (`fabrika_files`): staged before its object, claimed at its first reference, swept unreferenced. */
26
65
  readonly files: StoredFiles
27
66
  }
28
67
 
68
+ /** A connection of the layer and its locator. Only its holder can `release` it, which lets the layer be bound again. */
69
+ export interface ProvisionalBinding {
70
+ readonly locate: Locator
71
+ readonly relates: RelationCheck
72
+ readonly reveals: RevealCheck
73
+ readonly holdsThrough: PathCheck
74
+ readonly revealsToMembers: MemberRevealCheck
75
+ release(): void
76
+ }
77
+
29
78
  export interface FrameworkSide {
30
- /** Connects the layer once and answers its locator; a second call throws. */
31
- bind(runtime: DataLayerRuntime): Locator
79
+ /** Connects the layer once and answers its binding; a call while one is connected throws. */
80
+ bind(runtime: DataLayerRuntime): ProvisionalBinding
32
81
  readonly framework: FrameworkTables
33
- /** The untyped handle over one table that `@fabrika/db/testing` hands the isolation tests. */
82
+ /** The untyped handle over one table that `@fabrika/db/testing` hands the policy isolation kit. */
34
83
  probe(table: string): TableProbe
35
84
  /** One table's context, for the framework's own store builders (`defineStore`, `@fabrika/db/testing`). */
36
85
  tableContext(table: string, mode: 'dimension' | 'child'): TableContext