leo 1.0.3 copy "leo: ^1.0.3" to clipboard
leo: ^1.0.3 copied to clipboard

outdated

A set of general utilities for the server

Leo #

Server utilities to fast track backend development with dart

It is recommended to use the as keyword with the import statement when importing the package to prevent name conflicts, for example...

import 'package:leo/leo.dart' as leo

To quickly put a block of code in a try catch.


// the argument for tryCatch could either be a callback to a function or a future

// as a function
Map<String, dynamic> connectToServer(){
	// may fail to reach the server
}
var results = await leo.tryCatch(connectToServer);

// as a future
Future<Map<String, dynamic>> connectToServer() async {
	// may fail to reach the server
}

var future = connectToServer();
var results = await leo.tryCatch(future);


if(results != null){
	// proceed
}

To log output to file

await leo.log('data to log', logFile: 'path/to/file', clear: true, time: true);

To generate a random ID

var id = generateUUID(length: 30);

To execute a query on postgres

var dbAuth = {
	'host': 'localhost',
	'port': 5432,
	'db': 'db-name',
	'username': 'user',
	'password': 'pass'
};
var results = await leo.DB(dbAuth).query('SELECT * FROM table');

Leo has an ORM to help with getting and updating documents from the db (postgres) We will be working with a postgres db whose table's schema looks like this

CREATE TABLE IF NOT EXISTS names(
	id SERIAL PRIMARY KEY,
	firstname TEXT,
	lastname TEXT,
	bio TEXT,
	age TEXT,
	dob TEXT
);
// to fetch all documents
var table = 'names';
var columns = '*';
var documents = await leo.ORM(dbAuth).get(table, columns);
// to fetch specific documents
var whereClause = {
	'firstname': 'john'
};
var documents = await leo.ORM(dbAuth).get(table, columns, values: whereClause);
// to insert
var table = 'names'
var data = {
	'firstname': 'john',
	'lastname': 'doe'
};

var isInserted = await leo.ORM(dbAuth).insert(table, data);
// to update
var table = 'names';
var toChange = <String, dynamic>{
	'firstname':'james'
};
var whereClause = <String, dynamic>{
	'id': 1,
};

var isUpdated = await leo.ORM(dbAuth).update(table, toChange, whereClause);
// to add columns
var table = 'names';
var columns = <Map<String, dynamic>>[
	<String, dynamic>{
		'name': 'middlename',
		'type': 'TEXT',
		'constraints': <String>[
			'UNIQUE',
		]
	},

	<String, dynamic>{
		'name': 'sirname',
		'type': 'TEXT',
		'constraints': <String>[
			'UNIQUE',
		]
	},
];

var isAddedColumns = await leo.ORM(dbAuth, verbose: true).alter(table, columns, command: 'ADD');
// to drop columns
var columns = <Map<String, dynamic>>[{'name': 'middlename'}, {'name': 'sirname'}];
var isDropped = await leo.ORM(dbAuth, verbose: true).alter(table, columns, command: 'DROP');
// to delete
var table = 'names';
var whereClause = {
	'id': 1,
};

var isDeleted = await leo.ORM(dbAuth).delete(table, whereClause);

To run complex queries use leo.DB(dbAuth).query method instead of the orm

To get a random index between a range of indexes

var index = leo.getRandomNumber(min: 10, max: 10000);

To output info on screen with different colors


leo.pretifyOutput('to print on screen'); // will print in green
leo.pretifyOutput('to print on screen', color: 'red');

To start an isolate

var isolatateName = 'test'
var callback = (){ print('running this isolate')  return 'testing'; };
var onListenCallback = (data){
	print(data);
}
var isolateInfo = await initIsolate(isolateName, callback, onListenCallback: onListenCallback, verbose: true);

// isolateInfo returns a map with the isolate instance, receiver and the sendPort

To distribute work evenly across workers

var workload = [
	'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'
];

var howManyParts = 3;
var workloadListing = leo.fractionate(workload, howManyParts);

To create a file quickly (will also create the recursive directories on the path)

await leo.createFile('/path/to/file');

// to clear a file/truncate a file
await leo.createFile('/path/to/file', clear: true);

To create a server. The server is suited for APIs

// extend the server class
class MyServer extends leo.Server {

	@override
	final String header = '[server name]';

	@override
	final String ip = 'localhost';

	@override
	final int port = 8080;

	@override
	final String color = 'cyan';

	@override
	final String logFile = '/path/to/log';

	@override
	final Map<String, leo.RequestHandler> routes = {
		'/test': Test(), // this Test is defined below
	};

}

class Test extends leo.RequestHandler {

	@override
	Future<Map<String, dynamic>> Get([route, data]) async {

		// to access get parameters, do this
		// let's say the get request from the client is
		// http://localhost/test?key=one
		
		// to access the key value

		var key = route.req.uri.queryParameters['key'];

		var backToClient = <String, dynamic>{
			'isSuccessful': false,
		};
		
		return backToClient;
	}

	@override
	Future<Map<String, dynamic>> Post([route, data]) async {
		var backToClient = <String, dynamic>{
			'isSuccessful': false,
		};

		return backToClient;
	}

}
5
likes
0
pub points
0%
popularity

Publisher

unverified uploader

A set of general utilities for the server

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

colorize, http, intl, postgres, uuid

More

Packages that depend on leo