true-pg 0.4.2 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,6 +33,16 @@ const populatorMap = {
33
33
  domain: extractDomain,
34
34
  range: extractRange,
35
35
  };
36
+ const supported_kinds = [
37
+ "table",
38
+ "view",
39
+ "materializedView",
40
+ "enum",
41
+ "composite",
42
+ "function",
43
+ "domain",
44
+ "range",
45
+ ];
36
46
  export class Extractor {
37
47
  db;
38
48
  /**
@@ -103,11 +113,11 @@ export class Extractor {
103
113
  }
104
114
  const pgTypes = await fetchTypes(db, schemaNames);
105
115
  const filtered = options?.typeFilter ? pgTypes.filter(element => options.typeFilter(element)) : pgTypes;
106
- const typesToExtract = filtered.filter(x => x.kind);
107
- const skipped = filtered.filter(x => !x.kind).map(x => x.name);
116
+ const typesToExtract = filtered.filter(x => supported_kinds.includes(x.kind));
117
+ const skipped = filtered.filter(x => !supported_kinds.includes(x.kind)).map(x => `${x} (${x.kind})`);
108
118
  if (skipped.length) {
109
119
  console.warn("Skipping types of unsupported kinds:", skipped.join(", "));
110
- console.warn("This is a bug!");
120
+ console.warn("This is a bug! Proceeding as if nothing happened.");
111
121
  }
112
122
  options?.onProgressStart?.(typesToExtract.length);
113
123
  const populated = (await Promise.all(typesToExtract.map(async (pgType) => {
package/lib/index.js CHANGED
@@ -10,6 +10,16 @@ const time = (start) => {
10
10
  const end = performance.now();
11
11
  return (end - start).toFixed(2);
12
12
  };
13
+ const filter_overloaded_functions = (functions) => {
14
+ const counts = functions.reduce((acc, func) => {
15
+ acc[func.name] = (acc[func.name] ?? 0) + 1;
16
+ return acc;
17
+ }, {});
18
+ return [
19
+ functions.filter(func => counts[func.name] === 1),
20
+ functions.filter(func => counts[func.name] > 1),
21
+ ];
22
+ };
13
23
  const filter_function = (func) => {
14
24
  const typesToFilter = [
15
25
  "pg_catalog.trigger",
@@ -44,6 +54,11 @@ const filter_function = (func) => {
44
54
  }
45
55
  return func;
46
56
  };
57
+ const filter_unsupported_functions = (functions) => {
58
+ const filtered = functions.filter(filter_function);
59
+ const unsupported = filtered.filter(func => !filtered.includes(func));
60
+ return [filtered, unsupported];
61
+ };
47
62
  const write = (filename, file) => writeFile(filename, file + "\n");
48
63
  const multifile = async (generators, schemas, opts) => {
49
64
  const { out } = opts;
@@ -78,13 +93,20 @@ const multifile = async (generators, schemas, opts) => {
78
93
  for (const schema of Object.values(schemas)) {
79
94
  console.log("Selected schema '%s':\n", schema.name);
80
95
  const schemaDir = joinpath(out, schema.name);
81
- // skip functions that cannot be represented in JavaScript
82
- schema.functions = schema.functions.filter(filter_function);
96
+ const [supported_functions, unsupported_functions] = filter_unsupported_functions(schema.functions);
97
+ const [unique_functions, overloaded_functions] = filter_overloaded_functions(supported_functions);
98
+ schema.functions = unique_functions;
99
+ {
100
+ const skipped = unsupported_functions.map(f => ` - ${f.name}`);
101
+ if (skipped.length) {
102
+ warnings.push(`Skipping ${skipped.length} functions not representable in JavaScript (safe to ignore):\n` +
103
+ skipped.join("\n"));
104
+ }
105
+ }
83
106
  {
84
- const skipped = schema.functions.filter(f => !filter_function(f));
85
- const skipped_functions = skipped.map(f => ` - ${f.name}`).join("\n");
107
+ const skipped = overloaded_functions.map(f => ` - ${f.name}`);
86
108
  if (skipped.length) {
87
- warnings.push(`Skipping ${skipped.length} functions because they cannot be represented in JavaScript (safe to ignore):\n${skipped_functions}`);
109
+ warnings.push(`Skipping ${skipped.length} overloaded functions (not supported):\n` + skipped.join("\n"));
88
110
  }
89
111
  }
90
112
  let createIndex = false;
@@ -17,7 +17,6 @@ export const builtins = {
17
17
  "pg_catalog.varchar": "string",
18
18
  "pg_catalog.text": "string",
19
19
  "pg_catalog.uuid": "string",
20
- "pg_catalog.inet": "string",
21
20
  "pg_catalog.date": "Date",
22
21
  "pg_catalog.time": "Date",
23
22
  "pg_catalog.timetz": "Date",
@@ -32,6 +31,12 @@ export const builtins = {
32
31
  "pg_catalog.record": "Record<string, unknown>",
33
32
  "pg_catalog.void": "void",
34
33
  "pg_catalog.bytea": "string",
34
+ "pg_catalog.inet": "string",
35
+ "pg_catalog.cidr": "string",
36
+ "pg_catalog.macaddr": "string",
37
+ "pg_catalog.macaddr8": "string",
38
+ "pg_catalog.oid": "number",
39
+ "pg_catalog.refcursor": "string",
35
40
  "pg_catalog.vector": "number[]",
36
41
  "pg_catalog.tsvector": "string[]",
37
42
  };
@@ -17,7 +17,6 @@ export const builtins = {
17
17
  "pg_catalog.varchar": "z.string()",
18
18
  "pg_catalog.text": "z.string()",
19
19
  "pg_catalog.uuid": "z.string()",
20
- "pg_catalog.inet": "z.string()",
21
20
  "pg_catalog.date": "z.coerce.date()",
22
21
  "pg_catalog.time": "z.coerce.date()",
23
22
  "pg_catalog.timetz": "z.coerce.date()",
@@ -32,6 +31,12 @@ export const builtins = {
32
31
  "pg_catalog.record": "z.record(z.string(), z.unknown())",
33
32
  "pg_catalog.void": "z.void()",
34
33
  "pg_catalog.bytea": "z.string()",
34
+ "pg_catalog.inet": "z.string()",
35
+ "pg_catalog.cidr": "z.string()",
36
+ "pg_catalog.macaddr": "z.string()",
37
+ "pg_catalog.macaddr8": "z.string()",
38
+ "pg_catalog.oid": "z.number()",
39
+ "pg_catalog.refcursor": "z.string()",
35
40
  "pg_catalog.vector": "z.number().array()",
36
41
  "pg_catalog.tsvector": "z.string().array()",
37
42
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "true-pg",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "type": "module",
5
5
  "module": "lib/index.js",
6
6
  "main": "lib/index.js",