cloesce 0.0.5-unstable.1 → 0.0.5-unstable.2

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.
@@ -4,10 +4,7 @@ import { CrudKind } from "../ast.js";
4
4
  /**
5
5
  * cloesce/backend
6
6
  */
7
- export {
8
- CloesceApp,
9
- DependencyContainer as DependencyInjector,
10
- } from "../router/router.js";
7
+ export { CloesceApp, DependencyContainer as DependencyInjector, } from "../router/router.js";
11
8
  export type { MiddlewareFn, ResultMiddlewareFn } from "../router/router.js";
12
9
  export { HttpResult, Either, Stream } from "./common.js";
13
10
  export type { DeepPartial } from "./common.js";
@@ -178,9 +175,7 @@ export declare const DataSource: PropertyDecorator;
178
175
  * dogs: Dog[];
179
176
  * ```
180
177
  */
181
- export declare const OneToMany: (
182
- _foreignKeyColumn: string,
183
- ) => PropertyDecorator;
178
+ export declare const OneToMany: (_foreignKeyColumn: string) => PropertyDecorator;
184
179
  /**
185
180
  * Declares a one-to-one relationship between models.
186
181
  *
@@ -296,14 +291,10 @@ type Primitive = string | number | boolean | bigint | symbol | null | undefined;
296
291
  * }
297
292
  * ```
298
293
  */
