@hedystia/db 2.0.3 → 2.0.5
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/cache/manager.cjs +23 -0
- package/dist/cache/manager.cjs.map +1 -1
- package/dist/cache/manager.d.cts +14 -0
- package/dist/cache/manager.d.mts +14 -0
- package/dist/cache/manager.mjs +23 -0
- package/dist/cache/manager.mjs.map +1 -1
- package/dist/core/database.cjs +17 -5
- package/dist/core/database.cjs.map +1 -1
- package/dist/core/database.d.cts +4 -9
- package/dist/core/database.d.mts +4 -9
- package/dist/core/database.mjs +17 -5
- package/dist/core/database.mjs.map +1 -1
- package/dist/core/repository.cjs +32 -10
- package/dist/core/repository.cjs.map +1 -1
- package/dist/core/repository.d.cts +8 -1
- package/dist/core/repository.d.mts +8 -1
- package/dist/core/repository.mjs +32 -10
- package/dist/core/repository.mjs.map +1 -1
- package/dist/drivers/sql-compiler.cjs +6 -0
- package/dist/drivers/sql-compiler.cjs.map +1 -1
- package/dist/drivers/sql-compiler.mjs +6 -0
- package/dist/drivers/sql-compiler.mjs.map +1 -1
- package/dist/index.cjs +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.mts +3 -3
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/dist/schema/column.cjs +2 -2
- package/dist/schema/column.cjs.map +1 -1
- package/dist/schema/column.d.cts +2 -2
- package/dist/schema/column.d.mts +2 -2
- package/dist/schema/column.mjs +2 -2
- package/dist/schema/column.mjs.map +1 -1
- package/dist/schema/columns/index.cjs +12 -0
- package/dist/schema/columns/index.cjs.map +1 -1
- package/dist/schema/columns/index.d.cts +10 -2
- package/dist/schema/columns/index.d.mts +10 -2
- package/dist/schema/columns/index.mjs +12 -1
- package/dist/schema/columns/index.mjs.map +1 -1
- package/dist/schema/table.cjs +2 -1
- package/dist/schema/table.cjs.map +1 -1
- package/dist/schema/table.d.cts +4 -2
- package/dist/schema/table.d.mts +4 -2
- package/dist/schema/table.mjs +2 -1
- package/dist/schema/table.mjs.map +1 -1
- package/dist/types.d.cts +109 -4
- package/dist/types.d.mts +109 -4
- package/package.json +1 -1
package/dist/types.d.mts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
//#region src/types.d.ts
|
|
2
|
-
|
|
2
|
+
/** Supported column data types for schema definitions */
|
|
3
|
+
type ColumnDataType = "integer" | "varchar" | "text" | "boolean" | "json" | "array" | "datetime" | "decimal" | "float" | "char" | "timestamp" | "bigint" | "blob";
|
|
4
|
+
/**
|
|
5
|
+
* Database type identifier — can be a simple string shorthand or an object
|
|
6
|
+
* specifying both the database name and the driver provider to use
|
|
7
|
+
*/
|
|
3
8
|
type DatabaseType = "mysql" | "mariadb" | "sqlite" | "file" | {
|
|
4
9
|
name: "mysql";
|
|
5
10
|
provider: "mysql" | "mysql2";
|
|
@@ -13,18 +18,31 @@ type DatabaseType = "mysql" | "mariadb" | "sqlite" | "file" | {
|
|
|
13
18
|
name: "file";
|
|
14
19
|
provider: string;
|
|
15
20
|
};
|
|
21
|
+
/** Metadata describing a single database column */
|
|
16
22
|
interface ColumnMetadata {
|
|
23
|
+
/** Column name in the database */
|
|
17
24
|
name: string;
|
|
25
|
+
/** Data type of the column */
|
|
18
26
|
type: ColumnDataType;
|
|
27
|
+
/** Whether this column is a primary key */
|
|
19
28
|
primaryKey: boolean;
|
|
29
|
+
/** Whether this column auto-increments */
|
|
20
30
|
autoIncrement: boolean;
|
|
31
|
+
/** Whether this column disallows NULL values */
|
|
21
32
|
notNull: boolean;
|
|
33
|
+
/** Whether this column has a UNIQUE constraint */
|
|
22
34
|
unique: boolean;
|
|
35
|
+
/** Default value for the column, or `undefined` if none */
|
|
23
36
|
defaultValue: unknown;
|
|
37
|
+
/** Maximum character length (for varchar/char types) */
|
|
24
38
|
length?: number;
|
|
39
|
+
/** Total number of digits (for decimal types) */
|
|
25
40
|
precision?: number;
|
|
41
|
+
/** Number of decimal digits (for decimal types) */
|
|
26
42
|
scale?: number;
|
|
43
|
+
/** Custom database column name alias, if different from the property key */
|
|
27
44
|
columnAlias?: string;
|
|
45
|
+
/** Foreign key reference metadata, resolved after registration */
|
|
28
46
|
references?: {
|
|
29
47
|
table: string;
|
|
30
48
|
column: string;
|
|
@@ -33,11 +51,16 @@ interface ColumnMetadata {
|
|
|
33
51
|
relationName?: string;
|
|
34
52
|
};
|
|
35
53
|
}
|
|
54
|
+
/** Action to take when a referenced row is deleted or updated */
|
|
36
55
|
type ReferenceAction = "CASCADE" | "SET NULL" | "RESTRICT" | "NO ACTION";
|
|
56
|
+
/** Metadata describing a database table and its columns */
|
|
37
57
|
interface TableMetadata {
|
|
58
|
+
/** Table name in the database */
|
|
38
59
|
name: string;
|
|
60
|
+
/** Array of column metadata for this table */
|
|
39
61
|
columns: ColumnMetadata[];
|
|
40
62
|
}
|
|
63
|
+
/** Deferred foreign key reference metadata, used internally for lazy resolution */
|
|
41
64
|
type DeferredRefMeta<ColumnName extends string = string, TargetTable extends string = string, TargetColumn extends string = string, RelationName extends string | undefined = string | undefined> = {
|
|
42
65
|
columnName: ColumnName;
|
|
43
66
|
targetTable: TargetTable;
|
|
@@ -46,6 +69,10 @@ type DeferredRefMeta<ColumnName extends string = string, TargetTable extends str
|
|
|
46
69
|
onDelete?: ReferenceAction;
|
|
47
70
|
onUpdate?: ReferenceAction;
|
|
48
71
|
};
|
|
72
|
+
/**
|
|
73
|
+
* Internal representation of a table schema, including column metadata,
|
|
74
|
+
* deferred references, and optional per-table cache configuration
|
|
75
|
+
*/
|
|
49
76
|
type TableDefinition<T extends Record<string, any> = Record<string, any>, C extends Record<string, any> = {}, N extends string = string, Refs extends DeferredRefMeta = any> = {
|
|
50
77
|
__table: true;
|
|
51
78
|
__name: N;
|
|
@@ -53,6 +80,7 @@ type TableDefinition<T extends Record<string, any> = Record<string, any>, C exte
|
|
|
53
80
|
__refs: Refs;
|
|
54
81
|
__columns: ColumnMetadata[];
|
|
55
82
|
__columnMap: Record<string, string>;
|
|
83
|
+
__cache?: TableCacheConfig;
|
|
56
84
|
__deferredRefs: Array<{
|
|
57
85
|
columnName: string;
|
|
58
86
|
resolve: () => {
|
|
@@ -64,12 +92,16 @@ type TableDefinition<T extends Record<string, any> = Record<string, any>, C exte
|
|
|
64
92
|
relationName?: string;
|
|
65
93
|
}>;
|
|
66
94
|
} & C;
|
|
95
|
+
/** Extract the row type from a table definition */
|
|
67
96
|
type InferRow<T> = T extends {
|
|
68
97
|
__row: infer R;
|
|
69
98
|
} ? R : never;
|
|
99
|
+
/** Extract the insertable type from a table definition (auto-increment keys become optional) */
|
|
70
100
|
type InferInsert<T> = T extends TableDefinition<infer R, any, any> ? { [K in keyof R as K extends AutoIncrementKeys<T> ? never : K]: R[K] } & { [K in AutoIncrementKeys<T>]?: R[K] } : never;
|
|
71
101
|
type AutoIncrementKeys<T> = T extends TableDefinition<infer R, any, any> ? keyof R : never;
|
|
102
|
+
/** Extract the updatable type from a table definition (all fields become optional) */
|
|
72
103
|
type InferUpdate<T> = T extends TableDefinition<infer R, any, any> ? Partial<R> : never;
|
|
104
|
+
/** Condition operators for a single column in a WHERE clause */
|
|
73
105
|
interface WhereCondition {
|
|
74
106
|
eq?: unknown;
|
|
75
107
|
neq?: unknown;
|
|
@@ -84,25 +116,38 @@ interface WhereCondition {
|
|
|
84
116
|
isNull?: boolean;
|
|
85
117
|
between?: [unknown, unknown];
|
|
86
118
|
}
|
|
119
|
+
/** Type-safe WHERE clause supporting equality, operators, and logical combinators (OR/AND) */
|
|
87
120
|
type WhereClause<T = Record<string, any>> = { [K in keyof T]?: T[K] | WhereCondition } & {
|
|
88
121
|
OR?: WhereClause<T>[];
|
|
89
122
|
AND?: WhereClause<T>[];
|
|
90
123
|
};
|
|
124
|
+
/** Options for querying rows — filtering, sorting, pagination, and relation loading */
|
|
91
125
|
interface QueryOptions<T = Record<string, any>, Rel extends Record<string, any> = {}> {
|
|
126
|
+
/** Filter conditions */
|
|
92
127
|
where?: WhereClause<T>;
|
|
128
|
+
/** Columns to include in the result */
|
|
93
129
|
select?: Extract<keyof T, string>[];
|
|
130
|
+
/** Sort order for results */
|
|
94
131
|
orderBy?: Partial<Record<Extract<keyof T, string>, "asc" | "desc">>;
|
|
132
|
+
/** Maximum number of rows to return */
|
|
95
133
|
take?: number;
|
|
134
|
+
/** Number of rows to skip (for pagination) */
|
|
96
135
|
skip?: number;
|
|
136
|
+
/** Related tables to eagerly load */
|
|
97
137
|
with?: { [K in keyof Rel]?: boolean | QueryOptions<Rel[K] extends {
|
|
98
138
|
row: infer R;
|
|
99
139
|
} ? R : Rel[K]> };
|
|
100
140
|
}
|
|
141
|
+
/** Options for an UPDATE operation */
|
|
101
142
|
interface UpdateOptions<T = Record<string, any>> {
|
|
143
|
+
/** Filter to select which rows to update */
|
|
102
144
|
where: WhereClause<T>;
|
|
145
|
+
/** Partial data to apply to matching rows */
|
|
103
146
|
data: Partial<T>;
|
|
104
147
|
}
|
|
148
|
+
/** Options for a DELETE operation */
|
|
105
149
|
interface DeleteOptions<T = Record<string, any>> {
|
|
150
|
+
/** Filter to select which rows to delete */
|
|
106
151
|
where: WhereClause<T>;
|
|
107
152
|
}
|
|
108
153
|
interface MySQLConnectionConfig {
|
|
@@ -112,28 +157,81 @@ interface MySQLConnectionConfig {
|
|
|
112
157
|
password: string;
|
|
113
158
|
database: string;
|
|
114
159
|
}
|
|
160
|
+
/** Connection configuration for SQLite databases */
|
|
115
161
|
interface SQLiteConnectionConfig {
|
|
162
|
+
/** Path to the SQLite database file */
|
|
116
163
|
filename: string;
|
|
117
164
|
}
|
|
165
|
+
/** Connection configuration for file-based (JSON) storage */
|
|
118
166
|
interface FileConnectionConfig {
|
|
167
|
+
/** Directory where data files are stored */
|
|
119
168
|
directory: string;
|
|
120
169
|
}
|
|
170
|
+
/** Union of all supported connection configurations */
|
|
121
171
|
type ConnectionConfig = MySQLConnectionConfig | SQLiteConnectionConfig | FileConnectionConfig;
|
|
172
|
+
/**
|
|
173
|
+
* Global cache configuration for the database instance.
|
|
174
|
+
* Controls query result caching and entity caching behavior.
|
|
175
|
+
*/
|
|
122
176
|
interface CacheConfig {
|
|
177
|
+
/** Whether caching is enabled */
|
|
123
178
|
enabled: boolean;
|
|
179
|
+
/** Base time-to-live in milliseconds (default: 60000) */
|
|
124
180
|
ttl?: number;
|
|
181
|
+
/** Maximum TTL in milliseconds — limits adaptive TTL scaling (default: 300000) */
|
|
125
182
|
maxTtl?: number;
|
|
183
|
+
/** Maximum number of cache entries before eviction (default: 10000) */
|
|
126
184
|
maxEntries?: number;
|
|
127
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* Per-table cache configuration. Set on individual table definitions to override
|
|
188
|
+
* or enable caching for specific tables independently of the global setting.
|
|
189
|
+
* Useful for frequently accessed tables like user sessions or login data.
|
|
190
|
+
*/
|
|
191
|
+
interface TableCacheConfig {
|
|
192
|
+
/** Whether caching is enabled for this table */
|
|
193
|
+
enabled: boolean;
|
|
194
|
+
/** Base time-to-live in milliseconds for this table's cache entries */
|
|
195
|
+
ttl?: number;
|
|
196
|
+
/** Maximum TTL in milliseconds for this table's cache entries */
|
|
197
|
+
maxTtl?: number;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Top-level configuration for creating a database instance.
|
|
201
|
+
* Defines schemas, database type, connection, and optional features like
|
|
202
|
+
* schema sync, migrations, and caching.
|
|
203
|
+
*/
|
|
128
204
|
interface DatabaseConfig {
|
|
129
|
-
|
|
130
|
-
|
|
205
|
+
/**
|
|
206
|
+
* Table definitions — either an array of tables or a module namespace object.
|
|
207
|
+
* @example
|
|
208
|
+
* // Array form
|
|
209
|
+
* schemas: [users, posts]
|
|
210
|
+
* // Module namespace form (import * as schemas from "./schemas")
|
|
211
|
+
* schemas: schemas
|
|
212
|
+
*/
|
|
213
|
+
schemas: readonly AnyTableDef[] | Record<string, unknown>;
|
|
214
|
+
/**
|
|
215
|
+
* Migration definitions — either an array of migrations or a module namespace object.
|
|
216
|
+
* @example
|
|
217
|
+
* // Array form
|
|
218
|
+
* migrations: [createUsers, addAge]
|
|
219
|
+
* // Module namespace form (import * as migrations from "./migrations")
|
|
220
|
+
* migrations: migrations
|
|
221
|
+
*/
|
|
222
|
+
migrations?: MigrationDefinition[] | Record<string, unknown>;
|
|
223
|
+
/** Database type and optional driver provider */
|
|
131
224
|
database: DatabaseType;
|
|
225
|
+
/** Connection configuration (or array for future multi-connection support) */
|
|
132
226
|
connection: ConnectionConfig | ConnectionConfig[];
|
|
227
|
+
/** Whether to run pending migrations on initialization */
|
|
133
228
|
runMigrations?: boolean;
|
|
229
|
+
/** Whether to auto-create tables and add missing columns on initialization */
|
|
134
230
|
syncSchemas?: boolean;
|
|
231
|
+
/** Enable caching — `true` for defaults, or a {@link CacheConfig} object for fine-tuning */
|
|
135
232
|
cache?: boolean | CacheConfig;
|
|
136
233
|
}
|
|
234
|
+
/** Context object passed to migration up/down functions */
|
|
137
235
|
interface MigrationContext {
|
|
138
236
|
schema: {
|
|
139
237
|
createTable: (table: TableDefinition) => Promise<void>;
|
|
@@ -146,11 +244,16 @@ interface MigrationContext {
|
|
|
146
244
|
};
|
|
147
245
|
sql: (query: string, params?: unknown[]) => Promise<unknown>;
|
|
148
246
|
}
|
|
247
|
+
/** A named migration with up (apply) and down (rollback) functions */
|
|
149
248
|
interface MigrationDefinition {
|
|
249
|
+
/** Unique migration name (used for tracking executed migrations) */
|
|
150
250
|
name: string;
|
|
251
|
+
/** Function to apply the migration */
|
|
151
252
|
up: (ctx: MigrationContext) => Promise<void>;
|
|
253
|
+
/** Function to rollback the migration */
|
|
152
254
|
down: (ctx: MigrationContext) => Promise<void>;
|
|
153
255
|
}
|
|
256
|
+
/** Low-level database driver interface — implemented per database backend */
|
|
154
257
|
interface DatabaseDriver {
|
|
155
258
|
connect(): Promise<void>;
|
|
156
259
|
disconnect(): Promise<void>;
|
|
@@ -166,6 +269,7 @@ interface DatabaseDriver {
|
|
|
166
269
|
transaction<T>(fn: () => Promise<T>): Promise<T>;
|
|
167
270
|
getAllTableColumns?(): Promise<Record<string, ColumnMetadata[]>>;
|
|
168
271
|
}
|
|
272
|
+
/** Generic repository interface providing CRUD operations for a table */
|
|
169
273
|
interface Repository<T extends Record<string, any>> {
|
|
170
274
|
find(options?: QueryOptions<T>): Promise<T[]>;
|
|
171
275
|
findMany(options?: QueryOptions<T>): Promise<T[]>;
|
|
@@ -206,6 +310,7 @@ type ReverseRelationEntry<U extends AnyTableDef, TargetName extends string> = Ta
|
|
|
206
310
|
} } : never : never;
|
|
207
311
|
type ReverseRelationEntries<S extends readonly AnyTableDef[], T extends AnyTableDef> = ReverseRelationEntry<S[number], TableName<T>>;
|
|
208
312
|
type RelationsFor<S extends readonly AnyTableDef[], T extends AnyTableDef> = Simplify<UnionToIntersection<ForwardRelationEntries<S, T> | ReverseRelationEntries<S, T>>>;
|
|
313
|
+
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[];
|
|
209
314
|
type DepthPrev = [never, 0, 1, 2, 3];
|
|
210
315
|
type ExtractRelationRow<Rel> = Rel extends {
|
|
211
316
|
table: infer R;
|
|
@@ -225,5 +330,5 @@ type ResolveResult<S extends readonly AnyTableDef[], T extends AnyTableDef, O> =
|
|
|
225
330
|
with: infer W;
|
|
226
331
|
} ? W : undefined>;
|
|
227
332
|
//#endregion
|
|
228
|
-
export { AnyTableDef, CacheConfig, ColumnDataType, ColumnMetadata, ConnectionConfig, DatabaseConfig, DatabaseDriver, DatabaseType, DeferredRefMeta, DeleteOptions, FileConnectionConfig, InferInsert, InferRow, InferUpdate, MigrationContext, MigrationDefinition, MySQLConnectionConfig, QueryOptions, ReferenceAction, RelationQueryMap, RelationsFor, Repository, ResolveResult, SQLiteConnectionConfig, TableDefinition, TableMetadata, UpdateOptions, WhereClause, WhereCondition };
|
|
333
|
+
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 };
|
|
229
334
|
//# sourceMappingURL=types.d.mts.map
|