@an5/orm 1.0.6 → 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 -147
  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 -95
  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 +5 -7
  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,147 +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
- ```bash
21
- npm install @an5/orm
22
- ```
23
-
24
- ### Configuration
25
-
26
- ```bash
27
- cp .env.example .env
28
- # Edit .env. Set DATABASE_URL.
29
- ```
30
-
31
- ### Development Commands
32
-
33
- ```bash
34
- # Generate code from schema
35
- npx an5 generate
36
-
37
- # Push schema to database
38
- npx an5 db:push
39
-
40
- # Pull schema from database
41
- npx an5 db:pull
42
-
43
- # Seed database
44
- npx an5 db:seed
45
-
46
- # Compare schema with database
47
- npx an5 db:migrate diff
48
-
49
- # Run tests
50
- npm test
51
- ```
52
-
53
- ## Usage
54
-
55
- ```typescript
56
- import { An5ORM } from '@an5/orm';
57
-
58
- const db = new An5ORM();
59
-
60
- // CRUD Operations
61
- const users = await db.user.findMany({
62
- where: { email: { contains: '@example.com' } },
63
- orderBy: { createdAt: 'desc' },
64
- take: 10,
65
- });
66
-
67
- const user = await db.user.create({
68
- data: { email: 'john@example.com', name: 'John' },
69
- });
70
-
71
- // Relations
72
- const orders = await db.user.findMany({
73
- include: { orders: true },
74
- });
75
-
76
- // Vector Search
77
- const similar = await db.document.vectorSearch({
78
- vector: [0.1, 0.2, 0.3],
79
- take: 5,
80
- distanceMetric: 'cosine',
81
- });
82
-
83
- // Transactions
84
- await db.$transaction(async (tx) => {
85
- const user = await tx.user.create({ data: { email: 'jane@example.com' } });
86
- await tx.order.create({ data: { userId: user.id, total: 100 } });
87
- });
88
-
89
- // Raw Queries
90
- const results = await db.$queryRaw`SELECT * FROM users WHERE id = ${id}`;
91
- ```
92
-
93
- ## Schema Definition
94
-
95
- Schema files: `.an5`. Path: `an5Schema/`. SQL Server types.
96
-
97
- ```an5
98
- model User {
99
- id NVARCHAR(1000) @id @default(uuid())
100
- email NVARCHAR(255) @unique
101
- name NVARCHAR(255)?
102
- createdAt DATETIME2 @default(now())
103
- orders Order[]
104
-
105
- @@map("users")
106
- }
107
-
108
- model Order {
109
- id NVARCHAR(1000) @id @default(uuid())
110
- userId NVARCHAR(1000)
111
- total INT @default(0)
112
- user User @relation(fields: [userId], references: [id])
113
-
114
- @@map("orders")
115
- }
116
- ```
117
-
118
- ## Supported SQL Server Types
119
-
120
- Type mapping: `.an5` to TypeScript.
121
-
122
- | Schema Type | TypeScript |
123
- |-------------|------------|
124
- | `NVARCHAR(n)`, `VARCHAR(n)`, `CHAR(n)`, `TEXT` | `string` |
125
- | `INT`, `SMALLINT`, `TINYINT`, `FLOAT`, `REAL`, `DECIMAL`, `NUMERIC` | `number` |
126
- | `BIGINT` | `number \| bigint` |
127
- | `BIT` | `boolean` |
128
- | `DATETIME`, `DATETIME2`, `DATE`, `TIME` | `Date` |
129
- | `UNIQUEIDENTIFIER` | `string` |
130
- | `VARBINARY`, `BINARY`, `IMAGE` | `Buffer` |
131
-
132
- ## Testing
133
-
134
- ```bash
135
- # Unit tests
136
- node test/unit.test.js
137
-
138
- # Generator tests
139
- node test/generator.test.js
140
-
141
- # Smoke test
142
- npm test
143
- ```
144
-
145
- ## License
146
-
147
- 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>;