bradb 3.1.2 → 3.1.4

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.
@@ -136,10 +136,6 @@ export const ${name}Controller = {
136
136
  };`;
137
137
  }
138
138
  function generateService(name, table, imports) {
139
- const selection = `db
140
- .select()
141
- .from(${name}Table)
142
- .$dynamic()`;
143
139
  return `import { ServiceBuilder } from "bradb";
144
140
  ${imports.join('\n')};
145
141
 
@@ -149,12 +145,8 @@ export const ${name}Service = {
149
145
  create: builder.create(),
150
146
  update: builder.update(),
151
147
  delete: builder.delete(),
152
- findAll: builder.findAll(
153
- ${selection}
154
- ),
155
- findOne: builder.findOne(
156
- ${selection}
157
- )
148
+ findAll: builder.findAll(),
149
+ findOne: builder.findOne()
158
150
  };`;
159
151
  }
160
152
  function generateFilter(name, table, imports) {
@@ -1,5 +1,5 @@
1
1
  import { Filter, FilterMap, PrimaryKeyData } from "./types";
2
- import { AnyPgColumn, AnyPgTable, PgColumn, PgSelect, PgTransaction, SelectedFields } from "drizzle-orm/pg-core";
2
+ import { AnyPgTable, PgColumn, PgSelect, PgTransaction } from "drizzle-orm/pg-core";
3
3
  import { NodePgDatabase } from "drizzle-orm/node-postgres";
4
4
  import { InferInsertModel, SQL } from "drizzle-orm";
5
5
  import { ZodObject } from "zod";
@@ -14,9 +14,10 @@ export declare class ServiceBuilder<T extends AnyPgTable, TSchema extends Record
14
14
  readonly findOneConditions: (pkFilter: PKType) => SQL | undefined;
15
15
  readonly tableName: string;
16
16
  constructor(db: NodePgDatabase<TSchema>, table: T, map: FilterMap<FSchema>);
17
- findOne<S extends PgSelect>(select: S): (pkValues: PKType) => Promise<S["_"]["result"][0]>;
18
- TEST<F extends SelectedFields>(fields: F, cb: (qb: PgSelect) => PgSelect): (pkValues: PKType) => Promise<RowFromFields<F>>;
19
- findAll<S extends PgSelect>(select: S, paginated?: boolean): (filters?: Filter<FSchema>, p?: Pagination) => Promise<S["_"]["result"][0][]>;
17
+ findOne(): (pkValues: PKType) => Promise<typeof this.table["$inferSelect"]>;
18
+ findOne<S extends () => PgSelect>(select: S): (pkValues: PKType) => Awaited<ReturnType<S>["_"]["result"][0]>;
19
+ findAll(): (filters?: Filter<FSchema>, p?: Pagination) => Promise<typeof this.table["$inferSelect"]>;
20
+ findAll<S extends () => PgSelect>(select: S, paginated: boolean): (filters?: Filter<FSchema>, p?: Pagination) => Promise<Awaited<ReturnType<S>["_"]["result"][0]>>;
20
21
  count<S extends PgSelect>(select?: S): (filters?: Filter<FSchema>) => Promise<number>;
21
22
  create<S extends any>(hook?: (data: S extends Object ? S : InferInsertModel<T>, tx?: PgTransaction<any, TSchema, any>) => Promise<InferInsertModel<T>>): (data: S extends Object ? S : InferInsertModel<T>, tx?: PgTransaction<any, TSchema, any>) => Promise<{ [Key in keyof T["_"]["columns"] & string as Key]: T["_"]["columns"][Key]["_"]["notNull"] extends true ? T["_"]["columns"][Key]["_"]["data"] : T["_"]["columns"][Key]["_"]["data"] | null; } extends infer T_1 ? { [K in keyof T_1]: T_1[K]; } : never>;
22
23
  update<S extends any>(pre?: (data: S extends Object ? S : Partial<InferInsertModel<T>>, tx?: PgTransaction<any, TSchema, any>) => Promise<InferInsertModel<T>>): (pks: PKType, data: S extends Object ? S : Partial<InferInsertModel<T>>, tx?: PgTransaction<any, TSchema, any>) => Promise<{ [Key in keyof T["_"]["columns"] & string as Key]: T["_"]["columns"][Key]["_"]["notNull"] extends true ? T["_"]["columns"][Key]["_"]["data"] : T["_"]["columns"][Key]["_"]["data"] | null; } extends infer T_1 ? { [K in keyof T_1]: T_1[K]; } : never>;
@@ -24,7 +25,3 @@ export declare class ServiceBuilder<T extends AnyPgTable, TSchema extends Record
24
25
  softDelete(): (pks: PKType, tx?: PgTransaction<any, TSchema, any>) => Promise<void>;
25
26
  hardDelete(): (pks: PKType, tx?: PgTransaction<any, TSchema, any>) => Promise<void>;
26
27
  }
27
- type RowFromFields<T extends SelectedFields> = {
28
- [K in keyof T]: T[K] extends AnyPgColumn ? T[K]['_']['data'] : never;
29
- };
30
- export {};
@@ -24,35 +24,27 @@ class ServiceBuilder {
24
24
  }
25
25
  }
26
26
  findOne(select) {
27
+ const actualSelect = select ?? (() => this.db.select().from(this.table).$dynamic());
27
28
  return async (pkValues) => {
28
- const result = await select.where(this.findOneConditions(pkValues));
29
- if (result.length == 0)
30
- throw notFoundWithId(this.tableName, pkValues);
31
- return result[0];
32
- };
33
- }
34
- TEST(fields, cb) {
35
- return async (pkValues) => {
36
- const pre = this.db.select(fields).from(this.table).$dynamic();
37
- const result = await cb(pre).where(this.findOneConditions(pkValues));
38
- if (result.length == 0)
29
+ const result = await actualSelect().where(this.findOneConditions(pkValues));
30
+ if (result.length === 0) {
39
31
  throw notFoundWithId(this.tableName, pkValues);
32
+ }
40
33
  return result[0];
41
34
  };
42
35
  }
43
36
  findAll(select, paginated = true) {
44
- const base = (f) => select.where(this.findAllConditions(f));
37
+ if (!select)
38
+ select = () => this.db.select().from(this.table).$dynamic();
39
+ const base = (f) => select().where(this.findAllConditions(f));
45
40
  if (paginated) {
46
41
  return async (filters, p) => {
47
42
  if (!p)
48
- p = {
49
- page: 1,
50
- pageSize: 10
51
- };
43
+ p = { page: 1, pageSize: 10 };
44
+ const sub = select().where(this.findAllConditions(filters)).as('sub'); // TODO: not any
52
45
  const countQuery = this.db
53
46
  .select({ count: (0, drizzle_orm_1.count)() })
54
- .from(this.table)
55
- .where(this.findAllConditions(filters));
47
+ .from(sub);
56
48
  const offset = (p.page - 1) * p.pageSize;
57
49
  const items = await base(filters).limit(p.pageSize).offset(offset);
58
50
  const [res] = await countQuery;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bradb",
3
- "version": "3.1.2",
3
+ "version": "3.1.4",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",