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: bring-your-own-pg-client™ (browser support) #782

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,315 changes: 1,094 additions & 221 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@
"imports": {
"#package.json": "./package.json"
},
"exports": {
".": {
"types": "./dist/lib/index.d.ts",
"require": "./dist/lib/index.cjs",
"import": "./dist/lib/index.js"
},
"./base": {
"types": "./dist/lib/base.d.ts",
"require": "./dist/lib/base.cjs",
"import": "./dist/lib/base.js"
}
},
"repository": "supabase/postgres-meta",
"scripts": {
"check": "tsc -p tsconfig.json --noEmit",
"clean": "rimraf dist tsconfig.tsbuildinfo",
"format": "prettier --write '{src,test}/**/*.ts' '*.ts'",
"build": "tsc -p tsconfig.json && cpy 'src/lib/sql/*.sql' dist/lib/sql",
"build": "tsup",
"docs:export": "PG_META_EXPORT_DOCS=true node --loader ts-node/esm src/server/server.ts > openapi.json",
"gen:types:typescript": "PG_META_GENERATE_TYPES=typescript node --loader ts-node/esm src/server/server.ts",
"gen:types:go": "PG_META_GENERATE_TYPES=go node --loader ts-node/esm src/server/server.ts",
Expand Down Expand Up @@ -66,7 +78,9 @@
"pino-pretty": "^10.2.3",
"rimraf": "^5.0.5",
"ts-node": "^10.9.1",
"tsup": "^8.1.0",
"typescript": "^5.2.2",
"vite": "^5.3.1",
"vitest": "^1.2.2"
}
}
67 changes: 4 additions & 63 deletions src/lib/PostgresMeta.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,17 @@
import { PoolConfig } from 'pg'
import * as Parser from './Parser.js'
import PostgresMetaColumnPrivileges from './PostgresMetaColumnPrivileges.js'
import PostgresMetaColumns from './PostgresMetaColumns.js'
import PostgresMetaConfig from './PostgresMetaConfig.js'
import PostgresMetaExtensions from './PostgresMetaExtensions.js'
import PostgresMetaForeignTables from './PostgresMetaForeignTables.js'
import PostgresMetaFunctions from './PostgresMetaFunctions.js'
import PostgresMetaIndexes from './PostgresMetaIndexes.js'
import PostgresMetaMaterializedViews from './PostgresMetaMaterializedViews.js'
import PostgresMetaPolicies from './PostgresMetaPolicies.js'
import PostgresMetaPublications from './PostgresMetaPublications.js'
import PostgresMetaRelationships from './PostgresMetaRelationships.js'
import PostgresMetaRoles from './PostgresMetaRoles.js'
import PostgresMetaSchemas from './PostgresMetaSchemas.js'
import PostgresMetaTablePrivileges from './PostgresMetaTablePrivileges.js'
import PostgresMetaTables from './PostgresMetaTables.js'
import PostgresMetaTriggers from './PostgresMetaTriggers.js'
import PostgresMetaTypes from './PostgresMetaTypes.js'
import PostgresMetaVersion from './PostgresMetaVersion.js'
import PostgresMetaViews from './PostgresMetaViews.js'
import PostgresMetaBase from './PostgresMetaBase.js'
import { init } from './db.js'
import { PostgresMetaResult } from './types.js'

