nitro-drizzle 0.3.0-beta.2 → 0.3.0-beta.4
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/README.md +24 -3
- package/dist/config/defineConfig.d.mts +4 -4
- package/dist/config/defineConfig.mjs +4 -4
- package/dist/config/defineConfig.mjs.map +1 -1
- package/dist/context/internal/async.mjs +8 -0
- package/dist/context/internal/async.mjs.map +1 -1
- package/dist/context/internal/codegen.mjs +28 -0
- package/dist/context/internal/codegen.mjs.map +1 -0
- package/dist/context/internal/config.mjs +9 -4
- package/dist/context/internal/config.mjs.map +1 -1
- package/dist/context/internal/format.mjs +6 -0
- package/dist/context/internal/format.mjs.map +1 -1
- package/dist/context/internal/fs.mjs +7 -0
- package/dist/context/internal/fs.mjs.map +1 -1
- package/dist/context/internal/logger.mjs +6 -0
- package/dist/context/internal/logger.mjs.map +1 -1
- package/dist/context/internal/templates.mjs +84 -31
- package/dist/context/internal/templates.mjs.map +1 -1
- package/dist/context/internal/virtual.mjs +51 -26
- package/dist/context/internal/virtual.mjs.map +1 -1
- package/dist/context.d.mts +22 -15
- package/dist/context.mjs +58 -15
- package/dist/context.mjs.map +1 -1
- package/dist/dialects/sqlite.d.mts +2 -2
- package/dist/dialects/sqlite.mjs +3 -3
- package/dist/dialects/sqlite.mjs.map +1 -1
- package/dist/drivers/{internal/d1-dialect.mjs → d1/dialect.mjs} +4 -3
- package/dist/drivers/d1/dialect.mjs.map +1 -0
- package/dist/drivers/d1-http.d.mts +1 -1
- package/dist/drivers/d1-http.mjs +3 -3
- package/dist/drivers/d1-http.mjs.map +1 -1
- package/dist/drivers/d1.d.mts +2 -1
- package/dist/drivers/d1.mjs +3 -3
- package/dist/drivers/d1.mjs.map +1 -1
- package/dist/drivers/db0.d.mts +2 -0
- package/dist/drivers/db0.mjs +3 -2
- package/dist/drivers/db0.mjs.map +1 -1
- package/dist/drivers/index.d.mts +14 -7
- package/dist/drivers/index.mjs +3 -3
- package/dist/drivers/index.mjs.map +1 -1
- package/dist/drivers/mysql.mjs +2 -2
- package/dist/drivers/mysql.mjs.map +1 -1
- package/dist/drivers/pglite.d.mts +1 -0
- package/dist/drivers/pglite.mjs +3 -2
- package/dist/drivers/pglite.mjs.map +1 -1
- package/dist/drivers/postgresql.d.mts +2 -1
- package/dist/drivers/postgresql.mjs +2 -2
- package/dist/drivers/postgresql.mjs.map +1 -1
- package/dist/drivers/sqlite.mjs +2 -2
- package/dist/drivers/sqlite.mjs.map +1 -1
- package/dist/middleware/context.d.mts +8 -0
- package/dist/middleware/context.mjs +4 -0
- package/dist/middleware/context.mjs.map +1 -1
- package/dist/migrations/migrate.mjs +3 -2
- package/dist/migrations/migrate.mjs.map +1 -1
- package/dist/migrations/task.d.mts +4 -0
- package/dist/migrations/task.mjs +4 -0
- package/dist/migrations/task.mjs.map +1 -1
- package/dist/migrations/useMigrations.d.mts +2 -2
- package/dist/migrations/useMigrations.mjs +3 -1
- package/dist/migrations/useMigrations.mjs.map +1 -1
- package/dist/module/internal/fs.mjs +5 -0
- package/dist/module/internal/fs.mjs.map +1 -1
- package/dist/module/internal/path.mjs +6 -0
- package/dist/module/internal/path.mjs.map +1 -1
- package/dist/module/internal/reload.mjs +6 -0
- package/dist/module/internal/reload.mjs.map +1 -1
- package/dist/module/internal/resolver.mjs +6 -0
- package/dist/module/internal/resolver.mjs.map +1 -1
- package/dist/module/internal/rollup.mjs +5 -0
- package/dist/module/internal/rollup.mjs.map +1 -1
- package/dist/module/internal/types.mjs +16 -0
- package/dist/module/internal/types.mjs.map +1 -1
- package/dist/module/utils/assets.d.mts +5 -0
- package/dist/module/utils/assets.mjs +7 -0
- package/dist/module/utils/assets.mjs.map +1 -1
- package/dist/module/utils/externals.d.mts +5 -0
- package/dist/module/utils/externals.mjs +10 -0
- package/dist/module/utils/externals.mjs.map +1 -1
- package/dist/module/utils/nitro.d.mts +5 -0
- package/dist/module/utils/nitro.mjs +5 -0
- package/dist/module/utils/nitro.mjs.map +1 -1
- package/dist/module.d.mts +24 -2
- package/dist/module.mjs +21 -2
- package/dist/module.mjs.map +1 -1
- package/dist/plugins/legacy/init.mjs +27 -10
- package/dist/plugins/legacy/init.mjs.map +1 -1
- package/dist/runtime/internal/config.mjs +18 -6
- package/dist/runtime/internal/config.mjs.map +1 -1
- package/dist/runtime/internal/createDatasource.mjs +24 -5
- package/dist/runtime/internal/createDatasource.mjs.map +1 -1
- package/dist/runtime/useConfig.d.mts +15 -0
- package/dist/runtime/useConfig.mjs +22 -0
- package/dist/runtime/useConfig.mjs.map +1 -0
- package/dist/runtime/useDatasource.d.mts +5 -2
- package/dist/runtime/useDatasource.mjs +2 -1
- package/dist/runtime/useDatasource.mjs.map +1 -1
- package/dist/runtime/useDialect.d.mts +26 -0
- package/dist/runtime/useDialect.mjs +22 -0
- package/dist/runtime/useDialect.mjs.map +1 -0
- package/dist/runtime.d.mts +45 -28
- package/dist/runtime.mjs +3 -1
- package/dist/shared/variants.d.mts +35 -0
- package/dist/shared/variants.mjs +17 -0
- package/dist/shared/variants.mjs.map +1 -0
- package/dist/shared.d.mts +11 -3
- package/dist/shared.mjs +2 -1
- package/package.json +7 -5
- package/dist/drivers/internal/d1-dialect.mjs.map +0 -1
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
//#region src/module/utils/externals.ts
|
|
2
|
+
/**
|
|
3
|
+
* Adds module IDs to the inline externals list for legacy Nitro.
|
|
4
|
+
* @param nitroOptions - The legacy Nitro options
|
|
5
|
+
* @param moduleIds - The module IDs to add as inline externals
|
|
6
|
+
*/
|
|
2
7
|
function addInlineExternals(nitroOptions, moduleIds) {
|
|
3
8
|
nitroOptions.externals ||= {};
|
|
4
9
|
nitroOptions.externals.inline ||= [];
|
|
5
10
|
nitroOptions.externals.inline.push(...moduleIds);
|
|
6
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Adds module IDs to the no-externals list for modern Nitro.
|
|
14
|
+
* @param nitroOptions - The Nitro options
|
|
15
|
+
* @param moduleIds - The module IDs to exclude from externals
|
|
16
|
+
*/
|
|
7
17
|
function addNoExternals(nitroOptions, moduleIds) {
|
|
8
18
|
if (true === nitroOptions.noExternals) return;
|
|
9
19
|
nitroOptions.noExternals ||= [];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"externals.mjs","names":[],"sources":["../../../src/module/utils/externals.ts"],"sourcesContent":["import type { NitroOptions as LegacyNitroOptions } from \"nitropack/types\";\nimport type { NitroOptions } from \"nitro/types\";\n\nexport function addInlineExternals(nitroOptions: LegacyNitroOptions, moduleIds: readonly string[]) {\n // @ts-expect-error\n nitroOptions.externals ||= {};\n nitroOptions.externals.inline ||= [];\n nitroOptions.externals.inline.push(...moduleIds);\n}\n\nexport function addNoExternals(nitroOptions: NitroOptions, moduleIds: readonly string[]) {\n if (true === nitroOptions.noExternals) {\n return;\n }\n nitroOptions.noExternals ||= [];\n nitroOptions.noExternals.push(...moduleIds);\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"externals.mjs","names":[],"sources":["../../../src/module/utils/externals.ts"],"sourcesContent":["import type { NitroOptions as LegacyNitroOptions } from \"nitropack/types\";\nimport type { NitroOptions } from \"nitro/types\";\n\n/**\n * Adds module IDs to the inline externals list for legacy Nitro.\n * @param nitroOptions - The legacy Nitro options\n * @param moduleIds - The module IDs to add as inline externals\n */\nexport function addInlineExternals(nitroOptions: LegacyNitroOptions, moduleIds: readonly string[]) {\n // @ts-expect-error\n nitroOptions.externals ||= {};\n nitroOptions.externals.inline ||= [];\n nitroOptions.externals.inline.push(...moduleIds);\n}\n\n/**\n * Adds module IDs to the no-externals list for modern Nitro.\n * @param nitroOptions - The Nitro options\n * @param moduleIds - The module IDs to exclude from externals\n */\nexport function addNoExternals(nitroOptions: NitroOptions, moduleIds: readonly string[]) {\n if (true === nitroOptions.noExternals) {\n return;\n }\n nitroOptions.noExternals ||= [];\n nitroOptions.noExternals.push(...moduleIds);\n}\n"],"mappings":";;;;;;AAQA,SAAgB,mBAAmB,cAAkC,WAA8B;CAEjG,aAAa,cAAc,CAAC;CAC5B,aAAa,UAAU,WAAW,CAAC;CACnC,aAAa,UAAU,OAAO,KAAK,GAAG,SAAS;AACjD;;;;;;AAOA,SAAgB,eAAe,cAA4B,WAA8B;CACvF,IAAI,SAAS,aAAa,aACxB;CAEF,aAAa,gBAAgB,CAAC;CAC9B,aAAa,YAAY,KAAK,GAAG,SAAS;AAC5C"}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { Nitro } from "nitropack/types";
|
|
2
2
|
import { Nitro as Nitro$1 } from "nitro/types";
|
|
3
3
|
//#region src/module/utils/nitro.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Checks if the given Nitro instance is using the legacy API (nitropack).
|
|
6
|
+
* @param nitro - The Nitro instance to check
|
|
7
|
+
* @returns `true` if the Nitro instance is legacy (major version < 3)
|
|
8
|
+
*/
|
|
4
9
|
declare function isLegacy(nitro: Nitro$1 | Nitro): nitro is Nitro;
|
|
5
10
|
//#endregion
|
|
6
11
|
export { isLegacy };
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
//#region src/module/utils/nitro.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks if the given Nitro instance is using the legacy API (nitropack).
|
|
4
|
+
* @param nitro - The Nitro instance to check
|
|
5
|
+
* @returns `true` if the Nitro instance is legacy (major version < 3)
|
|
6
|
+
*/
|
|
2
7
|
function isLegacy(nitro) {
|
|
3
8
|
return nitro.meta.majorVersion < 3;
|
|
4
9
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nitro.mjs","names":[],"sources":["../../../src/module/utils/nitro.ts"],"sourcesContent":["import type { Nitro as LegacyNitro } from \"nitropack/types\";\nimport type { Nitro } from \"nitro/types\";\n\nexport function isLegacy(nitro: Nitro | LegacyNitro): nitro is LegacyNitro {\n return nitro.meta.majorVersion < 3;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"nitro.mjs","names":[],"sources":["../../../src/module/utils/nitro.ts"],"sourcesContent":["import type { Nitro as LegacyNitro } from \"nitropack/types\";\nimport type { Nitro } from \"nitro/types\";\n\n/**\n * Checks if the given Nitro instance is using the legacy API (nitropack).\n * @param nitro - The Nitro instance to check\n * @returns `true` if the Nitro instance is legacy (major version < 3)\n */\nexport function isLegacy(nitro: Nitro | LegacyNitro): nitro is LegacyNitro {\n return nitro.meta.majorVersion < 3;\n}\n"],"mappings":";;;;;;AAQA,SAAgB,SAAS,OAAkD;CACzE,OAAO,MAAM,KAAK,eAAe;AACnC"}
|
package/dist/module.d.mts
CHANGED
|
@@ -2,14 +2,24 @@ import { updateServerAssets } from "./module/utils/assets.mjs";
|
|
|
2
2
|
import { addInlineExternals } from "./module/utils/externals.mjs";
|
|
3
3
|
import { isLegacy } from "./module/utils/nitro.mjs";
|
|
4
4
|
import { ConfigPattern, MigrationOptions } from "nitro-drizzle/context";
|
|
5
|
+
import { ConnectorSpecifier, ConnectorVariants, ExpandVariants, Variant } from "nitro-drizzle/shared";
|
|
5
6
|
import { NitroModule } from "nitropack/types";
|
|
6
7
|
import { NitroModule as NitroModule$1 } from "nitro/types";
|
|
8
|
+
import { Connector } from "nitro-drizzle/drivers";
|
|
7
9
|
//#region src/module/index.d.ts
|
|
8
10
|
/**
|
|
9
11
|
* Datasource-specific configuration options.
|
|
10
|
-
* This interface is augmented by the module with connector-specific options.
|
|
11
12
|
*/
|
|
12
|
-
|
|
13
|
+
type DatasourceOptionsMapper<T extends Variant<Connector<any, any, any, any>, ConnectorSpecifier>> = T extends any ? Variant<{
|
|
14
|
+
drivers: readonly string[] | {
|
|
15
|
+
[name: string]: boolean;
|
|
16
|
+
};
|
|
17
|
+
}, Pick<T["selector"], "name">> : never;
|
|
18
|
+
type DatasourceOptionsVariants = DatasourceOptionsMapper<ConnectorVariants>;
|
|
19
|
+
/**
|
|
20
|
+
* Datasource-specific configuration options mapped by datasource name.
|
|
21
|
+
*/
|
|
22
|
+
type DatasourceOptions = ExpandVariants<DatasourceOptionsVariants, ["name"]>;
|
|
13
23
|
declare module "nitropack/types" {
|
|
14
24
|
interface NitroOptions {
|
|
15
25
|
drizzle?: ModuleOptions;
|
|
@@ -35,9 +45,16 @@ interface ModuleOptions {
|
|
|
35
45
|
*/
|
|
36
46
|
migrations?: false | Partial<MigrationOptions>;
|
|
37
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Resolved module configuration with required fields.
|
|
50
|
+
*/
|
|
38
51
|
type ModuleConfig = Required<ModuleOptions> & {
|
|
39
52
|
migrations: false | Required<MigrationOptions>;
|
|
40
53
|
};
|
|
54
|
+
/**
|
|
55
|
+
* Creates the default module configuration options.
|
|
56
|
+
* @returns The default module configuration
|
|
57
|
+
*/
|
|
41
58
|
declare function createDefaultOptions(): {
|
|
42
59
|
readonly baseDir: "./drizzle";
|
|
43
60
|
readonly configPattern: readonly ["drizzle.config.{js,ts}", "drizzle-*.config.{js,ts}"];
|
|
@@ -47,6 +64,11 @@ declare function createDefaultOptions(): {
|
|
|
47
64
|
readonly migrateOnInit: false;
|
|
48
65
|
};
|
|
49
66
|
};
|
|
67
|
+
/**
|
|
68
|
+
* Merges user-provided module options with defaults.
|
|
69
|
+
* @param options - Optional module configuration overrides
|
|
70
|
+
* @returns The resolved module configuration
|
|
71
|
+
*/
|
|
50
72
|
declare function defineModuleConfig(options?: ModuleOptions): ModuleConfig;
|
|
51
73
|
declare const module: NitroModule & NitroModule$1;
|
|
52
74
|
//#endregion
|
package/dist/module.mjs
CHANGED
|
@@ -11,6 +11,10 @@ import { resolveAlias } from "pathe/utils";
|
|
|
11
11
|
import { createContext } from "nitro-drizzle/context";
|
|
12
12
|
import { pkgName } from "nitro-drizzle/meta";
|
|
13
13
|
//#region src/module/index.ts
|
|
14
|
+
/**
|
|
15
|
+
* Creates the default module configuration options.
|
|
16
|
+
* @returns The default module configuration
|
|
17
|
+
*/
|
|
14
18
|
function createDefaultOptions() {
|
|
15
19
|
return {
|
|
16
20
|
baseDir: "./drizzle",
|
|
@@ -22,17 +26,23 @@ function createDefaultOptions() {
|
|
|
22
26
|
}
|
|
23
27
|
};
|
|
24
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Merges user-provided module options with defaults.
|
|
31
|
+
* @param options - Optional module configuration overrides
|
|
32
|
+
* @returns The resolved module configuration
|
|
33
|
+
*/
|
|
25
34
|
function defineModuleConfig(options) {
|
|
26
35
|
return defu(options, createDefaultOptions());
|
|
27
36
|
}
|
|
28
37
|
const module = {
|
|
29
38
|
name: pkgName,
|
|
30
39
|
async setup(nitro) {
|
|
40
|
+
const logger = nitro.logger.withTag(pkgName);
|
|
31
41
|
const moduleConfig = defineModuleConfig(nitro.options.drizzle);
|
|
32
42
|
const resolver = createResolver(nitro.options.rootDir, { alias: nitro.options.alias });
|
|
33
43
|
const serverDir = isLegacy(nitro) ? nitro.options.srcDir : nitro.options.serverDir;
|
|
34
44
|
if (!serverDir) {
|
|
35
|
-
|
|
45
|
+
logger.info("No server directory configured.");
|
|
36
46
|
return;
|
|
37
47
|
}
|
|
38
48
|
const baseDir = resolve(serverDir, resolveAlias(moduleConfig.baseDir, nitro.options.alias));
|
|
@@ -49,10 +59,18 @@ const module = {
|
|
|
49
59
|
cwd: process.cwd(),
|
|
50
60
|
resolver,
|
|
51
61
|
baseDir,
|
|
52
|
-
logger
|
|
62
|
+
logger,
|
|
53
63
|
configPattern: moduleConfig.configPattern,
|
|
54
64
|
datasources: { ...moduleConfig.datasources },
|
|
55
65
|
migrations: moduleConfig.migrations || void 0,
|
|
66
|
+
get initHooks() {
|
|
67
|
+
const hooks = [];
|
|
68
|
+
if (nitro.options.preset.startsWith("cloudflare")) {
|
|
69
|
+
if (nitro.options.preset.endsWith("durable")) hooks.push("cloudflare:durable:init", "cloudflare:durable:alarm");
|
|
70
|
+
hooks.push("request", "cloudflare:email", "cloudflare:queue", "cloudflare:scheduled", "cloudflare:tail", "cloudflare:trace");
|
|
71
|
+
}
|
|
72
|
+
return hooks;
|
|
73
|
+
},
|
|
56
74
|
tasks: nitro.options.experimental.tasks ? (tasks) => {
|
|
57
75
|
nitro.options.tasks ||= {};
|
|
58
76
|
Object.assign(nitro.options.tasks, tasks);
|
|
@@ -66,6 +84,7 @@ const module = {
|
|
|
66
84
|
declarations(declarations) {
|
|
67
85
|
nitro.hooks.hook("types:extend", async (types) => {
|
|
68
86
|
await addAugmentations(nitro, types, {
|
|
87
|
+
...declarations.shared,
|
|
69
88
|
...declarations.runtime,
|
|
70
89
|
...declarations.module
|
|
71
90
|
});
|
package/dist/module.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module.mjs","names":[],"sources":["../src/module/index.ts"],"sourcesContent":["import type { NitroModule as LegacyNitroModule } from \"nitropack/types\";\nimport type { NitroModule } from \"nitro/types\";\nimport { defu } from \"defu\";\nimport { resolve } from \"pathe\";\nimport { resolveAlias } from \"pathe/utils\";\n\nimport {\n createContext,\n type ConfigPattern,\n type ContextOptions,\n type MigrationOptions,\n} from \"nitro-drizzle/context\";\nimport type {
|
|
1
|
+
{"version":3,"file":"module.mjs","names":[],"sources":["../src/module/index.ts"],"sourcesContent":["import type { NitroModule as LegacyNitroModule } from \"nitropack/types\";\nimport type { NitroModule } from \"nitro/types\";\nimport { defu } from \"defu\";\nimport { resolve } from \"pathe\";\nimport { resolveAlias } from \"pathe/utils\";\n\nimport {\n createContext,\n type ConfigPattern,\n type ContextOptions,\n type MigrationOptions,\n} from \"nitro-drizzle/context\";\nimport type {\n VirtualModules,\n MaybePromise,\n NitroHookName,\n Variant,\n ConnectorSpecifier,\n ConnectorVariants,\n ExpandVariants,\n} from \"nitro-drizzle/shared\";\nimport { pkgName } from \"nitro-drizzle/meta\";\nimport type { Connector } from \"nitro-drizzle/drivers\";\n\nimport { updateServerAssets } from \"./utils/assets\";\nimport { addInlineExternals, addNoExternals } from \"./utils/externals\";\n\nimport { addAugmentations, addDeclarations } from \"./internal/types\";\nimport { createResolver } from \"./internal/resolver\";\nimport { reloadPlugin } from \"./internal/reload\";\nimport { addPlugin } from \"./internal/rollup\";\nimport { isLegacy } from \"./utils/nitro\";\n\n/**\n * Datasource-specific configuration options.\n */\ntype DatasourceOptionsMapper<T extends Variant<Connector<any, any, any, any>, ConnectorSpecifier>> =\n T extends any\n ? Variant<\n { drivers: readonly string[] | { [name: string]: boolean } },\n Pick<T[\"selector\"], \"name\">\n >\n : never;\n\ntype DatasourceOptionsVariants = DatasourceOptionsMapper<ConnectorVariants>;\n\n/**\n * Datasource-specific configuration options mapped by datasource name.\n */\nexport type DatasourceOptions = ExpandVariants<DatasourceOptionsVariants, [\"name\"]>;\n\ndeclare module \"nitropack/types\" {\n interface NitroOptions {\n drizzle?: ModuleOptions;\n }\n}\n\ndeclare module \"nitro/types\" {\n interface NitroOptions {\n drizzle?: ModuleOptions;\n }\n}\n\nexport interface ModuleOptions {\n /**\n * Directory to search datasources\n */\n baseDir?: string;\n /**\n * Patterns to search Drizzle configs\n */\n configPattern?: ConfigPattern;\n\n datasources?: DatasourceOptions;\n\n /**\n * Enable migrations storage\n */\n migrations?: false | Partial<MigrationOptions>;\n}\n\n/**\n * Resolved module configuration with required fields.\n */\nexport type ModuleConfig = Required<ModuleOptions> & {\n migrations: false | Required<MigrationOptions>;\n};\n\n/**\n * Creates the default module configuration options.\n * @returns The default module configuration\n */\nexport function createDefaultOptions() {\n return {\n baseDir: \"./drizzle\",\n configPattern: [\"drizzle.config.{js,ts}\", \"drizzle-*.config.{js,ts}\"],\n datasources: {},\n migrations: {\n storageBase: \"drizzle:migrations\",\n migrateOnInit: false,\n },\n } as const satisfies ModuleConfig;\n}\n\n/**\n * Merges user-provided module options with defaults.\n * @param options - Optional module configuration overrides\n * @returns The resolved module configuration\n */\nexport function defineModuleConfig(options?: ModuleOptions): ModuleConfig {\n return defu(options, createDefaultOptions());\n}\n\nconst module: LegacyNitroModule & NitroModule = {\n name: pkgName,\n async setup(nitro) {\n const logger = nitro.logger.withTag(pkgName);\n const moduleConfig = defineModuleConfig(nitro.options.drizzle);\n\n const resolver = createResolver(nitro.options.rootDir, {\n alias: nitro.options.alias,\n });\n\n const serverDir = isLegacy(nitro) ? nitro.options.srcDir : nitro.options.serverDir;\n if (!serverDir) {\n logger.info(\"No server directory configured.\");\n return;\n }\n\n const baseDir = resolve(serverDir, resolveAlias(moduleConfig.baseDir, nitro.options.alias));\n\n if (!isLegacy(nitro)) {\n nitro.options.handlers ||= [];\n nitro.options.handlers.push({\n route: \"/**\",\n handler: resolver.resolve(\"nitro-drizzle/middleware/context\"),\n middleware: true,\n });\n }\n\n const contextOptions: ContextOptions = {\n legacy: isLegacy(nitro),\n cwd: process.cwd(),\n resolver: resolver,\n baseDir,\n logger,\n configPattern: moduleConfig.configPattern,\n datasources: { ...moduleConfig.datasources },\n migrations: moduleConfig.migrations || void 0,\n get initHooks() {\n const hooks: NitroHookName[] = [];\n if (nitro.options.preset.startsWith(\"cloudflare\")) {\n if (nitro.options.preset.endsWith(\"durable\")) {\n hooks.push(\"cloudflare:durable:init\", \"cloudflare:durable:alarm\");\n }\n hooks.push(\n \"request\",\n \"cloudflare:email\",\n \"cloudflare:queue\",\n \"cloudflare:scheduled\",\n \"cloudflare:tail\",\n \"cloudflare:trace\",\n );\n }\n return hooks;\n },\n\n tasks: nitro.options.experimental.tasks\n ? (tasks) => {\n nitro.options.tasks ||= {};\n Object.assign(nitro.options.tasks, tasks);\n }\n : void 0,\n\n plugins(plugins) {\n nitro.options.plugins.push(\n ...plugins.map((plugin) => resolver.resolve(plugin).replace(/\\.mjs$/, \"\")),\n );\n },\n\n virtualModules(modules: VirtualModules): MaybePromise<void> {\n Object.assign(nitro.options.virtual, modules);\n },\n\n declarations(declarations): MaybePromise<void> {\n nitro.hooks.hook(\"types:extend\", async (types) => {\n await addAugmentations(nitro, types, {\n ...declarations.shared,\n ...declarations.runtime,\n ...declarations.module,\n });\n\n await addDeclarations(nitro, types, declarations.virtual);\n });\n },\n\n assets(assets) {\n updateServerAssets(nitro.options, assets);\n },\n\n externals(modules) {\n if (isLegacy(nitro)) {\n addInlineExternals(nitro.options, modules);\n } else {\n addNoExternals(nitro.options, modules);\n }\n },\n };\n\n const context = createContext(contextOptions);\n\n // auto-imports\n if (nitro.options.imports) {\n nitro.options.imports.imports ||= [];\n\n // TODO\n // nitro.options.imports.imports.push({\n // name: 'useMigrations',\n // as: 'useDrizzleMigrations',\n // from: 'nitro-drizzle/runtime',\n // })\n }\n\n nitro.hooks.hook(\"rollup:before\", async (nitro, config) => {\n await addPlugin(config, reloadPlugin(nitro, { baseDir }));\n });\n\n await context.init();\n },\n};\n\nexport default module;\n\nexport { addInlineExternals, updateServerAssets, isLegacy as isLegacyNitro };\n"],"mappings":";;;;;;;;;;;;;;;;;AA4FA,SAAgB,uBAAuB;CACrC,OAAO;EACL,SAAS;EACT,eAAe,CAAC,0BAA0B,0BAA0B;EACpE,aAAa,CAAC;EACd,YAAY;GACV,aAAa;GACb,eAAe;EACjB;CACF;AACF;;;;;;AAOA,SAAgB,mBAAmB,SAAuC;CACxE,OAAO,KAAK,SAAS,qBAAqB,CAAC;AAC7C;AAEA,MAAM,SAA0C;CAC9C,MAAM;CACN,MAAM,MAAM,OAAO;EACjB,MAAM,SAAS,MAAM,OAAO,QAAQ,OAAO;EAC3C,MAAM,eAAe,mBAAmB,MAAM,QAAQ,OAAO;EAE7D,MAAM,WAAW,eAAe,MAAM,QAAQ,SAAS,EACrD,OAAO,MAAM,QAAQ,MACvB,CAAC;EAED,MAAM,YAAY,SAAS,KAAK,IAAI,MAAM,QAAQ,SAAS,MAAM,QAAQ;EACzE,IAAI,CAAC,WAAW;GACd,OAAO,KAAK,iCAAiC;GAC7C;EACF;EAEA,MAAM,UAAU,QAAQ,WAAW,aAAa,aAAa,SAAS,MAAM,QAAQ,KAAK,CAAC;EAE1F,IAAI,CAAC,SAAS,KAAK,GAAG;GACpB,MAAM,QAAQ,aAAa,CAAC;GAC5B,MAAM,QAAQ,SAAS,KAAK;IAC1B,OAAO;IACP,SAAS,SAAS,QAAQ,kCAAkC;IAC5D,YAAY;GACd,CAAC;EACH;EAuEA,MAAM,UAAU,cAAc;GApE5B,QAAQ,SAAS,KAAK;GACtB,KAAK,QAAQ,IAAI;GACP;GACV;GACA;GACA,eAAe,aAAa;GAC5B,aAAa,EAAE,GAAG,aAAa,YAAY;GAC3C,YAAY,aAAa,cAAc,KAAK;GAC5C,IAAI,YAAY;IACd,MAAM,QAAyB,CAAC;IAChC,IAAI,MAAM,QAAQ,OAAO,WAAW,YAAY,GAAG;KACjD,IAAI,MAAM,QAAQ,OAAO,SAAS,SAAS,GACzC,MAAM,KAAK,2BAA2B,0BAA0B;KAElE,MAAM,KACJ,WACA,oBACA,oBACA,wBACA,mBACA,kBACF;IACF;IACA,OAAO;GACT;GAEA,OAAO,MAAM,QAAQ,aAAa,SAC7B,UAAU;IACT,MAAM,QAAQ,UAAU,CAAC;IACzB,OAAO,OAAO,MAAM,QAAQ,OAAO,KAAK;GAC1C,IACA,KAAK;GAET,QAAQ,SAAS;IACf,MAAM,QAAQ,QAAQ,KACpB,GAAG,QAAQ,KAAK,WAAW,SAAS,QAAQ,MAAM,CAAC,CAAC,QAAQ,UAAU,EAAE,CAAC,CAC3E;GACF;GAEA,eAAe,SAA6C;IAC1D,OAAO,OAAO,MAAM,QAAQ,SAAS,OAAO;GAC9C;GAEA,aAAa,cAAkC;IAC7C,MAAM,MAAM,KAAK,gBAAgB,OAAO,UAAU;KAChD,MAAM,iBAAiB,OAAO,OAAO;MACnC,GAAG,aAAa;MAChB,GAAG,aAAa;MAChB,GAAG,aAAa;KAClB,CAAC;KAED,MAAM,gBAAgB,OAAO,OAAO,aAAa,OAAO;IAC1D,CAAC;GACH;GAEA,OAAO,QAAQ;IACb,mBAAmB,MAAM,SAAS,MAAM;GAC1C;GAEA,UAAU,SAAS;IACjB,IAAI,SAAS,KAAK,GAChB,mBAAmB,MAAM,SAAS,OAAO;SAEzC,eAAe,MAAM,SAAS,OAAO;GAEzC;EAGyC,CAAC;EAG5C,IAAI,MAAM,QAAQ,SAChB,MAAM,QAAQ,QAAQ,YAAY,CAAC;EAUrC,MAAM,MAAM,KAAK,iBAAiB,OAAO,OAAO,WAAW;GACzD,MAAM,UAAU,QAAQ,aAAa,OAAO,EAAE,QAAQ,CAAC,CAAC;EAC1D,CAAC;EAED,MAAM,QAAQ,KAAK;CACrB;AACF"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { initHooks } from "#nitro-drizzle/runtime";
|
|
1
2
|
import { defineNitroPlugin } from "nitropack/runtime";
|
|
2
3
|
//#region src/plugins/legacy/init.ts
|
|
3
4
|
/**
|
|
@@ -12,20 +13,36 @@ var init_default = defineNitroPlugin(async (nitro) => {
|
|
|
12
13
|
};
|
|
13
14
|
nitro.hooks.hook("request", (event) => {
|
|
14
15
|
event.context.drizzle = {
|
|
15
|
-
readyState
|
|
16
|
+
get readyState() {
|
|
17
|
+
return readyState;
|
|
18
|
+
},
|
|
16
19
|
waitReady
|
|
17
20
|
};
|
|
18
21
|
});
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
resolve
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
const cleanup = [];
|
|
23
|
+
async function callInitHook() {
|
|
24
|
+
if (!initHook) {
|
|
25
|
+
initHook = new Promise((resolve, reject) => {
|
|
26
|
+
nitro.hooks.callHook("drizzle:init").then(() => {
|
|
27
|
+
readyState = "done";
|
|
28
|
+
resolve(readyState);
|
|
29
|
+
}).catch((err) => {
|
|
30
|
+
readyState = "error";
|
|
31
|
+
reject(err);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
for (const fn of cleanup) fn();
|
|
35
|
+
}
|
|
36
|
+
await initHook;
|
|
37
|
+
}
|
|
38
|
+
if (initHooks?.length) {
|
|
39
|
+
const cleanups = initHooks.map((hookName) => {
|
|
40
|
+
return nitro.hooks.hook(hookName, async () => {
|
|
41
|
+
await callInitHook();
|
|
42
|
+
});
|
|
26
43
|
});
|
|
27
|
-
|
|
28
|
-
await
|
|
44
|
+
cleanup.push(...cleanups);
|
|
45
|
+
} else await callInitHook();
|
|
29
46
|
});
|
|
30
47
|
//#endregion
|
|
31
48
|
export { init_default as default };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.mjs","names":[],"sources":["../../../src/plugins/legacy/init.ts"],"sourcesContent":["import { defineNitroPlugin } from \"nitropack/runtime\";\n\n/**\n * Nitro plugin that triggers the 'drizzle:init' hook during server initialization.\n * Used to run migration plugins at startup.\n */\nexport default defineNitroPlugin(async (nitro) => {\n let initHook: Promise<ReadyState>;\n let readyState: ReadyState = \"pending\";\n\n const waitReady = async () => {\n await initHook;\n };\n\n nitro.hooks.hook(\"request\", (event) => {\n event.context.drizzle = {\n readyState,\n waitReady,\n };\n });\n\n initHook = new Promise((resolve, reject) => {\n
|
|
1
|
+
{"version":3,"file":"init.mjs","names":[],"sources":["../../../src/plugins/legacy/init.ts"],"sourcesContent":["import { defineNitroPlugin } from \"nitropack/runtime\";\nimport { initHooks } from \"#nitro-drizzle/runtime\";\n\n/**\n * Nitro plugin that triggers the 'drizzle:init' hook during server initialization.\n * Used to run migration plugins at startup.\n */\nexport default defineNitroPlugin(async (nitro) => {\n let initHook: Promise<ReadyState>;\n let readyState: ReadyState = \"pending\";\n\n const waitReady = async () => {\n await initHook;\n };\n\n // TODO: should be replaced with middleware\n nitro.hooks.hook(\"request\", (event) => {\n event.context.drizzle = {\n get readyState() {\n return readyState;\n },\n waitReady,\n };\n });\n\n const cleanup: (() => void)[] = [];\n\n async function callInitHook() {\n if (!initHook) {\n initHook = new Promise((resolve, reject) => {\n nitro.hooks\n .callHook(\"drizzle:init\")\n .then(() => {\n readyState = \"done\";\n resolve(readyState);\n })\n .catch((err) => {\n readyState = \"error\";\n reject(err);\n });\n });\n\n for (const fn of cleanup) {\n fn();\n }\n }\n\n await initHook;\n }\n\n if (initHooks?.length) {\n const cleanups = initHooks.map((hookName) => {\n return nitro.hooks.hook(hookName, async () => {\n await callInitHook();\n });\n });\n\n cleanup.push(...cleanups);\n } else {\n await callInitHook();\n }\n});\n\nexport type ReadyState = \"pending\" | \"done\" | \"error\";\n\nexport type InitHooks = {\n \"drizzle:init\": () => void;\n};\n\ndeclare module \"nitropack/types\" {\n interface NitroRuntimeHooks extends InitHooks {}\n}\n\ndeclare module \"h3\" {\n interface H3EventContext {\n drizzle: {\n readyState: ReadyState;\n waitReady: () => Promise<void>;\n };\n }\n}\n"],"mappings":";;;;;;;AAOA,IAAA,eAAe,kBAAkB,OAAO,UAAU;CAChD,IAAI;CACJ,IAAI,aAAyB;CAE7B,MAAM,YAAY,YAAY;EAC5B,MAAM;CACR;CAGA,MAAM,MAAM,KAAK,YAAY,UAAU;EACrC,MAAM,QAAQ,UAAU;GACtB,IAAI,aAAa;IACf,OAAO;GACT;GACA;EACF;CACF,CAAC;CAED,MAAM,UAA0B,CAAC;CAEjC,eAAe,eAAe;EAC5B,IAAI,CAAC,UAAU;GACb,WAAW,IAAI,SAAS,SAAS,WAAW;IAC1C,MAAM,MACH,SAAS,cAAc,CAAC,CACxB,WAAW;KACV,aAAa;KACb,QAAQ,UAAU;IACpB,CAAC,CAAC,CACD,OAAO,QAAQ;KACd,aAAa;KACb,OAAO,GAAG;IACZ,CAAC;GACL,CAAC;GAED,KAAK,MAAM,MAAM,SACf,GAAG;EAEP;EAEA,MAAM;CACR;CAEA,IAAI,WAAW,QAAQ;EACrB,MAAM,WAAW,UAAU,KAAK,aAAa;GAC3C,OAAO,MAAM,MAAM,KAAK,UAAU,YAAY;IAC5C,MAAM,aAAa;GACrB,CAAC;EACH,CAAC;EAED,QAAQ,KAAK,GAAG,QAAQ;CAC1B,OACE,MAAM,aAAa;AAEvB,CAAC"}
|
|
@@ -1,24 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { useConfig } from "../useConfig.mjs";
|
|
2
|
+
import "../../runtime.mjs";
|
|
3
|
+
import { callConfigHook } from "#nitro-drizzle/runtime";
|
|
3
4
|
//#region src/runtime/internal/config.ts
|
|
4
5
|
const datasourceConfig = {};
|
|
5
6
|
/**
|
|
7
|
+
* Gets the driver configuration for a datasource, calling the config hook.
|
|
6
8
|
* @internal
|
|
9
|
+
* @template TName - The datasource name
|
|
10
|
+
* @param name - The datasource name
|
|
11
|
+
* @returns The driver configuration
|
|
12
|
+
* @throws If called inside the `drizzle:config` hook to prevent circular access
|
|
7
13
|
*/
|
|
8
|
-
async function
|
|
14
|
+
async function getDriverConfig(name) {
|
|
9
15
|
if (name in datasourceConfig && !datasourceConfig[name]) throw new Error("Cannot obtain datasource config. Do you try to obtain it inside the 'drizzle:config' hook?");
|
|
10
16
|
else {
|
|
11
17
|
datasourceConfig[name] = void 0;
|
|
12
|
-
const config =
|
|
13
|
-
await callConfigHook(name, config);
|
|
18
|
+
const { driver, config } = useConfig(name);
|
|
19
|
+
await callConfigHook(name, driver, config);
|
|
14
20
|
datasourceConfig[name] = config;
|
|
15
21
|
}
|
|
16
22
|
return datasourceConfig[name];
|
|
17
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Clears the cached driver configuration for a datasource.
|
|
26
|
+
* @internal
|
|
27
|
+
* @template TName - The datasource name
|
|
28
|
+
* @param name - The datasource name
|
|
29
|
+
*/
|
|
18
30
|
function clearCachedConfig(name) {
|
|
19
31
|
delete datasourceConfig[name];
|
|
20
32
|
}
|
|
21
33
|
//#endregion
|
|
22
|
-
export { clearCachedConfig,
|
|
34
|
+
export { clearCachedConfig, getDriverConfig };
|
|
23
35
|
|
|
24
36
|
//# sourceMappingURL=config.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.mjs","names":[],"sources":["../../../src/runtime/internal/config.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"config.mjs","names":[],"sources":["../../../src/runtime/internal/config.ts"],"sourcesContent":["import { type Datasources, type DatasourceConfig, useConfig } from \"..\";\nimport { callConfigHook } from \"#nitro-drizzle/runtime\";\n\nconst datasourceConfig: Partial<{\n [TName in keyof DatasourceConfig]: DatasourceConfig[TName][string];\n}> = {};\n\n/**\n * Gets the driver configuration for a datasource, calling the config hook.\n * @internal\n * @template TName - The datasource name\n * @param name - The datasource name\n * @returns The driver configuration\n * @throws If called inside the `drizzle:config` hook to prevent circular access\n */\nexport async function getDriverConfig<TName extends keyof Datasources & string>(name: TName) {\n if (name in datasourceConfig && !datasourceConfig[name]) {\n throw new Error(\n \"Cannot obtain datasource config. Do you try to obtain it inside the 'drizzle:config' hook?\",\n );\n } else {\n datasourceConfig[name] = undefined;\n\n const { driver, config } = useConfig(name);\n\n await callConfigHook(name, driver, config);\n\n datasourceConfig[name] = config;\n }\n\n return datasourceConfig[name];\n}\n\n/**\n * Clears the cached driver configuration for a datasource.\n * @internal\n * @template TName - The datasource name\n * @param name - The datasource name\n */\nexport function clearCachedConfig<TName extends keyof Datasources & string>(name: TName) {\n delete datasourceConfig[name];\n}\n"],"mappings":";;;;AAGA,MAAM,mBAED,CAAC;;;;;;;;;AAUN,eAAsB,gBAA0D,MAAa;CAC3F,IAAI,QAAQ,oBAAoB,CAAC,iBAAiB,OAChD,MAAM,IAAI,MACR,4FACF;MACK;EACL,iBAAiB,QAAQ,KAAA;EAEzB,MAAM,EAAE,QAAQ,WAAW,UAAU,IAAI;EAEzC,MAAM,eAAe,MAAM,QAAQ,MAAM;EAEzC,iBAAiB,QAAQ;CAC3B;CAEA,OAAO,iBAAiB;AAC1B;;;;;;;AAQA,SAAgB,kBAA4D,MAAa;CACvF,OAAO,iBAAiB;AAC1B"}
|
|
@@ -1,15 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getDriverConfig } from "./config.mjs";
|
|
2
|
+
import { useConfig } from "../useConfig.mjs";
|
|
3
|
+
import "../../runtime.mjs";
|
|
2
4
|
import { useDatasourceRegistry } from "#nitro-drizzle/runtime";
|
|
3
5
|
//#region src/runtime/internal/createDatasource.ts
|
|
4
6
|
/**
|
|
7
|
+
* Creates a datasource instance by name.
|
|
5
8
|
* @internal
|
|
9
|
+
* @param name - The datasource name
|
|
10
|
+
* @returns The datasource instance
|
|
6
11
|
*/
|
|
7
12
|
async function createDatasource(name) {
|
|
8
|
-
const datasourceProvider =
|
|
9
|
-
const
|
|
10
|
-
return await datasourceProvider.create(
|
|
13
|
+
const datasourceProvider = useDatasourceProvider(name);
|
|
14
|
+
const config = await getDriverConfig(name);
|
|
15
|
+
return await datasourceProvider.create(config);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Retrieves the datasource provider for a given datasource name.
|
|
19
|
+
* @internal
|
|
20
|
+
* @template TName - The datasource name
|
|
21
|
+
* @param name - The datasource name
|
|
22
|
+
* @returns The datasource provider for the configured driver
|
|
23
|
+
* @throws If the driver is not supported by the datasource
|
|
24
|
+
*/
|
|
25
|
+
function useDatasourceProvider(name) {
|
|
26
|
+
const datasourceRegistry = useDatasourceRegistry();
|
|
27
|
+
const { driver } = useConfig(name);
|
|
28
|
+
if (false === driver in datasourceRegistry[name]) throw new Error(`Driver "${driver}" is not supported by "${name}" datasource.`);
|
|
29
|
+
return datasourceRegistry[name][driver];
|
|
11
30
|
}
|
|
12
31
|
//#endregion
|
|
13
|
-
export { createDatasource };
|
|
32
|
+
export { createDatasource, useDatasourceProvider };
|
|
14
33
|
|
|
15
34
|
//# sourceMappingURL=createDatasource.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createDatasource.mjs","names":[],"sources":["../../../src/runtime/internal/createDatasource.ts"],"sourcesContent":["import { useDatasourceRegistry } from \"#nitro-drizzle/runtime\";\nimport
|
|
1
|
+
{"version":3,"file":"createDatasource.mjs","names":[],"sources":["../../../src/runtime/internal/createDatasource.ts"],"sourcesContent":["import { useDatasourceRegistry } from \"#nitro-drizzle/runtime\";\nimport { useConfig, type DatasourceRegistry } from \"..\";\nimport { getDriverConfig } from \"./config\";\n\n/**\n * Creates a datasource instance by name.\n * @internal\n * @param name - The datasource name\n * @returns The datasource instance\n */\nexport async function createDatasource(name: keyof DatasourceRegistry & string) {\n const datasourceProvider = useDatasourceProvider(name);\n const config = await getDriverConfig(name);\n return await datasourceProvider.create(config);\n}\n\n/**\n * Retrieves the datasource provider for a given datasource name.\n * @internal\n * @template TName - The datasource name\n * @param name - The datasource name\n * @returns The datasource provider for the configured driver\n * @throws If the driver is not supported by the datasource\n */\nexport function useDatasourceProvider<TName extends keyof DatasourceRegistry & string>(\n name: TName,\n) {\n const datasourceRegistry = useDatasourceRegistry();\n const { driver } = useConfig(name);\n if (false === driver in datasourceRegistry[name]) {\n throw new Error(`Driver \"${driver}\" is not supported by \"${name}\" datasource.`);\n }\n return datasourceRegistry[name][driver];\n}\n"],"mappings":";;;;;;;;;;;AAUA,eAAsB,iBAAiB,MAAyC;CAC9E,MAAM,qBAAqB,sBAAsB,IAAI;CACrD,MAAM,SAAS,MAAM,gBAAgB,IAAI;CACzC,OAAO,MAAM,mBAAmB,OAAO,MAAM;AAC/C;;;;;;;;;AAUA,SAAgB,sBACd,MACA;CACA,MAAM,qBAAqB,sBAAsB;CACjD,MAAM,EAAE,WAAW,UAAU,IAAI;CACjC,IAAI,UAAU,UAAU,mBAAmB,OACzC,MAAM,IAAI,MAAM,WAAW,OAAO,yBAAyB,KAAK,cAAc;CAEhF,OAAO,mBAAmB,KAAK,CAAC;AAClC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Datasources, PrimitiveProps } from "../runtime.mjs";
|
|
2
|
+
//#region src/runtime/useConfig.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Retrieves the runtime configuration for a specific datasource.
|
|
5
|
+
* @template TName - The datasource name
|
|
6
|
+
* @param name - The datasource name
|
|
7
|
+
* @returns The driver name and driver-specific configuration
|
|
8
|
+
*/
|
|
9
|
+
declare function useConfig<TName extends keyof Datasources & string>(name: TName): {
|
|
10
|
+
driver: string;
|
|
11
|
+
config: PrimitiveProps<{}>;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { useConfig };
|
|
15
|
+
//# sourceMappingURL=useConfig.d.mts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { defu } from "defu";
|
|
2
|
+
import { useRuntimeConfig } from "#nitro-drizzle/runtime";
|
|
3
|
+
//#region src/runtime/useConfig.ts
|
|
4
|
+
/**
|
|
5
|
+
* Retrieves the runtime configuration for a specific datasource.
|
|
6
|
+
* @template TName - The datasource name
|
|
7
|
+
* @param name - The datasource name
|
|
8
|
+
* @returns The driver name and driver-specific configuration
|
|
9
|
+
*/
|
|
10
|
+
function useConfig(name) {
|
|
11
|
+
const config = useRuntimeConfig()?.[name];
|
|
12
|
+
const driver = config?.driver;
|
|
13
|
+
if (!driver) throw new Error(`No driver name provided for datasource "${name}".`);
|
|
14
|
+
return {
|
|
15
|
+
driver,
|
|
16
|
+
config: defu(config[driver], {})
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { useConfig };
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=useConfig.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useConfig.mjs","names":[],"sources":["../../src/runtime/useConfig.ts"],"sourcesContent":["import { defu } from \"defu\";\nimport type { Datasources } from \".\";\nimport { useRuntimeConfig } from \"#nitro-drizzle/runtime\";\n\n/**\n * Retrieves the runtime configuration for a specific datasource.\n * @template TName - The datasource name\n * @param name - The datasource name\n * @returns The driver name and driver-specific configuration\n */\nexport function useConfig<TName extends keyof Datasources & string>(name: TName) {\n const runtimeConfig = useRuntimeConfig();\n const config = runtimeConfig?.[name];\n const driver = config?.driver;\n if (!driver) {\n throw new Error(`No driver name provided for datasource \"${name}\".`);\n }\n\n return {\n driver,\n config: defu(config[driver], {}),\n };\n}\n"],"mappings":";;;;;;;;;AAUA,SAAgB,UAAoD,MAAa;CAE/E,MAAM,SADgB,iBACK,CAAC,GAAG;CAC/B,MAAM,SAAS,QAAQ;CACvB,IAAI,CAAC,QACH,MAAM,IAAI,MAAM,2CAA2C,KAAK,GAAG;CAGrE,OAAO;EACL;EACA,QAAQ,KAAK,OAAO,SAAS,CAAC,CAAC;CACjC;AACF"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DatasourceVariants } from "../runtime.mjs";
|
|
2
|
+
import { ConnectorSpecifier, UnwrapVariant } from "nitro-drizzle/shared";
|
|
2
3
|
//#region src/runtime/useDatasource.d.ts
|
|
3
4
|
/** Options for datasource creation and lifecycle management. */
|
|
4
5
|
type UseDatasourceOptions = Partial<{
|
|
@@ -13,7 +14,9 @@ type UseDatasourceOptions = Partial<{
|
|
|
13
14
|
* @param options - Lifecycle options
|
|
14
15
|
* @returns The datasource instance
|
|
15
16
|
*/
|
|
16
|
-
declare function useDatasource<TName extends
|
|
17
|
+
declare function useDatasource<TName extends ConnectorSpecifier["name"]>(name: TName, options?: UseDatasourceOptions): Promise<UnwrapVariant<DatasourceVariants, ConnectorSpecifier & {
|
|
18
|
+
name: TName;
|
|
19
|
+
}>>;
|
|
17
20
|
//#endregion
|
|
18
21
|
export { UseDatasourceOptions, useDatasource };
|
|
19
22
|
//# sourceMappingURL=useDatasource.d.mts.map
|
|
@@ -15,7 +15,8 @@ async function useDatasource(name, options = {}) {
|
|
|
15
15
|
let datasourcePromise;
|
|
16
16
|
if (name in datasources) datasourcePromise = datasources[name];
|
|
17
17
|
else {
|
|
18
|
-
|
|
18
|
+
datasources[name] = createDatasource(name);
|
|
19
|
+
datasourcePromise = datasources[name];
|
|
19
20
|
const { autoClose = true } = options;
|
|
20
21
|
if (autoClose) {
|
|
21
22
|
let removeCloseHandler;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDatasource.mjs","names":[],"sources":["../../src/runtime/useDatasource.ts"],"sourcesContent":["import type { Datasources } from \".\";\nimport { createDatasource } from \"./internal/createDatasource\";\nimport { clearCachedConfig } from \"./internal/config\";\n\nimport { onServerClose } from \"#nitro-drizzle/runtime\";\n\nconst datasources: {\n [
|
|
1
|
+
{"version":3,"file":"useDatasource.mjs","names":[],"sources":["../../src/runtime/useDatasource.ts"],"sourcesContent":["import type { Datasources, DatasourceVariants } from \".\";\nimport { createDatasource } from \"./internal/createDatasource\";\nimport { clearCachedConfig } from \"./internal/config\";\n\nimport { onServerClose } from \"#nitro-drizzle/runtime\";\nimport type { ConnectorSpecifier, UnwrapVariant } from \"nitro-drizzle/shared\";\n\nconst datasources: {\n [TName in keyof Datasources & string]?: Promise<Datasources[TName][string]>;\n} = {};\n\n/** Options for datasource creation and lifecycle management. */\nexport type UseDatasourceOptions = Partial<{\n /** Whether to automatically close the datasource when Nitro app closes. */\n autoClose: boolean;\n}>;\n\n/**\n * Gets or creates a datasource instance by name.\n * Caches the datasource for reuse.\n * @template TName - The datasource name\n * @param name - The datasource name\n * @param options - Lifecycle options\n * @returns The datasource instance\n */\nexport async function useDatasource<TName extends ConnectorSpecifier[\"name\"]>(\n name: TName,\n options: UseDatasourceOptions = {},\n): Promise<UnwrapVariant<DatasourceVariants, ConnectorSpecifier & { name: TName }>> {\n let datasourcePromise: Promise<\n UnwrapVariant<DatasourceVariants, ConnectorSpecifier & { name: TName }>\n >;\n\n if (name in datasources) {\n // @ts-expect-error\n datasourcePromise = datasources[name]!;\n } else {\n datasources[name] = createDatasource(name);\n // @ts-expect-error\n datasourcePromise = datasources[name];\n const { autoClose = true } = options;\n if (autoClose) {\n let removeCloseHandler: () => void;\n removeCloseHandler = onServerClose(async () => {\n const datasource = await datasourcePromise;\n\n clearCachedConfig(name);\n cleanCachedDatasource(name);\n\n await datasource.close();\n\n removeCloseHandler();\n });\n }\n }\n\n return await datasourcePromise;\n}\n\nfunction cleanCachedDatasource<TName extends keyof Datasources & string>(name: TName) {\n delete datasources[name];\n}\n"],"mappings":";;;;AAOA,MAAM,cAEF,CAAC;;;;;;;;;AAgBL,eAAsB,cACpB,MACA,UAAgC,CAAC,GACiD;CAClF,IAAI;CAIJ,IAAI,QAAQ,aAEV,oBAAoB,YAAY;MAC3B;EACL,YAAY,QAAQ,iBAAiB,IAAI;EAEzC,oBAAoB,YAAY;EAChC,MAAM,EAAE,YAAY,SAAS;EAC7B,IAAI,WAAW;GACb,IAAI;GACJ,qBAAqB,cAAc,YAAY;IAC7C,MAAM,aAAa,MAAM;IAEzB,kBAAkB,IAAI;IACtB,sBAAsB,IAAI;IAE1B,MAAM,WAAW,MAAM;IAEvB,mBAAmB;GACrB,CAAC;EACH;CACF;CAEA,OAAO,MAAM;AACf;AAEA,SAAS,sBAAgE,MAAa;CACpF,OAAO,YAAY;AACrB"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ConnectorSpecifier, ExpandVariants, Variant } from "nitro-drizzle/shared";
|
|
2
|
+
import { DatasourceVariants, Datasources } from "nitro-drizzle/runtime";
|
|
3
|
+
import { Datasource, Schema } from "nitro-drizzle/drivers";
|
|
4
|
+
//#region src/runtime/useDialect.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Provides type-safe dialect-specific handlers for a datasource.
|
|
7
|
+
* Automatically resolves the correct handler based on the configured driver.
|
|
8
|
+
* @template TName - The datasource name
|
|
9
|
+
* @template THandlers - The dialect handlers mapping
|
|
10
|
+
* @param name - The datasource name
|
|
11
|
+
* @param handlers - An object mapping dialect names to handler functions
|
|
12
|
+
* @returns The result of the handler for the current dialect
|
|
13
|
+
*/
|
|
14
|
+
declare function useDialect<TName extends keyof Datasources & string, THandlers extends DialectHandlers<TName, DialectHandlerArgs[TName]>>(name: TName, handlers: THandlers): Promise<{ [K in keyof THandlers]: ReturnType<THandlers[K]>; }[keyof THandlers]>;
|
|
15
|
+
/**
|
|
16
|
+
* Maps dialect names to their handler argument types for a datasource.
|
|
17
|
+
* @template TName - The datasource name
|
|
18
|
+
* @template T - The datasource type
|
|
19
|
+
*/
|
|
20
|
+
type DialectHandlers<TName extends keyof Datasources & string, T> = T extends DialectHandlerArgs[TName] ? { [K in keyof T]: K extends keyof DialectHandlerArgs[TName] ? (...args: DialectHandlerArgs[TName][K]) => any : never; } : never;
|
|
21
|
+
type DialectHandlersArgsMapper<T extends Variant<Datasource<string, any, Schema>, ConnectorSpecifier>> = T extends Variant<infer V, infer S> ? Variant<[datasource: V], Pick<S, "name" | "dialect">> : never;
|
|
22
|
+
type DialectHandlerVariants = DialectHandlersArgsMapper<DatasourceVariants>;
|
|
23
|
+
type DialectHandlerArgs = ExpandVariants<DialectHandlerVariants, ["name", "dialect"]>;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { useDialect };
|
|
26
|
+
//# sourceMappingURL=useDialect.d.mts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useDatasourceProvider } from "./internal/createDatasource.mjs";
|
|
2
|
+
import { useDatasource } from "nitro-drizzle/runtime";
|
|
3
|
+
//#region src/runtime/useDialect.ts
|
|
4
|
+
/**
|
|
5
|
+
* Provides type-safe dialect-specific handlers for a datasource.
|
|
6
|
+
* Automatically resolves the correct handler based on the configured driver.
|
|
7
|
+
* @template TName - The datasource name
|
|
8
|
+
* @template THandlers - The dialect handlers mapping
|
|
9
|
+
* @param name - The datasource name
|
|
10
|
+
* @param handlers - An object mapping dialect names to handler functions
|
|
11
|
+
* @returns The result of the handler for the current dialect
|
|
12
|
+
*/
|
|
13
|
+
async function useDialect(name, handlers) {
|
|
14
|
+
const datasource = await useDatasource(name);
|
|
15
|
+
const { dialect } = useDatasourceProvider(name);
|
|
16
|
+
if (false === dialect in handlers) throw new Error(`No dialect-specific handler provided for dialect "${dialect}".`);
|
|
17
|
+
return await handlers[dialect](datasource);
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { useDialect };
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=useDialect.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDialect.mjs","names":[],"sources":["../../src/runtime/useDialect.ts"],"sourcesContent":["import type { Datasource, Schema } from \"nitro-drizzle/drivers\";\nimport { useDatasource, type Datasources, type DatasourceVariants } from \"nitro-drizzle/runtime\";\nimport type { ConnectorSpecifier, ExpandVariants, Variant } from \"nitro-drizzle/shared\";\nimport { useDatasourceProvider } from \"./internal/createDatasource\";\n\n/**\n * Provides type-safe dialect-specific handlers for a datasource.\n * Automatically resolves the correct handler based on the configured driver.\n * @template TName - The datasource name\n * @template THandlers - The dialect handlers mapping\n * @param name - The datasource name\n * @param handlers - An object mapping dialect names to handler functions\n * @returns The result of the handler for the current dialect\n */\nexport async function useDialect<\n TName extends keyof Datasources & string,\n THandlers extends DialectHandlers<TName, DialectHandlerArgs[TName]>,\n>(\n name: TName,\n handlers: THandlers,\n): Promise<{ [K in keyof THandlers]: ReturnType<THandlers[K]> }[keyof THandlers]> {\n const datasource = await useDatasource(name);\n const { dialect } = useDatasourceProvider(name);\n if (false === dialect in handlers) {\n throw new Error(`No dialect-specific handler provided for dialect \"${dialect}\".`);\n }\n return await handlers[dialect](datasource);\n}\n\n/**\n * Maps dialect names to their handler argument types for a datasource.\n * @template TName - The datasource name\n * @template T - The datasource type\n */\ntype DialectHandlers<\n TName extends keyof Datasources & string,\n T,\n> = T extends DialectHandlerArgs[TName]\n ? {\n [K in keyof T]: K extends keyof DialectHandlerArgs[TName]\n ? (...args: DialectHandlerArgs[TName][K]) => any\n : never;\n }\n : never;\n\ntype DialectHandlersArgsMapper<\n T extends Variant<Datasource<string, any, Schema>, ConnectorSpecifier>,\n> =\n T extends Variant<infer V, infer S>\n ? Variant<[datasource: V], Pick<S, \"name\" | \"dialect\">>\n : never;\n\ntype DialectHandlerVariants = DialectHandlersArgsMapper<DatasourceVariants>;\n\ntype DialectHandlerArgs = ExpandVariants<DialectHandlerVariants, [\"name\", \"dialect\"]>;\n"],"mappings":";;;;;;;;;;;;AAcA,eAAsB,WAIpB,MACA,UACgF;CAChF,MAAM,aAAa,MAAM,cAAc,IAAI;CAC3C,MAAM,EAAE,YAAY,sBAAsB,IAAI;CAC9C,IAAI,UAAU,WAAW,UACvB,MAAM,IAAI,MAAM,qDAAqD,QAAQ,GAAG;CAElF,OAAO,MAAM,SAAS,QAAQ,CAAC,UAAU;AAC3C"}
|