@ignisia/sql 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/README.md +8 -8
  2. package/dist/column/constants.js +97 -8
  3. package/dist/column/index.js +105 -8
  4. package/dist/column/types.js +1 -2
  5. package/dist/database/alter.d.ts +3 -3
  6. package/dist/database/alter.js +87 -15
  7. package/dist/database/column.d.ts +3 -3
  8. package/dist/database/column.js +33 -11
  9. package/dist/database/contract.d.ts +3 -3
  10. package/dist/database/contract.js +1 -0
  11. package/dist/database/index.d.ts +3 -3
  12. package/dist/database/index.js +92 -19
  13. package/dist/database/table.d.ts +3 -3
  14. package/dist/database/table.js +37 -19
  15. package/dist/database/types.d.ts +1 -1
  16. package/dist/database/types.js +1 -0
  17. package/dist/database/wrapper.d.ts +1 -1
  18. package/dist/database/wrapper.js +92 -9
  19. package/dist/{index-Dcm5xIpR.d.ts → index-DFrpzXEn.d.ts} +5 -1
  20. package/dist/{index-DJhQVUY3.d.ts → index-FMT0YEO7.d.ts} +17 -3
  21. package/dist/index.d.ts +3 -3
  22. package/dist/index.js +5 -32
  23. package/dist/migration/index.d.ts +3 -3
  24. package/dist/migration/index.js +48 -6
  25. package/dist/migration/runner.js +8 -21
  26. package/dist/migration/type.d.ts +3 -3
  27. package/dist/migration/type.js +1 -0
  28. package/dist/query/builder.d.ts +1 -1
  29. package/dist/query/builder.js +66 -16
  30. package/dist/query/condition.d.ts +1 -1
  31. package/dist/query/condition.js +97 -24
  32. package/dist/query/constants.d.ts +6 -1
  33. package/dist/query/constants.js +54 -17
  34. package/dist/query/contract.d.ts +1 -1
  35. package/dist/query/contract.js +1 -0
  36. package/dist/query/helper.d.ts +1 -1
  37. package/dist/query/helper.js +30 -18
  38. package/dist/query/index.d.ts +1 -1
  39. package/dist/query/index.js +195 -10
  40. package/dist/query/join.d.ts +1 -1
  41. package/dist/query/join.js +16 -6
  42. package/dist/query/sql.d.ts +1 -1
  43. package/dist/query/sql.js +99 -16
  44. package/dist/query/types.d.ts +1 -1
  45. package/dist/query/types.js +1 -0
  46. package/dist/query/utilities.d.ts +1 -1
  47. package/dist/query/utilities.js +175 -24
  48. package/dist/table/constants.js +5 -5
  49. package/dist/table/index.d.ts +1 -1
  50. package/dist/table/index.js +52 -14
  51. package/dist/table/types.d.ts +1 -1
  52. package/dist/table/types.js +1 -0
  53. package/dist/table/utilities.d.ts +1 -1
  54. package/dist/table/utilities.js +50 -15
  55. package/dist/types.js +1 -0
  56. package/dist/utilities.js +18 -8
  57. package/package.json +37 -2
  58. package/dist/chunk-4DQRB5XS.js +0 -94
  59. package/dist/chunk-CIWX3UCZ.js +0 -51
  60. package/dist/chunk-D2ASIT4Q.js +0 -44
  61. package/dist/chunk-FYSNJAGD.js +0 -19
  62. package/dist/chunk-G3LSCLIQ.js +0 -104
  63. package/dist/chunk-GLOHF5CP.js +0 -9
  64. package/dist/chunk-GY7R637S.js +0 -113
  65. package/dist/chunk-HKTHKQLK.js +0 -98
  66. package/dist/chunk-JF7OSNH4.js +0 -40
  67. package/dist/chunk-MG2S4V4N.js +0 -60
  68. package/dist/chunk-TQ2GXAE7.js +0 -663
  69. package/dist/chunk-V4OMHVJN.js +0 -96
  70. package/dist/chunk-W2DR3ZVK.js +0 -59
  71. package/dist/chunk-WVJGTZFI.js +0 -60
  72. package/dist/chunk-Y7FSRHH3.js +0 -22
