@an5/orm 1.0.9 → 1.1.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 (57) hide show
  1. package/README.md +10 -3
  2. package/dist/config.d.ts +9 -1
  3. package/dist/config.js +13 -18
  4. package/dist/generator/src/api.d.ts +35 -0
  5. package/dist/generator/src/api.js +58 -0
  6. package/dist/generator/src/config.d.ts +162 -0
  7. package/dist/generator/src/config.js +354 -0
  8. package/dist/generator/src/dialect.d.ts +88 -0
  9. package/dist/generator/src/dialect.js +318 -0
  10. package/dist/generator/src/dotnet-generator.d.ts +8 -0
  11. package/dist/generator/src/dotnet-generator.js +282 -101
  12. package/dist/generator/src/field-types.d.ts +96 -0
  13. package/dist/generator/src/field-types.js +433 -0
  14. package/dist/generator/src/golang-generator.d.ts +36 -0
  15. package/dist/generator/src/golang-generator.js +1230 -0
  16. package/dist/generator/src/index.js +69 -36
  17. package/dist/generator/src/metadata-generator.js +2 -1
  18. package/dist/generator/src/parser.d.ts +28 -2
  19. package/dist/generator/src/parser.js +92 -90
  20. package/dist/generator/src/push-apply.d.ts +37 -0
  21. package/dist/generator/src/push-apply.js +109 -0
  22. package/dist/generator/src/push-schema.d.ts +93 -0
  23. package/dist/generator/src/push-schema.js +189 -0
  24. package/dist/generator/src/python-generator.d.ts +19 -1
  25. package/dist/generator/src/python-generator.js +147 -64
  26. package/dist/generator/src/rust-generator.d.ts +38 -0
  27. package/dist/generator/src/rust-generator.js +971 -0
  28. package/dist/generator/src/suggest.d.ts +17 -0
  29. package/dist/generator/src/suggest.js +45 -0
  30. package/dist/generator/src/type-kinds.d.ts +27 -0
  31. package/dist/generator/src/type-kinds.js +72 -0
  32. package/dist/generator/src/types.d.ts +33 -0
  33. package/dist/generator/src/types.js +39 -0
  34. package/dist/migration-core.d.ts +16 -1
  35. package/dist/migration-core.js +60 -21
  36. package/dist/orm.d.ts +147 -0
  37. package/dist/orm.js +1654 -0
  38. package/dist/provider-support.d.ts +27 -0
  39. package/dist/provider-support.js +50 -0
  40. package/dist/sql-utils.js +8 -3
  41. package/generator/src/api.ts +45 -0
  42. package/generator/src/config.ts +496 -0
  43. package/generator/src/dialect.ts +415 -0
  44. package/generator/src/dotnet-generator.ts +277 -88
  45. package/generator/src/field-types.ts +473 -0
  46. package/generator/src/golang-generator.ts +94 -68
  47. package/generator/src/index.ts +71 -36
  48. package/generator/src/metadata-generator.ts +2 -2
  49. package/generator/src/parser.ts +122 -90
  50. package/generator/src/push-apply.ts +148 -0
  51. package/generator/src/push-schema.ts +250 -0
  52. package/generator/src/python-generator.ts +118 -62
  53. package/generator/src/rust-generator.ts +974 -0
  54. package/generator/src/suggest.ts +41 -0
  55. package/generator/src/type-kinds.ts +78 -0
  56. package/generator/src/types.ts +48 -0
  57. package/package.json +26 -6
package/README.md CHANGED
@@ -4,14 +4,14 @@ The Schema, Code Generator, and Migration Toolkit for the AN5 ecosystem.
4
4
 
5
5
  ## Overview
6
6
 
