peta-orm 0.3.0 → 0.4.1

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/index.d.mts CHANGED
@@ -1,6 +1,479 @@
1
- import { A as PaginatorJson, C as InsertGraphOptions, D as RelationType, E as RelationOptions, M as Database, O as PaginatedResult, S as QueryBuilder, T as Relation, _ as createColumn, a as ModelDefinition, b as Plugin, c as HookCallback, d as createHookManager, f as ModelId, g as ColumnValue, h as ColumnShape, i as ModelConfig, j as createPaginator, k as Paginator, l as HookManager, m as Column, n as Collection, o as ModelInstance, p as ORMLike, r as createCollection, s as Attribute, t as defineModel, u as LifecycleEvent, v as Constraint, w as UpsertGraphOptions, x as createQueryBuilder, y as SchemaConfig } from "./index-BdJnSMYi.mjs";
2
- import { Dialect } from "kysely";
1
+ import { Dialect, Kysely } from "kysely";
3
2
 
3
+ //#region src/lib/kysely.d.ts
4
+ type Database = Kysely<Record<string, never>>;
5
+ //#endregion
6
+ //#region src/pagination/index.d.ts
7
+ interface Paginator {
8
+ readonly data: Collection;
9
+ readonly total: number;
10
+ readonly perPage: number;
11
+ readonly currentPage: number;
12
+ readonly lastPage: number;
13
+ readonly hasMorePages: boolean;
14
+ readonly hasPages: boolean;
15
+ readonly firstItem: number;
16
+ readonly lastItem: number;
17
+ readonly onFirstPage: boolean;
18
+ readonly onLastPage: boolean;
19
+ readonly count: number;
20
+ map<T>(fn: (item: ModelInstance) => T): T[];
21
+ toJSON(): PaginatorJson;
22
+ }
23
+ interface PaginatorJson {
24
+ data: Record<string, unknown>[];
25
+ total: number;
26
+ perPage: number;
27
+ currentPage: number;
28
+ lastPage: number;
29
+ hasMorePages: boolean;
30
+ hasPages: boolean;
31
+ firstItem: number | null;
32
+ lastItem: number | null;
33
+ onFirstPage: boolean;
34
+ onLastPage: boolean;
35
+ }
36
+ type PaginatedResult = PaginatorJson;
37
+ declare function createPaginator(items: ModelInstance[], total: number, perPage: number, currentPage: number): Paginator;
38
+ //#endregion
39
+ //#region src/relations/base.d.ts
40
+ type RelationType = "hasMany" | "belongsTo" | "hasOne" | "manyToMany" | "hasManyThrough";
41
+ interface RelationOptions {
42
+ foreignKey?: string;
43
+ localKey?: string;
44
+ through?: string;
45
+ foreignPivotKey?: string;
46
+ relatedPivotKey?: string;
47
+ throughForeignKey?: string;
48
+ throughLocalKey?: string;
49
+ pivotExtras?: string[];
50
+ }
51
+ interface Relation {
52
+ readonly type: RelationType;
53
+ readonly relatedModelClass: ModelDefinition;
54
+ readonly foreignKey: string;
55
+ readonly localKey: string;
56
+ readonly throughTable?: string;
57
+ readonly foreignPivotKey?: string;
58
+ readonly relatedPivotKey?: string;
59
+ readonly throughForeignKey?: string;
60
+ readonly throughLocalKey?: string;
61
+ _morphMap?: Record<string, () => ModelDefinition>;
62
+ _morphType?: string;
63
+ _morphId?: string;
64
+ _morphTypeValue?: string;
65
+ query(parent: ModelInstance): QueryBuilder;
66
+ addEagerConstraints(query: QueryBuilder, models: ModelInstance[]): void;
67
+ match(models: ModelInstance[], results: ModelInstance[], relationName: string): void;
68
+ getResults(parent: ModelInstance): Promise<ModelInstance | ModelInstance[] | null>;
69
+ loadEager(models: ModelInstance[], relationName: string, constraints?: ((qb: QueryBuilder) => void) | null): Promise<void>;
70
+ }
71
+ //#endregion
72
+ //#region src/relations/graph/types.d.ts
73
+ interface InsertGraphOptions {
74
+ /** Allow `#id` / `#ref` special properties in the graph */
75
+ allowRefs?: boolean;
76
+ /**
77
+ * If `true`, objects with an `id` property get related (pivot row / FK set)
78
+ * instead of inserted. Can be an array of relation names to scope.
79
+ */
80
+ relate?: boolean | string[];
81
+ /**
82
+ * Whitelist of relation paths allowed for this graph operation.
83
+ * Accepts an array of dotted paths or a Set. If not set, all relations are allowed.
84
+ * When used via the query builder, the QB's `allowGraph()` set is forwarded automatically.
85
+ */
86
+ allowGraph?: string[] | Set<string>;
87
+ }
88
+ interface UpsertGraphOptions extends InsertGraphOptions {
89
+ /** Unrelate (set FK null / remove pivot) instead of deleting missing items */
90
+ unrelate?: boolean | string[];
91
+ /** Prevent deletion for all or specific relation paths */
92
+ noDelete?: boolean | string[];
93
+ /** Prevent insertion for all or specific relation paths */
94
+ noInsert?: boolean | string[];
95
+ /** Prevent update for all or specific relation paths */
96
+ noUpdate?: boolean | string[];
97
+ }
98
+ //#endregion
99
+ //#region src/query/types.d.ts
100
+ interface QueryBuilder extends PromiseLike<ModelInstance[]> {
101
+ execute(): Promise<ModelInstance[]>;
102
+ collect(): Promise<Collection>;
103
+ executeTakeFirst(): Promise<ModelInstance | undefined>;
104
+ executeTakeFirstOrThrow(): Promise<ModelInstance>;
105
+ find(id: number | string): Promise<ModelInstance | undefined>;
106
+ findOrFail(id: number | string): Promise<ModelInstance>;
107
+ first(): Promise<ModelInstance | undefined>;
108
+ toSQL(): {
109
+ sql: string;
110
+ parameters: readonly unknown[];
111
+ };
112
+ count(): Promise<number>;
113
+ sum(column: string): Promise<number>;
114
+ avg(column: string): Promise<number>;
115
+ min(column: string): Promise<number>;
116
+ max(column: string): Promise<number>;
117
+ withCount(relation: string): QueryBuilder;
118
+ withSum(relation: string, column: string): QueryBuilder;
119
+ withAvg(relation: string, column: string): QueryBuilder;
120
+ withMin(relation: string, column: string): QueryBuilder;
121
+ withMax(relation: string, column: string): QueryBuilder;
122
+ withExists(relation: string): QueryBuilder;
123
+ chunk(size: number, callback: (chunk: ModelInstance[]) => Promise<void>): Promise<void>;
124
+ paginate(page: number, perPage?: number): Promise<Paginator>;
125
+ insertGraph(data: Record<string, unknown> | Record<string, unknown>[], options?: InsertGraphOptions): Promise<any>;
126
+ upsertGraph(data: Record<string, unknown> | Record<string, unknown>[], options?: UpsertGraphOptions): Promise<any>;
127
+ with(...relations: (string | Record<string, (qb: QueryBuilder) => void>)[]): QueryBuilder;
128
+ /**
129
+ * Whitelist allowed relations (and nested paths) for eager loading.
130
+ * Throws if a relation path is not in the allow list.
131
+ *
132
+ * Supports dotted paths for granular control:
133
+ * - `allowGraph("posts")` allows `posts`, `posts.author`, `posts.author.profile`, etc.
134
+ * - `allowGraph("posts.author")` allows `posts.author` and `posts.author.profile`,
135
+ * but NOT bare `posts` or `posts.comments`.
136
+ *
137
+ * Multiple arguments are merged: `allowGraph("posts", "profile")`
138
+ */
139
+ allowGraph(...expressions: string[]): QueryBuilder;
140
+ updateMany(data: Record<string, unknown>): Promise<number>;
141
+ deleteMany(): Promise<number>;
142
+ withTrashed(): QueryBuilder;
143
+ onlyTrashed(): QueryBuilder;
144
+ whereIn(column: string, values: unknown[]): QueryBuilder;
145
+ whereInPivot(column: string, values: unknown[]): QueryBuilder;
146
+ has(relationName: string): QueryBuilder;
147
+ whereHas(relationName: string, callback?: (qb: QueryBuilder) => void): QueryBuilder;
148
+ whereDoesntHave(relationName: string, callback?: (qb: QueryBuilder) => void): QueryBuilder;
149
+ where(column: string, operator: unknown, value?: unknown): QueryBuilder;
150
+ whereRef(col1: string, operator: string, col2: string): QueryBuilder;
151
+ orWhere(column: string, operator: unknown, value?: unknown): QueryBuilder;
152
+ orderBy(column: string, direction?: "asc" | "desc"): QueryBuilder;
153
+ limit(n: number): QueryBuilder;
154
+ offset(n: number): QueryBuilder;
155
+ select(...columns: string[]): QueryBuilder;
156
+ selectAll(table?: string): QueryBuilder;
157
+ innerJoin(table: string, lhs: string, rhs: string): QueryBuilder;
158
+ leftJoin(table: string, lhs: string, rhs: string): QueryBuilder;
159
+ groupBy(...columns: string[]): QueryBuilder;
160
+ having(column: string, operator: string, value: unknown): QueryBuilder;
161
+ withoutGlobalScope(name: string): QueryBuilder;
162
+ all(): QueryBuilder;
163
+ /** @internal Access underlying Kysely builder for raw SQL operations */
164
+ _getKyselyQb(): any;
165
+ /** @internal Replace the underlying Kysely builder */
166
+ _replaceKyselyQb(newQb: any): void;
167
+ when(condition: unknown, callback: (q: QueryBuilder) => QueryBuilder): QueryBuilder;
168
+ unless(condition: unknown, callback: (q: QueryBuilder) => QueryBuilder): QueryBuilder;
169
+ }
170
+ //#endregion
171
+ //#region src/query/builder.d.ts
172
+ declare function createQueryBuilder(def: ModelDefinition, peta?: {
173
+ kysely: Database;
174
+ }): QueryBuilder;
175
+ //#endregion
176
+ //#region src/relations/related-query.d.ts
177
+ interface RelationQuery extends QueryBuilder {
178
+ /** Attach related model(s) for many-to-many relations. */
179
+ attach(ids: number | number[] | string | string[], pivotData?: Record<string, unknown>): Promise<void>;
180
+ /** Detach related model(s) for many-to-many relations. */
181
+ detach(ids?: number | number[] | string | string[]): Promise<void>;
182
+ /** Sync related models: attaches new IDs, detaches missing ones. */
183
+ sync(ids: (number | string)[] | Record<number | string, Record<string, unknown>>): Promise<void>;
184
+ /** Sync without detaching existing IDs. */
185
+ syncWithoutDetaching(ids: (number | string)[]): Promise<void>;
186
+ /** Update pivot data for a specific related model. */
187
+ updateExistingPivot(id: number | string, data: Record<string, unknown>): Promise<void>;
188
+ }
189
+ //#endregion
190
+ //#region src/plugins/index.d.ts
191
+ /**
192
+ * A plugin is a function that receives a model definition and can
193
+ * modify it by adding hooks, scopes, columns, or methods.
194
+ *
195
+ * ```ts
196
+ * const myPlugin = (options?: MyOptions): Plugin =>
197
+ * (def) => {
198
+ * def.addGlobalScope?.('active', (q) => q.where('active', true))
199
+ * return def
200
+ * }
201
+ * ```
202
+ */
203
+ type Plugin = (def: ModelDefinition) => undefined | ModelDefinition;
204
+ //#endregion
205
+ //#region src/columns/schema.d.ts
206
+ interface Constraint {
207
+ type: string;
208
+ args: unknown[];
209
+ }
210
+ interface SchemaConfig {
211
+ compile(dataType: string, args: unknown[], constraints: Constraint[]): unknown;
212
+ parse<T>(schema: unknown, value: unknown): T;
213
+ assert<T>(schema: unknown, value: unknown): T;
214
+ }
215
+ //#endregion
216
+ //#region src/columns/column.d.ts
217
+ interface Column<out T = unknown> {
218
+ readonly arkType: unknown;
219
+ readonly dataType: string;
220
+ readonly args: readonly unknown[];
221
+ readonly constraints: readonly Constraint[];
222
+ readonly isNullable: boolean;
223
+ readonly isPrimaryKey: boolean;
224
+ readonly isUnique: boolean;
225
+ readonly defaultValue: unknown;
226
+ hasConstraint(type: string): boolean;
227
+ parse(value: unknown): T;
228
+ assert(value: unknown): T;
229
+ primaryKey(): Column<T>;
230
+ nullable(): Column<T | null>;
231
+ default<V>(value: V): Column<T>;
232
+ unique(): Column<T>;
233
+ index(): Column<T>;
234
+ min(n: number): Column<T>;
235
+ max(n: number): Column<T>;
236
+ email(): Column<T>;
237
+ url(): Column<T>;
238
+ pattern(regex: RegExp | string): Column<T>;
239
+ references(table: () => unknown, columns: string[]): Column<T>;
240
+ }
241
+ declare function createColumn<T>(schema: SchemaConfig, dataType: string, args?: unknown[], constraints?: Constraint[]): Column<T>;
242
+ type ColumnShape = Record<string, Column>;
243
+ type ColumnValue<C> = C extends Column<infer T> ? T : never;
244
+ //#endregion
245
+ //#region src/types.d.ts
246
+ type ModelId = number & {
247
+ readonly __brand: "ModelId";
248
+ };
249
+ interface ModelLike {
250
+ get<T = unknown>(key: string): T;
251
+ set(key: string, value: unknown): void;
252
+ }
253
+ interface ORMLike {
254
+ readonly kysely: Database;
255
+ register(model: ModelDefinition$1<any>): void;
256
+ registerAll(...models: (ModelDefinition$1<any> | ModelDefinition$1<any>[])[]): void;
257
+ destroy(): Promise<void>;
258
+ transaction<T>(fn: (trx: import("kysely").Kysely<Record<string, never>>) => Promise<T>): Promise<T>;
259
+ readonly models: ReadonlyMap<string, ModelDefinition$1<any>>;
260
+ getModel<T extends ColumnShape = ColumnShape>(name: string): ModelDefinition$1<T> | undefined;
261
+ }
262
+ interface ModelDefinition$1<TColumns extends ColumnShape = ColumnShape> {
263
+ readonly table: string;
264
+ readonly columns: TColumns;
265
+ readonly relations: Record<string, Relation>;
266
+ readonly name: string;
267
+ _orm: ORMLike | null;
268
+ query(): QueryBuilder;
269
+ find(id: number | string): Promise<ModelInstance | undefined>;
270
+ findOrFail(id: number | string): Promise<ModelInstance>;
271
+ first(): Promise<ModelInstance | undefined>;
272
+ create(data: Record<string, unknown>): Promise<ModelInstance>;
273
+ insert(data: Record<string, unknown>): Promise<ModelInstance>;
274
+ insertMany(dataArray: Record<string, unknown>[]): Promise<ModelInstance[]>;
275
+ update(id: number | string, data: Record<string, unknown>): Promise<ModelInstance>;
276
+ delete(id: number | string): Promise<void>;
277
+ hydrate(row: Record<string, unknown>): ModelInstance;
278
+ on(event: string, callback: (model: ModelInstance) => void | Promise<void>): () => void;
279
+ getHooks(): HookManager;
280
+ addGlobalScope(name: string, callback: (qb: QueryBuilder) => void): void;
281
+ removeGlobalScope(name: string): void;
282
+ getGlobalScopes(): Map<string, (qb: QueryBuilder) => void> | undefined;
283
+ _init(orm: ORMLike): void;
284
+ }
285
+ //#endregion
286
+ //#region src/hooks/index.d.ts
287
+ type LifecycleEvent = "beforeCreate" | "afterCreate" | "beforeUpdate" | "afterUpdate" | "beforeSave" | "afterSave" | "beforeDelete" | "afterDelete" | "beforeRestore" | "afterRestore" | "beforeForceDelete" | "afterForceDelete";
288
+ type HookCallback = (model: ModelLike) => void | Promise<void>;
289
+ interface HookManager {
290
+ on(event: LifecycleEvent, callback: HookCallback): () => void;
291
+ off(event: LifecycleEvent, callback: HookCallback): void;
292
+ trigger(event: LifecycleEvent, model: ModelLike): Promise<void>;
293
+ clone(): HookManager;
294
+ }
295
+ declare function createHookManager(): HookManager;
296
+ //#endregion
297
+ //#region src/hooks/static.d.ts
298
+ interface StaticHookArgs {
299
+ /** Transform the mutating query into a SELECT to preview affected rows */
300
+ asFindQuery(): QueryBuilder;
301
+ /** Cancel the mutation and return a custom result */
302
+ cancelQuery(result: unknown): void;
303
+ /** The column data being inserted/updated (for create/update hooks) */
304
+ inputItems?: Record<string, unknown>[];
305
+ }
306
+ type StaticHookCallback = (args: StaticHookArgs) => void | Promise<void>;
307
+ //#endregion
308
+ //#region src/model/computed.d.ts
309
+ interface ComputedColumn<T = unknown> {
310
+ readonly type: "sql" | "runtime" | "batch";
311
+ readonly dependencies: string[];
312
+ /** Compute a value for a single record (runtime) */
313
+ compute?: (record: ModelInstance) => T;
314
+ /** Compute values for a batch of records (batch async) */
315
+ batchCompute?: (records: ModelInstance[]) => Promise<T[]>;
316
+ /** Raw SQL expression to inline in SELECT */
317
+ sql?: string;
318
+ }
319
+ //#endregion
320
+ //#region src/model/attribute.d.ts
321
+ /**
322
+ * Defines an accessor (`get`) and/or mutator (`set`) for a model attribute.
323
+ *
324
+ * ### Accessor (get)
325
+ * Transforms the attribute value when read via `model.get()` or `model.$toJSON()`.
326
+ * Receives the casted value and the model instance.
327
+ *
328
+ * ### Mutator (set)
329
+ * Transforms the attribute value when written via `model.set()`, `model.fill()`,
330
+ * or during model creation (`Model.insert()` / `Model.create()`).
331
+ * Receives the raw input value and the model instance, returns the value to store.
332
+ * Applied **before** type casting.
333
+ *
334
+ * ### Usage
335
+ * ```ts
336
+ * defineModel('users', {
337
+ * columns: { id, name, password, ... },
338
+ * attributes: {
339
+ * password: Attribute.make({
340
+ * set: (value) => Bun.password.hashSync(value, { algorithm: 'bcrypt' }),
341
+ * get: () => '***',
342
+ * }),
343
+ * fullName: Attribute.make({
344
+ * get: (_, instance) => `${instance.get('firstName')} ${instance.get('lastName')}`,
345
+ * }),
346
+ * },
347
+ * })
348
+ * ```
349
+ */
350
+ declare class Attribute<T = any> {
351
+ readonly get?: ((value: T, instance: ModelInstance) => any) | undefined;
352
+ readonly set?: ((value: any, instance: ModelInstance) => T) | undefined;
353
+ private constructor();
354
+ static make<T = any>(config: {
355
+ /** Transform the attribute value when read. Receives (castedValue, instance). */get?: (value: T, instance: ModelInstance) => any; /** Transform the attribute value when written. Receives (rawValue, instance), returns value to store. */
356
+ set?: (value: any, instance: ModelInstance) => T;
357
+ }): Attribute<T>;
358
+ }
359
+ //#endregion
360
+ //#region src/model/types.d.ts
361
+ interface ModelInstance {
362
+ readonly exists: boolean;
363
+ readonly attributes: Record<string, unknown>;
364
+ readonly dirtyAttributes: Record<string, unknown>;
365
+ isDirty(key?: string): boolean;
366
+ get<T = unknown>(key: string): T;
367
+ set(key: string, value: unknown): void;
368
+ fill(data: Record<string, unknown>): void;
369
+ reset(): void;
370
+ $getRelation<T = unknown>(name: string): T;
371
+ $setRelation(name: string, value: unknown): void;
372
+ $hasRelation(name: string): boolean;
373
+ $relationData(): Record<string, unknown>;
374
+ $load(...relations: string[]): Promise<void>;
375
+ $related(name: string): RelationQuery;
376
+ $save(): Promise<this>;
377
+ $delete(): Promise<void>;
378
+ $forceDelete(): Promise<void>;
379
+ $restore(): Promise<void>;
380
+ $trashed(): boolean;
381
+ $reload(): Promise<void>;
382
+ $toJSON(): Record<string, unknown>;
383
+ toJSON(): Record<string, unknown>;
384
+ }
385
+ interface ModelDefinition<TColumns extends ColumnShape = ColumnShape> {
386
+ readonly table: string;
387
+ readonly columns: TColumns;
388
+ readonly relations: Record<string, Relation>;
389
+ readonly name: string;
390
+ _orm: ORMLike | null;
391
+ query(): QueryBuilder;
392
+ find(id: number | string): Promise<ModelInstance | undefined>;
393
+ findOrFail(id: number | string): Promise<ModelInstance>;
394
+ first(): Promise<ModelInstance | undefined>;
395
+ create(data: Record<string, unknown>): Promise<ModelInstance>;
396
+ insert(data: Record<string, unknown>): Promise<ModelInstance>;
397
+ insertMany(dataArray: Record<string, unknown>[]): Promise<ModelInstance[]>;
398
+ update(id: number | string, data: Record<string, unknown>): Promise<ModelInstance>;
399
+ delete(id: number | string): Promise<void>;
400
+ insertGraph(data: Record<string, unknown> | Record<string, unknown>[], options?: InsertGraphOptions): Promise<any>;
401
+ upsertGraph(data: Record<string, unknown> | Record<string, unknown>[], options?: UpsertGraphOptions): Promise<any>;
402
+ hydrate(row: Record<string, unknown>): ModelInstance;
403
+ use(plugin: Plugin): ModelDefinition;
404
+ makeHelper<A extends any[], R>(fn: (qb: QueryBuilder, ...args: A) => R): (...args: A) => R;
405
+ on(event: string, callback: (model: ModelInstance) => void | Promise<void>): () => void;
406
+ getHooks(): HookManager;
407
+ beforeDelete(callback: StaticHookCallback): () => void;
408
+ afterDelete(callback: StaticHookCallback): () => void;
409
+ beforeUpdate(callback: StaticHookCallback): () => void;
410
+ afterUpdate(callback: StaticHookCallback): () => void;
411
+ beforeCreate(callback: StaticHookCallback): () => void;
412
+ afterCreate(callback: StaticHookCallback): () => void;
413
+ beforeFind(callback: StaticHookCallback): () => void;
414
+ afterFind(callback: StaticHookCallback): () => void;
415
+ addGlobalScope(name: string, callback: (qb: QueryBuilder) => void): void;
416
+ removeGlobalScope(name: string): void;
417
+ getGlobalScopes(): Map<string, (qb: QueryBuilder) => void> | undefined;
418
+ registerTimestamps?(createdAtCol?: string, updatedAtCol?: string): void;
419
+ registerSoftDeletes?(deletedAtCol?: string): void;
420
+ discover?(): Promise<never>;
421
+ _init(orm: ORMLike): void;
422
+ }
423
+ interface ModelConfig<TColumns extends ColumnShape = ColumnShape> {
424
+ columns: TColumns;
425
+ relations?: Record<string, Relation>;
426
+ casts?: Record<string, string>;
427
+ /** Per-attribute accessors (`get`) and/or mutators (`set`). See {@link Attribute.make}. */
428
+ attributes?: Record<string, Attribute<any>>;
429
+ hidden?: string[];
430
+ visible?: string[];
431
+ appends?: string[];
432
+ computed?: Record<string, ComputedColumn>;
433
+ }
434
+ //#endregion
435
+ //#region src/collection/index.d.ts
436
+ interface Collection {
437
+ readonly length: number;
438
+ [Symbol.iterator](): Iterator<ModelInstance>;
439
+ at(index: number): ModelInstance | undefined;
440
+ first(): ModelInstance | undefined;
441
+ last(): ModelInstance | undefined;
442
+ all(): ModelInstance[];
443
+ findBy(id: number | string): ModelInstance | undefined;
444
+ find(callback: (item: ModelInstance, index: number) => boolean): ModelInstance | undefined;
445
+ some(callback: (item: ModelInstance, index: number) => boolean): boolean;
446
+ includes(item: ModelInstance): boolean;
447
+ isEmpty(): boolean;
448
+ isNotEmpty(): boolean;
449
+ get(key: string): unknown[];
450
+ pluck(key: string): unknown[];
451
+ groupBy(key: string): Record<string, ModelInstance[]>;
452
+ keyBy(key: string): Record<string, ModelInstance>;
453
+ map<T>(fn: (item: ModelInstance, index: number) => T): T[];
454
+ filter(fn: (item: ModelInstance, index: number) => boolean): Collection;
455
+ reduce<T>(fn: (acc: T, item: ModelInstance, index: number) => T, initial: T): T;
456
+ forEach(fn: (item: ModelInstance, index: number) => void): void;
457
+ each(fn: (item: ModelInstance, index: number) => void): Collection;
458
+ unique(key?: string): Collection;
459
+ sortBy(key: string, direction?: "asc" | "desc"): Collection;
460
+ shuffle(): Collection;
461
+ take(n: number): Collection;
462
+ skip(n: number): Collection;
463
+ chunk(size: number): Collection[];
464
+ sum(key: string): number;
465
+ avg(key: string): number;
466
+ min(key: string): number;
467
+ max(key: string): number;
468
+ diff(other: Collection): Collection;
469
+ intersect(other: Collection): Collection;
470
+ concat(other: Collection): Collection;
471
+ push(...items: ModelInstance[]): void;
472
+ load(...relations: string[]): Promise<Collection>;
473
+ toJSON(): Record<string, unknown>[];
474
+ }
475
+ declare function createCollection(items?: ModelInstance[]): Collection;
476
+ //#endregion
4
477
  //#region src/columns/arktype.d.ts
