A model is a table plus the code that reads and writes it. prorm.define()
takes the name, the columns, and the table options.Column options#
| Option | Does |
|---|
type | The data type. Always call it: DataTypes.STRING(100), not DataTypes.STRING. |
primaryKey | Part of the primary key. |
autoIncrement | The database assigns it. |
allowNull | false adds NOT NULL. |
defaultValue | A default. A plain value becomes the column's DEFAULT; a function is not sent to the database (see Data types). |
unique | true, or a string to name the constraint. |
field | The column name, when it differs from the property — see Column names. |
comment | A column comment, where the engine has them. |
validate | Rules checked before every write — see Validation. |
secure | Hash the value before it is written, and never store the plain text — see Passwords. |
Passwords#
A column that says secure is hashed before every write, and the plain text
never reaches the database:true means scrypt, which needs nothing installed. 'bcrypt' and 'argon2'
name a package to install; an object of your own — { hash, verify } — is also
a method. Checking one at sign-in:Data types#
The common ones, and what they become:The full list, and what each one does on each engine, is in
Data types.Table options#
With the Post columns from the top of the page, sync() sends:[!NOTE]
underscored: true is accepted, but in this release it does not rename the
columns define() creates — createdAt stays createdAt. Name attributes in
snake_case yourself if the database convention needs it.
Creating the tables#
sync() is for getting started and for tests. Once a schema is real, changes
belong in migrations, which are ordinary JavaScript
files.Instances#
An instance moves through a small set of states, and each call is one
statement:[!TIP]
Prefer update(changes) to assigning and calling save() for edits:
update() writes only the changed columns, while save() on a loaded row
rewrites every column. Both validate what changed (see
Validation).
A module per model#
In a real app, each model is a function of the connection, and one module
wires them together:src/
db.js # new Prorm(...), requires every model, associates, exports
models/
user.js # module.exports = (prorm) => prorm.define('User', …)
post.js
server.js # const { User, Post } = require('./db')
Defining and associating every model before connect() matters: a model
defined on an open connection has its table created at once, before its
associations exist (see Associations).Next#
Hooks — code that runs around a write Modified at 2026-10-04 00:40:16