@beignet/provider-db-drizzle 0.0.36 → 0.0.37
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/CHANGELOG.md +6 -0
- package/README.md +81 -0
- package/dist/mysql/index.d.ts +25 -7
- package/dist/mysql/index.d.ts.map +1 -1
- package/dist/mysql/index.js +37 -50
- package/dist/mysql/index.js.map +1 -1
- package/dist/postgres/index.d.ts +31 -5
- package/dist/postgres/index.d.ts.map +1 -1
- package/dist/postgres/index.js +40 -51
- package/dist/postgres/index.js.map +1 -1
- package/dist/sqlite/index.d.ts +20 -2
- package/dist/sqlite/index.d.ts.map +1 -1
- package/dist/sqlite/index.js +37 -50
- package/dist/sqlite/index.js.map +1 -1
- package/package.json +3 -2
- package/skills/database-provider/SKILL.md +29 -1
- package/src/mysql/index.ts +103 -43
- package/src/postgres/index.ts +118 -42
- package/src/sqlite/index.ts +102 -36
package/src/postgres/index.ts
CHANGED
|
@@ -41,6 +41,7 @@ import {
|
|
|
41
41
|
type AnyProviderConfigSchema,
|
|
42
42
|
createProvider,
|
|
43
43
|
createProviderInstrumentation,
|
|
44
|
+
type ProviderInstrumentationTarget,
|
|
44
45
|
type ServiceProvider,
|
|
45
46
|
} from "@beignet/core/providers";
|
|
46
47
|
import { type ExtractTablesWithRelations, type SQL, sql } from "drizzle-orm";
|
|
@@ -605,12 +606,24 @@ export type DrizzlePostgresProvider<
|
|
|
605
606
|
Record<PortName, DbPort<TSchema>>
|
|
606
607
|
>;
|
|
607
608
|
|
|
609
|
+
/**
|
|
610
|
+
* Drizzle Postgres provider backed by an app-supplied pool.
|
|
611
|
+
*/
|
|
612
|
+
export type InjectedDrizzlePostgresProvider<
|
|
613
|
+
TSchema extends Record<string, unknown>,
|
|
614
|
+
PortName extends string = "db",
|
|
615
|
+
> = ServiceProvider<
|
|
616
|
+
unknown,
|
|
617
|
+
AnyProviderConfigSchema<void>,
|
|
618
|
+
Record<PortName, DbPort<TSchema>>
|
|
619
|
+
>;
|
|
620
|
+
|
|
608
621
|
/**
|
|
609
622
|
* Options for creating a Drizzle Postgres provider.
|
|
610
623
|
*
|
|
611
624
|
* @template TSchema - The Drizzle schema type
|
|
612
625
|
*/
|
|
613
|
-
|
|
626
|
+
interface DrizzlePostgresProviderBaseOptions<
|
|
614
627
|
TSchema extends Record<string, unknown>,
|
|
615
628
|
PortName extends string = "db",
|
|
616
629
|
> {
|
|
@@ -627,14 +640,34 @@ export interface DrizzlePostgresProviderOptions<
|
|
|
627
640
|
* with its own env prefix instead.
|
|
628
641
|
*/
|
|
629
642
|
portName?: PortName;
|
|
630
|
-
|
|
631
|
-
/**
|
|
632
|
-
* Optional node-postgres pool configuration merged into the pool created
|
|
633
|
-
* from `POSTGRES_DB_URL`, e.g. `max`, `ssl`, or timeouts.
|
|
634
|
-
*/
|
|
635
|
-
pool?: Omit<PoolConfig, "connectionString">;
|
|
636
643
|
}
|
|
637
644
|
|
|
645
|
+
export type DrizzlePostgresProviderOptions<
|
|
646
|
+
TSchema extends Record<string, unknown>,
|
|
647
|
+
PortName extends string = "db",
|
|
648
|
+
> = DrizzlePostgresProviderBaseOptions<TSchema, PortName> &
|
|
649
|
+
(
|
|
650
|
+
| {
|
|
651
|
+
/**
|
|
652
|
+
* Optional node-postgres pool configuration merged into the pool
|
|
653
|
+
* created from `POSTGRES_DB_URL`.
|
|
654
|
+
*/
|
|
655
|
+
pool?: Omit<PoolConfig, "connectionString">;
|
|
656
|
+
client?: never;
|
|
657
|
+
closeOnStop?: never;
|
|
658
|
+
}
|
|
659
|
+
| {
|
|
660
|
+
/**
|
|
661
|
+
* App-supplied node-postgres pool. Injected pools do not require
|
|
662
|
+
* provider configuration and remain caller-owned by default.
|
|
663
|
+
*/
|
|
664
|
+
client: Pool;
|
|
665
|
+
pool?: never;
|
|
666
|
+
/** Close the injected pool when the provider stops. @default false */
|
|
667
|
+
closeOnStop?: boolean;
|
|
668
|
+
}
|
|
669
|
+
);
|
|
670
|
+
|
|
638
671
|
/**
|
|
639
672
|
* Create a Drizzle Postgres provider factory.
|
|
640
673
|
*
|
|
@@ -658,14 +691,90 @@ export interface DrizzlePostgresProviderOptions<
|
|
|
658
691
|
* export const drizzlePostgresProvider = createDrizzlePostgresProvider({ schema });
|
|
659
692
|
* ```
|
|
660
693
|
*/
|
|
694
|
+
export function createDrizzlePostgresProvider<
|
|
695
|
+
TSchema extends Record<string, unknown>,
|
|
696
|
+
PortName extends string = "db",
|
|
697
|
+
>(
|
|
698
|
+
options: DrizzlePostgresProviderBaseOptions<TSchema, PortName> & {
|
|
699
|
+
client: Pool;
|
|
700
|
+
pool?: never;
|
|
701
|
+
closeOnStop?: boolean;
|
|
702
|
+
},
|
|
703
|
+
): InjectedDrizzlePostgresProvider<TSchema, PortName>;
|
|
704
|
+
export function createDrizzlePostgresProvider<
|
|
705
|
+
TSchema extends Record<string, unknown>,
|
|
706
|
+
PortName extends string = "db",
|
|
707
|
+
>(
|
|
708
|
+
options: DrizzlePostgresProviderBaseOptions<TSchema, PortName> & {
|
|
709
|
+
pool?: Omit<PoolConfig, "connectionString">;
|
|
710
|
+
client?: never;
|
|
711
|
+
closeOnStop?: never;
|
|
712
|
+
},
|
|
713
|
+
): DrizzlePostgresProvider<TSchema, PortName>;
|
|
661
714
|
export function createDrizzlePostgresProvider<
|
|
662
715
|
TSchema extends Record<string, unknown>,
|
|
663
716
|
PortName extends string = "db",
|
|
664
717
|
>(
|
|
665
718
|
options: DrizzlePostgresProviderOptions<TSchema, PortName>,
|
|
666
|
-
):
|
|
719
|
+
):
|
|
720
|
+
| DrizzlePostgresProvider<TSchema, PortName>
|
|
721
|
+
| InjectedDrizzlePostgresProvider<TSchema, PortName> {
|
|
722
|
+
return createDrizzlePostgresProviderImplementation(options);
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
function createDrizzlePostgresProviderImplementation<
|
|
726
|
+
TSchema extends Record<string, unknown>,
|
|
727
|
+
PortName extends string = "db",
|
|
728
|
+
>(options: DrizzlePostgresProviderOptions<TSchema, PortName>) {
|
|
667
729
|
const { schema, portName = "db" } = options;
|
|
668
730
|
|
|
731
|
+
const setupPool = (ports: unknown, pool: Pool, closeOnStop: boolean) => {
|
|
732
|
+
const instrumentation = createProviderInstrumentation(
|
|
733
|
+
ports as ProviderInstrumentationTarget,
|
|
734
|
+
{
|
|
735
|
+
providerName: "drizzle-postgres",
|
|
736
|
+
watcher: "db",
|
|
737
|
+
},
|
|
738
|
+
);
|
|
739
|
+
const logger = createDbQueryLogger(instrumentation, portName);
|
|
740
|
+
const db: NodePgDatabase<TSchema> = logger
|
|
741
|
+
? drizzle(pool, { schema, logger })
|
|
742
|
+
: drizzle(pool, { schema });
|
|
743
|
+
const dbPort: DbPort<TSchema> = {
|
|
744
|
+
drizzle: db,
|
|
745
|
+
pool,
|
|
746
|
+
checkHealth: () => checkPostgresHealth(pool),
|
|
747
|
+
};
|
|
748
|
+
|
|
749
|
+
return {
|
|
750
|
+
ports: { [portName]: dbPort } as Record<PortName, DbPort<TSchema>>,
|
|
751
|
+
async stop() {
|
|
752
|
+
if (!closeOnStop) return;
|
|
753
|
+
try {
|
|
754
|
+
await pool.end();
|
|
755
|
+
} catch (error) {
|
|
756
|
+
console.error(
|
|
757
|
+
"[drizzlePostgresProvider] Error closing database connection:",
|
|
758
|
+
error,
|
|
759
|
+
);
|
|
760
|
+
}
|
|
761
|
+
},
|
|
762
|
+
};
|
|
763
|
+
};
|
|
764
|
+
|
|
765
|
+
if (options.client) {
|
|
766
|
+
return createProvider<
|
|
767
|
+
unknown,
|
|
768
|
+
AnyProviderConfigSchema<void>,
|
|
769
|
+
Record<PortName, DbPort<TSchema>>
|
|
770
|
+
>({
|
|
771
|
+
name: "drizzle-postgres",
|
|
772
|
+
setup({ ports }) {
|
|
773
|
+
return setupPool(ports, options.client, options.closeOnStop ?? false);
|
|
774
|
+
},
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
|
|
669
778
|
return createProvider({
|
|
670
779
|
name: "drizzle-postgres",
|
|
671
780
|
|
|
@@ -687,40 +796,7 @@ export function createDrizzlePostgresProvider<
|
|
|
687
796
|
...options.pool,
|
|
688
797
|
});
|
|
689
798
|
|
|
690
|
-
|
|
691
|
-
providerName: "drizzle-postgres",
|
|
692
|
-
watcher: "db",
|
|
693
|
-
});
|
|
694
|
-
|
|
695
|
-
const logger = createDbQueryLogger(instrumentation, portName);
|
|
696
|
-
|
|
697
|
-
// 2. Create typed Drizzle instance
|
|
698
|
-
const db: NodePgDatabase<TSchema> = logger
|
|
699
|
-
? drizzle(pool, { schema, logger })
|
|
700
|
-
: drizzle(pool, { schema });
|
|
701
|
-
|
|
702
|
-
// 3. Build a typed DbPort
|
|
703
|
-
const dbPort: DbPort<TSchema> = {
|
|
704
|
-
drizzle: db,
|
|
705
|
-
pool,
|
|
706
|
-
checkHealth: () => checkPostgresHealth(pool),
|
|
707
|
-
};
|
|
708
|
-
|
|
709
|
-
return {
|
|
710
|
-
ports: { [portName]: dbPort } as Record<PortName, DbPort<TSchema>>,
|
|
711
|
-
async stop() {
|
|
712
|
-
// Gracefully close the connection pool
|
|
713
|
-
try {
|
|
714
|
-
await dbPort.pool.end();
|
|
715
|
-
} catch (error) {
|
|
716
|
-
// Log error but don't throw - we're shutting down anyway
|
|
717
|
-
console.error(
|
|
718
|
-
"[drizzlePostgresProvider] Error closing database connection:",
|
|
719
|
-
error,
|
|
720
|
-
);
|
|
721
|
-
}
|
|
722
|
-
},
|
|
723
|
-
};
|
|
799
|
+
return setupPool(ports, pool, true);
|
|
724
800
|
},
|
|
725
801
|
});
|
|
726
802
|
}
|
package/src/sqlite/index.ts
CHANGED
|
@@ -43,6 +43,7 @@ import {
|
|
|
43
43
|
type AnyProviderConfigSchema,
|
|
44
44
|
createProvider,
|
|
45
45
|
createProviderInstrumentation,
|
|
46
|
+
type ProviderInstrumentationTarget,
|
|
46
47
|
type ServiceProvider,
|
|
47
48
|
} from "@beignet/core/providers";
|
|
48
49
|
import { type Client, createClient } from "@libsql/client";
|
|
@@ -625,12 +626,22 @@ export type DrizzleSqliteProvider<
|
|
|
625
626
|
Record<PortName, DbPort<TSchema>>
|
|
626
627
|
>;
|
|
627
628
|
|
|
629
|
+
/** Drizzle SQLite provider backed by an app-supplied libSQL client. */
|
|
630
|
+
export type InjectedDrizzleSqliteProvider<
|
|
631
|
+
TSchema extends Record<string, unknown>,
|
|
632
|
+
PortName extends string = "db",
|
|
633
|
+
> = ServiceProvider<
|
|
634
|
+
unknown,
|
|
635
|
+
AnyProviderConfigSchema<void>,
|
|
636
|
+
Record<PortName, DbPort<TSchema>>
|
|
637
|
+
>;
|
|
638
|
+
|
|
628
639
|
/**
|
|
629
640
|
* Options for creating a Drizzle SQLite provider.
|
|
630
641
|
*
|
|
631
642
|
* @template TSchema - The Drizzle schema type
|
|
632
643
|
*/
|
|
633
|
-
|
|
644
|
+
interface DrizzleSqliteProviderBaseOptions<
|
|
634
645
|
TSchema extends Record<string, unknown>,
|
|
635
646
|
PortName extends string = "db",
|
|
636
647
|
> {
|
|
@@ -649,6 +660,23 @@ export interface DrizzleSqliteProviderOptions<
|
|
|
649
660
|
portName?: PortName;
|
|
650
661
|
}
|
|
651
662
|
|
|
663
|
+
export type DrizzleSqliteProviderOptions<
|
|
664
|
+
TSchema extends Record<string, unknown>,
|
|
665
|
+
PortName extends string = "db",
|
|
666
|
+
> = DrizzleSqliteProviderBaseOptions<TSchema, PortName> &
|
|
667
|
+
(
|
|
668
|
+
| {
|
|
669
|
+
client?: never;
|
|
670
|
+
closeOnStop?: never;
|
|
671
|
+
}
|
|
672
|
+
| {
|
|
673
|
+
/** App-supplied libSQL client, caller-owned by default. */
|
|
674
|
+
client: Client;
|
|
675
|
+
/** Close the injected client when the provider stops. @default false */
|
|
676
|
+
closeOnStop?: boolean;
|
|
677
|
+
}
|
|
678
|
+
);
|
|
679
|
+
|
|
652
680
|
/**
|
|
653
681
|
* Create a Drizzle SQLite provider factory.
|
|
654
682
|
*
|
|
@@ -671,14 +699,85 @@ export interface DrizzleSqliteProviderOptions<
|
|
|
671
699
|
* export const drizzleSqliteProvider = createDrizzleSqliteProvider({ schema });
|
|
672
700
|
* ```
|
|
673
701
|
*/
|
|
702
|
+
export function createDrizzleSqliteProvider<
|
|
703
|
+
TSchema extends Record<string, unknown>,
|
|
704
|
+
PortName extends string = "db",
|
|
705
|
+
>(
|
|
706
|
+
options: DrizzleSqliteProviderBaseOptions<TSchema, PortName> & {
|
|
707
|
+
client: Client;
|
|
708
|
+
closeOnStop?: boolean;
|
|
709
|
+
},
|
|
710
|
+
): InjectedDrizzleSqliteProvider<TSchema, PortName>;
|
|
711
|
+
export function createDrizzleSqliteProvider<
|
|
712
|
+
TSchema extends Record<string, unknown>,
|
|
713
|
+
PortName extends string = "db",
|
|
714
|
+
>(
|
|
715
|
+
options: DrizzleSqliteProviderBaseOptions<TSchema, PortName> & {
|
|
716
|
+
client?: never;
|
|
717
|
+
closeOnStop?: never;
|
|
718
|
+
},
|
|
719
|
+
): DrizzleSqliteProvider<TSchema, PortName>;
|
|
674
720
|
export function createDrizzleSqliteProvider<
|
|
675
721
|
TSchema extends Record<string, unknown>,
|
|
676
722
|
PortName extends string = "db",
|
|
677
723
|
>(
|
|
678
724
|
options: DrizzleSqliteProviderOptions<TSchema, PortName>,
|
|
679
|
-
):
|
|
725
|
+
):
|
|
726
|
+
| DrizzleSqliteProvider<TSchema, PortName>
|
|
727
|
+
| InjectedDrizzleSqliteProvider<TSchema, PortName> {
|
|
680
728
|
const { schema, portName = "db" } = options;
|
|
681
729
|
|
|
730
|
+
const setupClient = (
|
|
731
|
+
ports: unknown,
|
|
732
|
+
client: Client,
|
|
733
|
+
closeOnStop: boolean,
|
|
734
|
+
) => {
|
|
735
|
+
const instrumentation = createProviderInstrumentation(
|
|
736
|
+
ports as ProviderInstrumentationTarget,
|
|
737
|
+
{
|
|
738
|
+
providerName: "drizzle-sqlite",
|
|
739
|
+
watcher: "db",
|
|
740
|
+
},
|
|
741
|
+
);
|
|
742
|
+
const logger = createDbQueryLogger(instrumentation, portName);
|
|
743
|
+
const db: LibSQLDatabase<TSchema> = logger
|
|
744
|
+
? drizzle(client, { schema, logger })
|
|
745
|
+
: drizzle(client, { schema });
|
|
746
|
+
const dbPort: DbPort<TSchema> = {
|
|
747
|
+
drizzle: db,
|
|
748
|
+
client,
|
|
749
|
+
checkHealth: () => checkSqliteHealth(client),
|
|
750
|
+
};
|
|
751
|
+
|
|
752
|
+
return {
|
|
753
|
+
ports: { [portName]: dbPort } as Record<PortName, DbPort<TSchema>>,
|
|
754
|
+
async stop() {
|
|
755
|
+
if (!closeOnStop) return;
|
|
756
|
+
try {
|
|
757
|
+
await client.close();
|
|
758
|
+
} catch (error) {
|
|
759
|
+
console.error(
|
|
760
|
+
"[drizzleSqliteProvider] Error closing database connection:",
|
|
761
|
+
error,
|
|
762
|
+
);
|
|
763
|
+
}
|
|
764
|
+
},
|
|
765
|
+
};
|
|
766
|
+
};
|
|
767
|
+
|
|
768
|
+
if (options.client) {
|
|
769
|
+
return createProvider<
|
|
770
|
+
unknown,
|
|
771
|
+
AnyProviderConfigSchema<void>,
|
|
772
|
+
Record<PortName, DbPort<TSchema>>
|
|
773
|
+
>({
|
|
774
|
+
name: "drizzle-sqlite",
|
|
775
|
+
setup({ ports }) {
|
|
776
|
+
return setupClient(ports, options.client, options.closeOnStop ?? false);
|
|
777
|
+
},
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
|
|
682
781
|
return createProvider({
|
|
683
782
|
name: "drizzle-sqlite",
|
|
684
783
|
|
|
@@ -700,40 +799,7 @@ export function createDrizzleSqliteProvider<
|
|
|
700
799
|
authToken: config.DB_AUTH_TOKEN,
|
|
701
800
|
});
|
|
702
801
|
|
|
703
|
-
|
|
704
|
-
providerName: "drizzle-sqlite",
|
|
705
|
-
watcher: "db",
|
|
706
|
-
});
|
|
707
|
-
|
|
708
|
-
const logger = createDbQueryLogger(instrumentation, portName);
|
|
709
|
-
|
|
710
|
-
// 2. Create typed Drizzle instance
|
|
711
|
-
const db: LibSQLDatabase<TSchema> = logger
|
|
712
|
-
? drizzle(client, { schema, logger })
|
|
713
|
-
: drizzle(client, { schema });
|
|
714
|
-
|
|
715
|
-
// 3. Build a typed DbPort
|
|
716
|
-
const dbPort: DbPort<TSchema> = {
|
|
717
|
-
drizzle: db,
|
|
718
|
-
client,
|
|
719
|
-
checkHealth: () => checkSqliteHealth(client),
|
|
720
|
-
};
|
|
721
|
-
|
|
722
|
-
return {
|
|
723
|
-
ports: { [portName]: dbPort } as Record<PortName, DbPort<TSchema>>,
|
|
724
|
-
async stop() {
|
|
725
|
-
// Gracefully close the database connection
|
|
726
|
-
try {
|
|
727
|
-
await dbPort.client.close();
|
|
728
|
-
} catch (error) {
|
|
729
|
-
// Log error but don't throw - we're shutting down anyway
|
|
730
|
-
console.error(
|
|
731
|
-
"[drizzleSqliteProvider] Error closing database connection:",
|
|
732
|
-
error,
|
|
733
|
-
);
|
|
734
|
-
}
|
|
735
|
-
},
|
|
736
|
-
};
|
|
802
|
+
return setupClient(ports, client, true);
|
|
737
803
|
},
|
|
738
804
|
});
|
|
739
805
|
}
|