A Dart client for interacting with DORM APIs.
The DORM Dart Client provides easy-to-use classes for interacting with DORM API. It allows developers to map their database tables to Dart classes, and then perform CRUD operations on those classes, which are automatically translated into corresponding SQL statements.
The DORM supports a variety of databases, including MySQL and MSSQL. It provides a flexible and extensible architecture that allows developers to perform operations on databases and provides an API.
You can learn more about the DORM on its repository.
Import the dorm_client
package in your Dart file:
import 'package:dorm_client/dorm_client.dart';
To use the DORM Client, you need to have a class that represents the table in the database. For example, let's say we have a table named "profile" with two columns "id" and "name", we would create a Dart class named "Profile" like this:
import 'package:json_annotation/json_annotation.dart';
part 'profile.g.dart';
@JsonSerializable()
class Profile {
static final String tableName = "profile";
final String id;
final String name;
Profile({
required this.id,
required this.name,
});
factory Profile.fromJson(Map<String, dynamic> json) => _$ProfileFromJson(json);
Map<String, dynamic> toJson() => _$ProfileToJson(this);
@override
String toString() {
return "$id: $name";
}
}
Next, create an instance of the DORM
class, passing in the schema version, API URL, and token as arguments:
DORMRequest request = DORMRequest().addRead(
from: Profile.tableName,
);
DORMResponse response = await _dorm.post(request);
The post
method returns a DORMResponse
object, which contains the response from the API. You can extract the rows from the response using the rows method, passing in a function to parse the JSON data into a Profile object:
List<Profile>? profiles = response.rows(
Profile.tableName,
(json) => Profile.fromJson(json),
);
Finally, you can manipulate the retrieved data as desired.
The DORM
class is a client for communicating with a DORM server API. It uses the Dio library to make HTTP requests to the server.
The DORM class has a constructor that takes three required parameters:
schema
: the version of the DORM API being used.url
: the URL of the DORM API location.token
: the authentication token used to access the DORM API.
Future<Response> postRaw(DORMRequest request) async
This method sends a POST request to the DORM API.
It takes a single parameter, request, which is an instance of the DORMRequest
class.
It returns a Future<Response>
object representing the raw response from the server.
If an error occurs while making the request, a DROMServerException
is thrown with an appropriate error message.
Future<DORMResponse> post(DORMRequest request) async
This method sends a POST request to the DORM API and returns a DORMResponse
object.
It takes a single parameter, request, which is an instance of the DORMRequest
class.
If the server responds with an error message, a DROMServerException
is thrown with an appropriate error message.
If an error occurs while parsing the response, a DROMClientException
is thrown with an appropriate error message.
This class represents a request containing multiple jobs that can be sent to a database using the DORM library. A DORMRequest
consists of one or more DORMJobs
.
jobs
: A List ofDORMJobs
add(DORMJob job)
: Adds aDORMJob
to the jobs list and returns theDORMRequest
object.addRead({from, columns, where, join})
: Creates a newDORMRead
object with the provided parameters, adds it to the jobs list, and returns theDORMRequest
object.addInsert({from, values, before})
: Creates a newDORMInsert
object with the provided parameters, adds it to the jobs list, and returns theDORMRequest
object.addUpdate({from, values, where})
: Creates a newDORMUpdate
object with the provided parameters, adds it to the jobs list, and returns theDORMRequest
object.toJson()
: Converts the jobs list to a List ofMap
and returns it.
This class represents a read job to be executed on a database.
columns
: An optional List ofDORMColumn
objects that specifies which columns to include in the result.where
: An optional List ofDORMWhere
objects that specifies the conditions to filter the result.join
: An optionalDORMJoin
object that specifies a join to perform.op
: An optional String that specifies an 'OR', 'AND' or 'BETWEEN' operator.val1
: An optional String that specifies the first value for ranges like 'BETWEEN'.val2
: An optional String that specifies the second value for ranges like 'BETWEEN'.
toJson()
: Converts the job to aMap
and returns it.
This class represents an update job to be executed on a database.
values
: A List ofDORMValue
objects that specifies the values to update.where
: An optional List ofDORMWhere
objects that specifies the conditions to filter the rows to update.
toJson()
: Converts the job to aMap
and returns it.
This class represents a delete job to be executed on a database.
where
: An optional List ofDORMWhere
objects that specifies the conditions to filter the rows to delete.
toJson()
: Converts the job to aMap
and returns it.
This class represents a column in a DORMRead
job.
column
: A String that specifies the name of the column.
toJson()
: Converts the column to aMap
and returns it.
The DORMWhere
class represents a condition to filter the results of a query.
column
: A string that represents the name of the column to compare.value
: A value that represents the value to compare against.condition
: A string that represents the condition to use in the comparison.
toJson()
: Converts the condition to aMap
and returns it.
The DORMValue
class represents a value to insert or update in a table.
columnName
: the name of the column to insert or update.value
: the value to insert or update.
toJson()
: Converts the value to aMap
and returns it.
The DORMBefore
class represents a before operation to perform before inserting a new row into a table.
lastInsertId
: An object of typeDORMLastInsertId
that stores the value of the last inserted row ID in the database.
toJson()
: Converts the operation to aMap
and returns it.
This class represents the last insert ID in a database table. It contains information about the table from which the ID was generated and the column in that table where it was set.
fromTable
: the name of the table from which the last insert ID was generated.setColumn
: the name of the column in the table where the last insert ID was set.
toJson()
: Converts the ID to aMap
and returns it.
The DORMJoin
class represents a join operation in the database. It contains a map of column names and their corresponding values to be used in the join query.
jobsColumns
: A map of column names and their corresponding values to be used in the join query.
toJson()
: Converts the operation to aMap
and returns it.
Please support the development of this client as well the DORM itself.