dn-react-router-toolkit 0.9.4 → 0.9.5

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/api/index.js CHANGED
@@ -438,7 +438,11 @@ function putResourceHandler({
438
438
  // src/api/patch_resource_handler.ts
439
439
  var import_gw_response3 = require("gw-response");
440
440
  var import_drizzle_orm2 = require("drizzle-orm");
441
- function patchResourceHandler({ repository, validators }) {
441
+ function patchResourceHandler({
442
+ repository,
443
+ validators,
444
+ primaryKey
445
+ }) {
442
446
  return async (existing, request) => {
443
447
  const serilaizedParams = await request.json();
444
448
  const params = deserialize(serilaizedParams) || {};
@@ -457,8 +461,7 @@ function patchResourceHandler({ repository, validators }) {
457
461
  }
458
462
  }
459
463
  }
460
- const item = await repository.save({
461
- ...existing,
464
+ const item = await repository.update(existing[primaryKey], {
462
465
  ...params
463
466
  });
464
467
  return (0, import_gw_response3.httpCreated)(item);
@@ -473,7 +476,8 @@ function resourceHandler({
473
476
  existingConditions,
474
477
  injectUserId,
475
478
  roles,
476
- isOwnedBy
479
+ isOwnedBy,
480
+ primaryKey
477
481
  }) {
478
482
  const loader = async ({ request }) => {
479
483
  return (0, import_gw_result.ok)({});
@@ -485,7 +489,8 @@ function resourceHandler({
485
489
  message: "\uAD8C\uD55C\uC774 \uC5C6\uC2B5\uB2C8\uB2E4."
486
490
  });
487
491
  }
488
- const itemId = params.itemId;
492
+ const pathname = new URL(request.url).pathname;
493
+ const itemId = pathname.split("/").filter(Boolean).at(-1);
489
494
  if (itemId) {
490
495
  const existing = await repository.find(itemId);
491
496
  if (!existing) {
@@ -498,7 +503,8 @@ function resourceHandler({
498
503
  case "PATCH": {
499
504
  const handler = patchResourceHandler({
500
505
  repository,
501
- validators
506
+ validators,
507
+ primaryKey
502
508
  });
503
509
  return handler(existing, request);
504
510
  }
@@ -434,7 +434,11 @@ function putResourceHandler({
434
434
  // src/api/patch_resource_handler.ts
435
435
  import { httpBadRequest as httpBadRequest2, httpCreated as httpCreated2 } from "gw-response";
436
436
  import "drizzle-orm";
437
- function patchResourceHandler({ repository, validators }) {
437
+ function patchResourceHandler({
438
+ repository,
439
+ validators,
440
+ primaryKey
441
+ }) {
438
442
  return async (existing, request) => {
439
443
  const serilaizedParams = await request.json();
440
444
  const params = deserialize(serilaizedParams) || {};
@@ -453,8 +457,7 @@ function patchResourceHandler({ repository, validators }) {
453
457
  }
454
458
  }
455
459
  }
456
- const item = await repository.save({
457
- ...existing,
460
+ const item = await repository.update(existing[primaryKey], {
458
461
  ...params
459
462
  });
460
463
  return httpCreated2(item);
@@ -469,7 +472,8 @@ function resourceHandler({
469
472
  existingConditions,
470
473
  injectUserId,
471
474
  roles,
472
- isOwnedBy
475
+ isOwnedBy,
476
+ primaryKey
473
477
  }) {
474
478
  const loader = async ({ request }) => {
475
479
  return ok({});
@@ -481,7 +485,8 @@ function resourceHandler({
481
485
  message: "\uAD8C\uD55C\uC774 \uC5C6\uC2B5\uB2C8\uB2E4."
482
486
  });
483
487
  }
484
- const itemId = params.itemId;
488
+ const pathname = new URL(request.url).pathname;
489
+ const itemId = pathname.split("/").filter(Boolean).at(-1);
485
490
  if (itemId) {
486
491
  const existing = await repository.find(itemId);
487
492
  if (!existing) {
@@ -494,7 +499,8 @@ function resourceHandler({
494
499
  case "PATCH": {
495
500
  const handler = patchResourceHandler({
496
501
  repository,
497
- validators
502
+ validators,
503
+ primaryKey
498
504
  });
499
505
  return handler(existing, request);
500
506
  }
@@ -12,10 +12,11 @@ type PatchResourceValidators<T extends PgTableWithColumns<any>> = {
12
12
  type PatchResourceExistingConditions<T extends PgTableWithColumns<any>> = {
13
13
  [K in keyof InferSelectModel<T>]?: (value: InferSelectModel<T>[K]) => SQLWrapper;
14
14
  };
15
- type PatchResourceHandlerOptions<T extends PgTableWithColumns<any>, TSelect> = {
16
- repository: TableRepository<T, TSelect>;
15
+ type PatchResourceHandlerOptions<T extends PgTableWithColumns<any>, TSelect, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never> = {
16
+ repository: TableRepository<T, TSelect, TPrimaryKey>;
17
17
  validators?: PatchResourceValidators<T>;
18
+ primaryKey: TPrimaryKey;
18
19
  };
19
- declare function patchResourceHandler<T extends PgTableWithColumns<any>, TSelect>({ repository, validators }: PatchResourceHandlerOptions<T, TSelect>): (existing: TSelect, request: Request) => Promise<Response>;
20
+ declare function patchResourceHandler<T extends PgTableWithColumns<any>, TSelect, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never>({ repository, validators, primaryKey, }: PatchResourceHandlerOptions<T, TSelect, TPrimaryKey>): (existing: TSelect, request: Request) => Promise<Response>;
20
21
 
21
22
  export { type PatchResourceExistingConditions, type PatchResourceHandlerOptions, type PatchResourceValidators, patchResourceHandler };
@@ -12,10 +12,11 @@ type PatchResourceValidators<T extends PgTableWithColumns<any>> = {
12
12
  type PatchResourceExistingConditions<T extends PgTableWithColumns<any>> = {
13
13
  [K in keyof InferSelectModel<T>]?: (value: InferSelectModel<T>[K]) => SQLWrapper;
14
14
  };
15
- type PatchResourceHandlerOptions<T extends PgTableWithColumns<any>, TSelect> = {
16
- repository: TableRepository<T, TSelect>;
15
+ type PatchResourceHandlerOptions<T extends PgTableWithColumns<any>, TSelect, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never> = {
16
+ repository: TableRepository<T, TSelect, TPrimaryKey>;
17
17
  validators?: PatchResourceValidators<T>;
18
+ primaryKey: TPrimaryKey;
18
19
  };
19
- declare function patchResourceHandler<T extends PgTableWithColumns<any>, TSelect>({ repository, validators }: PatchResourceHandlerOptions<T, TSelect>): (existing: TSelect, request: Request) => Promise<Response>;
20
+ declare function patchResourceHandler<T extends PgTableWithColumns<any>, TSelect, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never>({ repository, validators, primaryKey, }: PatchResourceHandlerOptions<T, TSelect, TPrimaryKey>): (existing: TSelect, request: Request) => Promise<Response>;
20
21
 
21
22
  export { type PatchResourceExistingConditions, type PatchResourceHandlerOptions, type PatchResourceValidators, patchResourceHandler };
@@ -140,7 +140,11 @@ var import_jsx_runtime6 = require("react/jsx-runtime");
140
140
 
141
141
  // src/api/patch_resource_handler.ts
142
142
  var import_drizzle_orm = require("drizzle-orm");
143
- function patchResourceHandler({ repository, validators }) {
143
+ function patchResourceHandler({
144
+ repository,
145
+ validators,
146
+ primaryKey
147
+ }) {
144
148
  return async (existing, request) => {
145
149
  const serilaizedParams = await request.json();
146
150
  const params = deserialize(serilaizedParams) || {};
@@ -159,8 +163,7 @@ function patchResourceHandler({ repository, validators }) {
159
163
  }
160
164
  }
161
165
  }
162
- const item = await repository.save({
163
- ...existing,
166
+ const item = await repository.update(existing[primaryKey], {
164
167
  ...params
165
168
  });
166
169
  return (0, import_gw_response.httpCreated)(item);
@@ -113,7 +113,11 @@ import { Fragment as Fragment2, jsx as jsx6, jsxs as jsxs2 } from "react/jsx-run
113
113
 
114
114
  // src/api/patch_resource_handler.ts
115
115
  import "drizzle-orm";
116
- function patchResourceHandler({ repository, validators }) {
116
+ function patchResourceHandler({
117
+ repository,
118
+ validators,
119
+ primaryKey
120
+ }) {
117
121
  return async (existing, request) => {
118
122
  const serilaizedParams = await request.json();
119
123
  const params = deserialize(serilaizedParams) || {};
@@ -132,8 +136,7 @@ function patchResourceHandler({ repository, validators }) {
132
136
  }
133
137
  }
134
138
  }
135
- const item = await repository.save({
136
- ...existing,
139
+ const item = await repository.update(existing[primaryKey], {
137
140
  ...params
138
141
  });
139
142
  return httpCreated(item);
@@ -13,13 +13,13 @@ type PutResourceValidators<T extends PgTableWithColumns<any>> = {
13
13
  type PutResourceExistingConditions<T extends PgTableWithColumns<any>> = {
14
14
  [K in keyof InferSelectModel<T>]?: (value: InferSelectModel<T>[K]) => SQLWrapper;
15
15
  };
16
- type PutResourceHandlerOptions<T extends PgTableWithColumns<any>, TSelect> = {
17
- repository: TableRepository<T, TSelect>;
16
+ type PutResourceHandlerOptions<T extends PgTableWithColumns<any>, TSelect, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never> = {
17
+ repository: TableRepository<T, TSelect, TPrimaryKey>;
18
18
  validators?: PutResourceValidators<T>;
19
19
  existingConditions?: PutResourceExistingConditions<T>;
20
20
  injectUserId?: boolean;
21
21
  isOwnedBy?: (auth: AccessTokenPayload, item: TSelect) => boolean;
22
22
  };
23
- declare function putResourceHandler<T extends PgTableWithColumns<any>, TSelect>({ repository, validators, existingConditions, injectUserId, isOwnedBy, }: PutResourceHandlerOptions<T, TSelect>): (auth: AccessTokenPayload | undefined, request: Request) => Promise<Response>;
23
+ declare function putResourceHandler<T extends PgTableWithColumns<any>, TSelect, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never>({ repository, validators, existingConditions, injectUserId, isOwnedBy, }: PutResourceHandlerOptions<T, TSelect, TPrimaryKey>): (auth: AccessTokenPayload | undefined, request: Request) => Promise<Response>;
24
24
 
25
25
  export { type PutResourceExistingConditions, type PutResourceHandlerOptions, type PutResourceValidators, putResourceHandler };
@@ -13,13 +13,13 @@ type PutResourceValidators<T extends PgTableWithColumns<any>> = {
13
13
  type PutResourceExistingConditions<T extends PgTableWithColumns<any>> = {
14
14
  [K in keyof InferSelectModel<T>]?: (value: InferSelectModel<T>[K]) => SQLWrapper;
15
15
  };
16
- type PutResourceHandlerOptions<T extends PgTableWithColumns<any>, TSelect> = {
17
- repository: TableRepository<T, TSelect>;
16
+ type PutResourceHandlerOptions<T extends PgTableWithColumns<any>, TSelect, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never> = {
17
+ repository: TableRepository<T, TSelect, TPrimaryKey>;
18
18
  validators?: PutResourceValidators<T>;
19
19
  existingConditions?: PutResourceExistingConditions<T>;
20
20
  injectUserId?: boolean;
21
21
  isOwnedBy?: (auth: AccessTokenPayload, item: TSelect) => boolean;
22
22
  };
23
- declare function putResourceHandler<T extends PgTableWithColumns<any>, TSelect>({ repository, validators, existingConditions, injectUserId, isOwnedBy, }: PutResourceHandlerOptions<T, TSelect>): (auth: AccessTokenPayload | undefined, request: Request) => Promise<Response>;
23
+ declare function putResourceHandler<T extends PgTableWithColumns<any>, TSelect, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never>({ repository, validators, existingConditions, injectUserId, isOwnedBy, }: PutResourceHandlerOptions<T, TSelect, TPrimaryKey>): (auth: AccessTokenPayload | undefined, request: Request) => Promise<Response>;
24
24
 
25
25
  export { type PutResourceExistingConditions, type PutResourceHandlerOptions, type PutResourceValidators, putResourceHandler };
@@ -9,16 +9,17 @@ import 'drizzle-orm';
9
9
  import 'drizzle-orm/node-postgres';
10
10
  import 'gw-auth/server';
11
11
 
12
- type APIHandlerOptions<T extends PgTableWithColumns<any>, TSelect> = {
12
+ type APIHandlerOptions<T extends PgTableWithColumns<any>, TSelect, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never> = {
13
13
  withAuthAction: WithAuthHandler<ActionFunctionArgs>;
14
- repository: TableRepository<T, TSelect>;
14
+ repository: TableRepository<T, TSelect, TPrimaryKey>;
15
+ primaryKey: TPrimaryKey;
15
16
  validators?: PutResourceValidators<T>;
16
17
  existingConditions?: PutResourceExistingConditions<T>;
17
18
  injectUserId?: boolean;
18
19
  roles?: string[];
19
20
  isOwnedBy?: (auth: AccessTokenPayload, item: TSelect) => boolean;
20
21
  };
21
- declare function resourceHandler<T extends PgTableWithColumns<any>, TSelect>({ withAuthAction, repository, validators, existingConditions, injectUserId, roles, isOwnedBy, }: APIHandlerOptions<T, TSelect>): {
22
+ declare function resourceHandler<T extends PgTableWithColumns<any>, TSelect, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never>({ withAuthAction, repository, validators, existingConditions, injectUserId, roles, isOwnedBy, primaryKey, }: APIHandlerOptions<T, TSelect, TPrimaryKey>): {
22
23
  loader: ({ request }: LoaderFunctionArgs) => Promise<gw_result.Ok<{}>>;
23
24
  action: (arg: ActionFunctionArgs<any>) => Promise<unknown> | unknown;
24
25
  };
@@ -9,16 +9,17 @@ import 'drizzle-orm';
9
9
  import 'drizzle-orm/node-postgres';
10
10
  import 'gw-auth/server';
11
11
 
12
- type APIHandlerOptions<T extends PgTableWithColumns<any>, TSelect> = {
12
+ type APIHandlerOptions<T extends PgTableWithColumns<any>, TSelect, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never> = {
13
13
  withAuthAction: WithAuthHandler<ActionFunctionArgs>;
14
- repository: TableRepository<T, TSelect>;
14
+ repository: TableRepository<T, TSelect, TPrimaryKey>;
15
+ primaryKey: TPrimaryKey;
15
16
  validators?: PutResourceValidators<T>;
16
17
  existingConditions?: PutResourceExistingConditions<T>;
17
18
  injectUserId?: boolean;
18
19
  roles?: string[];
19
20
  isOwnedBy?: (auth: AccessTokenPayload, item: TSelect) => boolean;
20
21
  };
21
- declare function resourceHandler<T extends PgTableWithColumns<any>, TSelect>({ withAuthAction, repository, validators, existingConditions, injectUserId, roles, isOwnedBy, }: APIHandlerOptions<T, TSelect>): {
22
+ declare function resourceHandler<T extends PgTableWithColumns<any>, TSelect, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never>({ withAuthAction, repository, validators, existingConditions, injectUserId, roles, isOwnedBy, primaryKey, }: APIHandlerOptions<T, TSelect, TPrimaryKey>): {
22
23
  loader: ({ request }: LoaderFunctionArgs) => Promise<gw_result.Ok<{}>>;
23
24
  action: (arg: ActionFunctionArgs<any>) => Promise<unknown> | unknown;
24
25
  };
@@ -216,7 +216,11 @@ function putResourceHandler({
216
216
  // src/api/patch_resource_handler.ts
217
217
  var import_gw_response2 = require("gw-response");
218
218
  var import_drizzle_orm2 = require("drizzle-orm");
219
- function patchResourceHandler({ repository, validators }) {
219
+ function patchResourceHandler({
220
+ repository,
221
+ validators,
222
+ primaryKey
223
+ }) {
220
224
  return async (existing, request) => {
221
225
  const serilaizedParams = await request.json();
222
226
  const params = deserialize(serilaizedParams) || {};
@@ -235,8 +239,7 @@ function patchResourceHandler({ repository, validators }) {
235
239
  }
236
240
  }
237
241
  }
238
- const item = await repository.save({
239
- ...existing,
242
+ const item = await repository.update(existing[primaryKey], {
240
243
  ...params
241
244
  });
242
245
  return (0, import_gw_response2.httpCreated)(item);
@@ -251,7 +254,8 @@ function resourceHandler({
251
254
  existingConditions,
252
255
  injectUserId,
253
256
  roles,
254
- isOwnedBy
257
+ isOwnedBy,
258
+ primaryKey
255
259
  }) {
256
260
  const loader = async ({ request }) => {
257
261
  return (0, import_gw_result.ok)({});
@@ -263,7 +267,8 @@ function resourceHandler({
263
267
  message: "\uAD8C\uD55C\uC774 \uC5C6\uC2B5\uB2C8\uB2E4."
264
268
  });
265
269
  }
266
- const itemId = params.itemId;
270
+ const pathname = new URL(request.url).pathname;
271
+ const itemId = pathname.split("/").filter(Boolean).at(-1);
267
272
  if (itemId) {
268
273
  const existing = await repository.find(itemId);
269
274
  if (!existing) {
@@ -276,7 +281,8 @@ function resourceHandler({
276
281
  case "PATCH": {
277
282
  const handler = patchResourceHandler({
278
283
  repository,
279
- validators
284
+ validators,
285
+ primaryKey
280
286
  });
281
287
  return handler(existing, request);
282
288
  }
@@ -202,7 +202,11 @@ function putResourceHandler({
202
202
  // src/api/patch_resource_handler.ts
203
203
  import { httpBadRequest as httpBadRequest2, httpCreated as httpCreated2 } from "gw-response";
204
204
  import "drizzle-orm";
205
- function patchResourceHandler({ repository, validators }) {
205
+ function patchResourceHandler({
206
+ repository,
207
+ validators,
208
+ primaryKey
209
+ }) {
206
210
  return async (existing, request) => {
207
211
  const serilaizedParams = await request.json();
208
212
  const params = deserialize(serilaizedParams) || {};
@@ -221,8 +225,7 @@ function patchResourceHandler({ repository, validators }) {
221
225
  }
222
226
  }
223
227
  }
224
- const item = await repository.save({
225
- ...existing,
228
+ const item = await repository.update(existing[primaryKey], {
226
229
  ...params
227
230
  });
228
231
  return httpCreated2(item);
@@ -237,7 +240,8 @@ function resourceHandler({
237
240
  existingConditions,
238
241
  injectUserId,
239
242
  roles,
240
- isOwnedBy
243
+ isOwnedBy,
244
+ primaryKey
241
245
  }) {
242
246
  const loader = async ({ request }) => {
243
247
  return ok({});
@@ -249,7 +253,8 @@ function resourceHandler({
249
253
  message: "\uAD8C\uD55C\uC774 \uC5C6\uC2B5\uB2C8\uB2E4."
250
254
  });
251
255
  }
252
- const itemId = params.itemId;
256
+ const pathname = new URL(request.url).pathname;
257
+ const itemId = pathname.split("/").filter(Boolean).at(-1);
253
258
  if (itemId) {
254
259
  const existing = await repository.find(itemId);
255
260
  if (!existing) {
@@ -262,7 +267,8 @@ function resourceHandler({
262
267
  case "PATCH": {
263
268
  const handler = patchResourceHandler({
264
269
  repository,
265
- validators
270
+ validators,
271
+ primaryKey
266
272
  });
267
273
  return handler(existing, request);
268
274
  }
@@ -41,8 +41,8 @@ var BaseTableRepository = class {
41
41
  this.schema = db._.fullSchema[schema];
42
42
  this.pk = pk;
43
43
  }
44
- async find(id) {
45
- return await this.db.select().from(this.schema).where((0, import_drizzle_orm.eq)(this.schema[this.pk], id)).limit(1).then((res) => res[0]);
44
+ async find(pk) {
45
+ return await this.db.select().from(this.schema).where((0, import_drizzle_orm.eq)(this.schema[this.pk], pk)).limit(1).then((res) => res[0]);
46
46
  }
47
47
  async countTotal({ where }) {
48
48
  const [{ total }] = await this.db.select({ total: (0, import_drizzle_orm.count)() }).from(this.schema).where(where);
@@ -77,6 +77,10 @@ var BaseTableRepository = class {
77
77
  async delete(pk) {
78
78
  await this.db.delete(this.schema).where((0, import_drizzle_orm.eq)(this.schema[this.pk], pk));
79
79
  }
80
+ async update(pk, values) {
81
+ const [item] = await this.db.update(this.schema).set(values).where((0, import_drizzle_orm.eq)(this.schema[this.pk], pk)).returning();
82
+ return item;
83
+ }
80
84
  async select(key) {
81
85
  const rows = await this.db.select({ value: this.schema[key] }).from(this.schema).groupBy(this.schema[key]);
82
86
  return rows.map((row) => row.value);
@@ -14,8 +14,8 @@ var BaseTableRepository = class {
14
14
  this.schema = db._.fullSchema[schema];
15
15
  this.pk = pk;
16
16
  }
17
- async find(id) {
18
- return await this.db.select().from(this.schema).where(eq(this.schema[this.pk], id)).limit(1).then((res) => res[0]);
17
+ async find(pk) {
18
+ return await this.db.select().from(this.schema).where(eq(this.schema[this.pk], pk)).limit(1).then((res) => res[0]);
19
19
  }
20
20
  async countTotal({ where }) {
21
21
  const [{ total }] = await this.db.select({ total: count() }).from(this.schema).where(where);
@@ -50,6 +50,10 @@ var BaseTableRepository = class {
50
50
  async delete(pk) {
51
51
  await this.db.delete(this.schema).where(eq(this.schema[this.pk], pk));
52
52
  }
53
+ async update(pk, values) {
54
+ const [item] = await this.db.update(this.schema).set(values).where(eq(this.schema[this.pk], pk)).returning();
55
+ return item;
56
+ }
53
57
  async select(key) {
54
58
  const rows = await this.db.select({ value: this.schema[key] }).from(this.schema).groupBy(this.schema[key]);
55
59
  return rows.map((row) => row.value);
@@ -12,30 +12,32 @@ type FindAllOptions<T extends PgTableWithColumns<any>> = {
12
12
  type ColumnOf<T> = T extends PgTableWithColumns<infer TConfig> ? keyof TConfig["columns"] : never;
13
13
  type SelectModelOf<T> = T extends TableRepository<infer TSelect, any> ? TSelect : never;
14
14
  type InsertModelOf<T> = T extends TableRepository<any, infer TInsert> ? TInsert : never;
15
- interface TableRepository<T extends PgTableWithColumns<any>, TSelect = InferSelectModel<T>> {
15
+ interface TableRepository<T extends PgTableWithColumns<any>, TSelect = InferSelectModel<T>, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never> {
16
16
  schema: T;
17
- find: (id: string) => Promise<TSelect | undefined>;
17
+ find: (pk: TSelect[TPrimaryKey]) => Promise<TSelect | undefined>;
18
18
  countTotal: (options: {
19
19
  where?: SQL<unknown>;
20
20
  }) => Promise<number>;
21
21
  findAll: (options: FindAllOptions<T>) => Promise<TSelect[]>;
22
22
  save: (values: InferInsertModel<T>) => Promise<TSelect>;
23
- delete: (id: string) => Promise<void>;
23
+ update: (pk: TSelect[TPrimaryKey], values: Partial<InferInsertModel<T>>) => Promise<TSelect>;
24
+ delete: (pk: TSelect[TPrimaryKey]) => Promise<void>;
24
25
  select(key: keyof InferSelectModel<T>): Promise<InferSelectModel<T>[typeof key][]>;
25
26
  }
26
27
  type SchemaOf<TDatabase extends NodePgDatabase<any>, TSchemaKey extends keyof TDatabase["_"]["fullSchema"]> = TDatabase["_"]["fullSchema"][TSchemaKey];
27
- declare class BaseTableRepository<TDatabase extends NodePgDatabase<any>, TSchemaKey extends keyof TDatabase["_"]["fullSchema"], TSelect = InferSelectModel<SchemaOf<TDatabase, TSchemaKey>>, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never> implements TableRepository<SchemaOf<TDatabase, TSchemaKey>> {
28
+ declare class BaseTableRepository<TDatabase extends NodePgDatabase<any>, TSchemaKey extends keyof TDatabase["_"]["fullSchema"], TSelect = InferSelectModel<SchemaOf<TDatabase, TSchemaKey>>, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never> implements TableRepository<SchemaOf<TDatabase, TSchemaKey>, TSelect, TPrimaryKey> {
28
29
  db: TDatabase;
29
30
  schema: SchemaOf<TDatabase, TSchemaKey>;
30
31
  pk: TPrimaryKey;
31
32
  constructor(db: TDatabase, schema: TSchemaKey, pk?: TPrimaryKey);
32
- find(id: string): Promise<TSelect | undefined>;
33
+ find(pk: TSelect[TPrimaryKey]): Promise<TSelect | undefined>;
33
34
  countTotal({ where }: {
34
35
  where?: SQL<unknown>;
35
36
  }): Promise<number>;
36
37
  findAll(options: FindAllOptions<SchemaOf<TDatabase, TSchemaKey>>): Promise<TSelect[]>;
37
38
  save(values: InferInsertModel<SchemaOf<TDatabase, TSchemaKey>>): Promise<TSelect>;
38
- delete(pk: string): Promise<void>;
39
+ delete(pk: TSelect[TPrimaryKey]): Promise<void>;
40
+ update(pk: TSelect[TPrimaryKey], values: Partial<InferInsertModel<SchemaOf<TDatabase, TSchemaKey>>>): Promise<TSelect>;
39
41
  select(key: keyof InferSelectModel<SchemaOf<TDatabase, TSchemaKey>>): Promise<InferSelectModel<SchemaOf<TDatabase, TSchemaKey>>[typeof key][]>;
40
42
  }
41
43
 
@@ -12,30 +12,32 @@ type FindAllOptions<T extends PgTableWithColumns<any>> = {
12
12
  type ColumnOf<T> = T extends PgTableWithColumns<infer TConfig> ? keyof TConfig["columns"] : never;
13
13
  type SelectModelOf<T> = T extends TableRepository<infer TSelect, any> ? TSelect : never;
14
14
  type InsertModelOf<T> = T extends TableRepository<any, infer TInsert> ? TInsert : never;
15
- interface TableRepository<T extends PgTableWithColumns<any>, TSelect = InferSelectModel<T>> {
15
+ interface TableRepository<T extends PgTableWithColumns<any>, TSelect = InferSelectModel<T>, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never> {
16
16
  schema: T;
17
- find: (id: string) => Promise<TSelect | undefined>;
17
+ find: (pk: TSelect[TPrimaryKey]) => Promise<TSelect | undefined>;
18
18
  countTotal: (options: {
19
19
  where?: SQL<unknown>;
20
20
  }) => Promise<number>;
21
21
  findAll: (options: FindAllOptions<T>) => Promise<TSelect[]>;
22
22
  save: (values: InferInsertModel<T>) => Promise<TSelect>;
23
- delete: (id: string) => Promise<void>;
23
+ update: (pk: TSelect[TPrimaryKey], values: Partial<InferInsertModel<T>>) => Promise<TSelect>;
24
+ delete: (pk: TSelect[TPrimaryKey]) => Promise<void>;
24
25
  select(key: keyof InferSelectModel<T>): Promise<InferSelectModel<T>[typeof key][]>;
25
26
  }
26
27
  type SchemaOf<TDatabase extends NodePgDatabase<any>, TSchemaKey extends keyof TDatabase["_"]["fullSchema"]> = TDatabase["_"]["fullSchema"][TSchemaKey];
27
- declare class BaseTableRepository<TDatabase extends NodePgDatabase<any>, TSchemaKey extends keyof TDatabase["_"]["fullSchema"], TSelect = InferSelectModel<SchemaOf<TDatabase, TSchemaKey>>, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never> implements TableRepository<SchemaOf<TDatabase, TSchemaKey>> {
28
+ declare class BaseTableRepository<TDatabase extends NodePgDatabase<any>, TSchemaKey extends keyof TDatabase["_"]["fullSchema"], TSelect = InferSelectModel<SchemaOf<TDatabase, TSchemaKey>>, TPrimaryKey extends keyof TSelect = "id" extends keyof TSelect ? "id" : never> implements TableRepository<SchemaOf<TDatabase, TSchemaKey>, TSelect, TPrimaryKey> {
28
29
  db: TDatabase;
29
30
  schema: SchemaOf<TDatabase, TSchemaKey>;
30
31
  pk: TPrimaryKey;
31
32
  constructor(db: TDatabase, schema: TSchemaKey, pk?: TPrimaryKey);
32
- find(id: string): Promise<TSelect | undefined>;
33
+ find(pk: TSelect[TPrimaryKey]): Promise<TSelect | undefined>;
33
34
  countTotal({ where }: {
34
35
  where?: SQL<unknown>;
35
36
  }): Promise<number>;
36
37
  findAll(options: FindAllOptions<SchemaOf<TDatabase, TSchemaKey>>): Promise<TSelect[]>;
37
38
  save(values: InferInsertModel<SchemaOf<TDatabase, TSchemaKey>>): Promise<TSelect>;
38
- delete(pk: string): Promise<void>;
39
+ delete(pk: TSelect[TPrimaryKey]): Promise<void>;
40
+ update(pk: TSelect[TPrimaryKey], values: Partial<InferInsertModel<SchemaOf<TDatabase, TSchemaKey>>>): Promise<TSelect>;
39
41
  select(key: keyof InferSelectModel<SchemaOf<TDatabase, TSchemaKey>>): Promise<InferSelectModel<SchemaOf<TDatabase, TSchemaKey>>[typeof key][]>;
40
42
  }
41
43
 
@@ -33,8 +33,8 @@ var BaseTableRepository = class {
33
33
  this.schema = db._.fullSchema[schema];
34
34
  this.pk = pk;
35
35
  }
36
- async find(id) {
37
- return await this.db.select().from(this.schema).where((0, import_drizzle_orm.eq)(this.schema[this.pk], id)).limit(1).then((res) => res[0]);
36
+ async find(pk) {
37
+ return await this.db.select().from(this.schema).where((0, import_drizzle_orm.eq)(this.schema[this.pk], pk)).limit(1).then((res) => res[0]);
38
38
  }
39
39
  async countTotal({ where }) {
40
40
  const [{ total }] = await this.db.select({ total: (0, import_drizzle_orm.count)() }).from(this.schema).where(where);
@@ -69,6 +69,10 @@ var BaseTableRepository = class {
69
69
  async delete(pk) {
70
70
  await this.db.delete(this.schema).where((0, import_drizzle_orm.eq)(this.schema[this.pk], pk));
71
71
  }
72
+ async update(pk, values) {
73
+ const [item] = await this.db.update(this.schema).set(values).where((0, import_drizzle_orm.eq)(this.schema[this.pk], pk)).returning();
74
+ return item;
75
+ }
72
76
  async select(key) {
73
77
  const rows = await this.db.select({ value: this.schema[key] }).from(this.schema).groupBy(this.schema[key]);
74
78
  return rows.map((row) => row.value);
@@ -14,8 +14,8 @@ var BaseTableRepository = class {
14
14
  this.schema = db._.fullSchema[schema];
15
15
  this.pk = pk;
16
16
  }
17
- async find(id) {
18
- return await this.db.select().from(this.schema).where(eq(this.schema[this.pk], id)).limit(1).then((res) => res[0]);
17
+ async find(pk) {
18
+ return await this.db.select().from(this.schema).where(eq(this.schema[this.pk], pk)).limit(1).then((res) => res[0]);
19
19
  }
20
20
  async countTotal({ where }) {
21
21
  const [{ total }] = await this.db.select({ total: count() }).from(this.schema).where(where);
@@ -50,6 +50,10 @@ var BaseTableRepository = class {
50
50
  async delete(pk) {
51
51
  await this.db.delete(this.schema).where(eq(this.schema[this.pk], pk));
52
52
  }
53
+ async update(pk, values) {
54
+ const [item] = await this.db.update(this.schema).set(values).where(eq(this.schema[this.pk], pk)).returning();
55
+ return item;
56
+ }
53
57
  async select(key) {
54
58
  const rows = await this.db.select({ value: this.schema[key] }).from(this.schema).groupBy(this.schema[key]);
55
59
  return rows.map((row) => row.value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dn-react-router-toolkit",
3
- "version": "0.9.4",
3
+ "version": "0.9.5",
4
4
  "types": "./dist/index.d.ts",
5
5
  "main": "./dist/index.mjs",
6
6
  "module": "./dist/index.js",