export default class PostgresMeta {
query: (sql: string) => Promise<PostgresMetaResult<any>>
end: () => Promise<void>
columnPrivileges: PostgresMetaColumnPrivileges
columns: PostgresMetaColumns
config: PostgresMetaConfig
extensions: PostgresMetaExtensions
foreignTables: PostgresMetaForeignTables
functions: PostgresMetaFunctions
indexes: PostgresMetaIndexes
materializedViews: PostgresMetaMaterializedViews
policies: PostgresMetaPolicies
publications: PostgresMetaPublications
relationships: PostgresMetaRelationships
roles: PostgresMetaRoles
schemas: PostgresMetaSchemas
tablePrivileges: PostgresMetaTablePrivileges
tables: PostgresMetaTables
triggers: PostgresMetaTriggers
types: PostgresMetaTypes
version: PostgresMetaVersion
views: PostgresMetaViews

export default class PostgresMeta extends PostgresMetaBase {
parse = Parser.Parse
deparse = Parser.Deparse
format = Parser.Format

constructor(config: PoolConfig) {
const { query, end } = init(config)
this.query = query
super({ query, end })

this.end = end
this.columnPrivileges = new PostgresMetaColumnPrivileges(this.query)
this.columns = new PostgresMetaColumns(this.query)
this.config = new PostgresMetaConfig(this.query)
this.extensions = new PostgresMetaExtensions(this.query)
this.foreignTables = new PostgresMetaForeignTables(this.query)
this.functions = new PostgresMetaFunctions(this.query)
this.indexes = new PostgresMetaIndexes(this.query)
this.materializedViews = new PostgresMetaMaterializedViews(this.query)
this.policies = new PostgresMetaPolicies(this.query)
this.publications = new PostgresMetaPublications(this.query)
this.relationships = new PostgresMetaRelationships(this.query)
this.roles = new PostgresMetaRoles(this.query)
this.schemas = new PostgresMetaSchemas(this.query)
this.tablePrivileges = new PostgresMetaTablePrivileges(this.query)
this.tables = new PostgresMetaTables(this.query)
this.triggers = new PostgresMetaTriggers(this.query)
this.types = new PostgresMetaTypes(this.query)
this.version = new PostgresMetaVersion(this.query)
this.views = new PostgresMetaViews(this.query)
}
}
75 changes: 75 additions & 0 deletions src/lib/PostgresMetaBase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import PostgresMetaColumnPrivileges from './PostgresMetaColumnPrivileges.js'
import PostgresMetaColumns from './PostgresMetaColumns.js'
import PostgresMetaConfig from './PostgresMetaConfig.js'
import PostgresMetaExtensions from './PostgresMetaExtensions.js'
import PostgresMetaForeignTables from './PostgresMetaForeignTables.js'
import PostgresMetaFunctions from './PostgresMetaFunctions.js'
import PostgresMetaIndexes from './PostgresMetaIndexes.js'
import PostgresMetaMaterializedViews from './PostgresMetaMaterializedViews.js'
import PostgresMetaPolicies from './PostgresMetaPolicies.js'
import PostgresMetaPublications from './PostgresMetaPublications.js'
import PostgresMetaRelationships from './PostgresMetaRelationships.js'
import PostgresMetaRoles from './PostgresMetaRoles.js'
import PostgresMetaSchemas from './PostgresMetaSchemas.js'
import PostgresMetaTablePrivileges from './PostgresMetaTablePrivileges.js'
import PostgresMetaTables from './PostgresMetaTables.js'
import PostgresMetaTriggers from './PostgresMetaTriggers.js'
import PostgresMetaTypes from './PostgresMetaTypes.js'
import PostgresMetaVersion from './PostgresMetaVersion.js'
import PostgresMetaViews from './PostgresMetaViews.js'
import { EndFn, QueryFn } from './types.js'

export type PostgresMetaBaseOptions = {
query: QueryFn
end: EndFn
}

export default class PostgresMetaBase {
query: QueryFn
end: EndFn

columnPrivileges: PostgresMetaColumnPrivileges
columns: PostgresMetaColumns
config: PostgresMetaConfig
extensions: PostgresMetaExtensions
foreignTables: PostgresMetaForeignTables
functions: PostgresMetaFunctions
indexes: PostgresMetaIndexes
materializedViews: PostgresMetaMaterializedViews
policies: PostgresMetaPolicies
publications: PostgresMetaPublications
relationships: PostgresMetaRelationships
roles: PostgresMetaRoles
schemas: PostgresMetaSchemas
tablePrivileges: PostgresMetaTablePrivileges
tables: PostgresMetaTables
triggers: PostgresMetaTriggers
types: PostgresMetaTypes
version: PostgresMetaVersion
views: PostgresMetaViews

constructor(options: PostgresMetaBaseOptions) {
this.query = options.query
this.end = options.end

this.columnPrivileges = new PostgresMetaColumnPrivileges(this.query)
this.columns = new PostgresMetaColumns(this.query)
this.config = new PostgresMetaConfig(this.query)
this.extensions = new PostgresMetaExtensions(this.query)
this.foreignTables = new PostgresMetaForeignTables(this.query)
this.functions = new PostgresMetaFunctions(this.query)
this.indexes = new PostgresMetaIndexes(this.query)
this.materializedViews = new PostgresMetaMaterializedViews(this.query)
this.policies = new PostgresMetaPolicies(this.query)
this.publications = new PostgresMetaPublications(this.query)
this.relationships = new PostgresMetaRelationships(this.query)
this.roles = new PostgresMetaRoles(this.query)
this.schemas = new PostgresMetaSchemas(this.query)
this.tablePrivileges = new PostgresMetaTablePrivileges(this.query)
this.tables = new PostgresMetaTables(this.query)
this.triggers = new PostgresMetaTriggers(this.query)
this.types = new PostgresMetaTypes(this.query)
this.version = new PostgresMetaVersion(this.query)
this.views = new PostgresMetaViews(this.query)
}
}
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaColumnPrivileges.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ident, literal } from 'pg-format'
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
import { filterByList } from './helpers.js'
import { columnPrivilegesSql } from './sql/index.js'
import columnPrivilegesSql from './sql/column_privileges.sql'
import {
PostgresMetaResult,
PostgresColumnPrivileges,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaColumns.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ident, literal } from 'pg-format'
import PostgresMetaTables from './PostgresMetaTables.js'
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
import { columnsSql } from './sql/index.js'
import columnsSql from './sql/columns.sql'
import { PostgresMetaResult, PostgresColumn } from './types.js'
import { filterByList } from './helpers.js'

