@an5/orm 1.0.7 → 1.0.8

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 (52) hide show
  1. package/README.md +175 -153
  2. package/dist/an5Metadata.d.ts +10 -0
  3. package/dist/an5Metadata.js +29 -0
  4. package/dist/an5Orm.d.ts +4 -1
  5. package/dist/an5Orm.js +214 -409
  6. package/dist/config.d.ts +2 -0
  7. package/dist/config.js +24 -0
  8. package/dist/errors.d.ts +14 -0
  9. package/dist/errors.js +16 -0
  10. package/dist/generator/src/code-generator.d.ts +13 -13
  11. package/dist/generator/src/code-generator.js +225 -225
  12. package/dist/generator/src/dotnet-generator.d.ts +11 -11
  13. package/dist/generator/src/dotnet-generator.js +546 -546
  14. package/dist/generator/src/index.d.ts +1 -1
  15. package/dist/generator/src/index.js +88 -88
  16. package/dist/generator/src/metadata-generator.d.ts +8 -8
  17. package/dist/generator/src/metadata-generator.js +90 -90
  18. package/dist/generator/src/parser.d.ts +11 -11
  19. package/dist/generator/src/parser.js +206 -206
  20. package/dist/generator/src/python-generator.d.ts +9 -9
  21. package/dist/generator/src/python-generator.js +105 -105
  22. package/dist/generator/src/types.d.ts +27 -27
  23. package/dist/generator/src/types.js +2 -2
  24. package/dist/index.d.ts +3 -0
  25. package/dist/index.js +5 -1
  26. package/dist/logger.d.ts +53 -0
  27. package/dist/logger.js +117 -0
  28. package/dist/metadata.d.ts +24 -0
  29. package/dist/metadata.js +15 -0
  30. package/dist/scripts/release-cli.d.ts +2 -2
  31. package/dist/scripts/release-cli.js +115 -115
  32. package/dist/sql-utils.d.ts +29 -0
  33. package/dist/sql-utils.js +220 -0
  34. package/generator/src/code-generator.ts +230 -234
  35. package/generator/src/dotnet-generator.ts +544 -544
  36. package/generator/src/index.ts +116 -104
  37. package/generator/src/metadata-generator.ts +101 -99
  38. package/generator/src/parser.ts +202 -202
  39. package/generator/src/python-generator.ts +114 -114
  40. package/generator/src/types.ts +29 -29
  41. package/package.json +2 -6
  42. package/dist/cleanup.d.ts +0 -2
  43. package/dist/cleanup.js +0 -120
  44. package/dist/cli.js +0 -64
  45. package/dist/migrate.d.ts +0 -11
  46. package/dist/migrate.js +0 -301
  47. package/dist/pull.d.ts +0 -2
  48. package/dist/pull.js +0 -339
  49. package/dist/push.d.ts +0 -2
  50. package/dist/push.js +0 -281
  51. package/dist/seed.d.ts +0 -2
  52. package/dist/seed.js +0 -401
