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
@@ -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;
381
+ const schema = fieldSchemaForVariant(variant, column, tableName, key, primaryKeySet);
382
+ if (schema !== undefined) {
383
+ structFields[key] = schema;
387
384
  }
388
- if (update !== undefined) {
389
- config.update = update;
390
- } else {
391
- delete config.update;
392
- }
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,