Jackie D
Jackie D

Reputation: 93

Create a custom ZODB in Plone?

I need to store some information and be able to read and write this information. Can I create a custom small ZODB (http://www.zodb.org/en/latest/articles/ZODB1.html) and have Plone read/write to this? Would placing it in /var/filestorage/myzodb.fs cause any issues with the existing Plone and the database it uses?

Upvotes: 2

Views: 138

Answers (2)

MrTango
MrTango

Reputation: 620

You can also use souper for light weight data:

>>> from souper.soup import get_soup
>>> soup = get_soup('my_soup_id', context)
>>> soup
<souper.soup.Soup at 0x...>

It also supports indexing.

Upvotes: 0

avoinea
avoinea

Reputation: 579

As I said before, you can always use annotations to store non-schema related information on objects:

>>> from persistent.list import PersistentList
>>> from persistent.dict import PersistentDict
>>> from zope.annotation.interfaces import IAnnotations

>>> anno = IAnnotations(self.context)

>>> anno['employees'] = PersistentList(['Mary', 'Jo', 'Bob'])
>>> anno['codes'] = PersistentDict({'a': 1, 'b': 2})

See docs for more details

If you still want to store data to different ZODB file, you should take care of 3 major issues:

  • Scaling
  • DB connections
  • DB read/write conflict errors

To scale it up, you should use ZEO (or RelStorage). I will suppose you're using zc.buildout to deploy your Plone stack. Thus, within buildout.cfg:

[zeoserver]
recipe = plone.recipe.zeoserver
zeo-address = 127.0.0.1:8100
...
zeo-conf-additional =
    <filestorage 2>
      path ${buildout:directory}/var/myzodb.fs
    </filestorage>

[instance]
recipe = plone.recipe.zope2instance
...
zeo-client = True
zeo-address = 127.0.0.1:8100
zope-conf-additional =
  <zodb_db myzodb>
      <zeoclient>
          server 127.0.0.1:8100
          storage 2
          name myzodbstorage
      </zeoclient>
      mount-point /myzodb
  </zodb_db>

Now, to connect and store data to this new DB:

>>> from App.config import getConfiguration
>>> from zope.component.hooks import getSite

>>> db = getConfiguration().dbtab.getDatabase(name='myzodb')    
>>> conn = getSite()._p_jar.get_connection('myzodb')
>>> myzodb = conn.root()
>>> myzodb
{}

>>> myzodb['employees'] = ['Mary', 'Jo', 'Bob']
>>> myzodb['codes'] = {'a': 1, 'b': 2}

>>> import transaction
>>> transaction.commit()

Upvotes: 4

Related Questions