rizolang 0.0.0-stage → 0.3.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.
- package/LICENSE +202 -0
- package/README.md +430 -2
- package/build/src/ast.d.ts +148 -0
- package/build/src/ast.js +4 -0
- package/build/src/ast.js.map +1 -0
- package/build/src/bin.d.ts +2 -0
- package/build/src/bin.js +8 -0
- package/build/src/bin.js.map +1 -0
- package/build/src/check-api.d.ts +104 -0
- package/build/src/check-api.js +564 -0
- package/build/src/check-api.js.map +1 -0
- package/build/src/checker.d.ts +53 -0
- package/build/src/checker.js +235 -0
- package/build/src/checker.js.map +1 -0
- package/build/src/cli.d.ts +12 -0
- package/build/src/cli.js +170 -0
- package/build/src/cli.js.map +1 -0
- package/build/src/compiler.d.ts +38 -0
- package/build/src/compiler.js +62 -0
- package/build/src/compiler.js.map +1 -0
- package/build/src/diagnostics.d.ts +35 -0
- package/build/src/diagnostics.js +69 -0
- package/build/src/diagnostics.js.map +1 -0
- package/build/src/emit/routes.d.ts +22 -0
- package/build/src/emit/routes.js +316 -0
- package/build/src/emit/routes.js.map +1 -0
- package/build/src/emit/sql.d.ts +11 -0
- package/build/src/emit/sql.js +26 -0
- package/build/src/emit/sql.js.map +1 -0
- package/build/src/emit/typescript.d.ts +13 -0
- package/build/src/emit/typescript.js +77 -0
- package/build/src/emit/typescript.js.map +1 -0
- package/build/src/lexer.d.ts +17 -0
- package/build/src/lexer.js +124 -0
- package/build/src/lexer.js.map +1 -0
- package/build/src/migrations.d.ts +33 -0
- package/build/src/migrations.js +183 -0
- package/build/src/migrations.js.map +1 -0
- package/build/src/parser.d.ts +17 -0
- package/build/src/parser.js +434 -0
- package/build/src/parser.js.map +1 -0
- package/build/src/schema.d.ts +63 -0
- package/build/src/schema.js +151 -0
- package/build/src/schema.js.map +1 -0
- package/build/src/types.d.ts +42 -0
- package/build/src/types.js +77 -0
- package/build/src/types.js.map +1 -0
- package/build/src/version.d.ts +2 -0
- package/build/src/version.js +3 -0
- package/build/src/version.js.map +1 -0
- package/docs/SPEC-COMPACT.md +157 -0
- package/docs/SPEC.md +685 -0
- package/package.json +51 -4
- package/runtime/migrate.ts +131 -0
- package/runtime/password.ts +58 -0
- package/runtime/ratelimit.ts +53 -0
- package/runtime/server.ts +696 -0
- package/runtime/session.ts +50 -0
- package/runtime/types.ts +111 -0
- package/runtime/validate.ts +133 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { FIELD_TYPES } from "./types.js";
|
|
3
|
+
/** Name of the table that stores login sessions. Model tables can never start with "_". */
|
|
4
|
+
export const SESSIONS_TABLE = "_rizo_sessions";
|
|
5
|
+
const MAX_NAME = 63;
|
|
6
|
+
/** A constraint or index name, shortened with a hash if PostgreSQL would truncate it. */
|
|
7
|
+
export function constraintName(table, column, suffix) {
|
|
8
|
+
const full = `${table}_${column}_${suffix}`;
|
|
9
|
+
if (full.length <= MAX_NAME)
|
|
10
|
+
return full;
|
|
11
|
+
const hash = createHash("sha256").update(full).digest("hex").slice(0, 8);
|
|
12
|
+
return `${full.slice(0, MAX_NAME - suffix.length - hash.length - 2)}_${hash}_${suffix}`;
|
|
13
|
+
}
|
|
14
|
+
/** A role name as a SQL string literal. Role names are validated as [a-z][A-Za-z0-9]*. */
|
|
15
|
+
export const roleLiteral = (role) => {
|
|
16
|
+
if (!/^[a-z][A-Za-z0-9]*$/.test(role))
|
|
17
|
+
throw new Error(`unsafe role: ${role}`);
|
|
18
|
+
return `'${role}'`;
|
|
19
|
+
};
|
|
20
|
+
/** Quote a compiler-validated identifier. Names contain only [a-z0-9_], so this is always safe. */
|
|
21
|
+
export const q = (identifier) => {
|
|
22
|
+
if (!/^[a-z_][a-z0-9_]*$/.test(identifier))
|
|
23
|
+
throw new Error(`unsafe identifier: ${identifier}`);
|
|
24
|
+
return `"${identifier}"`;
|
|
25
|
+
};
|
|
26
|
+
const idColumn = {
|
|
27
|
+
name: "id", type: "UUID", primaryKey: true, default: "gen_random_uuid()", notNull: false,
|
|
28
|
+
unique: null, check: null, values: null, comment: null,
|
|
29
|
+
};
|
|
30
|
+
export function buildSchema(models, auth) {
|
|
31
|
+
const tables = models.map((model) => {
|
|
32
|
+
const columns = [idColumn, ...model.fields.map((f) => fieldColumn(model.table, f))];
|
|
33
|
+
if (auth !== undefined && auth.model === model && auth.roles.length > 0) {
|
|
34
|
+
columns.push({
|
|
35
|
+
name: "role", type: "TEXT", primaryKey: false, default: null, notNull: false, unique: null,
|
|
36
|
+
check: { name: constraintName(model.table, "role", "check"), expr: `${q("role")} IN (${auth.roles.map(roleLiteral).join(", ")})` },
|
|
37
|
+
values: [...auth.roles],
|
|
38
|
+
comment: "role: assigned by admins only, never accepted from requests or returned",
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
const foreignKeys = model.fields.flatMap((f) => {
|
|
42
|
+
if (f.link === undefined)
|
|
43
|
+
return [];
|
|
44
|
+
const target = models.find((m) => m.name === f.link);
|
|
45
|
+
if (target === undefined)
|
|
46
|
+
throw new Error(`unresolved link ${model.name}.${f.name}`);
|
|
47
|
+
return [{
|
|
48
|
+
name: constraintName(model.table, f.column, "fkey"),
|
|
49
|
+
column: f.column,
|
|
50
|
+
refTable: target.table,
|
|
51
|
+
onDelete: f.optional ? "SET NULL" : "RESTRICT",
|
|
52
|
+
index: constraintName(model.table, f.column, "idx"),
|
|
53
|
+
}];
|
|
54
|
+
});
|
|
55
|
+
return { name: model.table, columns, foreignKeys, comment: null };
|
|
56
|
+
});
|
|
57
|
+
if (auth !== undefined) {
|
|
58
|
+
const t = SESSIONS_TABLE;
|
|
59
|
+
const col = (name, type, extra) => ({
|
|
60
|
+
name, type, primaryKey: false, default: null, notNull: true, unique: null, check: null, values: null, comment: null, ...extra,
|
|
61
|
+
});
|
|
62
|
+
tables.push({
|
|
63
|
+
name: t,
|
|
64
|
+
comment: "Login sessions. Only a SHA-256 hash of each session token is stored.",
|
|
65
|
+
columns: [
|
|
66
|
+
col("token_hash", "TEXT", { primaryKey: true, notNull: false }),
|
|
67
|
+
col("user_id", "UUID", {}),
|
|
68
|
+
col("created_at", "TIMESTAMPTZ", { default: "now()" }),
|
|
69
|
+
col("expires_at", "TIMESTAMPTZ", {}),
|
|
70
|
+
],
|
|
71
|
+
foreignKeys: [{
|
|
72
|
+
name: constraintName(t, "user_id", "fkey"), column: "user_id", refTable: auth.model.table,
|
|
73
|
+
onDelete: "CASCADE", index: constraintName(t, "user_id", "idx"),
|
|
74
|
+
}],
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
return { version: 1, tables };
|
|
78
|
+
}
|
|
79
|
+
function fieldColumn(table, f) {
|
|
80
|
+
const info = FIELD_TYPES[f.type];
|
|
81
|
+
const col = q(f.column);
|
|
82
|
+
const checks = [];
|
|
83
|
+
if (info.secret) {
|
|
84
|
+
// Password limits apply to the plain-text input, not to the stored hash.
|
|
85
|
+
}
|
|
86
|
+
else if (info.length !== undefined) {
|
|
87
|
+
checks.push(...rangeChecks(`char_length(${col})`, f.min !== undefined && f.min > 0 ? f.min : undefined, f.max));
|
|
88
|
+
}
|
|
89
|
+
else if (info.valueRange !== undefined) {
|
|
90
|
+
checks.push(...rangeChecks(col, f.min, f.max));
|
|
91
|
+
}
|
|
92
|
+
if (f.type === "Email") {
|
|
93
|
+
checks.push(`${col} = lower(${col})`);
|
|
94
|
+
checks.push(`${col} ~ '^[^@[:space:]]+@[^@[:space:]]+[.][^@[:space:]]+$'`);
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
name: f.column,
|
|
98
|
+
type: info.sql,
|
|
99
|
+
primaryKey: false,
|
|
100
|
+
default: null,
|
|
101
|
+
notNull: !f.optional,
|
|
102
|
+
unique: f.unique ? constraintName(table, f.column, "key") : null,
|
|
103
|
+
check: checks.length > 0 ? { name: constraintName(table, f.column, "check"), expr: checks.join(" AND ") } : null,
|
|
104
|
+
values: null,
|
|
105
|
+
comment: info.secret ? `${f.name}: stored only as a salted hash, never as plain text` : null,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function rangeChecks(expr, min, max) {
|
|
109
|
+
if (min !== undefined && max !== undefined)
|
|
110
|
+
return [`${expr} BETWEEN ${min} AND ${max}`];
|
|
111
|
+
if (min !== undefined)
|
|
112
|
+
return [`${expr} >= ${min}`];
|
|
113
|
+
if (max !== undefined)
|
|
114
|
+
return [`${expr} <= ${max}`];
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
// ---------------------------------------------------------------- rendering
|
|
118
|
+
export function columnDefinition(c) {
|
|
119
|
+
const parts = [`${q(c.name)} ${c.type}`];
|
|
120
|
+
if (c.primaryKey)
|
|
121
|
+
parts.push("PRIMARY KEY");
|
|
122
|
+
if (c.default !== null)
|
|
123
|
+
parts.push(`DEFAULT ${c.default}`);
|
|
124
|
+
if (c.notNull)
|
|
125
|
+
parts.push("NOT NULL");
|
|
126
|
+
if (c.unique !== null)
|
|
127
|
+
parts.push(`CONSTRAINT ${q(c.unique)} UNIQUE`);
|
|
128
|
+
if (c.check !== null)
|
|
129
|
+
parts.push(`CONSTRAINT ${q(c.check.name)} CHECK (${c.check.expr})`);
|
|
130
|
+
return parts.join(" ");
|
|
131
|
+
}
|
|
132
|
+
export function createTable(t) {
|
|
133
|
+
const lines = t.columns.map((c) => `${c.comment !== null ? ` -- ${c.comment}\n` : ""} ${columnDefinition(c)}`);
|
|
134
|
+
return `${t.comment !== null ? `-- ${t.comment}\n` : ""}CREATE TABLE ${q(t.name)} (\n${lines.join(",\n")}\n);`;
|
|
135
|
+
}
|
|
136
|
+
export function addForeignKey(table, fk) {
|
|
137
|
+
return `ALTER TABLE ${q(table)} ADD CONSTRAINT ${q(fk.name)} FOREIGN KEY (${q(fk.column)}) REFERENCES ${q(fk.refTable)} (${q("id")}) ON DELETE ${fk.onDelete};`;
|
|
138
|
+
}
|
|
139
|
+
export function createIndex(table, fk) {
|
|
140
|
+
return `CREATE INDEX ${q(fk.index)} ON ${q(table)} (${q(fk.column)});`;
|
|
141
|
+
}
|
|
142
|
+
/** Statements that create the whole schema. Foreign keys come last, so tables may link in any order. */
|
|
143
|
+
export function createStatements(schema) {
|
|
144
|
+
const out = schema.tables.map(createTable);
|
|
145
|
+
for (const t of schema.tables) {
|
|
146
|
+
for (const fk of t.foreignKeys)
|
|
147
|
+
out.push(addForeignKey(t.name, fk), createIndex(t.name, fk));
|
|
148
|
+
}
|
|
149
|
+
return out;
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAGzC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAkDzC,2FAA2F;AAC3F,MAAM,CAAC,MAAM,cAAc,GAAG,gBAAgB,CAAC;AAE/C,MAAM,QAAQ,GAAG,EAAE,CAAC;AAEpB,yFAAyF;AACzF,MAAM,UAAU,cAAc,CAAC,KAAa,EAAE,MAAc,EAAE,MAAc;IAC1E,MAAM,IAAI,GAAG,GAAG,KAAK,IAAI,MAAM,IAAI,MAAM,EAAE,CAAC;IAC5C,IAAI,IAAI,CAAC,MAAM,IAAI,QAAQ;QAAE,OAAO,IAAI,CAAC;IACzC,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACzE,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,IAAI,IAAI,IAAI,MAAM,EAAE,CAAC;AAC1F,CAAC;AAED,0FAA0F;AAC1F,MAAM,CAAC,MAAM,WAA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C,CAAC,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,eAAe,EAAE,CAAC,QAAQ,GAAG,CAAC;AAClK,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,EAAc;IACvD,OAAO,gBAAgB,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC;AACzE,CAAC;AAED,wGAAwG;AACxG,MAAM,UAAU,gBAAgB,CAAC,MAAc;IAC7C,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAC3C,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;QAC9B,KAAK,MAAM,EAAE,IAAI,CAAC,CAAC,WAAW;YAAE,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;IAC/F,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ModifierKind } from "./ast.js";
|
|
2
|
+
/**
|
|
3
|
+
* The field type table: the single source of truth for what each Rizo Lang
|
|
4
|
+
* field type compiles to and which modifiers it accepts.
|
|
5
|
+
*/
|
|
6
|
+
export type FieldTypeName = "Text" | "Number" | "Bool" | "Email" | "Password" | "Date" | "ID";
|
|
7
|
+
export interface LengthRule {
|
|
8
|
+
/** Applied when no `min` is written (0 = no lower limit). */
|
|
9
|
+
readonly defaultMin: number;
|
|
10
|
+
/** Applied when no `max` is written. */
|
|
11
|
+
readonly defaultMax: number;
|
|
12
|
+
/** Smallest value `min`/`max` may be set to. */
|
|
13
|
+
readonly lowest: number;
|
|
14
|
+
/** Largest value `min`/`max` may be set to. */
|
|
15
|
+
readonly highest: number;
|
|
16
|
+
}
|
|
17
|
+
export interface FieldTypeInfo {
|
|
18
|
+
readonly sql: string;
|
|
19
|
+
/** TypeScript type in the stored (server-side) type. */
|
|
20
|
+
readonly ts: string;
|
|
21
|
+
/** TypeScript type in the input type (what a client sends). */
|
|
22
|
+
readonly tsInput: string;
|
|
23
|
+
readonly modifiers: readonly ModifierKind[];
|
|
24
|
+
/** For text-like types: min/max limit the length in characters. */
|
|
25
|
+
readonly length?: LengthRule;
|
|
26
|
+
/** For numbers: min/max limit the value. */
|
|
27
|
+
readonly valueRange?: {
|
|
28
|
+
readonly lowest: number;
|
|
29
|
+
readonly highest: number;
|
|
30
|
+
};
|
|
31
|
+
/** Never included in public types. */
|
|
32
|
+
readonly secret: boolean;
|
|
33
|
+
/** Human-readable reason a modifier is not allowed, keyed by modifier. */
|
|
34
|
+
readonly disallowedReason?: Partial<Record<ModifierKind, string>>;
|
|
35
|
+
}
|
|
36
|
+
export declare const INT32_MIN = -2147483648;
|
|
37
|
+
export declare const INT32_MAX = 2147483647;
|
|
38
|
+
export declare const FIELD_TYPES: Readonly<Record<FieldTypeName, FieldTypeInfo>>;
|
|
39
|
+
export declare const FIELD_TYPE_NAMES: FieldTypeName[];
|
|
40
|
+
export declare function isFieldTypeName(name: string): name is FieldTypeName;
|
|
41
|
+
/** Type names from other languages, mapped to the Rizo Lang type to use instead. */
|
|
42
|
+
export declare const TYPE_ALIASES: Readonly<Record<string, FieldTypeName>>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export const INT32_MIN = -2147483648;
|
|
2
|
+
export const INT32_MAX = 2147483647;
|
|
3
|
+
export const FIELD_TYPES = {
|
|
4
|
+
Text: {
|
|
5
|
+
sql: "TEXT", ts: "string", tsInput: "string",
|
|
6
|
+
modifiers: ["unique", "optional", "private", "min", "max"],
|
|
7
|
+
length: { defaultMin: 0, defaultMax: 10_000, lowest: 0, highest: 1_000_000 },
|
|
8
|
+
secret: false,
|
|
9
|
+
},
|
|
10
|
+
Number: {
|
|
11
|
+
sql: "INTEGER", ts: "number", tsInput: "number",
|
|
12
|
+
modifiers: ["unique", "optional", "private", "min", "max"],
|
|
13
|
+
valueRange: { lowest: INT32_MIN, highest: INT32_MAX },
|
|
14
|
+
secret: false,
|
|
15
|
+
},
|
|
16
|
+
Bool: {
|
|
17
|
+
sql: "BOOLEAN", ts: "boolean", tsInput: "boolean",
|
|
18
|
+
modifiers: ["optional", "private"],
|
|
19
|
+
secret: false,
|
|
20
|
+
disallowedReason: {
|
|
21
|
+
unique: "A Bool has only two values, so it cannot be unique.",
|
|
22
|
+
min: "A Bool has no size or range.",
|
|
23
|
+
max: "A Bool has no size or range.",
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
Email: {
|
|
27
|
+
sql: "TEXT", ts: "string", tsInput: "string",
|
|
28
|
+
modifiers: ["unique", "optional", "private", "min", "max"],
|
|
29
|
+
length: { defaultMin: 3, defaultMax: 254, lowest: 3, highest: 254 },
|
|
30
|
+
secret: false,
|
|
31
|
+
},
|
|
32
|
+
Password: {
|
|
33
|
+
sql: "TEXT", ts: "PasswordHash", tsInput: "string",
|
|
34
|
+
modifiers: ["optional", "min", "max"],
|
|
35
|
+
length: { defaultMin: 8, defaultMax: 128, lowest: 8, highest: 128 },
|
|
36
|
+
secret: true,
|
|
37
|
+
disallowedReason: {
|
|
38
|
+
unique: "Passwords are stored as salted hashes, so they cannot be unique.",
|
|
39
|
+
private: "Password fields are always hidden, so private is not needed.",
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
Date: {
|
|
43
|
+
sql: "TIMESTAMPTZ", ts: "Date", tsInput: "Date",
|
|
44
|
+
modifiers: ["unique", "optional", "private"],
|
|
45
|
+
secret: false,
|
|
46
|
+
disallowedReason: {
|
|
47
|
+
min: "Date ranges are not supported yet.",
|
|
48
|
+
max: "Date ranges are not supported yet.",
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
ID: {
|
|
52
|
+
sql: "UUID", ts: "string", tsInput: "string",
|
|
53
|
+
modifiers: ["unique", "optional", "private"],
|
|
54
|
+
secret: false,
|
|
55
|
+
disallowedReason: {
|
|
56
|
+
min: "An ID is a fixed-size UUID and has no range.",
|
|
57
|
+
max: "An ID is a fixed-size UUID and has no range.",
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
export const FIELD_TYPE_NAMES = Object.keys(FIELD_TYPES);
|
|
62
|
+
export function isFieldTypeName(name) {
|
|
63
|
+
return Object.hasOwn(FIELD_TYPES, name);
|
|
64
|
+
}
|
|
65
|
+
/** Type names from other languages, mapped to the Rizo Lang type to use instead. */
|
|
66
|
+
export const TYPE_ALIASES = {
|
|
67
|
+
String: "Text", string: "Text", Str: "Text", str: "Text", Varchar: "Text", varchar: "Text", Char: "Text",
|
|
68
|
+
text: "Text",
|
|
69
|
+
Int: "Number", int: "Number", Integer: "Number", integer: "Number", Float: "Number", float: "Number",
|
|
70
|
+
Double: "Number", Decimal: "Number", number: "Number", BigInt: "Number",
|
|
71
|
+
Boolean: "Bool", boolean: "Bool", bool: "Bool",
|
|
72
|
+
DateTime: "Date", Datetime: "Date", Timestamp: "Date", timestamp: "Date", date: "Date", Time: "Date",
|
|
73
|
+
UUID: "ID", Uuid: "ID", uuid: "ID", Id: "ID", id: "ID",
|
|
74
|
+
email: "Email", EmailAddress: "Email",
|
|
75
|
+
password: "Password", Hash: "Password",
|
|
76
|
+
};
|
|
77
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAqCA,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,UAAU,CAAC;AACrC,MAAM,CAAC,MAAM,SAAS,GAAG,UAAU,CAAC;AAEpC,MAAM,CAAC,MAAM,WAAW,GAAmD;IACzE,IAAI,EAAE;QACJ,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE,Q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AI,EAAE,MAAM;IACxG,IAAI,EAAE,MAAM;IACZ,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,QAAQ;IACpG,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ;IACvE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAC9C,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IACpG,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI;IACtD,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,OAAO;IACrC,QAAQ,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU;CACvC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,oEAAoE;AACpE,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# Rizo Lang (compact spec, v0.3)
|
|
2
|
+
|
|
3
|
+
You are writing Rizo Lang (`.rz` files). Follow these rules exactly. A file contains only `model` blocks, at most one `auth` line, and `api` blocks. Never write SQL, TypeScript or `page` blocks in `.rz` files.
|
|
4
|
+
|
|
5
|
+
## Example
|
|
6
|
+
|
|
7
|
+
```rizo
|
|
8
|
+
// comment
|
|
9
|
+
auth User by email
|
|
10
|
+
|
|
11
|
+
model User {
|
|
12
|
+
email: Email unique private
|
|
13
|
+
password: Password min 12
|
|
14
|
+
name: Text max 100
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
model Post {
|
|
18
|
+
author: User
|
|
19
|
+
title: Text min 1 max 200
|
|
20
|
+
views: Number min 0 optional
|
|
21
|
+
published: Bool
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
api GET /posts -> list Post.public {
|
|
25
|
+
public
|
|
26
|
+
return db.Post.list(published = true, order title, limit 20)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
api GET /posts/:id -> Post.public {
|
|
30
|
+
public
|
|
31
|
+
return db.Post.find(id)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
api POST /posts -> Post.public {
|
|
35
|
+
requires login
|
|
36
|
+
return db.Post.create(body, author = me)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
api PATCH /posts/:id -> Post.public {
|
|
40
|
+
requires login
|
|
41
|
+
requires owner author
|
|
42
|
+
return db.Post.update(id, body)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
api DELETE /posts/:id {
|
|
46
|
+
requires login
|
|
47
|
+
requires owner author
|
|
48
|
+
return db.Post.delete(id)
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Models
|
|
53
|
+
|
|
54
|
+
- Write `model Name {` with a PascalCase name, one field per line as `name: Type modifiers`, then `}`. No commas or semicolons.
|
|
55
|
+
- Field names are camelCase. Names contain only letters and digits.
|
|
56
|
+
- Every model gets `id: ID` automatically. Never declare `id`.
|
|
57
|
+
|
|
58
|
+
| Type | Meaning | Modifiers allowed |
|
|
59
|
+
|------------|-------------------------------------------------|-------------------------------------|
|
|
60
|
+
| `Text` | text, max 10000 chars by default | unique optional private min max |
|
|
61
|
+
| `Number` | whole number (32-bit). Use cents for money | unique optional private min max |
|
|
62
|
+
| `Bool` | true/false | optional private |
|
|
63
|
+
| `Email` | email address, 3–254 chars, stored lowercase | unique optional private min max |
|
|
64
|
+
| `Password` | hashed, never returned, 8–128 chars | optional min max |
|
|
65
|
+
| `Date` | date and time (UTC) | unique optional private |
|
|
66
|
+
| `ID` | UUID | unique optional private |
|
|
67
|
+
| *Model* | link to another model, e.g. `author: User` | unique optional private |
|
|
68
|
+
|
|
69
|
+
Modifiers:
|
|
70
|
+
- `unique`: no two records share the value.
|
|
71
|
+
- `optional`: the value may be null.
|
|
72
|
+
- `private`: never returned by the API.
|
|
73
|
+
- `min N` / `max N`: the length for text, the value for Number.
|
|
74
|
+
|
|
75
|
+
A field named `password` (or ending in `Password`) must have the type `Password`.
|
|
76
|
+
|
|
77
|
+
## auth
|
|
78
|
+
|
|
79
|
+
`auth User by email` turns on accounts. User needs `email: Email unique` and one `Password` field. It adds `POST /auth/signup`, `POST /auth/login`, `POST /auth/logout`, `POST /auth/logout-all`, `GET /auth/me`, `PATCH /auth/me` (edit profile), `POST /auth/password` and `DELETE /auth/me`. Sessions use a secure cookie. Don't write your own login endpoints.
|
|
80
|
+
|
|
81
|
+
Roles: `auth User by email roles admin, editor` (must include admin). Admins assign roles with `PUT /auth/users/:id/role`. Never add a `role` field yourself; it is hidden and never comes from request bodies.
|
|
82
|
+
|
|
83
|
+
## api
|
|
84
|
+
|
|
85
|
+
```
|
|
86
|
+
api METHOD /path/:param -> Model.public {
|
|
87
|
+
public (or: requires login)
|
|
88
|
+
requires role admin, editor (optional; needs requires login)
|
|
89
|
+
requires owner <link field> (optional; add "or role admin" to let admins act too)
|
|
90
|
+
return db.Model.action(arguments)
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
- Every endpoint has **exactly one** of `public` or `requires login`.
|
|
95
|
+
- `requires role admin` gives 403 to users without that role.
|
|
96
|
+
- `requires owner author` lets only the record's owner act; others get 404. It needs `requires login`, and it works with find, update and delete. It is **required** on update and delete of a model that links to the auth model.
|
|
97
|
+
- The return type is `-> Model.public` or `-> list Model.public`, and there is none for delete. Only `.public` can be returned.
|
|
98
|
+
- Paths use lowercase segments and `:camelCase` parameters. Every parameter must be used. `/auth` is reserved.
|
|
99
|
+
|
|
100
|
+
| Action | Method | Result |
|
|
101
|
+
|---------------------------------|--------------|--------------------|
|
|
102
|
+
| `db.M.find(id)` | GET | one record or 404 |
|
|
103
|
+
| `db.M.list(filters, order f desc, limit N)` | GET | `{ items, next }` page (limit ≤ 100, default 50) |
|
|
104
|
+
| `db.M.create(body)` | POST | 201 new record |
|
|
105
|
+
| `db.M.update(id, body)` | PUT or PATCH | changed record or 404 |
|
|
106
|
+
| `db.M.delete(id)` | DELETE | 204 or 404 |
|
|
107
|
+
|
|
108
|
+
- Filters are `field = value`. A value is `me`, a path parameter, a number, `true` or `false`.
|
|
109
|
+
- `body` is the validated JSON request body.
|
|
110
|
+
- `list` returns pages: clients call `?limit=10`, then `?after=<next>`. You can't order by an optional field.
|
|
111
|
+
- A link to the auth model must be set with `= me` in create, and is never taken from body.
|
|
112
|
+
- You cannot create, update or delete the auth model through `api`.
|
|
113
|
+
|
|
114
|
+
## Migrations
|
|
115
|
+
|
|
116
|
+
`rizolang build` writes `migrations/NNNN_*.sql` when models change. Removing a model or field, changing a field's type, or removing a role is refused (RZ0904). Never rename a field; add a new one instead. Apply migrations with `node migrate.ts`.
|
|
117
|
+
|
|
118
|
+
## Errors
|
|
119
|
+
|
|
120
|
+
The format is `file.rz:LINE:COL error RZxxxx: message`, followed by a `fix:` line. Apply the fix exactly as written.
|
|
121
|
+
|
|
122
|
+
## Wrong → Right
|
|
123
|
+
|
|
124
|
+
```rizo
|
|
125
|
+
name: String → name: Text
|
|
126
|
+
age: Int → age: Number
|
|
127
|
+
active: Boolean → active: Bool
|
|
128
|
+
createdAt: DateTime → createdAt: Date
|
|
129
|
+
userId: UUID → userId: ID
|
|
130
|
+
email: Email, → email: Email
|
|
131
|
+
name: Text(100) → name: Text max 100
|
|
132
|
+
email: Email @unique → email: Email unique
|
|
133
|
+
nickname?: Text → nickname: Text optional
|
|
134
|
+
first_name: Text → firstName: Text
|
|
135
|
+
model user { → model User {
|
|
136
|
+
id: ID → (remove it; id is automatic)
|
|
137
|
+
password: Text → password: Password
|
|
138
|
+
password: Password unique → password: Password
|
|
139
|
+
password: Password min 4 → password: Password min 8
|
|
140
|
+
price: Number min 0.5 → priceCents: Number min 50
|
|
141
|
+
isAdmin: Bool max 1 → isAdmin: Bool
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
In api blocks:
|
|
145
|
+
- Every endpoint needs `public` or `requires login`; never leave it out.
|
|
146
|
+
- Write `db.Post.create(body, author = me)`, not `db.Post.create(body)`.
|
|
147
|
+
- Write `-> Post.public`, not `-> Post`.
|
|
148
|
+
- Use `api DELETE /posts/:id`, not `api GET /posts/:id/delete`.
|
|
149
|
+
- Write `requires owner author` on update and delete of user-owned records.
|
|
150
|
+
|
|
151
|
+
## Checklist before answering
|
|
152
|
+
|
|
153
|
+
1. Only `model`, `auth` and `api` blocks, with one item per line.
|
|
154
|
+
2. Every type is Text, Number, Bool, Email, Password, Date, ID or a model name.
|
|
155
|
+
3. Model names are PascalCase, field names are camelCase, and no field is named `id`.
|
|
156
|
+
4. Every endpoint has `public` or `requires login`, a matching method and one `return` line.
|
|
157
|
+
5. Owned records use `author = me` on create and `requires owner` on update and delete.
|