What is SQL to Waterline Model Generator?
Waterline is the default ORM bundled with Sails.js, one of the most popular Node.js MVC frameworks. It abstracts away database-specific syntax so that the same model definition works across PostgreSQL, MySQL, MongoDB, Redis, and other adapters — you define attributes once and Waterline handles the translation. This converter reads your SQL CREATE TABLE statements and produces Sails.js model files with the module.exports structure that Sails expects. Each attribute is typed using Waterline's type system (string, number, boolean, json, ref), foreign key references become model associations with the _id suffix stripped, and SQL constraints like NOT NULL and UNIQUE are converted into Waterline validation rules (required: true, unique: true). Optional lifecycle callback stubs — beforeCreate, afterCreate, beforeUpdate, afterUpdate, beforeDestroy, afterDestroy — give you hooks for adding custom logic around persistence operations.
How to Use
- Paste your CREATE TABLE statements into the SQL input area
- Select the database dialect that matches your source schema
- Toggle options: include validation rules from SQL constraints, lifecycle callback stubs, and TypeScript type declarations
- Click Generate Waterline Models to produce the model definitions
- Copy each generated model into the api/models/ directory of your Sails.js project
Why Use This Tool?
Tips & Best Practices
- Waterline uses singular lowercase model names as identities — the converter automatically singularizes table names (users → user, categories → category)
- Foreign key columns ending in _id are converted to associations: the _id suffix is removed and a model reference is created pointing to the singularized parent table name
- Use lifecycle callbacks for cross-cutting concerns like hashing passwords in beforeCreate or logging audit trails in afterUpdate
- The customToJSON method is the right place to remove fields like hashedPassword or internal flags before records are serialized into API responses
Frequently Asked Questions
How does the converter map SQL types to Waterline attribute types?
INTEGER, BIGINT, and SERIAL map to number, VARCHAR and CHAR map to string with an optional maxLength property, TEXT maps to string with columnType: "text", BOOLEAN maps to boolean, TIMESTAMP and DATETIME map to ref with columnType: "datetime", DATE maps to ref with columnType: "date", JSON and JSONB map to json, FLOAT and DOUBLE map to number with columnType: "float", UUID maps to string with isUUID: true, and BLOB maps to ref with columnType: "binary".
When should I avoid Waterline and use a different ORM?
Waterline's adapter abstraction is convenient but can limit access to database-specific features like PostgreSQL window functions, MongoDB aggregation pipelines, or raw SQL performance tuning. If your application heavily relies on advanced queries, complex joins, or database-specific optimizations, consider using Sequelize, Objection.js, or a query builder like Knex directly instead of Waterline.
How are foreign key relationships converted to Waterline associations?
SQL REFERENCES clauses become Waterline model associations. For example, author_id INTEGER REFERENCES users(id) becomes author: { model: "user" } in the generated model. The _id suffix is stripped from the attribute name, and the referenced table name is singularized and lowercased to form the model identity.
What does the TypeScript option generate?
When enabled, the converter produces TypeScript interface declarations for each model's attributes and methods, along with a typed export using Waterline.getModel. This gives you IDE autocompletion and type checking when working with Sails models in a TypeScript project.
Is my SQL schema transmitted to any server during conversion?
No. The entire conversion process runs in your browser using a client-side parser. Your database schema is never sent over the network or stored on any external server.
Can I use the generated models with Sails.js hooks and blueprints?
Yes. The generated model files follow the standard Sails.js model format and are automatically loaded by the Sails ORM hook. They work with Sails blueprint routes (find, findOne, create, update, destroy) and can be customized with custom controller actions and route bindings.
Real-world Examples
Sails.js REST API with user and post models
A Sails.js application exposes a REST API for a blogging platform. The developer converts the SQL schema to Waterline models, then uses Sails blueprints for automatic CRUD routes with custom lifecycle hooks for password hashing.
Multi-adapter Sails project with MongoDB and PostgreSQL
A Sails.js application uses MongoDB for session storage and PostgreSQL for relational data. Waterline models work across both adapters with the same attribute definitions, and the converter generates models from the PostgreSQL schema that also work with the MongoDB adapter.