sqlite-hello 0.1.0-alpha.34

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.
Files changed (3) hide show
  1. package/README.md +88 -0
  2. package/package.json +33 -0
  3. package/src/index.js +52 -0
package/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # `sqlite-hello` NPM Package
2
+
3
+ `sqlite-hello` is distributed on `npm` for Node.js developers. To install on [supported platforms](#supported-platforms), simply run:
4
+
5
+ ```
6
+ npm install sqlite-hello
7
+ ```
8
+
9
+ The `sqlite-hello` package is meant to be used with Node SQLite clients like [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3) and [`node-sqlite3`](https://github.com/TryGhost/node-sqlite3). For `better-sqlite3`, call [`.loadExtension()`](https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md#loadextensionpath-entrypoint---this) on your database object, passing in [`getLoadablePath()`](#getLoadablePath).
10
+
11
+ ```js
12
+ import Database from "better-sqlite3";
13
+ import * as sqlite_hello from "sqlite-hello";
14
+
15
+ const db = new Database(":memory:");
16
+
17
+ db.loadExtension(sqlite_hello.getLoadablePath());
18
+
19
+ const version = db.prepare("select hello_version()").pluck().get();
20
+ console.log(version); // "v0.2.0"
21
+ ```
22
+
23
+ For `node-sqlite3`, call the similarly named [`.loadExtension()`](https://github.com/TryGhost/node-sqlite3/wiki/API#loadextensionpath--callback) method on your database object, and pass in [`getLoadablePath()`](#getLoadablePath).
24
+
25
+ ```js
26
+ import sqlite3 from "sqlite3";
27
+ import * as sqlite_hello from "sqlite-hello";
28
+
29
+ const db = new sqlite3.Database(":memory:");
30
+
31
+ db.loadExtension(sqlite_hello.getLoadablePath());
32
+
33
+ db.get("select hello_version()", (err, row) => {
34
+ console.log(row); // {hello_version(): "v0.2.0"}
35
+ });
36
+ ```
37
+
38
+ See [the full API Reference](#api-reference) for the Node API, and [`docs.md`](../../docs.md) for documentation on the `sqlite-hello` SQL API.
39
+
40
+ ## Supported Platforms
41
+
42
+ Since the underlying `hello0` SQLite extension is pre-compiled, the `sqlite-hello` NPM package only works on a few "platforms" (operating systems + CPU architectures). These platforms include:
43
+
44
+ - `darwin-x64` (MacOS x86_64)
45
+ - `darwin-arm64` (MacOS M1 and M2 chips)
46
+ - `win32-x64` (Windows x86_64)
47
+ - `linux-x64` (Linux x86_64)
48
+
49
+ To see which platform your machine is, check the [`process.arch`](https://nodejs.org/api/process.html#processarch) and [`process.platform`](https://nodejs.org/api/process.html#processplatform) values like so:
50
+
51
+ ```bash
52
+ $ node -e 'console.log([process.platform, process.arch])'
53
+ [ 'darwin', 'x64' ]
54
+ ```
55
+
56
+ When the `sqlite-hello` NPM package is installed, the correct pre-compiled extension for your operating system and CPU architecture will be downloaded from the [optional dependencies](https://docs.npmjs.com/cli/v9/configuring-npm/package-json#optionaldependencies), with platform-specific packages like `sqlite-hello-darwin-x64`. This will be automatically, there's no need to directly install those packages.
57
+
58
+ More platforms may be supported in the future. Consider [supporting my work](https://github.com/sponsors/asg017/) if you'd like to see more operating systems and CPU architectures supported in `sqlite-hello`.
59
+
60
+ ## API Reference
61
+
62
+ <a href="#getLoadablePath" name="getLoadablePath">#</a> <b>getLoadablePath</b> [<>](https://github.com/asg017/sqlite-hello/blob/main/npm/sqlite-hello/src/index.js "Source")
63
+
64
+ Returns the full path to where the `sqlite-hello` _should_ be installed, based on the `sqlite-hello`'s `package.json` optional dependencies and the host's operating system and architecture.
65
+
66
+ This path can be directly passed into [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3)'s [`.loadExtension()`](https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md#loadextensionpath-entrypoint---this).
67
+
68
+ ```js
69
+ import Database from "better-sqlite3";
70
+ import * as sqlite_hello from "sqlite-hello";
71
+
72
+ const db = new Database(":memory:");
73
+ db.loadExtension(sqlite_hello.getLoadablePath());
74
+ ```
75
+
76
+ It can also be used in [`node-sqlite3`](https://github.com/TryGhost/node-sqlite3)'s [`.loadExtension()`](https://github.com/TryGhost/node-sqlite3/wiki/API#loadextensionpath--callback).
77
+
78
+ ```js
79
+ import sqlite3 from "sqlite3";
80
+ import * as sqlite_hello from "sqlite-hello";
81
+
82
+ const db = new sqlite3.Database(":memory:");
83
+ db.loadExtension(sqlite_hello.getLoadablePath());
84
+ ```
85
+
86
+ This function throws an `Error` in two different cases. The first case is when `sqlite-hello` is installed and run on an [unsupported platform](#supported-platforms). The second case is when the platform-specific optional dependency is not installed. If you reach this, ensure you aren't using `--no-optional` flag, and [file an issue](https://github.com/asg017/sqlite-hello/issues/new) if you are stuck.
87
+
88
+ The `db.loadExtension()` function may also throw an Error if the compiled extension is incompatible with your SQLite connection for any reason, including missing system packages, outdated glib versions, or other misconfigurations. If you reach this, please [file an issue](https://github.com/asg017/sqlite-hello/issues/new).
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "//": "Autogenerated by the npm_generate_platform_packages.sh script, do not edit by hand",
3
+ "name": "sqlite-hello",
4
+ "version": "0.1.0-alpha.34",
5
+ "description": "",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/asg017/sqlite-hello.git",
9
+ "directory": "npm/sqlite-hello"
10
+ },
11
+ "author": "Alex Garcia <alexsebastian.garcia@gmail.com>",
12
+ "license": "(MIT OR Apache-2.0)",
13
+ "main": "src/index.js",
14
+ "type": "module",
15
+ "scripts": {
16
+ "test": "node test.js"
17
+ },
18
+ "files": [
19
+ "*.dylib",
20
+ "*.so",
21
+ "*.dll"
22
+ ],
23
+ "optionalDependencies": {
24
+ "sqlite-hello-darwin-arm64": "0.1.0-alpha.34",
25
+ "sqlite-hello-darwin-x64": "0.1.0-alpha.34",
26
+ "sqlite-hello-linux-x64": "0.1.0-alpha.34",
27
+ "sqlite-hello-windows-x64": "0.1.0-alpha.34"
28
+ },
29
+ "devDependencies": {
30
+ "better-sqlite3": "^8.1.0",
31
+ "sqlite3": "^5.1.4"
32
+ }
33
+ }
package/src/index.js ADDED
@@ -0,0 +1,52 @@
1
+ import { join } from "node:path";
2
+ import { fileURLToPath } from "node:url";
3
+ import { arch, platform } from "node:process";
4
+ import { statSync } from "node:fs";
5
+
6
+ const supportedPlatforms = [
7
+ ["darwin", "x64"],
8
+ ["darwin", "arm64"],
9
+ ["win32", "x64"],
10
+ ["linux", "x64"],
11
+ ];
12
+
13
+ function validPlatform(platform, arch) {
14
+ return (
15
+ supportedPlatforms.find(([p, a]) => platform == p && arch === a) !== null
16
+ );
17
+ }
18
+ function extensionSuffix(platform) {
19
+ if (platform === "win32") return "dll";
20
+ if (platform === "darwin") return "dylib";
21
+ return "so";
22
+ }
23
+ function platformPackageName(platform, arch) {
24
+ const os = platform === "win32" ? "windows" : platform;
25
+ return `sqlite-hello-${os}-${arch}`;
26
+ }
27
+
28
+ export function getLoadablePath() {
29
+ if (!validPlatform(platform, arch)) {
30
+ throw new Error(
31
+ `Unsupported platform for sqlite-hello, on a ${platform}-${arch} machine, but not in supported platforms (${supportedPlatforms
32
+ .map(([p, a]) => `${p}-${a}`)
33
+ .join(",")}). Consult the sqlite-hello NPM package README for details. `
34
+ );
35
+ }
36
+ const packageName = platformPackageName(platform, arch);
37
+ const loadablePath = join(
38
+ fileURLToPath(new URL(".", import.meta.url)),
39
+ "..",
40
+ "..",
41
+ packageName,
42
+ "lib",
43
+ `hello0.${extensionSuffix(platform)}`
44
+ );
45
+ if (!statSync(loadablePath, { throwIfNoEntry: false })) {
46
+ throw new Error(
47
+ `Loadble extension for sqlite-hello not found. Was the ${packageName} package installed? Avoid using the --no-optional flag, as the optional dependencies for sqlite-hello are required.`
48
+ );
49
+ }
50
+
51
+ return loadablePath;
52
+ }