@@ -1,5 +1,5 @@
1
1
  import '../column/index.js';
2
- export { D as DatabaseDefinition, b as DatabaseDialect, f as DatabaseOptions, P as PostgresConfig, S as SqliteConfig } from '../index-DJhQVUY3.js';
2
+ export { D as DatabaseDefinition, b as DatabaseDialect, g as DatabaseOptions, P as PostgresConfig, S as SqliteConfig } from '../index-FMT0YEO7.js';
3
3
  import '../table/constants.js';
4
4
  import '../column/constants.js';
5
5
  import '../column/types.js';
@@ -0,0 +1 @@
1
+
@@ -1,7 +1,7 @@
1
1
  import { SQL } from 'bun';
2
2
  import { Database } from 'bun:sqlite';
3
3
  import { Dialect } from '../table/constants.js';
4
- import { b as DatabaseDialect, P as PostgresConfig, S as SqliteConfig } from '../index-DJhQVUY3.js';
4
+ import { b as DatabaseDialect, P as PostgresConfig, S as SqliteConfig } from '../index-FMT0YEO7.js';
5
5
  import '../column/index.js';
6
6
  import '../column/constants.js';
7
7
  import '../column/types.js';
@@ -1,9 +1,92 @@
1
- import {
2
- DatabasePsql,
3
- DatabaseSqlite
4
- } from "../chunk-HKTHKQLK.js";
5
- import "../chunk-GLOHF5CP.js";
6
- export {
7
- DatabasePsql,
8
- DatabaseSqlite
9
- };
1
+ import { SQL } from 'bun';
2
+ import { Database } from 'bun:sqlite';
3
+ import { Dialect } from '../table/constants';
4
+
5
+ class DatabasePsql {
6
+ dialect;
7
+ options;
8
+ client;
9
+ status;
10
+ constructor(options) {
11
+ this.dialect = Dialect.POSTGRES;
12
+ this.options = options;
13
+ this.status = "connecting";
14
+ this.client = new SQL({
15
+ ...options,
16
+ onconnect: () => {
17
+ this.status = "connected";
18
+ },
19
+ onclose: () => {
20
+ this.status = "disconnected";
21
+ }
22
+ });
23
+ this.connect();
24
+ }
25
+ async connect() {
26
+ await this.client.connect();
27
+ return this;
28
+ }
29
+ async disconnect() {
30
+ await this.client.close();
31
+ return this;
32
+ }
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
+ async exec(sql, values) {
35
+ if (!values) {
36
+ return this.client.unsafe(sql);
37
+ }
38
+ return this.client.unsafe(sql, values);
39
+ }
40
+ async transaction(fn) {
41
+ try {
42
+ await this.exec("BEGIN");
43
+ const result = await fn();
44
+ await this.exec("COMMIT");
45
+ return result;
46
+ } catch (err) {
47
+ await this.exec("ROLLBACK");
48
+ throw err;
49
+ }
50
+ }
51
+ }
52
+ class DatabaseSqlite {
53
+ dialect;
54
+ options;
55
+ client;
56
+ status;
57
+ constructor(options) {
58
+ this.dialect = Dialect.SQLITE;
59
+ this.options = options;
60
+ this.status = "connecting";
61
+ this.client = new Database(options.filename);
62
+ this.status = "connected";
63
+ }
64
+ async connect() {
65
+ this.client = new Database(this.options.filename);
66
+ this.status = "connected";
67
+ return this;
68
+ }
69
+ async disconnect() {
70
+ this.client.close();
71
+ this.status = "disconnected";
72
+ return this;
73
+ }
74
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
75
+ async exec(sql, params) {
76
+ const query = this.client.prepare(sql, params);
77
+ return query.all();
78
+ }
79
+ async transaction(fn) {
80
+ try {
81
+ await this.exec("BEGIN");
82
+ const result = await fn();
83
+ await this.exec("COMMIT");
84
+ return result;
85
+ } catch (err) {
86
+ await this.exec("ROLLBACK");
87
+ throw err;
88
+ }
89
+ }
90
+ }
91
+
92
+ export { DatabasePsql, DatabaseSqlite };
@@ -1,5 +1,6 @@
1
1
  import { Column } from './column/index.js';
