| {% include anchor.html edit="true" title="Create a database" hash="create_database" %} |
| |
| {% highlight js %} |
| new PouchDB([name], [options]) |
| {% endhighlight %} |
| |
| This method creates a database or opens an existing one. If you use a URL like `'http://domain.com/dbname'`, then PouchDB will work as a client to an online CouchDB instance. Otherwise it will create a local database using [whatever backend is present](/adapters.html). |
| |
| ### Options |
| |
| * `name`: You can omit the `name` argument and specify it via `options` instead. Note that the name is required. |
| |
| **Options for local databases:** |
| |
| * `auto_compaction`: This turns on auto compaction, which means `compact()` is called after every change to the database. Defaults to `false`. |
| * `adapter`: One of `'idb'`, `'leveldb'`, or `'http'`. |
| * `revs_limit`: Specify how many old revisions we keep track (not a copy) of. Specifying a low value means Pouch may not be able to figure out whether a new revision received via replication is related to any it currently has which could result in a conflict. Defaults to `1000`. |
| |
| **Options for remote databases:** |
| |
| * `fetch(url, opts)`: Intercept or override the HTTP request, you can add or modify any headers or options relating to the http request then return a new fetch Promise. |
| * `auth.username` + `auth.password`: You can specify HTTP auth parameters either by using a database with a name in the form `http://user:pass@host/name` or via the `auth.username` + `auth.password` options. |
| * `skip_setup`: Initially PouchDB checks if the database exists, and tries to create it, if it does not exist yet. Set this to `true` to skip this setup. |
| |
| **WebSQL-only options:** |
| |
| _Note:_ as of PouchDB 7.0.0, this requires installing WebSQL as an add-on adapter. |
| |
| * `size`: Amount in MB to request for storage, which you will need if you are storing >5MB in order to [avoid storage limit errors in WebSQL on iOS/Safari](/errors.html#not_enough_space). Persistent views use a separate database per view which will also request storage space so keep in mind the upper storage limits when using persistent views on iOS. |
| |
| **Notes:** |
| |
| 1. In IndexedDB and WebSQL, PouchDB will use `_pouch_` to prefix the internal database names. Do not manually create databases with the same prefix. |
| 2. When acting as a client on Node, any other options given will be passed to [request][]. |
| 3. When using the `'leveldb'` adapter (the default on Node), any other options given will be passed to [levelup][]. |
| |
| [request]: https://github.com/mikeal/request |
| [levelup]: https://github.com/rvagg/node-levelup |
| [levelup_options]: https://github.com/rvagg/node-levelup/#options |
| |
| #### Example Usage: |
| {% highlight js %} |
| var db = new PouchDB('dbname'); |
| // or |
| var db = new PouchDB('http://localhost:5984/dbname'); |
| {% endhighlight %} |
| |
| Create a PouchDB that explicitly uses WebSQL (must install `pouchdb-adapter-websql` first): |
| |
| {% highlight js %} |
| var db = new PouchDB('dbname', {adapter : 'websql'}); |
| {% endhighlight %} |
| |
| Create an in-memory Pouch (must install `pouchdb-adapter-memory` first): |
| |
| {% highlight js %} |
| var db = new PouchDB('dbname', {adapter: 'memory'}); |
| {% endhighlight %} |
| |
| Create a remote PouchDB with special fetch options: |
| |
| {% highlight js %} |
| var db = new PouchDB('http://example.com/dbname', { |
| fetch: function (url, opts) { |
| opts.headers.set('X-Some-Special-Header', 'foo'); |
| return PouchDB.fetch(url, opts); |
| } |
| }); |
| {% endhighlight %} |
| |
| For more info, check out [adapters](/adapters.html). |