Skip to content

Commit

Permalink
Supabase updates
Browse files Browse the repository at this point in the history
  • Loading branch information
fgatti675 committed Sep 18, 2024
1 parent d0bc836 commit caf4952
Show file tree
Hide file tree
Showing 7 changed files with 10,318 additions and 80 deletions.
90 changes: 90 additions & 0 deletions examples/example_pro/src/SupabaseApp/SQLTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { BaseProperty, EntityReference } from "@firecms/core";

interface SQLReferenceProperty extends BaseProperty<EntityReference> {
dataType: "reference";

/**
* Name of the table this reference points to.
* You can leave this prop undefined if the table is not yet known,
* e.g. you are using a property builder and the table name depends on a different property.
*/
tableName?: string;

/**
* Name of the column in the table that this reference points to.
* Defaults to "id" if not specified.
*/
columnName?: string;

/**
* Allow selection of rows that pass the given filter only.
* e.g. `forceFilter: { name: [["LIKE", "%Smith%"]] }`
*/
forceFilter?: SQLFilterValues;

/**
* Properties/columns that need to be rendered when displaying a preview of this reference.
* If not specified, the first 3 columns are used. Only the first 3 specified values are considered.
*/
previewProperties?: string[];

/**
* Should the reference include the ID of the entity (row). Defaults to `true`
*/
includeId?: boolean;

/**
* Should the reference include a link to the entity (open the entity details). Defaults to `true`
*/
includeEntityLink?: boolean;

/**
* Specify any intermediate tables/columns for many-to-many relationships.
* This can be an array of objects specifying table and column mappings.
*/
intermediateTables?: IntermediateTable[];

/**
* Name of the table that references this table.
*/
referencedByTable?: string;

/**
* Name of the column in the referencing table that points to this table.
*/
referencedByColumn?: string;
}

/**
* Represents filter values in an SQL context.
* Each key is a column and the value is an array of conditions.
*/
interface SQLFilterValues {
[column: string]: [SQLComparisonOperator, any][];
}

/**
* Comparison operators used in SQL, could be extended based on requirements.
*/
type SQLComparisonOperator = "=" | "!=" | ">" | ">=" | "<" | "<=" | "LIKE" | "IN";

// export type WhereFilterOp =
// | "<"
// | "<="
// | "=="
// | "!="
// | ">="
// | ">"
// | "array-contains"
// | "in"
// | "not-in"
// | "array-contains-any";

/**
* Represents an intermediate table used for many-to-many relationships in SQL.
*/
interface IntermediateTable {
intermediateTableName: string;
sourceColumn: string;
targetColumn: string;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import { buildCollection } from "@firecms/core";
import { buildCollection, EnumValues } from "@firecms/core";

export const locales: EnumValues = {
es: "Spanish",
de: "German",
en: "English",
it: "Italian",
fr: {
id: "fr",
label: "French",
disabled: true
}
};

export const productsCollection = buildCollection<any>({
id: "products",
path: "Products",
path: "products",
name: "Products",
singularName: "Product",
group: "E-commerce",
Expand Down Expand Up @@ -37,6 +49,15 @@ export const productsCollection = buildCollection<any>({
clothing_woman: "Clothing woman",
}
},

available_locales: {
name: "Available locales",
dataType: "array",
of: {
dataType: "string",
enumValues: locales
}
},
related_products: {
dataType: "array",
name: "Related products",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ export function useSupabaseDelegate({ supabase }: SupabaseDataSourceProps): Supa
order,
startAfter
} = props;
const query = await buildQuery(path, filter, orderBy, order, startAfter, limit, false);

const {
data,
error
} = await query;
} = await buildQuery(path, filter, orderBy, order, startAfter, limit, false);

if (error) {
throw error;
Expand Down Expand Up @@ -193,37 +193,59 @@ export function useSupabaseDelegate({ supabase }: SupabaseDataSourceProps): Supa
status
} = props;
if (entityId) {
return Promise.resolve(supabase
.from(path)
.update(values)
.eq("id", entityId)
.then(() => {
return {
id: entityId,
path,
values: values as M
} as Entity<M>
}));
return new Promise<Entity<M>>((resolve, reject) => {
supabase
.from(path)
.update(values)
.eq("id", entityId)
.then(({
data,
error
}) => {
if (error) {
reject(error);
}
console.log("Supabase: Entity saved", data);
return resolve({
id: entityId,
path,
values: values as M
} as Entity<M>)
}, (error) => {
console.error("Supabase: Error saving entity", error);
reject(error);
});
});
} else {
const newId = crypto.randomUUID();
return Promise.resolve(supabase
.from(path)
.insert({
id: newId,
...values
})
.single()
.then(({ data }) => {
if (!data) {
throw new Error("No data returned");
}
return {
// gen new uuid

return new Promise<Entity<M>>((resolve, reject) => {
supabase
.from(path)
.insert({
id: newId,
path,
values: values as M
}
}));
...values
})
.single()
.then(({ data, error }) => {
if (error) {
reject(error);
}
if (!data) {
throw new Error("No data returned");
}
console.log("Supabase: Entity saved", data);
return resolve({
// gen new uuid
id: newId,
path,
values: values as M
})
}, (error) => {
console.error("Supabase: Error saving entity", error);
reject(error);
});
});
}
}, [supabase]);

Expand Down
Loading

0 comments on commit caf4952

Please sign in to comment.