@atscript/moost-db 0.1.39 → 0.1.41

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.
@@ -0,0 +1,204 @@
1
+ import * as _uniqu_url0 from "@uniqu/url";
2
+ import * as _atscript_db0 from "@atscript/db";
3
+ import { AtscriptDbReadable, AtscriptDbTable, FilterExpr, Uniquery, UniqueryControls } from "@atscript/db";
4
+ import * as _atscript_typescript_utils0 from "@atscript/typescript/utils";
5
+ import { TAtscriptAnnotatedType, TAtscriptDataType, Validator } from "@atscript/typescript/utils";
6
+ import { HttpError } from "@moostjs/event-http";
7
+ import * as moost from "moost";
8
+ import { Moost, TConsoleBase } from "moost";
9
+
10
+ //#region src/as-db-readable.controller.d.ts
11
+ /**
12
+ * Read-only database controller for Moost that works with any `AtscriptDbReadable`
13
+ * (tables or views). Provides query, pages, getOne, and meta endpoints.
14
+ *
15
+ * For write operations (insert, replace, update, delete), use {@link AsDbController}.
16
+ * For views, use {@link AsDbViewController}.
17
+ */
18
+ declare class AsDbReadableController<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType, DataType = TAtscriptDataType<T>> {
19
+ /** Reference to the underlying readable (table or view). */
20
+ protected readable: AtscriptDbReadable<T>;
21
+ /** Application-scoped logger. */
22
+ protected logger: TConsoleBase;
23
+ /** Cached serialized type definition (static, computed once). */
24
+ private _serializedType;
25
+ /** Cached search index list (static, computed once). */
26
+ private _searchIndexes;
27
+ constructor(readable: AtscriptDbReadable<T>, app: Moost);
28
+ /**
29
+ * One-time initialization hook. Override to seed data, register watchers, etc.
30
+ */
31
+ protected init(): void | Promise<void>;
32
+ private _queryControlsValidator?;
33
+ private _pagesControlsValidator?;
34
+ private _getOneControlsValidator?;
35
+ protected get queryControlsValidator(): Validator<any, unknown>;
36
+ protected get pagesControlsValidator(): Validator<any, unknown>;
37
+ protected get getOneControlsValidator(): Validator<any, unknown>;
38
+ protected validateControls(controls: Record<string, unknown>, type: "query" | "pages" | "getOne"): string | undefined;
39
+ protected validateInsights(insights: Map<string, unknown>): string | undefined;
40
+ protected validateParsed(parsed: Uniquery, type: "query" | "pages" | "getOne"): HttpError | undefined;
41
+ /**
42
+ * Compute an embedding vector from a search term.
43
+ * Override in subclass to integrate with your embedding provider (OpenAI, etc.).
44
+ * Called when `$vector` is present in query controls.
45
+ */
46
+ protected computeEmbedding(_search: string, _fieldName?: string): Promise<number[]>;
47
+ /**
48
+ * Transform filter before querying. Override to add tenant filtering, etc.
49
+ */
50
+ protected transformFilter(filter: FilterExpr): FilterExpr;
51
+ /**
52
+ * Transform projection before querying.
53
+ */
54
+ protected transformProjection(projection?: UniqueryControls["$select"]): UniqueryControls["$select"] | undefined;
55
+ protected parseQueryString(url: string): _uniqu_url0.UrlQuery;
56
+ protected returnOne(result: Promise<DataType | null>): Promise<DataType | HttpError>;
57
+ /**
58
+ * Extracts a composite identifier object from query params.
59
+ * Tries composite primary key first, then compound unique indexes.
60
+ */
61
+ protected extractCompositeId(query: Record<string, string>): Record<string, unknown> | HttpError;
62
+ /**
63
+ * **GET /query** — returns an array of records or a count.
64
+ */
65
+ query(url: string): Promise<DataType[] | number | HttpError>;
66
+ /**
67
+ * **GET /pages** — returns paginated records with metadata.
68
+ */
69
+ pages(url: string): Promise<{
70
+ data: DataType[];
71
+ page: number;
72
+ itemsPerPage: number;
73
+ pages: number;
74
+ count: number;
75
+ } | HttpError>;
76
+ /**
77
+ * **GET /one/:id** — retrieves a single record by ID or unique property.
78
+ */
79
+ getOne(id: string, url: string): Promise<DataType | HttpError>;
80
+ /**
81
+ * **GET /one?field1=val1&field2=val2** — retrieves a single record by composite key
82
+ * (composite primary key or compound unique index).
83
+ */
84
+ getOneComposite(query: Record<string, string>, url: string): Promise<DataType | HttpError>;
85
+ /**
86
+ * **GET /meta** — returns table/view metadata for UI.
87
+ */
88
+ meta(): {
89
+ searchable: boolean;
90
+ vectorSearchable: boolean;
91
+ searchIndexes: _atscript_db0.TSearchIndexInfo[];
92
+ type: _atscript_typescript_utils0.TSerializedAnnotatedType;
93
+ };
94
+ }
95
+ //#endregion
96
+ //#region src/as-db.controller.d.ts
97
+ /**
98
+ * Full CRUD database controller for Moost that works with any `AtscriptDbTable` +
99
+ * `BaseDbAdapter`. Extends {@link AsDbReadableController} with write operations.
100
+ *
101
+ * Subclass and provide the table via DI:
102
+ * ```ts
103
+ * ‎@TableController(usersTable)
104
+ * export class UsersController extends AsDbController<typeof UserModel> {}
105
+ * ```
106
+ */
107
+ declare class AsDbController<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType, DataType = TAtscriptDataType<T>> extends AsDbReadableController<T, DataType> {
108
+ /** Reference to the underlying table (typed for write access). */
109
+ protected get table(): AtscriptDbTable<T>;
110
+ constructor(table: AtscriptDbTable<T>, app: Moost);
111
+ /**
112
+ * Intercepts write operations. Return `undefined` to abort.
113
+ */
114
+ protected onWrite(action: "insert" | "insertMany" | "replace" | "replaceMany" | "update" | "updateMany", data: unknown): unknown;
115
+ /**
116
+ * Intercepts delete operations. Return `undefined` to abort.
117
+ */
118
+ protected onRemove(id: unknown): unknown;
119
+ /**
120
+ * **POST /** — inserts one or many records.
121
+ */
122
+ insert(payload: unknown): Promise<unknown>;
123
+ /**
124
+ * **PUT /** — fully replaces one or many records matched by primary key.
125
+ */
126
+ replace(payload: unknown): Promise<unknown>;
127
+ /**
128
+ * **PATCH /** — partially updates one or many records matched by primary key.
129
+ */
130
+ update(payload: unknown): Promise<unknown>;
131
+ /**
132
+ * **DELETE /:id** — removes a single record by primary key.
133
+ */
134
+ remove(id: string): Promise<unknown>;
135
+ /**
136
+ * **DELETE /?field1=val1&field2=val2** — removes a record by composite key
137
+ * (composite primary key or compound unique index).
138
+ */
139
+ removeComposite(query: Record<string, string>): Promise<unknown>;
140
+ }
141
+ //#endregion
142
+ //#region src/decorators.d.ts
143
+ /**
144
+ * DI token under which the {@link AtscriptDbReadable} instance
145
+ * is exposed to the readable controller's constructor via `@Inject`.
146
+ */
147
+ declare const READABLE_DEF = "__atscript_db_readable_def";
148
+ /**
149
+ * DI token under which the {@link AtscriptDbTable} instance
150
+ * is exposed to the controller's constructor via `@Inject`.
151
+ * Points to the same token as READABLE_DEF for backward compatibility.
152
+ */
153
+ declare const TABLE_DEF = "__atscript_db_readable_def";
154
+ /**
155
+ * Combines the boilerplate needed to turn an {@link AsDbController}
156
+ * subclass into a fully wired HTTP controller for a given `@db.table` model.
157
+ *
158
+ * Internally applies three decorators:
159
+ * 1. **Provide** — registers the table instance under {@link TABLE_DEF}.
160
+ * 2. **Controller** — registers the class as a Moost HTTP controller
161
+ * with an optional route prefix. Defaults to `table.tableName`.
162
+ * 3. **Inherit** — copies metadata (routes, guards, etc.) from the
163
+ * parent class so they stay active in the derived controller.
164
+ *
165
+ * @param table The {@link AtscriptDbTable} instance for this controller.
166
+ * @param prefix Optional route prefix. Defaults to `table.tableName`.
167
+ *
168
+ * @example
169
+ * ```ts
170
+ * ‎@TableController(usersTable)
171
+ * export class UsersController extends AsDbController<typeof UserModel> {}
172
+ * ```
173
+ */
174
+ declare const TableController: (table: AtscriptDbTable, prefix?: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
175
+ /**
176
+ * Combines the boilerplate needed to turn an {@link AsDbReadableController}
177
+ * subclass into a fully wired HTTP controller for a given `@db.view` or `@db.table` model.
178
+ *
179
+ * @param readable The {@link AtscriptDbReadable} instance (table or view).
180
+ * @param prefix Optional route prefix. Defaults to `readable.tableName`.
181
+ *
182
+ * @example
183
+ * ```ts
184
+ * ‎@ReadableController(activeTasksView)
185
+ * export class ActiveTasksController extends AsDbReadableController<typeof ActiveTasks> {}
186
+ * ```
187
+ */
188
+ declare const ReadableController: (readable: AtscriptDbReadable, prefix?: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
189
+ /**
190
+ * Alias for {@link ReadableController} — use with view-backed controllers.
191
+ *
192
+ * @example
193
+ * ```ts
194
+ * ‎@ViewController(activeTasksView)
195
+ * export class ActiveTasksController extends AsDbReadableController<typeof ActiveTasks> {}
196
+ * ```
197
+ */
198
+ declare const ViewController: (readable: AtscriptDbReadable, prefix?: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
199
+ //#endregion
200
+ //#region src/validation-interceptor.d.ts
201
+ declare const validationErrorTransform: () => moost.TInterceptorDef;
202
+ declare const UseValidationErrorTransform: () => ClassDecorator & MethodDecorator;
203
+ //#endregion
204
+ export { AsDbController, AsDbReadableController, READABLE_DEF, ReadableController, TABLE_DEF, TableController, UseValidationErrorTransform, ViewController, validationErrorTransform };
@@ -0,0 +1,204 @@
1
+ import * as _atscript_typescript_utils0 from "@atscript/typescript/utils";
2
+ import { TAtscriptAnnotatedType, TAtscriptDataType, Validator } from "@atscript/typescript/utils";
3
+ import { HttpError } from "@moostjs/event-http";
4
+ import * as moost from "moost";
5
+ import { Moost, TConsoleBase } from "moost";
6
+ import * as _uniqu_url0 from "@uniqu/url";
7
+ import * as _atscript_db0 from "@atscript/db";
8
+ import { AtscriptDbReadable, AtscriptDbTable, FilterExpr, Uniquery, UniqueryControls } from "@atscript/db";
9
+
10
+ //#region src/as-db-readable.controller.d.ts
11
+ /**
12
+ * Read-only database controller for Moost that works with any `AtscriptDbReadable`
13
+ * (tables or views). Provides query, pages, getOne, and meta endpoints.
14
+ *
15
+ * For write operations (insert, replace, update, delete), use {@link AsDbController}.
16
+ * For views, use {@link AsDbViewController}.
17
+ */
18
+ declare class AsDbReadableController<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType, DataType = TAtscriptDataType<T>> {
19
+ /** Reference to the underlying readable (table or view). */
20
+ protected readable: AtscriptDbReadable<T>;
21
+ /** Application-scoped logger. */
22
+ protected logger: TConsoleBase;
23
+ /** Cached serialized type definition (static, computed once). */
24
+ private _serializedType;
25
+ /** Cached search index list (static, computed once). */
26
+ private _searchIndexes;
27
+ constructor(readable: AtscriptDbReadable<T>, app: Moost);
28
+ /**
29
+ * One-time initialization hook. Override to seed data, register watchers, etc.
30
+ */
31
+ protected init(): void | Promise<void>;
32
+ private _queryControlsValidator?;
33
+ private _pagesControlsValidator?;
34
+ private _getOneControlsValidator?;
35
+ protected get queryControlsValidator(): Validator<any, unknown>;
36
+ protected get pagesControlsValidator(): Validator<any, unknown>;
37
+ protected get getOneControlsValidator(): Validator<any, unknown>;
38
+ protected validateControls(controls: Record<string, unknown>, type: "query" | "pages" | "getOne"): string | undefined;
39
+ protected validateInsights(insights: Map<string, unknown>): string | undefined;
40
+ protected validateParsed(parsed: Uniquery, type: "query" | "pages" | "getOne"): HttpError | undefined;
41
+ /**
42
+ * Compute an embedding vector from a search term.
43
+ * Override in subclass to integrate with your embedding provider (OpenAI, etc.).
44
+ * Called when `$vector` is present in query controls.
45
+ */
46
+ protected computeEmbedding(_search: string, _fieldName?: string): Promise<number[]>;
47
+ /**
48
+ * Transform filter before querying. Override to add tenant filtering, etc.
49
+ */
50
+ protected transformFilter(filter: FilterExpr): FilterExpr;
51
+ /**
52
+ * Transform projection before querying.
53
+ */
54
+ protected transformProjection(projection?: UniqueryControls["$select"]): UniqueryControls["$select"] | undefined;
55
+ protected parseQueryString(url: string): _uniqu_url0.UrlQuery;
56
+ protected returnOne(result: Promise<DataType | null>): Promise<DataType | HttpError>;
57
+ /**
58
+ * Extracts a composite identifier object from query params.
59
+ * Tries composite primary key first, then compound unique indexes.
60
+ */
61
+ protected extractCompositeId(query: Record<string, string>): Record<string, unknown> | HttpError;
62
+ /**
63
+ * **GET /query** — returns an array of records or a count.
64
+ */
65
+ query(url: string): Promise<DataType[] | number | HttpError>;
66
+ /**
67
+ * **GET /pages** — returns paginated records with metadata.
68
+ */
69
+ pages(url: string): Promise<{
70
+ data: DataType[];
71
+ page: number;
72
+ itemsPerPage: number;
73
+ pages: number;
74
+ count: number;
75
+ } | HttpError>;
76
+ /**
77
+ * **GET /one/:id** — retrieves a single record by ID or unique property.
78
+ */
79
+ getOne(id: string, url: string): Promise<DataType | HttpError>;
80
+ /**
81
+ * **GET /one?field1=val1&field2=val2** — retrieves a single record by composite key
82
+ * (composite primary key or compound unique index).
83
+ */
84
+ getOneComposite(query: Record<string, string>, url: string): Promise<DataType | HttpError>;
85
+ /**
86
+ * **GET /meta** — returns table/view metadata for UI.
87
+ */
88
+ meta(): {
89
+ searchable: boolean;
90
+ vectorSearchable: boolean;
91
+ searchIndexes: _atscript_db0.TSearchIndexInfo[];
92
+ type: _atscript_typescript_utils0.TSerializedAnnotatedType;
93
+ };
94
+ }
95
+ //#endregion
96
+ //#region src/as-db.controller.d.ts
97
+ /**
98
+ * Full CRUD database controller for Moost that works with any `AtscriptDbTable` +
99
+ * `BaseDbAdapter`. Extends {@link AsDbReadableController} with write operations.
100
+ *
101
+ * Subclass and provide the table via DI:
102
+ * ```ts
103
+ * ‎@TableController(usersTable)
104
+ * export class UsersController extends AsDbController<typeof UserModel> {}
105
+ * ```
106
+ */
107
+ declare class AsDbController<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedType, DataType = TAtscriptDataType<T>> extends AsDbReadableController<T, DataType> {
108
+ /** Reference to the underlying table (typed for write access). */
109
+ protected get table(): AtscriptDbTable<T>;
110
+ constructor(table: AtscriptDbTable<T>, app: Moost);
111
+ /**
112
+ * Intercepts write operations. Return `undefined` to abort.
113
+ */
114
+ protected onWrite(action: "insert" | "insertMany" | "replace" | "replaceMany" | "update" | "updateMany", data: unknown): unknown;
115
+ /**
116
+ * Intercepts delete operations. Return `undefined` to abort.
117
+ */
118
+ protected onRemove(id: unknown): unknown;
119
+ /**
120
+ * **POST /** — inserts one or many records.
121
+ */
122
+ insert(payload: unknown): Promise<unknown>;
123
+ /**
124
+ * **PUT /** — fully replaces one or many records matched by primary key.
125
+ */
126
+ replace(payload: unknown): Promise<unknown>;
127
+ /**
128
+ * **PATCH /** — partially updates one or many records matched by primary key.
129
+ */
130
+ update(payload: unknown): Promise<unknown>;
131
+ /**
132
+ * **DELETE /:id** — removes a single record by primary key.
133
+ */
134
+ remove(id: string): Promise<unknown>;
135
+ /**
136
+ * **DELETE /?field1=val1&field2=val2** — removes a record by composite key
137
+ * (composite primary key or compound unique index).
138
+ */
139
+ removeComposite(query: Record<string, string>): Promise<unknown>;
140
+ }
141
+ //#endregion
142
+ //#region src/decorators.d.ts
143
+ /**
144
+ * DI token under which the {@link AtscriptDbReadable} instance
145
+ * is exposed to the readable controller's constructor via `@Inject`.
146
+ */
147
+ declare const READABLE_DEF = "__atscript_db_readable_def";
148
+ /**
149
+ * DI token under which the {@link AtscriptDbTable} instance
150
+ * is exposed to the controller's constructor via `@Inject`.
151
+ * Points to the same token as READABLE_DEF for backward compatibility.
152
+ */
153
+ declare const TABLE_DEF = "__atscript_db_readable_def";
154
+ /**
155
+ * Combines the boilerplate needed to turn an {@link AsDbController}
156
+ * subclass into a fully wired HTTP controller for a given `@db.table` model.
157
+ *
158
+ * Internally applies three decorators:
159
+ * 1. **Provide** — registers the table instance under {@link TABLE_DEF}.
160
+ * 2. **Controller** — registers the class as a Moost HTTP controller
161
+ * with an optional route prefix. Defaults to `table.tableName`.
162
+ * 3. **Inherit** — copies metadata (routes, guards, etc.) from the
163
+ * parent class so they stay active in the derived controller.
164
+ *
165
+ * @param table The {@link AtscriptDbTable} instance for this controller.
166
+ * @param prefix Optional route prefix. Defaults to `table.tableName`.
167
+ *
168
+ * @example
169
+ * ```ts
170
+ * ‎@TableController(usersTable)
171
+ * export class UsersController extends AsDbController<typeof UserModel> {}
172
+ * ```
173
+ */
174
+ declare const TableController: (table: AtscriptDbTable, prefix?: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
175
+ /**
176
+ * Combines the boilerplate needed to turn an {@link AsDbReadableController}
177
+ * subclass into a fully wired HTTP controller for a given `@db.view` or `@db.table` model.
178
+ *
179
+ * @param readable The {@link AtscriptDbReadable} instance (table or view).
180
+ * @param prefix Optional route prefix. Defaults to `readable.tableName`.
181
+ *
182
+ * @example
183
+ * ```ts
184
+ * ‎@ReadableController(activeTasksView)
185
+ * export class ActiveTasksController extends AsDbReadableController<typeof ActiveTasks> {}
186
+ * ```
187
+ */
188
+ declare const ReadableController: (readable: AtscriptDbReadable, prefix?: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
189
+ /**
190
+ * Alias for {@link ReadableController} — use with view-backed controllers.
191
+ *
192
+ * @example
193
+ * ```ts
194
+ * ‎@ViewController(activeTasksView)
195
+ * export class ActiveTasksController extends AsDbReadableController<typeof ActiveTasks> {}
196
+ * ```
197
+ */
198
+ declare const ViewController: (readable: AtscriptDbReadable, prefix?: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
199
+ //#endregion
200
+ //#region src/validation-interceptor.d.ts
201
+ declare const validationErrorTransform: () => moost.TInterceptorDef;
202
+ declare const UseValidationErrorTransform: () => ClassDecorator & MethodDecorator;
203
+ //#endregion
204
+ export { AsDbController, AsDbReadableController, READABLE_DEF, ReadableController, TABLE_DEF, TableController, UseValidationErrorTransform, ViewController, validationErrorTransform };