nitro-drizzle 0.0.0 → 0.2.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/LICENSE +21 -0
- package/README.md +247 -0
- package/dist/config/defineConfig.d.mts +410 -0
- package/dist/config/defineConfig.mjs +34 -0
- package/dist/config/defineConfig.mjs.map +1 -0
- package/dist/config.d.mts +2 -0
- package/dist/config.mjs +2 -0
- package/dist/context/internal/async.mjs +15 -0
- package/dist/context/internal/async.mjs.map +1 -0
- package/dist/context/internal/config.mjs +31 -0
- package/dist/context/internal/config.mjs.map +1 -0
- package/dist/context/internal/fs.mjs +15 -0
- package/dist/context/internal/fs.mjs.map +1 -0
- package/dist/context/internal/logger.mjs +20 -0
- package/dist/context/internal/logger.mjs.map +1 -0
- package/dist/context/internal/templates.mjs +40 -0
- package/dist/context/internal/templates.mjs.map +1 -0
- package/dist/context/internal/virtual.mjs +71 -0
- package/dist/context/internal/virtual.mjs.map +1 -0
- package/dist/context.d.mts +113 -0
- package/dist/context.mjs +78 -0
- package/dist/context.mjs.map +1 -0
- package/dist/dialects/index.d.mts +39 -0
- package/dist/dialects/index.mjs +1 -0
- package/dist/dialects/mysql.d.mts +30 -0
- package/dist/dialects/mysql.mjs +30 -0
- package/dist/dialects/mysql.mjs.map +1 -0
- package/dist/dialects/postgresql.d.mts +29 -0
- package/dist/dialects/postgresql.mjs +29 -0
- package/dist/dialects/postgresql.mjs.map +1 -0
- package/dist/dialects/sqlite.d.mts +29 -0
- package/dist/dialects/sqlite.mjs +29 -0
- package/dist/dialects/sqlite.mjs.map +1 -0
- package/dist/drivers/d1-http.d.mts +41 -0
- package/dist/drivers/d1-http.mjs +105 -0
- package/dist/drivers/d1-http.mjs.map +1 -0
- package/dist/drivers/d1.d.mts +26 -0
- package/dist/drivers/d1.mjs +52 -0
- package/dist/drivers/d1.mjs.map +1 -0
- package/dist/drivers/db0.d.mts +20 -0
- package/dist/drivers/db0.mjs +27 -0
- package/dist/drivers/db0.mjs.map +1 -0
- package/dist/drivers/index.d.mts +35 -0
- package/dist/drivers/index.mjs +14 -0
- package/dist/drivers/index.mjs.map +1 -0
- package/dist/drivers/internal/d1-dialect.mjs +41 -0
- package/dist/drivers/internal/d1-dialect.mjs.map +1 -0
- package/dist/drivers/internal/sql.mjs +6 -0
- package/dist/drivers/internal/sql.mjs.map +1 -0
- package/dist/drivers/mysql.d.mts +27 -0
- package/dist/drivers/mysql.mjs +32 -0
- package/dist/drivers/mysql.mjs.map +1 -0
- package/dist/drivers/pglite.d.mts +29 -0
- package/dist/drivers/pglite.mjs +29 -0
- package/dist/drivers/pglite.mjs.map +1 -0
- package/dist/drivers/postgresql.d.mts +27 -0
- package/dist/drivers/postgresql.mjs +30 -0
- package/dist/drivers/postgresql.mjs.map +1 -0
- package/dist/drivers/sqlite.d.mts +28 -0
- package/dist/drivers/sqlite.mjs +30 -0
- package/dist/drivers/sqlite.mjs.map +1 -0
- package/dist/meta.d.mts +8 -0
- package/dist/meta.mjs +9 -0
- package/dist/meta.mjs.map +1 -0
- package/dist/migrations/internal/migrate.d.mts +16 -0
- package/dist/migrations/internal/migrate.mjs +33 -0
- package/dist/migrations/internal/migrate.mjs.map +1 -0
- package/dist/migrations/migrate.d.mts +16 -0
- package/dist/migrations/migrate.mjs +23 -0
- package/dist/migrations/migrate.mjs.map +1 -0
- package/dist/migrations/task.d.mts +7 -0
- package/dist/migrations/task.mjs +11 -0
- package/dist/migrations/task.mjs.map +1 -0
- package/dist/migrations/useMigrations.d.mts +46 -0
- package/dist/migrations/useMigrations.mjs +57 -0
- package/dist/migrations/useMigrations.mjs.map +1 -0
- package/dist/migrations.d.mts +4 -0
- package/dist/migrations.mjs +3 -0
- package/dist/module/internal/assets.d.mts +12 -0
- package/dist/module/internal/assets.mjs +21 -0
- package/dist/module/internal/assets.mjs.map +1 -0
- package/dist/module/internal/externals.mjs +19 -0
- package/dist/module/internal/externals.mjs.map +1 -0
- package/dist/module/internal/fs.mjs +11 -0
- package/dist/module/internal/fs.mjs.map +1 -0
- package/dist/module/internal/path.mjs +11 -0
- package/dist/module/internal/path.mjs.map +1 -0
- package/dist/module/internal/reload.mjs +17 -0
- package/dist/module/internal/reload.mjs.map +1 -0
- package/dist/module/internal/resolver.mjs +27 -0
- package/dist/module/internal/resolver.mjs.map +1 -0
- package/dist/module/internal/rollup.mjs +8 -0
- package/dist/module/internal/rollup.mjs.map +1 -0
- package/dist/module/internal/types.mjs +39 -0
- package/dist/module/internal/types.mjs.map +1 -0
- package/dist/module/internal/virtual.mjs +23 -0
- package/dist/module/internal/virtual.mjs.map +1 -0
- package/dist/module.d.mts +37 -0
- package/dist/module.mjs +82 -0
- package/dist/module.mjs.map +1 -0
- package/dist/plugins/init.d.mts +24 -0
- package/dist/plugins/init.mjs +33 -0
- package/dist/plugins/init.mjs.map +1 -0
- package/dist/plugins/migrate.d.mts +22 -0
- package/dist/plugins/migrate.mjs +25 -0
- package/dist/plugins/migrate.mjs.map +1 -0
- package/dist/runtime/internal/config.mjs +24 -0
- package/dist/runtime/internal/config.mjs.map +1 -0
- package/dist/runtime/internal/createDatasource.mjs +15 -0
- package/dist/runtime/internal/createDatasource.mjs.map +1 -0
- package/dist/runtime/useDatasource.d.mts +19 -0
- package/dist/runtime/useDatasource.mjs +35 -0
- package/dist/runtime/useDatasource.mjs.map +1 -0
- package/dist/runtime.d.mts +79 -0
- package/dist/runtime.mjs +2 -0
- package/dist/shared.d.mts +12 -0
- package/dist/shared.mjs +1 -0
- package/dist/utils/usePrimaryKey.d.mts +14 -0
- package/dist/utils/usePrimaryKey.mjs +19 -0
- package/dist/utils/usePrimaryKey.mjs.map +1 -0
- package/dist/utils.d.mts +2 -0
- package/dist/utils.mjs +2 -0
- package/package.json +100 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defineConfig.mjs","names":["relativePath","resolvePath"],"sources":["../../src/config/defineConfig.ts"],"sourcesContent":["import type { Config as DrizzleConfig } from \"drizzle-kit\";\nimport { resolve as resolvePath, relative as relativePath } from \"pathe\";\nimport { cwd } from \"node:process\";\nimport { fileURLToPath } from \"node:url\";\n\n/**\n * Define Drizzle config compatible with both 'nitro-drizzle/module' and 'drizzle-kit' packages.\n *\n * @param config\n * @param dirname\n * @returns\n */\nexport function defineConfig(config: DrizzleConfig, filename: string) {\n const { schema, out, ...other } = config;\n\n const workingDir = cwd();\n\n const dirname = fileURLToPath(new URL(\".\", filename));\n\n return {\n schema: transformToPluginCompatiblePath(dirname, workingDir, schema),\n out: transformToPluginCompatiblePath(dirname, workingDir, out),\n ...other,\n };\n}\n\nfunction transformToPluginCompatiblePath(\n dirname: string,\n cwd: string,\n path: string | undefined,\n): string | undefined;\nfunction transformToPluginCompatiblePath(\n dirname: string,\n cwd: string,\n path: string | string[] | undefined,\n): string | string[] | undefined;\nfunction transformToPluginCompatiblePath(\n dirname: string,\n cwd: string,\n path: string | string[] | undefined,\n) {\n if (!path) return;\n if (Array.isArray(path)) {\n return path.flatMap((path) => transformToRelative(dirname, cwd, path));\n }\n return relativePath(cwd, resolvePath(dirname, path));\n}\n\nfunction transformToRelative(dirname: string, cwd: string, path: string) {\n path = resolvePath(dirname, path);\n return relativePath(cwd, path);\n}\n"],"mappings":";;;;;;;;;;;AAYA,SAAgB,aAAa,QAAuB,UAAkB;CACpE,MAAM,EAAE,QAAQ,KAAK,GAAG,UAAU;CAElC,MAAM,aAAa,IAAI;CAEvB,MAAM,UAAU,cAAc,IAAI,IAAI,KAAK,QAAQ,CAAC;CAEpD,OAAO;EACL,QAAQ,gCAAgC,SAAS,YAAY,MAAM;EACnE,KAAK,gCAAgC,SAAS,YAAY,GAAG;EAC7D,GAAG;CACL;AACF;AAYA,SAAS,gCACP,SACA,KACA,MACA;CACA,IAAI,CAAC,MAAM;CACX,IAAI,MAAM,QAAQ,IAAI,GACpB,OAAO,KAAK,SAAS,SAAS,oBAAoB,SAAS,KAAK,IAAI,CAAC;CAEvE,OAAOA,SAAa,KAAKC,QAAY,SAAS,IAAI,CAAC;AACrD;AAEA,SAAS,oBAAoB,SAAiB,KAAa,MAAc;CACvE,OAAOA,QAAY,SAAS,IAAI;CAChC,OAAOD,SAAa,KAAK,IAAI;AAC/B"}
|
package/dist/config.mjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/context/internal/async.ts
|
|
2
|
+
async function* generateAsync(iterable, cb) {
|
|
3
|
+
let index = 0;
|
|
4
|
+
for await (const value of iterable) yield await cb(value, index++);
|
|
5
|
+
}
|
|
6
|
+
async function mapAsync(iterable, cb) {
|
|
7
|
+
const generator = generateAsync(iterable, cb);
|
|
8
|
+
const items = [];
|
|
9
|
+
for await (const item of generator) items.push(item);
|
|
10
|
+
return items;
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { mapAsync };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=async.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"async.mjs","names":[],"sources":["../../../src/context/internal/async.ts"],"sourcesContent":["export type MaybeAsyncIterable<T> = Iterable<T> | AsyncIterable<T>;\n\nasync function* generateAsync<T, U>(\n iterable: MaybeAsyncIterable<T>,\n cb: (value: T, index: number) => Promise<U>,\n) {\n let index = 0;\n for await (const value of iterable) {\n yield await cb(value, index++);\n }\n}\n\nexport async function mapAsync<T, U>(\n iterable: MaybeAsyncIterable<T>,\n cb: (value: T, index: number) => Promise<U>,\n) {\n const generator = generateAsync(iterable, cb);\n const items: U[] = [];\n for await (const item of generator) {\n items.push(item);\n }\n return items;\n}\n\nexport async function runParallel<Tasks extends ReadonlyArray<() => any>>(...tasks: Tasks) {\n return await Promise.all(tasks);\n}\n"],"mappings":";AAEA,gBAAgB,cACd,UACA,IACA;CACA,IAAI,QAAQ;CACZ,WAAW,MAAM,SAAS,UACxB,MAAM,MAAM,GAAG,OAAO,OAAO;AAEjC;AAEA,eAAsB,SACpB,UACA,IACA;CACA,MAAM,YAAY,cAAc,UAAU,EAAE;CAC5C,MAAM,QAAa,CAAC;CACpB,WAAW,MAAM,QAAQ,WACvB,MAAM,KAAK,IAAI;CAEjB,OAAO;AACT"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { resolve } from "pathe";
|
|
2
|
+
import { genObjectKey } from "knitwork";
|
|
3
|
+
//#region src/context/internal/config.ts
|
|
4
|
+
async function transformDrizzleConfig(drizzleConfig, { dirName, path, resolver, cwd }) {
|
|
5
|
+
const driver = "driver" in drizzleConfig ? drizzleConfig.driver : void 0;
|
|
6
|
+
const dialect = drizzleConfig.dialect;
|
|
7
|
+
return {
|
|
8
|
+
dirName,
|
|
9
|
+
name: genObjectKey(dirName.replace(DISABLED_DATASOURCE_DIRNAME_REGEX, "")),
|
|
10
|
+
enabled: !DISABLED_DATASOURCE_DIRNAME_REGEX.test(dirName),
|
|
11
|
+
dialect,
|
|
12
|
+
driver,
|
|
13
|
+
imports: {
|
|
14
|
+
config: path,
|
|
15
|
+
schema: (drizzleConfig.schema ? [drizzleConfig.schema].flat() : []).map((schemaFilename) => {
|
|
16
|
+
return resolver.resolve(resolve(cwd, schemaFilename));
|
|
17
|
+
}),
|
|
18
|
+
connector: `nitro-drizzle/drivers/${driver || dialect}`,
|
|
19
|
+
helpers: `nitro-drizzle/dialects/${driver || dialect}`
|
|
20
|
+
},
|
|
21
|
+
migrations: {
|
|
22
|
+
assets: drizzleConfig.out ? resolve(cwd, drizzleConfig.out) : void 0,
|
|
23
|
+
config: drizzleConfig.migrations
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
const DISABLED_DATASOURCE_DIRNAME_REGEX = /^[_-]+/;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { transformDrizzleConfig };
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=config.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.mjs","names":[],"sources":["../../../src/context/internal/config.ts"],"sourcesContent":["import type { Config as DrizzleConfig } from \"drizzle-kit\";\nimport type { DatasourceInfo, Resolver } from \"..\";\nimport { resolve } from \"pathe\";\nimport { genObjectKey } from \"knitwork\";\n\nexport async function transformDrizzleConfig(\n drizzleConfig: DrizzleConfig,\n { dirName, path, resolver, cwd }: TransformDrizzleConfigOptions,\n): Promise<DatasourceInfo> {\n const driver = \"driver\" in drizzleConfig ? drizzleConfig.driver : undefined;\n const dialect = drizzleConfig.dialect;\n return {\n dirName,\n name: genObjectKey(dirName.replace(DISABLED_DATASOURCE_DIRNAME_REGEX, \"\")),\n enabled: !DISABLED_DATASOURCE_DIRNAME_REGEX.test(dirName),\n dialect,\n driver,\n imports: {\n config: path,\n schema: (drizzleConfig.schema ? [drizzleConfig.schema].flat() : []).map((schemaFilename) => {\n return resolver.resolve(resolve(cwd, schemaFilename));\n }),\n // connector: resolver.tryResolve(resolve(cwd, './driver')) || resolver.resolve(join(connectorsDir, driver || dialect)),\n // connector: resolver.resolve(`nitro-drizzle/drivers/${driver || dialect}`),\n connector: `nitro-drizzle/drivers/${driver || dialect}`,\n helpers: `nitro-drizzle/dialects/${driver || dialect}`,\n },\n migrations: {\n assets: drizzleConfig.out ? resolve(cwd, drizzleConfig.out) : undefined,\n config: drizzleConfig.migrations,\n },\n };\n}\n\nconst DISABLED_DATASOURCE_DIRNAME_REGEX = /^[_-]+/;\n\nexport type TransformDrizzleConfigOptions = {\n dirName: string;\n path: string;\n cwd: string;\n resolver: Resolver;\n};\n"],"mappings":";;;AAKA,eAAsB,uBACpB,eACA,EAAE,SAAS,MAAM,UAAU,OACF;CACzB,MAAM,SAAS,YAAY,gBAAgB,cAAc,SAAS,KAAA;CAClE,MAAM,UAAU,cAAc;CAC9B,OAAO;EACL;EACA,MAAM,aAAa,QAAQ,QAAQ,mCAAmC,EAAE,CAAC;EACzE,SAAS,CAAC,kCAAkC,KAAK,OAAO;EACxD;EACA;EACA,SAAS;GACP,QAAQ;GACR,SAAS,cAAc,SAAS,CAAC,cAAc,MAAM,CAAC,CAAC,KAAK,IAAI,CAAC,EAAA,CAAG,KAAK,mBAAmB;IAC1F,OAAO,SAAS,QAAQ,QAAQ,KAAK,cAAc,CAAC;GACtD,CAAC;GAGD,WAAW,yBAAyB,UAAU;GAC9C,SAAS,0BAA0B,UAAU;EAC/C;EACA,YAAY;GACV,QAAQ,cAAc,MAAM,QAAQ,KAAK,cAAc,GAAG,IAAI,KAAA;GAC9D,QAAQ,cAAc;EACxB;CACF;AACF;AAEA,MAAM,oCAAoC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { glob } from "tinyglobby";
|
|
2
|
+
//#region src/context/internal/fs.ts
|
|
3
|
+
async function resolveFiles(baseDir, pattern, options) {
|
|
4
|
+
return (await glob(pattern, {
|
|
5
|
+
cwd: baseDir,
|
|
6
|
+
dot: true,
|
|
7
|
+
absolute: true,
|
|
8
|
+
ignore: options?.ignore,
|
|
9
|
+
followSymbolicLinks: options?.followSymbolicLinks
|
|
10
|
+
})).sort((a, b) => a.localeCompare(b));
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
export { resolveFiles };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=fs.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fs.mjs","names":[],"sources":["../../../src/context/internal/fs.ts"],"sourcesContent":["import { glob } from \"tinyglobby\";\n\nexport async function resolveFiles(\n baseDir: string,\n pattern: string | readonly string[],\n options?: {\n ignore?: string | readonly string[];\n followSymbolicLinks?: boolean;\n },\n): Promise<string[]> {\n const fileNames = await glob(pattern, {\n cwd: baseDir,\n dot: true,\n absolute: true,\n ignore: options?.ignore,\n followSymbolicLinks: options?.followSymbolicLinks,\n });\n\n return fileNames.sort((a, b) => a.localeCompare(b));\n}\n"],"mappings":";;AAEA,eAAsB,aACpB,SACA,SACA,SAImB;CASnB,QAAO,MARiB,KAAK,SAAS;EACpC,KAAK;EACL,KAAK;EACL,UAAU;EACV,QAAQ,SAAS;EACjB,qBAAqB,SAAS;CAChC,CAAC,EAAA,CAEgB,MAAM,GAAG,MAAM,EAAE,cAAc,CAAC,CAAC;AACpD"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { colorize } from "consola/utils";
|
|
2
|
+
//#region src/context/internal/logger.ts
|
|
3
|
+
function accentColor(text) {
|
|
4
|
+
return colorize("bold", colorize("greenBright", text.toString()));
|
|
5
|
+
}
|
|
6
|
+
function accent(strings, ...values) {
|
|
7
|
+
let result = "";
|
|
8
|
+
for (let i = 0; i < strings.length; i++) {
|
|
9
|
+
result += strings[i];
|
|
10
|
+
if (i < values.length) {
|
|
11
|
+
const value = accentColor(String(values[i]));
|
|
12
|
+
result += value;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
export { accent };
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=logger.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.mjs","names":[],"sources":["../../../src/context/internal/logger.ts"],"sourcesContent":["import { colorize } from \"consola/utils\";\n\nfunction accentColor(text: string | number | boolean) {\n return colorize(\"bold\", colorize(\"greenBright\", text.toString()));\n}\n\nexport function accent(strings: TemplateStringsArray, ...values: (string | number | boolean)[]) {\n let result = \"\";\n for (let i = 0; i < strings.length; i++) {\n result += strings[i];\n if (i < values.length) {\n const value = accentColor(String(values[i]));\n result += value;\n }\n }\n return result;\n}\n"],"mappings":";;AAEA,SAAS,YAAY,MAAiC;CACpD,OAAO,SAAS,QAAQ,SAAS,eAAe,KAAK,SAAS,CAAC,CAAC;AAClE;AAEA,SAAgB,OAAO,SAA+B,GAAG,QAAuC;CAC9F,IAAI,SAAS;CACb,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;EACvC,UAAU,QAAQ;EAClB,IAAI,IAAI,OAAO,QAAQ;GACrB,MAAM,QAAQ,YAAY,OAAO,OAAO,EAAE,CAAC;GAC3C,UAAU;EACZ;CACF;CACA,OAAO;AACT"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { genAugmentation, genInlineTypeImport, genString, genTypeImport } from "knitwork";
|
|
2
|
+
//#region src/context/internal/templates.ts
|
|
3
|
+
function runtimeDeclarations(datasources) {
|
|
4
|
+
return [genTypeImport("nitro-drizzle/runtime", ["ToDatasourceProvider"]), genAugmentation("nitro-drizzle/runtime", { DatasourceRegistry: Object.fromEntries(datasources.filter((datasource) => {
|
|
5
|
+
return datasource.enabled;
|
|
6
|
+
}).map(({ name, imports }) => {
|
|
7
|
+
const schemaType = genSchemaType(imports.schema);
|
|
8
|
+
return [name, `ToDatasourceProvider<${schemaType}, ${genInlineTypeImport(imports.connector)}<${schemaType}>>`];
|
|
9
|
+
})) })].join("\n");
|
|
10
|
+
}
|
|
11
|
+
function genSchemaType(imports) {
|
|
12
|
+
return imports.map((id) => `typeof import('${id}')`).join(" & ");
|
|
13
|
+
}
|
|
14
|
+
function moduleDeclarations(datasources) {
|
|
15
|
+
const names = datasources.map((d) => d.name);
|
|
16
|
+
return [genTypeImport("nitro-drizzle/module", ["ModuleOptions"]), genAugmentation("nitro-drizzle/module", { DatasourceOptions: [{}, { extends: `Record<${names.map((n) => genString(n)).join(" | ")}, { connector: string; }>` }] })].join("\n");
|
|
17
|
+
}
|
|
18
|
+
function augmentations(datasources) {
|
|
19
|
+
return { "nitro-drizzle/augmentations.d.ts": [
|
|
20
|
+
runtimeDeclarations(datasources),
|
|
21
|
+
moduleDeclarations(datasources),
|
|
22
|
+
`export {};`
|
|
23
|
+
].join("\n") };
|
|
24
|
+
}
|
|
25
|
+
function dialectDeclarations(datasources) {
|
|
26
|
+
return datasources.filter((d) => d.enabled).map(({ name, dialect }) => {
|
|
27
|
+
return `
|
|
28
|
+
declare module "#nitro-drizzle/dialects/${name}" {
|
|
29
|
+
export * from "nitro-drizzle/dialects/${dialect}";
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
}).join("\n");
|
|
33
|
+
}
|
|
34
|
+
function declarations(datasources) {
|
|
35
|
+
return { "#nitro-drizzle/*": { "nitro-drizzle/declarations.d.ts": [dialectDeclarations(datasources)].join("\n") } };
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { augmentations, declarations };
|
|
39
|
+
|
|
40
|
+
//# sourceMappingURL=templates.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templates.mjs","names":[],"sources":["../../../src/context/internal/templates.ts"],"sourcesContent":["import { genTypeImport, genAugmentation, genInlineTypeImport, genString } from \"knitwork\";\nimport type { DatasourceInfo } from \"..\";\nimport type { VirtualModules } from \"nitro-drizzle/shared\";\n\nfunction runtimeDeclarations(datasources: DatasourceInfo[]) {\n return [\n genTypeImport(\"nitro-drizzle/runtime\", [\"ToDatasourceProvider\"]),\n genAugmentation(\"nitro-drizzle/runtime\", {\n DatasourceRegistry: Object.fromEntries(\n datasources\n .filter((datasource) => {\n return datasource.enabled;\n })\n .map(({ name, imports }): [string, string] => {\n const schemaType = genSchemaType(imports.schema);\n const driverType = genInlineTypeImport(imports.connector);\n return [name, `${\"ToDatasourceProvider\"}<${schemaType}, ${driverType}<${schemaType}>>`];\n }),\n ),\n }),\n ].join(\"\\n\");\n}\n\nfunction genSchemaType(imports: string[]) {\n return imports.map((id) => `typeof import('${id}')`).join(\" & \");\n}\n\nfunction moduleDeclarations(datasources: DatasourceInfo[]) {\n const names = datasources.map((d) => d.name);\n\n return [\n genTypeImport(\"nitro-drizzle/module\", [\"ModuleOptions\"]),\n genAugmentation(\"nitro-drizzle/module\", {\n DatasourceOptions: [\n {},\n {\n extends: `Record<${names.map((n) => genString(n)).join(\" | \")}, { connector: string; }>`,\n },\n ],\n }),\n ].join(\"\\n\");\n}\n\nexport function augmentations(datasources: DatasourceInfo[]): VirtualModules<`${string}.d.ts`> {\n return {\n \"nitro-drizzle/augmentations.d.ts\": [\n runtimeDeclarations(datasources),\n moduleDeclarations(datasources),\n /* ts */ `export {};`,\n ].join(\"\\n\"),\n };\n}\n\nfunction dialectDeclarations(datasources: DatasourceInfo[]) {\n return datasources\n .filter((d) => d.enabled)\n .map(({ name, dialect }) => {\n return /*ts*/ `\n declare module \"#nitro-drizzle/dialects/${name}\" {\n export * from \"nitro-drizzle/dialects/${dialect}\";\n }\n `;\n })\n .join(\"\\n\");\n}\n\nexport function declarations(\n datasources: DatasourceInfo[],\n): Record<string, VirtualModules<`${string}.d.ts`>> {\n return {\n \"#nitro-drizzle/*\": {\n \"nitro-drizzle/declarations.d.ts\": [dialectDeclarations(datasources)].join(\"\\n\"),\n },\n };\n}\n"],"mappings":";;AAIA,SAAS,oBAAoB,aAA+B;CAC1D,OAAO,CACL,cAAc,yBAAyB,CAAC,sBAAsB,CAAC,GAC/D,gBAAgB,yBAAyB,EACvC,oBAAoB,OAAO,YACzB,YACG,QAAQ,eAAe;EACtB,OAAO,WAAW;CACpB,CAAC,CAAC,CACD,KAAK,EAAE,MAAM,cAAgC;EAC5C,MAAM,aAAa,cAAc,QAAQ,MAAM;EAE/C,OAAO,CAAC,MAAM,wBAA6B,WAAW,IADnC,oBAAoB,QAAQ,SACoB,EAAE,GAAG,WAAW,GAAG;CACxF,CAAC,CACL,EACF,CAAC,CACH,CAAC,CAAC,KAAK,IAAI;AACb;AAEA,SAAS,cAAc,SAAmB;CACxC,OAAO,QAAQ,KAAK,OAAO,kBAAkB,GAAG,GAAG,CAAC,CAAC,KAAK,KAAK;AACjE;AAEA,SAAS,mBAAmB,aAA+B;CACzD,MAAM,QAAQ,YAAY,KAAK,MAAM,EAAE,IAAI;CAE3C,OAAO,CACL,cAAc,wBAAwB,CAAC,eAAe,CAAC,GACvD,gBAAgB,wBAAwB,EACtC,mBAAmB,CACjB,CAAC,GACD,EACE,SAAS,UAAU,MAAM,KAAK,MAAM,UAAU,CAAC,CAAC,CAAC,CAAC,KAAK,KAAK,EAAE,2BAChE,CACF,EACF,CAAC,CACH,CAAC,CAAC,KAAK,IAAI;AACb;AAEA,SAAgB,cAAc,aAAiE;CAC7F,OAAO,EACL,oCAAoC;EAClC,oBAAoB,WAAW;EAC/B,mBAAmB,WAAW;EACrB;CACX,CAAC,CAAC,KAAK,IAAI,EACb;AACF;AAEA,SAAS,oBAAoB,aAA+B;CAC1D,OAAO,YACJ,QAAQ,MAAM,EAAE,OAAO,CAAC,CACxB,KAAK,EAAE,MAAM,cAAc;EAC1B,OAAc;gDAC4B,KAAK;gDACL,QAAQ;;;CAGpD,CAAC,CAAC,CACD,KAAK,IAAI;AACd;AAEA,SAAgB,aACd,aACkD;CAClD,OAAO,EACL,oBAAoB,EAClB,mCAAmC,CAAC,oBAAoB,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,EACjF,EACF;AACF"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { genImport, genObjectFromRaw, genSafeVariableName, genString } from "knitwork";
|
|
2
|
+
//#region src/context/internal/virtual.ts
|
|
3
|
+
function genDatabaseModuleVariableName(dbModuleIndex) {
|
|
4
|
+
return genSafeVariableName(`dbModule${dbModuleIndex}`);
|
|
5
|
+
}
|
|
6
|
+
const genSchemaModuleVariableName = (dbModuleIndex, schemaIndex) => genSafeVariableName(`schemaModule${dbModuleIndex}_${schemaIndex}`);
|
|
7
|
+
function mergeSchemaModules(schemaIds, dbModuleIndex) {
|
|
8
|
+
return schemaIds.map((_, schemaModuleIdIndex) => {
|
|
9
|
+
return ", " + genSchemaModuleVariableName(dbModuleIndex, schemaModuleIdIndex);
|
|
10
|
+
}).join("");
|
|
11
|
+
}
|
|
12
|
+
function runtimeVirtualModule(datasources) {
|
|
13
|
+
const parts = datasources.map(({ name, imports: { schema, connector } }, dbModuleIndex) => {
|
|
14
|
+
const databaseModuleVariableName = genDatabaseModuleVariableName(dbModuleIndex);
|
|
15
|
+
return {
|
|
16
|
+
imports: [genImport(connector, databaseModuleVariableName), ...schema.map((schemaModuleId, schemaModuleIdIndex) => {
|
|
17
|
+
return genImport(schemaModuleId, {
|
|
18
|
+
name: "*",
|
|
19
|
+
as: genSchemaModuleVariableName(dbModuleIndex, schemaModuleIdIndex)
|
|
20
|
+
});
|
|
21
|
+
})].join("\n"),
|
|
22
|
+
code: `
|
|
23
|
+
datasourceRegistry[${genString(name)}] = ${genObjectFromRaw({
|
|
24
|
+
create: `function (config) { return ${databaseModuleVariableName}(config, this.schema) }`,
|
|
25
|
+
schema: `Object.assign({}${mergeSchemaModules(schema, dbModuleIndex)})`
|
|
26
|
+
})};
|
|
27
|
+
`
|
|
28
|
+
};
|
|
29
|
+
}).reduce((acc, { imports, code }) => {
|
|
30
|
+
return {
|
|
31
|
+
imports: [...acc.imports, imports],
|
|
32
|
+
code: [...acc.code, code]
|
|
33
|
+
};
|
|
34
|
+
}, {
|
|
35
|
+
imports: [],
|
|
36
|
+
code: []
|
|
37
|
+
});
|
|
38
|
+
const datasourceRegistryVarName = genSafeVariableName("datasourceRegistry");
|
|
39
|
+
return `
|
|
40
|
+
${parts.imports.join("\n")}
|
|
41
|
+
|
|
42
|
+
const ${datasourceRegistryVarName} = {};
|
|
43
|
+
|
|
44
|
+
${parts.code.join("\n")}
|
|
45
|
+
|
|
46
|
+
export function useDatasourceRegistry() {
|
|
47
|
+
return ${datasourceRegistryVarName};
|
|
48
|
+
}
|
|
49
|
+
`;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Generates virtual modules for runtime datasource access.
|
|
53
|
+
* Creates module code that registers datasource providers and exposes a registry.
|
|
54
|
+
* @param datasources - Array of datasource information
|
|
55
|
+
* @returns Object mapping module IDs to their code
|
|
56
|
+
*/
|
|
57
|
+
function virtualModules(datasources) {
|
|
58
|
+
return {
|
|
59
|
+
"#nitro-drizzle/runtime": runtimeVirtualModule(datasources),
|
|
60
|
+
...dialectVirtualModules(datasources)
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function dialectVirtualModules(datasources) {
|
|
64
|
+
return Object.fromEntries(datasources.filter((d) => d.enabled).map(({ name, dialect }) => {
|
|
65
|
+
return [`#nitro-drizzle/dialects/${name}`, `export * from "nitro-drizzle/dialects/${dialect}";`];
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
//#endregion
|
|
69
|
+
export { virtualModules };
|
|
70
|
+
|
|
71
|
+
//# sourceMappingURL=virtual.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"virtual.mjs","names":[],"sources":["../../../src/context/internal/virtual.ts"],"sourcesContent":["import { genImport, genObjectFromRaw, genSafeVariableName, genString } from \"knitwork\";\nimport type { DatasourceInfo } from \"..\";\nimport type { VirtualModules } from \"nitro-drizzle/shared\";\n\nfunction genDatabaseModuleVariableName(dbModuleIndex: number) {\n return genSafeVariableName(`dbModule${dbModuleIndex}`);\n}\n\nconst genSchemaModuleVariableName = (dbModuleIndex: number, schemaIndex: number) =>\n genSafeVariableName(`schemaModule${dbModuleIndex}_${schemaIndex}`);\n\nfunction mergeSchemaModules(schemaIds: string[], dbModuleIndex: number) {\n return schemaIds\n .map((_, schemaModuleIdIndex) => {\n return \", \" + genSchemaModuleVariableName(dbModuleIndex, schemaModuleIdIndex);\n })\n .join(\"\");\n}\n\nfunction runtimeVirtualModule(datasources: DatasourceInfo[]) {\n const parts = datasources\n .map(({ name, imports: { schema, connector } }, dbModuleIndex) => {\n const databaseModuleVariableName = genDatabaseModuleVariableName(dbModuleIndex);\n\n const imports = [\n genImport(connector, databaseModuleVariableName),\n ...schema.map((schemaModuleId, schemaModuleIdIndex) => {\n return genImport(schemaModuleId, {\n name: \"*\",\n as: genSchemaModuleVariableName(dbModuleIndex, schemaModuleIdIndex),\n });\n }),\n ].join(\"\\n\");\n\n const code = /* js */ `\n datasourceRegistry[${genString(name)}] = ${genObjectFromRaw({\n create: /* js */ `function (config) { return ${databaseModuleVariableName}(config, this.schema) }`,\n schema: `Object.assign({}${mergeSchemaModules(schema, dbModuleIndex)})`,\n })};\n `;\n\n return {\n imports,\n code,\n };\n })\n .reduce(\n (acc, { imports, code }) => {\n return {\n imports: [...acc.imports, imports],\n code: [...acc.code, code],\n };\n },\n { imports: [] as string[], code: [] as string[] },\n );\n\n const datasourceRegistryVarName = genSafeVariableName(\"datasourceRegistry\");\n\n return /* js */ `\n ${parts.imports.join(\"\\n\")}\n \n const ${datasourceRegistryVarName} = {};\n \n ${parts.code.join(\"\\n\")}\n\n export function useDatasourceRegistry() {\n return ${datasourceRegistryVarName};\n }\n `;\n}\n\n/**\n * Generates virtual modules for runtime datasource access.\n * Creates module code that registers datasource providers and exposes a registry.\n * @param datasources - Array of datasource information\n * @returns Object mapping module IDs to their code\n */\nexport function virtualModules(datasources: DatasourceInfo[]): VirtualModules {\n return {\n \"#nitro-drizzle/runtime\": runtimeVirtualModule(datasources),\n ...dialectVirtualModules(datasources),\n };\n}\n\n// Replaced with aliases\nfunction dialectVirtualModules(datasources: DatasourceInfo[]): VirtualModules {\n return Object.fromEntries(\n datasources\n .filter((d) => d.enabled)\n .map(({ name, dialect }) => {\n return [\n `#nitro-drizzle/dialects/${name}`,\n `export * from \"nitro-drizzle/dialects/${dialect}\";`,\n ];\n }),\n );\n}\n"],"mappings":";;AAIA,SAAS,8BAA8B,eAAuB;CAC5D,OAAO,oBAAoB,WAAW,eAAe;AACvD;AAEA,MAAM,+BAA+B,eAAuB,gBAC1D,oBAAoB,eAAe,cAAc,GAAG,aAAa;AAEnE,SAAS,mBAAmB,WAAqB,eAAuB;CACtE,OAAO,UACJ,KAAK,GAAG,wBAAwB;EAC/B,OAAO,OAAO,4BAA4B,eAAe,mBAAmB;CAC9E,CAAC,CAAC,CACD,KAAK,EAAE;AACZ;AAEA,SAAS,qBAAqB,aAA+B;CAC3D,MAAM,QAAQ,YACX,KAAK,EAAE,MAAM,SAAS,EAAE,QAAQ,eAAe,kBAAkB;EAChE,MAAM,6BAA6B,8BAA8B,aAAa;EAmB9E,OAAO;GACL,SAlBc,CACd,UAAU,WAAW,0BAA0B,GAC/C,GAAG,OAAO,KAAK,gBAAgB,wBAAwB;IACrD,OAAO,UAAU,gBAAgB;KAC/B,MAAM;KACN,IAAI,4BAA4B,eAAe,mBAAmB;IACpE,CAAC;GACH,CAAC,CACH,CAAC,CAAC,KAAK,IAUC;GACN,MAAA;4BARoB,UAAU,IAAI,EAAE,MAAM,iBAAiB;IAC1D,QAAiB,8BAA8B,2BAA2B;IAC1E,QAAQ,mBAAmB,mBAAmB,QAAQ,aAAa,EAAE;GACvE,CAAC,EAAE;;EAMJ;CACF,CAAC,CAAC,CACD,QACE,KAAK,EAAE,SAAS,WAAW;EAC1B,OAAO;GACL,SAAS,CAAC,GAAG,IAAI,SAAS,OAAO;GACjC,MAAM,CAAC,GAAG,IAAI,MAAM,IAAI;EAC1B;CACF,GACA;EAAE,SAAS,CAAC;EAAe,MAAM,CAAC;CAAc,CAClD;CAEF,MAAM,4BAA4B,oBAAoB,oBAAoB;CAE1E,OAAgB;OACX,MAAM,QAAQ,KAAK,IAAI,EAAE;;aAEnB,0BAA0B;;OAEhC,MAAM,KAAK,KAAK,IAAI,EAAE;;;gBAGb,0BAA0B;;;AAG1C;;;;;;;AAQA,SAAgB,eAAe,aAA+C;CAC5E,OAAO;EACL,0BAA0B,qBAAqB,WAAW;EAC1D,GAAG,sBAAsB,WAAW;CACtC;AACF;AAGA,SAAS,sBAAsB,aAA+C;CAC5E,OAAO,OAAO,YACZ,YACG,QAAQ,MAAM,EAAE,OAAO,CAAC,CACxB,KAAK,EAAE,MAAM,cAAc;EAC1B,OAAO,CACL,2BAA2B,QAC3B,yCAAyC,QAAQ,GACnD;CACF,CAAC,CACL;AACF"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Config } from "drizzle-kit";
|
|
2
|
+
import { ConsolaInstance } from "consola";
|
|
3
|
+
import { MaybePromise, VirtualModules } from "nitro-drizzle/shared";
|
|
4
|
+
|
|
5
|
+
//#region src/context/index.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Context interface for managing Drizzle datasource configurations.
|
|
8
|
+
*/
|
|
9
|
+
interface Context {
|
|
10
|
+
/**
|
|
11
|
+
* Reloads the context, clearing any cached datasource information.
|
|
12
|
+
*/
|
|
13
|
+
reload(): MaybePromise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Returns a list of all resolved datasource information.
|
|
16
|
+
*/
|
|
17
|
+
datasources(): MaybePromise<DatasourceInfo[]>;
|
|
18
|
+
/**
|
|
19
|
+
* Returns type declaration code for all datasources.
|
|
20
|
+
*/
|
|
21
|
+
augmentations(): MaybePromise<VirtualModules>;
|
|
22
|
+
declarations(): MaybePromise<Record<string, VirtualModules>>;
|
|
23
|
+
/**
|
|
24
|
+
* Returns virtual module code for runtime datasource access.
|
|
25
|
+
*/
|
|
26
|
+
virtualModules(): MaybePromise<VirtualModules>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Glob pattern or array of patterns to match Drizzle configuration files.
|
|
30
|
+
*/
|
|
31
|
+
type ConfigPattern = string | readonly string[];
|
|
32
|
+
/**
|
|
33
|
+
* Resolver interface for importing modules and resolving paths.
|
|
34
|
+
*/
|
|
35
|
+
interface Resolver {
|
|
36
|
+
/**
|
|
37
|
+
* Resolves a module ID to its full path.
|
|
38
|
+
*/
|
|
39
|
+
resolve(id: string): string;
|
|
40
|
+
/**
|
|
41
|
+
* Attempts to resolve a module ID, returns undefined if not found.
|
|
42
|
+
*/
|
|
43
|
+
tryResolve(id: string): string | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* Imports a module dynamically.
|
|
46
|
+
* @param id - Module identifier to import
|
|
47
|
+
* @param options - Import options, set default: true to import default export
|
|
48
|
+
*/
|
|
49
|
+
import<T>(id: string, options?: {
|
|
50
|
+
default?: boolean;
|
|
51
|
+
}): Promise<T>;
|
|
52
|
+
}
|
|
53
|
+
interface ContextOptions {
|
|
54
|
+
logger?: ConsolaInstance;
|
|
55
|
+
/**
|
|
56
|
+
* Current working directory
|
|
57
|
+
*/
|
|
58
|
+
cwd: string;
|
|
59
|
+
/**
|
|
60
|
+
* Base project directory to search drizzle config files
|
|
61
|
+
*/
|
|
62
|
+
baseDir: string;
|
|
63
|
+
/**
|
|
64
|
+
* Pattern for drizzle config files
|
|
65
|
+
*/
|
|
66
|
+
configPattern: ConfigPattern;
|
|
67
|
+
/**
|
|
68
|
+
* Nuxt Kit resolver
|
|
69
|
+
*/
|
|
70
|
+
resolver: Resolver;
|
|
71
|
+
/**
|
|
72
|
+
* Connector options
|
|
73
|
+
*/
|
|
74
|
+
datasource: Record<string, {
|
|
75
|
+
connector: string;
|
|
76
|
+
}>;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Creates a Context instance for managing Drizzle datasource configurations.
|
|
80
|
+
* @param options - Configuration options for the context
|
|
81
|
+
* @returns A Context instance
|
|
82
|
+
*/
|
|
83
|
+
declare function createContext(options: ContextOptions): Context;
|
|
84
|
+
/**
|
|
85
|
+
* Information about a resolved Drizzle datasource.
|
|
86
|
+
*/
|
|
87
|
+
interface DatasourceInfo {
|
|
88
|
+
/** Directory name of the datasource (derived from config file location). */
|
|
89
|
+
dirName: string;
|
|
90
|
+
/** Unique name identifier for the datasource. */
|
|
91
|
+
name: string;
|
|
92
|
+
/** Whether the datasource is enabled (filtered by connector configuration). */
|
|
93
|
+
enabled: boolean;
|
|
94
|
+
/** Database dialect (e.g., 'sqlite', 'postgresql', 'mysql'). */
|
|
95
|
+
dialect: string;
|
|
96
|
+
/** Driver type if specified in config, otherwise undefined. */
|
|
97
|
+
driver: string | undefined;
|
|
98
|
+
/** Import paths for schema, connector, and helpers. */
|
|
99
|
+
imports: {
|
|
100
|
+
config: string;
|
|
101
|
+
schema: string[];
|
|
102
|
+
connector: string;
|
|
103
|
+
helpers: string;
|
|
104
|
+
};
|
|
105
|
+
/** Migration configuration for this datasource. */
|
|
106
|
+
migrations: {
|
|
107
|
+
assets?: string;
|
|
108
|
+
config?: Config["migrations"];
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
export { ConfigPattern, Context, ContextOptions, DatasourceInfo, Resolver, createContext };
|
|
113
|
+
//# sourceMappingURL=context.d.mts.map
|
package/dist/context.mjs
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { mapAsync } from "./context/internal/async.mjs";
|
|
2
|
+
import { accent } from "./context/internal/logger.mjs";
|
|
3
|
+
import { resolveFiles } from "./context/internal/fs.mjs";
|
|
4
|
+
import { augmentations, declarations } from "./context/internal/templates.mjs";
|
|
5
|
+
import { transformDrizzleConfig } from "./context/internal/config.mjs";
|
|
6
|
+
import { virtualModules } from "./context/internal/virtual.mjs";
|
|
7
|
+
import { colorize } from "consola/utils";
|
|
8
|
+
//#region src/context/index.ts
|
|
9
|
+
var DefaultContext = class {
|
|
10
|
+
#datasources = null;
|
|
11
|
+
#options;
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.#options = options;
|
|
14
|
+
}
|
|
15
|
+
async datasources() {
|
|
16
|
+
const { logger, baseDir, configPattern, resolver, datasource: datasourceOptions } = this.#options;
|
|
17
|
+
if (!this.#datasources) {
|
|
18
|
+
logger?.info("Searching drizzle datasources in", colorize("blue", baseDir));
|
|
19
|
+
let datasources = await mapAsync(await resolveFiles(baseDir, [configPattern].flat().map((pattern) => "*/" + pattern)), async (path) => {
|
|
20
|
+
const config = await this.#options.resolver.import(path, { default: true });
|
|
21
|
+
const [_, dirName] = path.match(/(.+\/(.+))\/.+$/).slice(1, 3);
|
|
22
|
+
return await transformDrizzleConfig(config, {
|
|
23
|
+
cwd: this.#options.cwd,
|
|
24
|
+
path,
|
|
25
|
+
dirName,
|
|
26
|
+
resolver
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
datasources = Object.entries(datasourceOptions).reduce((_datasources, [name, options]) => {
|
|
30
|
+
const datasource = datasources.find((d) => d.name == name && options.connector == (d.driver ? d.driver : d.dialect));
|
|
31
|
+
if (!datasource) throw new Error("Unable to resolve datasource " + name);
|
|
32
|
+
return [..._datasources, datasource];
|
|
33
|
+
}, []);
|
|
34
|
+
logger?.info("Found drizzle datasources:", datasources.toSorted((datasource) => {
|
|
35
|
+
return datasource.enabled ? -1 : 1;
|
|
36
|
+
}).map((datasource) => {
|
|
37
|
+
let msg = [datasource.name];
|
|
38
|
+
let color;
|
|
39
|
+
if (datasource.enabled) color = "greenBright";
|
|
40
|
+
else {
|
|
41
|
+
color = "gray";
|
|
42
|
+
msg.push("(disabled)");
|
|
43
|
+
}
|
|
44
|
+
return colorize(color, msg.join(" "));
|
|
45
|
+
}).join(", "));
|
|
46
|
+
const enabledDatasources = datasources.filter((d) => d.enabled);
|
|
47
|
+
logger?.info(accent`Using ${enabledDatasources.length} of ${datasources.length} resolved datasources` + (enabledDatasources.length > 0 ? ":" : ""), enabledDatasources.map((datasource) => {
|
|
48
|
+
return [colorize("greenBright", datasource.name), colorize("yellow", "(" + (datasource.driver || datasource.dialect) + ")")].join(" ");
|
|
49
|
+
}).join(", "));
|
|
50
|
+
this.#datasources = datasources;
|
|
51
|
+
}
|
|
52
|
+
return this.#datasources;
|
|
53
|
+
}
|
|
54
|
+
async augmentations() {
|
|
55
|
+
return augmentations(await this.datasources());
|
|
56
|
+
}
|
|
57
|
+
async declarations() {
|
|
58
|
+
return declarations(await this.datasources());
|
|
59
|
+
}
|
|
60
|
+
async virtualModules() {
|
|
61
|
+
return virtualModules(await this.datasources());
|
|
62
|
+
}
|
|
63
|
+
reload() {
|
|
64
|
+
this.#datasources = null;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Creates a Context instance for managing Drizzle datasource configurations.
|
|
69
|
+
* @param options - Configuration options for the context
|
|
70
|
+
* @returns A Context instance
|
|
71
|
+
*/
|
|
72
|
+
function createContext(options) {
|
|
73
|
+
return new DefaultContext(options);
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
export { createContext };
|
|
77
|
+
|
|
78
|
+
//# sourceMappingURL=context.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.mjs","names":["#options","#datasources"],"sources":["../src/context/index.ts"],"sourcesContent":["import type { ConsolaInstance } from \"consola\";\nimport { colorize, type ColorName } from \"consola/utils\";\nimport type { Config, Config as DrizzleConfig } from \"drizzle-kit\";\n\nimport { mapAsync } from \"./internal/async\";\nimport { accent } from \"./internal/logger\";\nimport { resolveFiles } from \"./internal/fs\";\nimport { augmentations, declarations } from \"./internal/templates\";\nimport { transformDrizzleConfig } from \"./internal/config\";\nimport { virtualModules } from \"./internal/virtual\";\n\nimport type { MaybePromise, VirtualModules } from \"nitro-drizzle/shared\";\n\n/**\n * Context interface for managing Drizzle datasource configurations.\n */\nexport interface Context {\n /**\n * Reloads the context, clearing any cached datasource information.\n */\n reload(): MaybePromise<void>;\n /**\n * Returns a list of all resolved datasource information.\n */\n datasources(): MaybePromise<DatasourceInfo[]>;\n /**\n * Returns type declaration code for all datasources.\n */\n augmentations(): MaybePromise<VirtualModules>;\n\n declarations(): MaybePromise<Record<string, VirtualModules>>;\n /**\n * Returns virtual module code for runtime datasource access.\n */\n virtualModules(): MaybePromise<VirtualModules>;\n}\n\nclass DefaultContext implements Context {\n #datasources: DatasourceInfo[] | null = null;\n readonly #options: ContextOptions;\n\n constructor(options: ContextOptions) {\n this.#options = options;\n }\n\n async datasources() {\n const {\n logger,\n baseDir,\n configPattern,\n resolver,\n datasource: datasourceOptions,\n } = this.#options;\n\n if (!this.#datasources) {\n logger?.info(\"Searching drizzle datasources in\", colorize(\"blue\", baseDir));\n\n const drizzleConfigsResolvedPaths = await resolveFiles(\n baseDir,\n [configPattern].flat().map((pattern) => \"*/\" + pattern),\n );\n\n let datasources: DatasourceInfo[] = await mapAsync(\n drizzleConfigsResolvedPaths,\n async (path) => {\n const config = await this.#options.resolver.import<DrizzleConfig>(path, {\n default: true,\n });\n const [_, dirName] = path.match(/(.+\\/(.+))\\/.+$/)!.slice(1, 3) as [string, string];\n\n return await transformDrizzleConfig(config, {\n cwd: this.#options.cwd,\n path,\n dirName,\n resolver,\n });\n },\n );\n\n datasources = Object.entries(datasourceOptions).reduce((_datasources, [name, options]) => {\n const datasource = datasources.find(\n (d) => d.name == name && options.connector == (d.driver ? d.driver : d.dialect),\n );\n if (!datasource) {\n throw new Error(\"Unable to resolve datasource \" + name);\n }\n return [..._datasources, datasource];\n }, [] as DatasourceInfo[]);\n\n logger?.info(\n \"Found drizzle datasources:\",\n datasources\n .toSorted((datasource) => {\n return datasource.enabled ? -1 : 1;\n })\n .map((datasource) => {\n let msg = [datasource.name];\n let color: ColorName;\n if (datasource.enabled) {\n color = \"greenBright\";\n } else {\n color = \"gray\";\n msg.push(\"(disabled)\");\n }\n return colorize(color, msg.join(\" \"));\n })\n .join(\", \"),\n );\n\n const enabledDatasources = datasources.filter((d) => d.enabled);\n\n logger?.info(\n accent`Using ${enabledDatasources.length} of ${datasources.length} resolved datasources` +\n (enabledDatasources.length > 0 ? \":\" : \"\"),\n enabledDatasources\n .map((datasource) => {\n return [\n colorize(\"greenBright\", datasource.name),\n colorize(\"yellow\", \"(\" + (datasource.driver || datasource.dialect) + \")\"),\n ].join(\" \");\n })\n .join(\", \"),\n );\n\n this.#datasources = datasources;\n }\n\n return this.#datasources;\n }\n\n async augmentations(): Promise<VirtualModules> {\n return augmentations(await this.datasources());\n }\n\n async declarations(): Promise<Record<string, VirtualModules>> {\n return declarations(await this.datasources());\n }\n\n async virtualModules(): Promise<VirtualModules> {\n return virtualModules(await this.datasources());\n }\n\n reload() {\n this.#datasources = null;\n }\n}\n\n/**\n * Glob pattern or array of patterns to match Drizzle configuration files.\n */\nexport type ConfigPattern = string | readonly string[];\n\n/**\n * Resolver interface for importing modules and resolving paths.\n */\nexport interface Resolver {\n /**\n * Resolves a module ID to its full path.\n */\n resolve(id: string): string;\n /**\n * Attempts to resolve a module ID, returns undefined if not found.\n */\n tryResolve(id: string): string | undefined;\n /**\n * Imports a module dynamically.\n * @param id - Module identifier to import\n * @param options - Import options, set default: true to import default export\n */\n import<T>(id: string, options?: { default?: boolean }): Promise<T>;\n}\n\nexport interface ContextOptions {\n logger?: ConsolaInstance;\n /**\n * Current working directory\n */\n cwd: string;\n /**\n * Base project directory to search drizzle config files\n */\n baseDir: string;\n /**\n * Pattern for drizzle config files\n */\n configPattern: ConfigPattern;\n /**\n * Nuxt Kit resolver\n */\n resolver: Resolver;\n /**\n * Connector options\n */\n datasource: Record<string, { connector: string }>;\n}\n\n/**\n * Creates a Context instance for managing Drizzle datasource configurations.\n * @param options - Configuration options for the context\n * @returns A Context instance\n */\nexport function createContext(options: ContextOptions): Context {\n return new DefaultContext(options);\n}\n\n/**\n * Information about a resolved Drizzle datasource.\n */\nexport interface DatasourceInfo {\n /** Directory name of the datasource (derived from config file location). */\n dirName: string;\n /** Unique name identifier for the datasource. */\n name: string;\n /** Whether the datasource is enabled (filtered by connector configuration). */\n enabled: boolean;\n /** Database dialect (e.g., 'sqlite', 'postgresql', 'mysql'). */\n dialect: string;\n /** Driver type if specified in config, otherwise undefined. */\n driver: string | undefined;\n /** Import paths for schema, connector, and helpers. */\n imports: {\n config: string;\n schema: string[];\n connector: string;\n helpers: string;\n };\n /** Migration configuration for this datasource. */\n migrations: {\n assets?: string;\n config?: Config[\"migrations\"];\n };\n}\n"],"mappings":";;;;;;;;AAqCA,IAAM,iBAAN,MAAwC;CACtC,eAAwC;CACxC;CAEA,YAAY,SAAyB;EACnC,KAAKA,WAAW;CAClB;CAEA,MAAM,cAAc;EAClB,MAAM,EACJ,QACA,SACA,eACA,UACA,YAAY,sBACV,KAAKA;EAET,IAAI,CAAC,KAAKC,cAAc;GACtB,QAAQ,KAAK,oCAAoC,SAAS,QAAQ,OAAO,CAAC;GAO1E,IAAI,cAAgC,MAAM,SACxC,MANwC,aACxC,SACA,CAAC,aAAa,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,YAAY,OAAO,OAAO,CACxD,GAIE,OAAO,SAAS;IACd,MAAM,SAAS,MAAM,KAAKD,SAAS,SAAS,OAAsB,MAAM,EACtE,SAAS,KACX,CAAC;IACD,MAAM,CAAC,GAAG,WAAW,KAAK,MAAM,iBAAiB,CAAC,CAAE,MAAM,GAAG,CAAC;IAE9D,OAAO,MAAM,uBAAuB,QAAQ;KAC1C,KAAK,KAAKA,SAAS;KACnB;KACA;KACA;IACF,CAAC;GACH,CACF;GAEA,cAAc,OAAO,QAAQ,iBAAiB,CAAC,CAAC,QAAQ,cAAc,CAAC,MAAM,aAAa;IACxF,MAAM,aAAa,YAAY,MAC5B,MAAM,EAAE,QAAQ,QAAQ,QAAQ,cAAc,EAAE,SAAS,EAAE,SAAS,EAAE,QACzE;IACA,IAAI,CAAC,YACH,MAAM,IAAI,MAAM,kCAAkC,IAAI;IAExD,OAAO,CAAC,GAAG,cAAc,UAAU;GACrC,GAAG,CAAC,CAAqB;GAEzB,QAAQ,KACN,8BACA,YACG,UAAU,eAAe;IACxB,OAAO,WAAW,UAAU,KAAK;GACnC,CAAC,CAAC,CACD,KAAK,eAAe;IACnB,IAAI,MAAM,CAAC,WAAW,IAAI;IAC1B,IAAI;IACJ,IAAI,WAAW,SACb,QAAQ;SACH;KACL,QAAQ;KACR,IAAI,KAAK,YAAY;IACvB;IACA,OAAO,SAAS,OAAO,IAAI,KAAK,GAAG,CAAC;GACtC,CAAC,CAAC,CACD,KAAK,IAAI,CACd;GAEA,MAAM,qBAAqB,YAAY,QAAQ,MAAM,EAAE,OAAO;GAE9D,QAAQ,KACN,MAAM,SAAS,mBAAmB,OAAO,MAAM,YAAY,OAAO,0BAC/D,mBAAmB,SAAS,IAAI,MAAM,KACzC,mBACG,KAAK,eAAe;IACnB,OAAO,CACL,SAAS,eAAe,WAAW,IAAI,GACvC,SAAS,UAAU,OAAO,WAAW,UAAU,WAAW,WAAW,GAAG,CAC1E,CAAC,CAAC,KAAK,GAAG;GACZ,CAAC,CAAC,CACD,KAAK,IAAI,CACd;GAEA,KAAKC,eAAe;EACtB;EAEA,OAAO,KAAKA;CACd;CAEA,MAAM,gBAAyC;EAC7C,OAAO,cAAc,MAAM,KAAK,YAAY,CAAC;CAC/C;CAEA,MAAM,eAAwD;EAC5D,OAAO,aAAa,MAAM,KAAK,YAAY,CAAC;CAC9C;CAEA,MAAM,iBAA0C;EAC9C,OAAO,eAAe,MAAM,KAAK,YAAY,CAAC;CAChD;CAEA,SAAS;EACP,KAAKA,eAAe;CACtB;AACF;;;;;;AAwDA,SAAgB,cAAc,SAAkC;CAC9D,OAAO,IAAI,eAAe,OAAO;AACnC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { InferMysqlTable } from "./mysql.mjs";
|
|
2
|
+
import { InferPgTable } from "./postgresql.mjs";
|
|
3
|
+
import { InferSqliteTable } from "./sqlite.mjs";
|
|
4
|
+
import { Column, SQL, Table } from "drizzle-orm";
|
|
5
|
+
import { SQLiteInsertBase } from "drizzle-orm/sqlite-core";
|
|
6
|
+
import { MySqlInsertBase } from "drizzle-orm/mysql-core";
|
|
7
|
+
import { PgInsertBase } from "drizzle-orm/pg-core";
|
|
8
|
+
|
|
9
|
+
//#region src/dialects/index.d.ts
|
|
10
|
+
interface OnConflictOptions<TSet extends Record<string, SQL | undefined>> {
|
|
11
|
+
set: TSet;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Options for ON CONFLICT DO UPDATE clause.
|
|
15
|
+
* @template T - The insert type
|
|
16
|
+
*/
|
|
17
|
+
type OnConflictDoUpdateOptions<T extends AnyInsert> = OnConflictOptions<{ [K in keyof InferColumns<InferTable<T>>]?: SQL }>;
|
|
18
|
+
/**
|
|
19
|
+
* Infers column types from a Drizzle table.
|
|
20
|
+
* @template TTable - The table type
|
|
21
|
+
*/
|
|
22
|
+
type InferColumns<TTable extends Table> = TTable extends Table<infer TTableConfig> ? TTableConfig["columns"] : never;
|
|
23
|
+
type AnyInsert = MySqlInsertBase<any, any, any, any, any, any> | PgInsertBase<any, any, any, any, any, any> | SQLiteInsertBase<any, any, any, any, any, any>;
|
|
24
|
+
/**
|
|
25
|
+
* Infers the table type from an insert statement.
|
|
26
|
+
* @template T - The insert type
|
|
27
|
+
*/
|
|
28
|
+
type InferTable<T extends AnyInsert> = T extends MySqlInsertBase<any, any, any, any, any, any> ? InferMysqlTable<T> : T extends PgInsertBase<any, any, any, any, any, any> ? InferPgTable<T> : T extends SQLiteInsertBase<any, any, any, any, any, any> ? InferSqliteTable<T> : never;
|
|
29
|
+
type RemoveNeverProps<T extends Record<string, any>> = { [K in keyof T as T[K] extends never ? never : K]: T[K] };
|
|
30
|
+
/**
|
|
31
|
+
* Infers primary key columns from a Drizzle table.
|
|
32
|
+
* @template T - The table type
|
|
33
|
+
*/
|
|
34
|
+
type InferPrimaryColumns<T extends Table> = RemoveNeverProps<{ [K in keyof InferColumns<T>]: InferColumns<T>[K] extends Column<infer TColumnConfig> ? Extract<TColumnConfig, {
|
|
35
|
+
isPrimaryKey: true;
|
|
36
|
+
}> extends never ? never : InferColumns<T>[K] : never }>;
|
|
37
|
+
//#endregion
|
|
38
|
+
export { InferColumns, InferPrimaryColumns, InferTable, OnConflictDoUpdateOptions };
|
|
39
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { InferPrimaryColumns, InferTable, OnConflictDoUpdateOptions } from "./index.mjs";
|
|
2
|
+
import { MySqlInsertBase } from "drizzle-orm/mysql-core";
|
|
3
|
+
|
|
4
|
+
//#region src/dialects/mysql.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Adds an ON CONFLICT DO UPDATE clause to a MySQL insert statement.
|
|
7
|
+
* @template TInsert - The insert type
|
|
8
|
+
* @param _ - Primary columns for conflict detection (used internally)
|
|
9
|
+
* @param insert - The insert statement
|
|
10
|
+
* @param options - Options containing the SET values
|
|
11
|
+
* @returns The modified insert statement
|
|
12
|
+
*/
|
|
13
|
+
declare function onConflictDoUpdate<TInsert extends MySqlInsertBase<any, any, any, any, any, any>>(_: InferPrimaryColumns<InferTable<TInsert>>, insert: TInsert, options: OnConflictDoUpdateOptions<TInsert>): TInsert | Omit<MySqlInsertBase<TInsert["_"]["table"], TInsert["_"]["queryResult"], TInsert["_"]["preparedQueryHKT"], TInsert["_"]["returning"], any, "$returning" | TInsert["_"]["excludedMethods"]>, "onDuplicateKeyUpdate" | TInsert["_"]["excludedMethods"]>;
|
|
14
|
+
/**
|
|
15
|
+
* Adds an ON CONFLICT DO NOTHING clause to a MySQL insert statement.
|
|
16
|
+
* Uses ON DUPLICATE KEY UPDATE with the primary key values for equivalent behavior.
|
|
17
|
+
* @template TInsert - The insert type
|
|
18
|
+
* @param target - Primary columns for conflict detection
|
|
19
|
+
* @param insert - The insert statement
|
|
20
|
+
* @returns The modified insert statement
|
|
21
|
+
*/
|
|
22
|
+
declare function onConflictDoNothing<TInsert extends MySqlInsertBase<any, any, any, any, any, any>>(target: InferPrimaryColumns<InferTable<TInsert>>, insert: TInsert): TInsert | Omit<MySqlInsertBase<TInsert["_"]["table"], TInsert["_"]["queryResult"], TInsert["_"]["preparedQueryHKT"], TInsert["_"]["returning"], any, "$returning" | TInsert["_"]["excludedMethods"]>, "onDuplicateKeyUpdate" | TInsert["_"]["excludedMethods"]>;
|
|
23
|
+
/**
|
|
24
|
+
* Infers the table type from a MySQL insert statement.
|
|
25
|
+
* @template T - The insert type
|
|
26
|
+
*/
|
|
27
|
+
type InferMysqlTable<T extends MySqlInsertBase<any, any, any, any, any, any>> = T extends MySqlInsertBase<infer TTable, any, any, any, any, any> ? TTable : never;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { InferMysqlTable, onConflictDoNothing, onConflictDoUpdate };
|
|
30
|
+
//# sourceMappingURL=mysql.d.mts.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { sql } from "drizzle-orm/sql";
|
|
2
|
+
//#region src/dialects/mysql.ts
|
|
3
|
+
/**
|
|
4
|
+
* Adds an ON CONFLICT DO UPDATE clause to a MySQL insert statement.
|
|
5
|
+
* @template TInsert - The insert type
|
|
6
|
+
* @param _ - Primary columns for conflict detection (used internally)
|
|
7
|
+
* @param insert - The insert statement
|
|
8
|
+
* @param options - Options containing the SET values
|
|
9
|
+
* @returns The modified insert statement
|
|
10
|
+
*/
|
|
11
|
+
function onConflictDoUpdate(_, insert, options) {
|
|
12
|
+
return insert.onDuplicateKeyUpdate({ set: options.set });
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Adds an ON CONFLICT DO NOTHING clause to a MySQL insert statement.
|
|
16
|
+
* Uses ON DUPLICATE KEY UPDATE with the primary key values for equivalent behavior.
|
|
17
|
+
* @template TInsert - The insert type
|
|
18
|
+
* @param target - Primary columns for conflict detection
|
|
19
|
+
* @param insert - The insert statement
|
|
20
|
+
* @returns The modified insert statement
|
|
21
|
+
*/
|
|
22
|
+
function onConflictDoNothing(target, insert) {
|
|
23
|
+
return onConflictDoUpdate(target, insert, { set: Object.fromEntries(Object.entries(target).map(([name, column]) => {
|
|
24
|
+
return [name, sql`${column}`];
|
|
25
|
+
})) });
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
export { onConflictDoNothing, onConflictDoUpdate };
|
|
29
|
+
|
|
30
|
+
//# sourceMappingURL=mysql.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mysql.mjs","names":[],"sources":["../../src/dialects/mysql.ts"],"sourcesContent":["import type { MySqlInsertBase } from \"drizzle-orm/mysql-core\";\nimport type { InferColumns, InferPrimaryColumns, InferTable, OnConflictDoUpdateOptions } from \".\";\nimport { type SQL, sql } from \"drizzle-orm/sql\";\n\n/**\n * Adds an ON CONFLICT DO UPDATE clause to a MySQL insert statement.\n * @template TInsert - The insert type\n * @param _ - Primary columns for conflict detection (used internally)\n * @param insert - The insert statement\n * @param options - Options containing the SET values\n * @returns The modified insert statement\n */\nexport function onConflictDoUpdate<TInsert extends MySqlInsertBase<any, any, any, any, any, any>>(\n _: InferPrimaryColumns<InferTable<TInsert>>,\n insert: TInsert,\n options: OnConflictDoUpdateOptions<TInsert>,\n):\n | TInsert\n | Omit<\n MySqlInsertBase<\n TInsert[\"_\"][\"table\"],\n TInsert[\"_\"][\"queryResult\"],\n TInsert[\"_\"][\"preparedQueryHKT\"],\n TInsert[\"_\"][\"returning\"],\n any,\n \"$returning\" | TInsert[\"_\"][\"excludedMethods\"]\n >,\n \"onDuplicateKeyUpdate\" | TInsert[\"_\"][\"excludedMethods\"]\n > {\n return insert.onDuplicateKeyUpdate({\n set: options.set,\n });\n}\n\n/**\n * Adds an ON CONFLICT DO NOTHING clause to a MySQL insert statement.\n * Uses ON DUPLICATE KEY UPDATE with the primary key values for equivalent behavior.\n * @template TInsert - The insert type\n * @param target - Primary columns for conflict detection\n * @param insert - The insert statement\n * @returns The modified insert statement\n */\nexport function onConflictDoNothing<TInsert extends MySqlInsertBase<any, any, any, any, any, any>>(\n target: InferPrimaryColumns<InferTable<TInsert>>,\n insert: TInsert,\n):\n | TInsert\n | Omit<\n MySqlInsertBase<\n TInsert[\"_\"][\"table\"],\n TInsert[\"_\"][\"queryResult\"],\n TInsert[\"_\"][\"preparedQueryHKT\"],\n TInsert[\"_\"][\"returning\"],\n any,\n \"$returning\" | TInsert[\"_\"][\"excludedMethods\"]\n >,\n \"onDuplicateKeyUpdate\" | TInsert[\"_\"][\"excludedMethods\"]\n > {\n const set = Object.fromEntries(\n Object.entries(target).map(([name, column]) => {\n return [name, sql`${column}`];\n }),\n ) as Record<keyof InferColumns<InferTable<TInsert>>, SQL>;\n\n return onConflictDoUpdate(target, insert, { set });\n}\n\n/**\n * Infers the table type from a MySQL insert statement.\n * @template T - The insert type\n */\nexport type InferMysqlTable<T extends MySqlInsertBase<any, any, any, any, any, any>> =\n T extends MySqlInsertBase<infer TTable, any, any, any, any, any> ? TTable : never;\n"],"mappings":";;;;;;;;;;AAYA,SAAgB,mBACd,GACA,QACA,SAaI;CACJ,OAAO,OAAO,qBAAqB,EACjC,KAAK,QAAQ,IACf,CAAC;AACH;;;;;;;;;AAUA,SAAgB,oBACd,QACA,QAaI;CAOJ,OAAO,mBAAmB,QAAQ,QAAQ,EAAE,KANhC,OAAO,YACjB,OAAO,QAAQ,MAAM,CAAC,CAAC,KAAK,CAAC,MAAM,YAAY;EAC7C,OAAO,CAAC,MAAM,GAAG,GAAG,QAAQ;CAC9B,CAAC,CAG2C,EAAE,CAAC;AACnD"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { InferColumns, InferTable, OnConflictDoUpdateOptions } from "./index.mjs";
|
|
2
|
+
import { PgInsertBase } from "drizzle-orm/pg-core";
|
|
3
|
+
|
|
4
|
+
//#region src/dialects/postgresql.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Adds an ON CONFLICT DO UPDATE clause to a PostgreSQL insert statement.
|
|
7
|
+
* @template TInsert - The insert type
|
|
8
|
+
* @param target - Partial object of columns to target for conflict detection
|
|
9
|
+
* @param insert - The insert statement
|
|
10
|
+
* @param options - Options containing the SET values
|
|
11
|
+
* @returns The modified insert statement
|
|
12
|
+
*/
|
|
13
|
+
declare function onConflictDoUpdate<TInsert extends PgInsertBase<any, any, any>>(target: Partial<InferColumns<InferTable<TInsert>>>, insert: TInsert, options: OnConflictDoUpdateOptions<TInsert>): TInsert | Omit<PgInsertBase<TInsert["_"]["table"], TInsert["_"]["queryResult"], TInsert["_"]["selectedFields"], TInsert["_"]["returning"], any, "onConflictDoNothing" | "onConflictDoUpdate" | TInsert["_"]["excludedMethods"]>, "onConflictDoNothing" | "onConflictDoUpdate" | TInsert["_"]["excludedMethods"]>;
|
|
14
|
+
/**
|
|
15
|
+
* Adds an ON CONFLICT DO NOTHING clause to a PostgreSQL insert statement.
|
|
16
|
+
* @template TInsert - The insert type
|
|
17
|
+
* @param _ - Partial object of columns (unused but required for API consistency)
|
|
18
|
+
* @param insert - The insert statement
|
|
19
|
+
* @returns The modified insert statement with DO NOTHING
|
|
20
|
+
*/
|
|
21
|
+
declare function onConflictDoNothing<TInsert extends PgInsertBase<any, any, any>>(target: Partial<InferColumns<InferTable<TInsert>>>, insert: TInsert): TInsert | Omit<PgInsertBase<TInsert["_"]["table"], TInsert["_"]["queryResult"], TInsert["_"]["selectedFields"], TInsert["_"]["returning"], any, "onConflictDoNothing" | "onConflictDoUpdate" | TInsert["_"]["excludedMethods"]>, "onConflictDoNothing" | "onConflictDoUpdate" | TInsert["_"]["excludedMethods"]>;
|
|
22
|
+
/**
|
|
23
|
+
* Infers the table type from a PostgreSQL insert statement.
|
|
24
|
+
* @template T - The insert type
|
|
25
|
+
*/
|
|
26
|
+
type InferPgTable<T extends PgInsertBase<any, any, any, any, any, any>> = T extends PgInsertBase<infer TTable, any, any, any, any, any> ? TTable : never;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { InferPgTable, onConflictDoNothing, onConflictDoUpdate };
|
|
29
|
+
//# sourceMappingURL=postgresql.d.mts.map
|