nitro-drizzle 0.2.15 → 0.3.0-beta.1
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/context/internal/async.mjs.map +1 -1
- package/dist/context/internal/virtual.mjs +40 -7
- package/dist/context/internal/virtual.mjs.map +1 -1
- package/dist/context.d.mts +3 -2
- package/dist/context.mjs +16 -8
- package/dist/context.mjs.map +1 -1
- package/dist/meta.d.mts +1 -1
- package/dist/meta.mjs +2 -1
- package/dist/meta.mjs.map +1 -1
- package/dist/middleware/context.d.mts +23 -0
- package/dist/middleware/context.mjs +30 -0
- package/dist/middleware/context.mjs.map +1 -0
- package/dist/migrations/task.d.mts +4 -2
- package/dist/migrations/task.mjs +3 -4
- package/dist/migrations/task.mjs.map +1 -1
- package/dist/migrations/useMigrations.mjs +3 -12
- package/dist/migrations/useMigrations.mjs.map +1 -1
- package/dist/module/internal/reload.mjs.map +1 -1
- package/dist/module/internal/rollup.mjs.map +1 -1
- package/dist/module/internal/types.mjs +11 -9
- package/dist/module/internal/types.mjs.map +1 -1
- package/dist/module/utils/assets.d.mts +2 -1
- package/dist/module/utils/assets.mjs.map +1 -1
- package/dist/module/utils/externals.d.mts +1 -0
- package/dist/module/utils/externals.mjs +6 -1
- package/dist/module/utils/externals.mjs.map +1 -1
- package/dist/module/utils/nitro.d.mts +7 -0
- package/dist/module/utils/nitro.mjs +8 -0
- package/dist/module/utils/nitro.mjs.map +1 -0
- package/dist/module.d.mts +10 -3
- package/dist/module.mjs +26 -10
- package/dist/module.mjs.map +1 -1
- package/dist/plugins/init.d.mts +3 -12
- package/dist/plugins/init.mjs +3 -23
- package/dist/plugins/init.mjs.map +1 -1
- package/dist/plugins/legacy/init.d.mts +24 -0
- package/dist/plugins/legacy/init.mjs +33 -0
- package/dist/plugins/legacy/init.mjs.map +1 -0
- package/dist/plugins/legacy/migrate.d.mts +21 -0
- package/dist/plugins/legacy/migrate.mjs +25 -0
- package/dist/plugins/legacy/migrate.mjs.map +1 -0
- package/dist/plugins/migrate.d.mts +2 -2
- package/dist/plugins/migrate.mjs +9 -4
- package/dist/plugins/migrate.mjs.map +1 -1
- package/dist/runtime/internal/config.mjs +3 -3
- package/dist/runtime/internal/config.mjs.map +1 -1
- package/dist/runtime/useDatasource.mjs +11 -7
- package/dist/runtime/useDatasource.mjs.map +1 -1
- package/dist/runtime.d.mts +9 -1
- package/package.json +6 -2
|
@@ -0,0 +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":";AAGA,SAAgB,SAAS,OAAkD;CACzE,OAAO,MAAM,KAAK,eAAe;AACnC"}
|
package/dist/module.d.mts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { updateServerAssets } from "./module/utils/assets.mjs";
|
|
2
2
|
import { addInlineExternals } from "./module/utils/externals.mjs";
|
|
3
|
+
import { isLegacy } from "./module/utils/nitro.mjs";
|
|
3
4
|
import { ConfigPattern, MigrationOptions } from "nitro-drizzle/context";
|
|
4
5
|
import { NitroModule } from "nitropack/types";
|
|
6
|
+
import { NitroModule as NitroModule$1 } from "nitro/types";
|
|
5
7
|
//#region src/module/index.d.ts
|
|
6
8
|
/**
|
|
7
9
|
* Datasource-specific configuration options.
|
|
@@ -13,6 +15,11 @@ declare module "nitropack/types" {
|
|
|
13
15
|
drizzle?: ModuleOptions;
|
|
14
16
|
}
|
|
15
17
|
}
|
|
18
|
+
declare module "nitro/types" {
|
|
19
|
+
interface NitroOptions {
|
|
20
|
+
drizzle?: ModuleOptions;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
16
23
|
interface ModuleOptions {
|
|
17
24
|
/**
|
|
18
25
|
* Directory to search datasources
|
|
@@ -32,7 +39,7 @@ type ModuleConfig = Required<ModuleOptions> & {
|
|
|
32
39
|
migrations: false | Required<MigrationOptions>;
|
|
33
40
|
};
|
|
34
41
|
declare function createDefaultOptions(): {
|
|
35
|
-
readonly baseDir: "
|
|
42
|
+
readonly baseDir: "./drizzle";
|
|
36
43
|
readonly configPattern: readonly ["drizzle.config.{js,ts}", "drizzle-*.config.{js,ts}"];
|
|
37
44
|
readonly datasources: {};
|
|
38
45
|
readonly migrations: {
|
|
@@ -41,7 +48,7 @@ declare function createDefaultOptions(): {
|
|
|
41
48
|
};
|
|
42
49
|
};
|
|
43
50
|
declare function defineModuleConfig(options?: ModuleOptions): ModuleConfig;
|
|
44
|
-
declare const module: NitroModule;
|
|
51
|
+
declare const module: NitroModule & NitroModule$1;
|
|
45
52
|
//#endregion
|
|
46
|
-
export { DatasourceOptions, ModuleConfig, ModuleOptions, addInlineExternals, createDefaultOptions, module as default, defineModuleConfig, updateServerAssets };
|
|
53
|
+
export { DatasourceOptions, ModuleConfig, ModuleOptions, addInlineExternals, createDefaultOptions, module as default, defineModuleConfig, isLegacy as isLegacyNitro, updateServerAssets };
|
|
47
54
|
//# sourceMappingURL=module.d.mts.map
|
package/dist/module.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { updateServerAssets } from "./module/utils/assets.mjs";
|
|
2
|
-
import { addInlineExternals } from "./module/utils/externals.mjs";
|
|
2
|
+
import { addInlineExternals, addNoExternals } from "./module/utils/externals.mjs";
|
|
3
|
+
import { isLegacy } from "./module/utils/nitro.mjs";
|
|
3
4
|
import { addAugmentations, addDeclarations } from "./module/internal/types.mjs";
|
|
4
5
|
import { createResolver } from "./module/internal/resolver.mjs";
|
|
5
6
|
import { reloadPlugin } from "./module/internal/reload.mjs";
|
|
@@ -12,7 +13,7 @@ import { pkgName } from "nitro-drizzle/meta";
|
|
|
12
13
|
//#region src/module/index.ts
|
|
13
14
|
function createDefaultOptions() {
|
|
14
15
|
return {
|
|
15
|
-
baseDir: "
|
|
16
|
+
baseDir: "./drizzle",
|
|
16
17
|
configPattern: ["drizzle.config.{js,ts}", "drizzle-*.config.{js,ts}"],
|
|
17
18
|
datasources: {},
|
|
18
19
|
migrations: {
|
|
@@ -29,39 +30,54 @@ const module = {
|
|
|
29
30
|
async setup(nitro) {
|
|
30
31
|
const moduleConfig = defineModuleConfig(nitro.options.drizzle);
|
|
31
32
|
const resolver = createResolver(nitro.options.rootDir, { alias: nitro.options.alias });
|
|
32
|
-
const
|
|
33
|
+
const serverDir = isLegacy(nitro) ? nitro.options.srcDir : nitro.options.serverDir;
|
|
34
|
+
if (!serverDir) {
|
|
35
|
+
nitro.logger.info("No server directory configured.");
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const baseDir = resolve(serverDir, resolveAlias(moduleConfig.baseDir, nitro.options.alias));
|
|
39
|
+
if (!isLegacy(nitro)) {
|
|
40
|
+
nitro.options.handlers ||= [];
|
|
41
|
+
nitro.options.handlers.push({
|
|
42
|
+
route: "/**",
|
|
43
|
+
handler: resolver.resolve("nitro-drizzle/middleware/context"),
|
|
44
|
+
middleware: true
|
|
45
|
+
});
|
|
46
|
+
}
|
|
33
47
|
const context = createContext({
|
|
48
|
+
legacy: isLegacy(nitro),
|
|
34
49
|
cwd: process.cwd(),
|
|
35
50
|
resolver,
|
|
36
51
|
baseDir,
|
|
37
52
|
logger: nitro.logger,
|
|
38
53
|
configPattern: moduleConfig.configPattern,
|
|
39
|
-
|
|
54
|
+
datasources: { ...moduleConfig.datasources },
|
|
40
55
|
migrations: moduleConfig.migrations || void 0,
|
|
41
56
|
tasks: nitro.options.experimental.tasks ? (tasks) => {
|
|
42
57
|
nitro.options.tasks ||= {};
|
|
43
58
|
Object.assign(nitro.options.tasks, tasks);
|
|
44
59
|
} : void 0,
|
|
45
60
|
plugins(plugins) {
|
|
46
|
-
nitro.options.plugins.push(...plugins);
|
|
61
|
+
nitro.options.plugins.push(...plugins.map((plugin) => resolver.resolve(plugin).replace(/\.mjs$/, "")));
|
|
47
62
|
},
|
|
48
63
|
virtualModules(modules) {
|
|
49
64
|
Object.assign(nitro.options.virtual, modules);
|
|
50
65
|
},
|
|
51
66
|
declarations(declarations) {
|
|
52
67
|
nitro.hooks.hook("types:extend", async (types) => {
|
|
53
|
-
await addAugmentations(nitro
|
|
68
|
+
await addAugmentations(nitro, types, {
|
|
54
69
|
...declarations.runtime,
|
|
55
70
|
...declarations.module
|
|
56
71
|
});
|
|
57
|
-
await addDeclarations(nitro
|
|
72
|
+
await addDeclarations(nitro, types, declarations.virtual);
|
|
58
73
|
});
|
|
59
74
|
},
|
|
60
75
|
assets(assets) {
|
|
61
76
|
updateServerAssets(nitro.options, assets);
|
|
62
77
|
},
|
|
63
|
-
|
|
64
|
-
addInlineExternals(nitro.options, modules);
|
|
78
|
+
externals(modules) {
|
|
79
|
+
if (isLegacy(nitro)) addInlineExternals(nitro.options, modules);
|
|
80
|
+
else addNoExternals(nitro.options, modules);
|
|
65
81
|
}
|
|
66
82
|
});
|
|
67
83
|
if (nitro.options.imports) nitro.options.imports.imports ||= [];
|
|
@@ -72,6 +88,6 @@ const module = {
|
|
|
72
88
|
}
|
|
73
89
|
};
|
|
74
90
|
//#endregion
|
|
75
|
-
export { addInlineExternals, createDefaultOptions, module as default, defineModuleConfig, updateServerAssets };
|
|
91
|
+
export { addInlineExternals, createDefaultOptions, module as default, defineModuleConfig, isLegacy as isLegacyNitro, updateServerAssets };
|
|
76
92
|
|
|
77
93
|
//# sourceMappingURL=module.mjs.map
|
package/dist/module.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module.mjs","names":[],"sources":["../src/module/index.ts"],"sourcesContent":["import type { NitroModule } from \"nitropack/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 { VirtualModules, MaybePromise } from \"nitro-drizzle/shared\";\nimport { pkgName } from \"nitro-drizzle/meta\";\n\nimport { updateServerAssets } from \"./utils/assets\";\nimport { addInlineExternals } 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\";\n\n/**\n * Datasource-specific configuration options.\n * This interface is augmented by the module with connector-specific options.\n */\nexport interface DatasourceOptions {\n // augmented by module\n}\n\ndeclare module \"nitropack/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\nexport type ModuleConfig = Required<ModuleOptions> & {\n migrations: false | Required<MigrationOptions>;\n};\n\nexport function createDefaultOptions() {\n return {\n baseDir: \"
|
|
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 { VirtualModules, MaybePromise } from \"nitro-drizzle/shared\";\nimport { pkgName } from \"nitro-drizzle/meta\";\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 * This interface is augmented by the module with connector-specific options.\n */\nexport interface DatasourceOptions {\n // augmented by module\n}\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\nexport type ModuleConfig = Required<ModuleOptions> & {\n migrations: false | Required<MigrationOptions>;\n};\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\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 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 nitro.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: nitro.logger,\n configPattern: moduleConfig.configPattern,\n datasources: { ...moduleConfig.datasources },\n migrations: moduleConfig.migrations || void 0,\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.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":";;;;;;;;;;;;;AAkEA,SAAgB,uBAAuB;CACrC,OAAO;EACL,SAAS;EACT,eAAe,CAAC,0BAA0B,0BAA0B;EACpE,aAAa,CAAC;EACd,YAAY;GACV,aAAa;GACb,eAAe;EACjB;CACF;AACF;AAEA,SAAgB,mBAAmB,SAAuC;CACxE,OAAO,KAAK,SAAS,qBAAqB,CAAC;AAC7C;AAEA,MAAM,SAA0C;CAC9C,MAAM;CACN,MAAM,MAAM,OAAO;EACjB,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,MAAM,OAAO,KAAK,iCAAiC;GACnD;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;EAqDA,MAAM,UAAU,cAAc;GAlD5B,QAAQ,SAAS,KAAK;GACtB,KAAK,QAAQ,IAAI;GACP;GACV;GACA,QAAQ,MAAM;GACd,eAAe,aAAa;GAC5B,aAAa,EAAE,GAAG,aAAa,YAAY;GAC3C,YAAY,aAAa,cAAc,KAAK;GAE5C,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;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"}
|
package/dist/plugins/init.d.mts
CHANGED
|
@@ -3,22 +3,13 @@
|
|
|
3
3
|
* Nitro plugin that triggers the 'drizzle:init' hook during server initialization.
|
|
4
4
|
* Used to run migration plugins at startup.
|
|
5
5
|
*/
|
|
6
|
-
declare const _default: import("
|
|
7
|
-
type ReadyState = "pending" | "done" | "error";
|
|
6
|
+
declare const _default: import("nitro/types").NitroAppPlugin;
|
|
8
7
|
type InitHooks = {
|
|
9
8
|
"drizzle:init": () => void;
|
|
10
9
|
};
|
|
11
|
-
declare module "
|
|
10
|
+
declare module "nitro/types" {
|
|
12
11
|
interface NitroRuntimeHooks extends InitHooks {}
|
|
13
12
|
}
|
|
14
|
-
declare module "h3" {
|
|
15
|
-
interface H3EventContext {
|
|
16
|
-
drizzle: {
|
|
17
|
-
readyState: ReadyState;
|
|
18
|
-
waitReady: () => Promise<void>;
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
13
|
//#endregion
|
|
23
|
-
export { InitHooks,
|
|
14
|
+
export { InitHooks, _default as default };
|
|
24
15
|
//# sourceMappingURL=init.d.mts.map
|
package/dist/plugins/init.mjs
CHANGED
|
@@ -1,31 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { definePlugin } from "nitro";
|
|
2
2
|
//#region src/plugins/init.ts
|
|
3
3
|
/**
|
|
4
4
|
* Nitro plugin that triggers the 'drizzle:init' hook during server initialization.
|
|
5
5
|
* Used to run migration plugins at startup.
|
|
6
6
|
*/
|
|
7
|
-
var init_default =
|
|
8
|
-
|
|
9
|
-
let readyState = "pending";
|
|
10
|
-
const waitReady = async () => {
|
|
11
|
-
await initHook;
|
|
12
|
-
};
|
|
13
|
-
nitro.hooks.hook("request", (event) => {
|
|
14
|
-
event.context.drizzle = {
|
|
15
|
-
readyState,
|
|
16
|
-
waitReady
|
|
17
|
-
};
|
|
18
|
-
});
|
|
19
|
-
initHook = new Promise((resolve, reject) => {
|
|
20
|
-
nitro.hooks.callHook("drizzle:init").then(() => {
|
|
21
|
-
readyState = "done";
|
|
22
|
-
resolve(readyState);
|
|
23
|
-
}).catch((err) => {
|
|
24
|
-
readyState = "error";
|
|
25
|
-
reject(err);
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
await initHook;
|
|
7
|
+
var init_default = definePlugin(async (nitro) => {
|
|
8
|
+
await nitro.hooks.callHook("drizzle:init");
|
|
29
9
|
});
|
|
30
10
|
//#endregion
|
|
31
11
|
export { init_default as default };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.mjs","names":[],"sources":["../../src/plugins/init.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"init.mjs","names":[],"sources":["../../src/plugins/init.ts"],"sourcesContent":["import { definePlugin } from \"nitro\";\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 definePlugin(async (nitro) => {\n await nitro.hooks.callHook(\"drizzle:init\");\n});\n\nexport type InitHooks = {\n \"drizzle:init\": () => void;\n};\n\ndeclare module \"nitro/types\" {\n interface NitroRuntimeHooks extends InitHooks {}\n}\n"],"mappings":";;;;;;AAMA,IAAA,eAAe,aAAa,OAAO,UAAU;CAC3C,MAAM,MAAM,MAAM,SAAS,cAAc;AAC3C,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/plugins/legacy/init.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Nitro plugin that triggers the 'drizzle:init' hook during server initialization.
|
|
4
|
+
* Used to run migration plugins at startup.
|
|
5
|
+
*/
|
|
6
|
+
declare const _default: import("nitropack").NitroAppPlugin;
|
|
7
|
+
type ReadyState = "pending" | "done" | "error";
|
|
8
|
+
type InitHooks = {
|
|
9
|
+
"drizzle:init": () => void;
|
|
10
|
+
};
|
|
11
|
+
declare module "nitropack/types" {
|
|
12
|
+
interface NitroRuntimeHooks extends InitHooks {}
|
|
13
|
+
}
|
|
14
|
+
declare module "h3" {
|
|
15
|
+
interface H3EventContext {
|
|
16
|
+
drizzle: {
|
|
17
|
+
readyState: ReadyState;
|
|
18
|
+
waitReady: () => Promise<void>;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { InitHooks, ReadyState, _default as default };
|
|
24
|
+
//# sourceMappingURL=init.d.mts.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { defineNitroPlugin } from "nitropack/runtime";
|
|
2
|
+
//#region src/plugins/legacy/init.ts
|
|
3
|
+
/**
|
|
4
|
+
* Nitro plugin that triggers the 'drizzle:init' hook during server initialization.
|
|
5
|
+
* Used to run migration plugins at startup.
|
|
6
|
+
*/
|
|
7
|
+
var init_default = defineNitroPlugin(async (nitro) => {
|
|
8
|
+
let initHook;
|
|
9
|
+
let readyState = "pending";
|
|
10
|
+
const waitReady = async () => {
|
|
11
|
+
await initHook;
|
|
12
|
+
};
|
|
13
|
+
nitro.hooks.hook("request", (event) => {
|
|
14
|
+
event.context.drizzle = {
|
|
15
|
+
readyState,
|
|
16
|
+
waitReady
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
initHook = new Promise((resolve, reject) => {
|
|
20
|
+
nitro.hooks.callHook("drizzle:init").then(() => {
|
|
21
|
+
readyState = "done";
|
|
22
|
+
resolve(readyState);
|
|
23
|
+
}).catch((err) => {
|
|
24
|
+
readyState = "error";
|
|
25
|
+
reject(err);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
await initHook;
|
|
29
|
+
});
|
|
30
|
+
//#endregion
|
|
31
|
+
export { init_default as default };
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=init.mjs.map
|
|
@@ -0,0 +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 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 await initHook;\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":";;;;;;AAMA,IAAA,eAAe,kBAAkB,OAAO,UAAU;CAChD,IAAI;CACJ,IAAI,aAAyB;CAE7B,MAAM,YAAY,YAAY;EAC5B,MAAM;CACR;CAEA,MAAM,MAAM,KAAK,YAAY,UAAU;EACrC,MAAM,QAAQ,UAAU;GACtB;GACA;EACF;CACF,CAAC;CAED,WAAW,IAAI,SAAS,SAAS,WAAW;EAC1C,MAAM,MACH,SAAS,cAAc,CAAC,CACxB,WAAW;GACV,aAAa;GACb,QAAQ,UAAU;EACpB,CAAC,CAAC,CACD,OAAO,QAAQ;GACd,aAAa;GACb,OAAO,GAAG;EACZ,CAAC;CACL,CAAC;CAED,MAAM;AACR,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { DatasourceRegistry } from "nitro-drizzle/runtime";
|
|
2
|
+
import { MigrationResult } from "nitro-drizzle/migrations";
|
|
3
|
+
import { NitroAppPlugin } from "nitropack/types";
|
|
4
|
+
//#region src/plugins/legacy/migrate.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Nitro plugin that runs migrations on initialization.
|
|
7
|
+
* Registers hooks to run migrations for datasources configured with migrateOnInit.
|
|
8
|
+
*/
|
|
9
|
+
declare const plugin: NitroAppPlugin;
|
|
10
|
+
/** Type for migration hooks registered for each datasource. */
|
|
11
|
+
interface MigrationHooks {
|
|
12
|
+
"drizzle:migrate:before": (datasource: keyof DatasourceRegistry & string) => Promise<void> | void;
|
|
13
|
+
"drizzle:migrate": (datasource: keyof DatasourceRegistry & string) => Promise<void> | void;
|
|
14
|
+
"drizzle:migrate:after": (datasource: keyof DatasourceRegistry & string, result: MigrationResult) => Promise<void> | void;
|
|
15
|
+
}
|
|
16
|
+
declare module "nitropack/types" {
|
|
17
|
+
interface NitroRuntimeHooks extends MigrationHooks {}
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { MigrationHooks, plugin as default };
|
|
21
|
+
//# sourceMappingURL=migrate.d.mts.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { MIGRATE_ON_INIT } from "#nitro-drizzle/migrations";
|
|
2
|
+
import { defineNitroPlugin } from "nitropack/runtime";
|
|
3
|
+
import { migrate } from "nitro-drizzle/migrations";
|
|
4
|
+
//#region src/plugins/legacy/migrate.ts
|
|
5
|
+
const MIGRATION_HOOK_KEY = "drizzle:migrate";
|
|
6
|
+
/**
|
|
7
|
+
* Nitro plugin that runs migrations on initialization.
|
|
8
|
+
* Registers hooks to run migrations for datasources configured with migrateOnInit.
|
|
9
|
+
*/
|
|
10
|
+
const plugin = defineNitroPlugin(async (nitro) => {
|
|
11
|
+
nitro.hooks.hook(MIGRATION_HOOK_KEY, async (name) => {
|
|
12
|
+
await nitro.hooks.callHook(`${MIGRATION_HOOK_KEY}:before`, name);
|
|
13
|
+
const result = await migrate(name);
|
|
14
|
+
await nitro.hooks.callHook(`${MIGRATION_HOOK_KEY}:after`, name, result);
|
|
15
|
+
});
|
|
16
|
+
nitro.hooks.hookOnce("drizzle:init", async () => {
|
|
17
|
+
await Promise.all(MIGRATE_ON_INIT.map((name) => {
|
|
18
|
+
return nitro.hooks.callHookParallel(MIGRATION_HOOK_KEY, name);
|
|
19
|
+
}));
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
//#endregion
|
|
23
|
+
export { plugin as default };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=migrate.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate.mjs","names":[],"sources":["../../../src/plugins/legacy/migrate.ts"],"sourcesContent":["import { defineNitroPlugin } from \"nitropack/runtime\";\nimport type { NitroAppPlugin } from \"nitropack/types\";\nimport { migrate, type MigrationResult } from \"nitro-drizzle/migrations\";\n\nimport { MIGRATE_ON_INIT } from \"#nitro-drizzle/migrations\";\nimport type { DatasourceRegistry } from \"nitro-drizzle/runtime\";\n\nconst MIGRATION_HOOK_KEY = \"drizzle:migrate\" as const;\n\n/**\n * Nitro plugin that runs migrations on initialization.\n * Registers hooks to run migrations for datasources configured with migrateOnInit.\n */\nconst plugin: NitroAppPlugin = defineNitroPlugin(async (nitro) => {\n nitro.hooks.hook(MIGRATION_HOOK_KEY, async (name) => {\n await nitro.hooks.callHook(`${MIGRATION_HOOK_KEY}:before`, name);\n const result = await migrate(name);\n await nitro.hooks.callHook(`${MIGRATION_HOOK_KEY}:after`, name, result);\n });\n\n nitro.hooks.hookOnce(\"drizzle:init\", async () => {\n await Promise.all(\n MIGRATE_ON_INIT.map((name) => {\n return nitro.hooks.callHookParallel(MIGRATION_HOOK_KEY, name);\n }),\n );\n });\n});\n\nexport default plugin;\n\n/** Type for migration hooks registered for each datasource. */\nexport interface MigrationHooks {\n \"drizzle:migrate:before\": (datasource: keyof DatasourceRegistry & string) => Promise<void> | void;\n \"drizzle:migrate\": (datasource: keyof DatasourceRegistry & string) => Promise<void> | void;\n \"drizzle:migrate:after\": (\n datasource: keyof DatasourceRegistry & string,\n result: MigrationResult,\n ) => Promise<void> | void;\n}\n\ndeclare module \"nitropack/types\" {\n interface NitroRuntimeHooks extends MigrationHooks {}\n}\n"],"mappings":";;;;AAOA,MAAM,qBAAqB;;;;;AAM3B,MAAM,SAAyB,kBAAkB,OAAO,UAAU;CAChE,MAAM,MAAM,KAAK,oBAAoB,OAAO,SAAS;EACnD,MAAM,MAAM,MAAM,SAAS,GAAG,mBAAmB,UAAU,IAAI;EAC/D,MAAM,SAAS,MAAM,QAAQ,IAAI;EACjC,MAAM,MAAM,MAAM,SAAS,GAAG,mBAAmB,SAAS,MAAM,MAAM;CACxE,CAAC;CAED,MAAM,MAAM,SAAS,gBAAgB,YAAY;EAC/C,MAAM,QAAQ,IACZ,gBAAgB,KAAK,SAAS;GAC5B,OAAO,MAAM,MAAM,iBAAiB,oBAAoB,IAAI;EAC9D,CAAC,CACH;CACF,CAAC;AACH,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DatasourceRegistry } from "nitro-drizzle/runtime";
|
|
2
2
|
import { MigrationResult } from "nitro-drizzle/migrations";
|
|
3
|
-
import { NitroAppPlugin } from "
|
|
3
|
+
import { NitroAppPlugin } from "nitro/types";
|
|
4
4
|
//#region src/plugins/migrate.d.ts
|
|
5
5
|
/**
|
|
6
6
|
* Nitro plugin that runs migrations on initialization.
|
|
@@ -13,7 +13,7 @@ interface MigrationHooks {
|
|
|
13
13
|
"drizzle:migrate": (datasource: keyof DatasourceRegistry & string) => Promise<void> | void;
|
|
14
14
|
"drizzle:migrate:after": (datasource: keyof DatasourceRegistry & string, result: MigrationResult) => Promise<void> | void;
|
|
15
15
|
}
|
|
16
|
-
declare module "
|
|
16
|
+
declare module "nitro/types" {
|
|
17
17
|
interface NitroRuntimeHooks extends MigrationHooks {}
|
|
18
18
|
}
|
|
19
19
|
//#endregion
|
package/dist/plugins/migrate.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { defineNitroPlugin } from "nitropack/runtime";
|
|
2
1
|
import { MIGRATE_ON_INIT } from "#nitro-drizzle/migrations";
|
|
2
|
+
import { definePlugin } from "nitro";
|
|
3
3
|
import { migrate } from "nitro-drizzle/migrations";
|
|
4
4
|
//#region src/plugins/migrate.ts
|
|
5
5
|
const MIGRATION_HOOK_KEY = "drizzle:migrate";
|
|
@@ -7,15 +7,20 @@ const MIGRATION_HOOK_KEY = "drizzle:migrate";
|
|
|
7
7
|
* Nitro plugin that runs migrations on initialization.
|
|
8
8
|
* Registers hooks to run migrations for datasources configured with migrateOnInit.
|
|
9
9
|
*/
|
|
10
|
-
const plugin =
|
|
10
|
+
const plugin = definePlugin(async (nitro) => {
|
|
11
11
|
nitro.hooks.hook(MIGRATION_HOOK_KEY, async (name) => {
|
|
12
12
|
await nitro.hooks.callHook(`${MIGRATION_HOOK_KEY}:before`, name);
|
|
13
13
|
const result = await migrate(name);
|
|
14
14
|
await nitro.hooks.callHook(`${MIGRATION_HOOK_KEY}:after`, name, result);
|
|
15
15
|
});
|
|
16
|
-
|
|
16
|
+
let hookCalled = false;
|
|
17
|
+
let removeHook;
|
|
18
|
+
removeHook = nitro.hooks.hook("drizzle:init", async () => {
|
|
19
|
+
if (hookCalled) return;
|
|
20
|
+
hookCalled = true;
|
|
21
|
+
removeHook();
|
|
17
22
|
await Promise.all(MIGRATE_ON_INIT.map((name) => {
|
|
18
|
-
return nitro.hooks.
|
|
23
|
+
return nitro.hooks.callHook(MIGRATION_HOOK_KEY, name);
|
|
19
24
|
}));
|
|
20
25
|
});
|
|
21
26
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrate.mjs","names":[],"sources":["../../src/plugins/migrate.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"migrate.mjs","names":[],"sources":["../../src/plugins/migrate.ts"],"sourcesContent":["import { definePlugin } from \"nitro\";\nimport type { NitroAppPlugin } from \"nitro/types\";\nimport { migrate, type MigrationResult } from \"nitro-drizzle/migrations\";\n\nimport { MIGRATE_ON_INIT } from \"#nitro-drizzle/migrations\";\nimport type { DatasourceRegistry } from \"nitro-drizzle/runtime\";\n\nconst MIGRATION_HOOK_KEY = \"drizzle:migrate\" as const;\n\n/**\n * Nitro plugin that runs migrations on initialization.\n * Registers hooks to run migrations for datasources configured with migrateOnInit.\n */\nconst plugin: NitroAppPlugin = definePlugin(async (nitro) => {\n nitro.hooks.hook(MIGRATION_HOOK_KEY, async (name) => {\n await nitro.hooks.callHook(`${MIGRATION_HOOK_KEY}:before`, name);\n const result = await migrate(name);\n await nitro.hooks.callHook(`${MIGRATION_HOOK_KEY}:after`, name, result);\n });\n\n let hookCalled = false;\n let removeHook: () => void;\n removeHook = nitro.hooks.hook(\"drizzle:init\", async () => {\n if (hookCalled) return;\n hookCalled = true;\n removeHook();\n await Promise.all(\n MIGRATE_ON_INIT.map((name) => {\n return nitro.hooks.callHook(MIGRATION_HOOK_KEY, name);\n }),\n );\n });\n});\n\nexport default plugin;\n\n/** Type for migration hooks registered for each datasource. */\nexport interface MigrationHooks {\n \"drizzle:migrate:before\": (datasource: keyof DatasourceRegistry & string) => Promise<void> | void;\n \"drizzle:migrate\": (datasource: keyof DatasourceRegistry & string) => Promise<void> | void;\n \"drizzle:migrate:after\": (\n datasource: keyof DatasourceRegistry & string,\n result: MigrationResult,\n ) => Promise<void> | void;\n}\n\ndeclare module \"nitro/types\" {\n interface NitroRuntimeHooks extends MigrationHooks {}\n}\n"],"mappings":";;;;AAOA,MAAM,qBAAqB;;;;;AAM3B,MAAM,SAAyB,aAAa,OAAO,UAAU;CAC3D,MAAM,MAAM,KAAK,oBAAoB,OAAO,SAAS;EACnD,MAAM,MAAM,MAAM,SAAS,GAAG,mBAAmB,UAAU,IAAI;EAC/D,MAAM,SAAS,MAAM,QAAQ,IAAI;EACjC,MAAM,MAAM,MAAM,SAAS,GAAG,mBAAmB,SAAS,MAAM,MAAM;CACxE,CAAC;CAED,IAAI,aAAa;CACjB,IAAI;CACJ,aAAa,MAAM,MAAM,KAAK,gBAAgB,YAAY;EACxD,IAAI,YAAY;EAChB,aAAa;EACb,WAAW;EACX,MAAM,QAAQ,IACZ,gBAAgB,KAAK,SAAS;GAC5B,OAAO,MAAM,MAAM,SAAS,oBAAoB,IAAI;EACtD,CAAC,CACH;CACF,CAAC;AACH,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { defu } from "defu";
|
|
2
|
-
import {
|
|
2
|
+
import { callConfigHook, useRuntimeConfig } from "#nitro-drizzle/runtime";
|
|
3
3
|
//#region src/runtime/internal/config.ts
|
|
4
4
|
const datasourceConfig = {};
|
|
5
5
|
/**
|
|
@@ -9,8 +9,8 @@ async function getCachedDatasourceConfig(name) {
|
|
|
9
9
|
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
10
|
else {
|
|
11
11
|
datasourceConfig[name] = void 0;
|
|
12
|
-
const config = defu(useRuntimeConfig()
|
|
13
|
-
await
|
|
12
|
+
const config = defu(useRuntimeConfig()?.[name], {});
|
|
13
|
+
await callConfigHook(name, config);
|
|
14
14
|
datasourceConfig[name] = config;
|
|
15
15
|
}
|
|
16
16
|
return datasourceConfig[name];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.mjs","names":[],"sources":["../../../src/runtime/internal/config.ts"],"sourcesContent":["import { defu } from \"defu\";\nimport {
|
|
1
|
+
{"version":3,"file":"config.mjs","names":[],"sources":["../../../src/runtime/internal/config.ts"],"sourcesContent":["import { defu } from \"defu\";\nimport type { Datasources, DatasourceConfig } from \"..\";\n\nimport { callConfigHook, useRuntimeConfig } from \"#nitro-drizzle/runtime\";\n\nconst datasourceConfig: Partial<DatasourceConfig> = {};\n\n/**\n * @internal\n */\nexport async function getCachedDatasourceConfig<TName extends keyof Datasources & string>(\n name: TName,\n) {\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 runtimeConfig = useRuntimeConfig();\n const config = defu(runtimeConfig?.[name], {});\n await callConfigHook(name, config);\n\n datasourceConfig[name] = config;\n }\n\n return datasourceConfig[name];\n}\n\nexport function clearCachedConfig<TName extends keyof Datasources>(name: TName) {\n delete datasourceConfig[name];\n}\n"],"mappings":";;;AAKA,MAAM,mBAA8C,CAAC;;;;AAKrD,eAAsB,0BACpB,MACA;CACA,IAAI,QAAQ,oBAAoB,CAAC,iBAAiB,OAChD,MAAM,IAAI,MACR,4FACF;MACK;EACL,iBAAiB,QAAQ,KAAA;EAGzB,MAAM,SAAS,KADO,iBACU,CAAC,GAAG,OAAO,CAAC,CAAC;EAC7C,MAAM,eAAe,MAAM,MAAM;EAEjC,iBAAiB,QAAQ;CAC3B;CAEA,OAAO,iBAAiB;AAC1B;AAEA,SAAgB,kBAAmD,MAAa;CAC9E,OAAO,iBAAiB;AAC1B"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { clearCachedConfig } from "./internal/config.mjs";
|
|
2
2
|
import { createDatasource } from "./internal/createDatasource.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { onServerClose } from "#nitro-drizzle/runtime";
|
|
4
4
|
//#region src/runtime/useDatasource.ts
|
|
5
5
|
const datasources = {};
|
|
6
6
|
/**
|
|
@@ -17,12 +17,16 @@ async function useDatasource(name, options = {}) {
|
|
|
17
17
|
else {
|
|
18
18
|
datasourcePromise = datasources[name] = createDatasource(name);
|
|
19
19
|
const { autoClose = true } = options;
|
|
20
|
-
if (autoClose)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
if (autoClose) {
|
|
21
|
+
let removeCloseHandler;
|
|
22
|
+
removeCloseHandler = onServerClose(async () => {
|
|
23
|
+
const datasource = await datasourcePromise;
|
|
24
|
+
clearCachedConfig(name);
|
|
25
|
+
cleanCachedDatasource(name);
|
|
26
|
+
await datasource.close();
|
|
27
|
+
removeCloseHandler();
|
|
28
|
+
});
|
|
29
|
+
}
|
|
26
30
|
}
|
|
27
31
|
return await datasourcePromise;
|
|
28
32
|
}
|
|
@@ -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 {
|
|
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 [K in keyof Datasources & string]?: Promise<Datasources[K]>;\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 keyof Datasources & string>(\n name: TName,\n options: UseDatasourceOptions = {},\n): Promise<Datasources[TName]> {\n let datasourcePromise: Promise<Datasources[TName]>;\n\n if (name in datasources) {\n datasourcePromise = datasources[name]!;\n } else {\n datasourcePromise = datasources[name] = createDatasource(name) as Datasources[TName];\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":";;;;AAMA,MAAM,cAEF,CAAC;;;;;;;;;AAgBL,eAAsB,cACpB,MACA,UAAgC,CAAC,GACJ;CAC7B,IAAI;CAEJ,IAAI,QAAQ,aACV,oBAAoB,YAAY;MAC3B;EACL,oBAAoB,YAAY,QAAQ,iBAAiB,IAAI;EAC7D,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"}
|
package/dist/runtime.d.mts
CHANGED
|
@@ -66,6 +66,11 @@ declare module "nitropack/types" {
|
|
|
66
66
|
drizzle?: RuntimeConfig;
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
|
+
declare module "nitro/types" {
|
|
70
|
+
interface NitroRuntimeConfig {
|
|
71
|
+
drizzle?: RuntimeConfig;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
69
74
|
type ConfigHookArgs = { [K in keyof DatasourceConfig]: [name: K, config: DatasourceConfig[K]]; }[keyof DatasourceConfig];
|
|
70
75
|
interface ConfigHooks {
|
|
71
76
|
"drizzle:config": (...args: ConfigHookArgs) => void | Promise<void>;
|
|
@@ -73,6 +78,9 @@ interface ConfigHooks {
|
|
|
73
78
|
declare module "nitropack/types" {
|
|
74
79
|
interface NitroRuntimeHooks extends ConfigHooks {}
|
|
75
80
|
}
|
|
81
|
+
declare module "nitro/types" {
|
|
82
|
+
interface NitroRuntimeHooks extends ConfigHooks {}
|
|
83
|
+
}
|
|
76
84
|
//#endregion
|
|
77
|
-
export { DatabaseFactory, DatasourceConfig, DatasourceProvider, DatasourceProviderConfig, DatasourceRegistry, Datasources, Primitive, PrimitiveProps, RuntimeConfig, ToDatasource, ToDatasourceProvider, UseDatasourceOptions, useDatasource };
|
|
85
|
+
export { ConfigHookArgs, ConfigHooks, DatabaseFactory, DatasourceConfig, DatasourceProvider, DatasourceProviderConfig, DatasourceRegistry, Datasources, Primitive, PrimitiveProps, RuntimeConfig, ToDatasource, ToDatasourceProvider, UseDatasourceOptions, useDatasource };
|
|
78
86
|
//# sourceMappingURL=runtime.d.mts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nitro-drizzle",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0-beta.1",
|
|
4
4
|
"description": "Nitro module to integrate Drizzle ORM with ease.",
|
|
5
5
|
"homepage": "https://github.com/Myshkouski/nitro-drizzle#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -29,7 +29,9 @@
|
|
|
29
29
|
"./drivers/*": "./dist/drivers/*.mjs",
|
|
30
30
|
"./meta": "./dist/meta.mjs",
|
|
31
31
|
"./module": "./dist/module.mjs",
|
|
32
|
+
"./middleware/*": "./dist/middleware/*.mjs",
|
|
32
33
|
"./plugins/*": "./dist/plugins/*.mjs",
|
|
34
|
+
"./plugins/legacy/*": "./dist/plugins/legacy/*.mjs",
|
|
33
35
|
"./runtime": "./dist/runtime.mjs",
|
|
34
36
|
"./utils": "./dist/utils.mjs",
|
|
35
37
|
"./package.json": "./package.json"
|
|
@@ -43,6 +45,7 @@
|
|
|
43
45
|
"defu": "^6.1.7",
|
|
44
46
|
"jiti": "^2.7.0",
|
|
45
47
|
"knitwork": "^1.3.0",
|
|
48
|
+
"nitro": "3.0.260610-beta",
|
|
46
49
|
"nitropack": "^2.13.4",
|
|
47
50
|
"ohash": "^2.0.11",
|
|
48
51
|
"pathe": "^2.0.3",
|
|
@@ -79,7 +82,8 @@
|
|
|
79
82
|
"drizzle-orm": "^0.45.2",
|
|
80
83
|
"h3": "^1.15.11",
|
|
81
84
|
"mysql2": "^3.22.6",
|
|
82
|
-
"postgres": "^3.4.9"
|
|
85
|
+
"postgres": "^3.4.9",
|
|
86
|
+
"srvx": "^0.11.21"
|
|
83
87
|
},
|
|
84
88
|
"peerDependenciesMeta": {
|
|
85
89
|
"@cloudflare/workers-types": {
|