fennec_pg is dart plugin for connecting to postgresql with orm. it belongs to fennec framework pub.dev but it can be used separately.


install the plugin from pub.dev

supported Feautures

  • connect to postgres
  • SelectBuilder for select operation with where clause etc.
  • FilterBuilder for filtering searched Data.
  • SelectBuilderWithNestedJsonOutPut for joins especially if you want related objects as nested json
  • Serializable for seriable your model dynamically.
  • Repository an Interface that can be used for create your own repository
  • relations one to one, one to many , belongs to.

create models

class User extends Serializable {
  @PrimaryKey(autoIncrement: true, columnType: ColumnType.bigInt)
  int? id;
  @Column(isNullable: false, indexType: IndexType.unique)
  late String name;
  @Column(isNullable: false, indexType: IndexType.unique)
  late String email;
      localKey: 'user_id',
      foreignKey: 'id',
      fetchType: FetchType.include,
      cascadeType: CascadeType.delete)
  Account? account;
  User.fromJson(Map<String, dynamic> map) {
    id = map['id'];
    name = map['name'];
    email = map['email'];
    if (map['account'] != null) {
      account = Account.fromJson(map['account']);

class Account extends Serializable {
  @PrimaryKey(autoIncrement: true, columnType: ColumnType.bigInt)
  int? id;
      localKey: 'id', foreignKey: 'user_id', fetchType: FetchType.include)
  User? user;

  Account.fromJson(Map<String, dynamic> map) {
    id = map['id'];
    if (map['user'] != null) {
      user = User.fromJson(map['user']);

create repository

class UserRepository extends Repository<User, int> {}
class AccountRepository extends Repository<Account, int> {}

user repository example

AccountRepository accountRepository = AccountRepository();
UserRepository userRepository = UserRepository();
User user = User();
user.email = '131@web.de';
user.name = 'ak1';
user.account = Account();
User? userResult = await userRepository.insert(user);
if (userResult != null) {
final result = await accountRepository.findAll();
for (var row in result) {

use SelectBuilder with FilterBuilder

SelectBuilder selectBuilder = SelectBuilder(['*'],table:'users');
FilterBuilder filterBuilder = FilterBuilder(Field.tableColumn('id'), '=', Field.int(2));
filterBuilder.or(FilterBuilder(Field.tableColumn('id'), '=', Field.int(4)));
final result = await PGConnectionAdapter.connection

for (var row in result) {

custom own query

 final result = await PGConnectionAdapter.connection
    .query('select * from users')

for (var row in result) {

start the connection with postgres

 var uri = 'postgres://user:password@localhost:5432/db-name';
await PGConnectionAdapter.init(uri);