This is a wrapper around @supabase/supabase-js that generates CRUD actions (like Prisma) to manage tables' data.
yarn add @supabase/supabase-js supabase-js-crud
import {createClient} from '@supabase/supabase-js';
const supabase = createClient('https://xyzcompany.supabase.co', 'public-anon-key');
import {DBTable} from 'supabase-js-crud';
const db = {
shop: new DBTable(supabase, 'shop'),
product: new DBTable(supabase, 'product'),
};
const shops = await db.shop.find();
const oneProduct = await db.product.findOne({where: {id: '123456789'}});
create: (params: CreateParams<T>, options?: DBTableMethodOptions) => Promise<T[] | null>;
createOne: (params: CreateOneParams<T>, options?: DBTableMethodOptions) => Promise<T | null>;
find: (params?: FindParams<T>, options?: DBTableMethodOptions) => Promise<T[]>;
findOne: (params?: FindOneParams<T>, options?: DBTableMethodOptions) => Promise<T | null>;
update: (params: UpdateParams<T>, options?: DBTableMethodOptions) => Promise<T[] | null>;
updateOne: (params: UpdateOneParams<T>, options?: DBTableMethodOptions) => Promise<T | null>;
delete: (params: DeleteParams<T>, options?: DBTableMethodOptions) => Promise<T[] | null>;
count: (params?: CountParams<T>, options?: DBTableMethodOptions) => Promise<number>;
TODO: add more detailed description for params
and options
types. For now, you can check them in types and they are mostly intuitive.
registerActions(...)
lets you register handlers for global actions such as onError
that might be useful if you develop separate API server.import {registerActions} from 'supabase-js-crud/dist/help';
registerActions({
onError: error => {
throw new InternalServerErrorException(error);
},
});
registerConstants(...)
lets you register global constants which are used in CRUD actions such as default value for take
. By default, it's 25.import {registerConstants} from 'supabase-js-crud/dist/help';
registerConstants({
take: 50,
});
You can also provide a model class to DBTable
so IDEs will help with autocompletion.
type Shop = {
id: string;
created_at: Date;
updated_at: Date;
name?: string;
};
const shopTable = new DBTable<Shop>(supabase, 'shop');
const oneShop = await shopTable.findOne({where: {name: {like: '%Supa%'}}});
For types generation, check out Supabase docs.
SupaShop
:await db.shop.createOne({data: {name: 'SupaShop'}});
await db.product.find({
where: {status: 'ACTIVE'},
include: ['*', 'shop:shop_id (id, name, status)'],
});
Bags
ordered by created_at
(descending):await db.product.find({
where: {status: 'ACTIVE'},
include: ['*', 'shop:shop_id (id, name, status)', 'category:category_id!inner (name)'],
innerWhere: {
'category.name': 'Bags',
},
order: {by: 'created_at', ascending: false},
take: 20,
});
Feel free to open an issue for suggestions as the library is in the beginning stages.
If you face any issues with the library, please, open an issue with the detailed explanation.
Credits go to the amazing team behind the awesome Supabase project!
This project is MIT licensed