masquerade-orm
v0.8.1
Published
Lightweight ORM compatible with SQLite and Postgresql in Node.js
Maintainers
Readme
MasqueradeORM is a lightweight ORM for Node.js that works seamlessly with both TypeScript and JavaScript.
Its goal is to hide SQL complexity while letting you work naturally in JS/TS syntax. Instead of forcing you into ORM-specific models, metadata systems, or decorators, MasqueradeORM lets you use your own classes directly, exactly as you normally would.
MasqueradeORM improves readability, maintainability, and workflow simplicity through a unified coding approach and extremely minimal setup. No ORM offers a simpler start. There’s no need to manage heavy configuration layers, maintain secondary schema systems, or even plan your database structure separately. Your schema and tables are generated automatically from a single source of truth: Your class definitions.
MasqueradeORM currently supports the following SQL clients:
- SQLite
- Postgresql
Installation
npm install masquerade-ormFeatures
- Effortless setup - no ORM-specific structures; just use your classes.
- Zero schema planning - tables and schema are generated automatically.
- Powerful IntelliSense - confidently build complex queries with real-time IDE feedback when something’s wrong.
- Minimal memory usage - one class instance per database row, minimizing memory usage and avoiding duplicates through smart state management.
- Optimized querying - fewer queries through intelligent transaction grouping without sacrificing data integrity.
- Relational WHERE clauses - easily write conditions that compare two columns within the same table or columns across different tables.
- Write complex WHERE conditions using a template-literal helper - enabling expressive comparisons like >=, LIKE, object-property access, and even array element matching, all without cluttering your query code.
- SQL injection protection - all queries are parameterized.
- Lightweight - minimal dependencies.
- Strong typing even in JavaScript - powered by JSDoc, no compile step required.
- Reduced data transfer size - improves performance in client-server setups (not applicable for embedded databases like SQLite).
- Abstract and non-abstract inheritance - enables the use of abstract classes, even in JavaScript.
- Combines the convenience of embedded SQLite with the strict typing of RDBMS
- Eager and lazy relations
- Unidirectional, bidirectional, and self-referenced relations
Example Code Implementation
Creating an ORM-Compatible Class
import { Entity } from 'masquerade'
type UserSettings = {
theme: 'light' | 'dark' | 'system'
twoStepVerification: boolean
locale: 'en' | 'es' | 'fr' | 'de'
}
export class User extends Entity {
username: string
email: string
password: string
createdAt: Date = new Date()
friendList: User[] = []
settings: UserSettings & object = {
locale: "en",
theme: "system",
twoStepVerification: false
}
constructor(username: string, email: string, password: string) {
super()
this.username = username
this.email = email
this.password = password
}
}Basic Find Example
// finds any User instance with email === lookupEmail
async function findUserByEmail(lookupEmail: string): Promise<User | undefined> {
const resultArray = await User.find({
where: { email: lookupEmail }
})
// the static 'find' method above is inherited from 'Entity'
return resultArray[0]
}Saving Instances
// Creating a new table row in the User table
const newUser = new User('JohnDoe57', '[email protected]', 'passwordHash')
// newUser will be saved to the database automatically, no explicit save call is required.
// Finding a user by email
const user = await findUserByEmail('[email protected]') // user's friendList is a promise
console.log(user.username === 'JohnDoe57') // trueMutating Data
All mutations are persisted implicitly and automatically, meaning that simply changing a value is enough for it to be reflected in the database.
Mutating non-Relational Properties
user.settings.theme = 'dark' Mutating Relational Properties
// lazy-load friendList
await user.friendList
// add a new relation
user.friendList.push(new User('JaneDoe33', '[email protected]', 'passwordHash2'))
// remove a relation
user.friendList.pop() 