@lunora/codegen 0.0.0 → 1.0.0-alpha.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 (34) hide show
  1. package/LICENSE.md +105 -0
  2. package/README.md +117 -9
  3. package/__assets__/package-og.svg +14 -0
  4. package/dist/index.d.mts +1473 -0
  5. package/dist/index.d.ts +1473 -0
  6. package/dist/index.mjs +28 -0
  7. package/dist/packem_shared/CONTAINERS_FILENAME-0K-pjNb8.mjs +224 -0
  8. package/dist/packem_shared/CodegenDiagnosticError-54jWDxA9.mjs +22 -0
  9. package/dist/packem_shared/LUNORA_ERROR_CODES-CySpQPD3.mjs +61 -0
  10. package/dist/packem_shared/buildOpenApiDocument-yHVN66Xd.mjs +183 -0
  11. package/dist/packem_shared/buildOpenRpcDocument-BZGY1-jT.mjs +60 -0
  12. package/dist/packem_shared/buildSchemaSnapshot-DzLDbWk3.mjs +233 -0
  13. package/dist/packem_shared/createCodegenProject-DGJm0_Pk.mjs +895 -0
  14. package/dist/packem_shared/discover-ast-CT6BgBr4.mjs +13 -0
  15. package/dist/packem_shared/discoverAuthApiCalls-C35R6z0T.mjs +62 -0
  16. package/dist/packem_shared/discoverCrons-BL6iGuJ3.mjs +254 -0
  17. package/dist/packem_shared/discoverFunctions-DEgAcRuD.mjs +460 -0
  18. package/dist/packem_shared/discoverHttpRoutes-C978pBiG.mjs +131 -0
  19. package/dist/packem_shared/discoverInserts-CRQdXvHO.mjs +39 -0
  20. package/dist/packem_shared/discoverMaskProcedures-B64zA740.mjs +217 -0
  21. package/dist/packem_shared/discoverMigrations-Doj_-BAA.mjs +91 -0
  22. package/dist/packem_shared/discoverNondeterministicCalls-4KiPQxQU.mjs +122 -0
  23. package/dist/packem_shared/discoverQueries-BkIi0dBD.mjs +62 -0
  24. package/dist/packem_shared/discoverRlsMetadata-DpRB1HMe.mjs +280 -0
  25. package/dist/packem_shared/discoverSchema-BBulgGbH.mjs +542 -0
  26. package/dist/packem_shared/discoverStorageRulesMetadata-DAqJUxUv.mjs +97 -0
  27. package/dist/packem_shared/discoverWorkflows-DRDQdhfq.mjs +84 -0
  28. package/dist/packem_shared/emitApi-hRVC-kE7.mjs +2426 -0
  29. package/dist/packem_shared/emitApp-CzZ6GbrD.mjs +593 -0
  30. package/dist/packem_shared/lintSchema-DicbOHvH.mjs +68 -0
  31. package/dist/packem_shared/parse-validator-tuQtHrsr.mjs +132 -0
  32. package/dist/packem_shared/paths-BRd6JHuF.mjs +11 -0
  33. package/dist/packem_shared/schemaFromIr-DTYsLBaA.mjs +57 -0
  34. package/package.json +45 -17
