MCPcopy
hub / github.com/drizzle-team/drizzle-orm / fromDatabase

Function fromDatabase

drizzle-kit/src/serializer/gelSerializer.ts:954–1535  ·  view source on GitHub ↗
(
	db: DB,
	tablesFilter: (table: string) => boolean = () => true,
	schemaFilters: string[],
	entities?: {
		roles: boolean | {
			provider?: string | undefined;
			include?: string[] | undefined;
			exclude?: string[] | undefined;
		};
	},
	progressCallback?: (
		stage: IntrospectStage,
		count: number,
		status: IntrospectStatus,
	) => void,
	tsSchema?: GelSchemaInternal,
)

Source from the content-addressed store, hash-verified

952}
953
954export const fromDatabase = async (
955 db: DB,
956 tablesFilter: (table: string) => boolean = () => true,
957 schemaFilters: string[],
958 entities?: {
959 roles: boolean | {
960 provider?: string | undefined;
961 include?: string[] | undefined;
962 exclude?: string[] | undefined;
963 };
964 },
965 progressCallback?: (
966 stage: IntrospectStage,
967 count: number,
968 status: IntrospectStatus,
969 ) => void,
970 tsSchema?: GelSchemaInternal,
971): Promise<GelSchemaInternal> => {
972 const result: Record<string, Table> = {};
973 // const views: Record<string, View> = {};
974 const policies: Record<string, Policy> = {};
975 const internals: GelKitInternals = { tables: {} };
976
977 const where = schemaFilters.map((t) => `n.nspname = '${t}'`).join(' or ');
978
979 const allTables = await db.query<{ table_schema: string; table_name: string; type: string; rls_enabled: boolean }>(
980 `SELECT
981 n.nspname::text AS table_schema,
982 c.relname::text AS table_name,
983 CASE
984 WHEN c.relkind = 'r' THEN 'table'
985 WHEN c.relkind = 'v' THEN 'view'
986 WHEN c.relkind = 'm' THEN 'materialized_view'
987 END AS type,
988 c.relrowsecurity AS rls_enabled
989FROM
990 pg_catalog.pg_class c
991JOIN
992 pg_catalog.pg_namespace n ON n.oid::text = c.relnamespace::text
993WHERE
994 c.relkind IN ('r', 'v', 'm')
995 ${where === '' ? '' : ` AND ${where}`};`,
996 );
997
998 const schemas = new Set(allTables.map((it) => it.table_schema));
999
1000 const allSchemas = await db.query<{
1001 table_schema: string;
1002 }>(`select s.nspname::text as table_schema
1003 from pg_catalog.pg_namespace s
1004 join pg_catalog.pg_user u on u.usesysid::text = s.nspowner::text
1005 where nspname not in ('information_schema', 'pg_catalog', 'public')
1006 and nspname::text not like 'pg_toast%'
1007 and nspname::text not like 'pg_temp_%'
1008 order by 1;`);
1009
1010 allSchemas.forEach((item) => {
1011 if (schemaFilters.includes(item.table_schema)) {

Callers 1

pgPushIntrospectFunction · 0.50

Calls 5

getColumnsInfoQueryFunction · 0.85
defaultForColumnFunction · 0.85
trimCharFunction · 0.70
queryMethod · 0.45

Tested by

no test coverage detected