@json-express/core
The foundational kernel and the runtime entrypoint of the entire JSONExpress ecosystem.
The @json-express/core package serves two roles:
- The contract layer — TypeScript interfaces every plugin must implement (
IDatabaseAdapter,ITransport,IApiGenerator, …) plus the schema engine (defineModel,defineRoutes,types). - The runtime — the
json-expressbinary that auto-discovers installed plugins frompackage.json, instantiates them in dependency order, and starts the server.
Core itself contains zero HTTP, zero database, and zero filesystem-handler logic. Every concrete capability comes from a separate @json-express/* package.
Installation
Most users install @json-express/boot, which depends on core. Install core directly only if you want to hand-pick every plugin:
npm install @json-express/coreYou will also need at least one transport, one adapter, one API generator, one logger, and a config provider for the runtime to boot.
The json-express binary
Core ships a single binary:
npx json-express # Start the server
npx json-express --seed # Run any installed seeders (idempotent)
npx json-express --seed-append # Append seed records instead of resetting
npx json-express --preset-init # Extract templates from an installed preset
npx json-express --preset-init=@scope/preset # Pick a specific preset by nameThe binary boots through these stages (see kernel.ts:boot):
- Load the
IConfigProvider(defaults to@json-express/config-env). - Recursively walk
package.jsondependencies for@json-express/*packages and any package whose name containsjson-express-. - Resolve the active package per category —
adapter,api,transport,logger,docs,id. If multiple are installed in one category, the binary aborts and asks you to setjex.<category>=<pkg>in.env. - Run every plugin's
onRegister, generate routes viaapiGenerator.generate(collections), compose middlewares per route, register routes with the transport. - Run seeders if
--seedwas passed. - Run
onBoot, mount docs routes, calltransport.start(port), fireonReady.
There is no separate @json-express/cli package required to use this binary.
What core exports
1. Interface contracts (src/types.ts)
The TypeScript interfaces that define the rules of the ecosystem:
IDatabaseAdapter— every database (Memory, JSON, Postgres, …)IApiGenerator— every API layer (REST, GraphQL, …)ITransport— every HTTP server (Express, Fastify, …)IMiddleware— every middleware (auth, validation, …)IPlugin— every boot-time plugin (identity, devcert, health, …)IConfigProvider— every configuration engine (env, file, …)ILogger,IKvStore,IQueueAdapter,IEmailProvider,ISeeder,IIdGenerator
2. The schema engine — defineModel and defineRoutes
defineModel is for entities (anything with fields). It auto-generates CRUD against the listed fields and lets you decorate with hooks, access rules, validation, custom endpoints, and GraphQL extensions.
import { defineModel, types } from '@json-express/core';
export default defineModel({
name: 'products',
fields: {
id: types.id(),
title: types.string({ unique: true }),
price: types.number(),
inStock: types.boolean({ default: true })
}
});defineRoutes is sugar for fieldless / route-only models — search endpoints, webhooks, anything that does not represent an entity. The filename becomes the mount prefix, the endpoint key the suffix.
// models/search.ts
import { defineRoutes } from '@json-express/core';
export default defineRoutes({
endpoints: {
'GET /': async ({ query }) => {
// GET /search?q=...
}
}
});Internally defineRoutes is defineModel({ exposeApi: false, ... }) — it suppresses the auto-generated CRUD surface and keeps everything else.
3. The kernel and runtime utilities
import { JsonExpressKernel, startServer, runPresetInit } from '@json-express/core';JsonExpressKernel— the awilix container, plugin registries, andboot()/shutdown()lifecycle.startServer()— whatnpx json-expresscalls. Auto-discovery + boot.runPresetInit(cwd, flagArg)— what--preset-initcalls. Copies a preset'stemplates/intocwd.
4. Access control utilities
evaluateAccess(rule, userPayload)— allow / deny / owner-check decision.needsOwnerCheck(rule)—trueif rule is'owner'.ownsRecord(record, ownerField, user)— compares the record owner field against the JWT payload.stripDeniedReadFields()/stripDeniedWriteFields()— strip forbidden fields from API payloads.
5. The adapter compliance suite
A testing utility that lets custom adapter authors verify their implementation:
import { runAdapterComplianceTests } from '@json-express/core';
await runAdapterComplianceTests(
async () => new MyCustomAdapter(),
async (db) => await db.disconnect()
);6. Request context (AsyncLocalStorage)
Per-request tracing wrapper:
import { RequestContext } from '@json-express/core';
const traceId = RequestContext.getTraceId(); // auto-injected UUIDRelated
- @json-express/boot — the recommended default stack
- Presets — bundle a stack as a single npm package
- @json-express/config-env — the default configuration provider
- Architecture — kernel, IoC container, plugin lifecycle in depth