sqleto 0.0.5+6 copy "sqleto: ^0.0.5+6" to clipboard
sqleto: ^0.0.5+6 copied to clipboard

discontinued
SDKDart

A ORM-ish for use with PostgreSQL

SQLeto #

Um projeto experimental de ORM em Dart para PostgreSQL

Instalação #

Adicione a dependencia sqleto e as dependencias de dev sqleto_generator e build_runner no arquivo pubspec.yaml

dependencies:
  sqleto:
  
dev_dependencies:
  sqleto_generator:
  build_runner:

Utilização #

Inicialmente deve ser criado os Schemas que serão utilizados.

Aqui como exemplo vamos criar um Schema para a classe User

import 'package:sqleto/sqleto.dart';

part 'user.g.dart';

abstract class User extends SQLetoSchema {
  @Column(type: SQLetoType.UUID, defaultValue: SQLetoDefaultValue.UUID_GENERATE_V4)
  final String id;

  @Column(type: SQLetoType.TEXT, unique: true)
  final String email;

  @Column(type: SQLetoType.TEXT, password: true)
  final String password;

  @Column(type: SQLetoType.TIMESTAMP, defaultValue: SQLetoDefaultValue.NOW)
  final DateTime createdAt;

  User({
    required this.id,
    required this.email,
    required this.password,
    required this.createdAt,
  });
}

Em seguida precisamos executar o comando do build_runner no terminal!

dart run build_runner build --delete-conflicting-outputs

Será gerado um arquivo para cada classe que extende da classe SQLetoSchema

Nesse caso será gerado um arquivo user.g.dart

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'user.dart';

// **************************************************************************
// SQLetoGenerator
// **************************************************************************

class UserSchema extends User {
  final String id;
  final String email;
  final String password;
  final DateTime createdAt;

  UserSchema._({
    required this.id,
    required this.email,
    required this.password,
    required this.createdAt,
  });

  factory UserSchema.empty() {
    return UserSchema._(
      id: '',
      email: '',
      password: '',
      createdAt: DateTime.now(),
    );
  }

  factory UserSchema.create({
    required String email,
    required String password,
  }) {
    return UserSchema._(
      id: '',
      email: email,
      password: password,
      createdAt: DateTime.now(),
    );
  }

  static UserSchema fromMap(Map<String, dynamic> map) {
    return UserSchema._(
      id: map['id'] ?? '',
      email: map['email'] ?? '',
      password: map['password'] ?? '',
      createdAt: DateTime.fromMillisecondsSinceEpoch(map['created_at'] ?? 0),
    );
  }

  Map<String, dynamic> toMap() => {
        'id': id,
        'email': email,
        'password': password,
        'created_at': createdAt,
      };

  UserSchema copyWith({
    String? email,
    String? password,
  }) {
    return UserSchema._(
      id: id,
      email: email ?? this.email,
      password: password ?? this.password,
      createdAt: createdAt,
    );
  }

  @override
  String get tableName => 'tb_user';

  @override
  Future<void> save() => SQLeto.instance.update<UserSchema>(() => this);

  @override
  Future<void> delete() => SQLeto.instance.delete<UserSchema>(() => this);
}

Agora é o momento de inicializar a classe SQLeto, primeiro criando a instancia de SQLetoConfig com as informações do banco e os Schemas que serão utilizados!

final config = SQLetoConfig(
  host: 'localhost',
  port: 5432,
  database: 'postgres',
  schemas: [
    UserSchema,
  ],
);

E executar o método initialize no antes de ser de fato utilizado!

final sqleto = await SQLeto.initialize(config);

Pronto, agora já podemos realizar as transações no banco de dados!

Atualmente está disponível os seguintes métodos:

INSERT

UserSchema user = UserSchema.create(email: 'johndoe@gmail.com', password: 'password');

user = await sqleto.insert<UserSchema>(() => user);
SELECT
// get all
List<UserSchema> users = await sqleto.select<UserSchema>();

// or with filters

final where = Where('email', Op.equals, 'johndoe@gmail.com');

// or if necessary with multiple filters

where.and(Where('created_at', Op.lessThan, DateTime.now()));

// applying filters
List<UserSchema> users = await sqleto.select<UserSchema>(where);

// you can also get Schema by their primary key
UserSchema user = await sqleto.findByPK(schema_primary_key);

UPDATE

UserSchema user = await sqleto.findByPK(schema_primary_key);

user = user.copyWith(email: 'johndoe_edited@gmail.com');

// pass the updated Schema instance
user = await sqleto.update<UserSchema>(() => user);

// or quickly
await user.save();

DELETE

UserSchema user = await sqleto.findByPK(schema_primary_key);

// pass the Schema instance
await sqleto.delete<UserSchema>(() => user);

// or quickly
await user.delete();
EM PROGRESSO
  • Relacionamento entre Schemas
  • Autoincrement de Colunas do tipo INTEGER
  • Adicionar mais tipos de dado
0
likes
130
points
0
downloads

Publisher

unverified uploader

Weekly Downloads

A ORM-ish for use with PostgreSQL

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

crypto, postgres, rxdart, sqleto_annotation

More

Packages that depend on sqleto