Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom client #29

Merged
merged 19 commits into from
Feb 6, 2024
Merged

Custom client #29

merged 19 commits into from
Feb 6, 2024

Conversation

david-plugge
Copy link
Owner

@david-plugge david-plugge commented Jan 12, 2024

New api!

Instead of using functions to create filters and more i decided to create a custom client instead.

"Code says more than words":

import type { Schema } from '../../example/Database.js';
import { eq, or } from '../filter.js';
import { TypedPocketBase } from '../client.js';

const db = new TypedPocketBase<Schema>('http://localhost:8090');
await db.admins.authWithPassword('[email protected]', 'secretpassword');

const res = await db.from('posts').getFullList({
	select: {
		id: true,
		content: true,
		owner: true,
		collectionName: true,
		expand: {
			owner: {
				username: true,
				email: true
			}
		}
	},
	filter: or(eq('content', 'bla'), eq('published', true)),
	sort: ['+id']
});

console.log(res);

You can now create your filters, sort and select values beforehand:

import type { Schema } from '../../example/Database.js';
import { eq, or } from '../filter.js';
import { TypedPocketBase } from '../client.js';

const db = new TypedPocketBase<Schema>('http://localhost:8090');
await db.admins.authWithPassword('[email protected]', 'secretpassword');

const sort = db.from('posts').createSort('+id');

const filter = db
	.from('posts')
	.createFilter(or(eq('content', 'bla'), eq('published', true)));

const select = db.from('posts').createSelect({
	id: true,
	content: true,
	owner: true,
	collectionName: true,
	expand: {
		owner: {
			username: true,
			email: true
		}
	}
});

const res = await db.from('posts').getFullList({
	filter,
	select,
	sort
});

console.log(res);

I personally think this is the way to go. The main benefits are:

  • less imports
  • no need for expand
  • view base and auth collections are correctly typed!

@david-plugge
Copy link
Owner Author

There is an issue regarding auth responses as they are wrapped in an object. This makes typing it kind of annoying. I'll try to find a good way to solve this, but maybe i'll delay this so i can release version 0.1.0 later this week.

@JaCoB1123
Copy link

I noticed an issue with sorts and filters. It seems the npm package is somehow inconsistent:
in dist/index.js the sort code is still the old version. in src/client.ts the code is correct.

Also the assignment of filters seems to be missing in prepareOptions so filters are not doing anything right now.

@david-plugge david-plugge marked this pull request as ready for review February 6, 2024 08:42
@david-plugge david-plugge merged commit f462286 into main Feb 6, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants