Giter VIP home page Giter VIP logo

dart-sdk's Introduction

PocketBase Dart SDK Pub Package

Official Multi-platform Dart SDK for interacting with the PocketBase Web API.

Installation

Add the library to your dependencies:

dart pub add pocketbase

# or with Flutter:
flutter pub add pocketbase

Import it in your Dart code:

import 'package:pocketbase/pocketbase.dart';

final pb = PocketBase('http://127.0.0.1:8090');

...

// authenticate as regular user
final userData = await pb.collection('users').authWithPassword('[email protected]', '123456');

// list and filter "example" collection records
final result = await pb.collection('example').getList(
  page:    1,
  perPage: 20,
  filter:  'status = true && created >= "2022-08-01"',
  sort:    '-created',
  expand:  'someRelField',
);

// subscribe to realtime "example" collection changes
pb.collection('example').subscribe("*", (e) {
  print(e.action); // create, update, delete
  print(e.record); // the changed record
});

// and much more...

More detailed API docs and copy-paste examples could be found in the API documentation for each service or in the Services section below.

Caveats

File upload

PocketBase Dart SDK handles file upload seamlessly by using http.MultipartFile list.

Here is a simple example of uploading a single text file together with some other regular fields:

import 'package:http/http.dart' as http;
import 'package:pocketbase/pocketbase.dart';

final pb = PocketBase('http://127.0.0.1:8090');

pb.collection('example').create(
  body: {
    'title': 'Hello world!',
    // ... any other regular field
  },
  files: [
    http.MultipartFile.fromString(
      'document', // the name of the file field
      'example content...',
      filename: 'example_document.txt',
    ),
  ],
).then((record) {
  print(record.id);
  print(record.getStringValue('title'));
});

Accessing RecordModel dynamic fields

The SDK comes with several helpers to make it easier working with the RecordService and RecordModel DTO. You could find more detailed documentation in the RecordModel class reference, but here are some examples:

final record = await pb.collection('example').getOne('RECORD_ID');

...

final email   = record.getStringValue('email');
final options = record.getListValue<String>('options');
final status  = record.getBoolValue('status');
final total   = record.getIntValue('total');
final price   = record.getDoubleValue('price');

Alternatively, you can also create your own typed DTO data classes and use for example the record.toJson() to populate your object, eg:

import "package:pocketbase/pocketbase.dart";
import 'package:json_annotation/json_annotation.dart';

part 'task.g.dart';

@JsonSerializable()
class Task {
  Task({this.id = '', this.description = ''});

  // type the collection fields you want to use...
  final String id;
  final String description;

  /// Creates a new Task instance form the provided RecordModel.
  factory Task.fromRecord(RecordModel record) => Task.fromJson(record.toJson());

  /// Connect the generated [_$Task] function to the `fromJson` factory.
  factory Task.fromJson(Map<String, dynamic> json) => _$Task(json);

  /// Connect the generated [_$Task] function to the `toJson` method.
  Map<String, dynamic> toJson() => _$Task(this);
}

...

// fetch your raw record model
final record = await pb.collection('tasks').getOne('TASK_ID');

final task = Task.fromRecord(record);

Error handling

All services return a standard Future-based response, so the error handling is straightforward:

pb.collection('example').getList(page: 1, perPage: 50).then((result) {
  // success...
  print('Result: $result');
}).catchError((error) {
  // error...
  print('Error: $error');
});

// OR if you are using the async/await syntax:
try {
  final result = await pb.collection('example').getList(page: 1, perPage: 50);
} catch (error) {
  print('Error: $error');
}

All response errors are normalized and wrapped as ClientException with the following public members that you could use:

ClientException {
    url            Uri     // The address of the failed request
    statusCode     int     // The status code of the failed request
    response       Map     // The JSON API error response
    isAbort        bool    // Indicates whether the request was aborted/cancelled
    originalError  dynamic // The original response error
}

AuthStore

The SDK keeps track of the authenticated token and auth model for you via the pb.authStore service. The default AuthStore class has the following public members that you could use:

AuthStore {
    token:    String                      // Getter for the stored auth token
    model:    RecordModel|AdminModel|null // Getter for the stored auth RecordModel or AdminModel
    isValid   bool                        // Getter to loosely check if the store has an existing and unexpired token
    onChange  Stream                      // Stream that gets triggered on each auth store change

    // methods
    save(token, model)             // update the store with the new auth data
    clear()                        // clears the current auth store state
}

To "logout" an authenticated record or admin, you can just call pb.authStore.clear().

To "listen" for changes in the auth store, you can listen to the onChange broadcast stream:

