duxt_orm 0.4.0
duxt_orm: ^0.4.0 copied to clipboard
ActiveRecord-style ORM for Dart. Supports PostgreSQL, MySQL, and SQLite with auto-migrations, query builder, and schema definitions.
0.4.0 #
Added #
- Auto column migrations -
migrate()now adds new columns to existing tables automatically- Compares schema definition against existing table columns
- Runs
ALTER TABLE ADD COLUMNfor any missing columns - Existing columns are never modified or dropped
- Driver-specific column introspection (PRAGMA for SQLite, information_schema for PostgreSQL/MySQL)
- Gracefully handles constraint incompatibilities (e.g., SQLite NOT NULL without DEFAULT)
0.3.1 #
Fixed #
- SQL injection in
escapeId()- All database adapters now strip quote characters from identifiers before wrapping:- SQLite: strips
"from identifiers - PostgreSQL: strips
"from identifiers - MySQL: strips
`from identifiers
- SQLite: strips
- Added
DatabaseAdapter.validateIdentifier()static helper for strict identifier validation
0.3.0 #
Added #
- Soft Deletes - Enabled by default on all models
destroy()now setsdeleted_atinstead of removing the rowforceDelete()- Permanently delete, bypassing soft deletesrestore()- Restore a soft-deleted recordtrashedgetter - Check if a record is soft-deletedwithTrashed()/onlyTrashed()query scopesdeleted_atcolumn auto-injected into schema during registration- Opt out with
softDeletes: falseinregisterModel()
- Nested Eager Loading - Dot notation for loading nested relations
await Model<Post>().include(['author.profile', 'comments.user']).get(); - Lifecycle Hooks - Override to run logic around persistence operations
beforeSave()/afterSave()- Called around insert/updatebeforeDelete()/afterDelete()- Called around delete (including soft delete)
QueryBuilder.forceDelete()- Permanently delete matching records via queryQueryBuilder.restore()- Restore soft-deleted records via query
0.2.2 #
Added #
Model<T>.include()- Eager load relations directly from Model interfacefinal post = await Model<Post>().include(['category']).find(1); print(post.category?.name);tableNameparameter on relations - Explicitly set related table names for better runtime resolutionBelongsTo<Category>(foreignKey: 'category_id', tableName: 'categories')
0.2.1 #
Added #
- Pivot table support for many-to-many relationships
Entity.registerPivotTable()- Register pivot tables with composite primary keysattach(relation, id)- Attach a related model through pivot tabledetach(relation, id)- Detach a related model from pivot tablesync(relation, ids)- Sync relations (replace all with given IDs)
- Composite primary keys - Schema now supports multi-column primary keys for pivot tables
- Pivot tables are automatically migrated with
DuxtOrm.migrate()
Example #
// In Post model
Entity.registerRelation<Post>('tags', BelongsToMany<Tag>(
pivotTable: 'post_tags',
foreignPivotKey: 'post_id',
relatedPivotKey: 'tag_id',
));
Entity.registerPivotTable('post_tags', schema: {
'post_id': Column.integer().notNull().references('posts'),
'tag_id': Column.integer().notNull().references('tags'),
}, primaryKey: ['post_id', 'tag_id']);
// Usage
await post.attach('tags', tagId);
await post.detach('tags', tagId);
await post.sync('tags', [1, 2, 3]);
0.2.0 #
Added #
- Relations support - Define relationships between models
HasMany<T>- One-to-many relationships (e.g., Category has many Posts)BelongsTo<T>- Inverse of has-many (e.g., Post belongs to Category)HasOne<T>- One-to-one relationships (e.g., User has one Profile)BelongsToMany<T>- Many-to-many through pivot tables
- Eager loading - Load related models in a single query with
.with_()// Load posts with their categories (no N+1 queries!) final posts = await Model.query<Post>() .with_(['category']) .get(); for (final post in posts) { print(post.category?.name); // Already loaded } - Relation accessors -
getRelation<T>(),setRelation(),relationLoaded() - Model registration for relations -
Model.registerRelation<T>(name, relation)
Changed #
toMap()should now include the id field for relations to work correctly
0.1.0 #
- Initial release
- ActiveRecord-style ORM with GORM-like auto-migrations
- Database adapters for PostgreSQL, MySQL, and SQLite
- Query builder with fluent API
- Schema definition with column types and modifiers
- Auto table name inference from class names
- Transaction support
- Raw query execution
- Aggregate functions (count, sum, avg, max, min)
- Bulk update and delete operations