AK-21/Graphite-Industrial-Intelligence
0
1"use strict";2var __defProp = Object.defineProperty;3var __getOwnPropDesc = Object.getOwnPropertyDescriptor;4var __getOwnPropNames = Object.getOwnPropertyNames;5var __hasOwnProp = Object.prototype.hasOwnProperty;6var __export = (target, all) => {7 for (var name in all)8 __defProp(target, name, { get: all[name], enumerable: true });9};10var __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};18var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);19var session_exports = {};20__export(session_exports, {21 PlanetScalePreparedQuery: () => PlanetScalePreparedQuery,22 PlanetScaleTransaction: () => PlanetScaleTransaction,23 PlanetscaleSession: () => PlanetscaleSession24});25module.exports = __toCommonJS(session_exports);26var import_core = require("../cache/core/index.cjs");27var import_column = require("../column.cjs");28var import_entity = require("../entity.cjs");29var import_logger = require("../logger.cjs");30var import_session = require("../mysql-core/session.cjs");31var import_sql = require("../sql/sql.cjs");32var import_utils = require("../utils.cjs");33class PlanetScalePreparedQuery extends import_session.MySqlPreparedQuery {34 constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, customResultMapper, generatedIds, returningIds) {35 super(cache, queryMetadata, cacheConfig);36 this.client = client;37 this.queryString = queryString;38 this.params = params;39 this.logger = logger;40 this.fields = fields;41 this.customResultMapper = customResultMapper;42 this.generatedIds = generatedIds;43 this.returningIds = returningIds;44 }45 static [import_entity.entityKind] = "PlanetScalePreparedQuery";46 rawQuery = { as: "object" };47 query = { as: "array" };48 async execute(placeholderValues = {}) {49 const params = (0, import_sql.fillPlaceholders)(this.params, placeholderValues);50 this.logger.logQuery(this.queryString, params);51 const {52 fields,53 client,54 queryString,55 rawQuery,56 query,57 joinsNotNullableMap,58 customResultMapper,59 returningIds,60 generatedIds61 } = this;62 if (!fields && !customResultMapper) {63 const res = await this.queryWithCache(queryString, params, async () => {64 return await client.execute(queryString, params, rawQuery);65 });66 const insertId = Number.parseFloat(res.insertId);67 const affectedRows = res.rowsAffected;68 if (returningIds) {69 const returningResponse = [];70 let j = 0;71 for (let i = insertId; i < insertId + affectedRows; i++) {72 for (const column of returningIds) {73 const key = returningIds[0].path[0];74 if ((0, import_entity.is)(column.field, import_column.Column)) {75 if (column.field.primary && column.field.autoIncrement) {76 returningResponse.push({ [key]: i });77 }78 if (column.field.defaultFn && generatedIds) {79 returningResponse.push({ [key]: generatedIds[j][key] });80 }81 }82 }83 j++;84 }85 return returningResponse;86 }87 return res;88 }89 const { rows } = await this.queryWithCache(queryString, params, async () => {90 return await client.execute(queryString, params, query);91 });92 if (customResultMapper) {93 return customResultMapper(rows);94 }95 return rows.map((row) => (0, import_utils.mapResultRow)(fields, row, joinsNotNullableMap));96 }97 iterator(_placeholderValues) {98 throw new Error("Streaming is not supported by the PlanetScale Serverless driver");99 }100}101class PlanetscaleSession extends import_session.MySqlSession {102 constructor(baseClient, dialect, tx, schema, options = {}) {103 super(dialect);104 this.baseClient = baseClient;105 this.schema = schema;106 this.options = options;107 this.client = tx ?? baseClient;108 this.logger = options.logger ?? new import_logger.NoopLogger();109 this.cache = options.cache ?? new import_core.NoopCache();110 }111 static [import_entity.entityKind] = "PlanetscaleSession";112 logger;113 client;114 cache;115 prepareQuery(query, fields, customResultMapper, generatedIds, returningIds, queryMetadata, cacheConfig) {116 return new PlanetScalePreparedQuery(117 this.client,118 query.sql,119 query.params,120 this.logger,121 this.cache,122 queryMetadata,123 cacheConfig,124 fields,125 customResultMapper,126 generatedIds,127 returningIds128 );129 }130 async query(query, params) {131 this.logger.logQuery(query, params);132 return await this.client.execute(query, params, { as: "array" });133 }134 async queryObjects(query, params) {135 return this.client.execute(query, params, { as: "object" });136 }137 all(query) {138 const querySql = this.dialect.sqlToQuery(query);139 this.logger.logQuery(querySql.sql, querySql.params);140 return this.client.execute(querySql.sql, querySql.params, { as: "object" }).then((eQuery) => eQuery.rows);141 }142 async count(sql2) {143 const res = await this.execute(sql2);144 return Number(145 res["rows"][0]["count"]146 );147 }148 transaction(transaction) {149 return this.baseClient.transaction((pstx) => {150 const session = new PlanetscaleSession(this.baseClient, this.dialect, pstx, this.schema, this.options);151 const tx = new PlanetScaleTransaction(152 this.dialect,153 session,154 this.schema155 );156 return transaction(tx);157 });158 }159}160class PlanetScaleTransaction extends import_session.MySqlTransaction {161 static [import_entity.entityKind] = "PlanetScaleTransaction";162 constructor(dialect, session, schema, nestedIndex = 0) {163 super(dialect, session, schema, nestedIndex, "planetscale");164 }165 async transaction(transaction) {166 const savepointName = `sp${this.nestedIndex + 1}`;167 const tx = new PlanetScaleTransaction(168 this.dialect,169 this.session,170 this.schema,171 this.nestedIndex + 1172 );173 await tx.execute(import_sql.sql.raw(`savepoint ${savepointName}`));174 try {175 const result = await transaction(tx);176 await tx.execute(import_sql.sql.raw(`release savepoint ${savepointName}`));177 return result;178 } catch (err) {179 await tx.execute(import_sql.sql.raw(`rollback to savepoint ${savepointName}`));180 throw err;181 }182 }183}184// Annotate the CommonJS export names for ESM import in node:1850 && (module.exports = {186 PlanetScalePreparedQuery,187 PlanetScaleTransaction,188 PlanetscaleSession189});190//# sourceMappingURL=session.cjs.map