Skip to content

Commit

Permalink
custom client
Browse files Browse the repository at this point in the history
  • Loading branch information
david-plugge committed Jan 18, 2024
1 parent e58d227 commit 2ebb944
Show file tree
Hide file tree
Showing 14 changed files with 1,031 additions and 759 deletions.
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

208 changes: 78 additions & 130 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,110 +36,91 @@ import PocketBase from 'pocketbase';
import { TypedPocketBase } from 'typed-pocketbase';
import { Schema } from './Database';

const db = new PocketBase('http://localhost:8090') as TypedPocketBase<Schema>;
const db = new TypedPocketBase<Schema>('http://localhost:8090');
```

Enjoy full type-safety:

```ts
import { createOptions, neq, sort } from 'typed-pocketbase';
import { neq } from 'typed-pocketbase';

db.collection('posts').getList(1, 10, createOptions({
db.from('posts').getFullList({
select: {
id: true,
title: true,
content: true
content: true,
expand: {
owner: {
username: true
}
}
}
sort: '-date',
filter: neq('content', '')
}));
});
```

Supported methods

- getFullList
- getList
- getFirstListItem
- getOne
- create
- update
- subscribe

## Selecting fields

```ts
import { createOptions } from 'typed-pocketbase';

db.collection('posts').getFullList(
createOptions({
select: {
id: showId,
title: true,
content: true
}
})
);
const showId = Math.random() < 0.5;

db.from('posts').getFullList({
select: {
id: showId,
title: true,
content: true
}
});
```

## Filtering columns

Use the `and`, `or` and other utility functions to filter rows:

```ts
import { createOptions, and, or, eq } from 'typed-pocketbase';
import { and, or, eq, gte, lt } from 'typed-pocketbase';

// get all posts created in 2022
db.collection('posts').getFullList(
createOptions({
// a "manual" filter is a tuple of length 3
filter: and(['date', '<', '2023-01-01'], ['data', '>=', '2022-01-01'])
})
);
db.from('posts').getFullList({
// a "manual" filter is a tuple of length 3
filter: and(['date', '<', '2023-01-01'], ['data', '>=', '2022-01-01'])
});

// get all posts expect for those created in 2022
db.collection('posts').getFullList(
createOptions({
filter: or(['date', '>=', '2023-01-01'], ['data', '<', '2022-01-01'])
})
);
db.from('posts').getFullList({
filter: or(['date', '>=', '2023-01-01'], ['data', '<', '2022-01-01'])
});

// get all posts that were create at '2023-01-01'
db.collection('posts').getFullList(
createOptions({
filter: eq('date', '2023-01-01')
})
);
db.from('posts').getFullList({ filter: eq('date', '2023-01-01') });

// combine or/and with helpers and manual filters
db.collection('posts').getFullList(
createOptions({
filter: or(
//
['date', '>=', '2023-01-01'],
lt('date', '2022-01-01')
)
})
);
db.from('posts').getFullList({
filter: or(
//
['date', '>=', '2023-01-01'],
lt('date', '2022-01-01')
)
});

// conditionally filter rows
// falsy values are excluded
db.collection('posts').getFullList(
createOptions({
filter: and(
//
gte('date', '2022-01-01'),
!untilNow && lt('date', '2023-01-01')
)
})
);
db.from('posts').getFullList({
filter: and(
//
gte('date', '2022-01-01'),
!untilNow && lt('date', '2023-01-01')
)
});

// filter for columns in relations
// works up to 6 levels deep, including the top level
db.collection('posts').getFullList(
createOptions({
filter: eq('owner.name', 'me')
})
);
db.from('posts').getFullList({
filter: eq('owner.name', 'me')
});
```

Most filter operators are available as short hand function.
Expand All @@ -148,109 +129,76 @@ Visit the [pocketbase documentation](https://pocketbase.io/docs/api-records/) to

## Sorting rows

Use `sort`, `asc` and `desc` to sort the rows:

```ts
import { createOptions, sort, asc, desc } from 'typed-pocketbase';

db.collection('posts').getFullList(
createOptions({
// sort by descending 'date'
sort: desc('date')
})
);

db.collection('posts').getFullList(
createOptions({
// sort by descending 'date' and ascending 'title'
sort: sort('-date', '+title')
})
);

db.collection('posts').getFullList(
createOptions({
// sort by descending 'date' and ascending 'title'
sort: sort(desc('date'), asc('title'))
})
);

// you can mix functions with +/- prefixes
db.collection('posts').getFullList(
createOptions({
// sort by descending 'date' and ascending 'title'
sort: sort(desc('date'), '+title')
})
);
db.from('posts').getFullList({
// sort by descending 'date'
sort: '-date'
});

db.from('posts').getFullList({
// sort by descending 'date' and ascending 'title'
sort: ['-date', '+title']
});

// conditionally sort rows
// falsy values are excluded
db.collection('posts').getFullList(
createOptions({
sort: sort(
//
desc('date'),
sortTitle && asc('title')
)
})
);
db.from('posts').getFullList({
sort: ['-date', sortTitle && '+title']
});
```

## Expanding

The `createOptions` function automatically expands your models:

```ts
import { createOptions } from 'typed-pocketbase';

db.collection('posts').getFullList(
createOptions({
$expand: {
db.from('posts').getFullList({
select: {
expand: {
user: true
}
})
);
}
});

// select nested columns
db.collection('posts').getFullList(
createOptions({
$expand: {
db.from('posts').getFullList({
select: {
expand: {
user: {
name: true
avatar: true
}
}
})
);
}
});

// nested expand
db.collection('posts').getFullList(
createOptions({
$expand: {
db.from('posts').getFullList({
select:{
expand: {
user: {
$expand: {
expand: {
profile: true
}
}
}
})
);
}
});
```

[Back relation expanding](https://pocketbase.io/docs/working-with-relations/#back-relation-expand) is support aswell:

```ts
import { createOptions } from 'typed-pocketbase';

db.collection('user').getFullList(
createOptions({
$expand: {
db.from('user').getFullList({
select: {
expand: {
'posts(user)': {
title: true,
created: true
}
}
})
);
}
});
```

## License
Expand Down
Loading

0 comments on commit 2ebb944

Please sign in to comment.