The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: materialized views
Alaister Young0e637786682a
diff --git a/package-lock.json b/package-lock.jsonindex 38d8fbb6ebc..b2eee282491 100644--- a/package-lock.json+++ b/package-lock.json@@ -9528,9 +9528,9 @@}},"node_modules/@supabase/postgres-meta": {- "version": "0.60.7",- "resolved": "https://registry.npmjs.org/@supabase/postgres-meta/-/postgres-meta-0.60.7.tgz",- "integrity": "sha512-vC5mwZ4sVAA5Hw57GSSDI7rGBY8/m8RscRHaFwfnqKgXFxKMvlfrzxnP/MwXg7EXRW1jfgvMAEkxuJNLDe1YMA==",+ "version": "0.62.0",+ "resolved": "https://registry.npmjs.org/@supabase/postgres-meta/-/postgres-meta-0.62.0.tgz",+ "integrity": "sha512-35p2KJXq1tvzIuEa1VTEbRl5Or5qQEYFLZmG30LwdyDYGzdVtuQ03rYpQLMKAG8ueuhul+PN0WCPfvH1yq3DhA==","dev": true,"dependencies": {"@sinclair/typebox": "^0.25.1",@@ -9539,7 +9539,7 @@"pgsql-parser": "^13.3.0","postgres-array": "^3.0.1","prettier": "^2.6.0",- "prettier-plugin-sql": "^0.12.1"+ "prettier-plugin-sql": "^0.13.0"},"engines": {"node": ">=16",@@ -30868,13 +30868,13 @@}},"node_modules/prettier-plugin-sql": {- "version": "0.12.1",- "resolved": "https://registry.npmjs.org/prettier-plugin-sql/-/prettier-plugin-sql-0.12.1.tgz",- "integrity": "sha512-2z0pMmQEC61Xywerw2FJFpEIaA9lJUjB5mj+9dfRU8HkU7iICFrIizpMjSSDMoGkhh5W+IRROVgLlgRdpphI7g==",+ "version": "0.13.0",+ "resolved": "https://registry.npmjs.org/prettier-plugin-sql/-/prettier-plugin-sql-0.13.0.tgz",+ "integrity": "sha512-Ui9603tDD6PFyr7JvIEoE6cIFMQnJVDriG+oLyVThsGo/MIl5ek18JhH3xtox9ux8jvyww/FUFrJzxpZ7FIdvw==","dev": true,"dependencies": {"node-sql-parser": "^4.4.0",- "sql-formatter": "^10.0.0",+ "sql-formatter": "^11.0.2","tslib": "^2.4.0"},"engines": {@@ -34450,16 +34450,16 @@"integrity": "sha512-VE0SOVEHCk7Qc8ulkWw3ntAzXuqf7S2lvwQaDLRnUeIEaKNQJzV6BwmLKhOqT61aGhfUMrXeaBk+oDGCzvhcug=="},"node_modules/sql-formatter": {- "version": "10.7.2",- "resolved": "https://registry.npmjs.org/sql-formatter/-/sql-formatter-10.7.2.tgz",- "integrity": "sha512-YspJXMr2rKMGZY50zYRBs2nCtEO/AKDrmQOxmV2CrGEOFSXUhY3YbsOUe6lX0T/7xbBs4pAYv4UObmwf78vtKg==",+ "version": "11.0.2",+ "resolved": "https://registry.npmjs.org/sql-formatter/-/sql-formatter-11.0.2.tgz",+ "integrity": "sha512-6QumAdGHEnI5dXEq1d0aBRP876AyA9Wp/UE7wopKNA2Mp9sKGRKVqGgoWHk4dr0J0nceesC85Y0p36qmGoNqhw==","dev": true,"dependencies": {"argparse": "^2.0.1","nearley": "^2.20.1"},"bin": {- "sql-formatter": "bin/sql-formatter-cli.js"+ "sql-formatter": "bin/sql-formatter-cli.cjs"}},"node_modules/sql-parser-cst": {@@ -39081,7 +39081,7 @@"@storybook/addon-links": "^6.4.19","@storybook/react": "^6.4.19","@storybook/testing-library": "^0.0.9",- "@supabase/postgres-meta": "^0.60.7",+ "@supabase/postgres-meta": "^0.62.0","@tailwindcss/typography": "^0.5.2","@testing-library/dom": "^8.19.0","@testing-library/react": "^12.1.5",@@ -45914,9 +45914,9 @@}},"@supabase/postgres-meta": {- "version": "0.60.7",- "resolved": "https://registry.npmjs.org/@supabase/postgres-meta/-/postgres-meta-0.60.7.tgz",- "integrity": "sha512-vC5mwZ4sVAA5Hw57GSSDI7rGBY8/m8RscRHaFwfnqKgXFxKMvlfrzxnP/MwXg7EXRW1jfgvMAEkxuJNLDe1YMA==",+ "version": "0.62.0",+ "resolved": "https://registry.npmjs.org/@supabase/postgres-meta/-/postgres-meta-0.62.0.tgz",+ "integrity": "sha512-35p2KJXq1tvzIuEa1VTEbRl5Or5qQEYFLZmG30LwdyDYGzdVtuQ03rYpQLMKAG8ueuhul+PN0WCPfvH1yq3DhA==","dev": true,"requires": {"@sinclair/typebox": "^0.25.1",@@ -45925,7 +45925,7 @@"pgsql-parser": "^13.3.0","postgres-array": "^3.0.1","prettier": "^2.6.0",- "prettier-plugin-sql": "^0.12.1"+ "prettier-plugin-sql": "^0.13.0"}},"@supabase/postgrest-js": {@@ -62800,13 +62800,13 @@"dev": true},"prettier-plugin-sql": {- "version": "0.12.1",- "resolved": "https://registry.npmjs.org/prettier-plugin-sql/-/prettier-plugin-sql-0.12.1.tgz",- "integrity": "sha512-2z0pMmQEC61Xywerw2FJFpEIaA9lJUjB5mj+9dfRU8HkU7iICFrIizpMjSSDMoGkhh5W+IRROVgLlgRdpphI7g==",+ "version": "0.13.0",+ "resolved": "https://registry.npmjs.org/prettier-plugin-sql/-/prettier-plugin-sql-0.13.0.tgz",+ "integrity": "sha512-Ui9603tDD6PFyr7JvIEoE6cIFMQnJVDriG+oLyVThsGo/MIl5ek18JhH3xtox9ux8jvyww/FUFrJzxpZ7FIdvw==","dev": true,"requires": {"node-sql-parser": "^4.4.0",- "sql-formatter": "^10.0.0",+ "sql-formatter": "^11.0.2","tslib": "^2.4.0"}},@@ -65525,9 +65525,9 @@"integrity": "sha512-VE0SOVEHCk7Qc8ulkWw3ntAzXuqf7S2lvwQaDLRnUeIEaKNQJzV6BwmLKhOqT61aGhfUMrXeaBk+oDGCzvhcug=="},"sql-formatter": {- "version": "10.7.2",- "resolved": "https://registry.npmjs.org/sql-formatter/-/sql-formatter-10.7.2.tgz",- "integrity": "sha512-YspJXMr2rKMGZY50zYRBs2nCtEO/AKDrmQOxmV2CrGEOFSXUhY3YbsOUe6lX0T/7xbBs4pAYv4UObmwf78vtKg==",+ "version": "11.0.2",+ "resolved": "https://registry.npmjs.org/sql-formatter/-/sql-formatter-11.0.2.tgz",+ "integrity": "sha512-6QumAdGHEnI5dXEq1d0aBRP876AyA9Wp/UE7wopKNA2Mp9sKGRKVqGgoWHk4dr0J0nceesC85Y0p36qmGoNqhw==","dev": true,"requires": {"argparse": "^2.0.1",@@ -65973,7 +65973,7 @@"@storybook/testing-library": "^0.0.9","@stripe/react-stripe-js": "^1.7.0","@stripe/stripe-js": "^1.25.0",- "@supabase/postgres-meta": "^0.60.7",+ "@supabase/postgres-meta": "^0.62.0","@supabase/react-data-grid": "^7.1.0-beta.7","@supabase/shared-types": "^0.1.40","@supabase/supabase-js": "^2.13.0",diff --git a/studio/components/interfaces/TableGridEditor/TableDefinition.tsx b/studio/components/interfaces/TableGridEditor/TableDefinition.tsxindex a7bf539c849..d0d8b28dbe0 100644--- a/studio/components/interfaces/TableGridEditor/TableDefinition.tsx+++ b/studio/components/interfaces/TableGridEditor/TableDefinition.tsx@@ -33,7 +33,8 @@ const TableDefinition = ({ id }: TableDefinitionProps) => {connectionString: project?.connectionString,},{- enabled: entityType?.type === ENTITY_TYPE.VIEW,+ enabled:+ entityType?.type === ENTITY_TYPE.VIEW || entityType?.type === ENTITY_TYPE.MATERIALIZED_VIEW,})@@ -50,11 +51,15 @@ const TableDefinition = ({ id }: TableDefinitionProps) => {)const { data: definition, isLoading } =- entityType?.type === ENTITY_TYPE.VIEW ? viewResult : tableResult+ entityType?.type === ENTITY_TYPE.VIEW || entityType?.type === ENTITY_TYPE.MATERIALIZED_VIEW+ ? viewResult+ : tableResultconst prepend =entityType?.type === ENTITY_TYPE.VIEW? `create view ${entityType.schema}.${entityType.name} as\n`+ : entityType?.type === ENTITY_TYPE.MATERIALIZED_VIEW+ ? `create materialized view ${entityType.schema}.${entityType.name} as\n`: ''const formattedDefinition = useMemo(diff --git a/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx b/studio/components/interfaces/TableGridEditor/TableGridEditor.tsxindex f8186be45a4..c4909d2264e 100644--- a/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx+++ b/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx@@ -208,7 +208,8 @@ const TableGridEditor = ({const tableId = selectedTable?.id- const isViewSelected = entityType?.type === ENTITY_TYPE.VIEW+ const isViewSelected =+ entityType?.type === ENTITY_TYPE.VIEW || entityType?.type === ENTITY_TYPE.MATERIALIZED_VIEWconst isTableSelected = entityType?.type === ENTITY_TYPE.TABLEconst isForeignTableSelected = entityType?.type === ENTITY_TYPE.FOREIGN_TABLEconst isLocked = meta.excludedSchemas.includes(entityType?.schema ?? '')diff --git a/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx b/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsxindex 09e50969b71..3d3c15330ea 100644--- a/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx+++ b/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx@@ -74,6 +74,8 @@ const TableEditorLayout = ({if (entity?.type) {switch (entity.type) {case ENTITY_TYPE.MATERIALIZED_VIEW:+ return meta.materializedViews.loadById(entity.id)+case ENTITY_TYPE.VIEW:return meta.views.loadById(entity.id)diff --git a/studio/package.json b/studio/package.jsonindex 919e962cd5b..c2299cd3fef 100644--- a/studio/package.json+++ b/studio/package.json@@ -108,7 +108,7 @@"@storybook/addon-links": "^6.4.19","@storybook/react": "^6.4.19","@storybook/testing-library": "^0.0.9",- "@supabase/postgres-meta": "^0.60.7",+ "@supabase/postgres-meta": "^0.62.0","@tailwindcss/typography": "^0.5.2","@testing-library/dom": "^8.19.0","@testing-library/react": "^12.1.5",diff --git a/studio/pages/project/[ref]/editor/[id].tsx b/studio/pages/project/[ref]/editor/[id].tsxindex b59be8949b5..721f01a1f04 100644--- a/studio/pages/project/[ref]/editor/[id].tsx+++ b/studio/pages/project/[ref]/editor/[id].tsx@@ -58,6 +58,7 @@ const TableEditorPage: NextPageWithLayout = () => {const tables: PostgresTable[] = meta.tables.list()const views: SchemaView[] = meta.views.list()+ const materializedViews = meta.materializedViews.list()const foreignTables: Partial<PostgresTable>[] = meta.foreignTables.list()const selectedTable = !isNaN(Number(id))@@ -66,6 +67,8 @@ const TableEditorPage: NextPageWithLayout = () => {// @ts-ignore.concat(views)// @ts-ignore+ .concat(materializedViews)+ // @ts-ignore.concat(foreignTables).find((table) => table.id === Number(id)): undefineddiff --git a/studio/stores/pgmeta/MaterializedViewStore.ts b/studio/stores/pgmeta/MaterializedViewStore.tsnew file mode 100644index 00000000000..96d81b3ceb4--- /dev/null+++ b/studio/stores/pgmeta/MaterializedViewStore.ts@@ -0,0 +1,39 @@+import { PostgresMaterializedView } from '@supabase/postgres-meta'+import { ResponseError } from 'types'+import PostgresMetaInterface, { IPostgresMetaInterface } from '../common/PostgresMetaInterface'+import { IRootStore } from '../RootStore'+import { get } from 'lib/common/fetch'++export interface IMaterializedViewStore extends IPostgresMetaInterface<PostgresMaterializedView> {+ loadById: (+ id: number | string+ ) => Promise<Partial<PostgresMaterializedView> | { error: ResponseError }>+}++export default class MaterializedViewStore extends PostgresMetaInterface<PostgresMaterializedView> {+ constructor(+ rootStore: IRootStore,+ dataUrl: string,+ headers?: {+ [prop: string]: any+ },+ options?: { identifier: string }+ ) {+ super(rootStore, dataUrl, headers, options)+ }++ async loadById(id: number | string) {+ try {+ const url = `${this.url}?id=${id}`+ const response = await get(url, { headers: this.headers })+ if (response.error) throw response.error++ const data = response as Partial<PostgresMaterializedView>+ // @ts-ignore+ this.data[id] = data+ return data+ } catch (error: any) {+ return { error }+ }+ }+}diff --git a/studio/stores/pgmeta/MetaStore.ts b/studio/stores/pgmeta/MetaStore.tsindex b2b2267e2c8..88fe5e0c8da 100644--- a/studio/stores/pgmeta/MetaStore.ts+++ b/studio/stores/pgmeta/MetaStore.ts@@ -44,6 +44,7 @@ import ExtensionsStore from './ExtensionsStore'import TypesStore from './TypesStore'import ForeignTableStore, { IForeignTableStore } from './ForeignTableStore'import ViewStore, { IViewStore } from './ViewStore'+import MaterializedViewStore, { IMaterializedViewStore } from './MaterializedViewStore'import { FOREIGN_KEY_DELETION_ACTION } from 'data/database/database-query-constants'const BATCH_SIZE = 1000@@ -57,6 +58,7 @@ export interface IMetaStore {columns: IPostgresMetaInterface<PostgresColumn>schemas: IPostgresMetaInterface<PostgresSchema>views: IViewStore+ materializedViews: IMaterializedViewStoreforeignTables: IForeignTableStorehooks: IPostgresMetaInterface<any>@@ -144,6 +146,7 @@ export default class MetaStore implements IMetaStore {columns: ColumnStoreschemas: SchemaStoreviews: ViewStore+ materializedViews: MaterializedViewStoreforeignTables: ForeignTableStorehooks: HooksStore@@ -197,6 +200,11 @@ export default class MetaStore implements IMetaStore {this.columns = new ColumnStore(this.rootStore, `${this.baseUrl}/columns`, this.headers)this.schemas = new SchemaStore(this.rootStore, `${this.baseUrl}/schemas`, this.headers)this.views = new ViewStore(this.rootStore, `${this.baseUrl}/views`, this.headers)+ this.materializedViews = new MaterializedViewStore(+ this.rootStore,+ `${this.baseUrl}/materialized-views`,+ this.headers+ )this.foreignTables = new ForeignTableStore(this.rootStore,`${this.baseUrl}/foreign-tables`,@@ -950,6 +958,9 @@ export default class MetaStore implements IMetaStore {this.views.setUrl(`${this.baseUrl}/views`)this.views.setHeaders(this.headers)+ this.materializedViews.setUrl(`${this.baseUrl}/materialized-views`)+ this.materializedViews.setHeaders(this.headers)+this.foreignTables.setUrl(`${this.baseUrl}/foreign-tables`)this.foreignTables.setHeaders(this.headers)