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

Function construct

drizzle-orm/src/postgres-js/driver.ts:22–68  ·  view source on GitHub ↗
(
	client: Sql,
	config: DrizzleConfig<TSchema> = {},
)

Source from the content-addressed store, hash-verified

20}
21
22function construct<TSchema extends Record<string, unknown> = Record<string, never>>(
23 client: Sql,
24 config: DrizzleConfig<TSchema> = {},
25): PostgresJsDatabase<TSchema> & {
26 $client: Sql;
27} {
28 const transparentParser = (val: any) => val;
29
30 // Override postgres.js default date parsers: https://github.com/porsager/postgres/discussions/761
31 for (const type of ['1184', '1082', '1083', '1114', '1182', '1185', '1115', '1231']) {
32 client.options.parsers[type as any] = transparentParser;
33 client.options.serializers[type as any] = transparentParser;
34 }
35 client.options.serializers['114'] = transparentParser;
36 client.options.serializers['3802'] = transparentParser;
37
38 const dialect = new PgDialect({ casing: config.casing });
39 let logger;
40 if (config.logger === true) {
41 logger = new DefaultLogger();
42 } else if (config.logger !== false) {
43 logger = config.logger;
44 }
45
46 let schema: RelationalSchemaConfig<TablesRelationalConfig> | undefined;
47 if (config.schema) {
48 const tablesConfig = extractTablesRelationalConfig(
49 config.schema,
50 createTableRelationsHelpers,
51 );
52 schema = {
53 fullSchema: config.schema,
54 schema: tablesConfig.tables,
55 tableNamesMap: tablesConfig.tableNamesMap,
56 };
57 }
58
59 const session = new PostgresJsSession(client, dialect, schema, { logger, cache: config.cache });
60 const db = new PostgresJsDatabase(dialect, session, schema as any) as PostgresJsDatabase<TSchema>;
61 (<any> db).$client = client;
62 (<any> db).$cache = config.cache;
63 if ((<any> db).$cache) {
64 (<any> db).$cache['invalidate'] = config.cache?.onMutate;
65 }
66
67 return db as any;
68}
69
70export function drizzle<
71 TSchema extends Record<string, unknown> = Record<string, never>,

Callers 2

drizzleFunction · 0.70
mockFunction · 0.70

Calls 1

Tested by

no test coverage detected