@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.
- package/README.md +175 -147
- package/dist/an5Metadata.d.ts +10 -0
- package/dist/an5Metadata.js +29 -0
- package/dist/an5Orm.d.ts +4 -1
- package/dist/an5Orm.js +214 -409
- package/dist/config.d.ts +2 -0
- package/dist/config.js +24 -0
- package/dist/errors.d.ts +14 -0
- package/dist/errors.js +16 -0
- package/dist/generator/src/code-generator.d.ts +13 -13
- package/dist/generator/src/code-generator.js +225 -225
- package/dist/generator/src/dotnet-generator.d.ts +11 -11
- package/dist/generator/src/dotnet-generator.js +546 -546
- package/dist/generator/src/index.d.ts +1 -1
- package/dist/generator/src/index.js +88 -88
- package/dist/generator/src/metadata-generator.d.ts +8 -8
- package/dist/generator/src/metadata-generator.js +90 -90
- package/dist/generator/src/parser.d.ts +11 -11
- package/dist/generator/src/parser.js +206 -206
- package/dist/generator/src/python-generator.d.ts +9 -9
- package/dist/generator/src/python-generator.js +105 -105
- package/dist/generator/src/types.d.ts +27 -27
- package/dist/generator/src/types.js +2 -2
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5 -1
- package/dist/logger.d.ts +53 -0
- package/dist/logger.js +117 -0
- package/dist/metadata.d.ts +24 -0
- package/dist/metadata.js +15 -0
- package/dist/scripts/release-cli.d.ts +2 -2
- package/dist/scripts/release-cli.js +115 -115
- package/dist/sql-utils.d.ts +29 -0
- package/dist/sql-utils.js +220 -0
- package/generator/src/code-generator.ts +230 -234
- package/generator/src/dotnet-generator.ts +544 -544
- package/generator/src/index.ts +116 -95
- package/generator/src/metadata-generator.ts +101 -99
- package/generator/src/parser.ts +202 -202
- package/generator/src/python-generator.ts +114 -114
- package/generator/src/types.ts +29 -29
- package/package.json +5 -7
- package/dist/cleanup.d.ts +0 -2
- package/dist/cleanup.js +0 -120
- package/dist/cli.js +0 -64
- package/dist/migrate.d.ts +0 -11
- package/dist/migrate.js +0 -301
- package/dist/pull.d.ts +0 -2
- package/dist/pull.js +0 -339
- package/dist/push.d.ts +0 -2
- package/dist/push.js +0 -281
- package/dist/seed.d.ts +0 -2
- 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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
```bash
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
#
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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
|
-
|
|
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>;
|