duxt_orm 0.4.0 copy "duxt_orm: ^0.4.0" to clipboard
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 COLUMN for 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
  • Added DatabaseAdapter.validateIdentifier() static helper for strict identifier validation

0.3.0 #

Added #

  • Soft Deletes - Enabled by default on all models
    • destroy() now sets deleted_at instead of removing the row
    • forceDelete() - Permanently delete, bypassing soft deletes
    • restore() - Restore a soft-deleted record
    • trashed getter - Check if a record is soft-deleted
    • withTrashed() / onlyTrashed() query scopes
    • deleted_at column auto-injected into schema during registration
    • Opt out with softDeletes: false in registerModel()
  • 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/update
    • beforeDelete() / afterDelete() - Called around delete (including soft delete)
  • QueryBuilder.forceDelete() - Permanently delete matching records via query
  • QueryBuilder.restore() - Restore soft-deleted records via query

0.2.2 #

Added #

  • Model<T>.include() - Eager load relations directly from Model interface
    final post = await Model<Post>().include(['category']).find(1);
    print(post.category?.name);
    
  • tableName parameter on relations - Explicitly set related table names for better runtime resolution
    BelongsTo<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 keys
    • attach(relation, id) - Attach a related model through pivot table
    • detach(relation, id) - Detach a related model from pivot table
    • sync(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
0
likes
140
points
445
downloads

Publisher

verified publisherbase.al

Weekly Downloads

ActiveRecord-style ORM for Dart. Supports PostgreSQL, MySQL, and SQLite with auto-migrations, query builder, and schema definitions.

Homepage
Repository (GitHub)
View/report issues

Topics

#orm #database #postgresql #mysql #sqlite

Documentation

API reference

License

MIT (license)

Dependencies

mysql_client, postgres, sqlite3

More

Packages that depend on duxt_orm