bun-query-builder 0.2.30 → 0.2.31

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.
package/dist/bin/cli.js CHANGED
@@ -18518,6 +18518,28 @@ ${sql};` : `${sql};`;
18518
18518
  recordMigrationQuery() {
18519
18519
  return "INSERT INTO migrations (migration) VALUES (?)";
18520
18520
  }
18521
+ jsonExtract(column, key) {
18522
+ return `JSON_UNQUOTE(JSON_EXTRACT(${column}, CONCAT('$.', ${key})))`;
18523
+ }
18524
+ dateBucket(column, grain, offsetHours = 0) {
18525
+ const shifted = offsetHours === 0 ? column : `DATE_ADD(${column}, INTERVAL ${offsetHours} HOUR)`;
18526
+ switch (grain) {
18527
+ case "hour":
18528
+ return `DATE_FORMAT(${shifted}, '%Y-%m-%dT%H:00:00.000Z')`;
18529
+ case "week":
18530
+ return `DATE_FORMAT(DATE_SUB(${shifted}, INTERVAL WEEKDAY(${shifted}) DAY), '%Y-%m-%dT00:00:00.000Z')`;
18531
+ case "month":
18532
+ return `DATE_FORMAT(${shifted}, '%Y-%m-01T00:00:00.000Z')`;
18533
+ case "year":
18534
+ return `DATE_FORMAT(${shifted}, '%Y-01-01T00:00:00.000Z')`;
18535
+ case "day":
18536
+ default:
18537
+ return `DATE_FORMAT(${shifted}, '%Y-%m-%dT00:00:00.000Z')`;
18538
+ }
18539
+ }
18540
+ booleanLiteral(value) {
18541
+ return value ? "TRUE" : "FALSE";
18542
+ }
18521
18543
  renderColumn(column) {
18522
18544
  const typeSql = this.getColumnType(column);
18523
18545
  const parts = [this.quoteIdentifier(column.name), typeSql];
@@ -18715,6 +18737,28 @@ ${sql};` : `${sql};`;
18715
18737
  recordMigrationQuery() {
18716
18738
  return "INSERT INTO migrations (migration) VALUES ($1)";
18717
18739
  }