pb.authStore.onChange.listen((e) {
  print(e.token);
  print(e.model);
});

If you want to customize the default AuthStore, you can extend it and pass a new custom instance as constructor argument to the client:

class CustomAuthStore extends AuthStore {
  ...
}

final pb = PocketBase('http://127.0.0.1:8090', authStore: CustomAuthStore());

Please note that at the moment the default AuthStore is not persistent! Built-in persistent store for Flutter apps may get added in the future, but for now you can check #13 (or pocketbase#1887 for examples).

Services

Crud handlers
// Returns a paginated records list.
๐Ÿ”“ pb.collection(collectionIdOrName).getList({page = 1, perPage = 30, filter?, sort?, expand?, query, headers});

// Returns a list with all records batch fetched at once.
๐Ÿ”“ pb.collection(collectionIdOrName).getFullList({batch = 100, filter?, sort?, expand?, query, headers});

// Returns the first found record matching the specified filter.
๐Ÿ”“ pb.collection(collectionIdOrName).getFirstListItem(filter, {expand?, query, headers});

// Returns a single record by its id.
๐Ÿ”“ pb.collection(collectionIdOrName).getOne(recordId, {expand?, query, headers});

// Creates (aka. register) a new record.
๐Ÿ”“ pb.collection(collectionIdOrName).create({body, files, expand?, query, headers});

// Updates an existing record by its id.
๐Ÿ”“ pb.collection(collectionIdOrName).update(recordId, {body, files, expand?, query, headers});

// Deletes a single record by its id.
๐Ÿ”“ pb.collection(collectionIdOrName).delete(recordId, {query, body, headers});
Realtime handlers
// Subscribe to realtime changes to the specified topic ("*" or recordId).
//
// It is safe to subscribe multiple times to the same topic.
//
// You can use the returned UnsubscribeFunc to remove a single registered subscription.
// If you want to remove all subscriptions related to the topic use unsubscribe(topic).
๐Ÿ”“ pb.collection(collectionIdOrName).subscribe(topic, callback);

// Unsubscribe from all registered subscriptions to the specified topic ("*" or recordId).
// If topic is not set, then it will remove all registered collection subscriptions.
๐Ÿ”“ pb.collection(collectionIdOrName).unsubscribe([topic]);
Auth handlers

Available only for "auth" type collections.

// Returns all available application auth methods.
๐Ÿ”“ pb.collection(collectionIdOrName).listAuthMethods({query, headers});

// Authenticates a record with their username/email and password.
๐Ÿ”“ pb.collection(collectionIdOrName).authWithPassword(usernameOrEmail, password, {expand?, query, body, headers});

// Authenticates a record with OAuth2 provider without custom redirects, deeplinks or even page reload.
๐Ÿ”“ pb.collection(collectionIdOrName).authWithOAuth2(provider, urlCallback {scopes, createData, expand?});

// Authenticates a record with OAuth2 code.
๐Ÿ”“ pb.collection(collectionIdOrName).authWithOAuth2Code(provider, code, codeVerifier, redirectUrl, {createData?, expand?, query, body, headers});

// Refreshes the current authenticated record model and auth token.
๐Ÿ” pb.collection(collectionIdOrName).authRefresh({expand?, query, body, headers});

// Sends a user password reset email.
๐Ÿ”“ pb.collection(collectionIdOrName).requestPasswordReset(email, {query, body, headers});

// Confirms a record password reset request.
๐Ÿ”“ pb.collection(collectionIdOrName).confirmPasswordReset(resetToken, newPassword, newPasswordConfirm, {expand?, query, body, headers});

// Sends a record verification email request.
๐Ÿ”“ pb.collection(collectionIdOrName).requestVerification(email, {query, body, headers});

// Confirms a record email verification request.
๐Ÿ”“ pb.collection(collectionIdOrName).confirmVerification(verificationToken, {expand?, query, body, headers});

// Sends a record email change request to the provider email.
๐Ÿ” pb.collection(collectionIdOrName).requestEmailChange(newEmail, {query, body, headers});

// Confirms record new email address.
๐Ÿ”“ pb.collection(collectionIdOrName).confirmEmailChange(emailChangeToken, userPassword, {expand?, query, body, headers});

// Lists all linked external auth providers for the specified record.
๐Ÿ” pb.collection(collectionIdOrName).listExternalAuths(recordId, {query, headers});

// Unlinks a single external auth provider relation from the specified record.
๐Ÿ” pb.collection(collectionIdOrName).unlinkExternalAuth(recordId, provider, {query, body headers});

FileService
// Builds and returns an absolute record file url for the provided filename.
๐Ÿ”“ pb.files.getUrl(record, filename, {thumb?, token?, query, body, headers});

