@atscript/moost-db 0.1.45 → 0.1.47

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/index.cjs CHANGED
@@ -36,7 +36,10 @@ const TABLE_DEF = READABLE_DEF;
36
36
  * export class UsersController extends AsDbController<typeof UserModel> {}
37
37
  * ```
38
38
  */
39
- const TableController = (table, prefix) => (0, moost.ApplyDecorators)((0, moost.Provide)(TABLE_DEF, () => table), (0, moost.Controller)(prefix || table.tableName), (0, moost.Inherit)());
39
+ const TableController = (table, prefix) => {
40
+ const resolvedPath = prefix || table.type.metadata.get("db.http.path");
41
+ return (0, moost.ApplyDecorators)((0, moost.Provide)(TABLE_DEF, () => table), (0, moost.Controller)(resolvedPath || table.tableName), (0, moost.Inherit)());
42
+ };
40
43
  /**
41
44
  * Combines the boilerplate needed to turn an {@link AsDbReadableController}
42
45
  * subclass into a fully wired HTTP controller for a given `@db.view` or `@db.table` model.
@@ -50,7 +53,10 @@ const TableController = (table, prefix) => (0, moost.ApplyDecorators)((0, moost.
50
53
  * export class ActiveTasksController extends AsDbReadableController<typeof ActiveTasks> {}
51
54
  * ```
52
55
  */
53
- const ReadableController = (readable, prefix) => (0, moost.ApplyDecorators)((0, moost.Provide)(READABLE_DEF, () => readable), (0, moost.Controller)(prefix || readable.tableName), (0, moost.Inherit)());
56
+ const ReadableController = (readable, prefix) => {
57
+ const resolvedPath = prefix || readable.type.metadata.get("db.http.path");
58
+ return (0, moost.ApplyDecorators)((0, moost.Provide)(READABLE_DEF, () => readable), (0, moost.Controller)(resolvedPath || readable.tableName), (0, moost.Inherit)());
59
+ };
54
60
  /**
55
61
  * Alias for {@link ReadableController} — use with view-backed controllers.
56
62
  *
@@ -199,18 +205,18 @@ let AsDbReadableController = class AsDbReadableController {
199
205
  readable;
200
206
  /** Application-scoped logger. */
201
207
  logger;
202
- /** Cached serialized type definition (static, computed once). */
208
+ /** Cached serialized type definition (lazy, computed on first access). */
203
209
  _serializedType;
204
- /** Cached search index list (static, computed once). */
205
- _searchIndexes;
210
+ /** Moost application instance. */
211
+ app;
206
212
  /** Cached full meta response (computed lazily on first meta() call). */
207
213
  _metaResponse;