package/README.md CHANGED
@@ -1,153 +1,175 @@
1
- # @an5/orm
2
-
3
- SQL Server ORM. Proxy client. CRUD. Vector search. Middleware. Raw queries. Transactions.
4
-
5
- ## Features
6
-
7
- - **Proxy Client.** Model access. `db.modelName` syntax.
8
- - **CRUD.** `findMany`, `findFirst`, `findUnique`, `create`, `update`, `delete`, `upsert`.
9
- - **Advanced Queries.** OR/AND, nested relations, aggregates, `groupBy`.
10
- - **Vector Search.** Native SQL Server `VECTOR_DISTANCE`. In-memory fallback.
11
- - **Middleware.** Hook ORM operations: logging, auth, validation.
12
- - **Raw Queries.** `$queryRaw`, `$executeRaw`. Auto `NOLOCK`.
13
- - **Transactions.** `$transaction`. Rollback support.
14
- - **Schema Generator.** Parse `.an5` files. Generate TypeScript/Python/.NET code.
15
-
16
- ## Quick Start
17
-
18
- ### Installation
19
-
20
- #### TypeScript / Node.js
21
- ```bash
22
- npm install @an5/orm
23
- ```
24
-
25
- #### Python
26
- ```bash
27
- pip install an5-orm
28
- ```
29
-
30
- ### Configuration
31
-
32
- ```bash
33
- cp .env.example .env
34
- # Edit .env. Set DATABASE_URL.
35
- ```
36
-
37
- ### Development Commands
38
-
39
- ```bash
40
- # Generate code from schema
41
- npx an5 generate
42
-
43
- # Push schema to database
44
- npx an5 db:push
45
-
46
- # Pull schema from database
47
- npx an5 db:pull
48
-
49
- # Seed database
50
- npx an5 db:seed
51
-
52
- # Compare schema with database
53
- npx an5 db:migrate diff
54
-
55
- # Run tests
56
- npm test
57
- ```
58
-
59
- ## Usage
60
-
61
- ```typescript
62
- import { An5ORM } from '@an5/orm';
63
-
64
- const db = new An5ORM();
65
-
66
- // CRUD Operations
67
- const users = await db.user.findMany({
68
- where: { email: { contains: '@example.com' } },
69
- orderBy: { createdAt: 'desc' },
70
- take: 10,
71
- });
72
-
73
- const user = await db.user.create({
74
- data: { email: 'john@example.com', name: 'John' },
75
- });
76
-
77
- // Relations
78
- const orders = await db.user.findMany({
79
- include: { orders: true },
80
- });
81
-
82
- // Vector Search
83
- const similar = await db.document.vectorSearch({
84
- vector: [0.1, 0.2, 0.3],
85
- take: 5,
86
- distanceMetric: 'cosine',
87
- });
88
-
89
- // Transactions
90
- await db.$transaction(async (tx) => {
91
- const user = await tx.user.create({ data: { email: 'jane@example.com' } });
92
- await tx.order.create({ data: { userId: user.id, total: 100 } });
93
- });
94
-
95
- // Raw Queries
96
- const results = await db.$queryRaw`SELECT * FROM users WHERE id = ${id}`;
97
- ```
98
-
99
- ## Schema Definition
100
-
101
- Schema files: `.an5`. Path: `an5Schema/`. SQL Server types.
102
-
103
- ```an5
104
- model User {
105
- id NVARCHAR(1000) @id @default(uuid())
106
- email NVARCHAR(255) @unique
107
- name NVARCHAR(255)?
108
- createdAt DATETIME2 @default(now())
109
- orders Order[]
110
-
111
- @@map("users")
112
- }
113
-
114
- model Order {
115
- id NVARCHAR(1000) @id @default(uuid())
116
- userId NVARCHAR(1000)
117
- total INT @default(0)
118
- user User @relation(fields: [userId], references: [id])
119
-
120
- @@map("orders")
121
- }
122
- ```
123
-
124
- ## Supported SQL Server Types
125
-
126
- Type mapping: `.an5` to TypeScript.
127
-
128
- | Schema Type | TypeScript |
129
- |-------------|------------|
130
- | `NVARCHAR(n)`, `VARCHAR(n)`, `CHAR(n)`, `TEXT` | `string` |
131
- | `INT`, `SMALLINT`, `TINYINT`, `FLOAT`, `REAL`, `DECIMAL`, `NUMERIC` | `number` |
132
- | `BIGINT` | `number \| bigint` |
133
- | `BIT` | `boolean` |
134
- | `DATETIME`, `DATETIME2`, `DATE`, `TIME` | `Date` |
135
- | `UNIQUEIDENTIFIER` | `string` |
136
- | `VARBINARY`, `BINARY`, `IMAGE` | `Buffer` |
137
-
138
- ## Testing
139
-
140
- ```bash
141
- # Unit tests
142
- node test/unit.test.js
143
-
144
- # Generator tests
145
- node test/generator.test.js
146
-
147
- # Smoke test
148
- npm test
149
- ```
150
-
151
- ## License
152
-
153
- MIT
1
+ # @an5/orm
2
+
3
+ SQL Server ORM. Proxy client. CRUD. Vector search. Middleware. Raw queries. Transactions.
4
+
5
+ ## Features
6
+
7
+ - **Proxy Client.** Model access. `db.modelName` syntax.
8
+ - **CRUD.** `findMany`, `findFirst`, `findUnique`, `create`, `update`, `delete`, `upsert`.
9
+ - **Advanced Queries.** OR/AND, nested relations, aggregates, `groupBy`.
10
+ - **Vector Search.** Native SQL Server `VECTOR_DISTANCE`. In-memory fallback.
11
+ - **Middleware.** Hook ORM operations: logging, auth, validation.
12
+ - **Raw Queries.** `$queryRaw`, `$executeRaw`. Auto `NOLOCK`.
13
+ - **Transactions.** `$transaction`. Rollback support.
14
+ - **Schema Generator.** Parse `.an5` files. Generate TypeScript/Python/.NET code.
15
+
16
+ ## Quick Start
17
+
18
+ ### Installation
19
+
20
+ #### TypeScript / Node.js
21
+ ```bash
22
+ npm install @an5/orm
23
+ ```
24
+
25
+ #### Python
26
+ ```bash
27
+ pip install an5-orm
28
+ ```
29
+
30
+ ### Configuration
31
+
32
+ ```bash
33
+ cp .env.example .env
34
+ # Edit .env. Set DATABASE_URL.
35
+ ```
36
+
37
+ ### Development Commands
38
+
39
+ Run from the `an5Orm/` repository directory (no separate CLI binary is shipped):
40
+
41
+ ```bash
42
+ # Generate client code from schema
43
+ npm run generate
44
+
45
+ # Push schema to database
46
+ npm run db:push
47
+
48
+ # Pull schema from database
49
+ npm run db:pull
50
+
51
+ # Seed database
52
+ npm run db:seed
53
+
54
+ # Compare schema with database
55
+ npm run db:migrate diff
56
+
57
+ # Generate migration SQL / show status
58
+ npm run db:migrate:generate
59
+ npm run db:migrate:status
60
+
61
+ # Run tests
62
+ npm test
63
+ ```
64
+
65
+ ## Usage
66
+
67
+ ```typescript
68
+ import { An5ORM } from '@an5/orm';
69
+
70
+ const db = new An5ORM();
71
+ ```
72
+
73
+ The default executor reads the `DATABASE_URL` environment variable. Schema metadata
74
+ (model→table mapping, relations, field types) is auto-loaded from the ORM's own generated
75
+ metadata file `an5Metadata.ts` (created by `npm run generate`, configured via
76
+ `outputs.typescript.ormMetadataFile`). The ORM owns this metadata locally — it never
77
+ imports from the generated client package (the client is generated *from* the ORM).
78
+
79
+ To provide metadata explicitly instead:
80
+
81
+ ```typescript
82
+ import { An5ORM } from '@an5/orm';
83
+ import { modelToTable, relationMap, modelFields } from './an5Metadata';
84
+
85
+ const db = new An5ORM(undefined, { modelToTable, relationMap, modelFields });
86
+ ```
87
+
88
+ // CRUD Operations
89
+ const users = await db.user.findMany({
90
+ where: { email: { contains: '@example.com' } },
91
+ orderBy: { createdAt: 'desc' },
92
+ take: 10,
93
+ });
94
+
95
+ const user = await db.user.create({
96
+ data: { email: 'john@example.com', name: 'John' },
97
+ });
98
+
99
+ // Relations
100
+ const orders = await db.user.findMany({
101
+ include: { orders: true },
102
+ });
103
+
104
+ // Vector Search
105
+ const similar = await db.document.vectorSearch({
106
+ vector: [0.1, 0.2, 0.3],
107
+ take: 5,
108
+ distanceMetric: 'cosine',
109
+ });
110
+
111
+ // Transactions
112
+ await db.$transaction(async (tx) => {
113
+ const user = await tx.user.create({ data: { email: 'jane@example.com' } });
114
+ await tx.order.create({ data: { userId: user.id, total: 100 } });
115
+ });
116
+
117
+ // Raw Queries
118
+ const results = await db.$queryRaw`SELECT * FROM users WHERE id = ${id}`;
119
+ ```
120
+
121
+ ## Schema Definition
122
+
123
+ Schema files: `.an5`. Path: `an5Schema/`. SQL Server types.
124
+
125
+ ```an5
126
+ model User {
127
+ id NVARCHAR(1000) @id @default(uuid())
128
+ email NVARCHAR(255) @unique
129
+ name NVARCHAR(255)?
130
+ createdAt DATETIME2 @default(now())
131
+ orders Order[]
132
+
133
+ @@map("users")
134
+ }
135
+
136
+ model Order {
137
+ id NVARCHAR(1000) @id @default(uuid())
138
+ userId NVARCHAR(1000)
139
+ total INT @default(0)
140
+ user User @relation(fields: [userId], references: [id])
141
+
142
+ @@map("orders")
143
+ }
144
+ ```
145
+
146
+ ## Supported SQL Server Types
147
+
148
+ Type mapping: `.an5` to TypeScript.
149
+
150
+ | Schema Type | TypeScript |
151
+ |-------------|------------|
152
+ | `NVARCHAR(n)`, `VARCHAR(n)`, `CHAR(n)`, `TEXT` | `string` |
153
+ | `INT`, `SMALLINT`, `TINYINT`, `FLOAT`, `REAL`, `DECIMAL`, `NUMERIC` | `number` |
154
+ | `BIGINT` | `number \| bigint` |
155
+ | `BIT` | `boolean` |
156
+ | `DATETIME`, `DATETIME2`, `DATE`, `TIME` | `Date` |
157
+ | `UNIQUEIDENTIFIER` | `string` |
158
+ | `VARBINARY`, `BINARY`, `IMAGE` | `Buffer` |
159
+
160
+ ## Testing
161
+
162
+ ```bash
163
+ # Unit tests
164
+ node test/unit.test.js
165
+
166
+ # Generator tests
167
+ node test/generator.test.js
168
+
169
+ # Smoke test
170
+ npm test
171
+ ```
172
+
173
+ ## License
174
+
175
+ MIT
@@ -0,0 +1,10 @@
1
+ import type { RelationDef } from "./metadata";
2
+ export declare const modelToTable: Record<string, string>;
3
+ export declare const modelDescriptions: Record<string, string | undefined>;
4
+ export declare const modelFields: Record<string, Record<string, {
5
+ ts: string;
6
+ sql: string;
7
+ description?: string;
8
+ }>>;
9
+ export declare const relationMap: Record<string, Record<string, RelationDef>>;
10
+ //# sourceMappingURL=an5Metadata.d.ts.map
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // This file is auto-generated. Do not edit directly.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.relationMap = exports.modelFields = exports.modelDescriptions = exports.modelToTable = void 0;
5
+ exports.modelToTable = {
6
+ embeddingConfig: "[dbo].[embeddingconfigs]",
7
+ llmConfig: "[dbo].[llmconfigs]",
8
+ user: "[dbo].[users]",
9
+ order: "[dbo].[orders]",
10
+ };
11
+ exports.modelDescriptions = {
12
+ embeddingConfig: "Embedding provider configuration. Stores API keys and model settings for RAG features.",
13
+ llmConfig: "LLM provider configuration. Stores API keys and model settings for AI features.",
14
+ user: "Represents a registered user in the database.",
15
+ order: "Represents a customer order in the system.",
16
+ };
17
+ exports.modelFields = {
18
+ embeddingConfig: { id: { ts: "string", sql: "NVARCHAR(1000)", description: "Primary key" }, provider: { ts: "string", sql: "NVARCHAR(100)", description: "Embedding provider: openai, cohere, custom" }, apiKey: { ts: "string", sql: "NVARCHAR(4000)", description: "API key for the embedding service" }, model: { ts: "string?", sql: "NVARCHAR(500)", description: "Model name, e.g. text-embedding-3-small" }, endpoint: { ts: "string?", sql: "NVARCHAR(2000)", description: "Custom endpoint URL" }, isActive: { ts: "boolean", sql: "BIT", description: "Whether this config is active" }, createdAt: { ts: "Date", sql: "DATETIME2", description: "Creation timestamp" }, updatedAt: { ts: "Date", sql: "DATETIME2", description: "Last update timestamp" } },
19
+ llmConfig: { id: { ts: "string", sql: "NVARCHAR(1000)", description: "Primary key" }, provider: { ts: "string", sql: "NVARCHAR(100)", description: "LLM provider: openai, gemini, custom, azure" }, apiKey: { ts: "string", sql: "NVARCHAR(4000)", description: "API key for the LLM provider" }, model: { ts: "string?", sql: "NVARCHAR(500)", description: "Model name, e.g. gpt-4o, gemini-2.5-flash" }, endpoint: { ts: "string?", sql: "NVARCHAR(2000)", description: "Custom endpoint URL" }, isActive: { ts: "boolean", sql: "BIT", description: "Whether this config is active" }, createdAt: { ts: "Date", sql: "DATETIME2", description: "Creation timestamp" }, updatedAt: { ts: "Date", sql: "DATETIME2", description: "Last update timestamp" } },
20
+ user: { id: { ts: "string", sql: "NVARCHAR(1000)", description: "Primary key for the User table (auto-generated UUID)" }, email: { ts: "string", sql: "NVARCHAR(255)", description: "Unique email address used for login and notifications" }, name: { ts: "string?", sql: "NVARCHAR(255)", description: "Display name of the user" }, createdAt: { ts: "Date", sql: "DATETIME2", description: "Timestamp when the user profile was created" } },
21
+ order: { id: { ts: "string", sql: "NVARCHAR(1000)", description: "Primary key for the Order table (auto-generated UUID)" }, userId: { ts: "string", sql: "NVARCHAR(1000)", description: "Foreign key linking to the User model who placed the order" }, total: { ts: "number", sql: "INT", description: "Total cost amount of the order" }, createdAt: { ts: "Date", sql: "DATETIME2", description: "The date and time when the order was created." } },
22
+ };
23
+ exports.relationMap = {
24
+ embeddingConfig: {},
25
+ llmConfig: {},
26
+ user: {},
27
+ order: {},
28
+ };
29
+ //# sourceMappingURL=an5Metadata.js.map
package/dist/an5Orm.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { An5Metadata } from "./metadata";
1
2
  type ExecutorFn = (queryText: string, params?: Record<string, any>) => Promise<any[]>;
2
3
  export interface MiddlewareParams {
3
4
  model?: string;
@@ -11,8 +12,10 @@ export declare class An5ORM {
11
12
  private customExecutor?;
12
13
  [key: string]: any;
13
14
  private middlewares;
14
- constructor(customExecutor?: ExecutorFn | undefined);
15
+ readonly metadata: An5Metadata;
16
+ constructor(customExecutor?: ExecutorFn | undefined, metadata?: An5Metadata);
15
17
  $use(middleware: Middleware): void;
18
+ parseWhere(modelName: string, where: any, params: Record<string, any>, prefix?: string): string;
16
19
  _executeMiddleware(params: MiddlewareParams, finalAction: (params: MiddlewareParams) => Promise<any>): Promise<any>;
17
20
  $connect(): Promise<void>;
18
21
  $disconnect(): Promise<void>;