@rwillians/qx 0.1.21 → 0.2.0

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.
@@ -363,6 +363,26 @@ const ddl = {
363
363
  ];
364
364
  return { sql: frags.join(''), params: values.params };
365
365
  },
366
+ /**
367
+ * @private Generates DDL for delete statement.
368
+ * @since 0.1.22
369
+ * @version 1
370
+ */
371
+ delete: (op) => {
372
+ const where = op.where
373
+ ? render.expr.any(op.where)
374
+ : EMPTY_RENDER_RESULT;
375
+ const frags = [
376
+ 'DELETE FROM ',
377
+ render.ref(op.table),
378
+ ' AS ',
379
+ render.ref(op.alias),
380
+ (where.frags.length > 0 ? ' WHERE ' : ''),
381
+ ...where.frags,
382
+ ';'
383
+ ];
384
+ return { sql: frags.join(''), params: [...where.params] };
385
+ },
366
386
  /**
367
387
  * @private Generates DDL for select statement.
368
388
  * @since 0.1.0
@@ -451,6 +471,18 @@ class BunSQLite {
451
471
  this.loggers.push(logger);
452
472
  return this;
453
473
  }
474
+ /**
475
+ * @public Executes a delete statement.
476
+ * @since 0.1.22
477
+ * @version 1
478
+ */
479
+ async delete(op) {
480
+ const { sql, params } = ddl.delete(op);
481
+ return await (0, adapter_1.withLoggedQuery)(this.loggers, { sql, params }, () => this.conn
482
+ .prepare(sql)
483
+ .run(...params)
484
+ .changes);
485
+ }
454
486
  /**
455
487
  * @public Executes a create table statement.
456
488
  * @since 0.1.0
package/dist/cjs/index.js CHANGED
@@ -566,6 +566,16 @@ class InsertBuilder {
566
566
  */
567
567
  const into = (table) => new InsertBuilder(table);
568
568
  exports.into = into;
569
+ /**
570
+ * @private Transforms a {@link Query} into a {@link DeleteStatement}.
571
+ * @since 0.1.22
572
+ * @version 1
573
+ */
574
+ const toDeleteStatement = (query) => ({
575
+ table: query.registry[query.from][TABLE_NAME],
576
+ alias: query.from,
577
+ where: query.where,
578
+ });
569
579
  /**
570
580
  * @private Transforms a {@link Query} into a {@link SelectStatement}.
571
581
  * @since 0.1.0
@@ -601,6 +611,15 @@ class QueryBuilder {
601
611
  // @TODO calls query engine with the query object
602
612
  return db.query(toSelectStatement(this.query));
603
613
  }
614
+ /**
615
+ * @public Deletes all rows matching the query's where clause,
616
+ * returning the number of deleted rows.
617
+ * @since 0.1.22
618
+ * @version 1
619
+ */
620
+ async delete(db) {
621
+ return db.delete(toDeleteStatement(this.query));
622
+ }
604
623
  /**
605
624
  * @public Checks whether any rows exist matching the query.
606
625
  * @since 0.1.6
@@ -360,6 +360,26 @@ const ddl = {
360
360
  ];
361
361
  return { sql: frags.join(''), params: values.params };
362
362
  },
363
+ /**
364
+ * @private Generates DDL for delete statement.
365
+ * @since 0.1.22
366
+ * @version 1
367
+ */
368
+ delete: (op) => {
369
+ const where = op.where
370
+ ? render.expr.any(op.where)
371
+ : EMPTY_RENDER_RESULT;
372
+ const frags = [
373
+ 'DELETE FROM ',
374
+ render.ref(op.table),
375
+ ' AS ',
376
+ render.ref(op.alias),
377
+ (where.frags.length > 0 ? ' WHERE ' : ''),
378
+ ...where.frags,
379
+ ';'
380
+ ];
381
+ return { sql: frags.join(''), params: [...where.params] };
382
+ },
363
383
  /**
364
384
  * @private Generates DDL for select statement.
365
385
  * @since 0.1.0
@@ -448,6 +468,18 @@ class BunSQLite {
448
468
  this.loggers.push(logger);
449
469
  return this;
450
470
  }
471
+ /**
472
+ * @public Executes a delete statement.
473
+ * @since 0.1.22
474
+ * @version 1
475
+ */
476
+ async delete(op) {
477
+ const { sql, params } = ddl.delete(op);
478
+ return await withLoggedQuery(this.loggers, { sql, params }, () => this.conn
479
+ .prepare(sql)
480
+ .run(...params)
481
+ .changes);
482
+ }
451
483
  /**
452
484
  * @public Executes a create table statement.
453
485
  * @since 0.1.0
package/dist/esm/index.js CHANGED
@@ -556,6 +556,16 @@ class InsertBuilder {
556
556
  * @version 1
557
557
  */