18740
+ jsonExtract(column, key) {
18741
+ return `(${column}::jsonb ->> ${key})`;
18742
+ }
18743
+ dateBucket(column, grain, offsetHours = 0) {
18744
+ const shifted = offsetHours === 0 ? `${column}::timestamp` : `(${column}::timestamp ${offsetHours >= 0 ? "+" : "-"} interval '${Math.abs(offsetHours)} hours')`;
18745
+ const truncated = grain === "week" ? `date_trunc('week', ${shifted})` : shifted;
18746
+ switch (grain) {
18747
+ case "hour":
18748
+ return `to_char(${truncated}, 'YYYY-MM-DD"T"HH24:00:00.000"Z"')`;
18749
+ case "month":
18750
+ return `to_char(${truncated}, 'YYYY-MM-01"T"00:00:00.000"Z"')`;
18751
+ case "year":
18752
+ return `to_char(${truncated}, 'YYYY-01-01"T"00:00:00.000"Z"')`;
18753
+ case "week":
18754
+ case "day":
18755
+ default:
18756
+ return `to_char(${truncated}, 'YYYY-MM-DD"T"00:00:00.000"Z"')`;
18757
+ }
18758
+ }
18759
+ booleanLiteral(value) {
18760
+ return value ? "TRUE" : "FALSE";
18761
+ }
18718
18762
  renderColumn(column) {
18719
18763
  const typeSql = column.isPrimaryKey ? this.getPrimaryKeyType(column) : this.getColumnType(column);
18720
18764
  const parts = [this.quoteIdentifier(column.name), typeSql];
@@ -18998,6 +19042,28 @@ class SQLiteDriver {
18998
19042
  recordMigrationQuery() {
18999
19043
  return "INSERT INTO migrations (migration) VALUES (?)";
19000
19044
  }
19045
+ jsonExtract(column, key) {
19046
+ return `json_extract(${column}, '$.' || ${key})`;
19047
+ }
19048
+ dateBucket(column, grain, offsetHours = 0) {
19049
+ const shifted = offsetHours === 0 ? column : `datetime(${column}, '${offsetHours >= 0 ? "+" : "-"}${Math.abs(offsetHours)} hours')`;
19050
+ switch (grain) {
19051
+ case "hour":
19052
+ return `strftime('%Y-%m-%dT%H:00:00.000Z', ${shifted})`;
19053
+ case "week":
19054
+ return `strftime('%Y-%m-%dT00:00:00.000Z', ${shifted}, '-6 days', 'weekday 1')`;
19055
+ case "month":
19056
+ return `strftime('%Y-%m-01T00:00:00.000Z', ${shifted})`;
19057
+ case "year":
19058
+ return `strftime('%Y-01-01T00:00:00.000Z', ${shifted})`;
19059
+ case "day":
19060
+ default:
19061
+ return `strftime('%Y-%m-%dT00:00:00.000Z', ${shifted})`;
19062
+ }
19063
+ }
19064
+ booleanLiteral(value) {
19065
+ return value ? "TRUE" : "FALSE";
19066
+ }
19001
19067
  renderColumn(column) {
19002
19068
  const typeSql = this.getColumnType(column);
19003
19069
  const parts = [this.quoteIdentifier(column.name), typeSql];
@@ -32067,7 +32133,7 @@ function getPrefix() {
32067
32133
  }
32068
32134
  var prefix = getPrefix();
32069
32135
  // package.json
32070
- var version2 = "0.2.30";
32136
+ var version2 = "0.2.31";
32071
32137
 
32072
32138
  // bin/cli.ts
32073
32139
  init_actions();
@@ -1,4 +1,5 @@
1
1
  import type { ColumnPlan, IndexPlan, RebuildTableSpec, TablePlan } from '../migrations';
2
+ import type { DateBucketGrain } from './postgres';
2
3
  export declare interface DialectDriver {
3
4
  createEnumType: (enumTypeName: string, values: string[]) => string
4
5
  createTable: (table: TablePlan) => string
@@ -39,5 +40,8 @@ export declare class MySQLDriver implements DialectDriver {
39
40
  createMigrationsTable(): string;
40
41
  getExecutedMigrationsQuery(): string;
41
42
  recordMigrationQuery(): string;
43
+ jsonExtract(column: string, key: string): string;
44
+ dateBucket(column: string, grain: DateBucketGrain, offsetHours?: number): string;
45
+ booleanLiteral(value: boolean): string;
42
46
  protected renderColumn(column: ColumnPlan): string;
43
47
  }
@@ -1,4 +1,11 @@
1
1
  import type { ColumnPlan, IndexPlan, RebuildTableSpec, TablePlan } from '../migrations';
2
+ /**
3
+ * The ISO shape every driver's {@link DialectDriver.dateBucket} must produce.
4
+ *
5
+ * Exported so a driver's tests can assert against one definition rather than
6
+ * each restating the format it happens to emit.
7
+ */
8
+ export declare const DATE_BUCKET_FORMAT: 'YYYY-MM-DDTHH:MM:SS.000Z';
2
9
  export declare interface DialectDriver {
3
10
  createEnumType: (enumTypeName: string, values: string[]) => string
4
11
  createTable: (table: TablePlan) => string
@@ -16,7 +23,12 @@ export declare interface DialectDriver {
16
23
  createMigrationsTable: () => string
17
24
  getExecutedMigrationsQuery: () => string
18
25
  recordMigrationQuery: () => string
26
+ jsonExtract: (column: string, key: string) => string
27
+ dateBucket: (column: string, grain: DateBucketGrain, offsetHours?: number) => string
28
+ booleanLiteral: (value: boolean) => string
19
29
  }
30
+ /** The buckets {@link DialectDriver.dateBucket} can truncate to. */
31
+ export type DateBucketGrain = 'hour' | 'day' | 'week' | 'month' | 'year';
20
32
  export declare class PostgresDriver implements DialectDriver {
21
33
  createEnumType(enumTypeName: string, values: string[]): string;
22
34
  createTable(table: TablePlan): string;
@@ -34,4 +46,7 @@ export declare class PostgresDriver implements DialectDriver {
34
46
  createMigrationsTable(): string;
35
47
  getExecutedMigrationsQuery(): string;
36
48
  recordMigrationQuery(): string;
49
+ jsonExtract(column: string, key: string): string;
50
+ dateBucket(column: string, grain: DateBucketGrain, offsetHours?: number): string;
51
+ booleanLiteral(value: boolean): string;
37
52
  }
@@ -1,4 +1,5 @@
1
1
  import type { ColumnPlan, IndexPlan, RebuildTableSpec, TablePlan } from '../migrations';
2
+ import type { DateBucketGrain } from './postgres';
2
3
  /**
3
4
  * Whether a plan type belongs to the numeric family. Used by the `_id`
4
5
  * safety net (numeric ids must store as INTEGER on SQLite) — non-numeric
@@ -40,4 +41,7 @@ export declare class SQLiteDriver implements DialectDriver {
40
41
  createMigrationsTable(): string;
41
42
  getExecutedMigrationsQuery(): string;
42
43
  recordMigrationQuery(): string;
44
+ jsonExtract(column: string, key: string): string;
45
+ dateBucket(column: string, grain: DateBucketGrain, offsetHours?: number): string;
46
+ booleanLiteral(value: boolean): string;
43
47
  }
package/dist/src/index.js CHANGED
@@ -18517,6 +18517,28 @@ ${sql};` : `${sql};`;
18517
18517
  recordMigrationQuery() {
18518
18518
  return "INSERT INTO migrations (migration) VALUES (?)";
18519
18519
  }
18520
+ jsonExtract(column, key) {
18521
+ return `JSON_UNQUOTE(JSON_EXTRACT(${column}, CONCAT('$.', ${key})))`;
18522
+ }
18523
+ dateBucket(column, grain, offsetHours = 0) {
18524
+ const shifted = offsetHours === 0 ? column : `DATE_ADD(${column}, INTERVAL ${offsetHours} HOUR)`;
18525
+ switch (grain) {
18526
+ case "hour":
18527
+ return `DATE_FORMAT(${shifted}, '%Y-%m-%dT%H:00:00.000Z')`;
18528
+ case "week":
18529
+ return `DATE_FORMAT(DATE_SUB(${shifted}, INTERVAL WEEKDAY(${shifted}) DAY), '%Y-%m-%dT00:00:00.000Z')`;
18530
+ case "month":
18531
+ return `DATE_FORMAT(${shifted}, '%Y-%m-01T00:00:00.000Z')`;
18532
+ case "year":
18533
+ return `DATE_FORMAT(${shifted}, '%Y-01-01T00:00:00.000Z')`;
18534
+ case "day":
18535
+ default:
18536
+ return `DATE_FORMAT(${shifted}, '%Y-%m-%dT00:00:00.000Z')`;
18537
+ }
18538
+ }
18539
+ booleanLiteral(value) {
18540
+ return value ? "TRUE" : "FALSE";
18541
+ }
18520
18542
  renderColumn(column) {
18521
18543
  const typeSql = this.getColumnType(column);
18522
18544
  const parts = [this.quoteIdentifier(column.name), typeSql];
@@ -18714,6 +18736,28 @@ ${sql};` : `${sql};`;
18714
18736
  recordMigrationQuery() {
18715
18737
  return "INSERT INTO migrations (migration) VALUES ($1)";
18716
18738
  }
18739
+ jsonExtract(column, key) {
18740
+ return `(${column}::jsonb ->> ${key})`;
18741
+ }
18742
+ dateBucket(column, grain, offsetHours = 0) {
18743
+ const shifted = offsetHours === 0 ? `${column}::timestamp` : `(${column}::timestamp ${offsetHours >= 0 ? "+" : "-"} interval '${Math.abs(offsetHours)} hours')`;
18744
+ const truncated = grain === "week" ? `date_trunc('week', ${shifted})` : shifted;
18745
+ switch (grain) {
18746
+ case "hour":
18747
+ return `to_char(${truncated}, 'YYYY-MM-DD"T"HH24:00:00.000"Z"')`;
18748
+ case "month":
18749
+ return `to_char(${truncated}, 'YYYY-MM-01"T"00:00:00.000"Z"')`;
18750
+ case "year":
18751
+ return `to_char(${truncated}, 'YYYY-01-01"T"00:00:00.000"Z"')`;
18752
+ case "week":
18753
+ case "day":
18754
+ default:
18755
+ return `to_char(${truncated}, 'YYYY-MM-DD"T"00:00:00.000"Z"')`;
18756
+ }
18757
+ }
18758
+ booleanLiteral(value) {
18759
+ return value ? "TRUE" : "FALSE";
18760
+ }
18717
18761
  renderColumn(column) {
18718
18762
  const typeSql = column.isPrimaryKey ? this.getPrimaryKeyType(column) : this.getColumnType(column);
18719
18763
  const parts = [this.quoteIdentifier(column.name), typeSql];
@@ -18997,6 +19041,28 @@ class SQLiteDriver {
18997
19041
  recordMigrationQuery() {
18998
19042
  return "INSERT INTO migrations (migration) VALUES (?)";
18999
19043
  }
19044
+ jsonExtract(column, key) {
19045
+ return `json_extract(${column}, '$.' || ${key})`;
19046
+ }
19047
+ dateBucket(column, grain, offsetHours = 0) {
19048
+ const shifted = offsetHours === 0 ? column : `datetime(${column}, '${offsetHours >= 0 ? "+" : "-"}${Math.abs(offsetHours)} hours')`;
19049
+ switch (grain) {
19050
+ case "hour":
19051
+ return `strftime('%Y-%m-%dT%H:00:00.000Z', ${shifted})`;
19052
+ case "week":
19053
+ return `strftime('%Y-%m-%dT00:00:00.000Z', ${shifted}, '-6 days', 'weekday 1')`;
19054
+ case "month":
19055
+ return `strftime('%Y-%m-01T00:00:00.000Z', ${shifted})`;
19056
+ case "year":
19057
+ return `strftime('%Y-01-01T00:00:00.000Z', ${shifted})`;
19058
+ case "day":
19059
+ default:
19060
+ return `strftime('%Y-%m-%dT00:00:00.000Z', ${shifted})`;
19061
+ }
19062
+ }
19063
+ booleanLiteral(value) {
19064
+ return value ? "TRUE" : "FALSE";
19065
+ }
19000
19066
  renderColumn(column) {
19001
19067
  const typeSql = this.getColumnType(column);
19002
19068
  const parts = [this.quoteIdentifier(column.name), typeSql];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-query-builder",
3
3
  "type": "module",
4
- "version": "0.2.30",
4
+ "version": "0.2.31",
5
5
  "description": "A simple yet performant query builder for TypeScript. Built with Bun.",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",
7
7
  "license": "MIT",