7
- `@an5/orm` provides core schema parsing, multi-language code generation (TypeScript, Python, .NET C#, Golang), database introspection (`pull`), schema deployment (`push`), and automated database migrations.
7
+ `@an5/orm` provides core schema parsing, multi-language code generation (TypeScript, Python, .NET C#, Golang, Rust), database introspection (`pull`), schema deployment (`push`), and automated database migrations.
8
8
 
9
- Runtime database connections, query building, transactions, and multi-database drivers (MSSQL, PostgreSQL, MySQL, SQLite, Google Sheets) are powered by **[@an5/adapters](https://github.com/an5ORM/an5/tree/main/an5Adapters)**.
9
+ Runtime database connections, query building, transactions, and multi-database drivers (MSSQL, PostgreSQL, MySQL, SQLite, Google Sheets) are powered by **[@an5/adapters](https://github.com/an5ORM/an5Adapters)**.
10
10
 
11
11
  ## Features
12
12
 
13
13
  - **Schema Definition:** Define models and relationships using concise `.an5` syntax with native database types.
14
- - **Multi-Language Generator:** Generate typed clients and entity models for TypeScript, Python, .NET (C#), and Golang.
14
+ - **Multi-Language Generator:** Generate typed clients and entity models for TypeScript, Python, .NET (C#), Golang, and Rust.
15
15
  - **Database Introspection (`db:pull`):** Inspect an existing database schema and generate matching `.an5` models.
16
16
  - **Direct Schema Push (`db:push`):** Synchronize `.an5` schema definitions directly to the database.
17
17
  - **Automated Migrations (`db:migrate`):** Diff schema against database, generate up/down migration SQL scripts, apply, rollback, and check migration status.
@@ -141,6 +141,13 @@ await db.$disconnect();
141
141
 
142
142
  ## Schema Definition (`.an5`)
143
143
 
144
+ Field types are validated against the database you are generating for. The provider
145
+ comes from the connection string (`DATABASE_URL` first, then `connectionString` in
146
+ `an5Orm.config.js`): `sqlserver://` (and the fallback) is SQL Server, `postgres://`
147
+ PostgreSQL, `mysql://` MySQL, `sqlite://` SQLite, `googlesheets://` Google Sheets.
148
+ A type the provider does not have stops generation with every offending field
149
+ listed at once — see `PROVIDER_FIELD_TYPES` in `generator/src/field-types.ts`.
150
+
144
151
  ```an5
145
152
  model User {
146
153
  id NVARCHAR(1000) @id @default(uuid())
package/dist/config.d.ts CHANGED
@@ -1,2 +1,10 @@
1
- export declare const config: any;
1
+ /**
2
+ * Workspace Configuration for an5Orm
3
+ *
4
+ * Re-exported from the generator so the CLI commands and the generator agree on
5
+ * one config shape. These files used to load an5Orm.config.js themselves with
6
+ * `any`, which meant the four of them could disagree about what was valid.
7
+ */
8
+ export { loadConfig, validateConfig, resolveOutputs, resolveConnectionString, formatIssues, ConfigError, DEFAULT_CONFIG, } from './generator/src/config';
9
+ export type { An5OrmConfig, TypeScriptOutput, ResolvedOutputs, LoadedConfig, ConfigIssue, } from './generator/src/config';
2
10
  //# sourceMappingURL=config.d.ts.map
package/dist/config.js CHANGED
@@ -1,24 +1,19 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.config = void 0;
3
+ exports.DEFAULT_CONFIG = exports.ConfigError = exports.formatIssues = exports.resolveConnectionString = exports.resolveOutputs = exports.validateConfig = exports.loadConfig = void 0;
7
4
  /**
8
5
  * Workspace Configuration for an5Orm
9
- * Reads from an5Orm.config.js and environment variables.
6
+ *
7
+ * Re-exported from the generator so the CLI commands and the generator agree on
8
+ * one config shape. These files used to load an5Orm.config.js themselves with
9
+ * `any`, which meant the four of them could disagree about what was valid.
10
10
  */
11
- const path_1 = __importDefault(require("path"));
12
- let workspaceConfig = {};
13
- try {
14
- const configPath = path_1.default.join(__dirname, '..', 'an5Orm.config.js');
15
- workspaceConfig = require(configPath);
16
- }
17
- catch { /* ignore */ }
18
- exports.config = {
19
- ...workspaceConfig,
20
- llm: workspaceConfig.llm || {
21
- providers: [],
22
- },
23
- };
11
+ var config_1 = require("./generator/src/config");
12
+ Object.defineProperty(exports, "loadConfig", { enumerable: true, get: function () { return config_1.loadConfig; } });
13
+ Object.defineProperty(exports, "validateConfig", { enumerable: true, get: function () { return config_1.validateConfig; } });
14
+ Object.defineProperty(exports, "resolveOutputs", { enumerable: true, get: function () { return config_1.resolveOutputs; } });
15
+ Object.defineProperty(exports, "resolveConnectionString", { enumerable: true, get: function () { return config_1.resolveConnectionString; } });
16
+ Object.defineProperty(exports, "formatIssues", { enumerable: true, get: function () { return config_1.formatIssues; } });
17
+ Object.defineProperty(exports, "ConfigError", { enumerable: true, get: function () { return config_1.ConfigError; } });
18
+ Object.defineProperty(exports, "DEFAULT_CONFIG", { enumerable: true, get: function () { return config_1.DEFAULT_CONFIG; } });
24
19
  //# sourceMappingURL=config.js.map
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Side-effect-free entrypoint for programmatic use of the code generator.
3
+ *
4
+ * Importing `./index` runs the CLI (`main()`), so tooling that wants to drive
5
+ * the generator in-process — such as the an5Agent `generateClientCode` tool —
6
+ * imports this module instead.
7
+ *
8
+ * The provider is optional and defaults to SQL Server. Pass one derived from
9
+ * the connection string when the schema targets another database, so field
10
+ * types are checked against that database's types — `providerForProject` reads
11
+ * it from the config file for callers that only know the project directory:
12
+ *
13
+ * ```ts
14
+ * import { SchemaParser, RustGenerator, providerForProject } from '@an5/orm/generator';
15
+ *
16
+ * const provider = providerForProject(__dirname);
17
+ * const models = await new SchemaParser('an5Schema', provider).parse();
18
+ * new RustGenerator('an5Client/rust').generate(models);
19
+ * ```
20
+ */
21
+ export { SchemaParser, sqlTypeToTs } from './parser';
22
+ export { CodeGenerator } from './code-generator';
23
+ export { MetadataGenerator } from './metadata-generator';
24
+ export { PythonGenerator } from './python-generator';
25
+ export { DotnetGenerator } from './dotnet-generator';
26
+ export { GolangGenerator } from './golang-generator';
27
+ export { RustGenerator } from './rust-generator';
28
+ export { detectProvider, providerFromConfig, providerForProject } from './config';
29
+ export { dialectFor, defaultClause, sqlLiteral } from './dialect';
30
+ export { fieldKind } from './type-kinds';
31
+ export type { FieldKind } from './type-kinds';
32
+ export type { ColumnSpec, PushDialect } from './dialect';
33
+ export { DEFAULT_PROVIDER, FieldTypeError, PROVIDERS, PROVIDER_FIELD_TYPES, PROVIDER_LABELS, defaultSqlTypeForTs, fieldTypesFor, resolveFieldType, unknownFieldTypeMessage, } from './field-types';
34
+ export type { FieldTypeIssue, Provider, TsType } from './field-types';
35
+ export type { Model, Field, Relation } from './types';
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.unknownFieldTypeMessage = exports.resolveFieldType = exports.fieldTypesFor = exports.defaultSqlTypeForTs = exports.PROVIDER_LABELS = exports.PROVIDER_FIELD_TYPES = exports.PROVIDERS = exports.FieldTypeError = exports.DEFAULT_PROVIDER = exports.fieldKind = exports.sqlLiteral = exports.defaultClause = exports.dialectFor = exports.providerForProject = exports.providerFromConfig = exports.detectProvider = exports.RustGenerator = exports.GolangGenerator = exports.DotnetGenerator = exports.PythonGenerator = exports.MetadataGenerator = exports.CodeGenerator = exports.sqlTypeToTs = exports.SchemaParser = void 0;
4
+ /**
5
+ * Side-effect-free entrypoint for programmatic use of the code generator.
6
+ *
7
+ * Importing `./index` runs the CLI (`main()`), so tooling that wants to drive
8
+ * the generator in-process — such as the an5Agent `generateClientCode` tool —
9
+ * imports this module instead.
10
+ *
11
+ * The provider is optional and defaults to SQL Server. Pass one derived from
12
+ * the connection string when the schema targets another database, so field
13
+ * types are checked against that database's types — `providerForProject` reads
14
+ * it from the config file for callers that only know the project directory:
15
+ *
16
+ * ```ts
17
+ * import { SchemaParser, RustGenerator, providerForProject } from '@an5/orm/generator';
18
+ *
19
+ * const provider = providerForProject(__dirname);
20
+ * const models = await new SchemaParser('an5Schema', provider).parse();
21
+ * new RustGenerator('an5Client/rust').generate(models);
22
+ * ```
23
+ */
24
+ var parser_1 = require("./parser");
25
+ Object.defineProperty(exports, "SchemaParser", { enumerable: true, get: function () { return parser_1.SchemaParser; } });
26
+ Object.defineProperty(exports, "sqlTypeToTs", { enumerable: true, get: function () { return parser_1.sqlTypeToTs; } });
27
+ var code_generator_1 = require("./code-generator");
28
+ Object.defineProperty(exports, "CodeGenerator", { enumerable: true, get: function () { return code_generator_1.CodeGenerator; } });
29
+ var metadata_generator_1 = require("./metadata-generator");
30
+ Object.defineProperty(exports, "MetadataGenerator", { enumerable: true, get: function () { return metadata_generator_1.MetadataGenerator; } });
31
+ var python_generator_1 = require("./python-generator");
32
+ Object.defineProperty(exports, "PythonGenerator", { enumerable: true, get: function () { return python_generator_1.PythonGenerator; } });
33
+ var dotnet_generator_1 = require("./dotnet-generator");
34
+ Object.defineProperty(exports, "DotnetGenerator", { enumerable: true, get: function () { return dotnet_generator_1.DotnetGenerator; } });
35
+ var golang_generator_1 = require("./golang-generator");
36
+ Object.defineProperty(exports, "GolangGenerator", { enumerable: true, get: function () { return golang_generator_1.GolangGenerator; } });
37
+ var rust_generator_1 = require("./rust-generator");
38
+ Object.defineProperty(exports, "RustGenerator", { enumerable: true, get: function () { return rust_generator_1.RustGenerator; } });
39
+ var config_1 = require("./config");
40
+ Object.defineProperty(exports, "detectProvider", { enumerable: true, get: function () { return config_1.detectProvider; } });
41
+ Object.defineProperty(exports, "providerFromConfig", { enumerable: true, get: function () { return config_1.providerFromConfig; } });
42
+ Object.defineProperty(exports, "providerForProject", { enumerable: true, get: function () { return config_1.providerForProject; } });
43
+ var dialect_1 = require("./dialect");
44
+ Object.defineProperty(exports, "dialectFor", { enumerable: true, get: function () { return dialect_1.dialectFor; } });
45
+ Object.defineProperty(exports, "defaultClause", { enumerable: true, get: function () { return dialect_1.defaultClause; } });
46
+ Object.defineProperty(exports, "sqlLiteral", { enumerable: true, get: function () { return dialect_1.sqlLiteral; } });
47
+ var type_kinds_1 = require("./type-kinds");
48
+ Object.defineProperty(exports, "fieldKind", { enumerable: true, get: function () { return type_kinds_1.fieldKind; } });
49
+ var field_types_1 = require("./field-types");
50
+ Object.defineProperty(exports, "DEFAULT_PROVIDER", { enumerable: true, get: function () { return field_types_1.DEFAULT_PROVIDER; } });
51
+ Object.defineProperty(exports, "FieldTypeError", { enumerable: true, get: function () { return field_types_1.FieldTypeError; } });
52
+ Object.defineProperty(exports, "PROVIDERS", { enumerable: true, get: function () { return field_types_1.PROVIDERS; } });
53
+ Object.defineProperty(exports, "PROVIDER_FIELD_TYPES", { enumerable: true, get: function () { return field_types_1.PROVIDER_FIELD_TYPES; } });
54
+ Object.defineProperty(exports, "PROVIDER_LABELS", { enumerable: true, get: function () { return field_types_1.PROVIDER_LABELS; } });
55
+ Object.defineProperty(exports, "defaultSqlTypeForTs", { enumerable: true, get: function () { return field_types_1.defaultSqlTypeForTs; } });
56
+ Object.defineProperty(exports, "fieldTypesFor", { enumerable: true, get: function () { return field_types_1.fieldTypesFor; } });
57
+ Object.defineProperty(exports, "resolveFieldType", { enumerable: true, get: function () { return field_types_1.resolveFieldType; } });
58
+ Object.defineProperty(exports, "unknownFieldTypeMessage", { enumerable: true, get: function () { return field_types_1.unknownFieldTypeMessage; } });
@@ -0,0 +1,162 @@
1
+ import { Provider } from './field-types';
2
+ /** TypeScript client output. */
3
+ export interface TypeScriptOutput {
4
+ /** Directory for the generated .ts files. */
5
+ outputDir: string;
6
+ /** Path of the generated metadata module. */
7
+ metadataFile: string;
8
+ }
9
+ export interface PythonOutput {
10
+ /** Path of the generated metadata module. */
11
+ metadataFile: string;
12
+ }
13
+ export interface DotnetOutput {
14
+ outputDir: string;
15
+ }
16
+ export interface GolangOutput {
17
+ outputDir: string;
18
+ }
19
+ export interface RustOutput {
20
+ outputDir: string;
21
+ }
22
+ export interface An5OrmConfig {
23
+ /**
24
+ * Database connection for the commands that need one — db:push, db:pull,
25
+ * db:migrate:*, db:cleanup.
26
+ *
27
+ * Optional on purpose: DATABASE_URL overrides it, so a checked-in config can
28
+ * hold a development database while CI supplies its own. Leave it unset
29
+ * rather than committing a password, and keep secrets in the environment.
30
+ *
31
+ * `| undefined` is explicit because the project compiles with
32
+ * exactOptionalPropertyTypes, which otherwise rejects an omitted value.
33
+ */
34
+ connectionString?: string | undefined;
35
+ /** Schema directory, relative to the config file. */
36
+ schemaDir: string;
37
+ outputs: {
38
+ typescript: TypeScriptOutput;
39
+ python: PythonOutput;
40
+ dotnet: DotnetOutput;
41
+ golang: GolangOutput;
42
+ rust: RustOutput;
43
+ };
44
+ pull: {
45
+ /** Regex patterns; tables matching any of them are skipped by db:pull. */
46
+ exclude: string[];
47
+ /** Keep relations that already exist in the schema. */
48
+ preserveRelations: boolean;
49
+ };
50
+ generation: {
51
+ /** Write the metadata module alongside the client. */
52
+ generateMetadata: boolean;
53
+ };
54
+ }
55
+ export declare const DEFAULT_CONFIG: An5OrmConfig;
56
+ /** Output paths resolved against the config file's directory. */
57
+ export interface ResolvedOutputs {
58
+ schemaDir: string;
59
+ typescriptDir: string;
60
+ typescriptMetadataFile: string;
61
+ pythonMetadataFile: string;
62
+ dotnetDir: string;
63
+ golangDir: string;
64
+ rustDir: string;
65
+ }
66
+ export declare function resolveOutputs(config: An5OrmConfig, rootDir: string): ResolvedOutputs;
67
+ /**
68
+ * The environment variables a caller can pass in.
69
+ *
70
+ * Spelled structurally rather than as `NodeJS.ProcessEnv`: that type is a Node
71
+ * global, and a parameter type ends up in the emitted `.d.ts`, so any consumer
72
+ * without `@types/node` would fail to compile against this package. `process.env` is
73
+ * assignable to it, and so is a plain `{}` in a test.
74
+ */
75
+ export type EnvLike = Record<string, string | undefined>;
76
+ /** One problem found in the config file. */
77
+ export interface ConfigIssue {
78
+ /** Dotted path, e.g. `outputs.typescript.outputDir`. */
79
+ path: string;
80
+ message: string;
81
+ }
82
+ export declare class ConfigError extends Error {
83
+ readonly issues: ConfigIssue[];
84
+ constructor(issues: ConfigIssue[]);
85
+ }
86
+ /**
87
+ * Checks a loaded config object and returns it with defaults applied.
88
+ * Throws ConfigError listing every problem found, not just the first.
89
+ */
90
+ export declare function validateConfig(raw: unknown): An5OrmConfig;
91
+ /**
92
+ * The connection string to use, or an error explaining where to put one.
93
+ *
94
+ * DATABASE_URL wins over the config file: it is the documented override and the
95
+ * one CI sets. A project may therefore commit a development connection and
96
+ * still have a different one in a pipeline without editing the file.
97
+ */
98
+ export declare function resolveConnectionString(config: An5OrmConfig, env?: EnvLike, command?: string): string;
99
+ /**
100
+ * The provider a connection string points at.
101
+ *
102
+ * Reads the scheme exactly as `An5Adapter` does
103
+ * (`an5Adapters/typescript/src/an5Adapter.ts`) so the generator and the adapter
104
+ * always talk about the same database: the same connection string must give the
105
+ * same provider, otherwise validation checks the types of one database while the
106
+ * SQL runs on another.
107
+ *
108
+ * Falls back to SQL Server when the scheme is unknown — it is the default
109
+ * provider, and ADO-style strings (`Server=...;Database=...`) have no scheme to
110
+ * read. `sqlite://`, a bare path ending in `.sqlite`, `.sqlite3` or `.db` — the same
111
+ * list `An5Adapter` uses.
112
+ */
113
+ export declare function detectProvider(connectionString: string | undefined | null): Provider;
114
+ /**
115
+ * The provider for this run: `DATABASE_URL` first, then the connection string in
116
+ * the config file, then the default.
117
+ *
118
+ * Does not throw when there is no connection string — `generate` has to work
119
+ * with an empty config, and the default provider still validates the schema as
120
+ * before.
121
+ */
122
+ export declare function providerFromConfig(config: An5OrmConfig, env?: EnvLike): Provider;
123
+ /**
124
+ * The provider configured for a project directory.
125
+ *
126
+ * For the tools outside this package that read `.an5` files — the an5Agent
127
+ * schema tools, the VS Code extension — so they validate against the same
128
+ * database the generator does instead of silently assuming SQL Server.
129
+ *
130
+ * Never throws: no config, an unreadable one, or an invalid one all mean the
131
+ * default provider, which is what the parser does on its own anyway.
132
+ */
133
+ export declare function providerForProject(cwd?: string, env?: EnvLike): Provider;
134
+ /**
135
+ * Renders issues for the terminal, one per line, paths aligned.
136
+ *
137
+ * Takes the shape rather than `ConfigIssue` so field type errors
138
+ * (`FieldTypeIssue`, from `./field-types`) print through the same code — both
139
+ * are "a path in the file and what is wrong with it".
140
+ */
141
+ export declare function formatIssues(issues: ReadonlyArray<{
142
+ path: string;
143
+ message: string;
144
+ }>): string;
145
+ export interface LoadedConfig {
146
+ /** The validated config, with defaults applied. */
147
+ config: An5OrmConfig;
148
+ /** Directory the config file lives in; every relative path resolves against it. */
149
+ rootDir: string;
150
+ /** Absolute output paths. */
151
+ outputs: ResolvedOutputs;
152
+ /** Absolute path of the config file, or null when none was found. */
153
+ configPath: string | null;
154
+ }
155
+ /**
156
+ * Finds, loads and validates an5Orm.config.js.
157
+ *
158
+ * Searched in order: the working directory, the same directory as `.cjs`, and
159
+ * the parent directory for each. When no file is found the defaults are used,
160
+ * so a project can generate without a config.
161
+ */
162
+ export declare function loadConfig(cwd?: string): LoadedConfig;