peta-orm 0.2.5 → 0.2.6
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/README.md +2 -2
- package/dist/builder/query-builder.d.ts +2 -0
- package/dist/builder/query-builder.d.ts.map +1 -1
- package/dist/collection-wwtv7qmv.js +8 -0
- package/dist/index-4xnrys72.js +56 -0
- package/dist/{index-fm51cegm.js → index-ddxdqxz6.js} +7 -52
- package/dist/index.js +4 -2
- package/dist/{paginator-1amtn90p.js → paginator-tmp4hxj5.js} +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -349,7 +349,7 @@ bun run bin/peta migrate:status
|
|
|
349
349
|
| 05 | [query-builder](./examples/05-query-builder.ts) | where, orderBy, join, has, whereHas, whereDoesntHave, count |
|
|
350
350
|
| 06 | [hooks-timestamps](./examples/06-hooks-timestamps.ts) | beforeCreate, afterCreate, registerTimestamps |
|
|
351
351
|
| 07 | [soft-deletes](./examples/07-soft-deletes.ts) | $delete, $restore, $forceDelete, withTrashed |
|
|
352
|
-
| 08 | [collection-paginator](./examples/08-collection-paginator.ts) | Collection, Paginator |
|
|
352
|
+
| 08 | [collection-paginator](./examples/08-collection-paginator.ts) | Collection, Paginator, `.collect()` |
|
|
353
353
|
| 09 | [hono-integration](./examples/09-hono-integration.ts) | Hono app + error handling with `DatabaseError` |
|
|
354
354
|
| 10 | [elysia-integration](./examples/10-elysia-integration.ts) | Elysia app stub |
|
|
355
355
|
| 11 | [many-to-many](./examples/11-many-to-many.ts) | ManyToMany via pivot table |
|
|
@@ -373,7 +373,7 @@ bun run bin/peta migrate:status
|
|
|
373
373
|
| **Core** | `Peta`, `Model`, `$t`, `Collection` | `src/index.ts` |
|
|
374
374
|
| **Discovery** | `peta.discover(glob)`, `peta.registerAll(...models)` | `src/peta.ts` |
|
|
375
375
|
| **Columns** | `t.integer()`, `t.string()`, `t.email()`, `.min()`, `.max()`, `.nullable()`, `.default()` | `src/columns/column-types.ts` |
|
|
376
|
-
| **Builders** | `.where()`, `.with()`, `.paginate()`, `.chunk()`, `.sum()`, `.toSQL()`, `.when()`, `.unless()` | `src/builder/query-builder.ts` |
|
|
376
|
+
| **Builders** | `.where()`, `.with()`, `.paginate()`, `.chunk()`, `.sum()`, `.toSQL()`, `.when()`, `.unless()`, `.collect()` | `src/builder/query-builder.ts` |
|
|
377
377
|
| **Relations** | `HasMany`, `BelongsTo`, `HasOne`, `ManyToMany`, `HasManyThrough` | `src/relations/Relation.ts` |
|
|
378
378
|
| **Polymorphic** | `MorphTo`, `MorphMany`, `MorphOne` | `src/relations/Morph.ts` |
|
|
379
379
|
| **Hooks** | `HookManager`, `on()`, `off()`, `trigger()` | `src/hooks/lifecycle.ts` |
|
|
@@ -2,6 +2,7 @@ import type { Kysely } from "kysely";
|
|
|
2
2
|
import type { Model, ModelClass } from "../model/model";
|
|
3
3
|
import type { PetaLike } from "../types";
|
|
4
4
|
import { type WithArg } from "./eager-loader";
|
|
5
|
+
import type { Collection } from "../collection/collection";
|
|
5
6
|
export declare class ModelQueryBuilder<T extends Model> {
|
|
6
7
|
#private;
|
|
7
8
|
constructor(modelClass: ModelClass<T>, peta: PetaLike, kysely?: Kysely<any>);
|
|
@@ -11,6 +12,7 @@ export declare class ModelQueryBuilder<T extends Model> {
|
|
|
11
12
|
when(condition: unknown, callback: (q: this) => this): this;
|
|
12
13
|
unless(condition: unknown, callback: (q: this) => this): this;
|
|
13
14
|
execute(): Promise<T[]>;
|
|
15
|
+
collect(): Promise<Collection<T>>;
|
|
14
16
|
executeTakeFirst(): Promise<T | undefined>;
|
|
15
17
|
executeTakeFirstOrThrow(): Promise<T>;
|
|
16
18
|
find(id: number | string): Promise<T | undefined>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query-builder.d.ts","sourceRoot":"","sources":["../../src/builder/query-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAsB,MAAM,QAAQ,CAAA;AAExD,O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,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI;IAKxD,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI;IAKvD,OAAO,CAAC,GAAG,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI;IAKnC,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI;CAI/D"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
Collection
|
|
4
|
+
} from "./index-ddxdqxz6.js";
|
|
5
|
+
|
|
6
|
+
// src/pagination/paginator.ts
|
|
7
|
+
class Paginator {
|
|
8
|
+
data;
|
|
9
|
+
total;
|
|
10
|
+
perPage;
|
|
11
|
+
currentPage;
|
|
12
|
+
lastPage;
|
|
13
|
+
constructor(items, total, perPage, currentPage) {
|
|
14
|
+
this.data = new Collection(items);
|
|
15
|
+
this.total = total;
|
|
16
|
+
this.perPage = perPage;
|
|
17
|
+
this.currentPage = currentPage;
|
|
18
|
+
this.lastPage = Math.max(Math.ceil(total / perPage), 1);
|
|
19
|
+
}
|
|
20
|
+
get hasMorePages() {
|
|
21
|
+
return this.currentPage < this.lastPage;
|
|
22
|
+
}
|
|
23
|
+
get hasPages() {
|
|
24
|
+
return this.lastPage > 1;
|
|
25
|
+
}
|
|
26
|
+
get firstItem() {
|
|
27
|
+
return (this.currentPage - 1) * this.perPage + 1;
|
|
28
|
+
}
|
|
29
|
+
get lastItem() {
|
|
30
|
+
return Math.min(this.firstItem + this.data.length - 1, this.total);
|
|
31
|
+
}
|
|
32
|
+
get onFirstPage() {
|
|
33
|
+
return this.currentPage <= 1;
|
|
34
|
+
}
|
|
35
|
+
get onLastPage() {
|
|
36
|
+
return this.currentPage >= this.lastPage;
|
|
37
|
+
}
|
|
38
|
+
get count() {
|
|
39
|
+
return this.data.length;
|
|
40
|
+
}
|
|
41
|
+
map(callback) {
|
|
42
|
+
return this.data.map(callback);
|
|
43
|
+
}
|
|
44
|
+
toJSON() {
|
|
45
|
+
return {
|
|
46
|
+
data: this.data.toJSON(),
|
|
47
|
+
total: this.total,
|
|
48
|
+
perPage: this.perPage,
|
|
49
|
+
currentPage: this.currentPage,
|
|
50
|
+
lastPage: this.lastPage,
|
|
51
|
+
hasMorePages: this.hasMorePages
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export { Paginator };
|
|
@@ -211,6 +211,11 @@ class ModelQueryBuilder {
|
|
|
211
211
|
}
|
|
212
212
|
return models;
|
|
213
213
|
}
|
|
214
|
+
async collect() {
|
|
215
|
+
const items = await this.execute();
|
|
216
|
+
const { Collection: Col } = await import("./collection-wwtv7qmv.js");
|
|
217
|
+
return new Col(items);
|
|
218
|
+
}
|
|
214
219
|
async executeTakeFirst() {
|
|
215
220
|
const rows = await this.execute();
|
|
216
221
|
return rows[0];
|
|
@@ -271,7 +276,7 @@ class ModelQueryBuilder {
|
|
|
271
276
|
perPage = Math.max(1, Math.min(perPage, 1000));
|
|
272
277
|
const total = await this.clone().count();
|
|
273
278
|
const items = await this.clone().limit(perPage).offset((page - 1) * perPage).execute();
|
|
274
|
-
const { Paginator } = await import("./paginator-
|
|
279
|
+
const { Paginator } = await import("./paginator-tmp4hxj5.js");
|
|
275
280
|
return new Paginator(items, total, perPage, page);
|
|
276
281
|
}
|
|
277
282
|
with(...relations) {
|
|
@@ -628,54 +633,4 @@ class Collection {
|
|
|
628
633
|
}
|
|
629
634
|
}
|
|
630
635
|
|
|
631
|
-
|
|
632
|
-
class Paginator {
|
|
633
|
-
data;
|
|
634
|
-
total;
|
|
635
|
-
perPage;
|
|
636
|
-
currentPage;
|
|
637
|
-
lastPage;
|
|
638
|
-
constructor(items, total, perPage, currentPage) {
|
|
639
|
-
this.data = new Collection(items);
|
|
640
|
-
this.total = total;
|
|
641
|
-
this.perPage = perPage;
|
|
642
|
-
this.currentPage = currentPage;
|
|
643
|
-
this.lastPage = Math.max(Math.ceil(total / perPage), 1);
|
|
644
|
-
}
|
|
645
|
-
get hasMorePages() {
|
|
646
|
-
return this.currentPage < this.lastPage;
|
|
647
|
-
}
|
|
648
|
-
get hasPages() {
|
|
649
|
-
return this.lastPage > 1;
|
|
650
|
-
}
|
|
651
|
-
get firstItem() {
|
|
652
|
-
return (this.currentPage - 1) * this.perPage + 1;
|
|
653
|
-
}
|
|
654
|
-
get lastItem() {
|
|
655
|
-
return Math.min(this.firstItem + this.data.length - 1, this.total);
|
|
656
|
-
}
|
|
657
|
-
get onFirstPage() {
|
|
658
|
-
return this.currentPage <= 1;
|
|
659
|
-
}
|
|
660
|
-
get onLastPage() {
|
|
661
|
-
return this.currentPage >= this.lastPage;
|
|
662
|
-
}
|
|
663
|
-
get count() {
|
|
664
|
-
return this.data.length;
|
|
665
|
-
}
|
|
666
|
-
map(callback) {
|
|
667
|
-
return this.data.map(callback);
|
|
668
|
-
}
|
|
669
|
-
toJSON() {
|
|
670
|
-
return {
|
|
671
|
-
data: this.data.toJSON(),
|
|
672
|
-
total: this.total,
|
|
673
|
-
perPage: this.perPage,
|
|
674
|
-
currentPage: this.currentPage,
|
|
675
|
-
lastPage: this.lastPage,
|
|
676
|
-
hasMorePages: this.hasMorePages
|
|
677
|
-
};
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
export { ValidationError, ModelNotFoundError, RelationNotFoundError, ModelNotRegisteredError, DatabaseError, normalizeError, DeleteBuilder, EagerLoader, Collection, Paginator, ModelQueryBuilder, UpdateBuilder };
|
|
636
|
+
export { ValidationError, ModelNotFoundError, RelationNotFoundError, ModelNotRegisteredError, DatabaseError, normalizeError, DeleteBuilder, EagerLoader, Collection, ModelQueryBuilder, UpdateBuilder };
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,9 @@ import {
|
|
|
7
7
|
ManyToMany,
|
|
8
8
|
Peta
|
|
9
9
|
} from "./index-sm1xx8gs.js";
|
|
10
|
+
import {
|
|
11
|
+
Paginator
|
|
12
|
+
} from "./index-4xnrys72.js";
|
|
10
13
|
import {
|
|
11
14
|
Collection,
|
|
12
15
|
DatabaseError,
|
|
@@ -15,12 +18,11 @@ import {
|
|
|
15
18
|
ModelNotFoundError,
|
|
16
19
|
ModelNotRegisteredError,
|
|
17
20
|
ModelQueryBuilder,
|
|
18
|
-
Paginator,
|
|
19
21
|
RelationNotFoundError,
|
|
20
22
|
UpdateBuilder,
|
|
21
23
|
ValidationError,
|
|
22
24
|
normalizeError
|
|
23
|
-
} from "./index-
|
|
25
|
+
} from "./index-ddxdqxz6.js";
|
|
24
26
|
import"./index-k18nf2r7.js";
|
|
25
27
|
// node_modules/@ark/util/out/arrays.js
|
|
26
28
|
var liftArray = (data) => Array.isArray(data) ? data : [data];
|