Skip to content

Commit

Permalink
Initial file storage support
Browse files Browse the repository at this point in the history
  • Loading branch information
kav committed Feb 20, 2024
1 parent 3398447 commit a471c0e
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions packages/ra-supabase-core/src/dataProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataProvider, fetchUtils } from 'ra-core';
import { DataProvider, fetchUtils, withLifecycleCallbacks } from 'ra-core';
import postgrestRestProvider, {
IDataProviderConfig,
defaultPrimaryKeys,
Expand All @@ -24,10 +24,19 @@ export const supabaseDataProvider = ({
defaultListOp = 'eq',
primaryKeys = defaultPrimaryKeys,
schema = defaultSchema,
storagePath,
filenameFromData = ({ filename }) => filename,
}: {
instanceUrl: string;
apiKey: string;
supabaseClient: SupabaseClient;
storagePath?: string | ((resource: string) => string);
filenameFromData?: (_: {
data: string;
resource: string;
field: string;
filename: string;
}) => string;
} & Partial<Omit<IDataProviderConfig, 'apiUrl'>>): DataProvider => {
const config: IDataProviderConfig = {
apiUrl: `${instanceUrl}/rest/v1`,
Expand All @@ -36,7 +45,50 @@ export const supabaseDataProvider = ({
primaryKeys,
schema,
};
return postgrestRestProvider(config);
return withLifecycleCallbacks(postgrestRestProvider(config), [
{
resource: '*',
beforeSave: async (
data: any,
dataProvider: DataProvider,
resource: string
) => {
if (!storagePath) return data;
const newFiles = (
await Promise.all(
Object.keys(data)
.filter(key => data[key]?.rawFile instanceof File)
.map(async (key: string) => {
const file = data[key];
const bucket =
storagePath instanceof Function
? storagePath(resource)
: storagePath;
const filename = filenameFromData({
data,
resource,
field: key,
filename: file.rawFile.name,
});

const { error } = await supabaseClient.storage
.from(bucket)
.upload(filename, file.rawFile);
if (error) throw error;
const {
data: { publicUrl },
} = supabaseClient.storage
.from(bucket)
.getPublicUrl(filename);

return { [key]: publicUrl };
})
)
).reduce((acc, val) => ({ ...acc, ...val }), {});
return { ...data, ...newFiles };
},
},
]);
};

/**
Expand Down

0 comments on commit a471c0e

Please sign in to comment.