AK-21/Graphite-Industrial-Intelligence
0
1import { hashQuery, NoopCache } from "../cache/core/cache.js";2import { entityKind, is } from "../entity.js";3import { DrizzleError, DrizzleQueryError, TransactionRollbackError } from "../errors.js";4import { QueryPromise } from "../query-promise.js";5import { BaseSQLiteDatabase } from "./db.js";6class ExecuteResultSync extends QueryPromise {7 constructor(resultCb) {8 super();9 this.resultCb = resultCb;10 }11 static [entityKind] = "ExecuteResultSync";12 async execute() {13 return this.resultCb();14 }15 sync() {16 return this.resultCb();17 }18}19class SQLitePreparedQuery {20 constructor(mode, executeMethod, query, cache, queryMetadata, cacheConfig) {21 this.mode = mode;22 this.executeMethod = executeMethod;23 this.query = query;24 this.cache = cache;25 this.queryMetadata = queryMetadata;26 this.cacheConfig = cacheConfig;27 if (cache && cache.strategy() === "all" && cacheConfig === void 0) {28 this.cacheConfig = { enable: true, autoInvalidate: true };29 }30 if (!this.cacheConfig?.enable) {31 this.cacheConfig = void 0;32 }33 }34 static [entityKind] = "PreparedQuery";35 /** @internal */36 joinsNotNullableMap;37 /** @internal */38 async queryWithCache(queryString, params, query) {39 if (this.cache === void 0 || is(this.cache, NoopCache) || this.queryMetadata === void 0) {40 try {41 return await query();42 } catch (e) {43 throw new DrizzleQueryError(queryString, params, e);44 }45 }46 if (this.cacheConfig && !this.cacheConfig.enable) {47 try {48 return await query();49 } catch (e) {50 throw new DrizzleQueryError(queryString, params, e);51 }52 }53 if ((this.queryMetadata.type === "insert" || this.queryMetadata.type === "update" || this.queryMetadata.type === "delete") && this.queryMetadata.tables.length > 0) {54 try {55 const [res] = await Promise.all([56 query(),57 this.cache.onMutate({ tables: this.queryMetadata.tables })58 ]);59 return res;60 } catch (e) {61 throw new DrizzleQueryError(queryString, params, e);62 }63 }64 if (!this.cacheConfig) {65 try {66 return await query();67 } catch (e) {68 throw new DrizzleQueryError(queryString, params, e);69 }70 }71 if (this.queryMetadata.type === "select") {72 const fromCache = await this.cache.get(73 this.cacheConfig.tag ?? (await hashQuery(queryString, params)),74 this.queryMetadata.tables,75 this.cacheConfig.tag !== void 0,76 this.cacheConfig.autoInvalidate77 );78 if (fromCache === void 0) {79 let result;80 try {81 result = await query();82 } catch (e) {83 throw new DrizzleQueryError(queryString, params, e);84 }85 await this.cache.put(86 this.cacheConfig.tag ?? (await hashQuery(queryString, params)),87 result,88 // make sure we send tables that were used in a query only if user wants to invalidate it on each write89 this.cacheConfig.autoInvalidate ? this.queryMetadata.tables : [],90 this.cacheConfig.tag !== void 0,91 this.cacheConfig.config92 );93 return result;94 }95 return fromCache;96 }97 try {98 return await query();99 } catch (e) {100 throw new DrizzleQueryError(queryString, params, e);101 }102 }103 getQuery() {104 return this.query;105 }106 mapRunResult(result, _isFromBatch) {107 return result;108 }109 mapAllResult(_result, _isFromBatch) {110 throw new Error("Not implemented");111 }112 mapGetResult(_result, _isFromBatch) {113 throw new Error("Not implemented");114 }115 execute(placeholderValues) {116 if (this.mode === "async") {117 return this[this.executeMethod](placeholderValues);118 }119 return new ExecuteResultSync(() => this[this.executeMethod](placeholderValues));120 }121 mapResult(response, isFromBatch) {122 switch (this.executeMethod) {123 case "run": {124 return this.mapRunResult(response, isFromBatch);125 }126 case "all": {127 return this.mapAllResult(response, isFromBatch);128 }129 case "get": {130 return this.mapGetResult(response, isFromBatch);131 }132 }133 }134}135class SQLiteSession {136 constructor(dialect) {137 this.dialect = dialect;138 }139 static [entityKind] = "SQLiteSession";140 prepareOneTimeQuery(query, fields, executeMethod, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {141 return this.prepareQuery(142 query,143 fields,144 executeMethod,145 isResponseInArrayMode,146 customResultMapper,147 queryMetadata,148 cacheConfig149 );150 }151 run(query) {152 const staticQuery = this.dialect.sqlToQuery(query);153 try {154 return this.prepareOneTimeQuery(staticQuery, void 0, "run", false).run();155 } catch (err) {156 throw new DrizzleError({ cause: err, message: `Failed to run the query '${staticQuery.sql}'` });157 }158 }159 /** @internal */160 extractRawRunValueFromBatchResult(result) {161 return result;162 }163 all(query) {164 return this.prepareOneTimeQuery(this.dialect.sqlToQuery(query), void 0, "run", false).all();165 }166 /** @internal */167 extractRawAllValueFromBatchResult(_result) {168 throw new Error("Not implemented");169 }170 get(query) {171 return this.prepareOneTimeQuery(this.dialect.sqlToQuery(query), void 0, "run", false).get();172 }173 /** @internal */174 extractRawGetValueFromBatchResult(_result) {175 throw new Error("Not implemented");176 }177 values(query) {178 return this.prepareOneTimeQuery(this.dialect.sqlToQuery(query), void 0, "run", false).values();179 }180 async count(sql) {181 const result = await this.values(sql);182 return result[0][0];183 }184 /** @internal */185 extractRawValuesValueFromBatchResult(_result) {186 throw new Error("Not implemented");187 }188}189class SQLiteTransaction extends BaseSQLiteDatabase {190 constructor(resultType, dialect, session, schema, nestedIndex = 0) {191 super(resultType, dialect, session, schema);192 this.schema = schema;193 this.nestedIndex = nestedIndex;194 }195 static [entityKind] = "SQLiteTransaction";196 rollback() {197 throw new TransactionRollbackError();198 }199}200export {201 ExecuteResultSync,202 SQLitePreparedQuery,203 SQLiteSession,204 SQLiteTransaction205};206//# sourceMappingURL=session.js.map