utils.cjs 7.8 KB

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