558
558
  const into = (table) => new InsertBuilder(table);
559
+ /**
560
+ * @private Transforms a {@link Query} into a {@link DeleteStatement}.
561
+ * @since 0.1.22
562
+ * @version 1
563
+ */
564
+ const toDeleteStatement = (query) => ({
565
+ table: query.registry[query.from][TABLE_NAME],
566
+ alias: query.from,
567
+ where: query.where,
568
+ });
559
569
  /**
560
570
  * @private Transforms a {@link Query} into a {@link SelectStatement}.
561
571
  * @since 0.1.0
@@ -591,6 +601,15 @@ class QueryBuilder {
591
601
  // @TODO calls query engine with the query object
592
602
  return db.query(toSelectStatement(this.query));
593
603
  }
604
+ /**
605
+ * @public Deletes all rows matching the query's where clause,
606
+ * returning the number of deleted rows.
607
+ * @since 0.1.22
608
+ * @version 1
609
+ */
610
+ async delete(db) {
611
+ return db.delete(toDeleteStatement(this.query));
612
+ }
594
613
  /**
595
614
  * @public Checks whether any rows exist matching the query.
596
615
  * @since 0.1.6
@@ -13,4 +13,4 @@ export declare const withLoggedQuery: <T>(logger: ILogger | ILogger[], data: {
13
13
  sql: string;
14
14
  params: any[];
15
15
  }, fn: (sql: string, params: any[]) => Promise<T> | T) => Promise<T>;
16
- export { type CodecsRegistry, type Column, type CreateTableStatement, type DDL, type Expr, type ExprAnd, type ExprBinaryOp, type ExprLiteral, type ExprNot, type ExprOr, type IDatabase, type ILogger, type InsertStatement, type Join, type OrderDirection, type PrimitiveToNativeTypeFactory, type SelectStatement, is, } from './index';
16
+ export { type CodecsRegistry, type Column, type CreateTableStatement, type DDL, type DeleteStatement, type Expr, type ExprAnd, type ExprBinaryOp, type ExprLiteral, type ExprNot, type ExprOr, type IDatabase, type ILogger, type InsertStatement, type Join, type OrderDirection, type PrimitiveToNativeTypeFactory, type SelectStatement, is, } from './index';
@@ -1,5 +1,5 @@
1
1
  import { Database } from 'bun:sqlite';
2
- import { type CreateTableStatement, type IDatabase, type ILogger, type InsertStatement, type SelectStatement } from './adapter';
2
+ import { type CreateTableStatement, type DeleteStatement, type IDatabase, type ILogger, type InsertStatement, type SelectStatement } from './adapter';
3
3
  /**
4
4
  * @private Bun SQLite database adapter implementation.
5
5
  * @since 0.1.0
@@ -15,6 +15,12 @@ declare class BunSQLite implements IDatabase {
15
15
  * @version 1
16
16
  */
17
17
  attachLogger(logger: ILogger): this;
18
+ /**
19
+ * @public Executes a delete statement.
20
+ * @since 0.1.22
21
+ * @version 1
22
+ */
23
+ delete(op: DeleteStatement): Promise<number>;
18
24
  /**
19
25
  * @public Executes a create table statement.
20
26
  * @since 0.1.0
@@ -954,6 +954,31 @@ type SelectStatement = {
954
954
  */
955
955
  offset?: number;
956
956
  };
