@type-map-for/mongodb
v0.1.1
Published
Compile-time TypeScript → MongoDB field mapping
Readme
@type-map-for/mongodb
Generate MongoDB collection schemas from TypeScript interfaces at compile time with BSON types and required field tracking.
Installation
npm install @type-map-for/mongodbUsage
import type { MongoSchema } from '@type-map-for/mongodb';
interface User {
id: string;
name: string;
email?: string;
tags: string[];
metadata: { source: string };
}
type UserSchema = MongoSchema<User>;
// Result: {
// bsonType: 'object'
// required?: Array<'id' | 'name' | 'tags' | 'metadata'>
// properties: {
// id: { bsonType: 'string' }
// name: { bsonType: 'string' }
// email: { bsonType: 'string' }
// tags: { bsonType: 'array' }
// metadata: { bsonType: 'object' }
// }
// }
// Use with schema validation
const userSchema: UserSchema = {
bsonType: 'object',
required: ['id', 'name', 'tags', 'metadata'],
properties: {
id: { bsonType: 'string' },
name: { bsonType: 'string' },
email: { bsonType: 'string' },
tags: { bsonType: 'array' },
metadata: { bsonType: 'object' }
}
} as const satisfies UserSchema;Type Mapping
| TypeScript | MongoDB BSON |
|------------|--------------|
| string | 'string' |
| number | 'int' | 'double' |
| boolean | 'bool' |
| T[] | 'array' |
| object | 'object' |
| Required fields | Included in required array |
| Optional fields | Omitted from required array |
Types
MongoField<T, K>- BSON type definition for propertyKof typeTMongoProperties<T>- Properties mapping for interfaceTMongoSchema<T>- Complete MongoDB schema with required field tracking
Contributing
Issues and pull requests welcome at github.com/nathancanine/type-map-for.
License
MIT