5
478
  declare function createArkTypeSchemaConfig(): SchemaConfig;
6
479
  //#endregion
@@ -63,6 +536,9 @@ declare class DatabaseError extends Error {
63
536
  //#region src/errors/normalizer.d.ts
64
537
  declare function normalizeError(e: unknown, table?: string): DatabaseError;
65
538
  //#endregion
539
+ //#region src/model/define.d.ts
540
+ declare function defineModel<TColumns extends ColumnShape>(table: string, config: ModelConfig<TColumns>): ModelDefinition<TColumns>;
541
+ //#endregion
66
542
  //#region src/orm/index.d.ts
67
543
  interface ORMConfig {
68
544
  dialect: Dialect;
package/dist/index.mjs CHANGED
@@ -1,10 +1,10 @@
1
1
  import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
- import { n as createCollection } from "./collection-Dv3sQPMx.mjs";
2
+ import { n as createCollection } from "./collection-PFmrQHyM.mjs";
3
3
  import { a as ModelNotRegisteredError, c as ValidationError, i as ModelNotFoundError, n as normalizeError, o as RelationNotAllowedError, r as DatabaseError, s as RelationNotFoundError } from "./errors-sfFJolfu.mjs";
4
4
  import { a as registerSoftDeletesFor, n as getSoftDeleteConfig, o as registerTimestampsFor, r as hasSoftDelete, s as createHookManager, t as getHooksFor } from "./hooks-BD0xy7uw.mjs";
5
- import { a as castValue, r as initRuntime, t as createInstance } from "./factory-rIbPGjRg.mjs";
5
+ import { a as castValue, r as initRuntime, t as createInstance } from "./factory-BBvIMQuc.mjs";
6
6
  import { a as getRawRelations, d as setExists, o as getState } from "./state-LtlHp6XV.mjs";
7
- import { a as setConfig$1, n as reloadModel, r as saveModel, t as getConfig$1 } from "./save-B8rudcT5.mjs";
7
+ import { a as setConfig$1, n as reloadModel, r as saveModel, t as getConfig$1 } from "./save-D5UKXvqC.mjs";
8
8
  import { type } from "arktype";
9
9
  import { Kysely, sql } from "kysely";
10
10
  import { ulid as ulid$1 } from "ulid";
@@ -948,7 +948,7 @@ function createQueryBuilder(def, peta) {
948
948
  execute: runExecute,
949
949
  async collect() {
950
950
  const items = await runExecute();
951
- const { createCollection } = await import("./collection-Dv3sQPMx.mjs").then((n) => n.t);
951
+ const { createCollection } = await import("./collection-PFmrQHyM.mjs").then((n) => n.t);
952
952
  return createCollection(items);
953
953
  },
954
954
  async executeTakeFirst() {
@@ -1396,16 +1396,16 @@ function defineModel(table, config) {
1396
1396
  return this.query().first();
1397
1397
  },
1398
1398
  async create(data) {
1399
- return (await import("./save-B8rudcT5.mjs").then((n) => n.i)).insertModel(def, data);
1399
+ return (await import("./save-D5UKXvqC.mjs").then((n) => n.i)).insertModel(def, data);
1400
1400
  },
1401
1401
  async insert(data) {
1402
- return (await import("./save-B8rudcT5.mjs").then((n) => n.i)).insertModel(def, data);
1402
+ return (await import("./save-D5UKXvqC.mjs").then((n) => n.i)).insertModel(def, data);
1403
1403
  },
1404
1404
  async insertMany(dataArray) {
1405
- return (await import("./save-B8rudcT5.mjs").then((n) => n.i)).insertManyModel(def, dataArray);
1405
+ return (await import("./save-D5UKXvqC.mjs").then((n) => n.i)).insertManyModel(def, dataArray);
1406
1406
  },
1407
1407
  async update(id, data) {
1408
- return (await import("./save-B8rudcT5.mjs").then((n) => n.i)).updateModel(def, id, data);
1408
+ return (await import("./save-D5UKXvqC.mjs").then((n) => n.i)).updateModel(def, id, data);
1409
1409
  },
1410
1410
  async insertGraph(data, options) {
1411
1411
  return (await Promise.resolve().then(() => graph_exports)).insertGraph(def, data, options);
@@ -2157,7 +2157,10 @@ function hasMany(relatedThunk, options = {}) {
2157
2157
  function hasOne(relatedThunk, options = {}) {
2158
2158
  const base = hasMany(relatedThunk, options);
2159
2159
  const result = {};
2160
- for (const key of Object.keys(base)) Object.defineProperty(result, key, Object.getOwnPropertyDescriptor(base, key));
2160
+ for (const key of Object.keys(base)) {
2161
+ const desc = Object.getOwnPropertyDescriptor(base, key);
2162
+ Object.defineProperty(result, key, desc);
2163
+ }
2161
2164
  result.match = function match(models, results, relationName) {
2162
2165
  const grouped = groupByArray$1(results, base.foreignKey);
2163
2166
  for (const model of models) {
@@ -1,7 +1,7 @@
1
1
  import { t as __exportAll } from "./rolldown-runtime-D7D4PA-g.mjs";
2
2
  import { n as normalizeError, r as DatabaseError } from "./errors-sfFJolfu.mjs";
3
3
  import { t as getHooksFor } from "./hooks-BD0xy7uw.mjs";
4
- import { i as applyCastsToData, o as prepareForDb, t as createInstance } from "./factory-rIbPGjRg.mjs";
4
+ import { i as applyCastsToData, o as prepareForDb, t as createInstance } from "./factory-BBvIMQuc.mjs";
5
5
  import { d as setExists, f as syncOriginal, i as getExists, o as getState } from "./state-LtlHp6XV.mjs";
6
6
  //#region src/model/save.ts
7
7
  var save_exports = /* @__PURE__ */ __exportAll({
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "peta-orm",
3
3
  "type": "module",
4
4
  "private": false,
5
- "version": "0.3.0",
5
+ "version": "0.4.1",
6
6
  "description": "ORM for Bun, built on Kysely",
7
7
  "license": "MIT",
8
8
  "repository": {
@@ -16,17 +16,9 @@
16
16
  ".": {
17
17
  "import": "./dist/index.mjs",
18
18
  "types": "./dist/index.d.mts"
19
- },
20
- "./migrator": {
21
- "import": "./dist/migrations/index.mjs",
22
- "types": "./dist/migrations/index.d.mts"
23
19
  }
24
20
  },
25
- "bin": {
26
- "peta": "./bin/peta"
27
- },
28
21
  "files": [
29
- "bin/",
30
22
  "dist/",
31
23
  "README.md",
32
24
  "LICENSE"
@@ -38,7 +30,7 @@
38
30
  "ulid": "^3.0.2"
39
31
  },
40
32
  "peerDependencies": {
41
- "kysely": "^0.28.17",
33
+ "kysely": "^0.29.2",
42
34
  "typescript": "^6.0.0"
43
35
  },
44
36
  "devDependencies": {
@@ -46,8 +38,9 @@
46
38
  "@types/bun": "^1.3.14",
47
39
  "elysia": "^1.4.28",
48
40
  "hono": "^4.12.25",
49
- "kysely": "^0.28.17",
50
- "kysely-bun-sqlite": "^0.4.0",
41
+ "kysely": "^0.29.2",
42
+ "@libsql/kysely-libsql": "^0.4.1",
43
+ "@libsql/client": "^0.8.0",
51
44
  "mysql2": "^3.22.5",
52
45
  "pg": "^8.21.0",
53
46
  "tsdown": "^0.22.2",
package/bin/peta DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env bun
2
- import { run } from "../dist/migrations/cli.mjs"
3
- await run()