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

feat: FindUnique by unique attribute #1204

Open
jjranalli opened this issue Oct 30, 2024 · 3 comments
Open

feat: FindUnique by unique attribute #1204

jjranalli opened this issue Oct 30, 2024 · 3 comments

Comments

@jjranalli
Copy link

as of v.0.6.22, findUnique only accepts id as parameter. it would be more useful if any unique attribute could be used to filter. this in turn would require adding support for marking columns as unique in the schema.

relatedly, I think a findFirst would also be nice addition. my current workaround is the following

const { items } = await db.Slicer.findMany({
    where: {
      address: slicerAddress
    },
    limit: 1
})
@jjranalli jjranalli changed the title [Feature Request] FindUnique by unique attribute feat: FindUnique by unique attribute Oct 30, 2024
@typedarray
Copy link
Collaborator

This will be largely addressed in the upcoming 0.7 release, which adds support for arbitrary SQL select statements within indexing functions and a high-level findFirst API.

However, we don't plan to support SQL unique constraints (other than the primary key), so this won't be 100% solved.

@typedarray
Copy link
Collaborator

We just released 0.7 - here's the new store API. No support for non-PK unique constraints, but you can now use the raw SQL API to achieve this:

const slicer = await db.sql.query.slicers.findFirst({
  where: (slicers, { eq }) => eq(slicers.address, slicerAddress),
});

Also, curious what your slicers column types look like - a composite primary key might help here.

@jjranalli
Copy link
Author

awesome, I'll look into it! this is the current schema

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

No branches or pull requests

2 participants