@hazejs/orm 0.0.0-stage → 0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026-present hazejs contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,28 @@
1
- # Temporary Holding Version
1
+ # @hazejs/orm
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Models, query builder, migrations and seeders for hazejs on top of [drizzle](https://orm.drizzle.team) — MySQL, PostgreSQL, SQLite, Turso, Cloudflare D1 — plus a MongoDB adapter (`@hazejs/orm/mongo`).
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ pnpm add @hazejs/orm
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ // app/model/Post.ts — columns and types come from database/schema/posts.ts
15
+ export class Post extends Model.define(posts) {
16
+ static override softDelete = true
17
+ static override hidden = ['secret']
18
+ }
19
+
20
+ const page = await Post.where('published', true).paginate(1, 20)
21
+ const post = await Post.findOrFail(id)
22
+ ```
23
+
24
+ Part of [hazejs](https://github.com/penna-org/haze) — see the main README for documentation.
25
+
26
+ ## License
27
+
28
+ [MIT](./LICENSE)
@@ -0,0 +1,125 @@
1
+ import { $ as TimestampOptions, A as DatabaseConfig, C as setDatabaseManager, D as DatabaseManager, E as Connection, M as Driver, N as defineDatabaseConfig, O as DatabaseManagerOptions, P as dialectOf, Q as ModelSettings, S as resolveSession, T as useDatabaseManager, Z as ModelMeta, _ as ModelQuery, a as TableOf, at as Direction, b as WriteOptions, c as RecordClass, ct as Paginated, d as BelongsTo, et as Conditions, f as HasMany, g as RelationQuery, h as Relation, i as ModelLike, it as ColumnKey, j as Dialect, k as ConnectionConfig, l as RelationName, lt as Row, m as QueryOf, n as Model, nt as WhereObject, o as ActiveRecord, ot as Insert, p as HasOne, r as ModelClass, rt as AnyDatabase, s as FieldOf, st as Operator, t as DefinedModel, tt as WhereCondition, u as RelationResult, v as QueryBuilder, w as transaction, x as Session, y as QueryOptions } from "./model-CBky_ILK.mjs";
2
+ import { ConfigInput, ConflictException, NotFoundException, ServiceProvider, UserProvider } from "@hazejs/core";
3
+ import { SQL, Table } from "drizzle-orm";
4
+ interface ModelUserProviderOptions {
5
+ /** 登录名字段(可多个,凭据中出现的都参与匹配),默认 `email` */
6
+ username?: string | string[];
7
+ /** 密码哈希字段(凭据中同名字段为明文密码),默认 `password` */
8
+ password?: string;
9
+ }
10
+ /**
11
+ * 基于模型的用户来源(config/auth.ts 的 providers):
12
+ *
13
+ * ```ts
14
+ * providers: { users: modelUserProvider(() => User, { username: 'email' }) }
15
+ * ```
16
+ *
17
+ * 模型以函数传入,避免配置文件与模型之间的循环导入。密码字段使用 `Hash.make()` 生成的哈希。
18
+ */
19
+ export declare function modelUserProvider<M extends ModelLike>(model: () => ModelClass<M>, options?: ModelUserProviderOptions): UserProvider<M>;
20
+ interface ScopedDb {
21
+ table: <T extends Table>(table: T) => QueryBuilder<T>;
22
+ transaction: <R>(callback: (tx: AnyDatabase) => Promise<R>) => Promise<R>;
23
+ execute: <R = Record<string, unknown>>(query: SQL) => Promise<R[]>;
24
+ drizzle: () => Promise<AnyDatabase>;
25
+ }
26
+ /** 数据库门面:默认连接的快捷入口,`Db.connection(name)` 切换连接 */
27
+ export declare const Db: {
28
+ connection: (name: string) => ScopedDb;
29
+ table: <T extends Table>(table: T) => QueryBuilder<T>;
30
+ transaction: <R>(callback: (tx: AnyDatabase) => Promise<R>) => Promise<R>;
31
+ execute: <R = Record<string, unknown>>(query: SQL) => Promise<R[]>;
32
+ drizzle: () => Promise<AnyDatabase>;
33
+ };
34
+ /** findOrFail() 未找到数据,渲染为 404 */
35
+ export declare class ModelNotFoundException extends NotFoundException {
36
+ readonly model: string;
37
+ constructor(model: string, key?: unknown);
38
+ }
39
+ export declare class DatabaseError extends Error {
40
+ name: string;
41
+ }
42
+ /** 违反唯一约束(重复数据),渲染为 409 */
43
+ export declare class UniqueConstraintException extends ConflictException {
44
+ constructor(cause: unknown);
45
+ }
46
+ /** 把各数据库的唯一约束错误统一转换为 UniqueConstraintException,其余原样返回 */
47
+ export declare function translateDatabaseError(error: unknown): unknown;
48
+ /** 内嵌的迁移文件:相对后端源码目录的路径 → 文件内容(生产构建由框架打包进产物) */
49
+ type MigrationFiles = Record<string, string>;
50
+ interface MigrateOptions {
51
+ /** 连接名,默认 default 连接 */
52
+ connection?: string;
53
+ /** 迁移目录(drizzle-kit generate 的输出目录);配合 files 时为其中的相对路径 */
54
+ folder: string;
55
+ /** 从内嵌文件读取迁移而不是磁盘(Edge / Serverless 产物没有源码目录) */
56
+ files?: MigrationFiles;
57
+ manager?: DatabaseManager;
58
+ }
59
+ interface MigrateResult {
60
+ connection: string;
61
+ folder: string;
62
+ /** 本次执行的迁移数 */
63
+ applied: number;
64
+ /** 迁移总数 */
65
+ total: number;
66
+ }
67
+ /**
68
+ * 执行 drizzle-kit 生成的迁移(已执行过的会跳过)。迁移记录与 drizzle 自带的迁移器相同,
69
+ * 可与 drizzle-kit migrate 混用。
70
+ */
71
+ export declare function migrate(options: MigrateOptions): Promise<MigrateResult>;
72
+ interface RunMigrationsOptions {
73
+ /** 连接名,默认 default 连接 */
74
+ connection?: string;
75
+ /** 对所有 SQL 连接执行(跳过 MongoDB 与没有迁移的连接) */
76
+ all?: boolean;
77
+ /** 内嵌的迁移文件;省略时从 srcDir 读取 */
78
+ files?: MigrationFiles;
79
+ /** 后端源码目录,默认 process.cwd() */
80
+ srcDir?: string;
81
+ manager?: DatabaseManager;
82
+ }
83
+ interface RunMigrationsResult extends Partial<MigrateResult> {
84
+ connection: string;
85
+ folder: string;
86
+ /** 跳过的原因 */
87
+ skipped?: 'mongodb' | 'no-migrations';
88
+ }
89
+ /** 迁移目录(相对后端源码目录):连接配置的 migrations,默认连接为 database/migrations,其它为 database/migrations/<name> */
90
+ export declare function migrationsFolder(name?: string, manager?: DatabaseManager): string;
91
+ /**
92
+ * 按 config/database.ts 执行迁移:`haze db:migrate` 与生产构建的迁移入口
93
+ * (`node dist/server/migrate.mjs`、Lambda 的 `index.migrate`)共用。
94
+ */
95
+ export declare function runMigrations(options?: RunMigrationsOptions): Promise<RunMigrationsResult[]>;
96
+ interface DatabaseProviderOptions {
97
+ /** 默认读取 config/database.ts */
98
+ config?: ConfigInput<DatabaseConfig>;
99
+ }
100
+ /**
101
+ * 数据库服务提供者(放在 app/provider/DatabaseProvider.ts):
102
+ *
103
+ * ```ts
104
+ * export default databaseProvider()
105
+ * ```
106
+ *
107
+ * 注册 DatabaseManager(可 inject),并让 Db / Model 使用该配置。连接在首次查询时建立,
108
+ * 开发时跨热更新复用,应用关闭时释放。
109
+ */
110
+ export declare function databaseProvider(options?: DatabaseProviderOptions): ServiceProvider;
111
+ interface RefreshDatabaseOptions {
112
+ /** 迁移目录,默认 `<cwd>/database/migrations`(全栈项目为 `server/database/migrations`) */
113
+ migrations?: string;
114
+ connection?: string;
115
+ }
116
+ /**
117
+ * 测试用:重建连接并执行迁移。配合 `.env.test` 中的 `DATABASE_URL=:memory:`,每个用例得到全新的数据库。
118
+ *
119
+ * ```ts
120
+ * const t = useTestApp()
121
+ * beforeEach(() => refreshDatabase())
122
+ * ```
123
+ */
124
+ export declare function refreshDatabase(options?: RefreshDatabaseOptions): Promise<void>;
125
+ export { ActiveRecord, BelongsTo, type ColumnKey, Conditions, type Connection, type ConnectionConfig, type DatabaseConfig, DatabaseManager, type DatabaseManagerOptions, type DatabaseProviderOptions, type DefinedModel, type Dialect, type Direction, type Driver, type FieldOf, HasMany, HasOne, type Insert, type MigrateOptions, type MigrateResult, type MigrationFiles, Model, type ModelClass, type ModelMeta, ModelQuery, type ModelSettings, type ModelUserProviderOptions, type Operator, type Paginated, QueryBuilder, type QueryOf, type QueryOptions, type RecordClass, type RefreshDatabaseOptions, Relation, type RelationName, type RelationQuery, type RelationResult, type Row, type RunMigrationsOptions, type RunMigrationsResult, type ScopedDb, type Session, type TableOf, type TimestampOptions, type WhereCondition, type WhereObject, type WriteOptions, defineDatabaseConfig, dialectOf, resolveSession, setDatabaseManager, transaction, useDatabaseManager };