Lightweight ORM compatible with SQLite and Postgresql in Node.js
npm install masquerade-ormbash
npm install masquerade-orm
`
Features
- 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 guidance and warnings 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.
- Expressive template-literal WHERE clauses - Write complex, readable conditions such as LIKE, ≥, nested property access, array element matching (and more) by using IntelliSense-enabled tagged template literals. Any SQL WHERE logic can be expressed through this API.
- Cross-column conditions - Easily write WHERE clauses that compare two columns (within the same table or across joined tables).
- Powerful relation capabilities - Full support for eager & lazy loading, unidirectional / bidirectional / self-referencing relationships, and modifying associations even when they are not loaded.
- SQL injection protection - All queries are parameterized.
- Minimal data transfer size - Improves performance in client-server setups (not applicable for embedded databases like SQLite).
- Soft + hard deletion support
- Abstract and non-abstract inheritance - Enables the use of abstract classes, even in JavaScript.
- Strong typing even in JavaScript - Powered by JSDoc, no compile step required.
- Smart schema cleanup - Automatically detect and easily remove unused tables and columns, reducing database bloat and improving performance.
- Lightweight - Minimal dependencies.
- Combines the convenience of embedded SQLite with the strict typing of RDBMS
Example Code Implementation
$3
`ts
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
}
}
`
$3
`ts
// finds any User instance with email === lookupEmail
async function findUserByEmail(lookupEmail: string): Promise {
const resultArray = await User.find({
where: { email: lookupEmail }
})
// the static 'find' method above is inherited from 'Entity'
return resultArray[0]
}
`
$3
`ts
// Creating a new table row in the User table
const newUser = new User('JohnDoe57', 'johnDoe@yahoo.com', 'passwordHash')
// newUser will be saved to the database automatically, no explicit save call is required.
// Finding a user by email
const user = await findUserByEmail('johnDoe@yahoo.com') // user's friendList is a promise
console.log(user.username === 'JohnDoe57') // true
`
$3
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
`ts
user.settings.theme = 'dark'
`
Mutating Relational Properties
`ts
// lazy-load friendList
await user.friendList
// add a new relation
user.friendList.push(new User('JaneDoe33', 'janeDoe@yahoo.com', 'passwordHash2'))
// remove a relation
user.friendList.pop()
``