db.cjs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 db_exports = {};
  20. __export(db_exports, {
  21. SingleStoreDatabase: () => SingleStoreDatabase,
  22. withReplicas: () => withReplicas
  23. });
  24. module.exports = __toCommonJS(db_exports);
  25. var import_entity = require("../entity.cjs");
  26. var import_selection_proxy = require("../selection-proxy.cjs");
  27. var import_sql = require("../sql/sql.cjs");
  28. var import_subquery = require("../subquery.cjs");
  29. var import_count = require("./query-builders/count.cjs");
  30. var import_query_builders = require("./query-builders/index.cjs");
  31. class SingleStoreDatabase {
  32. constructor(dialect, session, schema) {
  33. this.dialect = dialect;
  34. this.session = session;
  35. this._ = schema ? {
  36. schema: schema.schema,
  37. fullSchema: schema.fullSchema,
  38. tableNamesMap: schema.tableNamesMap
  39. } : {
  40. schema: void 0,
  41. fullSchema: {},
  42. tableNamesMap: {}
  43. };
  44. this.query = {};
  45. this.$cache = { invalidate: async (_params) => {
  46. } };
  47. }
  48. static [import_entity.entityKind] = "SingleStoreDatabase";
  49. // We are waiting for SingleStore support for `json_array` function
  50. /**@inrernal */
  51. query;
  52. /**
  53. * Creates a subquery that defines a temporary named result set as a CTE.
  54. *
  55. * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
  56. *
  57. * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
  58. *
  59. * @param alias The alias for the subquery.
  60. *
  61. * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
  62. *
  63. * @example
  64. *
  65. * ```ts
  66. * // Create a subquery with alias 'sq' and use it in the select query
  67. * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
  68. *
  69. * const result = await db.with(sq).select().from(sq);
  70. * ```
  71. *
  72. * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
  73. *
  74. * ```ts
  75. * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
  76. * const sq = db.$with('sq').as(db.select({
  77. * name: sql<string>`upper(${users.name})`.as('name'),
  78. * })
  79. * .from(users));
  80. *
  81. * const result = await db.with(sq).select({ name: sq.name }).from(sq);
  82. * ```
  83. */
  84. $with = (alias, selection) => {
  85. const self = this;
  86. const as = (qb) => {
  87. if (typeof qb === "function") {
  88. qb = qb(new import_query_builders.QueryBuilder(self.dialect));
  89. }
  90. return new Proxy(
  91. new import_subquery.WithSubquery(
  92. qb.getSQL(),
  93. selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}),
  94. alias,
  95. true
  96. ),
  97. new import_selection_proxy.SelectionProxyHandler({ alias, sqlAliasedBehavior: "alias", sqlBehavior: "error" })
  98. );
  99. };
  100. return { as };
  101. };
  102. $count(source, filters) {
  103. return new import_count.SingleStoreCountBuilder({ source, filters, session: this.session });
  104. }
  105. /**
  106. * Incorporates a previously defined CTE (using `$with`) into the main query.
  107. *
  108. * This method allows the main query to reference a temporary named result set.
  109. *
  110. * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
  111. *
  112. * @param queries The CTEs to incorporate into the main query.
  113. *
  114. * @example
  115. *
  116. * ```ts
  117. * // Define a subquery 'sq' as a CTE using $with
  118. * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
  119. *
  120. * // Incorporate the CTE 'sq' into the main query and select from it
  121. * const result = await db.with(sq).select().from(sq);
  122. * ```
  123. */
  124. with(...queries) {
  125. const self = this;
  126. function select(fields) {
  127. return new import_query_builders.SingleStoreSelectBuilder({
  128. fields: fields ?? void 0,
  129. session: self.session,
  130. dialect: self.dialect,
  131. withList: queries
  132. });
  133. }
  134. function selectDistinct(fields) {
  135. return new import_query_builders.SingleStoreSelectBuilder({
  136. fields: fields ?? void 0,
  137. session: self.session,
  138. dialect: self.dialect,
  139. withList: queries,
  140. distinct: true
  141. });
  142. }
  143. function update(table) {
  144. return new import_query_builders.SingleStoreUpdateBuilder(table, self.session, self.dialect, queries);
  145. }
  146. function delete_(table) {
  147. return new import_query_builders.SingleStoreDeleteBase(table, self.session, self.dialect, queries);
  148. }
  149. return { select, selectDistinct, update, delete: delete_ };
  150. }
  151. select(fields) {
  152. return new import_query_builders.SingleStoreSelectBuilder({ fields: fields ?? void 0, session: this.session, dialect: this.dialect });
  153. }
  154. selectDistinct(fields) {
  155. return new import_query_builders.SingleStoreSelectBuilder({
  156. fields: fields ?? void 0,
  157. session: this.session,
  158. dialect: this.dialect,
  159. distinct: true
  160. });
  161. }
  162. /**
  163. * Creates an update query.
  164. *
  165. * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.
  166. *
  167. * Use `.set()` method to specify which values to update.
  168. *
  169. * See docs: {@link https://orm.drizzle.team/docs/update}
  170. *
  171. * @param table The table to update.
  172. *
  173. * @example
  174. *
  175. * ```ts
  176. * // Update all rows in the 'cars' table
  177. * await db.update(cars).set({ color: 'red' });
  178. *
  179. * // Update rows with filters and conditions
  180. * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));
  181. * ```
  182. */
  183. update(table) {
  184. return new import_query_builders.SingleStoreUpdateBuilder(table, this.session, this.dialect);
  185. }
  186. /**
  187. * Creates an insert query.
  188. *
  189. * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
  190. *
  191. * See docs: {@link https://orm.drizzle.team/docs/insert}
  192. *
  193. * @param table The table to insert into.
  194. *
  195. * @example
  196. *
  197. * ```ts
  198. * // Insert one row
  199. * await db.insert(cars).values({ brand: 'BMW' });
  200. *
  201. * // Insert multiple rows
  202. * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
  203. * ```
  204. */
  205. insert(table) {
  206. return new import_query_builders.SingleStoreInsertBuilder(table, this.session, this.dialect);
  207. }
  208. /**
  209. * Creates a delete query.
  210. *
  211. * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.
  212. *
  213. * See docs: {@link https://orm.drizzle.team/docs/delete}
  214. *
  215. * @param table The table to delete from.
  216. *
  217. * @example
  218. *
  219. * ```ts
  220. * // Delete all rows in the 'cars' table
  221. * await db.delete(cars);
  222. *
  223. * // Delete rows with filters and conditions
  224. * await db.delete(cars).where(eq(cars.color, 'green'));
  225. * ```
  226. */
  227. delete(table) {
  228. return new import_query_builders.SingleStoreDeleteBase(table, this.session, this.dialect);
  229. }
  230. execute(query) {
  231. return this.session.execute(typeof query === "string" ? import_sql.sql.raw(query) : query.getSQL());
  232. }
  233. $cache;
  234. transaction(transaction, config) {
  235. return this.session.transaction(transaction, config);
  236. }
  237. }
  238. const withReplicas = (primary, replicas, getReplica = () => replicas[Math.floor(Math.random() * replicas.length)]) => {
  239. const select = (...args) => getReplica(replicas).select(...args);
  240. const selectDistinct = (...args) => getReplica(replicas).selectDistinct(...args);
  241. const $count = (...args) => getReplica(replicas).$count(...args);
  242. const $with = (...args) => getReplica(replicas).with(...args);
  243. const update = (...args) => primary.update(...args);
  244. const insert = (...args) => primary.insert(...args);
  245. const $delete = (...args) => primary.delete(...args);
  246. const execute = (...args) => primary.execute(...args);
  247. const transaction = (...args) => primary.transaction(...args);
  248. return {
  249. ...primary,
  250. update,
  251. insert,
  252. delete: $delete,
  253. execute,
  254. transaction,
  255. $primary: primary,
  256. $replicas: replicas,
  257. select,
  258. selectDistinct,
  259. $count,
  260. with: $with,
  261. get query() {
  262. return getReplica(replicas).query;
  263. }
  264. };
  265. };
  266. // Annotate the CommonJS export names for ESM import in node:
  267. 0 && (module.exports = {
  268. SingleStoreDatabase,
  269. withReplicas
  270. });
  271. //# sourceMappingURL=db.cjs.map