Skip to content

Manipulating the database

olibou edited this page · 2 revisions

General

Resources

For Developers

How to's

Set up

Advanced steps

Integrating Etherpad in your web app

for Developers

Clone this wiki locally

Some exemples on how to manipulate the Db

Instantiate the Db API:

var db = require('ep_etherpad-lite/node/db/DB').db

Get data from the Db:

db.get("record_key", function(err, record_value){

    // do something

});

Set data in the Db:

// Replace data value if the record already exists or create a new record with this key

db.set("record_key", data);

// Add a subvalue to an existing record data

db.setSub("record_key", ["sub_key"], value);

db.setsub example:

{"colorId":"#79d9d9","name":"tutti","timestamp":1364832712430,"padIDs":{"mypad":1}}

db.setSub("record_key", ["email"], "tutti@frutti.org");

{"colorId":"#79d9d9","name":"tutti","timestamp":1364832712430,"padIDs":{"mypad":1},"email":"tutti@frutti.org"}

Options

Db caching:

By default, the results from a db.get are cached (set initially to 1000).
In some case, it's not what you want.
To remove the caching system for requests, you can set the 'caching' option to 0

db['dbSettings'].cache = 0;

Migrating dirty to mysql in perl

Here is a small code to migrate dirty.db to mysql ...

#!/usr/bin/perl
use strict;
use DBI;

my $dbh = DBI->connect("DBI:mysql:database=etherpad;host=localhost", "yourMysqlId", "yourMysqlPasswd",) or die;
$dbh->prepare("TRUNCATE TABLE store")->execute();
$dbh->prepare("SET CHARACTER SET utf8")->execute();

open(F,"var/dirty.db") or die;

while (<F>) {
    if (m|\{\"key\":\"(.*)\",\"val\":(.*)\}|) {
    my ($k,$v) = ($1,$2);
    my $sth = $dbh->prepare("SELECT `key` FROM store WHERE `key` = ?") or die;
    $sth->execute($k) or die;
    my @a = $sth->fetchrow();
    if ($a[0]) {
        $sth = $dbh->prepare("UPDATE store set `value` = ? WHERE `key` = ?") or die;
        $sth->execute($v,$k) or die;
    } else {
        $sth = $dbh->prepare("INSERT INTO store (`key`,`value`) VALUES (?,?)") or die;
        $sth->execute($k,$v) or die;
    }
    } else {
    die "Err!\n";
    }
}
close F;

Something went wrong with that request. Please try again.