2
- import { T as Table, D as DatabaseDefinition, a as TimestampOptions, M as MergeTimestampParanoid, b as DatabaseDialect, f as DatabaseOptions, d as QueryBuilder } from './index-DJhQVUY3.js';
2
+ import { T as Table, D as DatabaseDefinition, a as TimestampOptions, M as MergeTimestampParanoid, f as QuerHooks, b as DatabaseDialect, g as DatabaseOptions, d as QueryBuilder, h as QueryRunHooks } from './index-FMT0YEO7.js';
3
+ import { QueryHooksType } from './query/constants.js';
3
4
  import { Dialect } from './table/constants.js';
4
5
  import { AcceptedColumnTypes } from './column/constants.js';
5
6
 
@@ -75,6 +76,7 @@ interface ColumnAlterationContract<DbDialect extends Dialect, Tables extends Rec
75
76
  }
76
77
 
77
78
  declare class Database<DbDialect extends Dialect, Tables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<DatabaseDefinition<DbDialect>> = DatabaseDefinition<DbDialect>> {
79
+ readonly hooks: Partial<QuerHooks>;
78
80
  readonly dialect: DbDialect;
79
81
  readonly defintion: Definition;
80
82
  readonly tables: Tables;
@@ -92,6 +94,8 @@ declare class Database<DbDialect extends Dialect, Tables extends Record<string,
92
94
  dropColumnNotNull: ColumnAlterationContract<DbDialect, Tables, Definition>['dropColumnNotNull'];
93
95
  protected constructor(options: DatabaseOptions<DbDialect, Tables>);
94
96
  table<TableName extends keyof Tables & string, Table extends Tables[TableName]>(tableName: TableName): QueryBuilder<TableName, Table>;
97
+ addHook(type: QueryHooksType, fn: QueryRunHooks): this;
98
+ removeHook(type: QueryHooksType, fn: QueryRunHooks): this;
95
99
  transaction<T, U extends () => Promise<T>>(fn: U): Promise<unknown>;
96
100
  static define<DbDialect extends Dialect, Tables extends Record<string, Table<string, Record<string, Column>>>>(options: DatabaseOptions<DbDialect, Tables>): Database<DbDialect, Tables, DatabaseDefinition<DbDialect>>;
97
101
  }
@@ -1,7 +1,7 @@
1
1
  import { Column } from './column/index.js';
2
2
  import { AcceptedColumnTypes } from './column/constants.js';
3
3
  import { UnionToIntersection } from './types.js';
4
- import { QueryType, OrderBy, AggregationFunction, AcceptedOperator, ConditionClause, LogicalOperator } from './query/constants.js';
4
+ import { QueryType, OrderBy, AggregationFunction, QueryHooksType, AcceptedOperator, ConditionClause, LogicalOperator } from './query/constants.js';
5
5
  import { Dialect } from './table/constants.js';
6
6
 
