@arkstack/common 0.17.3 → 0.17.4

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.
@@ -72,6 +72,16 @@ declare const perPage: (query: {
72
72
  */
73
73
  declare function getModel<TName extends ModelName>(modelName: TName): Promise<ModelRegistry[TName]>;
74
74
  declare function getModel<TModel extends AbstractModelConstructor = ModelConstructor>(modelName: string): Promise<TModel>;
75
+ /**
76
+ * Synchronously import an application model by name.
77
+ *
78
+ * Apps can augment `ModelRegistry` to make `getModel('User')` return `typeof User`.
79
+ * Without a registry entry, pass the class type explicitly: `getModel<typeof User>('User')`.
80
+ *
81
+ * @param modelName
82
+ */
83
+ declare function getModelSync<TName extends ModelName>(modelName: TName): ModelRegistry[TName];
84
+ declare function getModelSync<TModel extends AbstractModelConstructor = ModelConstructor>(modelName: string): TModel;
75
85
  declare const initializeGlobalContext: ({
76
86
  Request,
77
87
  Response,
@@ -108,4 +118,4 @@ declare const abortIf: <T>(boolean: T, message?: string, code?: number) => asser
108
118
  */
109
119
  declare const assertFound: <T>(value: T | null | undefined, message: string, code?: number) => asserts value is T;
110
120
  //#endregion
111
- export { abortIf as a, initializeGlobalContext as c, Hash as d, Encryption as f, abort as i, isClass as l, ModelConstructor as n, assertFound as o, ModelRegistry as r, getModel as s, AbstractModelConstructor as t, perPage as u };
121
+ export { abortIf as a, getModelSync as c, perPage as d, Hash as f, abort as i, initializeGlobalContext as l, ModelConstructor as n, assertFound as o, Encryption as p, ModelRegistry as r, getModel as s, AbstractModelConstructor as t, isClass as u };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /// <reference path="./app.d.ts" />
2
- import { a as abortIf, c as initializeGlobalContext, d as Hash, f as Encryption, i as abort, l as isClass, n as ModelConstructor, o as assertFound, r as ModelRegistry, s as getModel, t as AbstractModelConstructor, u as perPage } from "./helpers-FHx7jP7g.js";
2
+ import { a as abortIf, c as getModelSync, d as perPage, f as Hash, i as abort, l as initializeGlobalContext, n as ModelConstructor, o as assertFound, p as Encryption, r as ModelRegistry, s as getModel, t as AbstractModelConstructor, u as isClass } from "./helpers-Bn6dk-9j.js";
3
3
  import { JitiOptions, JitiResolveOptions } from "jiti";
4
4
  import { Arkstack } from "@arkstack/contract";
5
5
  import pino from "pino";
@@ -870,4 +870,4 @@ declare const devTlsCredentials: (host?: string) => Promise<TlsCredentials>;
870
870
  */
871
871
  declare const localNetworkAddress: () => string | undefined;
872
872
  //#endregion
873
- export { AbstractModelConstructor, AppConfig, AppException, ArkstackErrorPayload, ArkstackErrorShape, CONFIG_KEY, Choice, Choices, ConfigLoader, ConfigRegistry, ConfigShape, DotPath, DotPathValue, Encryption, EnvKey, EnvLoader, EnvLookup, EnvRegistry, EnvReturn, ErrorHandler, Exception, FileImporter, GlobalConfig, GlobalEnv, Hash, Hook, HookArgs, HookFor, HookName, HookPos, HookPositions, HookRegistry, IHook, Logger, LoggerChalk, LoggerLog, LoggerParseSignature, MergedConfig, ModelConstructor, ModelRegistry, Primitive, PublishConfirmation, PublishEntry, PublishFilter, PublishGroup, Publisher, RequestException, TlsCredentials, UnionToIntersection, abort, abortIf, appKey, appUrl, assertFound, bindGracefulShutdown, bootWithDetectedPort, config, configLoader, createErrorPayload, devTlsCredentials, discoverCommands, env, envLoader, getErrorLogger, getModel, getPrimaryError, getValidationErrors, importFile, initializeGlobalContext, interopDefault, isClass, isModelNotFoundError, isValidationError, loadPrototypes, localNetworkAddress, logUnhandledError, nodeEnv, normalizeStatusCode, outputDir, perPage, rebuildOutput, renderError, resolveRuntimeDir, resolveRuntimeModule, serializeError, shouldHideStack, shouldLogError, toErrorShape, toOutputPath };
873
+ export { AbstractModelConstructor, AppConfig, AppException, ArkstackErrorPayload, ArkstackErrorShape, CONFIG_KEY, Choice, Choices, ConfigLoader, ConfigRegistry, ConfigShape, DotPath, DotPathValue, Encryption, EnvKey, EnvLoader, EnvLookup, EnvRegistry, EnvReturn, ErrorHandler, Exception, FileImporter, GlobalConfig, GlobalEnv, Hash, Hook, HookArgs, HookFor, HookName, HookPos, HookPositions, HookRegistry, IHook, Logger, LoggerChalk, LoggerLog, LoggerParseSignature, MergedConfig, ModelConstructor, ModelRegistry, Primitive, PublishConfirmation, PublishEntry, PublishFilter, PublishGroup, Publisher, RequestException, TlsCredentials, UnionToIntersection, abort, abortIf, appKey, appUrl, assertFound, bindGracefulShutdown, bootWithDetectedPort, config, configLoader, createErrorPayload, devTlsCredentials, discoverCommands, env, envLoader, getErrorLogger, getModel, getModelSync, getPrimaryError, getValidationErrors, importFile, initializeGlobalContext, interopDefault, isClass, isModelNotFoundError, isValidationError, loadPrototypes, localNetworkAddress, logUnhandledError, nodeEnv, normalizeStatusCode, outputDir, perPage, rebuildOutput, renderError, resolveRuntimeDir, resolveRuntimeModule, serializeError, shouldHideStack, shouldLogError, toErrorShape, toOutputPath };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { _ as EnvLoader, a as env, c as nodeEnv, d as resolveRuntimeDir, f as resolveRuntimeModule, g as configLoader, h as ConfigLoader, i as discoverCommands, l as outputDir, m as CONFIG_KEY, n as appUrl, o as importFile, p as toOutputPath, r as config, s as interopDefault, t as appKey, u as rebuildOutput, v as envLoader } from "./system-XvUhJFS0.js";
2
- import { _ as Hash, c as abortIf, d as initializeGlobalContext, f as isClass, g as Exception, h as AppException, l as assertFound, m as RequestException, p as perPage, s as abort, u as getModel, v as Encryption } from "./utils-BGjeEW4M.js";
2
+ import { _ as Exception, c as abortIf, d as getModelSync, f as initializeGlobalContext, g as AppException, h as RequestException, l as assertFound, m as perPage, p as isClass, s as abort, u as getModel, v as Hash, y as Encryption } from "./utils-C3mcZZMN.js";
3
3
  import { Hook as Hook$1 } from "@arkstack/foundry";
4
4
  import { Arkstack } from "@arkstack/contract";
5
5
  import { str } from "@h3ravel/support";
@@ -599,4 +599,4 @@ const localNetworkAddress = () => {
599
599
  for (const list of Object.values(networkInterfaces())) for (const net of list ?? []) if (net.family === "IPv4" && !net.internal) return net.address;
600
600
  };
601
601
  //#endregion
602
- export { AppException, CONFIG_KEY, ConfigLoader, Encryption, EnvLoader, ErrorHandler, Exception, Hash, Hook, Logger, Publisher, RequestException, abort, abortIf, appKey, appUrl, assertFound, bindGracefulShutdown, bootWithDetectedPort, config, configLoader, createErrorPayload, devTlsCredentials, discoverCommands, env, envLoader, getErrorLogger, getModel, getPrimaryError, getValidationErrors, importFile, initializeGlobalContext, interopDefault, isClass, isModelNotFoundError, isValidationError, loadPrototypes, localNetworkAddress, logUnhandledError, nodeEnv, normalizeStatusCode, outputDir, perPage, rebuildOutput, renderError, resolveRuntimeDir, resolveRuntimeModule, serializeError, shouldHideStack, shouldLogError, toErrorShape, toOutputPath };
602
+ export { AppException, CONFIG_KEY, ConfigLoader, Encryption, EnvLoader, ErrorHandler, Exception, Hash, Hook, Logger, Publisher, RequestException, abort, abortIf, appKey, appUrl, assertFound, bindGracefulShutdown, bootWithDetectedPort, config, configLoader, createErrorPayload, devTlsCredentials, discoverCommands, env, envLoader, getErrorLogger, getModel, getModelSync, getPrimaryError, getValidationErrors, importFile, initializeGlobalContext, interopDefault, isClass, isModelNotFoundError, isValidationError, loadPrototypes, localNetworkAddress, logUnhandledError, nodeEnv, normalizeStatusCode, outputDir, perPage, rebuildOutput, renderError, resolveRuntimeDir, resolveRuntimeModule, serializeError, shouldHideStack, shouldLogError, toErrorShape, toOutputPath };
@@ -1,4 +1,4 @@
1
- import { a as abortIf, c as initializeGlobalContext, d as Hash, f as Encryption, i as abort, l as isClass, n as ModelConstructor, o as assertFound, r as ModelRegistry, s as getModel, t as AbstractModelConstructor, u as perPage } from "../helpers-FHx7jP7g.js";
1
+ import { a as abortIf, c as getModelSync, d as perPage, f as Hash, i as abort, l as initializeGlobalContext, n as ModelConstructor, o as assertFound, p as Encryption, r as ModelRegistry, s as getModel, t as AbstractModelConstructor, u as isClass } from "../helpers-Bn6dk-9j.js";
2
2
  import { Model } from "arkormx";
3
3
 
4
4
  //#region src/utils/traits.d.ts
@@ -185,4 +185,4 @@ type Derived<T extends (Trait | TypeFactory<Trait> | Cons)> = T extends TypeFact
185
185
  */
186
186
  declare function uses<T extends (Trait | TypeFactory<Trait> | Cons)>(instance: unknown, trait: T): instance is Derived<T>;
187
187
  //#endregion
188
- export { AbstractModelConstructor, Derived, Encryption, Hash, ModelConstructor, ModelRegistry, Trait, abort, abortIf, assertFound, callTraitMethods, crc32, getModel, getTraitMethods, initializeGlobalContext, isClass, perPage, trait, use, uses };
188
+ export { AbstractModelConstructor, Derived, Encryption, Hash, ModelConstructor, ModelRegistry, Trait, abort, abortIf, assertFound, callTraitMethods, crc32, getModel, getModelSync, getTraitMethods, initializeGlobalContext, isClass, perPage, trait, use, uses };
@@ -1,2 +1,2 @@
1
- import { _ as Hash, a as use, c as abortIf, d as initializeGlobalContext, f as isClass, i as trait, l as assertFound, n as crc32, o as uses, p as perPage, r as getTraitMethods, s as abort, t as callTraitMethods, u as getModel, v as Encryption } from "../utils-BGjeEW4M.js";
2
- export { Encryption, Hash, abort, abortIf, assertFound, callTraitMethods, crc32, getModel, getTraitMethods, initializeGlobalContext, isClass, perPage, trait, use, uses };
1
+ import { a as use, c as abortIf, d as getModelSync, f as initializeGlobalContext, i as trait, l as assertFound, m as perPage, n as crc32, o as uses, p as isClass, r as getTraitMethods, s as abort, t as callTraitMethods, u as getModel, v as Hash, y as Encryption } from "../utils-C3mcZZMN.js";
2
+ export { Encryption, Hash, abort, abortIf, assertFound, callTraitMethods, crc32, getModel, getModelSync, getTraitMethods, initializeGlobalContext, isClass, perPage, trait, use, uses };
@@ -1,4 +1,5 @@
1
1
  import { a as env, f as resolveRuntimeModule, o as importFile, t as appKey } from "./system-XvUhJFS0.js";
2
+ import { createRequire } from "node:module";
2
3
  import { createCipheriv, createDecipheriv, createHash, randomBytes } from "node:crypto";
3
4
  import { Arkstack } from "@arkstack/contract";
4
5
  import path from "node:path";
@@ -191,6 +192,22 @@ async function getModel(modelName) {
191
192
  if (typeof model !== "function") throw new Error(`Model "${modelName}" not found`);
192
193
  return model;
193
194
  }
195
+ const isModelModule = (value) => typeof value === "object" && value !== null;
196
+ const resolveModelExport = (module, modelName) => {
197
+ if (!isModelModule(module)) return module;
198
+ return module.default ?? module[modelName] ?? module;
199
+ };
200
+ function getModelSync(modelName) {
201
+ const require = createRequire(import.meta.url);
202
+ const { Arkorm, getUserConfig } = require("arkormx");
203
+ const exportName = path.basename(modelName, path.extname(modelName));
204
+ const registeredModel = Arkorm.getRegisteredModels().find((model) => model.name === exportName);
205
+ if (registeredModel) return registeredModel;
206
+ const modelPath = getUserConfig().paths?.models || "./src/models";
207
+ const model = resolveModelExport(require(resolveRuntimeModule(path.join(path.isAbsolute(modelPath) ? modelPath : path.join(Arkstack.rootDir(), modelPath), modelName))), exportName);
208
+ if (typeof model !== "function") throw new Error(`Model "${modelName}" not found`);
209
+ return model;
210
+ }
194
211
  const initializeGlobalContext = async ({ Request, Response, Session } = {}) => {
195
212
  try {
196
213
  const { Request: Req, Response: Res, Session: Ses } = await import("@arkstack/http");
@@ -449,4 +466,4 @@ function uses(instance, trait) {
449
466
  return false;
450
467
  }
451
468
  //#endregion
452
- export { Hash as _, use as a, abortIf as c, initializeGlobalContext as d, isClass as f, Exception as g, AppException as h, trait as i, assertFound as l, RequestException as m, crc32 as n, uses as o, perPage as p, getTraitMethods as r, abort as s, callTraitMethods as t, getModel as u, Encryption as v };
469
+ export { Exception as _, use as a, abortIf as c, getModelSync as d, initializeGlobalContext as f, AppException as g, RequestException as h, trait as i, assertFound as l, perPage as m, crc32 as n, uses as o, isClass as p, getTraitMethods as r, abort as s, callTraitMethods as t, getModel as u, Hash as v, Encryption as y };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkstack/common",
3
- "version": "0.17.3",
3
+ "version": "0.17.4",
4
4
  "type": "module",
5
5
  "description": "Core utilities, primitives, and shared infrastructure for the Arkstack ecosystem.",
6
6
  "homepage": "https://arkstack.toneflix.net",
@@ -46,9 +46,9 @@
46
46
  },
47
47
  "peerDependencies": {
48
48
  "@h3ravel/support": "^2.2.4",
49
- "arkormx": "^2.11.4",
50
- "@arkstack/contract": "^0.17.3",
51
- "@arkstack/foundry": "^0.17.3"
49
+ "arkormx": "^2.11.6",
50
+ "@arkstack/contract": "^0.17.4",
51
+ "@arkstack/foundry": "^0.17.4"
52
52
  },
53
53
  "optionalDependencies": {
54
54
  "@faker-js/faker": "^10.4.0"