Code Monkey home page Code Monkey logo

feathers-datascript's Introduction

feathers-datascript

A datascript and datomic service for FeathersJS.

See clj-crud for more inspiration.

Article on CRUD here and here

WIP: Under development

To help with development, you can run the tests with npm run test.

Create DB

import {datascript as d} from 'datascript'

var db = d.db_with(d.empty_db(schema), [data])
                     [

UPSERT (Insert/Update in one)

When a transaction is processed, all the temporary ids are translated to actual entity ids. Temporary ids with the same partition and negative number value are mapped to the same entity id. Temporary ids are mapped to new entity ids unless you use a temporary id with an attribute defined as :db/unique :db.unique/identity, in which case the system will map your temporary id to an existing entity if one exists with the same attribute and value (update) or will make a new entity if one does not exist (insert). All further adds in the transaction that apply to that same temporary id are applied to the "upserted" entity.

Please see Datomic Tutorial Section Adding, updating, and retracting data

ADD entity (generated ID)

To add nested object, use attribute refs, ie. :todo/project below

from: add-todo

let project = {':db/add': -1, ':project/name': 'my project'}

let todo = {':db/id': -2,
            ':todo/text:': 'clean kitchen',
            ':todo/done': false,
            ':todo/project': -1,
            ':todo/due': new Date(),
            ':todo/tags': ['house', 'chore']}

d.transact(conn, [project, todo]);

DELETE entity (by ID)

d.transact(conn, {':db/retractEntity': -1});

UPDATE entity (by ID)

See Add, since attributes/entities (same) are simply updated if already exists.

FIND by criteria

Datascript and Datomic both use datalog for queries.

In the example below, ?e is the entity ID and ?n and ?a are attribute values that must match

var q = '[:find ?n ?a 
          :where [?e "name" ?n] 
                 [?e "age" ?a]
        ]';

You can pass query parameters via :in

// pass parameter ?e using `in:`
q = '[:find ?aka 
      :in $ ?e 
      :where [?e "aka" ?aka]]'

assert_eq_set([["Ivan", 17]], d.q(q, db));

// passing multiple criteria arguments for `:in` - `?e` and `?email`
res     = d.q('[:find ?e ?email \
                  :in  $ [[?n ?email]] \
                  :where [?e "name" ?n]]',
                people_db,

Good luck and enjoy!

feathers-datascript's People

Contributors

kristianmandrup avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

jiasws

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.