7
7
  type ColumnSelector<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>> = `${Alias}."${keyof TableRef['columns'] & string}"` | `${Alias}.*` | {
@@ -118,6 +118,19 @@ type SelectQueryOutput<Alias extends string, TableRef extends Table<string, Reco
118
118
  type QueryOutput<Alias extends string, TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables>> = Definition extends {
119
119
  queryType: infer Type;
120
120
  } ? Type extends null ? never : Type extends typeof QueryType.INSERT | typeof QueryType.UPDATE | typeof QueryType.DELETE ? InsertUpdateDeleteQueryOutput<TableRef>[] : Type extends typeof QueryType.SELECT ? SelectQueryOutput<Alias, TableRef, JoinedTables, Definition, AllowedColumn>[] : never : never;
121
+ interface QueryRunHooksOptions {
122
+ query: string;
123
+ hook: QueryHooksType;
124
+ params: unknown[] | null | undefined;
125
+ type: QueryType;
126
+ }
127
+ interface QueryRunHooks {
128
+ (options: QueryRunHooksOptions): void;
129
+ }
130
+ interface QuerHooks {
131
+ after: Set<QueryRunHooks>;
132
+ before: Set<QueryRunHooks>;
133
+ }
121
134
 
122
135
  declare const createdAt: Column<AcceptedColumnTypes, number | readonly string[], {
123
136
  type: "DATETIME";
@@ -190,7 +203,7 @@ interface DatabaseDialect {
190
203
  }
191
204
 
192
205
  declare class Table<TableName extends string, Columns extends Record<string, Column>, DbDialect extends Dialect = Dialect, CreatedAt extends string = string, UpdatedAt extends string = string, Timestamp extends TimestampOptions<CreatedAt, UpdatedAt> | boolean = TimestampOptions<CreatedAt, UpdatedAt> | boolean, Paranoid extends string | boolean = string | boolean> {
193
- database: DatabaseDialect | null;
206
+ client: DatabaseDialect | null;
194
207
  readonly dialect: DbDialect;
195
208
  readonly name: TableName;
196
209
  readonly columns: Columns;
@@ -262,6 +275,7 @@ interface QueryConditionContract<Alias extends string, TableRef extends Table<st
262
275
  }
263
276
 
264
277
  declare class QueryBuilder<Alias extends TableRef['name'], TableRef extends Table<string, Record<string, Column>>, JoinedTables extends Record<string, Table<string, Record<string, Column>>> = NonNullable<unknown>, Definition extends Partial<QueryDefinition<Alias, TableRef, JoinedTables>> = NonNullable<unknown>, AllowedColumn extends ColumnSelector<Alias, TableRef, JoinedTables> = ColumnSelector<Alias, TableRef, JoinedTables>, StrictAllowedColumn extends StrictColumnSelector<Alias, TableRef, JoinedTables> = StrictColumnSelector<Alias, TableRef, JoinedTables>> {
278
+ readonly hooks: Partial<QuerHooks>;
265
279
  readonly table: TableRef;
266
280
  readonly definition: Definition;
267
281
  readonly _output: QueryOutput<Alias, TableRef, JoinedTables, Definition, AllowedColumn>;
@@ -341,4 +355,4 @@ declare class QueryBuilder<Alias extends TableRef['name'], TableRef extends Tabl
341
355
  infer(): this['_output'];
342
356
  }
343
357
 
344
- export { type AliasedColumn as A, rawHaving as B, type ColumnSelector as C, type DatabaseDefinition as D, addCondition as E, where as F, or as G, having as H, type QueryTransformerContract as I, type QueryConditionContract as J, type MergeTimestampParanoid as M, type PostgresConfig as P, type QueryDefinition as Q, type RawColumn as R, type SqliteConfig as S, Table as T, type WhereValue as W, type TimestampOptions as a, type DatabaseDialect as b, type StrictColumnSelector as c, QueryBuilder as d, type SelectableColumn as e, type DatabaseOptions as f, type AcceptedOrderBy as g, type AcceptedInsertValues as h, type AcceptedUpdateValues as i, type AggregateColumn as j, type SelectQueryOutput as k, type QueryOutput as l, createdAt as m, deletedAt as n, defineColumns as o, type TableOptions as p, type TableOutput as q, alias as r, clone as s, rawCol as t, updatedAt as u, col as v, aggregateCol as w, addRawCondition as x, rawWhere as y, rawOr as z };
358
+ export { type AliasedColumn as A, addRawCondition as B, type ColumnSelector as C, type DatabaseDefinition as D, rawWhere as E, rawOr as F, rawHaving as G, addCondition as H, where as I, or as J, having as K, type QueryTransformerContract as L, type MergeTimestampParanoid as M, type QueryConditionContract as N, type PostgresConfig as P, type QueryDefinition as Q, type RawColumn as R, type SqliteConfig as S, Table as T, type WhereValue as W, type TimestampOptions as a, type DatabaseDialect as b, type StrictColumnSelector as c, QueryBuilder as d, type SelectableColumn as e, type QuerHooks as f, type DatabaseOptions as g, type QueryRunHooks as h, type AcceptedOrderBy as i, type AcceptedInsertValues as j, type AcceptedUpdateValues as k, type AggregateColumn as l, type SelectQueryOutput as m, type QueryOutput as n, type QueryRunHooksOptions as o, createdAt as p, deletedAt as q, defineColumns as r, type TableOptions as s, type TableOutput as t, updatedAt as u, alias as v, clone as w, rawCol as x, col as y, aggregateCol as z };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  export { Column } from './column/index.js';
2
- export { D as Database } from './index-Dcm5xIpR.js';
2
+ export { D as Database } from './index-DFrpzXEn.js';
3
3
  export { Migration } from './migration/index.js';
4
- export { d as QueryBuilder, T as Table } from './index-DJhQVUY3.js';
4
+ export { d as QueryBuilder, T as Table } from './index-FMT0YEO7.js';
5
5
  import './table/constants.js';
6
6
  import './column/constants.js';
7
7
  import './column/types.js';
8
+ import './query/constants.js';
8
9
  import './migration/type.js';
9
10
  import './types.js';
10
- import './query/constants.js';
package/dist/index.js CHANGED
@@ -1,32 +1,5 @@
1
- import {
2
- Migration
3
- } from "./chunk-CIWX3UCZ.js";
4
- import {
5
- Database
6
- } from "./chunk-4DQRB5XS.js";
7
- import "./chunk-HKTHKQLK.js";
8
- import "./chunk-JF7OSNH4.js";
9
- import "./chunk-D2ASIT4Q.js";
10
- import {
11
- Table
12
- } from "./chunk-W2DR3ZVK.js";
13
- import {
14
- QueryBuilder
15
- } from "./chunk-TQ2GXAE7.js";
16
- import "./chunk-MG2S4V4N.js";
17
- import "./chunk-FYSNJAGD.js";
18
- import "./chunk-WVJGTZFI.js";
19
- import {
20
- Column
21
- } from "./chunk-GY7R637S.js";
22
- import "./chunk-G3LSCLIQ.js";
23
- import "./chunk-Y7FSRHH3.js";
24
- import "./chunk-V4OMHVJN.js";
25
- import "./chunk-GLOHF5CP.js";
26
- export {
27
- Column,
28
- Database,
29
- Migration,
30
- QueryBuilder,
31
- Table
32
- };
1
+ export { Column } from './column';
2
+ export { Database } from './database';
3
+ export { Migration } from './migration';
4
+ export { QueryBuilder } from './query';
5
+ export { Table } from './table';
@@ -1,12 +1,12 @@
1
1
  import { Column } from '../column/index.js';
2
- import { D as Database } from '../index-Dcm5xIpR.js';
3
- import { T as Table } from '../index-DJhQVUY3.js';
2
+ import { D as Database } from '../index-DFrpzXEn.js';
3
+ import { T as Table } from '../index-FMT0YEO7.js';
4
4
  import { Dialect } from '../table/constants.js';
5
5
  import { MigrationOptions, MigrationFn } from './type.js';
6
6
  import '../column/constants.js';
7
7
  import '../column/types.js';
8
- import '../types.js';
9
8
  import '../query/constants.js';
9
+ import '../types.js';
10
10
 
11
11
  declare class Migration<DbDialect extends Dialect, Tables extends Record<string, Table<string, Record<string, Column>>>> {
12
12
  readonly db: Database<DbDialect, Tables>;
@@ -1,6 +1,48 @@
1
- import {
2
- Migration
3
- } from "../chunk-CIWX3UCZ.js";
4
- export {
5
- Migration
6
- };
1
+ class Migration {
2
+ db;
3
+ _up;
4
+ _down;
5
+ constructor(options) {
6
+ this.db = options.db;
7
+ this._up = options.up;
8
+ this._down = options.down;
9
+ }
10
+ get up() {
11
+ return this._up;
12
+ }
13
+ get down() {
14
+ return this._down;
15
+ }
16
+ static setUp(migrator, fn) {
17
+ migrator._up = () => fn(migrator.db);
18
+ return migrator;
19
+ }
20
+ static setDown(migrator, fn) {
21
+ migrator._down = () => fn(migrator.db);
22
+ return migrator;
23
+ }
24
+ static define(db, options) {
25
+ const migration = new Migration({
26
+ db,
27
+ up: options?.up ? () => options.up(db) : null,
28
+ down: options?.down ? () => options.down(db) : null
29
+ });
30
+ return {
31
+ migration,
32
+ setUp(fn) {
33
+ return Migration.setUp(
34
+ migration,
35
+ fn
36
+ );
37
+ },
38
+ setDown(fn) {
39
+ return Migration.setDown(
40
+ migration,
41
+ fn
42
+ );
43
+ }
44
+ };
45
+ }
46
+ }
47
+
48
+ export { Migration };
@@ -1,26 +1,14 @@
1
- import {
2
- Table
3
- } from "../chunk-W2DR3ZVK.js";
4
- import "../chunk-TQ2GXAE7.js";
5
- import "../chunk-MG2S4V4N.js";
6
- import "../chunk-FYSNJAGD.js";
7
- import "../chunk-WVJGTZFI.js";
8
- import {
9
- Column
10
- } from "../chunk-GY7R637S.js";
11
- import "../chunk-G3LSCLIQ.js";
12
- import "../chunk-Y7FSRHH3.js";
13
- import "../chunk-GLOHF5CP.js";
1
+ import { Column } from '../column';
2
+ import { Table } from '../table';
14
3
 
15
- // src/migration/runner.ts
16
- var nameColumn = Column.define({
4
+ const nameColumn = Column.define({
17
5
  type: "TEXT",
18
6
  length: 255
19
7
  }).primaryKey().notNull();
20
- var runAtColumn = Column.define({
8
+ const runAtColumn = Column.define({
21
9
  type: "DATETIME"
22
10
  }).notNull();
23
- var migrationTable = null;
11
+ let migrationTable = null;
24
12
  async function runMigration(filePath, direction) {
25
13
  const migrationFile = await import(filePath);
26
14
  const fileName = filePath.split("/").pop();
@@ -39,7 +27,7 @@ async function runMigration(filePath, direction) {
39
27
  },
40
28
  dialect: migration.db.dialect
41
29
  });
42
- migrationTable.database = migration.db.client;
30
+ migrationTable.client = migration.db.client;
43
31
  await migrationTable.create();
44
32
  }
45
33
  const fn = migration[direction];
@@ -64,6 +52,5 @@ async function runMigration(filePath, direction) {
64
52
  return query.delete().where('ignisia_migration."name"', "eq", fileName).exec();
65
53
  });
66
54
  }
67
- export {
68
- runMigration
69
- };
55
+
56
+ export { runMigration };
@@ -1,11 +1,11 @@
1
1
  import { Column } from '../column/index.js';
2
- import { D as Database } from '../index-Dcm5xIpR.js';
3
- import { T as Table } from '../index-DJhQVUY3.js';
2
+ import { D as Database } from '../index-DFrpzXEn.js';
3
+ import { T as Table } from '../index-FMT0YEO7.js';
4
4
  import { Dialect } from '../table/constants.js';
5
5
  import '../column/constants.js';
6
6
  import '../column/types.js';
7
- import '../types.js';
8
7
  import '../query/constants.js';
8
+ import '../types.js';
9
9
 
10
10
  interface MigrationOptions<DbDialect extends Dialect, Tables extends Record<string, Table<string, Record<string, Column>>>> {
11
11
  db: Database<DbDialect, Tables>;
@@ -0,0 +1 @@
1
+
@@ -1,4 +1,4 @@
1
- import { T as Table, Q as QueryDefinition, C as ColumnSelector, c as StrictColumnSelector, d as QueryBuilder } from '../index-DJhQVUY3.js';
1
+ import { T as Table, Q as QueryDefinition, C as ColumnSelector, c as StrictColumnSelector, d as QueryBuilder } from '../index-FMT0YEO7.js';
2
2
  import { Column } from '../column/index.js';
3
3
  import '../column/constants.js';
4
4
  import '../types.js';
@@ -1,16 +1,66 @@
1
- import {
2
- buildDeleteQuery,
3
- buildInsertQuery,
4
- buildSelectQuery,
5
- buildUpdateQuery
6
- } from "../chunk-TQ2GXAE7.js";
7
- import "../chunk-MG2S4V4N.js";
8
- import "../chunk-FYSNJAGD.js";
9
- import "../chunk-Y7FSRHH3.js";
10
- import "../chunk-GLOHF5CP.js";
11
- export {
12
- buildDeleteQuery,
13
- buildInsertQuery,
14
- buildSelectQuery,
15
- buildUpdateQuery
16
- };
1
+ import { quoteIdentifier } from '../utilities';
2
+ import { getTableSelectName } from './utilities';
3
+
4
+ function buildSelectQuery(q) {
5
+ const from = getTableSelectName(q);
6
+ const columns = [];
7
+ if (q.definition.select?.length) {
8
+ for (const col of q.definition.select) {
9
+ if (typeof col === "object") {
10
+ const alias = quoteIdentifier(col.as.replace(/"/g, ""));
11
+ columns.push(`${col.column} AS ${alias}`);
12
+ continue;
13
+ }
14
+ if (!col.endsWith("*")) {
15
+ const alias = quoteIdentifier(col.replace(/"/g, ""));
16
+ columns.push(`${col} AS ${alias}`);
17
+ continue;
18
+ }
19
+ columns.push(col);
20
+ }
21
+ }
22
+ if (q.definition?.aggregates) {
23
+ for (const aggregate of q.definition.aggregates) {
24
+ columns.push(
25
+ `${aggregate.fn}(${aggregate.column}) AS ${quoteIdentifier(aggregate.as)}`
26
+ );
27
+ }
28
+ }
29
+ const distinct = q.definition.distinct ? "DISTINCT " : "";
30
+ return `SELECT ${distinct}${columns.join(", ")} FROM ${from}`;
31
+ }
32
+ function buildInsertQuery(q) {
33
+ const rows = q.definition?.insertValues;
34
+ if (!rows?.length) {
35
+ throw new Error(`INSERT requires values`);
36
+ }
37
+ const keys = Object.keys(rows[0]);
38
+ const columns = keys.map(quoteIdentifier).join(", ");
39
+ const rowPlaceholders = `(${keys.map(() => "?").join(", ")})`;
40
+ const placeholders = rows.map(() => rowPlaceholders).join(", ");
41
+ q.definition.params = rows.flatMap(
42
+ (row) => keys.map((key) => row[key])
43
+ );
44
+ return `INSERT INTO ${q.table.name} (${columns}) VALUES ${placeholders} RETURNING *`;
45
+ }
46
+ function buildUpdateQuery(q) {
47
+ if (!q.definition?.updateValues) {
48
+ throw new Error(`UPDATE requires values`);
49
+ }
50
+ let keys = Object.keys(q.definition.updateValues);
51
+ const updateParams = keys.map(
52
+ (key) => q.definition.updateValues[key]
53
+ );
54
+ keys = keys.map(quoteIdentifier);
55
+ if (q.definition?.params) {
56
+ q.definition.params = [...updateParams, ...q.definition.params];
57
+ } else {
58
+ q.definition.params = updateParams;
59
+ }
60
+ return `UPDATE ${q.table.name} SET ${keys.map((key) => `${key} = ?`.trim()).join(", ")}`;
61
+ }
62
+ function buildDeleteQuery(q) {
63
+ return `DELETE FROM ${q.table.name}`;
64
+ }
65
+
66
+ export { buildDeleteQuery, buildInsertQuery, buildSelectQuery, buildUpdateQuery };
@@ -1,4 +1,4 @@
1
- export { E as addCondition, x as addRawCondition, H as having, G as or, B as rawHaving, z as rawOr, y as rawWhere, F as where } from '../index-DJhQVUY3.js';
1
+ export { H as addCondition, B as addRawCondition, K as having, J as or, G as rawHaving, F as rawOr, E as rawWhere, I as where } from '../index-FMT0YEO7.js';
2
2
  import '../column/index.js';
3
3
  import './constants.js';
4
4
  import '../column/constants.js';
@@ -1,24 +1,97 @@
1
- import {
2
- addCondition,
3
- addRawCondition,
4
- having,
5
- or,
6
- rawHaving,
7
- rawOr,
8
- rawWhere,
9
- where
10
- } from "../chunk-TQ2GXAE7.js";
11
- import "../chunk-MG2S4V4N.js";
12
- import "../chunk-FYSNJAGD.js";
13
- import "../chunk-Y7FSRHH3.js";
14
- import "../chunk-GLOHF5CP.js";
15
- export {
16
- addCondition,
17
- addRawCondition,
18
- having,
19
- or,
20
- rawHaving,
21
- rawOr,
22
- rawWhere,
23
- where
24
- };
1
+ import { LogicalOperator, ConditionClause, AcceptedOperator } from './constants';
2
+ import { rawCol } from './helper';
3
+ import { getCondition } from './utilities';
4
+
5
+ function addRawCondition(query, clause, column, logical, params) {
6
+ const validClause = clause.toLowerCase();
7
+ if (!query.definition[validClause]) query.definition[validClause] = [];
8
+ const condition = column(rawCol);
9
+ const logicalPrefix = query.definition[validClause].length > 0 ? logical : "";
10
+ query.definition[validClause].push(`${logicalPrefix} ${condition}`.trim());
11
+ if (!query.definition.params) query.definition.params = [];
12
+ if (typeof params === "undefined") {
13
+ return query;
14
+ }
15
+ if (Array.isArray(params)) {
16
+ query.definition.params.push(...params);
17
+ } else {
18
+ query.definition.params.push(params);
19
+ }
20
+ return query;
21
+ }
22
+ function rawWhere(column, params) {
23
+ return addRawCondition(
24
+ this,
25
+ ConditionClause.WHERE,
26
+ column,
27
+ LogicalOperator.AND,
28
+ params
29
+ );
30
+ }
31
+ function rawOr(column, params) {
32
+ return addRawCondition(
33
+ this,
34
+ ConditionClause.WHERE,
35
+ column,
36
+ LogicalOperator.OR,
37
+ params
38
+ );
39
+ }
40
+ function rawHaving(column, params) {
41
+ return addRawCondition(
42
+ this,
43
+ ConditionClause.HAVING,
44
+ column,
45
+ LogicalOperator.AND,
46
+ params
47
+ );
48
+ }
49
+ function addCondition(query, clause, column, operator, value, logical) {
50
+ const validClause = clause.toLowerCase();
51
+ const condition = getCondition(query.table.dialect, column, operator, value);
52
+ if (!query.definition[validClause]) query.definition[validClause] = [];
53
+ const logicalPrefix = query.definition[validClause].length > 0 ? logical : "";
54
+ query.definition[validClause].push(`${logicalPrefix} ${condition}`.trim());
55
+ if (operator === AcceptedOperator.IS_NULL || operator === AcceptedOperator.IS_NOT_NULL) {
56
+ return query;
57
+ }
58
+ if (!query.definition.params) query.definition.params = [];
59
+ if (Array.isArray(value)) {
60
+ query.definition.params.push(...value);
61
+ } else {
62
+ query.definition.params.push(value);
63
+ }
64
+ return query;
65
+ }
66
+ function where(column, operator, value) {
67
+ return addCondition(
68
+ this,
69
+ ConditionClause.WHERE,
70
+ column,
71
+ operator,
72
+ value,
73
+ LogicalOperator.AND
74
+ );
75
+ }
76
+ function or(column, operator, value) {
77
+ return addCondition(
78
+ this,
79
+ ConditionClause.WHERE,
80
+ column,
81
+ operator,
82
+ value,
83
+ LogicalOperator.OR
84
+ );
85
+ }
86
+ function having(column, operator, value) {
87
+ return addCondition(
88
+ this,
89
+ ConditionClause.HAVING,
90
+ column,
91
+ operator,
92
+ value,
93
+ LogicalOperator.AND
94
+ );
95
+ }
96
+
97
+ export { addCondition, addRawCondition, having, or, rawHaving, rawOr, rawWhere, where };
@@ -54,5 +54,10 @@ declare const AcceptedJoin: {
54
54
  readonly NATURAL: "NATURAL";
55
55
  };
56
56
  type AcceptedJoin = (typeof AcceptedJoin)[keyof typeof AcceptedJoin];
57
+ declare const QueryHooksType: {
58
+ readonly AFTER: "after";
59
+ readonly BEFORE: "before";
60
+ };
61
+ type QueryHooksType = (typeof QueryHooksType)[keyof typeof QueryHooksType];
57
62
 
58
- export { AcceptedJoin, AcceptedOperator, AggregationFunction, ConditionClause, LogicalOperator, OrderBy, QueryType };
63
+ export { AcceptedJoin, AcceptedOperator, AggregationFunction, ConditionClause, LogicalOperator, OrderBy, QueryHooksType, QueryType };