Skip to content

Commit

Permalink
Updated example for version 3 of the postgres package
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Sep 7, 2024
1 parent 5f04d91 commit b2c6c0e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ Insert vectors

```dart
await connection.execute(
"INSERT INTO items (embedding) VALUES (@a), (@b), (@c)",
substitutionValues: {
Sql.named("INSERT INTO items (embedding) VALUES (@a), (@b), (@c)"),
parameters: {
"a": pgvector.encode([1, 1, 1]),
"b": pgvector.encode([2, 2, 2]),
"c": pgvector.encode([1, 1, 2])
Expand All @@ -53,14 +53,14 @@ await connection.execute(
Get the nearest neighbors

```dart
List<List<dynamic>> results = await connection.query(
"SELECT id, embedding FROM items ORDER BY embedding <-> @embedding LIMIT 5",
substitutionValues: {
List<List<dynamic>> results = await connection.execute(
Sql.named("SELECT id, embedding FROM items ORDER BY embedding <-> @embedding LIMIT 5"),
parameters: {
"embedding": pgvector.encode([1, 1, 1])
});
for (final row in results) {
print(row[0]);
print(pgvector.decode(row[1]));
print(pgvector.decode(row[1].bytes));
}
```

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ dependencies:

dev_dependencies:
lints: ^2.0.0
postgres: ^2.6.2
postgres: ^3.0.0
test: ^1.21.0
24 changes: 14 additions & 10 deletions test/postgres_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import 'package:test/test.dart';

void main() {
test('works', () async {
var connection = PostgreSQLConnection(
"localhost", 5432, "pgvector_dart_test",
username: Platform.environment["USER"]);
await connection.open();
var connection = await Connection.open(
Endpoint(
host: "localhost",
port: 5432,
database: "pgvector_dart_test",
username: Platform.environment["USER"]),
settings: ConnectionSettings(sslMode: SslMode.disable));

await connection.execute("CREATE EXTENSION IF NOT EXISTS vector");
await connection.execute("DROP TABLE IF EXISTS items");
Expand All @@ -17,21 +20,22 @@ void main() {
"CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))");

await connection.execute(
"INSERT INTO items (embedding) VALUES (@a), (@b), (@c)",
substitutionValues: {
Sql.named("INSERT INTO items (embedding) VALUES (@a), (@b), (@c)"),
parameters: {
"a": pgvector.encode([1, 1, 1]),
"b": pgvector.encode([2, 2, 2]),
"c": pgvector.encode([1, 1, 2])
});

List<List<dynamic>> results = await connection.query(
"SELECT id, embedding FROM items ORDER BY embedding <-> @embedding LIMIT 5",
substitutionValues: {
List<List<dynamic>> results = await connection.execute(
Sql.named(
"SELECT id, embedding FROM items ORDER BY embedding <-> @embedding LIMIT 5"),
parameters: {
"embedding": pgvector.encode([1, 1, 1])
});
for (final row in results) {
print(row[0]);
print(pgvector.decode(row[1]));
print(pgvector.decode(row[1].bytes));
}

await connection
Expand Down

0 comments on commit b2c6c0e

Please sign in to comment.