@hedystia/db 2.0.9 → 2.0.10
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/dist/core/database.cjs +6 -2
- package/dist/core/database.cjs.map +1 -1
- package/dist/core/database.d.cts +3 -3
- package/dist/core/database.d.mts +3 -3
- package/dist/core/database.mjs +6 -2
- package/dist/core/database.mjs.map +1 -1
- package/dist/core/repository.cjs +32 -16
- package/dist/core/repository.cjs.map +1 -1
- package/dist/core/repository.d.cts +2 -0
- package/dist/core/repository.d.mts +2 -0
- package/dist/core/repository.mjs +33 -16
- package/dist/core/repository.mjs.map +1 -1
- package/dist/drivers/index.cjs +2 -0
- package/dist/drivers/index.cjs.map +1 -1
- package/dist/drivers/index.mjs +3 -0
- package/dist/drivers/index.mjs.map +1 -1
- package/dist/drivers/mysql.cjs +2 -1
- package/dist/drivers/mysql.cjs.map +1 -1
- package/dist/drivers/mysql.mjs +2 -1
- package/dist/drivers/mysql.mjs.map +1 -1
- package/dist/drivers/s3.cjs +282 -0
- package/dist/drivers/s3.cjs.map +1 -0
- package/dist/drivers/s3.mjs +286 -0
- package/dist/drivers/s3.mjs.map +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs.map +1 -1
- package/dist/schema/registry.cjs +2 -2
- package/dist/schema/registry.cjs.map +1 -1
- package/dist/schema/registry.d.cts +1 -1
- package/dist/schema/registry.d.mts +1 -1
- package/dist/schema/registry.mjs +2 -2
- package/dist/schema/registry.mjs.map +1 -1
- package/dist/types.d.cts +31 -11
- package/dist/types.d.mts +31 -11
- package/package.json +6 -2
- package/readme.md +60 -71
package/dist/types.d.mts
CHANGED
|
@@ -5,7 +5,7 @@ type ColumnDataType = "integer" | "varchar" | "text" | "boolean" | "json" | "arr
|
|
|
5
5
|
* Database type identifier — can be a simple string shorthand or an object
|
|
6
6
|
* specifying both the database name and the driver provider to use
|
|
7
7
|
*/
|
|
8
|
-
type DatabaseType = "mysql" | "mariadb" | "sqlite" | "file" | {
|
|
8
|
+
type DatabaseType = "mysql" | "mariadb" | "sqlite" | "file" | "s3" | {
|
|
9
9
|
name: "mysql";
|
|
10
10
|
provider: "mysql" | "mysql2";
|
|
11
11
|
} | {
|
|
@@ -17,6 +17,9 @@ type DatabaseType = "mysql" | "mariadb" | "sqlite" | "file" | {
|
|
|
17
17
|
} | {
|
|
18
18
|
name: "file";
|
|
19
19
|
provider: string;
|
|
20
|
+
} | {
|
|
21
|
+
name: "s3";
|
|
22
|
+
provider: string;
|
|
20
23
|
};
|
|
21
24
|
/** Metadata describing a single database column */
|
|
22
25
|
interface ColumnMetadata {
|
|
@@ -217,8 +220,23 @@ interface FileConnectionConfig {
|
|
|
217
220
|
/** Directory where data files are stored */
|
|
218
221
|
directory: string;
|
|
219
222
|
}
|
|
223
|
+
/** Connection configuration for S3-based storage */
|
|
224
|
+
interface S3ConnectionConfig {
|
|
225
|
+
/** S3 bucket name */
|
|
226
|
+
bucket: string;
|
|
227
|
+
/** S3 endpoint URL */
|
|
228
|
+
endpoint?: string;
|
|
229
|
+
/** AWS region */
|
|
230
|
+
region?: string;
|
|
231
|
+
/** S3 access key ID */
|
|
232
|
+
accessKeyId?: string;
|
|
233
|
+
/** S3 secret access key */
|
|
234
|
+
secretAccessKey?: string;
|
|
235
|
+
/** Key prefix for stored objects */
|
|
236
|
+
prefix?: string;
|
|
237
|
+
}
|
|
220
238
|
/** Union of all supported connection configurations */
|
|
221
|
-
type ConnectionConfig = MySQLConnectionConfig | SQLiteConnectionConfig | FileConnectionConfig;
|
|
239
|
+
type ConnectionConfig = MySQLConnectionConfig | SQLiteConnectionConfig | FileConnectionConfig | S3ConnectionConfig;
|
|
222
240
|
/**
|
|
223
241
|
* Global cache configuration for the database instance.
|
|
224
242
|
* Controls query result caching and entity caching behavior.
|
|
@@ -361,16 +379,18 @@ type SchemaByName<S extends readonly AnyTableDef[], N extends string> = Extract<
|
|
|
361
379
|
type StripIdSuffix<S extends string> = S extends `${infer Base}Id` ? Base : S extends `${infer Base}_id` ? Base : S;
|
|
362
380
|
type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends ((x: infer I) => void) ? I : never;
|
|
363
381
|
type Simplify<T> = { [K in keyof T]: T[K] } & {};
|
|
364
|
-
type
|
|
365
|
-
|
|
382
|
+
type IsArray<T> = T extends readonly any[] ? true : false;
|
|
383
|
+
type SchemaKey<S, T extends AnyTableDef> = IsArray<S> extends true ? TableName<T> : { [K in keyof S]: S[K] extends T ? K : never }[keyof S];
|
|
384
|
+
type ForwardRelationEntries<S, T extends AnyTableDef> = TableRefs<T> extends infer R ? R extends DeferredRefMeta<infer Col, infer ToTable, any, infer Name> ? { [K in Name extends string ? Name : StripIdSuffix<Col>]: {
|
|
385
|
+
table: SchemaByName<InferSchemas<S>, ToTable>;
|
|
366
386
|
many: false;
|
|
367
387
|
} } : never : never;
|
|
368
|
-
type ReverseRelationEntry<U extends AnyTableDef, TargetName extends string> = TableRefs<U> extends infer R ? R extends DeferredRefMeta<any, TargetName, any, any> ? { [K in
|
|
388
|
+
type ReverseRelationEntry<S, U extends AnyTableDef, TargetName extends string> = TableRefs<U> extends infer R ? R extends DeferredRefMeta<any, TargetName, any, any> ? { [K in SchemaKey<S, U> & string]: {
|
|
369
389
|
table: U;
|
|
370
390
|
many: true;
|
|
371
391
|
} } : never : never;
|
|
372
|
-
type ReverseRelationEntries<S
|
|
373
|
-
type RelationsFor<S
|
|
392
|
+
type ReverseRelationEntries<S, T extends AnyTableDef> = ReverseRelationEntry<S, InferSchemas<S>[number], TableName<T>>;
|
|
393
|
+
type RelationsFor<S, T extends AnyTableDef> = Simplify<UnionToIntersection<ForwardRelationEntries<S, T> | ReverseRelationEntries<S, T>>>;
|
|
374
394
|
type InferSchemas<T> = T extends readonly AnyTableDef[] ? T : T extends Record<string, any> ? Array<{ [K in keyof T]: T[K] extends AnyTableDef ? T[K] : never }[keyof T]> : readonly AnyTableDef[];
|
|
375
395
|
type DepthPrev = [never, 0, 1, 2, 3];
|
|
376
396
|
type ExtractRelationRow<Rel> = Rel extends {
|
|
@@ -379,17 +399,17 @@ type ExtractRelationRow<Rel> = Rel extends {
|
|
|
379
399
|
type ExtractRelationMany<Rel> = Rel extends {
|
|
380
400
|
many: true;
|
|
381
401
|
} ? true : false;
|
|
382
|
-
type RelationQueryMap<S
|
|
402
|
+
type RelationQueryMap<S, T extends AnyTableDef, D extends number = 3> = [D] extends [never] ? {} : { [K in keyof RelationsFor<S, T>]: {
|
|
383
403
|
row: ExtractRelationRow<RelationsFor<S, T>[K]>;
|
|
384
404
|
many: ExtractRelationMany<RelationsFor<S, T>[K]>;
|
|
385
405
|
relations: RelationsFor<S, T>[K] extends {
|
|
386
406
|
table: infer R extends AnyTableDef;
|
|
387
407
|
} ? RelationQueryMap<S, R, DepthPrev[D]> : {};
|
|
388
408
|
} };
|
|
389
|
-
type ResolveWith<S
|
|
390
|
-
type ResolveResult<S
|
|
409
|
+
type ResolveWith<S, T extends AnyTableDef, W> = [W] extends [undefined] ? {} : W extends Record<string, any> ? { [K in keyof W & keyof RelationsFor<S, T>]: ExtractRelationMany<RelationsFor<S, T>[K]> extends true ? ExtractRelationRow<RelationsFor<S, T>[K]>[] : ExtractRelationRow<RelationsFor<S, T>[K]> | null } : {};
|
|
410
|
+
type ResolveResult<S, T extends AnyTableDef, O> = InferRow<T> & ResolveWith<S, T, O extends {
|
|
391
411
|
with: infer W;
|
|
392
412
|
} ? W : undefined>;
|
|
393
413
|
//#endregion
|
|
394
|
-
export { AnyTableDef, CacheConfig, ColumnDataType, ColumnMetadata, ConnectionConfig, DatabaseConfig, DatabaseDriver, DatabaseType, DeferredRefMeta, DeleteOptions, FileConnectionConfig, InferInsert, InferRow,
|
|
414
|
+
export { AnyTableDef, CacheConfig, ColumnDataType, ColumnMetadata, ConnectionConfig, DatabaseConfig, DatabaseDriver, DatabaseType, DeferredRefMeta, DeleteOptions, FileConnectionConfig, InferInsert, InferRow, InferUpdate, MigrationContext, MigrationDefinition, MySQLConnectionConfig, QueryOptions, ReferenceAction, RelationQueryMap, RelationsFor, Repository, ResolveResult, S3ConnectionConfig, SQLiteConnectionConfig, TableCacheConfig, TableDefinition, TableMetadata, UpdateOptions, WhereClause, WhereCondition };
|
|
395
415
|
//# sourceMappingURL=types.d.mts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hedystia/db",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.10",
|
|
4
4
|
"description": "Next-gen TypeScript ORM for building type-safe database layers at lightspeed",
|
|
5
5
|
"homepage": "https://docs.hedystia.com",
|
|
6
6
|
"devDependencies": {
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"mysql": ">= 2.0.0",
|
|
13
13
|
"better-sqlite3": ">= 12.0.0",
|
|
14
14
|
"sqlite3": ">= 6.0.0",
|
|
15
|
-
"sql.js": ">= 1.0.0"
|
|
15
|
+
"sql.js": ">= 1.0.0",
|
|
16
|
+
"@aws-sdk/client-s3": ">= 3.0.0"
|
|
16
17
|
},
|
|
17
18
|
"peerDependenciesMeta": {
|
|
18
19
|
"typescript": {
|
|
@@ -32,6 +33,9 @@
|
|
|
32
33
|
},
|
|
33
34
|
"sql.js": {
|
|
34
35
|
"optional": true
|
|
36
|
+
},
|
|
37
|
+
"@aws-sdk/client-s3": {
|
|
38
|
+
"optional": true
|
|
35
39
|
}
|
|
36
40
|
},
|
|
37
41
|
"bin": {
|
package/readme.md
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
# @hedystia/db
|
|
2
2
|
|
|
3
|
-
Next-gen TypeScript ORM for building type-safe database layers at lightspeed.
|
|
3
|
+
Next-gen TypeScript ORM for building type-safe database layers at lightspeed. Focused on performance, developer experience, and multi-driver flexibility.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 💎 **Type-safe by Design**: First-class TypeScript inference for all your queries.
|
|
8
|
+
- 🚀 **Multi-Driver Support**: Native drivers for SQLite, MySQL, and experimental support for File/S3 storage.
|
|
9
|
+
- ⚡ **Auto-Mapping**: Decouple database table/column names from your TypeScript models.
|
|
10
|
+
- 🔗 **Smart Relations**: Define relationships once and load them eagerly with intuitive `with` queries.
|
|
11
|
+
- 📦 **Zero-Config Migrations**: CLI for generating atomic migrations and schema synchronization.
|
|
12
|
+
- 🧠 **Built-in Caching**: Adaptive TTL caching with automatic invalidation on writes.
|
|
4
13
|
|
|
5
14
|
## Installation
|
|
6
15
|
|
|
@@ -8,111 +17,91 @@ Next-gen TypeScript ORM for building type-safe database layers at lightspeed.
|
|
|
8
17
|
bun add @hedystia/db
|
|
9
18
|
```
|
|
10
19
|
|
|
11
|
-
###
|
|
20
|
+
### Choose your Driver
|
|
12
21
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
2. `sqlite3`
|
|
19
|
-
3. `sql.js`
|
|
20
|
-
4. `bun:sqlite`
|
|
21
|
-
|
|
22
|
-
```bash
|
|
23
|
-
# You can install any of these
|
|
22
|
+
| Database | Driver Package | Command |
|
|
23
|
+
| --- | --- | --- |
|
|
24
|
+
| **SQLite** | `better-sqlite3` or `sqlite3` | `bun add better-sqlite3` |
|
|
25
|
+
| **MySQL** | `mysql2` | `bun add mysql2` |
|
|
26
|
+
| **Edge** | `bun:sqlite` or `sql.js` | *Built-in or bun add sql.js* |
|
|
24
27
|
|
|
25
|
-
|
|
26
|
-
bun add sqlite3
|
|
27
|
-
bun add sql.js
|
|
28
|
-
|
|
29
|
-
# Or use bun:sqlite if you are using Bun
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
#### MySQL & MariaDB Driver
|
|
33
|
-
Supports both `mysql2` and `mysql` libraries.
|
|
34
|
-
- **Naming**: Use `database: "mysql"` or `database: "mariadb"`.
|
|
35
|
-
- **MariaDB**: Fully supported through the same drivers.
|
|
36
|
-
|
|
37
|
-
```bash
|
|
38
|
-
bun add mysql2 # Preferred
|
|
39
|
-
# or
|
|
40
|
-
bun add mysql
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
# File-based (no extra install needed)
|
|
28
|
+
---
|
|
44
29
|
|
|
45
30
|
## Quick Start
|
|
46
31
|
|
|
47
|
-
### Define your
|
|
32
|
+
### Define your Schema
|
|
33
|
+
|
|
34
|
+
Define your tables using the `table` function. This allows you to map database properties to your preferred TypeScript keys.
|
|
48
35
|
|
|
49
|
-
```
|
|
36
|
+
```typescript
|
|
50
37
|
import { table, integer, varchar, text } from "@hedystia/db";
|
|
51
38
|
|
|
52
|
-
export const users = table("
|
|
39
|
+
export const users = table("example_users", {
|
|
53
40
|
id: integer().primaryKey().autoIncrement(),
|
|
54
|
-
|
|
41
|
+
fullName: varchar(255).name("full_name").notNull(),
|
|
55
42
|
email: varchar(255).unique(),
|
|
56
|
-
age: integer().default(0),
|
|
57
43
|
});
|
|
58
44
|
|
|
59
|
-
export const posts = table("
|
|
45
|
+
export const posts = table("example_posts", {
|
|
60
46
|
id: integer().primaryKey().autoIncrement(),
|
|
61
|
-
|
|
47
|
+
authorId: integer().name("author_id").references(() => users.id),
|
|
62
48
|
title: varchar(255).notNull(),
|
|
63
|
-
|
|
49
|
+
body: text(),
|
|
64
50
|
});
|
|
65
51
|
```
|
|
66
52
|
|
|
67
|
-
|
|
53
|
+
### Initialize the Database
|
|
68
54
|
|
|
69
|
-
|
|
55
|
+
Use the **Object Schema** method (recommended) to automatically alias your tables on the database instance.
|
|
70
56
|
|
|
71
|
-
```
|
|
57
|
+
```typescript
|
|
72
58
|
import { database } from "@hedystia/db";
|
|
73
|
-
import { users, posts } from "./
|
|
59
|
+
import { users, posts } from "./schema";
|
|
74
60
|
|
|
75
61
|
const db = database({
|
|
76
|
-
schemas:
|
|
62
|
+
schemas: { users, posts }, // Alias tables as db.users and db.posts
|
|
77
63
|
database: "sqlite",
|
|
78
|
-
connection: { filename: "./
|
|
79
|
-
syncSchemas: true,
|
|
80
|
-
cache: true,
|
|
64
|
+
connection: { filename: "./main.db" },
|
|
65
|
+
syncSchemas: true, // Auto-create tables during development
|
|
81
66
|
});
|
|
82
67
|
|
|
83
68
|
await db.initialize();
|
|
84
69
|
```
|
|
85
70
|
|
|
86
|
-
### Query your
|
|
87
|
-
|
|
88
|
-
```ts
|
|
89
|
-
// Insert
|
|
90
|
-
const user = await db.users.insert({ name: "Alice", email: "alice@example.com" });
|
|
91
|
-
|
|
92
|
-
// Find
|
|
93
|
-
const allUsers = await db.users.find();
|
|
94
|
-
const alice = await db.users.findFirst({ where: { name: "Alice" } });
|
|
71
|
+
### Query your Data
|
|
95
72
|
|
|
96
|
-
|
|
97
|
-
|
|
73
|
+
```typescript
|
|
74
|
+
// Insert a record
|
|
75
|
+
const newUser = await db.users.insert({
|
|
76
|
+
fullName: "Alice Vance",
|
|
77
|
+
email: "alice@hedystia.com"
|
|
78
|
+
});
|
|
98
79
|
|
|
99
|
-
//
|
|
100
|
-
await db.users.
|
|
80
|
+
// Find with eager relations
|
|
81
|
+
const userWithPosts = await db.users.findFirst({
|
|
82
|
+
where: { fullName: "Alice Vance" },
|
|
83
|
+
with: { posts: true }
|
|
84
|
+
});
|
|
101
85
|
|
|
102
|
-
//
|
|
103
|
-
const
|
|
86
|
+
// Powerful filtering
|
|
87
|
+
const recentPosts = await db.posts.find({
|
|
88
|
+
where: { authorId: 1, title: { like: "%Hedystia%" } },
|
|
89
|
+
orderBy: { id: "desc" },
|
|
90
|
+
take: 5
|
|
91
|
+
});
|
|
104
92
|
```
|
|
105
93
|
|
|
106
|
-
##
|
|
94
|
+
## Advanced Usage
|
|
95
|
+
|
|
96
|
+
Explore our detailed documentation for advanced topics:
|
|
107
97
|
|
|
108
|
-
- **
|
|
109
|
-
- **
|
|
110
|
-
- **
|
|
111
|
-
- **Migrations**
|
|
112
|
-
- **
|
|
113
|
-
- **Relations** — Foreign key references with eager loading
|
|
98
|
+
- [**Schema Definition**](https://docs.hedystia.com/db/schema) - Custom types, constraints, and aliases.
|
|
99
|
+
- [**Relations**](https://docs.hedystia.com/db/relations) - One-to-many, many-to-one, and cascade behaviors.
|
|
100
|
+
- [**Queries**](https://docs.hedystia.com/db/queries) - Full API reference for find, insert, update, and delete.
|
|
101
|
+
- [**CLI & Migrations**](https://docs.hedystia.com/db/cli) - Managing schema changes safely.
|
|
102
|
+
- [**Drivers**](https://docs.hedystia.com/db/drivers/sqlite) - Specific configuration for MySQL, SQLite, S3, and more.
|
|
114
103
|
|
|
115
|
-
## Links
|
|
104
|
+
## Community & Links
|
|
116
105
|
|
|
117
106
|
- [Documentation](https://docs.hedystia.com/db/start)
|
|
118
107
|
- [GitHub](https://github.com/Hedystia/Hedystia)
|