effect-qb 0.17.0 → 4.0.0-beta.66

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.
Files changed (38) hide show
  1. package/README.md +7 -0
  2. package/dist/mysql.js +207 -134
  3. package/dist/postgres/metadata.js +121 -52
  4. package/dist/postgres.js +210 -138
  5. package/dist/sqlite.js +207 -134
  6. package/package.json +2 -4
  7. package/src/internal/column-state.d.ts +3 -3
  8. package/src/internal/column-state.ts +3 -3
  9. package/src/internal/column.ts +8 -8
  10. package/src/internal/dialect.ts +1 -1
  11. package/src/internal/executor.ts +54 -35
  12. package/src/internal/query.d.ts +1 -1
  13. package/src/internal/query.ts +1 -1
  14. package/src/internal/runtime/driver-value-mapping.ts +3 -3
  15. package/src/internal/runtime/schema.ts +28 -38
  16. package/src/internal/runtime/value.ts +20 -23
  17. package/src/internal/scalar.d.ts +1 -1
  18. package/src/internal/scalar.ts +1 -1
  19. package/src/internal/schema-derivation.d.ts +12 -61
  20. package/src/internal/schema-derivation.ts +95 -43
  21. package/src/internal/table.d.ts +29 -22
  22. package/src/internal/table.ts +178 -29
  23. package/src/mysql/column.ts +6 -6
  24. package/src/mysql/executor.ts +4 -4
  25. package/src/mysql/function/temporal.ts +1 -1
  26. package/src/mysql/internal/dsl.ts +23 -17
  27. package/src/mysql/table.ts +28 -25
  28. package/src/postgres/column.ts +11 -11
  29. package/src/postgres/executor.ts +4 -4
  30. package/src/postgres/function/temporal.ts +1 -1
  31. package/src/postgres/internal/dsl.ts +23 -17
  32. package/src/postgres/schema-management.ts +1 -2
  33. package/src/postgres/table.ts +13 -21
  34. package/src/sqlite/column.ts +6 -6
  35. package/src/sqlite/executor.ts +4 -4
  36. package/src/sqlite/function/temporal.ts +1 -1
  37. package/src/sqlite/internal/dsl.ts +7 -6
  38. package/src/sqlite/table.ts +17 -25
package/dist/postgres.js CHANGED
@@ -347,12 +347,7 @@ var validateOptions = (tableName, fields, options) => {
347
347
  };
348
348
 
349
349
  // src/internal/schema-derivation.ts
350
- import * as VariantSchema from "@effect/experimental/VariantSchema";
351
350
  import * as Schema2 from "effect/Schema";
352
- var TableSchema = VariantSchema.make({
353
- variants: ["select", "insert", "update"],
354
- defaultVariant: "select"
355
- });
356
351
  var maybeBrandSchema = (column, tableName, columnName) => column.metadata.brand === true ? Schema2.brand(`${tableName}.${columnName}`)(column.schema) : column.schema;
357
352
  var selectSchema = (column, tableName, columnName) => column.metadata.nullable ? Schema2.NullOr(maybeBrandSchema(column, tableName, columnName)) : maybeBrandSchema(column, tableName, columnName);
