utils.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { Column } from "./column.js";
  2. import { is } from "./entity.js";
  3. import { Param, SQL, View } from "./sql/sql.js";
  4. import { Subquery } from "./subquery.js";
  5. import { getTableName, Table } from "./table.js";
  6. import { ViewBaseConfig } from "./view-common.js";
  7. function mapResultRow(columns, row, joinsNotNullableMap) {
  8. const nullifyMap = {};
  9. const result = columns.reduce(
  10. (result2, { path, field }, columnIndex) => {
  11. let decoder;
  12. if (is(field, Column)) {
  13. decoder = field;
  14. } else if (is(field, SQL)) {
  15. decoder = field.decoder;
  16. } else if (is(field, Subquery)) {
  17. decoder = field._.sql.decoder;
  18. } else {
  19. decoder = field.sql.decoder;
  20. }
  21. let node = result2;
  22. for (const [pathChunkIndex, pathChunk] of path.entries()) {
  23. if (pathChunkIndex < path.length - 1) {
  24. if (!(pathChunk in node)) {
  25. node[pathChunk] = {};
  26. }
  27. node = node[pathChunk];
  28. } else {
  29. const rawValue = row[columnIndex];
  30. const value = node[pathChunk] = rawValue === null ? null : decoder.mapFromDriverValue(rawValue);
  31. if (joinsNotNullableMap && is(field, Column) && path.length === 2) {
  32. const objectName = path[0];
  33. if (!(objectName in nullifyMap)) {
  34. nullifyMap[objectName] = value === null ? getTableName(field.table) : false;
  35. } else if (typeof nullifyMap[objectName] === "string" && nullifyMap[objectName] !== getTableName(field.table)) {
  36. nullifyMap[objectName] = false;
  37. }
  38. }
  39. }
  40. }
  41. return result2;
  42. },
  43. {}
  44. );
  45. if (joinsNotNullableMap && Object.keys(nullifyMap).length > 0) {
  46. for (const [objectName, tableName] of Object.entries(nullifyMap)) {
  47. if (typeof tableName === "string" && !joinsNotNullableMap[tableName]) {
  48. result[objectName] = null;
  49. }
  50. }
  51. }
  52. return result;
  53. }
  54. function orderSelectedFields(fields, pathPrefix) {
  55. return Object.entries(fields).reduce((result, [name, field]) => {
  56. if (typeof name !== "string") {
  57. return result;
  58. }
  59. const newPath = pathPrefix ? [...pathPrefix, name] : [name];
  60. if (is(field, Column) || is(field, SQL) || is(field, SQL.Aliased) || is(field, Subquery)) {
  61. result.push({ path: newPath, field });
  62. } else if (is(field, Table)) {
  63. result.push(...orderSelectedFields(field[Table.Symbol.Columns], newPath));
  64. } else {
  65. result.push(...orderSelectedFields(field, newPath));
  66. }
  67. return result;
  68. }, []);
  69. }
  70. function haveSameKeys(left, right) {
  71. const leftKeys = Object.keys(left);
  72. const rightKeys = Object.keys(right);
  73. if (leftKeys.length !== rightKeys.length) {
  74. return false;
  75. }
  76. for (const [index, key] of leftKeys.entries()) {
  77. if (key !== rightKeys[index]) {
  78. return false;
  79. }
  80. }
  81. return true;
  82. }
  83. function mapUpdateSet(table, values) {
  84. const entries = Object.entries(values).filter(([, value]) => value !== void 0).map(([key, value]) => {
  85. if (is(value, SQL) || is(value, Column)) {
  86. return [key, value];
  87. } else {
  88. return [key, new Param(value, table[Table.Symbol.Columns][key])];
  89. }
  90. });
  91. if (entries.length === 0) {
  92. throw new Error("No values to set");
  93. }
  94. return Object.fromEntries(entries);
  95. }
  96. function applyMixins(baseClass, extendedClasses) {
  97. for (const extendedClass of extendedClasses) {
  98. for (const name of Object.getOwnPropertyNames(extendedClass.prototype)) {
  99. if (name === "constructor") continue;
  100. Object.defineProperty(
  101. baseClass.prototype,
  102. name,
  103. Object.getOwnPropertyDescriptor(extendedClass.prototype, name) || /* @__PURE__ */ Object.create(null)
  104. );
  105. }
  106. }
  107. }
  108. function getTableColumns(table) {
  109. return table[Table.Symbol.Columns];
  110. }
  111. function getViewSelectedFields(view) {
  112. return view[ViewBaseConfig].selectedFields;
  113. }
  114. function getTableLikeName(table) {
  115. return is(table, Subquery) ? table._.alias : is(table, View) ? table[ViewBaseConfig].name : is(table, SQL) ? void 0 : table[Table.Symbol.IsAlias] ? table[Table.Symbol.Name] : table[Table.Symbol.BaseName];
  116. }
  117. function getColumnNameAndConfig(a, b) {
  118. return {
  119. name: typeof a === "string" && a.length > 0 ? a : "",
  120. config: typeof a === "object" ? a : b
  121. };
  122. }
  123. const _ = {};
  124. const __ = {};
  125. function isConfig(data) {
  126. if (typeof data !== "object" || data === null) return false;
  127. if (data.constructor.name !== "Object") return false;
  128. if ("logger" in data) {
  129. const type = typeof data["logger"];
  130. if (type !== "boolean" && (type !== "object" || typeof data["logger"]["logQuery"] !== "function") && type !== "undefined") return false;
  131. return true;
  132. }
  133. if ("schema" in data) {
  134. const type = typeof data["schema"];
  135. if (type !== "object" && type !== "undefined") return false;
  136. return true;
  137. }
  138. if ("casing" in data) {
  139. const type = typeof data["casing"];
  140. if (type !== "string" && type !== "undefined") return false;
  141. return true;
  142. }
  143. if ("mode" in data) {
  144. if (data["mode"] !== "default" || data["mode"] !== "planetscale" || data["mode"] !== void 0) return false;
  145. return true;
  146. }
  147. if ("connection" in data) {
  148. const type = typeof data["connection"];
  149. if (type !== "string" && type !== "object" && type !== "undefined") return false;
  150. return true;
  151. }
  152. if ("client" in data) {
  153. const type = typeof data["client"];
  154. if (type !== "object" && type !== "function" && type !== "undefined") return false;
  155. return true;
  156. }
  157. if (Object.keys(data).length === 0) return true;
  158. return false;
  159. }
  160. const textDecoder = typeof TextDecoder === "undefined" ? null : new TextDecoder();
  161. export {
  162. applyMixins,
  163. getColumnNameAndConfig,
  164. getTableColumns,
  165. getTableLikeName,
  166. getViewSelectedFields,
  167. haveSameKeys,
  168. isConfig,
  169. mapResultRow,
  170. mapUpdateSet,
  171. orderSelectedFields,
  172. textDecoder
  173. };
  174. //# sourceMappingURL=utils.js.map