@@ -0,0 +1,132 @@
1
+ import { Node } from 'ts-morph';
2
+
3
+ const FIELD_NAME_RE = /^[A-Za-z_$][\w$]*$/u;
4
+ const COLUMN_MODIFIERS = /* @__PURE__ */ new Set(["$defaultFn", "$onUpdateFn", "$type", "default", "defaultNow", "nullable", "unique"]);
5
+ const applyColumnModifier = (base, modifier) => {
6
+ const column = { notNull: true, ...base.column };
7
+ switch (modifier) {
8
+ case "$defaultFn":
9
+ case "default":
10
+ case "defaultNow": {
11
+ column.hasDefault = true;
12
+ break;
13
+ }
14
+ case "$onUpdateFn": {
15
+ column.hasOnUpdate = true;
16
+ break;
17
+ }
18
+ case "$type": {
19
+ break;
20
+ }
21
+ case "nullable": {
22
+ column.notNull = false;
23
+ break;
24
+ }
25
+ case "unique": {
26
+ column.unique = true;
27
+ break;
28
+ }
29
+ }
30
+ return { ...base, column };
31
+ };
32
+ const SCALAR_KINDS = /* @__PURE__ */ new Set(["any", "bigint", "boolean", "bytes", "date", "null", "number", "string", "timestamp"]);
33
+ const TRANSPARENT_MODIFIERS = /* @__PURE__ */ new Set(["check", "meta"]);
34
+ const parseValidator = (expression) => {
35
+ if (Node.isCallExpression(expression)) {
36
+ return parseValidatorCall(expression);
37
+ }
38
+ return { kind: "any", sourceText: expression.getText() };
39
+ };
40
+ const parseObjectShape = (object) => {
41
+ const out = {};
42
+ for (const property of object.getProperties()) {
43
+ if (!Node.isPropertyAssignment(property)) {
44
+ continue;
45
+ }
46
+ const nameNode = property.getNameNode();
47
+ if (Node.isComputedPropertyName(nameNode)) {
48
+ continue;
49
+ }
50
+ const initializer = property.getInitializer();
51
+ if (!initializer) {
52
+ continue;
53
+ }
54
+ const fieldName = property.getName();
55
+ if (!FIELD_NAME_RE.test(fieldName)) {
56
+ throw new Error(`@lunora/codegen: field name is not a valid JS identifier: ${JSON.stringify(fieldName)}`);
57
+ }
58
+ out[fieldName] = parseValidator(initializer);
59
+ }
60
+ return out;
61
+ };
62
+ const parseArgument = (argument, fallback) => argument && Node.isExpression(argument) ? parseValidator(argument) : fallback;
63
+ const parseBuilderMember = (member, args) => {
64
+ if (SCALAR_KINDS.has(member)) {
65
+ return { kind: member };
66
+ }
67
+ const [first, second] = args;
68
+ switch (member) {
69
+ case "array": {
70
+ return { inner: parseArgument(first, { kind: "any" }), kind: "array" };
71
+ }
72
+ case "from": {
73
+ return { kind: "from" };
74
+ }
75
+ case "id": {
76
+ return { kind: "id", tableName: first && Node.isStringLiteral(first) ? first.getLiteralText() : "_unknown_" };
77
+ }
78
+ case "literal": {
79
+ return {
80
+ kind: "literal",
81
+ // Captures the source text — for string/number/boolean/null literals
82
+ // this matches the TS type representation directly.
83
+ literalValue: first ? first.getText() : "undefined"
84
+ };
85
+ }
86
+ case "object": {
87
+ return first && Node.isObjectLiteralExpression(first) ? { kind: "object", shape: parseObjectShape(first) } : { kind: "object", shape: {} };
88
+ }
89
+ case "optional": {
90
+ return { inner: parseArgument(first, { kind: "any" }), kind: "optional" };
91
+ }
92
+ case "record": {
93
+ return {
94
+ keyType: parseArgument(first, { kind: "string" }),
95
+ kind: "record",
96
+ valueType: parseArgument(second, { kind: "any" })
97
+ };
98
+ }
99
+ case "storage": {
100
+ return first && Node.isStringLiteral(first) ? { bucket: first.getLiteralText(), kind: "storage" } : { kind: "storage" };
101
+ }
102
+ case "union": {
103
+ return {
104
+ kind: "union",
105
+ members: args.filter((argument) => Node.isExpression(argument)).map((argument) => parseValidator(argument))
106
+ };
107
+ }
108
+ default: {
109
+ throw new Error(`Unsupported validator kind: ${member}`);
110
+ }
111
+ }
112
+ };
113
+ const parseValidatorCall = (call) => {
114
+ const callee = call.getExpression();
115
+ if (!Node.isPropertyAccessExpression(callee)) {
116
+ return { kind: "any", sourceText: call.getText() };
117
+ }
118
+ const member = callee.getName();
119
+ const args = call.getArguments();
120
+ if (COLUMN_MODIFIERS.has(member)) {
121
+ const receiver = callee.getExpression();
122
+ const base = Node.isExpression(receiver) ? parseValidator(receiver) : { kind: "any" };
123
+ return applyColumnModifier(base, member);
124
+ }
125
+ if (TRANSPARENT_MODIFIERS.has(member)) {
126
+ const receiver = callee.getExpression();
127
+ return Node.isExpression(receiver) ? parseValidator(receiver) : { kind: "any" };
128
+ }
129
+ return parseBuilderMember(member, args);
130
+ };
131
+
132
+ export { parseObjectShape as a, parseValidator as p };
@@ -0,0 +1,11 @@
1
+ const INDEX_SUFFIX = /\/index$/u;
2
+ const NON_IDENTIFIER = /[^\dA-Za-z]/gu;
3
+ const sanitizeNamespace = (filePath) => (
4
+ // `lunora/ratelimit/index.ts` surfaces as `api.ratelimit.*` (and dispatches as
5
+ // `ratelimit:fn`) rather than the noisy `api.ratelimit_index.*` — the registry
6
+ // convention. Only a trailing `/index` is dropped, so `lunora/index.ts` and
7
+ // `lunora/ratelimit/queries.ts` (→ `ratelimit_queries`) are unaffected.
8
+ filePath.replace(INDEX_SUFFIX, "").replaceAll(NON_IDENTIFIER, "_")
9
+ );
10
+
11
+ export { sanitizeNamespace as s };
@@ -0,0 +1,57 @@
1
+ const parseLiteral = (text) => {
2
+ try {
3
+ return JSON.parse(text);
4
+ } catch {
5
+ const trimmed = text.trim();
6
+ if (trimmed.length >= 2 && (trimmed.startsWith("'") || trimmed.startsWith('"')) && trimmed.at(-1) === trimmed[0]) {
7
+ return trimmed.slice(1, -1);
8
+ }
9
+ return trimmed;
10
+ }
11
+ };
12
+ const convertValidator = (ir) => {
13
+ const meta = {};
14
+ if (ir.column !== void 0) {
15
+ meta.column = {
16
+ // A non-undefined `defaultValue` is the sentinel `hasServerDefault`
17
+ // reads to skip the column (the server fills `.default()`/`.$defaultFn()`).
18
+ // eslint-disable-next-line unicorn/no-null -- null is a deliberate non-undefined sentinel here
19
+ defaultValue: ir.column.hasDefault === true ? null : void 0,
20
+ notNull: ir.column.notNull,
21
+ unique: ir.column.unique
22
+ };
23
+ }
24
+ if (ir.inner !== void 0) {
25
+ meta.inner = convertValidator(ir.inner);
26
+ }
27
+ if (ir.members !== void 0) {
28
+ meta.members = ir.members.map((member) => convertValidator(member));
29
+ }
30
+ if (ir.shape !== void 0) {
31
+ meta.shape = Object.fromEntries(Object.entries(ir.shape).map(([field, child]) => [field, convertValidator(child)]));
32
+ }
33
+ if (ir.tableName !== void 0) {
34
+ meta.tableName = ir.tableName;
35
+ }
36
+ if (ir.literalValue !== void 0) {
37
+ meta.value = parseLiteral(ir.literalValue);
38
+ }
39
+ if (ir.keyType !== void 0) {
40
+ meta.keyValidator = convertValidator(ir.keyType);
41
+ }
42
+ if (ir.valueType !== void 0) {
43
+ meta.valueValidator = convertValidator(ir.valueType);
44
+ }
45
+ return { _meta: meta, kind: ir.kind };
46
+ };
47
+ const schemaFromIr = (ir) => {
48
+ const tables = Object.fromEntries(
49
+ ir.tables.map((table) => [
50
+ table.name,
51
+ { shape: Object.fromEntries(Object.entries(table.shape).map(([field, validator]) => [field, convertValidator(validator)])) }
52
+ ])
53
+ );
54
+ return { tables };
55
+ };
56
+
57
+ export { schemaFromIr };
package/package.json CHANGED
@@ -1,31 +1,59 @@
1
1
  {
2
2
  "name": "@lunora/codegen",
3
- "version": "0.0.0",
3
+ "version": "1.0.0-alpha.1",
4
4
  "description": "Code generator for Lunora: emits _generated/{api,server,dataModel}.ts from your schema",
5
- "license": "FSL-1.1-Apache-2.0",
5
+ "keywords": [
6
+ "cloudflare",
7
+ "codegen",
8
+ "durable-objects",
9
+ "lunora",
10
+ "schema",
11
+ "ts-morph",
12
+ "type-safe",
13
+ "workers"
14
+ ],
6
15
  "homepage": "https://lunora.sh",
16
+ "bugs": "https://github.com/anolilab/lunora/issues",
17
+ "license": "FSL-1.1-Apache-2.0",
18
+ "author": {
19
+ "name": "Daniel Bannert",
20
+ "email": "d.bannert@anolilab.de"
21
+ },
7
22
  "repository": {
8
23
  "type": "git",
9
24
  "url": "git+https://github.com/anolilab/lunora.git",
10
25
  "directory": "packages/codegen"
11
26
  },
12
- "bugs": {
13
- "url": "https://github.com/anolilab/lunora/issues"
14
- },
15
- "keywords": [
16
- "lunora",
17
- "cloudflare",
18
- "workers",
19
- "durable-objects",
20
- "codegen",
21
- "schema",
22
- "type-safe",
23
- "ts-morph"
27
+ "files": [
28
+ "dist",
29
+ "__assets__",
30
+ "README.md",
31
+ "LICENSE.md"
24
32
  ],
33
+ "type": "module",
34
+ "sideEffects": false,
35
+ "main": "./dist/index.mjs",
36
+ "module": "./dist/index.mjs",
37
+ "types": "./dist/index.d.ts",
38
+ "exports": {
39
+ ".": {
40
+ "types": "./dist/index.d.ts",
41
+ "import": "./dist/index.mjs"
42
+ },
43
+ "./package.json": "./package.json"
44
+ },
25
45
  "publishConfig": {
26
46
  "access": "public"
27
47
  },
28
- "files": [
29
- "README.md"
30
- ]
48
+ "dependencies": {
49
+ "@lunora/advisor": "1.0.0-alpha.1",
50
+ "@lunora/container": "1.0.0-alpha.1",
51
+ "@lunora/scheduler": "1.0.0-alpha.1",
52
+ "@lunora/values": "1.0.0-alpha.1",
53
+ "@lunora/workflow": "1.0.0-alpha.1",
54
+ "ts-morph": "^28.0.0"
55
+ },
56
+ "engines": {
57
+ "node": "^22.15.0 || >=24.11.0"
58
+ }
31
59
  }