@schemat/parser-mongoose
v0.2.1
Published
Mongoose parser for Schemat (Mongoose schemas -> IR, static AST).
Maintainers
Readme
@schemat/parser-mongoose
Mongoose parser for Schemat: turns
Mongoose schema definitions into the canonical Schemat IR by statically parsing
the source (via ts-morph) — it never connects to MongoDB, imports your app,
or runs your code.
MongoDB is schemaless at the database level. This parser reads the application-level Mongoose schema (a logical model in code), not a physical database schema. "Tables" are Mongoose models/collections; "relations" are derived from
refconventions, not enforced foreign keys.
Install
npm i @schemat/parser-mongooseUsage
import { mongooseParser } from "@schemat/parser-mongoose";
const ir = await mongooseParser.parse({ projectPath: "." });
// Or point at specific model files:
const ir2 = await mongooseParser.parse({ projectPath: ".", files: ["src/models/user.ts"] });mongooseParser implements the SchemaParser interface (detect + parse)
from @schemat/core and returns
an IRSchema.
Detection
Detects a Mongoose project when any of these hold:
mongooseis a dependency inpackage.json, or- a source file contains
new Schema(/new mongoose.Schema(, or - a
models/*file imports mongoose.
What it maps
new Schema({...})+model('Name', schema)→ tables (named after the model; falls back to the schema variable name when nomodel()call links it)- Every model gets an implicit
_idObjectId primary key (matching Mongoose) - Fields (shorthand
age: Numberand options-object{ type, required, unique, default, enum, ref }) → columns;required→ not-null,unique,default enum: ['a','b']on a String field → an enum named<Model>_<field>{ type: ObjectId, ref: 'Other' }→ aone-to-manyrelation (fromColumns: [field],toColumns: ['_id']); an array of refs ([{ type: ObjectId, ref }]) →many-to-many
Limitations (v1)
- Nested subdocuments (inline object fields like
address: { city: String }) are collapsed to a singleObjectcolumn — not recursed into separate tables. - Arrays of primitives collapse to an
Arraycolumn. reftargets are matched by model name; a ref to a model not present in the parsed files still emits the edge (Mongo doesn't enforce FKs).
License
MIT © Ali Reza Hamid
