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.
- package/dist/ast.d.ts +80 -100
- package/dist/ast.js +12 -12
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +331 -368
- package/dist/extractor/err.d.ts +26 -26
- package/dist/extractor/err.js +100 -129
- package/dist/extractor/extract.d.ts +17 -60
- package/dist/extractor/extract.js +764 -826
- package/dist/orm.wasm +0 -0
- package/dist/router/crud.d.ts +1 -1
- package/dist/router/crud.js +43 -42
- package/dist/router/router.d.ts +98 -135
- package/dist/router/router.js +381 -424
- package/dist/router/validator.d.ts +6 -11
- package/dist/router/validator.js +144 -158
- package/dist/router/wasm.d.ts +22 -56
- package/dist/router/wasm.js +79 -91
- package/dist/ui/backend.d.ts +181 -214
- package/dist/ui/backend.js +245 -258
- package/dist/ui/client.d.ts +1 -1
- package/dist/ui/common.d.ts +31 -54
- package/dist/ui/common.d.ts.map +1 -1
- package/dist/ui/common.js +159 -171
- package/package.json +2 -2
package/dist/ui/backend.d.ts
CHANGED
|
@@ -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
|
-
|
|
301
|
-
|
|
302
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
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
|