Expand Down
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { configSql } from './sql/index.js'
import configSql from './sql/config.sql'
import { PostgresMetaResult, PostgresConfig } from './types.js'

export default class PostgresMetaConfig {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaExtensions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ident, literal } from 'pg-format'
import { extensionsSql } from './sql/index.js'
import extensionsSql from './sql/extensions.sql'
import { PostgresMetaResult, PostgresExtension } from './types.js'

export default class PostgresMetaExtensions {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/PostgresMetaForeignTables.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { literal } from 'pg-format'
import { coalesceRowsToArray, filterByList } from './helpers.js'
import { columnsSql, foreignTablesSql } from './sql/index.js'
import columnsSql from './sql/columns.sql'
import foreignTablesSql from './sql/foreign_tables.sql'
import { PostgresMetaResult, PostgresForeignTable } from './types.js'

export default class PostgresMetaForeignTables {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaFunctions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ident, literal } from 'pg-format'
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
import { filterByList } from './helpers.js'
import { functionsSql } from './sql/index.js'
import functionsSql from './sql/functions.sql'
import { PostgresMetaResult, PostgresFunction, PostgresFunctionCreate } from './types.js'

export default class PostgresMetaFunctions {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaIndexes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ident, literal } from 'pg-format'
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
import { filterByList } from './helpers.js'
import { indexesSql } from './sql/index.js'
import indexesSql from './sql/indexes.sql'
import { PostgresMetaResult, PostgresIndex } from './types.js'

export default class PostgresMetaFunctions {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/PostgresMetaMaterializedViews.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { literal } from 'pg-format'
import { coalesceRowsToArray, filterByList } from './helpers.js'
import { columnsSql, materializedViewsSql } from './sql/index.js'
import columnsSql from './sql/columns.sql'
import materializedViewsSql from './sql/materialized_views.sql'
import { PostgresMetaResult, PostgresMaterializedView } from './types.js'

export default class PostgresMetaMaterializedViews {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaPolicies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ident, literal } from 'pg-format'
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
import { filterByList } from './helpers.js'
import { policiesSql } from './sql/index.js'
import policiesSql from './sql/policies.sql'
import { PostgresMetaResult, PostgresPolicy } from './types.js'

export default class PostgresMetaPolicies {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaPublications.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ident, literal } from 'pg-format'
import { publicationsSql } from './sql/index.js'
import publicationsSql from './sql/publications.sql'
import { PostgresMetaResult, PostgresPublication, PostgresTable } from './types.js'

export default class PostgresMetaPublications {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/PostgresMetaRelationships.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { literal } from 'pg-format'
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
import { tableRelationshipsSql, viewsKeyDependenciesSql } from './sql/index.js'
import tableRelationshipsSql from './sql/table_relationships.sql'
import viewsKeyDependenciesSql from './sql/views_key_dependencies.sql'
import { PostgresMetaResult, PostgresRelationship } from './types.js'

/*
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaRoles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ident, literal } from 'pg-format'
import { rolesSql } from './sql/index.js'
import rolesSql from './sql/roles.sql'
import {
PostgresMetaResult,
PostgresRole,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ident, literal } from 'pg-format'
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
import { schemasSql } from './sql/index.js'
import schemasSql from './sql/schemas.sql'
import {
PostgresMetaResult,
PostgresSchema,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaTablePrivileges.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ident, literal } from 'pg-format'
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
import { filterByList } from './helpers.js'
import { tablePrivilegesSql } from './sql/index.js'
import tablePrivilegesSql from './sql/table_privileges.sql'
import {
PostgresMetaResult,
PostgresTablePrivileges,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/PostgresMetaTables.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ident, literal } from 'pg-format'
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
import { coalesceRowsToArray, filterByList } from './helpers.js'
import { columnsSql, tablesSql } from './sql/index.js'
import columnsSql from './sql/columns.sql'
import tablesSql from './sql/tables.sql'
import {
PostgresMetaResult,
PostgresTable,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaTriggers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ident, literal } from 'pg-format'
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
import { filterByList } from './helpers.js'
import { triggersSql } from './sql/index.js'
import triggersSql from './sql/triggers.sql'
import { PostgresMetaResult, PostgresTrigger } from './types.js'

export default class PostgresMetaTriggers {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
import { filterByList } from './helpers.js'
import { typesSql } from './sql/index.js'
import typesSql from './sql/types.sql'
import { PostgresMetaResult, PostgresType } from './types.js'

export default class PostgresMetaTypes {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/PostgresMetaVersion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { versionSql } from './sql/index.js'
import versionSql from './sql/version.sql'
import { PostgresMetaResult, PostgresVersion } from './types.js'

export default class PostgresMetaVersion {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/PostgresMetaViews.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { literal } from 'pg-format'
import { DEFAULT_SYSTEM_SCHEMAS } from './constants.js'
import { coalesceRowsToArray, filterByList } from './helpers.js'
import { columnsSql, viewsSql } from './sql/index.js'
import columnsSql from './sql/columns.sql'
import viewsSql from './sql/views.sql'
import { PostgresMetaResult, PostgresView } from './types.js'

export default class PostgresMetaViews {
Expand Down
29 changes: 29 additions & 0 deletions src/lib/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export { default as PostgresMetaBase } from './PostgresMetaBase.js'
export { wrapError, wrapResult } from './helpers.js'
export {
EndFn,
PostgresColumn,
PostgresConfig,
PostgresExtension,
PostgresFunction,
PostgresFunctionCreate,
PostgresIndex,
PostgresMaterializedView,
PostgresMetaErr,
PostgresMetaOk,
PostgresMetaResult,
PostgresPolicy,
PostgresPrimaryKey,
PostgresPublication,
PostgresRelationship,
PostgresRole,
PostgresSchema,
PostgresSchemaCreate,
PostgresSchemaUpdate,
PostgresTable,
PostgresTrigger,
PostgresType,
PostgresVersion,
PostgresView,
QueryFn,
} from './types.js'
Loading