Getting started with Serverpod mini

Serverpod
Serverpod
Published in
3 min readFeb 22, 2024

A common feature request from our community has been to have a more lightweight version of Serverpod without any requirements for external dependencies. Meet Serverpod mini, a slimmer version of Serverpod that does not need to be connected to a Postgres database. Serverpod mini is part of the Serverpod 2.0 release.

Serverpod mini

Installing Serverpod mini

To try out Serverpod mini, install our tooling by running:

dart pub global activate serverpod_cli

Now, you can create a mini project by running the following:

serverpod create myminipod --mini

Serverpod will create a new project for you. It contains three Dart packages, but you only need to pay attention to the myminipod_server and myminipod_flutter directories. The server directory contains your server files, and the flutter directory contains your app. The third package (myminipod_client) contains generated code that is used by the Flutter app to communicate with the server.

Start your server by changing directory into your server directory, and run the bin/main.dart file:

cd myminipod/myminipod_server
dart bin/main.dart

Your default project comes with a sample Flutter app, all hooked up to talk with your server. Run it with the flutter command:

cd myminipod/myminipod_flutter
flutter run -d chrome

Easy as that.

Bonus tip: If you are using VS Code, install our Serverpod extension. It will help you validate any Serverpod-related files in your project!

Creating models

In Serverpod, you define your models in easy-to-read YAML-files, which you place in your server’s lib/src/models directory. Model files will be converted to Dart classes that can be serialized and sent to and from the server to your app. This is an example of a model file:

class: Company
fields:
name: String
foundedDate: DateTime?
employees: List<String>

For types, you can use most basic Dart types, such as String, double, int, bool, DateTime, and ByteData. You can also include List and Map, just make sure to specify their types. Any other class specified among your models is also supported.

Whenever you add or edit a model file, run serverpod generate in your server directory. Then, Serverpod will generate all the updated Dart classes:

cd myminipod/myminipod_server
serverpod generate

Adding methods to your server

With Serverpod, you add Dart methods to endpoints placed in your server’s lib/src/endpoints directory. By doing so, Serverpod will analyze your server code and automatically generate the corresponding methods in your Flutter app. Calling a method on the server is just like calling a local method in your app.

For the server methods to work, there are a few things you need to keep in mind:

  • You must place the methods in a class that extends the Endpoint class.
  • The methods must return a typed Future. The types you use in your methods are the same as those supported by your models.
  • The first parameter of your method must be a Session object. The session contains extra information about the call being made to the server, such as the HTTP request object.

This is an example of an endpoint that uses the Company class that we defined in the example model in the previous section.

import 'package:serverpod/serverpod.dart';

class CompanyEndpoint extends Endpoint {
Future<bool> isLegit(Session session, Company company) async {
// Check if the company has the foundedDate set and that it
// has been around for more than one year.

if (company.foundedDate == null) {
return false;
}

var oneYearAgo = DateTime.now().subract(Duration(days: 365));
return company.foundedDate!.isBefore(oneYearAgo);
}
}

After adding or modifying endpoints and endpoint methods, you must run serverpod generate to keep your Flutter app up-to-date.

cd myminipod/myminipod_server
serverpod generate

Calling the server methods from the app

When you run serverpod generate Serverpod will add your endpoints and server methods to the client object in your Flutter app. From the client, you can access all endpoints and methods.

To call the endpoint method we just created from Flutter, just create a Company object, call the method, and await the result:

var company = Company(
name: 'Serverpod',
foundedDate: DateTime(2021, 9, 27),
employees: [
'Alex',
'Isak',
'Viktor',
],
);

var result = await client.company.isLegit(company);

Conclusion

You are now ready to start exploring the exciting world of Serverpod! And even if you start out with Serverpod mini, you can always upgrade to the full version later.

--

--