Skip to content

Commit

Permalink
Support Presets operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
happy-san committed Jul 24, 2023
1 parent 464a13d commit e679309
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.4.0

* Requires Dart 3.0 or later.
* Added `Presets` support.
* Updated dependencies.

# 0.3.1

* Added `dimensions` field to support creating a vector field in schema.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Add `typesense` as a [dependency in your pubspec.yaml file](https://flutter.dev/

```@yaml
dependencies:
typesense: ^0.3.1
typesense: ^0.4.0
```

## Usage
Expand Down
2 changes: 2 additions & 0 deletions example/console-simple/bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'keys.dart' as keys;
import 'overrides.dart' as overrides;
import 'synonyms.dart' as synonyms;
import 'aliases.dart' as aliases;
import 'presets.dart' as presets;
import 'cluster_operations.dart' as cluster_operations;
import 'miscellaneous.dart' as miscellaneous;

Expand Down Expand Up @@ -57,6 +58,7 @@ void main() async {
await overrides.runExample(client);
await synonyms.runExample(client);
await aliases.runExample(client);
await presets.runExample(client);
await cluster_operations.runExample(client);
await miscellaneous.runExample(client);
}
145 changes: 145 additions & 0 deletions example/console-simple/bin/presets.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import 'package:typesense/typesense.dart';
import 'package:logging/logging.dart';

import 'util.dart';
import 'collections.dart' as collections;
import 'documents.dart' as documents;

final log = Logger('Presets');

Future<void> runExample(Client client) async {
logInfoln(log, '--Presets example--');
await init(client);
await create(client);
await search(client);
await retrieveAll(client);
await retrieve(client);
await delete(client);
await collections.delete(client);
}

final _documents = [
{
'id': '124',
'company_name': 'Stark Industries',
'num_employees': 5215,
'country': 'USA'
},
{
'id': '125',
'company_name': 'Acme Corp',
'num_employees': 1002,
'country': 'France'
},
{
'id': '127',
'company_name': 'Stark Corp',
'num_employees': 1031,
'country': 'USA'
},
{
'id': '126',
'company_name': 'Doofenshmirtz Inc',
'num_employees': 2,
'country': 'Tri-State Area'
}
];

Future<void> init(Client client) async {
await documents.init(client);
await documents.importDocs(client, 'companies', _documents);
}

Future<void> create(Client client) async {
try {
logInfoln(log, 'Creating preset "country_filter_preset".');
log.fine(
await client.presets.upsert('country_filter_preset', {
'value': {
'filter_by': 'country:=USA',
}
}),
);

await writePropagationDelay();
} on RequestException catch (e, stackTrace) {
log.severe(e.message, e, stackTrace);
} catch (e, stackTrace) {
log.severe(e, stackTrace);
}
}

Future<void> search(Client client) async {
try {
logInfoln(log, 'Simple search.');
log.fine(
await client.collection('companies').documents.search(
{
'q': 'Corp',
'query_by': 'company_name',
},
),
);

logInfoln(log, 'Search with filter_by.');
log.fine(
await client.collection('companies').documents.search(
{
'q': 'Corp',
'query_by': 'company_name',
'filter_by': 'country:=USA',
},
),
);

logInfoln(log, 'Search using preset.');
log.fine(
await client.collection('companies').documents.search(
{
'q': 'Corp',
'query_by': 'company_name',
'preset': 'country_filter_preset',
},
),
);
} on RequestException catch (e, stackTrace) {
log.severe(e.message, e, stackTrace);
} catch (e, stackTrace) {
log.severe(e, stackTrace);
}
}

Future<void> retrieveAll(Client client) async {
try {
logInfoln(log, 'Retrieving all presets.');
log.fine(await client.presets.retrieve());
} on RequestException catch (e, stackTrace) {
log.severe(e.message, e, stackTrace);
} catch (e, stackTrace) {
log.severe(e, stackTrace);
}
}

Future<void> retrieve(Client client) async {
try {
logInfoln(log, 'Retrieving preset "country_filter_preset".');
log.fine(await client.preset('country_filter_preset').retrieve());
} on RequestException catch (e, stackTrace) {
log.severe(e.message, e, stackTrace);
} catch (e, stackTrace) {
log.severe(e, stackTrace);
}
}

Future<void> delete(Client client) async {
try {
logInfoln(log, 'Deleting preset "country_filter_preset".');
log.fine(await client.preset('country_filter_preset').delete());

await writePropagationDelay();
} on RequestException catch (e, stackTrace) {
log.severe(e.message, e, stackTrace);
} catch (e, stackTrace) {
log.severe(e, stackTrace);
}
}
53 changes: 34 additions & 19 deletions example/console-simple/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,105 +5,120 @@ packages:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
sha256: db4766341bd8ecb66556f31ab891a5d596ef829221993531bd64a8e6342f0cda
url: "https://pub.dev"
source: hosted
version: "2.8.2"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
sha256: fb98c0f6d12c920a02ee2d998da788bca066ca5f148492b7085ee23372b12306
url: "https://pub.dev"
source: hosted
version: "1.3.1"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
sha256: "6d4193120997ecfd09acf0e313f13dc122b119e5eca87ef57a7d065ec9183762"
url: "https://pub.dev"
source: hosted
version: "1.15.0"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab
url: "https://pub.dev"
source: hosted
version: "3.0.2"
version: "3.0.3"
flutter_lints:
dependency: "direct dev"
description:
name: flutter_lints
url: "https://pub.dartlang.org"
sha256: b543301ad291598523947dc534aaddc5aaad597b709d2426d3a0e0d44c5cb493
url: "https://pub.dev"
source: hosted
version: "1.0.4"
http:
dependency: transitive
description:
name: http
url: "https://pub.dartlang.org"
sha256: "759d1a329847dd0f39226c688d3e06a6b8679668e350e2891a6474f8b4bb8525"
url: "https://pub.dev"
source: hosted
version: "0.13.4"
version: "1.1.0"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
sha256: e362d639ba3bc07d5a71faebb98cde68c05bfbcfbbb444b60b6f60bb67719185
url: "https://pub.dev"
source: hosted
version: "4.0.0"
lints:
dependency: transitive
description:
name: lints
url: "https://pub.dartlang.org"
sha256: a2c3d198cb5ea2e179926622d433331d8b58374ab8f29cdda6e863bd62fd369c
url: "https://pub.dev"
source: hosted
version: "1.0.1"
logging:
dependency: "direct main"
description:
name: logging
url: "https://pub.dartlang.org"
sha256: "0520a4826042a8a5d09ddd4755623a50d37ee536d79a70452aff8c8ad7bb6c27"
url: "https://pub.dev"
source: hosted
version: "1.0.1"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
sha256: "5202fdd37b4da5fd14a237ed0a01cad6c1efd4c99b5b5a0d3c9237f3728c9485"
url: "https://pub.dev"
source: hosted
version: "1.7.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
sha256: "2ad4cddff7f5cc0e2d13069f2a3f7a73ca18f66abd6f5ecf215219cdb3638edb"
url: "https://pub.dev"
source: hosted
version: "1.8.0"
source_span:
dependency: transitive
description:
name: source_span
url: "https://pub.dartlang.org"
sha256: d5f89a9e52b36240a80282b3dc0667dd36e53459717bb17b8fb102d30496606a
url: "https://pub.dev"
source: hosted
version: "1.8.1"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
sha256: dd11571b8a03f7cadcf91ec26a77e02bfbd6bbba2a512924d3116646b4198fc4
url: "https://pub.dev"
source: hosted
version: "1.1.0"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
sha256: a88162591b02c1f3a3db3af8ce1ea2b374bd75a7bb8d5e353bcfbdc79d719830
url: "https://pub.dev"
source: hosted
version: "1.2.0"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
sha256: "53bdf7e979cfbf3e28987552fd72f637e63f3c8724c9e56d9246942dc2fa36ee"
url: "https://pub.dev"
source: hosted
version: "1.3.0"
typesense:
Expand All @@ -112,6 +127,6 @@ packages:
path: "../.."
relative: true
source: path
version: "0.3.1"
version: "0.3.2"
sdks:
dart: ">=2.17.0 <3.0.0"
dart: ">=3.0.0 <4.0.0"
15 changes: 14 additions & 1 deletion lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import 'key.dart';
import 'debug.dart';
import 'stats.dart';
import 'health.dart';
import 'preset.dart';
import 'presets.dart';
import 'metrics.dart';
import 'operations.dart';
import 'multi_search.dart';
Expand All @@ -26,6 +28,7 @@ class Client {
final Collections collections;
final Aliases aliases;
final Keys keys;
final Presets presets;
final Debug debug;
final Stats stats;
final Health health;
Expand All @@ -34,7 +37,8 @@ class Client {
final MultiSearch multiSearch;
final _individualCollections = HashMap<String, Collection>(),
_individualAliases = HashMap<String, Alias>(),
_individualKeys = HashMap<int, Key>();
_individualKeys = HashMap<int, Key>(),
_individualPresets = HashMap<String, Preset>();

Client._(
this.config,
Expand All @@ -43,6 +47,7 @@ class Client {
this.collections,
this.aliases,
this.keys,
this.presets,
this.debug,
this.stats,
this.health,
Expand All @@ -68,6 +73,7 @@ class Client {
Collections(apiCall, CollectionsApiCall(config, nodePool)),
Aliases(apiCall),
Keys(apiCall),
Presets(apiCall),
Debug(apiCall),
Stats(apiCall),
Health(apiCall),
Expand Down Expand Up @@ -100,4 +106,11 @@ class Client {
}
return _individualKeys[id]!;
}

Preset preset(String presetName) {
if (!_individualPresets.containsKey(presetName)) {
_individualPresets[presetName] = Preset(presetName, _apiCall);
}
return _individualPresets[presetName]!;
}
}
Loading

0 comments on commit e679309

Please sign in to comment.