Subject: [PATCH 11/11] gnu: Add node-sqlite3.

From: Philip McGrath <[email-masked]>
Date: Sun, 14 Nov 2021 08:04:09 -0500

* gnu/packages/node-xyz.scm (node-sqlite3): New variable.
---
 gnu/packages/node-xyz.scm | 114 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 114 insertions(+)

diff --git a/gnu/packages/node-xyz.scm b/gnu/packages/node-xyz.scm
index 8aa93122df..d30b6f6e04 100644
--- a/gnu/packages/node-xyz.scm
+++ b/gnu/packages/node-xyz.scm
@@ -22,6 +22,8 @@
 
 (define-module (gnu packages node-xyz)
   #:use-module ((guix licenses) #:prefix license:)
+  #:use-module (gnu packages sqlite)
+  #:use-module (guix gexp)
   #:use-module (guix packages)
   #:use-module (guix git-download)
   #:use-module (guix build-system node))
@@ -636,3 +638,115 @@ (define-public node-addon-api
 @code{libuv} (included in a project via @code{#include <uv.h>}) are not
 ABI-stable across Node.js major versions.")
     (license license:expat)))
+
+(define-public node-sqlite3
+  (package
+    (name "node-sqlite3")
+    (version "5.0.2")
+    (source
+     (origin
+       (method git-fetch)
+       (uri (git-reference
+             (url "https://github.com/mapbox/node-sqlite3")
+             (commit (string-append "v" version))))
+       (file-name (git-file-name name version))
+       (sha256
+        (base32 "0sbbzzli282nxyfha10zx0k5m8hdp0sf3ipl59khjb7wm449j86h"))
+       (snippet
+        (with-imported-modules '((guix build utils))
+          #~(begin
+              (use-modules (guix build utils))
+              ;; unbundle sqlite
+              '(for-each delete-file-recursively
+                         (find-files "deps" gzip-file?)))))))
+    (inputs
+     `(("node-addon-api" ,node-addon-api)
+       ("sqlite" ,sqlite)))
+    (build-system node-build-system)
+    (arguments
+     `(#:tests?
+       #f ; FIXME: tests depend on node-mocha
+       #:modules
+       ((guix build node-build-system)
+        (guix build json)
+        (srfi srfi-1)
+        (ice-9 match)
+        (guix build utils))
+       #:phases
+       (modify-phases %standard-phases
+         (add-before 'configure 'npm-config-sqlite
+           (lambda* (#:key inputs #:allow-other-keys)
+             (setenv "npm_config_sqlite" (assoc-ref inputs "sqlite"))))
+         (add-after 'unpack 'un-cloud-ify
+           ;; Normally, this is "built" using @mapbox/node-pre-gyp,
+           ;; which publishes or downloads pre-built binaries
+           ;; or falls back to building from source.
+           ;; Here, we patch out all of that and just build directly.
+           ;; It would be better to patch a version of @mapbox/node-pre-gyp
+           ;; that always builds from source, but there are a number
+           ;; of dependencies that need to be packaged or removed.
+           (lambda args
+             (with-atomic-file-replacement "package.json"
+               (lambda (in out)
+                 (let ((js (read-json in)))
+                   (match (assoc-ref js "binary")
+                     (('@ . alist)
+                      (setenv "GYP_DEFINES"
+                              (string-append
+                               "module_name="
+                               (assoc-ref alist "module_name")
+                               " "
+                               "module_path="
+                               (assoc-ref alist "module_path")))))
+                   (write-json
+                    (fold (match-lambda*
+                            (((key update) js)
+                             (assoc-set! js
+                                         key
+                                         (match (assoc-ref js key)
+                                           (('@ . alist)
+                                            (cons '@ (update alist)))
+                                           (other
+                                            (update other))))))
+                          js
+                          `(("dependencies"
+                             ,(lambda (deps)
+                                (assoc-remove!
+                                 (assoc-remove! deps "@mapbox/node-pre-gyp")
+                                 "node-pre-gyp")))
+                            ("devDependencies"
+                             ,(lambda (deps)
+                                (assoc-remove!
+                                 (assoc-remove! deps "aws-sdk")
+                                 "@mapbox/cloudfriend")))
+                            ("scripts"
+                             ,(lambda (scripts)
+                                ;; install script would use node-pre-gyp
+                                (assoc-remove! scripts "install")))))
+                    out))))))
+         (add-after 'un-cloud-ify 'remove-missing-dev-deps
+           ;; Remove some missing testing/linting dependencies
+           ;; so we don't have to skip the configure phase entirely.
+           (lambda args
+             (with-atomic-file-replacement "package.json"
+               (lambda (in out)
+                 (write-json
+                  (let ((js (read-json in)))
+                    (assoc-set!
+                     (assoc-remove!
+                      (assoc-remove! (assoc-remove! js "peerDependencies")
+                                     "peerDependenciesMeta")
+                      "optionalDependencies")
+                     "devDependencies"
+                     (match (assoc-ref js "devDependencies")
+                       (('@ . deps)
+                        (cons '@ (assoc-remove! (assoc-remove! deps "eslint")
+                                                "mocha"))))))
+                  out))))))))
+    (home-page "https://github.com/mapbox/node-sqlite3")
+    (synopsis "Asynchronous, non-blocking SQLite3 bindings for Node.js")
+    (description
+     "The Node.js add-on @code{node-sqlite3} provides a set of a asynchronous,
+non-blocking bindings for SQLite3, written in modern C++ and tested for memory
+leaks.")
+     (license license:bsd-3)))
-- 
2.32.0