@biref/scanner 0.0.2 → 0.1.0

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.ts CHANGED
@@ -1,23 +1,20 @@
1
1
  /**
2
- * Structural type for the database client the Postgres adapter consumes.
2
+ * Structural type for the database client the MySQL adapter consumes.
3
3
  *
4
- * Declares a single `query` method that takes a SQL string plus
5
- * optional parameters and returns an object with a `rows` array.
6
- * `pg.Client`, `pg.Pool`, and any structurally compatible library
7
- * satisfy the shape without the SDK importing `pg`.
4
+ * Declares a single `execute` method that takes a SQL string plus
5
+ * optional parameters and returns a tuple of `[rows, fields]`.
6
+ * `mysql2/promise` Connection, Pool, and any structurally compatible
7
+ * library satisfy the shape without the SDK importing `mysql2`.
8
8
  *
9
9
  * @example
10
10
  * ```ts
11
- * import pg from 'pg';
12
- * const client = new pg.Client({ ... });
13
- * await client.connect();
14
- * postgresAdapter.create(client);
11
+ * import mysql from 'mysql2/promise';
12
+ * const connection = await mysql.createConnection({ ... });
13
+ * mysqlAdapter.create(connection);
15
14
  * ```
16
15
  */
17
- interface PostgresClient {
18
- query<TRow = unknown>(text: string, params?: readonly unknown[]): Promise<{
19
- rows: TRow[];
20
- }>;
16
+ interface MySQLClient {
17
+ execute<TRow = unknown>(sql: string, params?: readonly unknown[]): Promise<[TRow[], unknown]>;
21
18
  }
22
19
 
23
20
  /**
@@ -325,46 +322,46 @@ interface IntrospectOptions {
325
322
  }
326
323
 
327
324
  /**
328
- * Postgres implementation of the `Introspector` port.
325
+ * MySQL implementation of the `Introspector` port.
329
326
  *
330
- * Runs six queries against `pg_catalog` in parallel (tables, columns,
331
- * primary keys, foreign keys, indexes, constraints) and hands the
332
- * result to `EntityAssembler`, which stitches them into a paradigm-
327
+ * Runs six queries against `information_schema` in parallel (tables,
328
+ * columns, primary keys, foreign keys, indexes, constraints) and hands
329
+ * the result to `EntityAssembler`, which stitches them into a paradigm-
333
330
  * neutral `DataModel`.
334
331
  *
335
332
  * What it returns for each entity:
336
333
  * - fields with normalized type categories
337
334
  * - identifier (primary key columns)
338
335
  * - relationships in BOTH directions
339
- * - constraints (unique, check, exclusion)
336
+ * - constraints (unique, check)
340
337
  * - indexes (excluding the primary key index)
341
338
  *
342
- * Accepts any client that satisfies `PostgresClient`, so it works with
343
- * `pg.Client`, `pg.Pool`, or any compatible library without the SDK
344
- * importing `pg`.
339
+ * Accepts any client that satisfies `MySQLClient`, so it works with
340
+ * `mysql2/promise` Connection, Pool, or any compatible library without
341
+ * the SDK importing `mysql2`.
345
342
  */
346
- declare class PostgresIntrospector implements Introspector {
343
+ declare class MySQLIntrospector implements Introspector {
347
344
  private readonly client;
348
- readonly name = "postgres";
345
+ readonly name = "mysql";
349
346
  readonly kind: "relational";
350
- constructor(client: PostgresClient);
347
+ constructor(client: MySQLClient);
351
348
  introspect(options?: IntrospectOptions): Promise<DataModel>;
352
349
  private resolveNamespaces;
353
- private static readonly ALL_NAMESPACES_SQL;
350
+ private currentDatabase;
351
+ private static readonly ALL_SCHEMAS_SQL;
354
352
  private static applyEntityFilters;
355
353
  }
356
354
 
357
355
  /**
358
- * Adapter metadata constants for Postgres.
356
+ * Adapter metadata constants for MySQL.
359
357
  *
360
358
  * Centralized so the rest of the adapter never references the literal
361
- * string 'postgres' or the URL schemes by hand. Anywhere those values
362
- * are needed, import the constant.
359
+ * string 'mysql' or the URL schemes by hand.
363
360
  */
364
- declare const POSTGRES_ADAPTER_NAME = "postgres";
365
- /** Literal type for the Postgres adapter name, used for discrimination. */
366
- type PostgresAdapterName = typeof POSTGRES_ADAPTER_NAME;
367
- declare const POSTGRES_URL_SCHEMES: readonly string[];
361
+ declare const MYSQL_ADAPTER_NAME = "mysql";
362
+ /** Literal type for the MySQL adapter name, used for discrimination. */
363
+ type MySQLAdapterName = typeof MYSQL_ADAPTER_NAME;
364
+ declare const MYSQL_URL_SCHEMES: readonly string[];
368
365
 
369
366
  /**
370
367
  * Output of `QueryEngine.build`.
@@ -447,22 +444,21 @@ interface QueryEngine<TCommand = unknown> {
447
444
  }
448
445
 
449
446
  /**
450
- * Postgres implementation of the `QueryEngine` port.
447
+ * MySQL implementation of the `QueryEngine` port.
451
448
  *
452
- * Produces parameterized SQL using `$1`, `$2`, ... placeholders and
453
- * double-quoted identifiers. Validates the spec against the
454
- * `DataModel` before emitting any SQL: unknown entities or fields
455
- * throw rather than yielding a broken query.
449
+ * Produces parameterized SQL using `?` placeholders and backtick-quoted
450
+ * identifiers. Validates the spec against the `DataModel` before
451
+ * emitting any SQL: unknown entities or fields throw rather than
452
+ * yielding a broken query.
456
453
  *
457
454
  * Scope:
458
455
  * - SELECT against a single entity
459
456
  * - WHERE filters (every `FilterOperator`)
460
457
  * - ORDER BY
461
458
  * - LIMIT and OFFSET
462
- * - No joins, group by, or aggregates yet
463
459
  */
464
- declare class PostgresQueryEngine implements QueryEngine<string> {
465
- readonly name = "postgres";
460
+ declare class MySQLQueryEngine implements QueryEngine<string> {
461
+ readonly name = "mysql";
466
462
  readonly kind: "relational";
467
463
  build(spec: QuerySpec, model: DataModel): BuiltQuery<string>;
468
464
  }
@@ -531,26 +527,27 @@ declare class DefaultRecordParser implements RecordParser {
531
527
  }
532
528
 
533
529
  /**
534
- * Postgres-aware `RecordParser`.
530
+ * MySQL-aware `RecordParser`.
535
531
  *
536
- * Extends `DefaultRecordParser` with behavior that matches how the
537
- * `pg` driver returns Postgres values:
532
+ * Extends `DefaultRecordParser` with behavior that matches how `mysql2`
533
+ * returns MySQL values:
538
534
  *
539
- * - `int8` / `bigint` columns are returned by `pg` as strings by
540
- * default (to preserve precision). This parser converts them to
541
- * JS `bigint` so downstream code does not silently lose digits.
535
+ * - `bigint` columns are returned by `mysql2` as strings by default
536
+ * (unless `supportBigNumbers` + `bigNumberStrings` are configured).
537
+ * This parser converts them to JS `bigint`.
542
538
  *
543
- * - `json` / `jsonb` columns are usually already parsed by `pg`,
544
- * but when they arrive as strings (for example from drivers that
545
- * do not auto-parse) this parser parses them.
539
+ * - `tinyint(1)` columns (category `boolean`) may arrive as `0`/`1`
540
+ * numbers. This parser normalizes them to JS `boolean`.
546
541
  *
547
- * - All other categories fall through to `DefaultRecordParser`.
542
+ * - `json` columns may arrive as strings depending on driver config.
543
+ * This parser parses them when needed.
548
544
  *
549
- * Use this parser when consuming rows that come from a `pg.Client` or
550
- * `pg.Pool`. For adapters that handle these concerns themselves, the
551
- * default parser is still appropriate.
545
+ * - `decimal` columns are returned as strings by `mysql2` to
546
+ * preserve precision. Passed through as-is.
547
+ *
548
+ * - All other categories fall through to `DefaultRecordParser`.
552
549
  */
553
- declare class PostgresRecordParser extends DefaultRecordParser {
550
+ declare class MySQLRecordParser extends DefaultRecordParser {
554
551
  protected coerce(field: Field, raw: unknown): ParsedValue;
555
552
  }
556
553
 
@@ -582,7 +579,7 @@ interface RawQueryRunner {
582
579
  * adapters with any name; the type accepts arbitrary strings in
583
580
  * addition to the known ones (see `AdapterName`).
584
581
  */
585
- type KnownAdapterName = 'postgres';
582
+ type KnownAdapterName = 'postgres' | 'mysql';
586
583
  /**
587
584
  * Adapter name type.
588
585
  *
@@ -684,6 +681,141 @@ interface AdapterFactory<TClient = unknown, TName extends AdapterName = AdapterN
684
681
  create(client: TClient): Adapter<TName>;
685
682
  }
686
683
 
684
+ /**
685
+ * MySQL adapter factory.
686
+ *
687
+ * Implements `AdapterFactory<MySQLClient, MySQLAdapterName>`.
688
+ * `create` builds an `Adapter` bound to the user-provided client.
689
+ *
690
+ * @example
691
+ * ```ts
692
+ * import mysql from 'mysql2/promise';
693
+ * import { Biref, mysqlAdapter } from '@biref/scanner';
694
+ *
695
+ * const connection = await mysql.createConnection({ ... });
696
+ *
697
+ * const biref = Biref.builder()
698
+ * .withAdapter(mysqlAdapter.create(connection))
699
+ * .build();
700
+ *
701
+ * const model = await biref.scan();
702
+ * ```
703
+ */
704
+ declare const mysqlAdapter: AdapterFactory<MySQLClient, MySQLAdapterName>;
705
+ /**
706
+ * Type guard: narrows a generic `Adapter` to an `Adapter<MySQLAdapterName>`.
707
+ */
708
+ declare function isMySQLAdapter(adapter: Adapter): adapter is Adapter<MySQLAdapterName>;
709
+
710
+ /**
711
+ * Structural type for the database client the Postgres adapter consumes.
712
+ *
713
+ * Declares a single `query` method that takes a SQL string plus
714
+ * optional parameters and returns an object with a `rows` array.
715
+ * `pg.Client`, `pg.Pool`, and any structurally compatible library
716
+ * satisfy the shape without the SDK importing `pg`.
717
+ *
718
+ * @example
719
+ * ```ts
720
+ * import pg from 'pg';
721
+ * const client = new pg.Client({ ... });
722
+ * await client.connect();
723
+ * postgresAdapter.create(client);
724
+ * ```
725
+ */
726
+ interface PostgresClient {
727
+ query<TRow = unknown>(text: string, params?: readonly unknown[]): Promise<{
728
+ rows: TRow[];
729
+ }>;
730
+ }
731
+
732
+ /**
733
+ * Postgres implementation of the `Introspector` port.
734
+ *
735
+ * Runs six queries against `pg_catalog` in parallel (tables, columns,
736
+ * primary keys, foreign keys, indexes, constraints) and hands the
737
+ * result to `EntityAssembler`, which stitches them into a paradigm-
738
+ * neutral `DataModel`.
739
+ *
740
+ * What it returns for each entity:
741
+ * - fields with normalized type categories
742
+ * - identifier (primary key columns)
743
+ * - relationships in BOTH directions
744
+ * - constraints (unique, check, exclusion)
745
+ * - indexes (excluding the primary key index)
746
+ *
747
+ * Accepts any client that satisfies `PostgresClient`, so it works with
748
+ * `pg.Client`, `pg.Pool`, or any compatible library without the SDK
749
+ * importing `pg`.
750
+ */
751
+ declare class PostgresIntrospector implements Introspector {
752
+ private readonly client;
753
+ readonly name = "postgres";
754
+ readonly kind: "relational";
755
+ constructor(client: PostgresClient);
756
+ introspect(options?: IntrospectOptions): Promise<DataModel>;
757
+ private resolveNamespaces;
758
+ private static readonly ALL_NAMESPACES_SQL;
759
+ private static applyEntityFilters;
760
+ }
761
+
762
+ /**
763
+ * Adapter metadata constants for Postgres.
764
+ *
765
+ * Centralized so the rest of the adapter never references the literal
766
+ * string 'postgres' or the URL schemes by hand. Anywhere those values
767
+ * are needed, import the constant.
768
+ */
769
+ declare const POSTGRES_ADAPTER_NAME = "postgres";
770
+ /** Literal type for the Postgres adapter name, used for discrimination. */
771
+ type PostgresAdapterName = typeof POSTGRES_ADAPTER_NAME;
772
+ declare const POSTGRES_URL_SCHEMES: readonly string[];
773
+
774
+ /**
775
+ * Postgres implementation of the `QueryEngine` port.
776
+ *
777
+ * Produces parameterized SQL using `$1`, `$2`, ... placeholders and
778
+ * double-quoted identifiers. Validates the spec against the
779
+ * `DataModel` before emitting any SQL: unknown entities or fields
780
+ * throw rather than yielding a broken query.
781
+ *
782
+ * Scope:
783
+ * - SELECT against a single entity
784
+ * - WHERE filters (every `FilterOperator`)
785
+ * - ORDER BY
786
+ * - LIMIT and OFFSET
787
+ * - No joins, group by, or aggregates yet
788
+ */
789
+ declare class PostgresQueryEngine implements QueryEngine<string> {
790
+ readonly name = "postgres";
791
+ readonly kind: "relational";
792
+ build(spec: QuerySpec, model: DataModel): BuiltQuery<string>;
793
+ }
794
+
795
+ /**
796
+ * Postgres-aware `RecordParser`.
797
+ *
798
+ * Extends `DefaultRecordParser` with behavior that matches how the
799
+ * `pg` driver returns Postgres values:
800
+ *
801
+ * - `int8` / `bigint` columns are returned by `pg` as strings by
802
+ * default (to preserve precision). This parser converts them to
803
+ * JS `bigint` so downstream code does not silently lose digits.
804
+ *
805
+ * - `json` / `jsonb` columns are usually already parsed by `pg`,
806
+ * but when they arrive as strings (for example from drivers that
807
+ * do not auto-parse) this parser parses them.
808
+ *
809
+ * - All other categories fall through to `DefaultRecordParser`.
810
+ *
811
+ * Use this parser when consuming rows that come from a `pg.Client` or
812
+ * `pg.Pool`. For adapters that handle these concerns themselves, the
813
+ * default parser is still appropriate.
814
+ */
815
+ declare class PostgresRecordParser extends DefaultRecordParser {
816
+ protected coerce(field: Field, raw: unknown): ParsedValue;
817
+ }
818
+
687
819
  /**
688
820
  * Postgres adapter factory.
689
821
  *
@@ -1078,9 +1210,12 @@ type DefaultSelect<S extends BirefSchemaShape, Ns extends keyof S, E extends key
1078
1210
  };
1079
1211
  /**
1080
1212
  * Narrowed selection from a `select('id', 'email')` call.
1213
+ *
1214
+ * The `Keys` tuple may include `'*'` from the overload union; it is
1215
+ * excluded before projecting so only real field names appear.
1081
1216
  */
1082
- type PickSelect<S extends BirefSchemaShape, Ns extends keyof S, E extends keyof S[Ns], Keys extends readonly (keyof FieldsOf<S, Ns, E>)[]> = {
1083
- readonly [K in Keys[number]]: TypeOf<S, Ns, E, K>;
1217
+ type PickSelect<S extends BirefSchemaShape, Ns extends keyof S, E extends keyof S[Ns], Keys extends readonly (keyof FieldsOf<S, Ns, E> | '*')[]> = {
1218
+ readonly [K in Exclude<Keys[number], '*'> as K extends keyof FieldsOf<S, Ns, E> ? K : never]: TypeOf<S, Ns, E, K>;
1084
1219
  };
1085
1220
  /**
1086
1221
  * An accumulated include map on a chain builder. Each key is a
@@ -1152,20 +1287,27 @@ type NullaryOps = 'is-null' | 'is-not-null';
1152
1287
  */
1153
1288
  interface TypedChain<S extends BirefSchemaShape, Ns extends keyof S, E extends keyof S[Ns], Sel extends Selection = DefaultSelect<S, Ns, E>, Inc extends IncludeMap = Record<string, never>> {
1154
1289
  /**
1155
- * Narrow the projection to a specific set of field names. Each name
1156
- * must exist on the entity; unknowns throw at chain time.
1290
+ * Zero-argument shorthand for the wildcard.
1157
1291
  */
1158
- select<const K extends readonly (keyof FieldsOf<S, Ns, E>)[]>(...fields: K): TypedChain<S, Ns, E, PickSelect<S, Ns, E, K>, Inc>;
1292
+ select(): TypedChain<S, Ns, E, DefaultSelect<S, Ns, E>, Inc>;
1159
1293
  /**
1160
1294
  * Explicit wildcard: every field of the entity, same shape as
1161
1295
  * calling `findMany()` without `select` at all. Kept as a
1162
1296
  * discoverable sentinel so `.select('*')` reads clearly in code.
1297
+ *
1298
+ * Listed before the generic overload so TypeScript's overload
1299
+ * resolver picks it for the literal `'*'` argument.
1163
1300
  */
1164
1301
  select(wildcard: '*'): TypedChain<S, Ns, E, DefaultSelect<S, Ns, E>, Inc>;
1165
1302
  /**
1166
- * Zero-argument shorthand for the wildcard.
1303
+ * Narrow the projection to a specific set of field names. Each name
1304
+ * must exist on the entity; unknowns throw at chain time.
1305
+ *
1306
+ * The union includes `'*'` so autocomplete surfaces the wildcard
1307
+ * alongside real field names, but the overload above captures the
1308
+ * `'*'` literal before this one fires.
1167
1309
  */
1168
- select(): TypedChain<S, Ns, E, DefaultSelect<S, Ns, E>, Inc>;
1310
+ select<const K extends readonly (keyof FieldsOf<S, Ns, E> | '*')[]>(...fields: K): TypedChain<S, Ns, E, PickSelect<S, Ns, E, K>, Inc>;
1169
1311
  where<F extends keyof FieldsOf<S, Ns, E>, Op extends Exclude<OpsFor<CategoryOfField<S, Ns, E, F>>, NullaryOps>>(field: F, operator: Op, value: ValueFor<Op, TypeOf<S, Ns, E, F>>): TypedChain<S, Ns, E, Sel, Inc>;
1170
1312
  where<F extends keyof FieldsOf<S, Ns, E>>(field: F, operator: Extract<OpsFor<CategoryOfField<S, Ns, E, F>>, NullaryOps>): TypedChain<S, Ns, E, Sel, Inc>;
1171
1313
  orderBy<F extends keyof FieldsOf<S, Ns, E>>(field: F, direction?: 'asc' | 'desc'): TypedChain<S, Ns, E, Sel, Inc>;
@@ -1504,4 +1646,4 @@ type ApplySchemaOverrides<Raw extends BirefSchemaShape, Overrides extends BirefO
1504
1646
  };
1505
1647
  type ReplaceFieldTs<Field, NewTs> = Field extends SchemaFieldDescriptor<infer _OldTs, infer Nullable, infer Identifier, infer Category> ? SchemaFieldDescriptor<NewTs, Nullable, Identifier, Category> : Field;
1506
1648
 
1507
- export { type Adapter, type AdapterFactory, type AdapterName, AdapterRegistry, type ApplySchemaOverrides, Biref, BirefBuilder, type BirefOverridesShape, type BirefSchemaShape, type BuiltQuery, type BuiltQueryMetadata, type CardinalityOf, type CategoryOf, type CategoryOfField, ChainBuilder, type ChainBuilderContext, type Constraint, type ConstraintKind, CsvFormatter, type CsvFormatterOptions, DataModel, DefaultRecordParser, type DefaultSelect, type EngineKind, type Entity, type EntityRef, type Field, type FieldType, type FieldTypeCategory, type FieldsOf, type Filter, type FilterOperator, type Formatter, type HydratedRow, type IncludeMap, type Index, type IndexKind, type IntrospectOptions, type Introspector, JsonFormatter, type JsonFormatterOptions, type KnownAdapterName, type NullaryOps, type OpsFor, type OrderBy, POSTGRES_ADAPTER_NAME, POSTGRES_URL_SCHEMES, type ParsedRecord, type ParsedValue, type PickSelect, type PostgresAdapterName, type PostgresClient, PostgresIntrospector, PostgresQueryEngine, PostgresRecordParser, type QueryEngine, type QueryInclude, type QueryPlan, QueryPlanExecutor, type QuerySpec, RawFormatter, type RawQueryRunner, type RecordParser, type Reference, type ReferentialAction, type RelationsOf, type RelationsOfEntity, type Relationship, type RelationshipDirection, Scanner, type SchemaEntityDescriptor, type SchemaFieldDescriptor, type SchemaFile, type SchemaNamespaceDescriptor, type SchemaRelationDescriptor, type Selection, type SerializeOptions, type Split, type TargetE, type TargetNs, type TypeOf, type TypedChain, type TypedQueryRoot, type UntypedQueryRoot, type ValueFor, type WrapForCardinality, generateSchema, generateSchemaFiles, isPostgresAdapter, overridesScaffold, postgresAdapter, tsTypeFor };
1649
+ export { type Adapter, type AdapterFactory, type AdapterName, AdapterRegistry, type ApplySchemaOverrides, Biref, BirefBuilder, type BirefOverridesShape, type BirefSchemaShape, type BuiltQuery, type BuiltQueryMetadata, type CardinalityOf, type CategoryOf, type CategoryOfField, ChainBuilder, type ChainBuilderContext, type Constraint, type ConstraintKind, CsvFormatter, type CsvFormatterOptions, DataModel, DefaultRecordParser, type DefaultSelect, type EngineKind, type Entity, type EntityRef, type Field, type FieldType, type FieldTypeCategory, type FieldsOf, type Filter, type FilterOperator, type Formatter, type HydratedRow, type IncludeMap, type Index, type IndexKind, type IntrospectOptions, type Introspector, JsonFormatter, type JsonFormatterOptions, type KnownAdapterName, MYSQL_ADAPTER_NAME, MYSQL_URL_SCHEMES, type MySQLAdapterName, type MySQLClient, MySQLIntrospector, MySQLQueryEngine, MySQLRecordParser, type NullaryOps, type OpsFor, type OrderBy, POSTGRES_ADAPTER_NAME, POSTGRES_URL_SCHEMES, type ParsedRecord, type ParsedValue, type PickSelect, type PostgresAdapterName, type PostgresClient, PostgresIntrospector, PostgresQueryEngine, PostgresRecordParser, type QueryEngine, type QueryInclude, type QueryPlan, QueryPlanExecutor, type QuerySpec, RawFormatter, type RawQueryRunner, type RecordParser, type Reference, type ReferentialAction, type RelationsOf, type RelationsOfEntity, type Relationship, type RelationshipDirection, Scanner, type SchemaEntityDescriptor, type SchemaFieldDescriptor, type SchemaFile, type SchemaNamespaceDescriptor, type SchemaRelationDescriptor, type Selection, type SerializeOptions, type Split, type TargetE, type TargetNs, type TypeOf, type TypedChain, type TypedQueryRoot, type UntypedQueryRoot, type ValueFor, type WrapForCardinality, generateSchema, generateSchemaFiles, isMySQLAdapter, isPostgresAdapter, mysqlAdapter, overridesScaffold, postgresAdapter, tsTypeFor };