MCPcopy Create free account
hub / github.com/WiseLibs/better-sqlite3 / parseTableDefinition

Function parseTableDefinition

lib/methods/table.js:41–121  ·  view source on GitHub ↗
(def, verb, moduleName)

Source from the content-addressed store, hash-verified

39}
40
41function parseTableDefinition(def, verb, moduleName) {
42 // Validate required properties
43 if (!hasOwnProperty.call(def, 'rows')) {
44 throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "rows" property`);
45 }
46 if (!hasOwnProperty.call(def, 'columns')) {
47 throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition without a "columns" property`);
48 }
49
50 // Validate "rows" property
51 const rows = def.rows;
52 if (typeof rows !== 'function' || Object.getPrototypeOf(rows) !== GeneratorFunctionPrototype) {
53 throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "rows" property (should be a generator function)`);
54 }
55
56 // Validate "columns" property
57 let columns = def.columns;
58 if (!Array.isArray(columns) || !isStringArray(columns = [...columns])) {
59 throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "columns" property (should be an array of strings)`);
60 }
61 if (columns.length !== new Set(columns).size) {
62 throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate column names`);
63 }
64 if (!columns.length) {
65 throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with zero columns`);
66 }
67
68 // Validate "parameters" property
69 let parameters;
70 if (hasOwnProperty.call(def, 'parameters')) {
71 parameters = def.parameters;
72 if (!Array.isArray(parameters) || !isStringArray(parameters = [...parameters])) {
73 throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "parameters" property (should be an array of strings)`);
74 }
75 } else {
76 parameters = inferParameters(rows);
77 }
78 if (parameters.length !== new Set(parameters).size) {
79 throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with duplicate parameter names`);
80 }
81 if (parameters.length > 32) {
82 throw new RangeError(`Virtual table module "${moduleName}" ${verb} a table definition with more than the maximum number of 32 parameters`);
83 }
84 for (const parameter of parameters) {
85 if (columns.includes(parameter)) {
86 throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with column "${parameter}" which was ambiguously defined as both a column and parameter`);
87 }
88 }
89
90 // Validate "safeIntegers" option
91 let safeIntegers = 2;
92 if (hasOwnProperty.call(def, 'safeIntegers')) {
93 const bool = def.safeIntegers;
94 if (typeof bool !== 'boolean') {
95 throw new TypeError(`Virtual table module "${moduleName}" ${verb} a table definition with an invalid "safeIntegers" property (should be a boolean)`);
96 }
97 safeIntegers = +bool;
98 }

Callers 2

table.jsFile · 0.85
wrapFactoryFunction · 0.85

Calls 3

isStringArrayFunction · 0.85
inferParametersFunction · 0.85
wrapGeneratorFunction · 0.85

Tested by

no test coverage detected