Code Monkey home page Code Monkey logo

datascript-storage-sql's Introduction

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.

datascript-storage-sql's People

Contributors

tonsky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

maxweber

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.