dsql 1.0.4 copy "dsql: ^1.0.4" to clipboard
dsql: ^1.0.4 copied to clipboard

Dart SQL library for generating .dart files from .sql files

DSQL ORM #

An Experimental Dart ORM

- Installation
dart pub add dsql
or
dependencies:
    dsql: any
- Usage
  • Put your sql files in migrations directory with a commentary with the name of the Entity that will be generated like the example bellow.
-- entity: UserEntity
CREATE TABLE IF NOT EXISTS tb_users (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
  name VARCHAR(255) NOT NULL,
  username VARCHAR(24) NOT NULL UNIQUE,
  email VARCHAR(255) NOT NULL UNIQUE,
  password VARCHAR(255) NOT NULL,
  image VARCHAR(255),
  bio VARCHAR(255),
  created_at TIMESTAMP NOT NULL DEFAULT NOW (),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW ()
);

-- entity: PostEntity
CREATE TABLE IF NOT EXISTS tb_posts (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
  post_id UUID,
  content VARCHAR(255) NOT NULL,
  user_id UUID NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT NOW (),
  updated_at TIMESTAMP NOT NULL DEFAULT NOW (),
  CONSTRAINT fk_post_replies FOREIGN KEY (post_id) REFERENCES tb_posts (id),
  CONSTRAINT fk_user_posts FOREIGN KEY (user_id) REFERENCES tb_users (id)
);

-- entity: LikeEntity
CREATE TABLE IF NOT EXISTS tb_likes (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
  post_id UUID NOT NULL,
  user_id UUID NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT NOW (),
  CONSTRAINT fk_post_likes FOREIGN KEY (post_id) REFERENCES tb_posts (id),
  CONSTRAINT fk_user_likes FOREIGN KEY (user_id) REFERENCES tb_users (id)
);
  • Set the DATABASE_URL environment variable to your database url.
Note: you can also create a .env file with the database url.
  • After that you can generate the dart files with the command dart run sdql --generate or dart run dsql --migrate to create the tables in the database or update them and generate the dart files.

  • When you migrate your sql files, a table called __migrations will be created in the selected database/schema. When new migrations are created, they will be inserted in this table with the date of the migration.

  • If some changes are made in your sql files, you can run dart run dsql --migrate to update the tables in the database (It will drop the tables and recreate them) and generate the dart files.

Note: this is very experimental, it is not ready yet. To works properly, you need to create your sql files like the example, in case of FOREIGN KEY, you need to set exactly of the example: CONSTRAINT fk_constraint_name FOREIGN KEY (column_name) REFERENCES table_name (column_name).
  • This is how the Entities generated by DSQL looks like:
class UserEntity {
 final String id;
 final String name;
 final String username;
 final String email;
 final String password;
 final String? image;
 final String? bio;
 final DateTime createdAt;
 final DateTime updatedAt;
 final List<PostEntity> userPosts;
 final List<LikeEntity> userLikes;
 final List<FollowerEntity> followers;
 final List<FollowerEntity> following;

 const UserEntity({
   required this.id,
   required this.name,
   required this.username,
   required this.email,
   required this.password,
   this.image,
   this.bio,
   required this.createdAt,
   required this.updatedAt,
   this.userPosts = const <PostEntity>[],
   this.userLikes = const <LikeEntity>[],
   this.followers = const <FollowerEntity>[],
   this.following = const <FollowerEntity>[],
 });

 UserEntity copyWith({
   String? id,
   String? name,
   String? username,
   String? email,
   String? password,
   String? image,
   String? bio,
   DateTime? createdAt,
   DateTime? updatedAt,
   List<PostEntity>? userPosts,
   List<LikeEntity>? userLikes,
   List<FollowerEntity>? followers,
   List<FollowerEntity>? following,
 }) {
   return UserEntity(
     id: id ?? this.id,
     name: name ?? this.name,
     username: username ?? this.username,
     email: email ?? this.email,
     password: password ?? this.password,
     image: image ?? this.image,
     bio: bio ?? this.bio,
     createdAt: createdAt ?? this.createdAt,
     updatedAt: updatedAt ?? this.updatedAt,
     userPosts: userPosts ?? this.userPosts,
     userLikes: userLikes ?? this.userLikes,
     followers: followers ?? this.followers,
     following: following ?? this.following,
   );
 }

 Map<String, dynamic> toMap() {
   return {
     'id': id,
     'name': name,
     'username': username,
     'email': email,
     'password': password,
     'image': image,
     'bio': bio,
     'created_at': createdAt,
     'updated_at': updatedAt,
     'user_posts': userPosts.map((e) => e.toMap()).toList(),
     'user_likes': userLikes.map((e) => e.toMap()).toList(),
     'followers': followers.map((e) => e.toMap()).toList(),
     'following': following.map((e) => e.toMap()).toList(),
   };
 }

 String toJson() => json.encode(toMap());

 factory UserEntity.fromMap(Map<String, dynamic> map) {
   return UserEntity(
     id: map['id'] as String,
     name: map['name'] as String,
     username: map['username'] as String,
     email: map['email'] as String,
     password: map['password'] as String,
     image: map['image'] as String,
     bio: map['bio'] as String,
     createdAt: map['created_at'] as DateTime,
     updatedAt: map['updated_at'] as DateTime,
     userPosts: List<PostEntity>.from(
       (map['user_posts'] as List).map(
         (innerMap) {
           return PostEntity.fromMap(innerMap);
         },
       ),
     ),
     userLikes: List<LikeEntity>.from(
       (map['user_likes'] as List).map(
         (innerMap) {
           return LikeEntity.fromMap(innerMap);
         },
       ),
     ),
     followers: List<FollowerEntity>.from(
       (map['followers'] as List).map(
         (innerMap) {
           return FollowerEntity.fromMap(innerMap);
         },
       ),
     ),
     following: List<FollowerEntity>.from(
       (map['following'] as List).map(
         (innerMap) {
           return FollowerEntity.fromMap(innerMap);
         },
       ),
     ),
   );
 }

 @override
 String toString() {
   return 'UserEntity(id: $id, name: $name, username: $username, email: $email, password: $password, image: $image, bio: $bio, createdAt: $createdAt, updatedAt: $updatedAt, userPosts: $userPosts, userLikes: $userLikes, followers: $followers, following: $following)';
 }

 @override
 bool operator ==(Object other) {
   if (identical(this, other)) return true;

   return other is UserEntity &&
       other.id == id &&
       other.name == name &&
       other.username == username &&
       other.email == email &&
       other.password == password &&
       other.image == image &&
       other.bio == bio &&
       other.createdAt == createdAt &&
       other.updatedAt == updatedAt;
 }

 @override
 int get hashCode {
   return id.hashCode ^
       name.hashCode ^
       username.hashCode ^
       email.hashCode ^
       password.hashCode ^
       image.hashCode ^
       bio.hashCode ^
       createdAt.hashCode ^
       updatedAt.hashCode;
 }
}
To a complete example, see the example folder in the repository.

Under Development!!! #

4
likes
0
points
12
downloads

Publisher

unverified uploader

Weekly Downloads

Dart SQL library for generating .dart files from .sql files

Repository (GitHub)
View/report issues

Topics

#postgres #orm

License

unknown (license)

Dependencies

args, path, postgres, strings

More

Packages that depend on dsql