358
353
  var insertSchema = (column, tableName, columnName) => {
@@ -369,42 +364,37 @@ var updateSchema = (column, tableName, columnName, isPrimaryKey) => {
369
364
  const base = column.metadata.nullable ? Schema2.NullOr(maybeBrandSchema(column, tableName, columnName)) : maybeBrandSchema(column, tableName, columnName);
370
365
  return Schema2.optional(base);
371
366
  };
372
- var deriveSchemas = (tableName, fields, primaryKeyColumns) => {
367
+ var fieldSchemaForVariant = (variant, column, tableName, columnName, primaryKeySet) => {
368
+ switch (variant) {
369
+ case "select":
370
+ return selectSchema(column, tableName, columnName);
371
+ case "insert":
372
+ return insertSchema(column, tableName, columnName);
373
+ case "update":
374
+ return updateSchema(column, tableName, columnName, primaryKeySet.has(columnName));
375
+ }
376
+ };
377
+ var deriveSchema = (variant, tableName, fields, primaryKeyColumns) => {
373
378
  const primaryKeySet = new Set(primaryKeyColumns);
374
- const variants = {};
379
+ const structFields = {};
375
380
  for (const [key, column] of Object.entries(fields)) {
376
- const config = {
377
- select: selectSchema(column, tableName, key),
378
- insert: undefined,
379
- update: undefined
380
- };
381
- const insert = insertSchema(column, tableName, key);
382
- const update = updateSchema(column, tableName, key, primaryKeySet.has(key));
383
- if (insert !== undefined) {
384
- config.insert = insert;
385
- } else {
386
- delete config.insert;
387
- }
388
- if (update !== undefined) {
389
- config.update = update;
390
- } else {
391
- delete config.update;
381
+ const schema = fieldSchemaForVariant(variant, column, tableName, key, primaryKeySet);
382
+ if (schema !== undefined) {
383
+ structFields[key] = schema;
392
384
  }
393
- variants[key] = TableSchema.Field(config);
394
385
  }
395
- const struct = TableSchema.Struct(variants);
396
- return {
397
- select: TableSchema.extract(struct, "select"),
398
- insert: TableSchema.extract(struct, "insert"),
399
- update: TableSchema.extract(struct, "update")
400
- };
386
+ return Schema2.Struct(structFields);
401
387
  };
388
+ var deriveSelectSchema = (tableName, fields, primaryKeyColumns) => deriveSchema("select", tableName, fields, primaryKeyColumns);
389
+ var deriveInsertSchema = (tableName, fields, primaryKeyColumns) => deriveSchema("insert", tableName, fields, primaryKeyColumns);
390
+ var deriveUpdateSchema = (tableName, fields, primaryKeyColumns) => deriveSchema("update", tableName, fields, primaryKeyColumns);
402
391
 
403
392
  // src/internal/table.ts
404
393
  var TypeId4 = Symbol.for("effect-qb/Table");
405
394
  var OptionsSymbol = Symbol.for("effect-qb/Table/normalizedOptions");
406
395
  var options = Symbol.for("effect-qb/Table/declaredOptions");
407
396
  var CacheSymbol = Symbol.for("effect-qb/Table/cache");
397
+ var SchemaCacheSymbol = Symbol.for("effect-qb/Table/schemaCache");
408
398
  var DeclaredOptionsSymbol = Symbol.for("effect-qb/Table/factoryDeclaredOptions");
409
399
  var TableProto = {
410
400
  pipe() {
@@ -427,14 +417,86 @@ var buildArtifacts = (name, fields, declaredOptions, schemaName) => {
427
417
  validateOptions(name, fields, declaredOptions);
428
418
  const primaryKey = resolvePrimaryKeyColumns(fields, declaredOptions);
429
419
  const columns = Object.fromEntries(Object.entries(fields).map(([key, column]) => [key, bindColumn(name, key, column, name, schemaName)]));
430
- const schemas = deriveSchemas(name, fields, primaryKey);
431
420
  return {
432
421
  columns,
433
- schemas,
434
422
  normalizedOptions,
435
423
  primaryKey
436
424
  };
437
425
  };
426
+ var getSchemaCache = (table) => {
427
+ const target = table;
428
+ if (target[SchemaCacheSymbol] !== undefined) {
429
+ return target[SchemaCacheSymbol];
430
+ }
431
+ const cache = {};
432
+ Object.defineProperty(table, SchemaCacheSymbol, {
433
+ configurable: true,
434
+ value: cache
435
+ });
436
+ return cache;
437
+ };
438
+ var deriveTableSchema = (table, variant) => {
439
+ const state = table[TypeId4];
440
+ switch (variant) {
441
+ case "select":
442
+ return deriveSelectSchema(state.name, state.fields, state.primaryKey);
443
+ case "insert":
444
+ return deriveInsertSchema(state.name, state.fields, state.primaryKey);
445
+ case "update":
446
+ return deriveUpdateSchema(state.name, state.fields, state.primaryKey);
447
+ }
448
+ };
449
+ var schemaFor = (table, variant) => {
450
+ const cache = getSchemaCache(table);
451
+ const cached = cache[variant];
452
+ if (cached !== undefined) {
453
+ return cached;
454
+ }
455
+ const schema = deriveTableSchema(table, variant);
456
+ cache[variant] = schema;
457
+ return schema;
458
+ };
459
+ function selectSchema2(table) {
460
+ return schemaFor(table, "select");
461
+ }
462
+ function insertSchema2(table) {
463
+ return schemaFor(table, "insert");
464
+ }
465
+ function updateSchema2(table) {
466
+ return schemaFor(table, "update");
467
+ }
468
+ var schemasFor = (table) => {
469
+ const cache = getSchemaCache(table);
470
+ if (cache.schemas !== undefined) {
471
+ return cache.schemas;
472
+ }
473
+ const schemas = {};
474
+ Object.defineProperties(schemas, {
475
+ select: {
476
+ enumerable: true,
477
+ get: () => selectSchema2(table)
478
+ },
479
+ insert: {
480
+ enumerable: true,
481
+ get: () => insertSchema2(table)
482
+ },
483
+ update: {
484
+ enumerable: true,
485
+ get: () => updateSchema2(table)
486
+ }
487
+ });
488
+ cache.schemas = schemas;
489
+ return schemas;
490
+ };
491
+ var defineSchemasGetter = (table) => {
492
+ Object.defineProperty(table, "schemas", {
493
+ configurable: true,
494
+ enumerable: true,
495
+ get() {
496
+ return schemasFor(table);
497
+ }
498
+ });
499
+ };
438
500
  var makeTable = (name, fields, declaredOptions, baseName = name, kind = "schema", schemaName, schemaMode = "default") => {
439
501
  const resolvedSchemaName = schemaMode === "explicit" ? schemaName : "public";
440
502
  const artifacts = buildArtifacts(name, fields, declaredOptions, resolvedSchemaName);
@@ -442,7 +504,7 @@ var makeTable = (name, fields, declaredOptions, baseName = name, kind = "schema"
442
504
  const table = attachPipe2(Object.create(TableProto));
443
505
  table.name = name;
444
506
  table.columns = artifacts.columns;
445
- table.schemas = artifacts.schemas;
507
+ defineSchemasGetter(table);
446
508
  table[TypeId4] = {
447
509
  name,
448
510
  baseName,
@@ -515,7 +577,6 @@ var ensureClassArtifacts = (self) => {
515
577
  const table = applyDeclaredOptions(makeTable(state.name, state.fields, [], state.name, "schema", state.schemaName, state.schemaName === undefined || state.schemaName === "public" ? "default" : "explicit"), classOptions);
516
578
  const artifacts = {
517
579
  columns: table.columns,
518
- schemas: table.schemas,
519
580
  normalizedOptions: table[OptionsSymbol],
520
581
  primaryKey: table[TypeId4].primaryKey
521
582
  };
@@ -538,7 +599,7 @@ var makeOption = (option) => {
538
599
  return builder;
539
600
  };
540
601
  var option = (spec) => makeOption(spec);
541
- function make2(name, fields, schemaName) {
602
+ function make(name, fields, schemaName) {
542
603
  const resolvedSchemaName = arguments.length >= 3 ? schemaName : "public";
543
604
  return makeTable(name, fields, [], name, "schema", resolvedSchemaName, arguments.length >= 3 ? "explicit" : "default");
544
605
  }
@@ -555,7 +616,7 @@ var alias = (table, aliasName) => {
555
616
  const aliased = attachPipe2(Object.create(TableProto));
556
617
  aliased.name = aliasName;
557
618
  aliased.columns = columns;
558
- aliased.schemas = deriveSchemas(aliasName, state.fields, state.primaryKey);
619
+ defineSchemasGetter(aliased);
559
620
  aliased[TypeId4] = {
560
621
  name: aliasName,
561
622
  baseName: state.baseName,
@@ -596,7 +657,7 @@ function Class(name, schemaName) {
596
657
  return ensureClassArtifacts(this).columns;
597
658
  }
598
659
  static get schemas() {
599
- return ensureClassArtifacts(this).schemas;
660
+ return schemasFor(this);
600
661
  }
601
662
  static get [TypeId4]() {
602
663
  const declaredOptions = extractDeclaredOptions(this[options]);
@@ -2684,7 +2745,7 @@ import * as Schema4 from "effect/Schema";
2684
2745
 
2685
2746
  // src/internal/runtime/value.ts
2686
2747
  import * as Schema3 from "effect/Schema";
2687
- var brandString = (pattern2, brand4) => Schema3.String.pipe(Schema3.pattern(pattern2), Schema3.brand(brand4));
2748
+ var brandString = (pattern, brand4) => Schema3.String.pipe(Schema3.check(Schema3.isPattern(pattern)), Schema3.brand(brand4));
2688
2749
  var localDatePattern = /^(\d{4})-(\d{2})-(\d{2})$/;
2689
2750
  var isValidLocalDateString = (value) => {
2690
2751
  const match = localDatePattern.exec(value);
@@ -2737,11 +2798,11 @@ var isValidInstantString = (value) => {
2737
2798
  const match = instantPattern.exec(value);
2738
2799
  return match !== null && isValidLocalDateString(match[1]) && isValidLocalTimeString(match[2]) && isValidOffset(match[3]);
2739
2800
  };
2740
- var LocalDateStringSchema = Schema3.String.pipe(Schema3.pattern(localDatePattern), Schema3.filter(isValidLocalDateString), Schema3.brand("LocalDateString"));
2741
- var LocalTimeStringSchema = Schema3.String.pipe(Schema3.pattern(localTimePattern), Schema3.filter(isValidLocalTimeString), Schema3.brand("LocalTimeString"));
2742
- var OffsetTimeStringSchema = Schema3.String.pipe(Schema3.pattern(offsetTimePattern), Schema3.filter(isValidOffsetTimeString), Schema3.brand("OffsetTimeString"));
2743
- var LocalDateTimeStringSchema = Schema3.String.pipe(Schema3.pattern(localDateTimePattern), Schema3.filter(isValidLocalDateTimeString), Schema3.brand("LocalDateTimeString"));
2744
- var InstantStringSchema = Schema3.String.pipe(Schema3.pattern(instantPattern), Schema3.filter(isValidInstantString), Schema3.brand("InstantString"));
2801
+ var LocalDateStringSchema = Schema3.String.pipe(Schema3.check(Schema3.isPattern(localDatePattern)), Schema3.check(Schema3.makeFilter((value) => isValidLocalDateString(value))), Schema3.brand("LocalDateString"));
2802
+ var LocalTimeStringSchema = Schema3.String.pipe(Schema3.check(Schema3.isPattern(localTimePattern)), Schema3.check(Schema3.makeFilter((value) => isValidLocalTimeString(value))), Schema3.brand("LocalTimeString"));
2803
+ var OffsetTimeStringSchema = Schema3.String.pipe(Schema3.check(Schema3.isPattern(offsetTimePattern)), Schema3.check(Schema3.makeFilter((value) => isValidOffsetTimeString(value))), Schema3.brand("OffsetTimeString"));
2804
+ var LocalDateTimeStringSchema = Schema3.String.pipe(Schema3.check(Schema3.isPattern(localDateTimePattern)), Schema3.check(Schema3.makeFilter((value) => isValidLocalDateTimeString(value))), Schema3.brand("LocalDateTimeString"));
2805
+ var InstantStringSchema = Schema3.String.pipe(Schema3.check(Schema3.isPattern(instantPattern)), Schema3.check(Schema3.makeFilter((value) => isValidInstantString(value))), Schema3.brand("InstantString"));
2745
2806
  var YearStringSchema = brandString(/^\d{4}$/, "YearString");
2746
2807
  var canonicalizeBigIntString = (input) => {
2747
2808
  const trimmed = input.trim();
@@ -2781,13 +2842,22 @@ var isCanonicalDecimalString = (value) => {
2781
2842
  return false;
2782
2843
  }
2783
2844
  };
2784
- var BigIntStringSchema = Schema3.String.pipe(Schema3.filter(isCanonicalBigIntString), Schema3.brand("BigIntString"));
2785
- var DecimalStringSchema = Schema3.String.pipe(Schema3.filter(isCanonicalDecimalString), Schema3.brand("DecimalString"));
2786
- var JsonValueSchema = Schema3.suspend(() => Schema3.Union(Schema3.String, Schema3.Number.pipe(Schema3.finite()), Schema3.Boolean, Schema3.Null, Schema3.Array(JsonValueSchema), Schema3.Record({
2787
- key: Schema3.String,
2788
- value: JsonValueSchema
2789
- })));
2790
- var JsonPrimitiveSchema = Schema3.Union(Schema3.String, Schema3.Number.pipe(Schema3.finite()), Schema3.Boolean, Schema3.Null);
2845
+ var BigIntStringSchema = Schema3.String.pipe(Schema3.check(Schema3.makeFilter((value) => isCanonicalBigIntString(value))), Schema3.brand("BigIntString"));
2846
+ var DecimalStringSchema = Schema3.String.pipe(Schema3.check(Schema3.makeFilter((value) => isCanonicalDecimalString(value))), Schema3.brand("DecimalString"));
2847
+ var JsonValueSchema = Schema3.suspend(() => Schema3.Union([
2848
+ Schema3.String,
2849
+ Schema3.Number.check(Schema3.isFinite()),
2850
+ Schema3.Boolean,
2851
+ Schema3.Null,
2852
+ Schema3.Array(JsonValueSchema),
2853
+ Schema3.Record(Schema3.String, JsonValueSchema)
2854
+ ]));
2855
+ var JsonPrimitiveSchema = Schema3.Union([
2856
+ Schema3.String,
2857
+ Schema3.Number.check(Schema3.isFinite()),
2858
+ Schema3.Boolean,
2859
+ Schema3.Null
2860
+ ]);
2791
2861
 
2792
2862
  // src/internal/runtime/normalize.ts
2793
2863
  var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
@@ -4995,8 +5065,7 @@ var EnumProto = {
4995
5065
  };
4996
5066
  },
4997
5067
  column() {
4998
- const values = this.values.map((value) => Schema6.Literal(value));
4999
- return makeColumnDefinition(values.length === 1 ? values[0] : Schema6.Union(...values), {
5068
+ return makeColumnDefinition(Schema6.Literals(this.values), {
5000
5069
  dbType: this.type(),
5001
5070
  nullable: false,
5002
5071
  hasDefault: false,
@@ -5634,8 +5703,8 @@ var renderNumericDdlType = (kind, options2) => {
5634
5703
  }
5635
5704
  return options2.scale === undefined ? `${kind}(${options2.precision})` : `${kind}(${options2.precision},${options2.scale})`;
5636
5705
  };
5637
- var boundedString = (length) => length === undefined ? Schema8.String : Schema8.String.pipe(Schema8.maxLength(length));
5638
- var finiteNumber = Schema8.Number.pipe(Schema8.finite());
5706
+ var boundedString = (length) => length === undefined ? Schema8.String : Schema8.String.check(Schema8.isMaxLength(length));
5707
+ var finiteNumber = Schema8.Number.check(Schema8.isFinite());
5639
5708
  var custom = (schema3, dbType) => makeColumnDefinition(schema3, {
5640
5709
  dbType: enrichDbType(dbType),
5641
5710
  nullable: false,
@@ -5647,7 +5716,7 @@ var custom = (schema3, dbType) => makeColumnDefinition(schema3, {
5647
5716
  ddlType: undefined,
5648
5717
  identity: undefined
5649
5718
  });
5650
- var uuid = () => primitive(Schema8.UUID, postgresDatatypes.uuid());
5719
+ var uuid = () => primitive(Schema8.String.check(Schema8.isUUID()), postgresDatatypes.uuid());
5651
5720
  var text = () => primitive(Schema8.String, postgresDatatypes.text());
5652
5721
  var int = () => primitive(Schema8.Int, postgresDatatypes.int4());
5653
5722
  var int2 = () => primitive(Schema8.Int, postgresDatatypes.int2());
@@ -5672,7 +5741,7 @@ var time = () => primitive(LocalTimeStringSchema, postgresDatatypes.time());
5672
5741
  var timetz = () => primitive(OffsetTimeStringSchema, postgresDatatypes.timetz());
5673
5742
  var timestamptz = () => primitive(InstantStringSchema, postgresDatatypes.timestamptz());
5674
5743
  var interval = () => primitive(Schema8.String, postgresDatatypes.interval());
5675
- var bytea = () => primitive(Schema8.Uint8ArrayFromSelf, postgresDatatypes.bytea());
5744
+ var bytea = () => primitive(Schema8.Uint8Array, postgresDatatypes.bytea());
5676
5745
  var name = () => primitive(Schema8.String, postgresDatatypes.name());
5677
5746
  var oid = () => primitive(Schema8.Int, postgresDatatypes.oid());
5678
5747
  var regclass = () => primitive(Schema8.String, postgresDatatypes.regclass());
@@ -11131,27 +11200,28 @@ var exports_executor = {};
11131
11200
  __export(exports_executor, {
11132
11201
  withTransaction: () => withTransaction2,
11133
11202
  withSavepoint: () => withSavepoint2,
11134
- make: () => make6,
11203
+ make: () => make5,
11135
11204
  driver: () => driver2,
11136
11205
  custom: () => custom3
11137
11206
  });
11138
11207
  import * as Effect2 from "effect/Effect";
11139
- import * as SqlClient3 from "@effect/sql/SqlClient";
11208
+ import * as SqlClient3 from "effect/unstable/sql/SqlClient";
11140
11209
  import * as Stream2 from "effect/Stream";
11141
11210
 
11142
11211
  // src/internal/executor.ts
11143
11212
  import * as Chunk from "effect/Chunk";
11144
11213
  import * as Effect from "effect/Effect";
11214
+ import * as Exit from "effect/Exit";
11145
11215
  import * as Option from "effect/Option";
11146
11216
  import * as Schema11 from "effect/Schema";
11147
- import * as SqlClient from "@effect/sql/SqlClient";
11217
+ import * as SqlClient from "effect/unstable/sql/SqlClient";
11148
11218
  import * as Stream from "effect/Stream";
11149
11219
 
11150
11220
  // src/internal/runtime/schema.ts
11151
11221
  import * as Schema10 from "effect/Schema";
11152
11222
  import * as SchemaAST from "effect/SchemaAST";
11153
11223
  var schemaCache = new WeakMap;
11154
- var FiniteNumberSchema = Schema10.Number.pipe(Schema10.finite());
11224
+ var FiniteNumberSchema = Schema10.Number.check(Schema10.isFinite());
11155
11225
  var runtimeSchemaForTag = (tag) => {
11156
11226
  switch (tag) {
11157
11227
  case "string":
@@ -11179,14 +11249,11 @@ var runtimeSchemaForTag = (tag) => {
11179
11249
  case "decimalString":
11180
11250
  return DecimalStringSchema;
11181
11251
  case "bytes":
11182
- return Schema10.Uint8ArrayFromSelf;
11252
+ return Schema10.Uint8Array;
11183
11253
  case "array":
11184
11254
  return Schema10.Array(Schema10.Unknown);
11185
11255
  case "record":
11186
- return Schema10.Record({
11187
- key: Schema10.String,
11188
- value: Schema10.Unknown
11189
- });
11256
+ return Schema10.Record(Schema10.String, Schema10.Unknown);
11190
11257
  case "null":
11191
11258
  return Schema10.Null;
11192
11259
  case "unknown":
@@ -11224,22 +11291,18 @@ var unionAst = (asts) => {
11224
11291
  if (asts.length === 1) {
11225
11292
  return asts[0];
11226
11293
  }
11227
- return SchemaAST.Union.make(asts);
11294
+ return new SchemaAST.Union(asts, "anyOf");
11228
11295
  };
11229
11296
  var propertyAstOf = (ast, key2) => {
11230
11297
  switch (ast._tag) {
11231
- case "Transformation":
11232
- return propertyAstOf(SchemaAST.typeAST(ast), key2);
11233
- case "Refinement":
11234
- return propertyAstOf(ast.from, key2);
11235
11298
  case "Suspend":
11236
- return propertyAstOf(ast.f(), key2);
11237
- case "TypeLiteral": {
11299
+ return propertyAstOf(ast.thunk(), key2);
11300
+ case "Objects": {
11238
11301
  const property = ast.propertySignatures.find((entry) => entry.name === key2);
11239
11302
  if (property !== undefined) {
11240
11303
  return property.type;
11241
11304
  }
11242
- const index5 = ast.indexSignatures.find((entry) => entry.parameter._tag === "StringKeyword");
11305
+ const index5 = ast.indexSignatures.find((entry) => entry.parameter._tag === "String");
11243
11306
  return index5?.type;
11244
11307
  }
11245
11308
  case "Union": {
@@ -11255,21 +11318,17 @@ var propertyAstOf = (ast, key2) => {
11255
11318
  };
11256
11319
  var numberAstOf = (ast, index5) => {
11257
11320
  switch (ast._tag) {
11258
- case "Transformation":
11259
- return numberAstOf(SchemaAST.typeAST(ast), index5);
11260
- case "Refinement":
11261
- return numberAstOf(ast.from, index5);
11262
11321
  case "Suspend":
11263
- return numberAstOf(ast.f(), index5);
11264
- case "TupleType": {
11322
+ return numberAstOf(ast.thunk(), index5);
11323
+ case "Arrays": {
11265
11324
  const element = ast.elements[index5];
11266
11325
  if (element !== undefined) {
11267
- return element.type;
11326
+ return element;
11268
11327
  }
11269
11328
  if (ast.rest.length === 0) {
11270
11329
  return;
11271
11330
  }
11272
- return unionAst(ast.rest.map((entry) => entry.type));
11331
+ return unionAst(ast.rest);
11273
11332
  }
11274
11333
  case "Union": {
11275
11334
  const values2 = ast.types.flatMap((member) => {
@@ -11284,7 +11343,7 @@ var numberAstOf = (ast, index5) => {
11284
11343
  };
11285
11344
  var exactJsonSegments = (segments) => segments.every((segment) => segment.kind === "key" || segment.kind === "index");
11286
11345
  var schemaAstAtExactJsonPath = (schema4, segments) => {
11287
- let current = SchemaAST.typeAST(schema4.ast);
11346
+ let current = schema4.ast;
11288
11347
  for (const segment of segments) {
11289
11348
  if (segment.kind === "key") {
11290
11349
  const property = propertyAstOf(current, segment.key);
@@ -11314,28 +11373,28 @@ var unionSchemas = (schemas) => {
11314
11373
  if (resolved.length === 1) {
11315
11374
  return resolved[0];
11316
11375
  }
11317
- return Schema10.Union(...resolved);
11376
+ return Schema10.Union(resolved);
11318
11377
  };
11319
11378
  var firstSelectedExpression = (plan) => {
11320
11379
  const selection = getAst(plan).select;
11321
11380
  return flattenSelection(selection)[0]?.expression;
11322
11381
  };
11323
11382
  var isJsonCompatibleAst = (ast) => {
11383
+ ast = SchemaAST.toType(ast);
11324
11384
  switch (ast._tag) {
11325
- case "StringKeyword":
11326
- case "NumberKeyword":
11327
- case "BooleanKeyword":
11328
- case "TupleType":
11329
- case "TypeLiteral":
11385
+ case "String":
11386
+ case "Number":
11387
+ case "Boolean":
11388
+ case "Null":
11389
+ case "Arrays":
11390
+ case "Objects":
11330
11391
  return true;
11331
11392
  case "Literal":
11332
- return ast.literal === null || typeof ast.literal === "string" || typeof ast.literal === "number" || typeof ast.literal === "boolean";
11393
+ return typeof ast.literal === "string" || typeof ast.literal === "number" || typeof ast.literal === "boolean";
11333
11394
  case "Union":
11334
11395
  return ast.types.every(isJsonCompatibleAst);
11335
- case "Transformation":
11336
- return isJsonCompatibleAst(SchemaAST.typeAST(ast));
11337
11396
  case "Suspend":
11338
- return isJsonCompatibleAst(ast.f());
11397
+ return isJsonCompatibleAst(ast.thunk());
11339
11398
  default:
11340
11399
  return false;
11341
11400
  }
@@ -11344,14 +11403,14 @@ var jsonCompatibleSchema = (schema4) => {
11344
11403
  if (schema4 === undefined) {
11345
11404
  return;
11346
11405
  }
11347
- const ast = SchemaAST.typeAST(schema4.ast);
11406
+ const ast = SchemaAST.toType(schema4.ast);
11348
11407
  return isJsonCompatibleAst(ast) ? schema4 : JsonValueSchema;
11349
11408
  };
11350
11409
  var buildStructSchema = (entries, context) => {
11351
11410
  const fields2 = Object.fromEntries(entries.map((entry) => [entry.key, expressionRuntimeSchema(entry.value, context) ?? JsonValueSchema]));
11352
11411
  return Schema10.Struct(fields2);
11353
11412
  };
11354
- var buildTupleSchema = (values2, context) => Schema10.Tuple(...values2.map((value) => expressionRuntimeSchema(value, context) ?? JsonValueSchema));
11413
+ var buildTupleSchema = (values2, context) => Schema10.Tuple(values2.map((value) => expressionRuntimeSchema(value, context) ?? JsonValueSchema));
11355
11414
  var deriveCaseSchema = (ast, context) => {
11356
11415
  if (context === undefined) {
11357
11416
  return unionSchemas([
@@ -11548,24 +11607,31 @@ var hasWriteCapability = (plan) => {
11548
11607
  }
11549
11608
  return false;
11550
11609
  };
11551
- var makeRowDecodeError = (rendered, projection, expression, raw, stage, cause, normalized) => ({
11552
- _tag: "RowDecodeError",
11553
- message: stage === "normalize" ? `Failed to normalize projection '${projection.alias}'` : `Failed to decode projection '${projection.alias}' against its runtime schema`,
11554
- dialect: rendered.dialect,
11555
- query: {
11556
- sql: rendered.sql,
11557
- params: rendered.params
11558
- },
11559
- projection: {
11560
- alias: projection.alias,
11561
- path: projection.path
11562
- },
11563
- dbType: expression[TypeId2].dbType,
11564
- raw,
11565
- normalized,
11566
- stage,
11567
- cause
11568
- });
11610
+ var makeRowDecodeError = (rendered, projection, expression, raw, stage, cause, normalized) => {
11611
+ const schemaError = Schema11.isSchemaError(cause) ? {
11612
+ message: cause.message,
11613
+ issue: cause.issue
11614
+ } : undefined;
11615
+ return {
11616
+ _tag: "RowDecodeError",
11617
+ message: stage === "normalize" ? `Failed to normalize projection '${projection.alias}'` : `Failed to decode projection '${projection.alias}' against its runtime schema`,
11618
+ dialect: rendered.dialect,
11619
+ query: {
11620
+ sql: rendered.sql,
11621
+ params: rendered.params
11622
+ },
11623
+ projection: {
11624
+ alias: projection.alias,
11625
+ path: projection.path
11626
+ },
11627
+ dbType: expression[TypeId2].dbType,
11628
+ raw,
11629
+ normalized,
11630
+ stage,
11631
+ cause,
11632
+ ...schemaError === undefined ? {} : { schemaError }
11633
+ };
11634
+ };
11569
11635
  var hasOptionalSourceDependency = (expression, scope) => {
11570
11636
  const state = expression[TypeId2];
11571
11637
  return Object.keys(state.dependencies).some((sourceName) => !scope.absentSourceNames.has(sourceName) && scope.sourceModes.get(sourceName) === "optional");
@@ -11608,8 +11674,8 @@ var decodeProjectionValue = (rendered, projection, expression, raw, scope, drive
11608
11674
  driverValueMapping: expression[TypeId2].driverValueMapping,
11609
11675
  valueMappings
11610
11676
  });
11611
- } catch (cause) {
11612
- throw makeRowDecodeError(rendered, projection, expression, raw, "normalize", cause);
11677
+ } catch (cause2) {
11678
+ throw makeRowDecodeError(rendered, projection, expression, raw, "normalize", cause2);
11613
11679
  }
11614
11680
  }
11615
11681
  const nullability = effectiveRuntimeNullability(expression, scope);
@@ -11635,11 +11701,15 @@ var decodeProjectionValue = (rendered, projection, expression, raw, scope, drive
11635
11701
  if (Schema11.is(schema4)(normalized)) {
11636
11702
  return normalized;
11637
11703
  }
11638
- try {
11639
- return Schema11.decodeUnknownSync(schema4)(normalized);
11640
- } catch (cause) {
11641
- throw makeRowDecodeError(rendered, projection, expression, raw, "schema", cause, normalized);
11704
+ const decoded = Schema11.decodeUnknownExit(schema4)(normalized);
11705
+ if (Exit.isSuccess(decoded)) {
11706
+ return decoded.value;
11642
11707
  }
11708
+ const cause = Option.match(Exit.findErrorOption(decoded), {
11709
+ onNone: () => decoded.cause,
11710
+ onSome: (schemaError) => schemaError
11711
+ });
11712
+ throw makeRowDecodeError(rendered, projection, expression, raw, "schema", cause, normalized);
11643
11713
  };
11644
11714
  var makeRowDecoder = (rendered, plan, options2 = {}) => {
11645
11715
  const projections = flattenSelection(getAst(plan).select);
@@ -11662,15 +11732,11 @@ var makeRowDecoder = (rendered, plan, options2 = {}) => {
11662
11732
  return decoded;
11663
11733
  };
11664
11734
  };
11665
- var decodeChunk = (rendered, plan, rows, options2 = {}) => {
11666
- const decodeRow = makeRowDecoder(rendered, plan, options2);
11667
- return Chunk.unsafeFromArray(Chunk.toReadonlyArray(rows).map((row) => decodeRow(row)));
11668
- };
11669
11735
  var decodeRows = (rendered, plan, rows, options2 = {}) => {
11670
11736
  const decodeRow = makeRowDecoder(rendered, plan, options2);
11671
11737
  return rows.map((row) => decodeRow(row));
11672
11738
  };
11673
- var make4 = (dialect, execute) => ({
11739
+ var make3 = (dialect, execute) => ({
11674
11740
  dialect,
11675
11741
  execute(plan) {
11676
11742
  return execute(plan);
@@ -11693,7 +11759,7 @@ function driver(dialect, executeOrHandlers) {
11693
11759
  }
11694
11760
  };
11695
11761
  }
11696
- var streamFromSqlClient = (query) => Stream.unwrapScoped(Effect.flatMap(SqlClient.SqlClient, (sql) => Effect.flatMap(Effect.serviceOption(SqlClient.TransactionConnection), Option.match({
11762
+ var streamFromSqlClient = (query) => Stream.unwrap(Effect.flatMap(SqlClient.SqlClient, (sql) => Effect.flatMap(Effect.serviceOption(sql.transactionService), Option.match({
11697
11763
  onNone: () => sql.reserve,
11698
11764
  onSome: ([connection]) => Effect.succeed(connection)
11699
11765
  })).pipe(Effect.map((connection) => connection.executeStream(query.sql, [...query.params], undefined)))));
@@ -11719,7 +11785,7 @@ var validateProjectionPathsMatchSelection = (plan, projections) => {
11719
11785
  }
11720
11786
  }
11721
11787
  };
11722
- function make5(dialect, render2) {
11788
+ function make4(dialect, render2) {
11723
11789
  if (typeof render2 !== "function") {
11724
11790
  throw new Error(`Renderer.make requires an explicit render implementation for dialect: ${dialect}`);
11725
11791
  }
@@ -11795,8 +11861,8 @@ var fromDriver = (renderer, sqlDriver, driverMode = "raw", valueMappings) => ({
11795
11861
  },
11796
11862
  stream(plan) {
11797
11863
  const rendered = renderer.render(plan);
11798
- return Stream2.mapError(Stream2.mapChunksEffect(sqlDriver.stream(rendered), (rows) => Effect2.try({
11799
- try: () => decodeChunk(rendered, plan, rows, { driverMode, valueMappings }),
11864
+ return Stream2.mapError(Stream2.mapArrayEffect(sqlDriver.stream(rendered), (rows) => Effect2.try({
11865
+ try: () => decodeRows(rendered, plan, rows, { driverMode, valueMappings }),
11800
11866
  catch: (error) => error
11801
11867
  })), (error) => {
11802
11868
  if (typeof error === "object" && error !== null && "_tag" in error && error._tag === "RowDecodeError") {
@@ -11811,13 +11877,13 @@ var sqlClientDriver = () => driver2({
11811
11877
  execute: (query) => Effect2.flatMap(SqlClient3.SqlClient, (sql) => sql.unsafe(query.sql, [...query.params])),
11812
11878
  stream: (query) => streamFromSqlClient(query)
11813
11879
  });
11814
- function make6(options2 = {}) {
11880
+ function make5(options2 = {}) {
11815
11881
  if (options2.driver) {
11816
- return fromDriver(options2.renderer ?? make5("postgres", (plan) => renderPostgresPlan(plan, { valueMappings: options2.valueMappings })), options2.driver, options2.driverMode, options2.valueMappings);
11882
+ return fromDriver(options2.renderer ?? make4("postgres", (plan) => renderPostgresPlan(plan, { valueMappings: options2.valueMappings })), options2.driver, options2.driverMode, options2.valueMappings);
11817
11883
  }
11818
- return fromDriver(options2.renderer ?? make5("postgres", (plan) => renderPostgresPlan(plan, { valueMappings: options2.valueMappings })), sqlClientDriver(), options2.driverMode, options2.valueMappings);
11884
+ return fromDriver(options2.renderer ?? make4("postgres", (plan) => renderPostgresPlan(plan, { valueMappings: options2.valueMappings })), sqlClientDriver(), options2.driverMode, options2.valueMappings);
11819
11885
  }
11820
- var custom3 = (execute) => make4("postgres", execute);
11886
+ var custom3 = (execute) => make3("postgres", execute);
11821
11887
  // src/postgres/query.ts
11822
11888
  var exports_query2 = {};
11823
11889
  __export(exports_query2, {
@@ -11930,15 +11996,18 @@ __export(exports_schema_expression2, {
11930
11996
  // src/postgres/table.ts
11931
11997
  var exports_table2 = {};
11932
11998
  __export(exports_table2, {
11999
+ updateSchema: () => updateSchema3,
11933
12000
  unique: () => unique4,
12001
+ selectSchema: () => selectSchema3,
11934
12002
  schema: () => schema4,
11935
12003
  primaryKey: () => primaryKey4,
11936
12004
  options: () => options2,
11937
12005
  option: () => option2,
11938
- make: () => make7,
12006
+ make: () => make6,
12007
+ insertSchema: () => insertSchema3,
11939
12008
  index: () => index5,
11940
12009
  foreignKey: () => foreignKey4,
11941
- check: () => check2,
12010
+ check: () => check3,
11942
12011
  alias: () => alias2,
11943
12012
  TypeId: () => TypeId10,
11944
12013
  OptionsSymbol: () => OptionsSymbol2,
@@ -11947,7 +12016,7 @@ __export(exports_table2, {
11947
12016
  var TypeId10 = TypeId4;
11948
12017
  var OptionsSymbol2 = OptionsSymbol;
11949
12018
  var options2 = options;
11950
- var make7 = (name2, fields2, schemaName = "public") => make2(name2, fields2, schemaName);
12019
+ var make6 = (name2, fields2, schemaName = "public") => make(name2, fields2, schemaName);
11951
12020
  var schema4 = (schemaName) => {
11952
12021
  const table = (name2, fields2, ...declaredOptions) => schema(schemaName).table(name2, fields2, ...declaredOptions);
11953
12022
  return {
@@ -12046,7 +12115,7 @@ var foreignKey4 = (columnsOrSpec, target, referencedColumns) => isObject(columns
12046
12115
  initiallyDeferred: spec.initiallyDeferred
12047
12116
  });
12048
12117
  })() : foreignKey(columnsOrSpec, target, referencedColumns);
12049
- var check2 = (nameOrSpec, predicate) => isObject(nameOrSpec) ? (() => {
12118
+ var check3 = (nameOrSpec, predicate) => isObject(nameOrSpec) ? (() => {
12050
12119
  const spec = nameOrSpec;
12051
12120
  const specPredicate = spec.predicate;
12052
12121
  const placeholder = {
@@ -12077,6 +12146,9 @@ var check2 = (nameOrSpec, predicate) => isObject(nameOrSpec) ? (() => {
12077
12146
  predicate: predicateOrFactory
12078
12147
  });
12079
12148
  })();
12149
+ var selectSchema3 = selectSchema2;
12150
+ var insertSchema3 = insertSchema2;
12151
+ var updateSchema3 = updateSchema2;
12080
12152
 
12081
12153
  // src/postgres/schema.ts
12082
12154
  var schema5 = (schemaName) => ({
@@ -12088,11 +12160,11 @@ var schema5 = (schemaName) => ({
12088
12160
  var exports_renderer2 = {};
12089
12161
  __export(exports_renderer2, {
12090
12162
  postgres: () => postgres,
12091
- make: () => make8,
12163
+ make: () => make7,
12092
12164
  TypeId: () => TypeId9
12093
12165
  });
12094
- var make8 = (options3 = {}) => make5("postgres", (plan) => renderPostgresPlan(plan, options3));
12095
- var postgres = make8();
12166
+ var make7 = (options3 = {}) => make4("postgres", (plan) => renderPostgresPlan(plan, options3));
12167
+ var postgres = make7();
12096
12168
  export {
12097
12169
  sequence,
12098
12170
  schema5 as schema,