| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- import { Column } from "./column.js";
- import { is } from "./entity.js";
- import { Param, SQL, View } from "./sql/sql.js";
- import { Subquery } from "./subquery.js";
- import { getTableName, Table } from "./table.js";
- import { ViewBaseConfig } from "./view-common.js";
- function mapResultRow(columns, row, joinsNotNullableMap) {
- const nullifyMap = {};
- const result = columns.reduce(
- (result2, { path, field }, columnIndex) => {
- let decoder;
- if (is(field, Column)) {
- decoder = field;
- } else if (is(field, SQL)) {
- decoder = field.decoder;
- } else if (is(field, Subquery)) {
- decoder = field._.sql.decoder;
- } else {
- decoder = field.sql.decoder;
- }
- let node = result2;
- for (const [pathChunkIndex, pathChunk] of path.entries()) {
- if (pathChunkIndex < path.length - 1) {
- if (!(pathChunk in node)) {
- node[pathChunk] = {};
- }
- node = node[pathChunk];
- } else {
- const rawValue = row[columnIndex];
- const value = node[pathChunk] = rawValue === null ? null : decoder.mapFromDriverValue(rawValue);
- if (joinsNotNullableMap && is(field, Column) && path.length === 2) {
- const objectName = path[0];
- if (!(objectName in nullifyMap)) {
- nullifyMap[objectName] = value === null ? getTableName(field.table) : false;
- } else if (typeof nullifyMap[objectName] === "string" && nullifyMap[objectName] !== getTableName(field.table)) {
- nullifyMap[objectName] = false;
- }
- }
- }
- }
- return result2;
- },
- {}
- );
- if (joinsNotNullableMap && Object.keys(nullifyMap).length > 0) {
- for (const [objectName, tableName] of Object.entries(nullifyMap)) {
- if (typeof tableName === "string" && !joinsNotNullableMap[tableName]) {
- result[objectName] = null;
- }
- }
- }
- return result;
- }
- function orderSelectedFields(fields, pathPrefix) {
- return Object.entries(fields).reduce((result, [name, field]) => {
- if (typeof name !== "string") {
- return result;
- }
- const newPath = pathPrefix ? [...pathPrefix, name] : [name];
- if (is(field, Column) || is(field, SQL) || is(field, SQL.Aliased) || is(field, Subquery)) {
- result.push({ path: newPath, field });
- } else if (is(field, Table)) {
- result.push(...orderSelectedFields(field[Table.Symbol.Columns], newPath));
- } else {
- result.push(...orderSelectedFields(field, newPath));
- }
- return result;
- }, []);
- }
- function haveSameKeys(left, right) {
- const leftKeys = Object.keys(left);
- const rightKeys = Object.keys(right);
- if (leftKeys.length !== rightKeys.length) {
- return false;
- }
- for (const [index, key] of leftKeys.entries()) {
- if (key !== rightKeys[index]) {
- return false;
- }
- }
- return true;
- }
- function mapUpdateSet(table, values) {
- const entries = Object.entries(values).filter(([, value]) => value !== void 0).map(([key, value]) => {
- if (is(value, SQL) || is(value, Column)) {
- return [key, value];
- } else {
- return [key, new Param(value, table[Table.Symbol.Columns][key])];
- }
- });
- if (entries.length === 0) {
- throw new Error("No values to set");
- }
- return Object.fromEntries(entries);
- }
- function applyMixins(baseClass, extendedClasses) {
- for (const extendedClass of extendedClasses) {
- for (const name of Object.getOwnPropertyNames(extendedClass.prototype)) {
- if (name === "constructor") continue;
- Object.defineProperty(
- baseClass.prototype,
- name,
- Object.getOwnPropertyDescriptor(extendedClass.prototype, name) || /* @__PURE__ */ Object.create(null)
- );
- }
- }
- }
- function getTableColumns(table) {
- return table[Table.Symbol.Columns];
- }
- function getViewSelectedFields(view) {
- return view[ViewBaseConfig].selectedFields;
- }
- function getTableLikeName(table) {
- 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];
- }
- function getColumnNameAndConfig(a, b) {
- return {
- name: typeof a === "string" && a.length > 0 ? a : "",
- config: typeof a === "object" ? a : b
- };
- }
- const _ = {};
- const __ = {};
- function isConfig(data) {
- if (typeof data !== "object" || data === null) return false;
- if (data.constructor.name !== "Object") return false;
- if ("logger" in data) {
- const type = typeof data["logger"];
- if (type !== "boolean" && (type !== "object" || typeof data["logger"]["logQuery"] !== "function") && type !== "undefined") return false;
- return true;
- }
- if ("schema" in data) {
- const type = typeof data["schema"];
- if (type !== "object" && type !== "undefined") return false;
- return true;
- }
- if ("casing" in data) {
- const type = typeof data["casing"];
- if (type !== "string" && type !== "undefined") return false;
- return true;
- }
- if ("mode" in data) {
- if (data["mode"] !== "default" || data["mode"] !== "planetscale" || data["mode"] !== void 0) return false;
- return true;
- }
- if ("connection" in data) {
- const type = typeof data["connection"];
- if (type !== "string" && type !== "object" && type !== "undefined") return false;
- return true;
- }
- if ("client" in data) {
- const type = typeof data["client"];
- if (type !== "object" && type !== "function" && type !== "undefined") return false;
- return true;
- }
- if (Object.keys(data).length === 0) return true;
- return false;
- }
- const textDecoder = typeof TextDecoder === "undefined" ? null : new TextDecoder();
- export {
- applyMixins,
- getColumnNameAndConfig,
- getTableColumns,
- getTableLikeName,
- getViewSelectedFields,
- haveSameKeys,
- isConfig,
- mapResultRow,
- mapUpdateSet,
- orderSelectedFields,
- textDecoder
- };
- //# sourceMappingURL=utils.js.map
|