957
+ /**
958
+ * @public Represents a delete statement.
959
+ * @since 0.1.22
960
+ * @version 1
961
+ */
962
+ type DeleteStatement = {
963
+ /**
964
+ * @public The name of the table to delete from.
965
+ * @since 0.1.22
966
+ * @version 1
967
+ */
968
+ table: string;
969
+ /**
970
+ * @public The alias of the table used in the where clause.
971
+ * @since 0.1.22
972
+ * @version 1
973
+ */
974
+ alias: string;
975
+ /**
976
+ * @public The query's where clause.
977
+ * @since 0.1.22
978
+ * @version 1
979
+ */
980
+ where?: Expr;
981
+ };
957
982
  /**
958
983
  * @private A codec for encoding data to the database's expected type,
959
984
  * and decoding data from the database to the application's
@@ -1020,6 +1045,13 @@ interface IDatabase {
1020
1045
  * @version 1
1021
1046
  */
1022
1047
  createTable(op: CreateTableStatement): Promise<void>;
1048
+ /**
1049
+ * @public Executes a delete statement, returning the number of
1050
+ * deleted rows.
1051
+ * @since 0.1.22
1052
+ * @version 1
1053
+ */
1054
+ delete(op: DeleteStatement): Promise<number>;
1023
1055
  /**
1024
1056
  * @public Executes an insert statement, returning the newly
1025
1057
  * inserted rows.
@@ -1170,6 +1202,13 @@ declare class QueryBuilder<T extends Record<string, Aliased<string, Table>>, S e
1170
1202
  * @version 1
1171
1203
  */
1172
1204
  all(db: IDatabase): Promise<Expand<InferSelection<Query<T, S>>>[]>;
1205
+ /**
1206
+ * @public Deletes all rows matching the query's where clause,
1207
+ * returning the number of deleted rows.
1208
+ * @since 0.1.22
1209
+ * @version 1
1210
+ */
1211
+ delete(db: IDatabase): Promise<number>;
1173
1212
  /**
1174
1213
  * @public Checks whether any rows exist matching the query.
1175
1214
  * @since 0.1.6
@@ -1230,4 +1269,4 @@ declare class QueryBuilder<T extends Record<string, Aliased<string, Table>>, S e
1230
1269
  * @version 1
1231
1270
  */
1232
1271
  declare const from: <S extends string, T extends Aliased<S, Table>>(table: T) => QueryBuilder<{ [K in T[typeof TABLE_ALIAS]]: T; }, T>;
1233
- export { type CodecsRegistry, type Column, type CreateTableStatement, type DDL, type Expr, type ExprAnd, type ExprBinaryOp, type ExprEq, type ExprGt, type ExprGte, type ExprIn, type ExprIs, type ExprIsNot, type ExprLike, type ExprLiteral, type ExprLt, type ExprLte, type ExprNe, type ExprNot, type ExprNotIn, type ExprNotLike, type ExprOr, type IDatabase, type ILogger, type Join, type InsertStatement, type OrderDirection, type Primitive, type PrimitiveToNativeTypeFactory, type SelectStatement, type Table, defineColumn as column, defineTable as table, expr, from, into, is, transaction, types as t, };
1272
+ export { type CodecsRegistry, type Column, type CreateTableStatement, type DDL, type DeleteStatement, type Expr, type ExprAnd, type ExprBinaryOp, type ExprEq, type ExprGt, type ExprGte, type ExprIn, type ExprIs, type ExprIsNot, type ExprLike, type ExprLiteral, type ExprLt, type ExprLte, type ExprNe, type ExprNot, type ExprNotIn, type ExprNotLike, type ExprOr, type IDatabase, type ILogger, type Join, type InsertStatement, type OrderDirection, type Primitive, type PrimitiveToNativeTypeFactory, type SelectStatement, type Table, defineColumn as column, defineTable as table, expr, from, into, is, transaction, types as t, };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rwillians/qx",
3
3
  "description": "A zero-dependencies teeny tiny ORM for SQLite.",
4
- "version": "0.1.21",
4
+ "version": "0.2.0",
5
5
  "author": "Rafael Willians <me@rwillians.com>",
6
6
  "license": "MIT",
7
7
  "repository": {