vite-plugin-postgres-import 0.8.0 → 0.8.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/package.json CHANGED
@@ -1,14 +1,20 @@
1
1
  {
2
2
  "name": "vite-plugin-postgres-import",
3
3
  "description": "Vite plugin for importing SQL files as JS modules with sqlc-style annotations",
4
- "version": "0.8.0",
4
+ "version": "0.8.1",
5
5
  "main": "plugin.js",
6
+ "types": "plugin.d.ts",
6
7
  "type": "module",
8
+ "scripts": {
9
+ "prepack": "tsc -p ."
10
+ },
7
11
  "author": "SirKostya009",
8
12
  "license": "MIT",
9
13
  "repository": "github:sirkostya009/vite-plugin-postgres-import",
10
14
  "files": [
11
- "parse.js"
15
+ "parse.js",
16
+ "plugin.d.ts",
17
+ "plugin.d.ts.map"
12
18
  ],
13
19
  "keywords": [
14
20
  "vite",
@@ -18,20 +24,16 @@
18
24
  "sqlc"
19
25
  ],
20
26
  "devDependencies": {
21
- "@sveltejs/kit": "^2",
22
27
  "@types/pg": "*",
28
+ "typescript": "^7",
23
29
  "vite": "^8"
24
30
  },
25
31
  "peerDependencies": {
26
- "@sveltejs/kit": "^2",
27
32
  "@types/pg": "*",
28
33
  "pg": "*",
29
34
  "pg-cursor": "*"
30
35
  },
31
36
  "peerDependenciesMeta": {
32
- "@sveltejs/kit": {
33
- "optional": true
34
- },
35
37
  "pg-cursor": {
36
38
  "optional": true
37
39
  },
package/parse.js CHANGED
@@ -172,16 +172,7 @@ export function codegen(
172
172
  ) {
173
173
  let dts = [
174
174
  // `declare module '${modulePrefix}${filename}.sql' {`
175
- `import type { QueryResultRow, QueryResult, QueryArrayResult, QueryConfig, QueryArrayConfig, QueryConfigValues, Submittable } from 'pg';
176
-
177
- interface QueryableObject {
178
- query<T extends Submittable>(queryStream: T): T;
179
- query<R extends any[] = any[], I = any[]>(queryConfig: QueryArrayConfig<I>, values?: QueryConfigValues<I>): Promise<QueryArrayResult<R>>;
180
- query<R extends QueryResultRow = any, I = any>(queryConfig: QueryConfig<I>): Promise<QueryResult<R>>;
181
- query<R extends QueryResultRow = any, I = any[]>(queryTextOrConfig: string | QueryConfig<I>, values?: QueryConfigValues<I>): Promise<QueryResult<R>>;
182
- }
183
-
184
- type Queryable = QueryableObject | Promise<QueryableObject>;`,
175
+ `import type { QueryResultRow, QueryResult, QueryArrayResult } from 'pg';\nimport { Queryable } from 'vite-plugin-postgres-import';`,
185
176
  ];
186
177
  let js = [
187
178
  `import { escapeLiteral } from 'pg';\nimport { ${runtime.export} as __runtime } from '${runtime.module}';`,
package/plugin.d.ts ADDED
@@ -0,0 +1,68 @@
1
+ import type { QueryArrayConfig, QueryArrayResult, QueryConfig, QueryConfigValues, QueryResult, QueryResultRow, Submittable } from "pg";
2
+ import type { Plugin } from "vite";
3
+ export type QueryableObject = {
4
+ query<T extends Submittable>(queryStream: T): T;
5
+ query<R extends any[] = any[], I = any[]>(queryConfig: QueryArrayConfig<I>, values?: QueryConfigValues<I>): Promise<QueryArrayResult<R>>;
6
+ query<R extends QueryResultRow = any, I = any>(queryConfig: QueryConfig<I>): Promise<QueryResult<R>>;
7
+ query<R extends QueryResultRow = any, I = any[]>(queryTextOrConfig: string | QueryConfig<I>, values?: QueryConfigValues<I>): Promise<QueryResult<R>>;
8
+ };
9
+ export type Queryable = QueryableObject | Promise<QueryableObject>;
10
+ export type PostgresOptions = {
11
+ /**
12
+ * Path to a folder where all declaration are kept relative to `rootFolder`, i.e. a file at path `src/sql/module.sql` will have its `.d.ts` file generated into `${typesFolder}/src/sql/module.sql.d.ts`. Make sure you include this one in your `tsconfig.json`. Defaults to `"node_modules/@types/vite-plugin-postgres-import/"`.
13
+ */
14
+ typesFolder?: string;
15
+ /**
16
+ * Root folder relative to which path calculation will be happening. Defaults to `process.cwd()`.
17
+ */
18
+ rootFolder?: string;
19
+ /**
20
+ * Where the default runtime comes from.
21
+ */
22
+ runtime?: RuntimeOptions;
23
+ };
24
+ export type RuntimeOptions = {
25
+ /**
26
+ * Module specifier the default runtime is imported from in every generated module. Defaults to `"#db-runtime"`.
27
+ */
28
+ module?: string;
29
+ /**
30
+ * Named export of `module` providing the default runtime. Defaults to `"db"`.
31
+ */
32
+ export?: string;
33
+ /**
34
+ * Whether the export is a `Queryable` itself or a function returning one (possibly async). Defaults to `"object"`.
35
+ */
36
+ type?: "object" | "function";
37
+ };
38
+ /**
39
+ * @import { QueryArrayConfig, QueryArrayResult, QueryConfig, QueryConfigValues, QueryResult, QueryResultRow, Submittable } from "pg"
40
+ * @import { Plugin, TransformResult } from "vite"
41
+ */
42
+ /**
43
+ * @typedef {{
44
+ * query<T extends Submittable>(queryStream: T): T;
45
+ * query<R extends any[] = any[], I = any[]>(queryConfig: QueryArrayConfig<I>, values?: QueryConfigValues<I>): Promise<QueryArrayResult<R>>;
46
+ * query<R extends QueryResultRow = any, I = any>(queryConfig: QueryConfig<I>): Promise<QueryResult<R>>;
47
+ * query<R extends QueryResultRow = any, I = any[]>(queryTextOrConfig: string | QueryConfig<I>, values?: QueryConfigValues<I>): Promise<QueryResult<R>>;
48
+ * }} QueryableObject
49
+ */
50
+ /** @typedef {QueryableObject | Promise<QueryableObject>} Queryable */
51
+ /**
52
+ * @typedef {object} PostgresOptions
53
+ * @property {string} [typesFolder] Path to a folder where all declaration are kept relative to `rootFolder`, i.e. a file at path `src/sql/module.sql` will have its `.d.ts` file generated into `${typesFolder}/src/sql/module.sql.d.ts`. Make sure you include this one in your `tsconfig.json`. Defaults to `"node_modules/@types/vite-plugin-postgres-import/"`.
54
+ * @property {string} [rootFolder] Root folder relative to which path calculation will be happening. Defaults to `process.cwd()`.
55
+ * @property {RuntimeOptions} [runtime] Where the default runtime comes from.
56
+ */
57
+ /**
58
+ * @typedef {object} RuntimeOptions
59
+ * @property {string} [module] Module specifier the default runtime is imported from in every generated module. Defaults to `"#db-runtime"`.
60
+ * @property {string} [export] Named export of `module` providing the default runtime. Defaults to `"db"`.
61
+ * @property {"object" | "function"} [type] Whether the export is a `Queryable` itself or a function returning one (possibly async). Defaults to `"object"`.
62
+ */
63
+ /**
64
+ * @param {PostgresOptions} [opts]
65
+ * @returns {Promise<Plugin>}
66
+ */
67
+ export default function postgres(opts?: PostgresOptions): Promise<Plugin>;
68
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["plugin.js"],"names":[],"mappings":"AAKG,OAAQ,KAAA,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,WAAW,EAAE,iBAAiB,EAAE,WAAW,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,IAAI,CAClI;AAAA,OAAQ,KAAA,EAAE,MAAM,EAAmB,MAAM,MAAM,CAAA;AAI/C,YAKG,eAAe,GALR;IACR,KAAK,CAAC,CAAC,SAAS,WAAW,EAAE,WAAW,EAAE,CAAC,GAAG,CAAC,CAAC;IAChD,KAAK,CAAC,CAAC,SAAS,GAAG,EAAE,GAAG,GAAG,EAAE,EAAE,CAAC,GAAG,GAAG,EAAE,EAAE,WAAW,EAAE,gBAAgB,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC;IACzI,KAAK,CAAC,CAAC,SAAS,cAAc,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;IACrG,KAAK,CAAC,CAAC,SAAS,cAAc,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,EAAE,EAAE,iBAAiB,EAAE,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;CACtJ,CAAiB;AAGjB,YAAsD,SAAS,GAArD,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC,CAAW;AAGhE,YAAkB,eAAe,GACjC;;;;IAAoB,WAAW,AAA/B,CACA,EADW,MAAM,CACjB;;;;IAAoB,UAAU,AAA9B,CACA,EADW,MAAM,CACjB;;;;IAA4B,OAAO,AAAnC,CACF,EADa,cAAc,CAC3B;CAAA,CAAA;AAGE,YAAkB,cAAc,GAChC;;;;IAAoB,MAAM,AAA1B,CACA,EADW,MAAM,CACjB;;;;IAAoB,MAAM,AAA1B,CACA,EADW,MAAM,CACjB;;;;IAAmC,IAAI,AAAvC,CACF,EADa,QAAQ,GAAG,UAAU,CAClC;CAAA,CAAA;AA5BD;;;GAGG;AAEH;;;;;;;GAOG;AAEH,sEAAsE;AAEtE;;;;;GAKG;AAEH;;;;;GAKG;AAEH;;;GAGG;AACH,wBAA8B,QAAQ,CAAC,IAAI,AAHxC,CACA,EADQ,eAGqC,GAFnC,OAAO,CAAC,MAAM,CAAC,CAiJ3B"}
package/plugin.js CHANGED
@@ -3,27 +3,45 @@ import path from "node:path";
3
3
  import { codegen, encodeMappings, exportMappings, parseModule } from "./parse.js";
4
4
 
5
5
  /**
6
- * @param {object} [opts]
7
- * @param {string} [opts.typesFolder="node_modules/@types/vite-plugin-postgres-import/"]
8
- * Path to a folder where all declarations are kept relative to `rootFolder`, i.e. a file at path
9
- * `src/sql/module.sql` will have its `.d.ts` file generated into `${typesFolder}/src/sql/module.sql.d.ts`.
10
- * Make sure you include this one in your `tsconfig.json`.
11
- * @param {string} [opts.rootFolder=process.cwd()]
12
- * Root folder relative to which path calculation will be happening.
13
- * @param {object} [opts.runtime] Where the default runtime comes from.
14
- * @param {string} [opts.runtime.module="#db-runtime"]
15
- * Module specifier the default runtime is imported from in every generated module.
16
- * @param {string} [opts.runtime.export="db"]
17
- * Named export of `runtime.module` providing the default runtime.
18
- * @param {"object" | "function"} [opts.runtime.type="object"]
19
- * Whether the runtime export is a `Queryable` itself or a function returning one (possibly async).
20
- * @returns {Promise<import('vite').Plugin>}
6
+ * @import { QueryArrayConfig, QueryArrayResult, QueryConfig, QueryConfigValues, QueryResult, QueryResultRow, Submittable } from "pg"
7
+ * @import { Plugin, TransformResult } from "vite"
21
8
  */
22
- export default async function postgres({
23
- typesFolder = "node_modules/@types/vite-plugin-postgres-import/",
24
- rootFolder = process.cwd(),
25
- runtime: { module: runtimeModule = "#db-runtime", export: runtimeExport = "db", type: runtimeType = "object" } = {},
26
- } = {}) {
9
+
10
+ /**
11
+ * @typedef {{
12
+ * query<T extends Submittable>(queryStream: T): T;
13
+ * query<R extends any[] = any[], I = any[]>(queryConfig: QueryArrayConfig<I>, values?: QueryConfigValues<I>): Promise<QueryArrayResult<R>>;
14
+ * query<R extends QueryResultRow = any, I = any>(queryConfig: QueryConfig<I>): Promise<QueryResult<R>>;
15
+ * query<R extends QueryResultRow = any, I = any[]>(queryTextOrConfig: string | QueryConfig<I>, values?: QueryConfigValues<I>): Promise<QueryResult<R>>;
16
+ * }} QueryableObject
17
+ */
18
+
19
+ /** @typedef {QueryableObject | Promise<QueryableObject>} Queryable */
20
+
21
+ /**
22
+ * @typedef {object} PostgresOptions
23
+ * @property {string} [typesFolder] Path to a folder where all declaration are kept relative to `rootFolder`, i.e. a file at path `src/sql/module.sql` will have its `.d.ts` file generated into `${typesFolder}/src/sql/module.sql.d.ts`. Make sure you include this one in your `tsconfig.json`. Defaults to `"node_modules/@types/vite-plugin-postgres-import/"`.
24
+ * @property {string} [rootFolder] Root folder relative to which path calculation will be happening. Defaults to `process.cwd()`.
25
+ * @property {RuntimeOptions} [runtime] Where the default runtime comes from.
26
+ */
27
+
28
+ /**
29
+ * @typedef {object} RuntimeOptions
30
+ * @property {string} [module] Module specifier the default runtime is imported from in every generated module. Defaults to `"#db-runtime"`.
31
+ * @property {string} [export] Named export of `module` providing the default runtime. Defaults to `"db"`.
32
+ * @property {"object" | "function"} [type] Whether the export is a `Queryable` itself or a function returning one (possibly async). Defaults to `"object"`.
33
+ */
34
+
35
+ /**
36
+ * @param {PostgresOptions} [opts]
37
+ * @returns {Promise<Plugin>}
38
+ */
39
+ export default async function postgres(opts = {}) {
40
+ const {
41
+ typesFolder = "node_modules/@types/vite-plugin-postgres-import/",
42
+ rootFolder = process.cwd(),
43
+ runtime: { module: runtimeModule = "#db-runtime", export: runtimeExport = "db", type: runtimeType = "object" } = {},
44
+ } = opts;
27
45
  const runtime = { module: runtimeModule, export: runtimeExport, type: runtimeType };
28
46
  const imports = await fs
29
47
  .readFile(path.join(rootFolder, "package.json"), "utf8")
@@ -86,7 +104,7 @@ export default async function postgres({
86
104
  },
87
105
  };
88
106
 
89
- /** @returns {Promise<import("vite").TransformResult>} */
107
+ /** @returns {Promise<TransformResult | undefined>} */
90
108
  async function transform(/** @type {string} */ sql, /** @type {string} */ id) {
91
109
  if (!/\.sql$/.test(id)) {
92
110
  return;