@rawsql-ts/sql-contract 0.1.0
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 +21 -0
- package/README.md +476 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/mapper/index.d.ts +192 -0
- package/dist/mapper/index.js +691 -0
- package/dist/mapper/index.js.map +1 -0
- package/dist/query-params.d.ts +2 -0
- package/dist/query-params.js +3 -0
- package/dist/query-params.js.map +1 -0
- package/dist/writer/index.d.ts +52 -0
- package/dist/writer/index.js +155 -0
- package/dist/writer/index.js.map +1 -0
- package/dist/writer/presets.d.ts +25 -0
- package/dist/writer/presets.js +67 -0
- package/dist/writer/presets.js.map +1 -0
- package/package.json +63 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import type { QueryParams } from '../query-params';
|
|
2
|
+
export type { QueryParams } from '../query-params';
|
|
3
|
+
/**
|
|
4
|
+
* A single database row returned by a SQL driver.
|
|
5
|
+
* Row keys are SQL column names, which must be strings; symbol keys are not supported.
|
|
6
|
+
*/
|
|
7
|
+
export type Row = Record<string, unknown>;
|
|
8
|
+
/**
|
|
9
|
+
* Executes SQL and returns the resulting rows.
|
|
10
|
+
*
|
|
11
|
+
* The mapper keeps this layer DBMS/driver agnostic; callers inject the concrete
|
|
12
|
+
* executor that speaks to the desired database.
|
|
13
|
+
*/
|
|
14
|
+
export type QueryExecutor = (sql: string, params: QueryParams) => Promise<Row[]>;
|
|
15
|
+
/**
|
|
16
|
+
* Defines how a column prefix, key, and optional overrides describe a row mapping.
|
|
17
|
+
*/
|
|
18
|
+
export interface RowMappingOptions<T, K extends Extract<keyof T, string>> {
|
|
19
|
+
name: string;
|
|
20
|
+
key: K;
|
|
21
|
+
prefix?: string;
|
|
22
|
+
columnMap?: Partial<Record<Extract<keyof T, string>, string>>;
|
|
23
|
+
coerce?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Row mappings rely on a very narrow coercion helper; it only receives the
|
|
26
|
+
* raw column value so callers can swap in their own rules.
|
|
27
|
+
*/
|
|
28
|
+
coerceFn?: (value: unknown) => unknown;
|
|
29
|
+
}
|
|
30
|
+
export type { RowMappingOptions as EntityOptions };
|
|
31
|
+
/**
|
|
32
|
+
* Describes how a child mapping references a parent mapping.
|
|
33
|
+
*/
|
|
34
|
+
export interface BelongsToOptions {
|
|
35
|
+
optional?: boolean;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Controls how raw column names are normalized for simple mapping.
|
|
39
|
+
*/
|
|
40
|
+
export type KeyTransform = 'snake_to_camel' | 'none' | ((column: string) => string);
|
|
41
|
+
/**
|
|
42
|
+
* Supported type hints for `mapSimpleRows`, controlling how primitive values are coerced before identifier normalization.
|
|
43
|
+
*/
|
|
44
|
+
export type SimpleMapTypeHint = 'string' | 'number' | 'boolean' | 'date' | 'bigint';
|
|
45
|
+
/**
|
|
46
|
+
* Options that influence simple (duck-typed) row mapping.
|
|
47
|
+
*/
|
|
48
|
+
export interface MapperOptions {
|
|
49
|
+
keyTransform?: KeyTransform;
|
|
50
|
+
idKeysAsString?: boolean;
|
|
51
|
+
typeHints?: Record<string, SimpleMapTypeHint>;
|
|
52
|
+
coerceDates?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Receives both the normalized property name and the original column name so callers
|
|
55
|
+
* can implement property-specific logic without re-running normalization.
|
|
56
|
+
*/
|
|
57
|
+
coerceFn?: (args: {
|
|
58
|
+
key: string;
|
|
59
|
+
sourceKey: string;
|
|
60
|
+
value: unknown;
|
|
61
|
+
}) => unknown;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A mapping-bound reader that executes SQL and enforces row-count contracts.
|
|
65
|
+
*/
|
|
66
|
+
export interface MapperReader<T> {
|
|
67
|
+
list(sql: string, params?: QueryParams): Promise<T[]>;
|
|
68
|
+
one(sql: string, params?: QueryParams): Promise<T>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Named presets for simple mapping that avoid implicit inference.
|
|
72
|
+
*/
|
|
73
|
+
export declare const mapperPresets: {
|
|
74
|
+
safe(): MapperOptions;
|
|
75
|
+
appLike(): MapperOptions;
|
|
76
|
+
};
|
|
77
|
+
type ParentLink<T> = {
|
|
78
|
+
propertyName: string;
|
|
79
|
+
parent: RowMapping<any>;
|
|
80
|
+
localKey: string;
|
|
81
|
+
optional: boolean;
|
|
82
|
+
};
|
|
83
|
+
type RowContext = {
|
|
84
|
+
row: Row;
|
|
85
|
+
normalizedColumns: Map<string, string>;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Builds a row mapping that can be consumed by {@link Mapper#query} or {@link mapRows}.
|
|
89
|
+
*/
|
|
90
|
+
export declare class RowMapping<T, K extends Extract<keyof T, string> = Extract<keyof T, string>> {
|
|
91
|
+
readonly name: string;
|
|
92
|
+
readonly key: K;
|
|
93
|
+
readonly prefix: string;
|
|
94
|
+
readonly parents: ParentLink<T>[];
|
|
95
|
+
private readonly columnMap;
|
|
96
|
+
private readonly overrideLookup;
|
|
97
|
+
private readonly prefixNormalized;
|
|
98
|
+
private readonly prefixLength;
|
|
99
|
+
private readonly shouldCoerce;
|
|
100
|
+
private readonly coerceFn;
|
|
101
|
+
constructor(options: RowMappingOptions<T, K>);
|
|
102
|
+
/**
|
|
103
|
+
* Registers a parent relationship that will be attached after the current row is mapped.
|
|
104
|
+
*/
|
|
105
|
+
belongsTo<P, PK extends Extract<keyof P, string>>(propertyName: Extract<keyof T, string>, parent: RowMapping<P, PK>, localKey: Extract<keyof T, string>, options?: BelongsToOptions): this;
|
|
106
|
+
/**
|
|
107
|
+
* Registers a parent relationship with an explicit local key.
|
|
108
|
+
*/
|
|
109
|
+
belongsToWithLocalKey<P, PK extends Extract<keyof P, string>>(propertyName: Extract<keyof T, string>, parent: RowMapping<P, PK>, localKey: Extract<keyof T, string>): this;
|
|
110
|
+
/**
|
|
111
|
+
* Registers an optional parent relationship with an explicit local key.
|
|
112
|
+
*/
|
|
113
|
+
belongsToOptional<P, PK extends Extract<keyof P, string>>(propertyName: Extract<keyof T, string>, parent: RowMapping<P, PK>, localKey?: Extract<keyof T, string>): this;
|
|
114
|
+
matchColumn(columnName: string): string | undefined;
|
|
115
|
+
resolveColumnName(propertyName: string): string;
|
|
116
|
+
readKeyValue(ctx: RowContext): unknown;
|
|
117
|
+
assignFields(target: Record<string, unknown>, ctx: RowContext): void;
|
|
118
|
+
private normalizeColumnValue;
|
|
119
|
+
}
|
|
120
|
+
export { RowMapping as EntityMapping };
|
|
121
|
+
/**
|
|
122
|
+
* Creates a new row mapping from the provided options.
|
|
123
|
+
*/
|
|
124
|
+
export declare function rowMapping<T, K extends Extract<keyof T, string> = Extract<keyof T, string>>(options: RowMappingOptions<T, K>): RowMapping<T, K>;
|
|
125
|
+
/**
|
|
126
|
+
* @deprecated Use {@link rowMapping} instead.
|
|
127
|
+
*/
|
|
128
|
+
export declare function entity<T, K extends Extract<keyof T, string> = Extract<keyof T, string>>(options: RowMappingOptions<T, K>): RowMapping<T, K>;
|
|
129
|
+
/**
|
|
130
|
+
* Builds a column map by prefixing each property with the provided prefix and
|
|
131
|
+
* converting property names to snake_case.
|
|
132
|
+
*/
|
|
133
|
+
export declare function columnMapFromPrefix<K extends string>(prefix: string, properties: readonly K[]): Record<K, string>;
|
|
134
|
+
/**
|
|
135
|
+
* Executes SQL via the provided executor and maps the rows using the supplied mapping.
|
|
136
|
+
*/
|
|
137
|
+
export declare class Mapper {
|
|
138
|
+
private readonly executor;
|
|
139
|
+
private readonly defaults;
|
|
140
|
+
constructor(executor: QueryExecutor, defaults?: MapperOptions | undefined);
|
|
141
|
+
query<T>(sql: string, params: QueryParams, mapping: RowMapping<T>): Promise<T[]>;
|
|
142
|
+
query<T>(sql: string, params?: QueryParams, options?: MapperOptions): Promise<T[]>;
|
|
143
|
+
queryOne<T>(sql: string, params: QueryParams, mapping: RowMapping<T>): Promise<T | undefined>;
|
|
144
|
+
queryOne<T>(sql: string, params?: QueryParams, options?: MapperOptions): Promise<T | undefined>;
|
|
145
|
+
/**
|
|
146
|
+
* Binds a structured row mapping to a reader that exposes `list` and `one`.
|
|
147
|
+
*/
|
|
148
|
+
bind<T>(mapping: RowMapping<T>): MapperReader<T>;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* This package maps rows and does not manage DB drivers.
|
|
152
|
+
* Inject a query executor rather than wiring connections inside the mapper.
|
|
153
|
+
*/
|
|
154
|
+
export declare function createMapper(executor: QueryExecutor, defaults?: MapperOptions): Mapper;
|
|
155
|
+
/**
|
|
156
|
+
* Creates a mapper using the supplied executor and user defaults.
|
|
157
|
+
* This helper is the recommended entry point when wiring an executor because
|
|
158
|
+
* it clearly signals where defaults are configured.
|
|
159
|
+
*/
|
|
160
|
+
export declare function createMapperFromExecutor(executor: QueryExecutor, defaults?: MapperOptions): Mapper;
|
|
161
|
+
/**
|
|
162
|
+
* Creates a reader-bound mapper using the supplied executor.
|
|
163
|
+
* When no overrides are provided, the app-like preset is applied so snake_case
|
|
164
|
+
* columns are normalized to camelCase keys by default.
|
|
165
|
+
*/
|
|
166
|
+
export declare function createReader(executor: QueryExecutor, options?: MapperOptions): Mapper;
|
|
167
|
+
/**
|
|
168
|
+
* Normalizes an executor returning `{ rows }` so it can be consumed by the mapper.
|
|
169
|
+
*/
|
|
170
|
+
export declare function toRowsExecutor(executorOrTarget: ((sql: string, params: QueryParams) => Promise<{
|
|
171
|
+
rows: Row[];
|
|
172
|
+
} | {
|
|
173
|
+
rows: Row[];
|
|
174
|
+
rowCount?: number;
|
|
175
|
+
} | Row[]>) | {
|
|
176
|
+
[key: string]: (...args: unknown[]) => Promise<unknown>;
|
|
177
|
+
}, methodName?: string): QueryExecutor;
|
|
178
|
+
/**
|
|
179
|
+
* Maps a pre-fetched row array into typed objects defined by a row mapping.
|
|
180
|
+
* Row values remain `unknown`, and the mapper only applies the general-purpose
|
|
181
|
+
* coercion rules declared in `coerceColumnValue`.
|
|
182
|
+
*/
|
|
183
|
+
export declare function mapRows<T>(rows: Row[], mapping: RowMapping<T>): T[];
|
|
184
|
+
/**
|
|
185
|
+
* Maps pre-fetched rows into typed DTOs using the simple map preset, honoring key transforms, type hints, and optional coercion settings.
|
|
186
|
+
*
|
|
187
|
+
* @template T Target DTO shape.
|
|
188
|
+
* @param rows Rows produced by the SQL executor.
|
|
189
|
+
* @param options Optional overrides that control key normalization, coercion, and type hints.
|
|
190
|
+
* @returns An array of `T` instances synthesized from `rows`.
|
|
191
|
+
*/
|
|
192
|
+
export declare function mapSimpleRows<T>(rows: Row[], options?: MapperOptions): T[];
|