@hedystia/db 2.0.9 → 2.0.11

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/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 ForwardRelationEntries<S extends readonly AnyTableDef[], 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>]: {
365
- table: SchemaByName<S, ToTable>;
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 TableName<U>]: {
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 extends readonly AnyTableDef[], T extends AnyTableDef> = ReverseRelationEntry<S[number], TableName<T>>;
373
- type RelationsFor<S extends readonly AnyTableDef[], T extends AnyTableDef> = Simplify<UnionToIntersection<ForwardRelationEntries<S, T> | ReverseRelationEntries<S, T>>>;
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 extends readonly AnyTableDef[], T extends AnyTableDef, D extends number = 3> = [D] extends [never] ? {} : { [K in keyof RelationsFor<S, T>]: {
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 extends readonly AnyTableDef[], 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 } : {};
390
- type ResolveResult<S extends readonly AnyTableDef[], T extends AnyTableDef, O> = InferRow<T> & ResolveWith<S, T, O extends {
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, InferSchemas, InferUpdate, MigrationContext, MigrationDefinition, MySQLConnectionConfig, QueryOptions, ReferenceAction, RelationQueryMap, RelationsFor, Repository, ResolveResult, SQLiteConnectionConfig, TableCacheConfig, TableDefinition, TableMetadata, UpdateOptions, WhereClause, WhereCondition };
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.9",
3
+ "version": "2.0.11",
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,94 @@ Next-gen TypeScript ORM for building type-safe database layers at lightspeed.
8
17
  bun add @hedystia/db
9
18
  ```
10
19
 
11
- ### Database Drivers
12
-
13
- Install the driver for your database. The ORM will automatically detect and use the most appropriate library available.
14
-
15
- #### SQLite Driver
16
- Supports multiple libraries in the following priority:
17
- 1. `better-sqlite3`
18
- 2. `sqlite3`
19
- 3. `sql.js`
20
- 4. `bun:sqlite`
21
-
22
- ```bash
23
- # You can install any of these
24
-
25
- bun add better-sqlite3
26
- bun add sqlite3
27
- bun add sql.js
28
-
29
- # Or use bun:sqlite if you are using Bun
30
- ```
20
+ ### Choose your Driver
31
21
 
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.
22
+ | Database | Driver Package | Command |
23
+ | --- | --- | --- |
24
+ | **SQLite** | `better-sqlite3`, `sqlite3`, `sql.js` | `bun add better-sqlite3` |
25
+ | **MySQL** | `mysql2` or `mysql` | `bun add mysql2` |
26
+ | **S3** | `@aws-sdk/client-s3` | `bun add @aws-sdk/client-s3` |
27
+ | **File** | *Built-in* | *No installation needed* |
28
+ | **Edge** | `bun:sqlite` (Bun only) | *No installation needed* |
36
29
 
37
- ```bash
38
- bun add mysql2 # Preferred
39
- # or
40
- bun add mysql
41
- ```
42
30
 
43
- # File-based (no extra install needed)
31
+ ---
44
32
 
45
33
  ## Quick Start
46
34
 
47
- ### Define your schema
35
+ ### Define your Schema
36
+
37
+ Define your tables using the `table` function. This allows you to map database properties to your preferred TypeScript keys.
48
38
 
49
- ```ts
39
+ ```typescript
50
40
  import { table, integer, varchar, text } from "@hedystia/db";
51
41
 
52
- export const users = table("users", {
42
+ export const users = table("example_users", {
53
43
  id: integer().primaryKey().autoIncrement(),
54
- name: varchar(255).notNull(),
44
+ fullName: varchar(255).name("full_name").notNull(),
55
45
  email: varchar(255).unique(),
56
- age: integer().default(0),
57
46
  });
58
47
 
59
- export const posts = table("posts", {
48
+ export const posts = table("example_posts", {
60
49
  id: integer().primaryKey().autoIncrement(),
61
- userId: integer().references(() => users.id, { onDelete: "CASCADE" }),
50
+ authorId: integer().name("author_id").references(() => users.id),
62
51
  title: varchar(255).notNull(),
63
- content: text(),
52
+ body: text(),
64
53
  });
65
54
  ```
66
55
 
67
- > **Note:** The `d.xxx()` prefix style (e.g., `d.integer()`) is still supported for backward compatibility.
56
+ ### Initialize the Database
68
57
 
69
- ### Create the database
58
+ Use the **Object Schema** method (recommended) to automatically alias your tables on the database instance.
70
59
 
71
- ```ts
60
+ ```typescript
72
61
  import { database } from "@hedystia/db";
73
- import { users, posts } from "./schemas";
62
+ import { users, posts } from "./schema";
74
63
 
75
64
  const db = database({
76
- schemas: [users, posts],
65
+ schemas: { users, posts }, // Alias tables as db.users and db.posts
77
66
  database: "sqlite",
78
- connection: { filename: "./data.db" },
79
- syncSchemas: true,
80
- cache: true,
67
+ connection: { filename: "./main.db" },
68
+ syncSchemas: true, // Auto-create tables during development
81
69
  });
82
70
 
83
71
  await db.initialize();
84
72
  ```
85
73
 
86
- ### Query your data
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" } });
74
+ ### Query your Data
95
75
 
96
- // Update
97
- await db.users.update({ where: { id: 1 }, data: { age: 26 } });
76
+ ```typescript
77
+ // Insert a record
78
+ const newUser = await db.users.insert({
79
+ fullName: "Alice Vance",
80
+ email: "alice@hedystia.com"
81
+ });
98
82
 
99
- // Delete
100
- await db.users.delete({ where: { id: 1 } });
83
+ // Find with eager relations
84
+ const userWithPosts = await db.users.findFirst({
85
+ where: { fullName: "Alice Vance" },
86
+ with: { posts: true }
87
+ });
101
88
 
102
- // Relations
103
- const usersWithPosts = await db.users.find({ with: { posts: true } });
89
+ // Powerful filtering
90
+ const recentPosts = await db.posts.find({
91
+ where: { authorId: 1, title: { like: "%Hedystia%" } },
92
+ orderBy: { id: "desc" },
93
+ take: 5
94
+ });
104
95
  ```
105
96
 
106
- ## Features
97
+ ## Advanced Usage
98
+
99
+ Explore our detailed documentation for advanced topics:
107
100
 
108
- - **One schema, multiple databases** — MySQL, SQLite, and File drivers
109
- - **Type-safe queries** Full TypeScript inference for all operations
110
- - **Smart caching** Adaptive TTL cache with automatic invalidation
111
- - **Migrations** CLI and programmatic migration support
112
- - **Schema sync** Automatic table creation and column diffing
113
- - **Relations** — Foreign key references with eager loading
101
+ - [**Schema Definition**](https://docs.hedystia.com/db/schema) - Custom types, constraints, and aliases.
102
+ - [**Relations**](https://docs.hedystia.com/db/relations) - One-to-many, many-to-one, and cascade behaviors.
103
+ - [**Queries**](https://docs.hedystia.com/db/queries) - Full API reference for find, insert, update, and delete.
104
+ - [**CLI & Migrations**](https://docs.hedystia.com/db/cli) - Managing schema changes safely.
105
+ - [**Drivers**](https://docs.hedystia.com/db/drivers/sqlite) - Specific configuration for MySQL, SQLite, S3, and more.
114
106
 
115
- ## Links
107
+ ## Community & Links
116
108
 
117
109
  - [Documentation](https://docs.hedystia.com/db/start)
118
110
  - [GitHub](https://github.com/Hedystia/Hedystia)