// Requests a new private file access token for the current auth model (admin or record).
๐Ÿ” pb.files.getToken({query, body, headers});

// Authenticates an admin account by its email and password.
๐Ÿ”“ pb.admins.authWithPassword(email, password, {query, body, headers});

// Refreshes the current admin authenticated model and token.
๐Ÿ” pb.admins.authRefresh({query, body, headers});

// Sends an admin password reset email.
๐Ÿ”“ pb.admins.requestPasswordReset(email, {query, body, headers});

// Confirms an admin password reset request.
๐Ÿ”“ pb.admins.confirmPasswordReset(resetToken, newPassword, newPasswordConfirm, {query, body, headers});

// Returns a paginated admins list.
๐Ÿ” pb.admins.getList({page = 1, perPage = 30, filter?, sort?, query, headers});

// Returns a list with all admins batch fetched at once.
๐Ÿ” pb.admins.getFullList({batch = 100, filter?, sort?, query, headers});

// Returns the first found admin matching the specified filter.
๐Ÿ” pb.admins.getFirstListItem(filter, {query, headers});

// Returns a single admin by their id.
๐Ÿ” pb.admins.getOne(id, {query, headers});

// Creates a new admin.
๐Ÿ” pb.admins.create({body, files, query, headers});

// Updates an existing admin by their id.
๐Ÿ” pb.admins.update(id, {body, files, query, headers});

// Deletes a single admin by their id.
๐Ÿ” pb.admins.delete(id, {query, body, headers});

CollectionService (Detailed class reference, API docs)

// Returns a paginated collections list.
๐Ÿ” pb.collections.getList({page = 1, perPage = 30, filter?, sort?, query, headers});

// Returns a list with all collections batch fetched at once.
๐Ÿ” pb.collections.getFullList({batch = 100, filter?, sort?, query, headers});

// Returns the first found collection matching the specified filter.
๐Ÿ” pb.collections.getFirstListItem(filter, {query, headers});

// Returns a single collection by its id.
๐Ÿ” pb.collections.getOne(id, {query, headers});

// Creates (aka. register) a new collection.
๐Ÿ” pb.collections.create({body, files, query, headers});

// Updates an existing collection by its id.
๐Ÿ” pb.collections.update(id, {body, files, query, headers});

// Deletes a single collection by its id.
๐Ÿ” pb.collections.delete(id, {query, body, headers});

// Imports the provided collections.
๐Ÿ” pb.collections.import(collections, {deleteMissing=false, query, body, headers});

// Returns a paginated log requests list.
๐Ÿ” pb.logs.getRequestsList({page = 1, perPage = 30, filter?, sort?, query, headers});

// Returns a single log request by its id.
๐Ÿ” pb.logs.getRequest(id, {query, headers});

// Returns a map with all available app settings.
๐Ÿ” pb.settings.getAll({query, headers});

// Bulk updates app settings.
๐Ÿ” pb.settings.update({body, query, headers});

// Performs a S3 storage connection test.
๐Ÿ” pb.settings.testS3({body, query, headers});

// Sends a test email (verification, password-reset, email-change).
๐Ÿ” pb.settings.testEmail(toEmail, template, {body, query, headers});

// Generates a new Apple OAuth2 client secret.
๐Ÿ” pb.settings.generateAppleClientSecret(clientId, teamId, keyId, privateKey, duration, {body, query, headers});

This service is usually used with custom realtime actions. For records realtime subscriptions you can use the subscribe/unsubscribe methods available in the pb.collection() RecordService.

// Initialize the realtime connection (if not already) and register the subscription.
๐Ÿ”“ pb.realtime.subscribe(subscription, callback);

// Unsubscribe from a subscription (if empty - unsubscibe from all registered subscriptions).
๐Ÿ”“ pb.realtime.unsubscribe([subscription = '']);

// Unsubscribe from all subscriptions starting with the provided prefix.
๐Ÿ”“ pb.realtime.unsubscribeByPrefix(subscriptionsPrefix);

HealthService
// Checks the health status of the api.
๐Ÿ”“ pb.health.check({query, headers});

Limitations

PocketBase Dart SDK is built on top of the standard dart-lang/http package and inherits some of its limitations:

Depending on the users demand, we can implement workarounds for the above limitations, but it would be better to wait the upstream library to apply the necessary fixes.

Development

# run the unit tests
dart test

# view dartdoc locally
dart doc

# run the example
dart run example/example.dart

# generate the DTOs json serializable artifacts
dart run build_runner build

dart-sdk's People

Contributors

ganigeorgiev avatar irmhonde avatar rifaldhiaw avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.