npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rethink-knex-adapter

v0.4.20

Published

Knex adapter for rethinkdb/thinky APIs to convert a project with less effort

Downloads

400

Readme

RethinkDb / Thinky adapter to Knex.js

Rethinkdb is dead. How do we migrate to a real database? Let's adapt the Thinky API to knex so we don't have to rewrite every single query/model definition, at least to start with.

This project tries to implement a good portion of the Thinky api through Knex, but also assumes you are converting your code 'one way' -- to knex, rather than supporting both

Configuration/Code Migration

The first step to switching is wherever in your code you have something like:

// OLD CODE:
import thinky from 'thinky'
export default thinky({
   host: process.env.DB_HOST,
   ...
 })

change it to:

import dumbThinky from 'rethink-knex-adapter'
export default dumbThinky({
   //KNEX CONFIG HERE
   client: 'sqlite3',
   connection: { filename: "./mydb.sqlite" },
   // for sqlite and other backends that don't support field defaults
   defaultUnsupported: true
})

Probably the next steps will be:

  1. Migrate any array() or object() fields to other tables or textfields with dumped json.
  2. Change unsupported methods from r.table('foo')... to r.knex.from('foo')... to use a direct connection to the knex driver.

Schema creation

  • Since we want real references instead of the weird thing that ReThinkdb does, any field ending in {table}_id is assumed to be a foreign key reference to {table}

    • To stop this behavior add .stopReference() to the model field definition
    • To force this behavior add .foreign(tableName) to the model field definition
  • We auto-create an id increment() field for each table -- even fields marked primary key will get it, but get() will use what you mark as a primary key

  • If you add 'timestamps' to the third argument of createModel, it will add created_at and updated_at timestamp fields (you can also use the 'standard' r.type.date().default(r.now()) code)

  • type.point() is not supported -- I suggest you change your schema to separate fields for lat/lng

  • type.array() and type.object() are not supported (object() is only ok for createModel's table definition)

Currently supported query methods

Not all queries on r.... are supported -- some of them are too much of a pain to implement. I'll take pull requests!

  • [x] bracket -- e.g. r.table('foo').getAll(bar, {'index': 'bars'}).limit(1)(0) (The last (0) is the 'bracket')
  • [x] bracket post-join for left/right, e.g. ...eqJoin(foo, r.table(bar))('right')
  • [ ] changes() (see below)
  • [x] count()
  • [x] default(defaultVal)
  • [x] delete()
  • [x] distinct() NOTE: at least if it works as a sql query
  • [x] eqJoin(leftTableField, r.table(rightTable))
  • [x] eqJoin(leftTableField, r.table(rightTable), rightTableIndex)
  • [x] filter(dictOfQueryValues)
  • [ ] filter(function)
  • [x] get(pkValue)
  • [x] getAll(val, {index: column}) (column defaults to pk if not avail)
  • [x] getAll(...bunchOfIds, {index: column})
  • [x] getAll([val1, val2], {index: multi_column_index})
  • [ ] group()
  • [ ] innerJoin()
  • [x] limit(max)
  • [ ] map(func) -- probably not, but might work (if rethink doesn't process it into bytecode)
  • [x] map({targetVal: r.row(sourceColumn), ...})
  • [ ] merge()
  • [x] orderBy(column)
  • [x] orderBy(r.desc(column))
  • [x] pluck(fieldname_or_index)
  • [ ] pluck(function)
  • [ ] sum()
  • [x] table(table)
  • [ ] ungroup
  • [x] update(updateData)
  • [x] zip()

changes()

Though we do not support how changes() works in rethinkdb -- which are essentially uploaded triggers back to your code, what IS implemented is a model listener. So if you run:

r.table('foo').changes().then(function(res) {
   if (res.old_val) { // was an update
      seeDifferences(res.old_val.field1, res.new_val.field2)
   } else { // was an insert record
      doSomething(res.new_val)
   }
})

You'll note that you can't run filter() before hand. The function signature is also pretty different (no cursors, etc)

This will also not trigger unless the change was done through and by the same process with the thinky() connection that you registered with. If you are adapting a code base, this might mean you need to load/setup your changes() listener in other processes that send changes to the relevant models.