299
- export type IncludeTree<T> = (T extends Primitive
300
- ? never
301
- : {
302
- [K in keyof T]?: T[K] extends (infer U)[]
303
- ? IncludeTree<NonNullable<U>>
304
- : IncludeTree<NonNullable<T[K]>>;
305
- }) & {
306
- __brand?: "IncludeTree";
294
+ export type IncludeTree<T> = (T extends Primitive ? never : {
295
+ [K in keyof T]?: T[K] extends (infer U)[] ? IncludeTree<NonNullable<U>> : IncludeTree<NonNullable<T[K]>>;
296
+ }) & {
297
+ __brand?: "IncludeTree";
307
298
  };
308
299
  /**
309
300
  * Represents the name of a `@DataSource` available on a model type `T`,
@@ -330,11 +321,8 @@ export type IncludeTree<T> = (T extends Primitive
330
321
  * async foo(ds: "default" | "none"): Promise<void> {...}
331
322
  * ```
332
323
  */
333
- export type DataSourceOf<T extends object> = (
334
- | KeysOfType<T, IncludeTree<T>>
335
- | "none"
336
- ) & {
337
- __brand?: "DataSource";
324
+ export type DataSourceOf<T extends object> = (KeysOfType<T, IncludeTree<T>> | "none") & {
325
+ __brand?: "DataSource";
338
326
  };
339
327
  /**
340
328
  * A branded `number` type indicating that the corresponding
@@ -354,203 +342,182 @@ export type DataSourceOf<T extends object> = (
354
342
  * ```
355
343
  */
356
344
  export type Integer = number & {
357
- __brand?: "Integer";
345
+ __brand?: "Integer";
358
346
  };
359
347
  /**
360
348
  * Exposes the ORM primitives Cloesce uses to interact with D1 databases.
361
349
  */
362
350
  export declare class Orm {
363
- private db;
364
- private constructor();
365
- /**
366
- * Creates an instance of an `Orm`
367
- * @param db The database to use for ORM calls.
368
- */
369
- static fromD1(db: D1Database): Orm;
370
- /**
371
- * Maps SQL records to an instantiated Model. The records must be flat
372
- * (e.g., of the form "id, name, address") or derive from a Cloesce data source view
373
- * (e.g., of the form "Horse.id, Horse.name, Horse.address")
374
- *
375
- * Assumes the data is formatted correctly, throwing an error otherwise.
376
- *
377
- * @param ctor The model constructor
378
- * @param records D1 Result records
379
- * @param includeTree Include tree to define the relationships to join.
380
- */
381
- static mapSql<T extends object>(
382
- ctor: new () => T,
383
- records: Record<string, any>[],
384
- includeTree?: IncludeTree<T> | null,
385
- ): T[];
386
- /**
387
- * Executes an "upsert" query, adding or augmenting a model in the database.
388
- *
389
- * If a model's primary key is not defined in `newModel`, the query is assumed to be an insert.
390
- *
391
- * If a model's primary key _is_ defined, but some attributes are missing, the query is assumed to be an update.
392
- *
393
- * Finally, if the primary key is defined, but all attributes are included, a SQLite upsert will be performed.
394
- *
395
- * In any other case, an error string will be returned.
396
- *
397
- * ### Inserting a new Model
398
- * ```ts
399
- * const model = {name: "julio", lastname: "pumpkin"};
400
- * const idRes = await orm.upsert(Person, model, null);
401
- * ```
402
- *
403
- * ### Updating an existing model
404
- * ```ts
405
- * const model = {id: 1, name: "timothy"};
406
- * const idRes = await orm.upsert(Person, model, null);
407
- * // (in db)=> {id: 1, name: "timothy", lastname: "pumpkin"}
408
- * ```
409
- *
410
- * ### Upserting a model
411
- * ```ts
412
- * // (assume a Person already exists)
413
- * const model = {
414
- * id: 1,
415
- * lastname: "burger", // updates last name
416
- * dog: {
417
- * name: "fido" // insert dog relationship
418
- * }
419
- * };
420
- * const idRes = await orm.upsert(Person, model, null);
421
- * // (in db)=> Person: {id: 1, dogId: 1 ...} ; Dog: {id: 1, name: "fido"}
422
- * ```
423
- *
424
- * @param ctor A model constructor.
425
- * @param newModel The new or augmented model.
426
- * @param includeTree An include tree describing which foreign keys to join.
427
- * @returns An error string, or the primary key of the inserted model.
428
- */
429
- upsert<T extends object>(
430
- ctor: new () => T,
431
- newModel: DeepPartial<T>,
432
- includeTree?: IncludeTree<T> | null,
433
- ): Promise<Either<string, any>>;
434
- /**
435
- * Returns a select query, creating a CTE view for the model using the provided include tree.
436
- *
437
- * @param ctor The model constructor.
438
- * @param includeTree An include tree describing which related models to join.
439
- * @param from An optional custom `FROM` clause to use instead of the base table.
440
- * @param tagCte An optional CTE name to tag the query with. Defaults to "Model.view".
441
- *
442
- * ### Example:
443
- * ```ts
444
- * // Using a data source
445
- * const query = Orm.listQuery(Person, "default");
446
- *
447
- * // Using a custom from statement
448
- * const query = Orm.listQuery(Person, null, "SELECT * FROM Person WHERE age > 18");
449
- * ```
450
- *
451
- * ### Example SQL output:
452
- * ```sql
453
- * WITH Person_view AS (
454
- * SELECT
455
- * "Person"."id" AS "id",
456
- * ...
457
- * FROM "Person"
458
- * LEFT JOIN ...
459
- * )
460
- * SELECT * FROM Person_view
461
- * ```
462
- */
463
- static listQuery<T extends object>(
464
- ctor: new () => T,
465
- opts: {
466
- includeTree?: IncludeTree<T> | null;
467
- from?: string;
468
- tagCte?: string;
469
- },
470
- ): string;
471
- /**
472
- * Returns a select query for a single model by primary key, creating a CTE view using the provided include tree.
473
- *
474
- * @param ctor The model constructor.
475
- * @param includeTree An include tree describing which related models to join.
476
- *
477
- * ### Example:
478
- * ```ts
479
- * // Using a data source
480
- * const query = Orm.getQuery(Person, "default");
481
- * ```
482
- *
483
- * ### Example SQL output:
484
- *
485
- * ```sql
486
- * WITH Person_view AS (
487
- * SELECT
488
- * "Person"."id" AS "id",
489
- * ...
490
- * FROM "Person"
491
- * LEFT JOIN ...
492
- * )
493
- * SELECT * FROM Person_view WHERE [Person].[id] = ?
494
- * ```
495
- */
496
- static getQuery<T extends object>(
497
- ctor: new () => T,
498
- includeTree?: IncludeTree<T> | null,
499
- ): string;
500
- /**
501
- * Retrieves all instances of a model from the database.
502
- * @param ctor The model constructor.
503
- * @param includeTree An include tree describing which related models to join.
504
- * @param from An optional custom `FROM` clause to use instead of the base table.
505
- * @returns Either an error string, or an array of model instances.
506
- *
507
- * ### Example:
508
- * ```ts
509
- * const orm = Orm.fromD1(env.db);
510
- * const horses = await orm.list(Horse, Horse.default);
511
- * ```
512
- *
513
- * ### Example with custom from:
514
- * ```ts
515
- * const orm = Orm.fromD1(env.db);
516
- * const adultHorses = await orm.list(Horse, Horse.default, "SELECT * FROM Horse ORDER BY age DESC LIMIT 10");
517
- * ```
518
- *
519
- * =>
520
- *
521
- * ```sql
522
- * SELECT
523
- * "Horse"."id" AS "id",
524
- * ...
525
- * FROM (SELECT * FROM Horse ORDER BY age DESC LIMIT 10)
526
- * LEFT JOIN ...
527
- * ```
528
- *
529
- */
530
- list<T extends object>(
531
- ctor: new () => T,
532
- opts: {
533
- includeTree?: IncludeTree<T> | null;
534
- from?: string;
535
- },
536
- ): Promise<Either<string, T[]>>;
537
- /**
538
- * Retrieves a single model by primary key.
539
- * @param ctor The model constructor.
540
- * @param id The primary key value.
541
- * @param includeTree An include tree describing which related models to join.
542
- * @returns Either an error string, or the model instance (null if not found).
543
- *
544
- * ### Example:
545
- * ```ts
546
- * const orm = Orm.fromD1(env.db);
547
- * const horse = await orm.get(Horse, 1, Horse.default);
548
- * ```
549
- */
550
- get<T extends object>(
551
- ctor: new () => T,
552
- id: any,
553
- includeTree?: IncludeTree<T> | null,
554
- ): Promise<Either<string, T | null>>;
351
+ private db;
352
+ private constructor();
353
+ /**
354
+ * Creates an instance of an `Orm`
355
+ * @param db The database to use for ORM calls.
356
+ */
357
+ static fromD1(db: D1Database): Orm;
358
+ /**
359
+ * Maps SQL records to an instantiated Model. The records must be flat
360
+ * (e.g., of the form "id, name, address") or derive from a Cloesce data source view
361
+ * (e.g., of the form "Horse.id, Horse.name, Horse.address")
362
+ *
363
+ * Assumes the data is formatted correctly, throwing an error otherwise.
364
+ *
365
+ * @param ctor The model constructor
366
+ * @param records D1 Result records
367
+ * @param includeTree Include tree to define the relationships to join.
368
+ */
369
+ static mapSql<T extends object>(ctor: new () => T, records: Record<string, any>[], includeTree?: IncludeTree<T> | null): T[];
370
+ /**
371
+ * Executes an "upsert" query, adding or augmenting a model in the database.
372
+ *
373
+ * If a model's primary key is not defined in `newModel`, the query is assumed to be an insert.
374
+ *
375
+ * If a model's primary key _is_ defined, but some attributes are missing, the query is assumed to be an update.
376
+ *
377
+ * Finally, if the primary key is defined, but all attributes are included, a SQLite upsert will be performed.
378
+ *
379
+ * In any other case, an error string will be returned.
380
+ *
381
+ * ### Inserting a new Model
382
+ * ```ts
383
+ * const model = {name: "julio", lastname: "pumpkin"};
384
+ * const idRes = await orm.upsert(Person, model, null);
385
+ * ```
386
+ *
387
+ * ### Updating an existing model
388
+ * ```ts
389
+ * const model = {id: 1, name: "timothy"};
390
+ * const idRes = await orm.upsert(Person, model, null);
391
+ * // (in db)=> {id: 1, name: "timothy", lastname: "pumpkin"}
392
+ * ```
393
+ *
394
+ * ### Upserting a model
395
+ * ```ts
396
+ * // (assume a Person already exists)
397
+ * const model = {
398
+ * id: 1,
399
+ * lastname: "burger", // updates last name
400
+ * dog: {
401
+ * name: "fido" // insert dog relationship
402
+ * }
403
+ * };
404
+ * const idRes = await orm.upsert(Person, model, null);
405
+ * // (in db)=> Person: {id: 1, dogId: 1 ...} ; Dog: {id: 1, name: "fido"}
406
+ * ```
407
+ *
408
+ * @param ctor A model constructor.
409
+ * @param newModel The new or augmented model.
410
+ * @param includeTree An include tree describing which foreign keys to join.
411
+ * @returns An error string, or the primary key of the inserted model.
412
+ */
413
+ upsert<T extends object>(ctor: new () => T, newModel: DeepPartial<T>, includeTree?: IncludeTree<T> | null): Promise<Either<string, any>>;
414
+ /**
415
+ * Returns a select query, creating a CTE view for the model using the provided include tree.
416
+ *
417
+ * @param ctor The model constructor.
418
+ * @param includeTree An include tree describing which related models to join.
419
+ * @param from An optional custom `FROM` clause to use instead of the base table.
420
+ * @param tagCte An optional CTE name to tag the query with. Defaults to "Model.view".
421
+ *
422
+ * ### Example:
423
+ * ```ts
424
+ * // Using a data source
425
+ * const query = Orm.listQuery(Person, "default");
426
+ *
427
+ * // Using a custom from statement
428
+ * const query = Orm.listQuery(Person, null, "SELECT * FROM Person WHERE age > 18");
429
+ * ```
430
+ *
431
+ * ### Example SQL output:
432
+ * ```sql
433
+ * WITH Person_view AS (
434
+ * SELECT
435
+ * "Person"."id" AS "id",
436
+ * ...
437
+ * FROM "Person"
438
+ * LEFT JOIN ...
439
+ * )
440
+ * SELECT * FROM Person_view
441
+ * ```
442
+ */
443
+ static listQuery<T extends object>(ctor: new () => T, opts: {
444
+ includeTree?: IncludeTree<T> | null;
445
+ from?: string;
446
+ tagCte?: string;
447
+ }): string;
448
+ /**
449
+ * Returns a select query for a single model by primary key, creating a CTE view using the provided include tree.
450
+ *
451
+ * @param ctor The model constructor.
452
+ * @param includeTree An include tree describing which related models to join.
453
+ *
454
+ * ### Example:
455
+ * ```ts
456
+ * // Using a data source
457
+ * const query = Orm.getQuery(Person, "default");
458
+ * ```
459
+ *
460
+ * ### Example SQL output:
461
+ *
462
+ * ```sql
463
+ * WITH Person_view AS (
464
+ * SELECT
465
+ * "Person"."id" AS "id",
466
+ * ...
467
+ * FROM "Person"
468
+ * LEFT JOIN ...
469
+ * )
470
+ * SELECT * FROM Person_view WHERE [Person].[id] = ?
471
+ * ```
472
+ */
473
+ static getQuery<T extends object>(ctor: new () => T, includeTree?: IncludeTree<T> | null): string;
474
+ /**
475
+ * Retrieves all instances of a model from the database.
476
+ * @param ctor The model constructor.
477
+ * @param includeTree An include tree describing which related models to join.
478
+ * @param from An optional custom `FROM` clause to use instead of the base table.
479
+ * @returns Either an error string, or an array of model instances.
480
+ *
481
+ * ### Example:
482
+ * ```ts
483
+ * const orm = Orm.fromD1(env.db);
484
+ * const horses = await orm.list(Horse, Horse.default);
485
+ * ```
486
+ *
487
+ * ### Example with custom from:
488
+ * ```ts
489
+ * const orm = Orm.fromD1(env.db);
490
+ * const adultHorses = await orm.list(Horse, Horse.default, "SELECT * FROM Horse ORDER BY age DESC LIMIT 10");
491
+ * ```
492
+ *
493
+ * =>
494
+ *
495
+ * ```sql
496
+ * SELECT
497
+ * "Horse"."id" AS "id",
498
+ * ...
499
+ * FROM (SELECT * FROM Horse ORDER BY age DESC LIMIT 10)
500
+ * LEFT JOIN ...
501
+ * ```
502
+ *
503
+ */
504
+ list<T extends object>(ctor: new () => T, opts: {
505
+ includeTree?: IncludeTree<T> | null;
506
+ from?: string;
507
+ }): Promise<Either<string, T[]>>;
508
+ /**
509
+ * Retrieves a single model by primary key.
510
+ * @param ctor The model constructor.
511
+ * @param id The primary key value.
512
+ * @param includeTree An include tree describing which related models to join.
513
+ * @returns Either an error string, or the model instance (null if not found).
514
+ *
515
+ * ### Example:
516
+ * ```ts
517
+ * const orm = Orm.fromD1(env.db);
518
+ * const horse = await orm.get(Horse, 1, Horse.default);
519
+ * ```
520
+ */
521
+ get<T extends object>(ctor: new () => T, id: any, includeTree?: IncludeTree<T> | null): Promise<Either<string, T | null>>;
555
522
  }
556
- //# sourceMappingURL=backend.d.ts.map
523
+ //# sourceMappingURL=backend.d.ts.map