bradb 2.1.0 → 2.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.
- package/LICENSE +0 -0
- package/README.md +0 -0
- package/dist/bin/brad.d.ts +0 -0
- package/dist/bin/brad.js +0 -0
- package/dist/index.d.ts +0 -0
- package/dist/index.js +0 -0
- package/dist/src/commands.d.ts +0 -0
- package/dist/src/commands.js +0 -0
- package/dist/src/controller.d.ts +2 -2
- package/dist/src/controller.js +8 -4
- package/dist/src/errors.d.ts +0 -0
- package/dist/src/errors.js +0 -0
- package/dist/src/filters.d.ts +0 -0
- package/dist/src/filters.js +0 -0
- package/dist/src/relational.d.ts +0 -0
- package/dist/src/relational.js +0 -0
- package/dist/src/service.d.ts +2 -2
- package/dist/src/service.js +16 -9
- package/dist/src/types.d.ts +0 -0
- package/dist/src/types.js +0 -0
- package/package.json +1 -1
package/LICENSE
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
File without changes
|
package/dist/bin/brad.d.ts
CHANGED
|
File without changes
|
package/dist/bin/brad.js
CHANGED
|
File without changes
|
package/dist/index.d.ts
CHANGED
|
File without changes
|
package/dist/index.js
CHANGED
|
File without changes
|
package/dist/src/commands.d.ts
CHANGED
|
File without changes
|
package/dist/src/commands.js
CHANGED
|
File without changes
|
package/dist/src/controller.d.ts
CHANGED
|
@@ -5,11 +5,11 @@ import { Filter } from "./types";
|
|
|
5
5
|
import { InferInsertModel } from "drizzle-orm";
|
|
6
6
|
export declare function findOneBuilder<TReturn>(service: {
|
|
7
7
|
findOne: (id: any) => Promise<TReturn>;
|
|
8
|
-
}, hook?: (item: TReturn) =>
|
|
8
|
+
}, hook?: (item: TReturn) => Promise<object>): (req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>;
|
|
9
9
|
export declare function findAllBuilder<FSchema extends ZodObject, TReturn>(service: {
|
|
10
10
|
findAll: (filters?: Filter<FSchema>, page?: number, pageSize?: number) => Promise<TReturn[]>;
|
|
11
11
|
count: (filters?: Filter<FSchema>) => Promise<number>;
|
|
12
|
-
}, filter: FSchema, hook?: (items: TReturn[], total: number) =>
|
|
12
|
+
}, filter: FSchema, hook?: (items: TReturn[], total: number) => Promise<object[]>, hasPagination?: boolean): (req: Request, res: Response) => Promise<Response<any, Record<string, any>>>;
|
|
13
13
|
export declare function createBuilder<CSchema extends ZodObject, TReturn>(service: {
|
|
14
14
|
create: (data: z.core.output<CSchema>) => Promise<TReturn>;
|
|
15
15
|
}, schema: CSchema, hook?: (req: Request, res: Response, data: z.core.output<CSchema>, item: TReturn) => Promise<object>): (req: Request, res: Response) => Promise<Response<any, Record<string, any>>>;
|
package/dist/src/controller.js
CHANGED
|
@@ -11,7 +11,8 @@ function findOneBuilder(service, hook) {
|
|
|
11
11
|
return async (req, res) => {
|
|
12
12
|
const item = await service.findOne(req.params.id);
|
|
13
13
|
if (hook !== undefined) {
|
|
14
|
-
hook(item);
|
|
14
|
+
const i = await hook(item);
|
|
15
|
+
return res.status(200).json(i);
|
|
15
16
|
}
|
|
16
17
|
res.status(200).json(item);
|
|
17
18
|
};
|
|
@@ -32,17 +33,20 @@ function findAllBuilder(service, filter, hook, hasPagination = true) {
|
|
|
32
33
|
}
|
|
33
34
|
const totalProm = service.count(filters);
|
|
34
35
|
const [items, total] = await Promise.all([itemsProm, totalProm]);
|
|
35
|
-
|
|
36
|
+
let resItems = items;
|
|
37
|
+
if (hook != undefined) {
|
|
38
|
+
resItems = await hook(items, total);
|
|
39
|
+
}
|
|
36
40
|
if (hasPagination) {
|
|
37
41
|
return res.status(200).json({
|
|
38
42
|
pagination,
|
|
39
|
-
items,
|
|
43
|
+
items: resItems,
|
|
40
44
|
total
|
|
41
45
|
});
|
|
42
46
|
}
|
|
43
47
|
else {
|
|
44
48
|
return res.status(200).json({
|
|
45
|
-
items,
|
|
49
|
+
items: resItems,
|
|
46
50
|
total
|
|
47
51
|
});
|
|
48
52
|
}
|
package/dist/src/errors.d.ts
CHANGED
|
File without changes
|
package/dist/src/errors.js
CHANGED
|
File without changes
|
package/dist/src/filters.d.ts
CHANGED
|
File without changes
|
package/dist/src/filters.js
CHANGED
|
File without changes
|
package/dist/src/relational.d.ts
CHANGED
|
File without changes
|
package/dist/src/relational.js
CHANGED
|
File without changes
|
package/dist/src/service.d.ts
CHANGED
|
@@ -16,8 +16,8 @@ export declare class ServiceBuilder<T extends Table, TSchema extends Record<stri
|
|
|
16
16
|
findOne<S extends PgSelect>(select: S): (id: PrimaryKeyType<T>) => Promise<Awaited<ReturnType<S["execute"]>>[number]>;
|
|
17
17
|
findAll<S extends PgSelect>(select: S, paginated?: boolean): (filters?: Filter<FSchema>, page?: number, pageSize?: number) => Promise<Awaited<ReturnType<S["execute"]>>[number][]>;
|
|
18
18
|
count(): (filters?: Filter<FSchema>) => Promise<number>;
|
|
19
|
-
create(
|
|
20
|
-
update(): (id: PrimaryKeyType<T>, data:
|
|
19
|
+
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>;
|
|
20
|
+
update<S extends any>(pre?: (data: S extends Object ? S : InferInsertModel<T>, tx?: PgTransaction<any, TSchema, any>) => Promise<InferInsertModel<T>>): (id: PrimaryKeyType<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>;
|
|
21
21
|
delete(): (id: PrimaryKeyType<T>, tx?: PgTransaction<any, TSchema, any>) => Promise<void>;
|
|
22
22
|
softDelete(): (id: PrimaryKeyType<T>, tx?: PgTransaction<any, TSchema, any>) => Promise<void>;
|
|
23
23
|
hardDelete(): (id: PrimaryKeyType<T>, tx?: PgTransaction<any, TSchema, any>) => Promise<void>;
|
package/dist/src/service.js
CHANGED
|
@@ -57,31 +57,35 @@ class ServiceBuilder {
|
|
|
57
57
|
return result.count;
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
|
-
create(
|
|
60
|
+
create(hook) {
|
|
61
61
|
return async (data, tx) => {
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
// If pre exists data is going to be InferInsertModel<T>
|
|
63
|
+
let values = data;
|
|
64
|
+
if (hook) {
|
|
65
|
+
values = await hook(data);
|
|
64
66
|
}
|
|
65
67
|
const executor = tx || this.db;
|
|
66
68
|
const [result] = await executor
|
|
67
69
|
.insert(this.table)
|
|
68
|
-
.values(
|
|
70
|
+
.values(values)
|
|
69
71
|
.returning()
|
|
70
72
|
.catch(errors_1.handleSqlError);
|
|
71
73
|
return result;
|
|
72
74
|
};
|
|
73
75
|
}
|
|
74
|
-
update() {
|
|
75
|
-
return async (
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
update(pre) {
|
|
77
|
+
return async (id, data, tx) => {
|
|
78
|
+
let values = data;
|
|
79
|
+
if (pre) {
|
|
80
|
+
values = await pre(data);
|
|
81
|
+
}
|
|
78
82
|
if (Object.keys(data).length == 0) {
|
|
79
83
|
throw new errors_1.BadRequest("update needs at least one field");
|
|
80
84
|
}
|
|
81
85
|
const executor = tx || this.db;
|
|
82
86
|
const [result] = await executor
|
|
83
87
|
.update(this.table)
|
|
84
|
-
.set(
|
|
88
|
+
.set(values)
|
|
85
89
|
.where(this.findOneConditions(id))
|
|
86
90
|
.returning()
|
|
87
91
|
.catch(errors_1.handleSqlError);
|
|
@@ -148,6 +152,9 @@ function getPKs(table) {
|
|
|
148
152
|
return pks;
|
|
149
153
|
}
|
|
150
154
|
function notFoundWithId(tableName, pkFilter) {
|
|
155
|
+
// for (const [key, val] of pkFilter.values()) {
|
|
156
|
+
//
|
|
157
|
+
// }
|
|
151
158
|
// TODO: proper message
|
|
152
159
|
return new errors_1.NotFound(`${tableName} not found`);
|
|
153
160
|
}
|
package/dist/src/types.d.ts
CHANGED
|
File without changes
|
package/dist/src/types.js
CHANGED
|
File without changes
|