GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - tonsky/datascript-storage-sql: SQL Storage implementation for DataScript

BranchesTags
Open more actions menu

Latest commit

 

History

7 Commits
7 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
dev
dev
 
 
script
script
 
 
src/datascript/storage/sql
src/datascript/storage/sql
 
 
test/datascript/storage/sql
test/datascript/storage/sql
 
 
.gitignore
.gitignore
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
deps.edn
deps.edn
 
 
project.clj
project.clj
 
 

Repository files navigation

SQL Storage implementation for DataScript

See datascript/doc/storage.md

Using

Add this to deps.edn:

io.github.tonsky/datascript-storage-sql {:mvn/version "2.0.0"}

Create storage by passing in javax.sql.DataSource and :dbtype option:

(def datasource
  (doto (org.sqlite.SQLiteDataSource.)
    (.setUrl "jdbc:sqlite:target/db.sqlite")))

(def storage
  (storage-sql/make datasource
    {:dbtype :sqlite}))

You can also pass optional options:

(storage-sql/make datasource
  {:dbtype     :sqlite
   :batch-size 1000
   :table      "datascript"
   :ddl        "create table if not exists datascript (addr INTEGER primary key, content TEXT)"
   :freeze-str pr-str
   :thaw-str   clojure.edn/read-string})

Or use binary serialization:

(storage-sql/make datasource
  {:dbtype :sqlite
   
   :freeze-bytes
   (fn ^bytes [obj]
     (with-open [out (ByteArrayOutputStream.)]
       (t/write (t/writer out :msgpack) obj)
       (.toByteArray out)))
   
   :thaw-bytes
   (fn [^bytes b]
     (t/read
      (t/reader (ByteArrayInputStream. b) :msgpack)))})

After that, use it as any other storage:

(d/create-conn schema {:storage storage})

or

(d/store db storage)

Currently supported :dbtype-s:

  • :h2
  • :mysql
  • :postgresql
  • :sqlite

If your JDBC driver only provides you with DataSource and you want to add some basic pooling on top, use storage-sql/pool:

(def datasource
  (doto (SQLiteDataSource.)
    (.setUrl "jdbc:sqlite:target/db.sqlite")))

(def pooled-datasource
  (storage-sql/pool datasource
    {:max-conn      10
     :max-idle-conn 4}))

(def storage
  (storage-sql/make pooled-datasource
    {:dbtype :sqlite}))

pool takes non-pooled DataSource and returns new DataSource that pools connections for you.

If you used pool to create storage, you can close it this way:

(storage-sql/close storage)

License

Copyright © 2023 Nikita Prokopov

Licensed under MIT License.

About

SQL Storage implementation for DataScript

Topics

clojuredatascriptsql

Resources

Readme
MIT license
Activity

Stars

48 stars

Watchers

3 watching

Forks

4 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.