Dartabase Model

Serverside Database Object Models for simple data manipulation using MySQL/PGSQL without having to write SQL

inspired by Ruby on Rails models

This requires the use of Dartabase Migration

Tested on 

Dart SDK version 1.16.0
    

Uses

MYSQL via sqljocky version 0.14.1

PGSQL via postgresql version 0.3.3


####Dartabase Documentation 1.Dartabase Migration - How to install and use

2.Dartabase Model - How to how to install and use - current

####Dartabase Tutorials 1.How to create a Dartabase supported app from scratch

2.How to create a Dartabase supported app for an existing database


HOW TO SETUP MODEL

After you have successfully finished setting up 'Dartabase Migration'

  1. Install Dartabase Model the usual pubspec way

  2. Inside your project, at the beginning of the main method insert

    Model.initiate("path-to-your-project");
    

now it should look kinda like this:

	-----dataserver.dart--START--

	library dataServer;

	import 'package:dartabase_model/dartabase_model.dart';

	main(){
	  Model.initiate("C:\\darttestproject\\DartabaseServer");
	  ... your code
	}

	-----dataserver.dart--END--

  1. Imagine you have ONLY created one database table named 'account'

    with the column 'name'
    
  2. You have to extend all classes that you want to connected to the database

    with 'Model'
    

in this case we create a class Account with id, name, created_at and updated_at

	-----account.dart--START--
		
	part of dataServer;
	
	class Account extends Model{
	  num id;// database column autogenerated by migration		
	  String name;
	  DateTime created_at;// database column autogenerated by migration
      DateTime updated_at;// database column autogenerated by migration
	}
	
	-----account.dart--END--
  1. Now add account.dart as part to dataServer so you can access Account

    -----dataserver.dart--START--
    
    library dataServer;
    
    import 'package:dartabase_model/dartabase_model.dart';
    part "account.dart";	
    
    main(){
      Model.initiate("C:\\darttestproject\\DartabaseServer");
      ... your code
    }
    
    -----dataserver.dart--END--
    

HOW TO USE SIMPLE MODEL FUNCTIONS

Future save()

returns String "created" or "updated"
 
var process = await player.save();
if(process == "created" || process == "updated"){
  //your code
}

Future findBy(String column,var value)

returns an (player) object if one exists 
else 
returns null

var player = await player.findBy("name","tim");
if(player != null){
  //your code
}
     

Future findById(var id)

accepted type of id is (String || int || num)
 
returns an (player) object if one exists 
else 
returns null

var player = await player.findById("3");
if(player != null){
  //your code
}

Future findAllBy(String column, var value)

returns a list of (player) objects if one exists 
else 
returns empty list

List players = await player.findAllBy("name","tim");
if(!players.isEmpty){
  //your code
}

Future findAll()

returns a list of all (player) objects if one exists 
else 
returns empty list

List players = await player.findAll();
if(!players.isEmpty){
  //your code
} 

Future delete()

deletes the object //TODO and all its relations

await player.delete();
//your code


RELATIONS

Future receive(object)

creates relation between the two objects (player and character)
    
var result = await player.receive(character);
//your code

Future hasOne(object)

returns an (character) object if one exists 
else 
returns null
   
var character = await player.hasOne(new Character());
if(character != null){
  //your code
}

Future hasMany(object)

returns a list of (character) objects if one exists 
else 
returns empty list

List characters = await player.hasMany(new Character());
if(!characters.isEmpty){
  //your code
}

Future hasOneWith(object,String column,String value)

returns an (character) object if one exists 
else 
returns null
 
var character = await player.hasOneWith(new Character(),'level','3');
if(character != null){
  //your code
}

Future hasManyWith(object,String column,String value)

Returns a list of (character) objects if one exists 
else 
Returns empty list
   
List characters = await player.hasManyWith(new Character(),'level','3');
if(!characters.isEmpty){
  //your code
} 

Future remove(object)

remove relation between the two objects (player and character)

var result = await player.remove(character);
//your code

TODO

*test functionality in bigger project
*add more features like implementing and removing dependencies
*add automated tests
*and much more

Please let me know about bugs you find and or improvements/features you would like to see in future.

ENJOY

Libraries

dartabaseModel