palgres 0.53.0 → 0.55.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/dist/chunks/5EMZSH7B.js +5 -0
- package/dist/chunks/DLP3GKUJ.js +309 -0
- package/dist/chunks/EBHTHWOS.js +2 -0
- package/dist/dsl.js +2 -2
- package/dist/index.js +156 -125
- package/dist/testing.js +2 -2
- package/package.json +1 -1
- package/types/access/src/apply.d.ts +98 -0
- package/types/access/src/base.d.ts +49 -0
- package/types/access/src/catalog.d.ts +53 -0
- package/types/access/src/compile.d.ts +120 -0
- package/types/access/src/errors.d.ts +7 -0
- package/types/access/src/index.d.ts +10 -0
- package/types/access/src/model.d.ts +87 -0
- package/types/access/src/reference.d.ts +12 -0
- package/types/access/src/text.d.ts +104 -0
- package/types/access/src/validation.d.ts +22 -0
- package/types/access/src/world.d.ts +43 -0
- package/types/palgres/src/access.d.ts +63 -0
- package/types/palgres/src/connect.d.ts +10 -1
- package/types/palgres/src/dsl/policy.d.ts +24 -0
- package/types/palgres/src/dsl/to-model.d.ts +11 -1
- package/types/palgres/src/forget.d.ts +6 -6
- package/types/palgres/src/index.d.ts +1 -1
- package/types/palgres/src/model.d.ts +8 -0
- package/types/palgres/src/plan.d.ts +5 -1
- package/types/palgres/src/query/access.d.ts +169 -0
- package/types/palgres/src/query/env-gen.d.ts +2 -0
- package/types/palgres/src/query/index.d.ts +9 -7
- package/types/palgres/src/query/surface.d.ts +7 -1
- package/types/palgres/src/query/typed.d.ts +13 -5
- package/types/palgres/src/testing/access.d.ts +17 -0
- package/types/palgres/src/testing/fake.d.ts +13 -1
- package/types/palgres/src/testing/index.d.ts +2 -1
- package/dist/chunks/ULLX3GNS.js +0 -2
- package/dist/chunks/X2B6AJ7P.js +0 -5
- package/dist/chunks/XPFGITD6.js +0 -2
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
export type Literal = {
|
|
2
|
+
t: 'string';
|
|
3
|
+
v: string;
|
|
4
|
+
} | {
|
|
5
|
+
t: 'number';
|
|
6
|
+
v: string;
|
|
7
|
+
} | {
|
|
8
|
+
t: 'boolean';
|
|
9
|
+
v: boolean;
|
|
10
|
+
};
|
|
11
|
+
/** What a `where` says of a row of the type's table. */
|
|
12
|
+
export type Condition = {
|
|
13
|
+
op: 'cmp';
|
|
14
|
+
column: string;
|
|
15
|
+
cmp: '=' | '!=' | '<' | '<=' | '>' | '>=';
|
|
16
|
+
value: Literal;
|
|
17
|
+
} | {
|
|
18
|
+
op: 'null';
|
|
19
|
+
column: string;
|
|
20
|
+
negated: boolean;
|
|
21
|
+
} | {
|
|
22
|
+
op: 'in';
|
|
23
|
+
column: string;
|
|
24
|
+
values: Literal[];
|
|
25
|
+
} | {
|
|
26
|
+
op: 'and' | 'or';
|
|
27
|
+
parts: Condition[];
|
|
28
|
+
} | {
|
|
29
|
+
op: 'not';
|
|
30
|
+
part: Condition;
|
|
31
|
+
};
|
|
32
|
+
/** A permission's expression. */
|
|
33
|
+
export type Expr = {
|
|
34
|
+
op: 'ref';
|
|
35
|
+
name: string;
|
|
36
|
+
} | {
|
|
37
|
+
op: 'arrow';
|
|
38
|
+
relation: string;
|
|
39
|
+
name: string;
|
|
40
|
+
} | {
|
|
41
|
+
op: 'union' | 'intersect';
|
|
42
|
+
parts: Expr[];
|
|
43
|
+
} | {
|
|
44
|
+
op: 'exclude';
|
|
45
|
+
base: Expr;
|
|
46
|
+
minus: Expr;
|
|
47
|
+
};
|
|
48
|
+
/** A relationship kept in Palbase's table: given and taken through the API. */
|
|
49
|
+
export interface StoredRelationAst {
|
|
50
|
+
kind: 'stored';
|
|
51
|
+
name: string;
|
|
52
|
+
/** 'user', or the names of types. */
|
|
53
|
+
subjects: string[];
|
|
54
|
+
line: number;
|
|
55
|
+
}
|
|
56
|
+
/** A relationship read from a column of the type's own table. */
|
|
57
|
+
export interface ColumnRelationAst {
|
|
58
|
+
kind: 'column';
|
|
59
|
+
name: string;
|
|
60
|
+
/** 'user' (the column holds a subject's id) or a type (the column holds that type's object id). */
|
|
61
|
+
subject: string;
|
|
62
|
+
column: string;
|
|
63
|
+
where: Condition | null;
|
|
64
|
+
line: number;
|
|
65
|
+
}
|
|
66
|
+
export type RelationAst = StoredRelationAst | ColumnRelationAst;
|
|
67
|
+
export interface PermissionAst {
|
|
68
|
+
name: string;
|
|
69
|
+
expr: Expr;
|
|
70
|
+
line: number;
|
|
71
|
+
}
|
|
72
|
+
export type RowCommand = 'select' | 'insert' | 'update' | 'delete';
|
|
73
|
+
export interface RowsAst {
|
|
74
|
+
commands: RowCommand[];
|
|
75
|
+
name: string;
|
|
76
|
+
line: number;
|
|
77
|
+
}
|
|
78
|
+
export interface TableAst {
|
|
79
|
+
/** null: the database's default schema (public). */
|
|
80
|
+
schema: string | null;
|
|
81
|
+
name: string;
|
|
82
|
+
/** null: the table's primary key. */
|
|
83
|
+
key: string | null;
|
|
84
|
+
line: number;
|
|
85
|
+
}
|
|
86
|
+
export interface TypeAst {
|
|
87
|
+
name: string;
|
|
88
|
+
table: TableAst | null;
|
|
89
|
+
relations: RelationAst[];
|
|
90
|
+
permissions: PermissionAst[];
|
|
91
|
+
rows: RowsAst[];
|
|
92
|
+
line: number;
|
|
93
|
+
}
|
|
94
|
+
export interface SchemaAst {
|
|
95
|
+
types: TypeAst[];
|
|
96
|
+
}
|
|
97
|
+
/** The largest schema text read: a schema is a page or two, and nothing here should be made to read a megabyte. */
|
|
98
|
+
export declare const MAX_TEXT: number;
|
|
99
|
+
/** The longest text value a condition compares with. */
|
|
100
|
+
export declare const MAX_VALUE = 256;
|
|
101
|
+
/** How deep parentheses and `not` may nest: what reads them calls itself once a level. */
|
|
102
|
+
export declare const MAX_NESTING = 32;
|
|
103
|
+
/** Reads an access schema's text. Throws AccessError('syntax') naming the line. */
|
|
104
|
+
export declare function parse(text: string): SchemaAst;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface ValidationFailure {
|
|
2
|
+
/** The expectation, as a sentence. */
|
|
3
|
+
expectation: string;
|
|
4
|
+
/** What the schema answers instead. */
|
|
5
|
+
answer: string;
|
|
6
|
+
}
|
|
7
|
+
export interface ValidationResult {
|
|
8
|
+
passed: number;
|
|
9
|
+
failed: ValidationFailure[];
|
|
10
|
+
}
|
|
11
|
+
/** What is wrong with the validation file itself (not with the schema's answers). */
|
|
12
|
+
export declare class ValidationFileError extends Error {
|
|
13
|
+
constructor(message: string);
|
|
14
|
+
}
|
|
15
|
+
/** The schema's text a validation file names, when it does not carry it: the path, for whoever reads files. */
|
|
16
|
+
export declare function schemaPathOf(doc: unknown): string | null;
|
|
17
|
+
/**
|
|
18
|
+
* Answers a validation file's expectations from its own rows and relationships. `schemaText` is the schema it
|
|
19
|
+
* names (read by the caller) — ignored when the file carries its own. Throws AccessError for a schema that does not
|
|
20
|
+
* hold together and ValidationFileError for a file that cannot be read as one.
|
|
21
|
+
*/
|
|
22
|
+
export declare function runValidation(doc: unknown, schemaText: string | null): ValidationResult;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** How a column's values compare, which is all a schema needs to know of its type. */
|
|
2
|
+
export type Family =
|
|
3
|
+
/** text, varchar and an enum: equal when the same characters. */
|
|
4
|
+
'text'
|
|
5
|
+
/** uuid: 8-4-4-4-12 hexadecimal digits, equal whatever the letters' case. */
|
|
6
|
+
| 'uuid'
|
|
7
|
+
/** smallint, integer, bigint: equal when the same number ("07" is 7). */
|
|
8
|
+
| 'int' | 'bool'
|
|
9
|
+
/** numeric and the floating types: a `where` may compare them; they are no key and no id. */
|
|
10
|
+
| 'numeric';
|
|
11
|
+
export interface TableShape {
|
|
12
|
+
/** The column whose value is the object's id. */
|
|
13
|
+
key: string;
|
|
14
|
+
/** Every column the schema reads, the key among them. */
|
|
15
|
+
columns: Record<string, Family>;
|
|
16
|
+
}
|
|
17
|
+
/** A column's value. An integer is a number or a bigint; a uuid and a text are strings. */
|
|
18
|
+
export type Value = string | number | bigint | boolean | null;
|
|
19
|
+
/** One stored relationship: `subjectType` is 'user' or a type's name. */
|
|
20
|
+
export interface Relationship {
|
|
21
|
+
objectType: string;
|
|
22
|
+
objectId: string;
|
|
23
|
+
relation: string;
|
|
24
|
+
subjectType: string;
|
|
25
|
+
subjectId: string;
|
|
26
|
+
}
|
|
27
|
+
export interface World {
|
|
28
|
+
/** By type name, for each type that names a table. */
|
|
29
|
+
tables: Map<string, {
|
|
30
|
+
shape: TableShape;
|
|
31
|
+
rows: Record<string, Value>[];
|
|
32
|
+
}>;
|
|
33
|
+
relationships: Relationship[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* An id as its type writes it, or null when the text is no value of that type: an integer's digits without a sign's
|
|
37
|
+
* zero or leading zeros, a uuid in lower case, a text as it is. Two ids are the same when this is the same — the
|
|
38
|
+
* database compares them the same way. An integer is at most 18 digits here, so that none overflows a bigint; a uuid
|
|
39
|
+
* is the hyphenated form alone.
|
|
40
|
+
*/
|
|
41
|
+
export declare function canonicalId(family: Family, text: string): string | null;
|
|
42
|
+
/** A column's value as an id's text: what `column::text` is in the database. Null stays null. */
|
|
43
|
+
export declare function idOf(family: Family, value: Value): string | null;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { type Compiled } from '../../access/src/index.js';
|
|
2
|
+
import type { Schema } from './model.js';
|
|
3
|
+
export declare const accessToken: (name: string) => string;
|
|
4
|
+
export declare const namesAccess: (text: string | undefined) => boolean;
|
|
5
|
+
/** A name as a policy may say it. */
|
|
6
|
+
export declare const ACCESS_NAME: RegExp;
|
|
7
|
+
/**
|
|
8
|
+
* What a version keeps of its compiled access schema (in its layout), so that the version after it can stand
|
|
9
|
+
* beside it and then take its place without compiling it again: a later compiler may write it otherwise, and what
|
|
10
|
+
* is in the database is what this one wrote.
|
|
11
|
+
*/
|
|
12
|
+
export interface RecordedAccess {
|
|
13
|
+
digest: string;
|
|
14
|
+
/** The compiled schema's name in the database: access_<digest>. */
|
|
15
|
+
schema: string;
|
|
16
|
+
names: {
|
|
17
|
+
type: string;
|
|
18
|
+
name: string;
|
|
19
|
+
view: string;
|
|
20
|
+
idType: string;
|
|
21
|
+
}[];
|
|
22
|
+
tables: {
|
|
23
|
+
type: string;
|
|
24
|
+
schema: string;
|
|
25
|
+
name: string;
|
|
26
|
+
key: string;
|
|
27
|
+
}[];
|
|
28
|
+
/** "The asker holds it on this row", for each name of a type with a table. */
|
|
29
|
+
predicates: {
|
|
30
|
+
type: string;
|
|
31
|
+
name: string;
|
|
32
|
+
sql: string;
|
|
33
|
+
}[];
|
|
34
|
+
/** The relations whose relationships are kept, each with the kinds of subject it takes: type, relation, subject. */
|
|
35
|
+
stored: [string, string, string][];
|
|
36
|
+
}
|
|
37
|
+
export interface AccessRoles {
|
|
38
|
+
authenticated: string;
|
|
39
|
+
service: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The model's access schema, compiled against the model's own tables; null when it has none. Throws AccessError when
|
|
43
|
+
* the schema does not hold together or names a table or a column the model does not declare as it says.
|
|
44
|
+
*/
|
|
45
|
+
export declare function compileAccess(schema: Schema, roles: AccessRoles, aliases?: {
|
|
46
|
+
name: string;
|
|
47
|
+
type: string;
|
|
48
|
+
}[]): Compiled | null;
|
|
49
|
+
/** The digest a model's access schema compiles to: what the model carries, so that a compiler that writes it otherwise is another version. */
|
|
50
|
+
export declare function accessDigest(schema: Omit<Schema, 'access'>, text: string, roles: AccessRoles): string;
|
|
51
|
+
export declare function recordAccess(c: Compiled, text: string): RecordedAccess;
|
|
52
|
+
/**
|
|
53
|
+
* A policy's text with each name it reads replaced by that name's predicate on the table's rows. Answers what is
|
|
54
|
+
* wrong instead when the table is no type's, or the type has no such name.
|
|
55
|
+
*/
|
|
56
|
+
export declare function resolvePolicy(text: string, table: {
|
|
57
|
+
schema: string;
|
|
58
|
+
name: string;
|
|
59
|
+
}, access: Pick<RecordedAccess, 'tables' | 'predicates'> | null): {
|
|
60
|
+
sql: string;
|
|
61
|
+
} | {
|
|
62
|
+
refused: string;
|
|
63
|
+
};
|
|
@@ -18,6 +18,15 @@ export interface Binding {
|
|
|
18
18
|
pooled?: boolean;
|
|
19
19
|
/** Where a table of Postgres schema `name` is read by this version: the schema itself, or its version's views. */
|
|
20
20
|
schema(name: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* The compiled access schema this version asks (its digest), when the app has one: what the app asks
|
|
23
|
+
* (access_<digest>.can, …) is asked of its own. Which version a transaction is, it says as it opens
|
|
24
|
+
* (palgres.version in the bind statement), so that while a migration is open a row policy answers each version
|
|
25
|
+
* by its own rules.
|
|
26
|
+
*/
|
|
27
|
+
access?: string;
|
|
28
|
+
/** That schema's text: what the engine reads the names an app may ask by from (Database.$access). */
|
|
29
|
+
accessText?: string;
|
|
21
30
|
}
|
|
22
31
|
/** The binding, for connections a transaction-mode pooler lends (Binding.pooled). */
|
|
23
32
|
export declare function pooledBinding(b: Binding): Binding;
|
|
@@ -28,7 +37,7 @@ export declare function pooledBinding(b: Binding): Binding;
|
|
|
28
37
|
* (applicationName). A pooled app (Binding.pooled) holds one such.
|
|
29
38
|
*/
|
|
30
39
|
export declare function sayRunning(b: Binding): string;
|
|
31
|
-
export declare function binding(version: number, direct: boolean): Binding;
|
|
40
|
+
export declare function binding(version: number, direct: boolean, access?: string, accessText?: string): Binding;
|
|
32
41
|
export declare class VersionNotMigrated extends Error {
|
|
33
42
|
readonly digest: string;
|
|
34
43
|
constructor(d: string);
|
|
@@ -61,6 +61,15 @@ export type PolicyExpr = {
|
|
|
61
61
|
kind: "isNull";
|
|
62
62
|
expr: PolicyExpr;
|
|
63
63
|
negated: boolean;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* "The asker holds this relation or permission on the row", as the app's
|
|
67
|
+
* access schema says it. The plan writes the compiled predicate in its
|
|
68
|
+
* place; until then it is a name.
|
|
69
|
+
*/
|
|
70
|
+
| {
|
|
71
|
+
kind: "access";
|
|
72
|
+
name: string;
|
|
64
73
|
} | {
|
|
65
74
|
/**
|
|
66
75
|
* A correlated `EXISTS (SELECT 1 FROM <table> <alias> WHERE <predicate>)`
|
|
@@ -234,6 +243,21 @@ export interface PolicyExprCtx<Cols extends string = string> {
|
|
|
234
243
|
auth: {
|
|
235
244
|
uid(): PolicyExprRef;
|
|
236
245
|
};
|
|
246
|
+
/**
|
|
247
|
+
* The asker holds a relation or a permission on this row, as the app's
|
|
248
|
+
* access schema defines it for the type this table carries:
|
|
249
|
+
*
|
|
250
|
+
* ```ts
|
|
251
|
+
* policy("members read").for("select").using(p.can("view"))
|
|
252
|
+
* ```
|
|
253
|
+
*
|
|
254
|
+
* It can stand beside other conditions (`p.or(p.can("view"), …)`). The
|
|
255
|
+
* schema is the app's (`db.access`); a name it does not have, or a table no
|
|
256
|
+
* type of it names, is refused when the migration is planned. Not allowed
|
|
257
|
+
* in a CHECK constraint, nor inside `existsIn` (it is about this table's
|
|
258
|
+
* row).
|
|
259
|
+
*/
|
|
260
|
+
can(name: string): PolicyExprRef;
|
|
237
261
|
/** A plain value, for saying explicitly that one side of a comparison is a literal. */
|
|
238
262
|
lit(v: string | number | boolean | null): PolicyExprRef;
|
|
239
263
|
and(...parts: PolicyExprRef[]): PolicyExprRef;
|
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import { type Policy, type Schema, type Table } from '../model.js';
|
|
2
|
+
import { type AccessRoles } from '../access.js';
|
|
2
3
|
import type { SchemaDef } from './schema.js';
|
|
3
4
|
/** One or more schemas (db/public.ts, db/billing.ts, …) as the model. */
|
|
4
|
-
|
|
5
|
+
/**
|
|
6
|
+
* The schema files as the model. `access` is the app's access schema, as its text: it is compiled here against the
|
|
7
|
+
* tables declared (a schema that does not hold together, or names a table or a column they do not have as it says,
|
|
8
|
+
* throws AccessError), and the model carries what it compiled to. `roles` are the database's roles the app's
|
|
9
|
+
* requests take, when they are not the usual two.
|
|
10
|
+
*/
|
|
11
|
+
export declare function toModel(schemas: SchemaDef | SchemaDef[], options?: {
|
|
12
|
+
access?: string;
|
|
13
|
+
roles?: AccessRoles;
|
|
14
|
+
}): Schema;
|
|
5
15
|
/** The columns of `t` a policy's SQL names quoted — as the renderer writes a column; a hand-written policy that quotes nothing names none. */
|
|
6
16
|
export declare function policyColumns(t: Table, p: Policy): string[];
|
|
@@ -9,10 +9,13 @@ interface Begin {
|
|
|
9
9
|
interface Runs {
|
|
10
10
|
query(sql: string, params?: unknown[]): PromiseLike<unknown>;
|
|
11
11
|
}
|
|
12
|
-
|
|
12
|
+
/** What forgetting a user took: rows deleted, rows that forgot them, and — of an app with an access schema — the relationships they held. */
|
|
13
|
+
export interface Forgotten {
|
|
13
14
|
deleted: number;
|
|
14
15
|
detached: number;
|
|
15
|
-
|
|
16
|
+
relationships?: number;
|
|
17
|
+
}
|
|
18
|
+
export declare function forgetUser(sql: Begin, binding: Binding, schema: Schema, userId: string, serviceRole?: string): Promise<Forgotten>;
|
|
16
19
|
/**
|
|
17
20
|
* The same, in a transaction already open as service_role — a delivery's own ($asService(): all its handlers' work, or
|
|
18
21
|
* none, within the request's share of connections): in a savepoint of it, so a failure leaves the rest of the
|
|
@@ -21,8 +24,5 @@ export declare function forgetUser(sql: Begin, binding: Binding, schema: Schema,
|
|
|
21
24
|
*/
|
|
22
25
|
export declare function forgetUserIn(service: {
|
|
23
26
|
attempt<T>(fn: (tx: Runs) => Promise<T>): Promise<T>;
|
|
24
|
-
}, binding: Binding, schema: Schema, userId: string): Promise<
|
|
25
|
-
deleted: number;
|
|
26
|
-
detached: number;
|
|
27
|
-
}>;
|
|
27
|
+
}, binding: Binding, schema: Schema, userId: string): Promise<Forgotten>;
|
|
28
28
|
export {};
|
|
@@ -6,7 +6,7 @@ export { applicationName, cutOff, DEFAULT_ROLES, isDirect, MigrationOpen, Migrat
|
|
|
6
6
|
export type { Adoption, MigratorOptions, Pool, Reserved, StartResult, Status, Version } from './migrate.js';
|
|
7
7
|
export { bind, binding, bindStatement, pooledBinding, sayRunning, VersionNotMigrated } from './connect.js';
|
|
8
8
|
export type { Binding } from './connect.js';
|
|
9
|
-
export { forgetUser, forgetUserIn } from './forget.js';
|
|
9
|
+
export { forgetUser, forgetUserIn, type Forgotten } from './forget.js';
|
|
10
10
|
export { adoptionBaseline, drift, type Baseline } from './adopt.js';
|
|
11
11
|
export { introspect, type Introspected } from './introspect.js';
|
|
12
12
|
export { pullSchema, type Pulled } from './pull.js';
|
|
@@ -5,6 +5,14 @@ export interface Schema {
|
|
|
5
5
|
extensions: string[];
|
|
6
6
|
/** Named SQL the typed schema cannot say (an expression unique index, a function): applied once by name. */
|
|
7
7
|
raw: Raw[];
|
|
8
|
+
/**
|
|
9
|
+
* The app's access schema (who may do what on which object): its text, and the digest it compiles to — carried
|
|
10
|
+
* so that a compiler which writes it otherwise makes another version, and the database is compiled again.
|
|
11
|
+
*/
|
|
12
|
+
access?: {
|
|
13
|
+
text: string;
|
|
14
|
+
compiled: string;
|
|
15
|
+
};
|
|
8
16
|
}
|
|
9
17
|
export interface Table {
|
|
10
18
|
/** The Postgres schema; "public" unless given. */
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type RecordedAccess } from './access.js';
|
|
1
2
|
import { type Raw, type Schema } from './model.js';
|
|
2
3
|
import { type SearchGeneration } from './search.js';
|
|
3
4
|
/** A version's names: each visible table and column of the schema, and where it is in the database. */
|
|
@@ -11,6 +12,8 @@ export interface Layout {
|
|
|
11
12
|
physical: string;
|
|
12
13
|
}[];
|
|
13
14
|
}[];
|
|
15
|
+
/** The version's access schema as it was compiled into the database: what the version after it stands beside, then replaces. */
|
|
16
|
+
access?: RecordedAccess;
|
|
14
17
|
}
|
|
15
18
|
export type Step =
|
|
16
19
|
/** One transaction, under the migrator's lock timeout; tried again as a whole. */
|
|
@@ -111,10 +114,11 @@ export interface Roles {
|
|
|
111
114
|
service: string;
|
|
112
115
|
}
|
|
113
116
|
export interface PlanInput {
|
|
114
|
-
/** The version serving now, or null for a database Palgres has not migrated yet. */
|
|
117
|
+
/** The version serving now, or null for a database Palgres has not migrated yet; `access` is what its layout recorded of its access schema. */
|
|
115
118
|
from: {
|
|
116
119
|
n: number;
|
|
117
120
|
schema: Schema;
|
|
121
|
+
access?: RecordedAccess | null;
|
|
118
122
|
} | null;
|
|
119
123
|
/** The new version's number: past every version the database has recorded, an abandoned one too. */
|
|
120
124
|
next: number;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/** An object's id: text in the database; a number is written as its digits. */
|
|
2
|
+
export type AccessId = string | number | bigint;
|
|
3
|
+
/** One object of the access schema: its type and its id. */
|
|
4
|
+
export interface AccessObject<K extends string = string> {
|
|
5
|
+
type: K;
|
|
6
|
+
id: AccessId;
|
|
7
|
+
}
|
|
8
|
+
/** Who holds a relation: a user by id (a bare string is one), or an object — `{ type: "team", id }`. */
|
|
9
|
+
export type AccessSubject = string | {
|
|
10
|
+
user: string;
|
|
11
|
+
} | AccessObject;
|
|
12
|
+
export interface AccessRelationship {
|
|
13
|
+
object: {
|
|
14
|
+
type: string;
|
|
15
|
+
id: string;
|
|
16
|
+
};
|
|
17
|
+
relation: string;
|
|
18
|
+
subject: {
|
|
19
|
+
type: string;
|
|
20
|
+
id: string;
|
|
21
|
+
};
|
|
22
|
+
/** When it was given, ISO 8601. */
|
|
23
|
+
since: string;
|
|
24
|
+
}
|
|
25
|
+
export interface AccessHistoryEntry {
|
|
26
|
+
/** ISO 8601. */
|
|
27
|
+
at: string;
|
|
28
|
+
action: 'grant' | 'revoke';
|
|
29
|
+
object: {
|
|
30
|
+
type: string;
|
|
31
|
+
id: string;
|
|
32
|
+
};
|
|
33
|
+
relation: string;
|
|
34
|
+
subject: {
|
|
35
|
+
type: string;
|
|
36
|
+
id: string;
|
|
37
|
+
};
|
|
38
|
+
/** Who did it: what the app said (`by`), else the request's user, else the database login. */
|
|
39
|
+
by: string | null;
|
|
40
|
+
}
|
|
41
|
+
export interface AccessFilter {
|
|
42
|
+
/** Of this object; without an id, of every object of the type. */
|
|
43
|
+
object?: {
|
|
44
|
+
type: string;
|
|
45
|
+
id?: AccessId;
|
|
46
|
+
};
|
|
47
|
+
relation?: string;
|
|
48
|
+
subject?: AccessSubject;
|
|
49
|
+
/** At most this many (default 100, at most 1000). */
|
|
50
|
+
limit?: number;
|
|
51
|
+
}
|
|
52
|
+
export type AccessRefusal = 'no_schema' | 'unknown_type' | 'unknown_name' | 'not_kept' | 'bad_id' | 'no_object' | 'too_many' | 'not_the_service';
|
|
53
|
+
/** What an access question or change was refused for. Thrown before anything is sent wherever the schema alone says so. */
|
|
54
|
+
export declare class AccessRefused extends Error {
|
|
55
|
+
readonly code: AccessRefusal;
|
|
56
|
+
constructor(code: AccessRefusal, message: string, options?: {
|
|
57
|
+
cause?: unknown;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
type Known<A> = [keyof A] extends [never] ? false : true;
|
|
61
|
+
/** A type of the schema. */
|
|
62
|
+
export type AccessTypeOf<A> = Known<A> extends true ? keyof A & string : string;
|
|
63
|
+
/** A relation or a permission a user can hold on a `K`. */
|
|
64
|
+
export type AccessNameOf<A, K> = K extends keyof A ? (A[K] extends {
|
|
65
|
+
names: infer N extends string;
|
|
66
|
+
} ? N : string) : string;
|
|
67
|
+
/** A relation a `K` keeps relationships of. */
|
|
68
|
+
export type AccessRelationOf<A, K> = K extends keyof A ? (A[K] extends {
|
|
69
|
+
relations: infer R;
|
|
70
|
+
} ? keyof R & string : string) : string;
|
|
71
|
+
/** What holds relation `R` of a `K`: a user (a bare id, or `{ user }`), or an object of a type the relation takes. */
|
|
72
|
+
export type AccessSubjectOf<A, K, R> = K extends keyof A ? A[K] extends {
|
|
73
|
+
relations: infer M;
|
|
74
|
+
} ? R extends keyof M ? M[R] extends infer S extends string ? (S extends 'user' ? string | {
|
|
75
|
+
user: string;
|
|
76
|
+
} : {
|
|
77
|
+
type: S;
|
|
78
|
+
id: AccessId;
|
|
79
|
+
}) : AccessSubject : AccessSubject : AccessSubject : AccessSubject;
|
|
80
|
+
/**
|
|
81
|
+
* `Database.$access`: the app's access schema, asked and changed.
|
|
82
|
+
*
|
|
83
|
+
* `can`, `objects` and `held` are about the request's caller. The rest name a subject or change who holds what, and
|
|
84
|
+
* are the service role's: `Database.$asService().$access` — where those three are still about the caller.
|
|
85
|
+
*/
|
|
86
|
+
export interface AccessOps<A = unknown> {
|
|
87
|
+
/** Whether the caller holds `name` — a relation or a permission — on the object. Nobody is signed in: no. */
|
|
88
|
+
can<K extends AccessTypeOf<A>>(name: AccessNameOf<A, K>, object: AccessObject<K>): Promise<boolean>;
|
|
89
|
+
/** The ids of the objects of `type` the caller holds `name` on. */
|
|
90
|
+
objects<K extends AccessTypeOf<A>>(name: AccessNameOf<A, K>, type: K): Promise<string[]>;
|
|
91
|
+
/** Every relation and permission the caller holds on the object, sorted: what a screen shows or hides, in one statement. */
|
|
92
|
+
held<K extends AccessTypeOf<A>>(object: AccessObject<K>): Promise<AccessNameOf<A, K>[]>;
|
|
93
|
+
/** Whether `subject` (a user's id) holds `name` on the object. The service role's. */
|
|
94
|
+
check<K extends AccessTypeOf<A>>(subject: string, name: AccessNameOf<A, K>, object: AccessObject<K>): Promise<boolean>;
|
|
95
|
+
/** The ids of the objects of `type` that `subject` holds `name` on. The service role's. */
|
|
96
|
+
objectsOf<K extends AccessTypeOf<A>>(subject: string, name: AccessNameOf<A, K>, type: K): Promise<string[]>;
|
|
97
|
+
/** The users who hold `name` on the object, sorted. Refused past 10,000 related subjects: ask `check` then. The service role's. */
|
|
98
|
+
subjects<K extends AccessTypeOf<A>>(name: AccessNameOf<A, K>, object: AccessObject<K>): Promise<string[]>;
|
|
99
|
+
/**
|
|
100
|
+
* Gives `subject` the relation on the object; false when they had it. The object must exist (a type with a table).
|
|
101
|
+
* `by` is who did it, kept in the history: the request's user when it is not said. The service role's.
|
|
102
|
+
*/
|
|
103
|
+
grant<K extends AccessTypeOf<A>, R extends AccessRelationOf<A, K>>(object: AccessObject<K>, relation: R, subject: AccessSubjectOf<A, K, R>, options?: {
|
|
104
|
+
by?: string;
|
|
105
|
+
}): Promise<boolean>;
|
|
106
|
+
/** Takes the relation back; false when they did not have it. In force from the next statement on. The service role's. */
|
|
107
|
+
revoke<K extends AccessTypeOf<A>, R extends AccessRelationOf<A, K>>(object: AccessObject<K>, relation: R, subject: AccessSubjectOf<A, K, R>, options?: {
|
|
108
|
+
by?: string;
|
|
109
|
+
}): Promise<boolean>;
|
|
110
|
+
/** The relationships kept, by object, relation and subject. The service role's. */
|
|
111
|
+
relationships(filter?: AccessFilter): Promise<AccessRelationship[]>;
|
|
112
|
+
/** What was given and taken, the latest first: who, to whom, when. The service role's. */
|
|
113
|
+
history(filter?: AccessFilter): Promise<AccessHistoryEntry[]>;
|
|
114
|
+
}
|
|
115
|
+
/** What a version's access schema lets the app say, read from its text once. */
|
|
116
|
+
export interface AccessNames {
|
|
117
|
+
/** The compiled schema's name in the database. */
|
|
118
|
+
schema: string;
|
|
119
|
+
types: Map<string, {
|
|
120
|
+
names: Set<string>;
|
|
121
|
+
kept: Map<string, Set<string>>;
|
|
122
|
+
}>;
|
|
123
|
+
}
|
|
124
|
+
/** The names of an access schema's text, for the version that compiled it to `digest`. */
|
|
125
|
+
export declare function accessNames(text: string, digest: string): AccessNames;
|
|
126
|
+
/** The app's access schema as an augmentation's body: a type an entry, its names and what its relations take. */
|
|
127
|
+
export declare function accessTypesBlock(text: string, indent: string): string;
|
|
128
|
+
interface Asks {
|
|
129
|
+
query<T = unknown>(sql: string, params?: unknown[]): Promise<T[]>;
|
|
130
|
+
}
|
|
131
|
+
/** Whose transaction asks: nobody's (no one is signed in), a signed-in user's, or the service role's. */
|
|
132
|
+
export type AccessAsker = 'anon' | 'authenticated' | 'service';
|
|
133
|
+
/** A filter as the database is asked it: the columns named, each with the value it must have. */
|
|
134
|
+
export interface AccessWhere {
|
|
135
|
+
object_type?: string;
|
|
136
|
+
object_id?: string;
|
|
137
|
+
relation?: string;
|
|
138
|
+
subject_type?: string;
|
|
139
|
+
subject_id?: string;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The questions answered without a database (palgres/testing's database in memory): the same operations, after the
|
|
143
|
+
* same checks of what was asked, with the schema's second evaluator in the database's place. What it refuses, it
|
|
144
|
+
* refuses as AccessRefused, with the database's codes.
|
|
145
|
+
*/
|
|
146
|
+
export interface AccessVia {
|
|
147
|
+
/** Whom `can`, `objects` and `held` are about; undefined when nobody is signed in. */
|
|
148
|
+
caller(): string | undefined;
|
|
149
|
+
check(subject: string, name: string, type: string, id: string): boolean;
|
|
150
|
+
objects(subject: string, name: string, type: string): string[];
|
|
151
|
+
subjects(name: string, type: string, id: string): string[];
|
|
152
|
+
change(fn: 'grant' | 'revoke', object: {
|
|
153
|
+
type: string;
|
|
154
|
+
id: string;
|
|
155
|
+
}, relation: string, subject: {
|
|
156
|
+
type: string;
|
|
157
|
+
id: string;
|
|
158
|
+
}, by: string | undefined): boolean;
|
|
159
|
+
relationships(where: AccessWhere, limit: number): AccessRelationship[];
|
|
160
|
+
history(where: AccessWhere, limit: number): AccessHistoryEntry[];
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* The access operations over what asks (`raw`: the transaction current when each is called), for the version whose
|
|
164
|
+
* access schema `names` are — undefined when the app declares none: every operation then says so. `as` is whose
|
|
165
|
+
* transaction it is: nobody (anon) holds nothing and is not the database's to ask, and only the service role's
|
|
166
|
+
* names a subject or changes who holds what — said here, since the database's refusal would end the transaction.
|
|
167
|
+
*/
|
|
168
|
+
export declare function accessOps(raw: () => Asks, names: AccessNames | undefined, as: AccessAsker, unanswered?: string, via?: AccessVia): AccessOps;
|
|
169
|
+
export {};
|
|
@@ -11,6 +11,7 @@ export declare const ENV_BLOCK_END = "// palbase:env:end";
|
|
|
11
11
|
*/
|
|
12
12
|
export declare function makeEnvDts(schemas: readonly SchemaDef[], options?: {
|
|
13
13
|
module?: string;
|
|
14
|
+
access?: string;
|
|
14
15
|
}): string;
|
|
15
16
|
/**
|
|
16
17
|
* The same types as a block of their own, between the markers and holding the
|
|
@@ -20,4 +21,5 @@ export declare function makeEnvDts(schemas: readonly SchemaDef[], options?: {
|
|
|
20
21
|
*/
|
|
21
22
|
export declare function envBlock(schemas: readonly SchemaDef[], options?: {
|
|
22
23
|
module?: string;
|
|
24
|
+
access?: string;
|
|
23
25
|
}): string;
|
|
@@ -55,10 +55,10 @@ export interface DatabaseOptions {
|
|
|
55
55
|
*/
|
|
56
56
|
prepare?: boolean;
|
|
57
57
|
}
|
|
58
|
-
/** One request's — or one job's — database. `T`, `S`: the project's tables (the framework's `Tables`, `Schemas`). */
|
|
59
|
-
export interface RequestDatabase<T, S> {
|
|
60
|
-
/** What a handler reaches as `Database`: typed tables, the `$` operations, $atomic, $asService(). */
|
|
61
|
-
readonly database: EnvTypedDatabase<T, S>;
|
|
58
|
+
/** One request's — or one job's — database. `T`, `S`: the project's tables (the framework's `Tables`, `Schemas`); `A`: its access schema (`Access`). */
|
|
59
|
+
export interface RequestDatabase<T, S, A = unknown> {
|
|
60
|
+
/** What a handler reaches as `Database`: typed tables, the `$` operations, $access, $atomic, $asService(). */
|
|
61
|
+
readonly database: EnvTypedDatabase<T, S, A>;
|
|
62
62
|
/** The string-keyed client the typed surface is built on. */
|
|
63
63
|
readonly client: DBClient;
|
|
64
64
|
/** Commits what was opened — nothing, if nothing was. Once, after the handler returns. */
|
|
@@ -66,9 +66,9 @@ export interface RequestDatabase<T, S> {
|
|
|
66
66
|
/** Rolls back what was opened. Once, when the handler throws. */
|
|
67
67
|
rollback(reason: unknown): Promise<void>;
|
|
68
68
|
}
|
|
69
|
-
export interface AppDatabase<T, S> {
|
|
69
|
+
export interface AppDatabase<T, S, A = unknown> {
|
|
70
70
|
readonly binding: Binding;
|
|
71
|
-
request(identity: Identity, options?: RequestOptions): RequestDatabase<T, S>;
|
|
71
|
+
request(identity: Identity, options?: RequestOptions): RequestDatabase<T, S, A>;
|
|
72
72
|
/**
|
|
73
73
|
* Seals again, with the key in force, every encrypted value sealed with an
|
|
74
74
|
* older one — what follows a rotation of the project's field keys, once the
|
|
@@ -85,9 +85,11 @@ export interface AppDatabase<T, S> {
|
|
|
85
85
|
}[]>;
|
|
86
86
|
}
|
|
87
87
|
/** The engine over `binding`'s version, its `Database` typed by `T` (public's tables) and `S` (the other schemas'). */
|
|
88
|
-
export declare function createDatabase<T = Record<never, never>, S = Record<never, never
|
|
88
|
+
export declare function createDatabase<T = Record<never, never>, S = Record<never, never>, A = unknown>(options: DatabaseOptions): Promise<AppDatabase<T, S, A>>;
|
|
89
89
|
export type { DatabaseAdmissionOptions } from './admission.js';
|
|
90
90
|
export { fieldAddress, type FieldCipher, type FieldsSource } from '../fields.js';
|
|
91
|
+
export { AccessRefused, accessNames, accessTypesBlock } from './access.js';
|
|
92
|
+
export type { AccessFilter, AccessHistoryEntry, AccessId, AccessNameOf, AccessNames, AccessObject, AccessOps, AccessRefusal, AccessRelationOf, AccessRelationship, AccessSubject, AccessSubjectOf, AccessTypeOf } from './access.js';
|
|
91
93
|
export type { AtomicClient, DBClient, DBOps, FindManyOptions, RowLock, SearchOptions } from './client.js';
|
|
92
94
|
export type { Embed } from './search.js';
|
|
93
95
|
export type { DatabaseBudget, DatabaseDiagnostics, DatabaseDiagnosticsOptions, DatabaseQueryEvent } from './diagnostics.js';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type AccessAsker, type AccessNames, type AccessVia } from './access.js';
|
|
1
2
|
import type { DBClient } from './client.js';
|
|
2
3
|
import type { EnvTypedDatabase } from './typed.js';
|
|
3
4
|
/**
|
|
@@ -6,4 +7,9 @@ import type { EnvTypedDatabase } from './typed.js';
|
|
|
6
7
|
* `S` the other schemas'). `$asService()` is built on each call, over the
|
|
7
8
|
* request's one service transaction.
|
|
8
9
|
*/
|
|
9
|
-
export declare function typedDatabase<T, S>(client: DBClient
|
|
10
|
+
export declare function typedDatabase<T, S, A = unknown>(client: DBClient, options?: {
|
|
11
|
+
access?: AccessNames;
|
|
12
|
+
as?: AccessAsker;
|
|
13
|
+
unanswered?: string;
|
|
14
|
+
via?: AccessVia;
|
|
15
|
+
}): EnvTypedDatabase<T, S, A>;
|