@observablehq/notebook-kit 1.1.0-rc.12 → 1.1.0-rc.13

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/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/observablehq/notebook-kit.git"
7
7
  },
8
- "version": "1.1.0-rc.12",
8
+ "version": "1.1.0-rc.13",
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "test": "vitest",
@@ -67,6 +67,7 @@
67
67
  "@eslint/js": "^9.29.0",
68
68
  "@types/jsdom": "^21.1.7",
69
69
  "@types/markdown-it": "^14.1.2",
70
+ "bun-types": "^1.2.20",
70
71
  "eslint": "^9.29.0",
71
72
  "globals": "^16.2.0",
72
73
  "htl": "^0.3.1",
@@ -1,6 +1,6 @@
1
1
  import type { ColumnSchema, QueryParam } from "../runtime/index.js";
2
2
  export { hash as getQueryHash, nameHash as getNameHash } from "../lib/hash.js";
3
- export type DatabaseConfig = DuckDBConfig | SnowflakeConfig | PostgresConfig;
3
+ export type DatabaseConfig = DuckDBConfig | SQLiteConfig | SnowflakeConfig | PostgresConfig;
4
4
  export type DuckDBConfig = {
5
5
  type: "duckdb";
6
6
  path?: string;
@@ -8,6 +8,10 @@ export type DuckDBConfig = {
8
8
  [key: string]: string;
9
9
  };
10
10
  };
11
+ export type SQLiteConfig = {
12
+ type: "sqlite";
13
+ path?: string;
14
+ };
11
15
  export type SnowflakeConfig = {
12
16
  type: "snowflake";
13
17
  account: string;
@@ -22,8 +22,12 @@ export async function getDatabaseConfig(sourcePath, databaseName) {
22
22
  config = { type: "postgres" };
23
23
  else if (databaseName === "duckdb")
24
24
  config = { type: "duckdb" };
25
- else if (/\.(duck)?db$/i.test(databaseName))
25
+ else if (databaseName === "sqlite")
26
+ config = { type: "sqlite" };
27
+ else if (/\.duckdb$/i.test(databaseName))
26
28
  config = { type: "duckdb", path: databaseName };
29
+ else if (/\.db$/i.test(databaseName))
30
+ config = { type: "sqlite", path: databaseName }; // TODO disambiguate
27
31
  else
28
32
  throw new Error(`database not found: ${databaseName}`);
29
33
  }
@@ -33,6 +37,8 @@ export async function getDatabase(config, context) {
33
37
  switch (config.type) {
34
38
  case "duckdb":
35
39
  return (await import("./duckdb.js")).default(config, context);
40
+ case "sqlite":
41
+ return (await (typeof Bun !== "undefined" ? import("./sqlite-bun.js") : import("./sqlite-node.js"))).default(config, context);
36
42
  case "snowflake":
37
43
  return (await import("./snowflake.js")).default(config);
38
44
  case "postgres":
@@ -0,0 +1,2 @@
1
+ import type { DatabaseContext, SQLiteConfig, QueryTemplateFunction } from "./index.js";
2
+ export default function sqlite({ path }: SQLiteConfig, context: DatabaseContext): QueryTemplateFunction;
@@ -0,0 +1,30 @@
1
+ import { join } from "node:path";
2
+ import { Database } from "bun:sqlite";
3
+ import { getColumnType } from "./sqlite.js";
4
+ export default function sqlite({ path = ":memory:" }, context) {
5
+ if (path !== undefined)
6
+ path = join(context.cwd, path);
7
+ return async (strings, ...params) => {
8
+ const date = new Date();
9
+ const database = new Database(path);
10
+ try {
11
+ const statement = database.prepare(strings.join("?"));
12
+ const rows = statement.all(...params);
13
+ return {
14
+ rows,
15
+ schema: getStatementSchema(statement),
16
+ duration: Date.now() - +date,
17
+ date
18
+ };
19
+ }
20
+ finally {
21
+ database.close();
22
+ }
23
+ };
24
+ }
25
+ function getStatementSchema(statement) {
26
+ return statement.columnNames.map((name, i) => ({
27
+ name,
28
+ type: getColumnType(statement.columnTypes[i])
29
+ }));
30
+ }
@@ -0,0 +1,2 @@
1
+ import type { DatabaseContext, SQLiteConfig, QueryTemplateFunction } from "./index.js";
2
+ export default function sqlite({ path }: SQLiteConfig, context: DatabaseContext): QueryTemplateFunction;
@@ -0,0 +1,29 @@
1
+ import { join } from "node:path";
2
+ import { DatabaseSync } from "node:sqlite";
3
+ import { getColumnType } from "./sqlite.js";
4
+ export default function sqlite({ path = ":memory:" }, context) {
5
+ if (path !== undefined)
6
+ path = join(context.cwd, path);
7
+ return async (strings, ...params) => {
8
+ const date = new Date();
9
+ const database = new DatabaseSync(path);
10
+ try {
11
+ const statement = database.prepare(strings.join("?"));
12
+ const rows = statement.all(...params);
13
+ return {
14
+ rows,
15
+ schema: getStatementSchema(statement),
16
+ duration: Date.now() - +date,
17
+ date
18
+ };
19
+ }
20
+ finally {
21
+ database.close();
22
+ }
23
+ };
24
+ }
25
+ function getStatementSchema(statement) {
26
+ return statement
27
+ .columns()
28
+ .map((column) => ({ name: column.name, type: getColumnType(column.type) }));
29
+ }
@@ -0,0 +1,2 @@
1
+ import type { ColumnSchema } from "../runtime/index.js";
2
+ export declare function getColumnType(type: string | null): ColumnSchema["type"];
@@ -0,0 +1,36 @@
1
+ export function getColumnType(type) {
2
+ switch (type) {
3
+ case "INT":
4
+ case "INTEGER":
5
+ case "TINYINT":
6
+ case "SMALLINT":
7
+ case "MEDIUMINT":
8
+ case "BIGINT":
9
+ case "UNSIGNED BIG INT":
10
+ case "INT2":
11
+ case "INT8":
12
+ return "integer";
13
+ case "TEXT":
14
+ case "CLOB":
15
+ return "string";
16
+ case "REAL":
17
+ case "DOUBLE":
18
+ case "DOUBLE PRECISION":
19
+ case "FLOAT":
20
+ case "NUMERIC":
21
+ return "number";
22
+ case "BLOB":
23
+ return "buffer";
24
+ case "DATE":
25
+ case "DATETIME":
26
+ return "string"; // TODO convert strings to Date instances in sql.js
27
+ case null:
28
+ return "other";
29
+ default:
30
+ return /^(?:(?:(?:VARYING|NATIVE) )?CHARACTER|(?:N|VAR|NVAR)CHAR)\(/.test(type)
31
+ ? "string"
32
+ : /^(?:DECIMAL|NUMERIC)\(/.test(type)
33
+ ? "number"
34
+ : "other";
35
+ }
36
+ }
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/observablehq/notebook-kit.git"
7
7
  },
8
- "version": "1.1.0-rc.12",
8
+ "version": "1.1.0-rc.13",
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "test": "vitest",
@@ -67,6 +67,7 @@
67
67
  "@eslint/js": "^9.29.0",
68
68
  "@types/jsdom": "^21.1.7",
69
69
  "@types/markdown-it": "^14.1.2",
70
+ "bun-types": "^1.2.20",
70
71
  "eslint": "^9.29.0",
71
72
  "globals": "^16.2.0",
72
73
  "htl": "^0.3.1",