208
214
  constructor(readable, app) {
209
215
  this.readable = readable;
210
- this._serializedType = (0, _atscript_typescript_utils.serializeAnnotatedType)(readable.type, this.getSerializeOptions());
211
- this._searchIndexes = readable.getSearchIndexes();
216
+ this.app = app;
212
217
  this.logger = app.getLogger(`db [${readable.tableName}]`);
213
218
  this.logger.info(`Initializing ${readable.isView ? "view" : "table"} controller`);
219
+ this._resolveHttpPath();
214
220
  try {
215
221
  const p = this.init();
216
222
  if (p instanceof Promise) p.catch((error) => {
@@ -221,6 +227,16 @@ let AsDbReadableController = class AsDbReadableController {
221
227
  throw error;
222
228
  }
223
229
  }
230
+ /** Sets @db.http.path on the type metadata from the controller's computed prefix. */
231
+ _resolveHttpPath() {
232
+ const overview = this.app.getControllersOverview?.()?.find((o) => o.type === this.constructor);
233
+ if (overview?.computedPrefix) this.readable.type.metadata.set("db.http.path", overview.computedPrefix);
234
+ }
235
+ /** Lazily serializes the type (after all controllers have set their @db.http.path). */
236
+ getSerializedType() {
237
+ if (!this._serializedType) this._serializedType = (0, _atscript_typescript_utils.serializeAnnotatedType)(this.readable.type, this.getSerializeOptions());
238
+ return this._serializedType;
239
+ }
224
240
  /**
225
241
  * One-time initialization hook. Override to seed data, register watchers, etc.
226
242
  */
@@ -232,21 +248,24 @@ let AsDbReadableController = class AsDbReadableController {
232
248
  * Override in subclass to customise what annotations are exposed to clients.
233
249
  */
234
250
  getSerializeOptions() {
235
- return { processAnnotation: ({ key, value }) => {
236
- if (key.startsWith("meta.") || key.startsWith("expect.") || key.startsWith("db.rel.")) return {
237
- key,
238
- value
239
- };
240
- if (key === "db.json" || key === "db.patch.strategy" || key.startsWith("db.default")) return {
241
- key,
242
- value
243
- };
244
- if (key.startsWith("db.")) return;
245
- return {
246
- key,
247
- value
248
- };
249
- } };
251
+ return {
252
+ refDepth: 1,
253
+ processAnnotation: ({ key, value }) => {
254
+ if (key.startsWith("meta.") || key.startsWith("expect.") || key.startsWith("db.rel.")) return {
255
+ key,
256
+ value
257
+ };
258
+ if (key === "db.json" || key === "db.patch.strategy" || key.startsWith("db.default") || key === "db.http.path") return {
259
+ key,
260
+ value
261
+ };
262
+ if (key.startsWith("db.")) return;
263
+ return {
264
+ key,
265
+ value
266
+ };
267
+ }
268
+ };
250
269
  }
251
270
  /**
252
271
  * Whether this controller is read-only (no write endpoints).
@@ -507,12 +526,12 @@ let AsDbReadableController = class AsDbReadableController {
507
526
  this._metaResponse = {
508
527
  searchable: this.readable.isSearchable(),
509
528
  vectorSearchable: this.readable.isVectorSearchable(),
510
- searchIndexes: this._searchIndexes,
529
+ searchIndexes: this.readable.getSearchIndexes(),
511
530
  primaryKeys: [...this.readable.primaryKeys],
512
531
  readOnly: this._isReadOnly(),
513
532
  relations,
514
533
  fields,
515
- type: this._serializedType
534
+ type: this.getSerializedType()
516
535
  };
517
536
  return this._metaResponse;
518
537
  }
package/dist/index.d.cts CHANGED
@@ -1,5 +1,6 @@
1
- import * as _uniqu_url0 from "@uniqu/url";
1
+ import * as _atscript_typescript_utils0 from "@atscript/typescript/utils";
2
2
  import { TAtscriptAnnotatedType, TAtscriptDataType, TSerializeOptions, Validator, serializeAnnotatedType } from "@atscript/typescript/utils";
3
+ import * as _uniqu_url0 from "@uniqu/url";
3
4
  import { AtscriptDbReadable, AtscriptDbTable, FilterExpr, Uniquery, UniqueryControls } from "@atscript/db";
4
5
  import { HttpError } from "@moostjs/event-http";
5
6
  import * as moost from "moost";
@@ -18,13 +19,17 @@ declare class AsDbReadableController<T extends TAtscriptAnnotatedType = TAtscrip
18
19
  protected readable: AtscriptDbReadable<T>;
19
20
  /** Application-scoped logger. */
20
21
  protected logger: TConsoleBase;
21
- /** Cached serialized type definition (static, computed once). */
22
- private _serializedType;
23
- /** Cached search index list (static, computed once). */
24
- private _searchIndexes;
22
+ /** Cached serialized type definition (lazy, computed on first access). */
23
+ private _serializedType?;
24
+ /** Moost application instance. */
25
+ protected app: Moost;
25
26
  /** Cached full meta response (computed lazily on first meta() call). */
26
27
  private _metaResponse?;
27
28
  constructor(readable: AtscriptDbReadable<T>, app: Moost);
29
+ /** Sets @db.http.path on the type metadata from the controller's computed prefix. */
30
+ private _resolveHttpPath;
31
+ /** Lazily serializes the type (after all controllers have set their @db.http.path). */
32
+ protected getSerializedType(): _atscript_typescript_utils0.TSerializedAnnotatedType;
28
33
  /**
29
34
  * One-time initialization hook. Override to seed data, register watchers, etc.
30
35
  */
package/dist/index.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ import * as _atscript_typescript_utils0 from "@atscript/typescript/utils";
1
2
  import { TAtscriptAnnotatedType, TAtscriptDataType, TSerializeOptions, Validator, serializeAnnotatedType } from "@atscript/typescript/utils";
2
3
  import { HttpError } from "@moostjs/event-http";
3
4
  import * as moost from "moost";
@@ -18,13 +19,17 @@ declare class AsDbReadableController<T extends TAtscriptAnnotatedType = TAtscrip
18
19
  protected readable: AtscriptDbReadable<T>;
19
20
  /** Application-scoped logger. */
20
21
  protected logger: TConsoleBase;
21
- /** Cached serialized type definition (static, computed once). */
22
- private _serializedType;
23
- /** Cached search index list (static, computed once). */
24
- private _searchIndexes;
22
+ /** Cached serialized type definition (lazy, computed on first access). */
23
+ private _serializedType?;
24
+ /** Moost application instance. */
25
+ protected app: Moost;
25
26
  /** Cached full meta response (computed lazily on first meta() call). */
26
27
  private _metaResponse?;
27
28
  constructor(readable: AtscriptDbReadable<T>, app: Moost);
29
+ /** Sets @db.http.path on the type metadata from the controller's computed prefix. */
30
+ private _resolveHttpPath;
31
+ /** Lazily serializes the type (after all controllers have set their @db.http.path). */
32
+ protected getSerializedType(): _atscript_typescript_utils0.TSerializedAnnotatedType;
28
33
  /**
29
34
  * One-time initialization hook. Override to seed data, register watchers, etc.
30
35
  */
package/dist/index.mjs CHANGED
@@ -35,7 +35,10 @@ const TABLE_DEF = READABLE_DEF;
35
35
  * export class UsersController extends AsDbController<typeof UserModel> {}
36
36
  * ```
37
37
  */
38
- const TableController = (table, prefix) => ApplyDecorators(Provide(TABLE_DEF, () => table), Controller(prefix || table.tableName), Inherit());
38
+ const TableController = (table, prefix) => {
39
+ const resolvedPath = prefix || table.type.metadata.get("db.http.path");
40
+ return ApplyDecorators(Provide(TABLE_DEF, () => table), Controller(resolvedPath || table.tableName), Inherit());
41
+ };
39
42
  /**
40
43
  * Combines the boilerplate needed to turn an {@link AsDbReadableController}
41
44
  * subclass into a fully wired HTTP controller for a given `@db.view` or `@db.table` model.
@@ -49,7 +52,10 @@ const TableController = (table, prefix) => ApplyDecorators(Provide(TABLE_DEF, ()
49
52
  * export class ActiveTasksController extends AsDbReadableController<typeof ActiveTasks> {}
50
53
  * ```
51
54
  */
52
- const ReadableController = (readable, prefix) => ApplyDecorators(Provide(READABLE_DEF, () => readable), Controller(prefix || readable.tableName), Inherit());
55
+ const ReadableController = (readable, prefix) => {
56
+ const resolvedPath = prefix || readable.type.metadata.get("db.http.path");
57
+ return ApplyDecorators(Provide(READABLE_DEF, () => readable), Controller(resolvedPath || readable.tableName), Inherit());
58
+ };
53
59
  /**
54
60
  * Alias for {@link ReadableController} — use with view-backed controllers.
55
61
  *
@@ -198,18 +204,18 @@ let AsDbReadableController = class AsDbReadableController {
198
204
  readable;
199
205
  /** Application-scoped logger. */
200
206
  logger;
201
- /** Cached serialized type definition (static, computed once). */
207
+ /** Cached serialized type definition (lazy, computed on first access). */
202
208
  _serializedType;
203
- /** Cached search index list (static, computed once). */
204
- _searchIndexes;
209
+ /** Moost application instance. */
210
+ app;
205
211
  /** Cached full meta response (computed lazily on first meta() call). */
206
212
  _metaResponse;
207
213
  constructor(readable, app) {
208
214
  this.readable = readable;
209
- this._serializedType = serializeAnnotatedType(readable.type, this.getSerializeOptions());
210
- this._searchIndexes = readable.getSearchIndexes();
215
+ this.app = app;
211
216
  this.logger = app.getLogger(`db [${readable.tableName}]`);
212
217
  this.logger.info(`Initializing ${readable.isView ? "view" : "table"} controller`);
218
+ this._resolveHttpPath();
213
219
  try {
214
220
  const p = this.init();
215
221
  if (p instanceof Promise) p.catch((error) => {
@@ -220,6 +226,16 @@ let AsDbReadableController = class AsDbReadableController {
220
226
  throw error;
221
227
  }
222
228
  }
229
+ /** Sets @db.http.path on the type metadata from the controller's computed prefix. */
230
+ _resolveHttpPath() {
231
+ const overview = this.app.getControllersOverview?.()?.find((o) => o.type === this.constructor);
232
+ if (overview?.computedPrefix) this.readable.type.metadata.set("db.http.path", overview.computedPrefix);
233
+ }
234
+ /** Lazily serializes the type (after all controllers have set their @db.http.path). */
235
+ getSerializedType() {
236
+ if (!this._serializedType) this._serializedType = serializeAnnotatedType(this.readable.type, this.getSerializeOptions());
237
+ return this._serializedType;
238
+ }
223
239
  /**
224
240
  * One-time initialization hook. Override to seed data, register watchers, etc.
225
241
  */
@@ -231,21 +247,24 @@ let AsDbReadableController = class AsDbReadableController {
231
247
  * Override in subclass to customise what annotations are exposed to clients.
232
248
  */
233
249
  getSerializeOptions() {
234
- return { processAnnotation: ({ key, value }) => {
235
- if (key.startsWith("meta.") || key.startsWith("expect.") || key.startsWith("db.rel.")) return {
236
- key,
237
- value
238
- };
239
- if (key === "db.json" || key === "db.patch.strategy" || key.startsWith("db.default")) return {
240
- key,
241
- value
242
- };
243
- if (key.startsWith("db.")) return;
244
- return {
245
- key,
246
- value
247
- };
248
- } };
250
+ return {
251
+ refDepth: 1,
252
+ processAnnotation: ({ key, value }) => {
253
+ if (key.startsWith("meta.") || key.startsWith("expect.") || key.startsWith("db.rel.")) return {
254
+ key,
255
+ value
256
+ };
257
+ if (key === "db.json" || key === "db.patch.strategy" || key.startsWith("db.default") || key === "db.http.path") return {
258
+ key,
259
+ value
260
+ };
261
+ if (key.startsWith("db.")) return;
262
+ return {
263
+ key,
264
+ value
265
+ };
266
+ }
267
+ };
249
268
  }
250
269
  /**
251
270
  * Whether this controller is read-only (no write endpoints).
@@ -506,12 +525,12 @@ let AsDbReadableController = class AsDbReadableController {
506
525
  this._metaResponse = {
507
526
  searchable: this.readable.isSearchable(),
508
527
  vectorSearchable: this.readable.isVectorSearchable(),
509
- searchIndexes: this._searchIndexes,
528
+ searchIndexes: this.readable.getSearchIndexes(),
510
529
  primaryKeys: [...this.readable.primaryKeys],
511
530
  readOnly: this._isReadOnly(),
512
531
  relations,
513
532
  fields,
514
- type: this._serializedType
533
+ type: this.getSerializedType()
515
534
  };
516
535
  return this._metaResponse;
517
536
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/moost-db",
3
- "version": "0.1.45",
3
+ "version": "0.1.47",
4
4
  "description": "Generic database controller for Moost with Atscript.",
5
5
  "keywords": [
6
6
  "annotations",
@@ -41,19 +41,19 @@
41
41
  "@uniqu/url": "^0.1.2"
42
42
  },
43
43
  "devDependencies": {
44
- "@atscript/core": "^0.1.44",
45
- "@atscript/typescript": "^0.1.44",
46
- "@moostjs/event-http": "^0.6.4",
44
+ "@atscript/core": "^0.1.47",
45
+ "@atscript/typescript": "^0.1.47",
46
+ "@moostjs/event-http": "^0.6.6",
47
47
  "@uniqu/core": "^0.1.2",
48
- "moost": "^0.6.4",
49
- "unplugin-atscript": "^0.1.44"
48
+ "moost": "^0.6.6",
49
+ "unplugin-atscript": "^0.1.47"
50
50
  },
51
51
  "peerDependencies": {
52
- "@atscript/typescript": "^0.1.44",
53
- "@moostjs/event-http": "^0.6.4",
52
+ "@atscript/typescript": "^0.1.47",
53
+ "@moostjs/event-http": "^0.6.6",
54
54
  "@uniqu/core": "^0.1.2",
55
- "moost": "^0.6.4",
56
- "@atscript/db": "^0.1.45"
55
+ "moost": "^0.6.6",
56
+ "@atscript/db": "^0.1.47"
57
57
  },
58
58
  "scripts": {
59
59
  "postinstall": "asc -f dts",