blob: 3d0e25902ad994c5d94e2031f01c48ce26a63dec [file]
{% 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'`, `'websql'`, or `'http'`. If unspecified, PouchDB will infer this automatically, preferring IndexedDB to WebSQL in browsers that support both (i.e. Chrome, Opera and Android 4.4+).
* `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:**
* `ajax`: Ajax requester options. For instance, passing in the options `{ajax: {timeout: 10000}}` will allow you to set the max timeout for an HTTP request. These are passed verbatim to [request][] (in Node.js) or [a request shim](https://github.com/pouchdb/pouchdb/blob/master/lib/deps/request-browser.js) (in the browser), with the exception of:
* `ajax.cache`: Appends a random string to the end of all HTTP GET requests to avoid them being cached on IE. Set this to `true` to prevent this happening.
* `ajax.headers`: The `ajax.headers` option allows you to customise headers that are sent to the remote HTTP Server.
* `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.
* `ajax.withCredentials`: Set to `false` to disable transferring cookies or [HTTP Auth information](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials). Defaults to `true`.
* `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.
* `request`: Allows the caller to specify a function that is called for each ajax request, this can be used to modify the request before it is sent, modify the contents that are returned or completely override how the request is made, for example:
##### Add a calculated header to requests
{% highlight js %}
var db = new PouchDB('dbname', {
request: function(opts, ajax) {
opts.headers.myauth = authtoken();
return ajax(opts);
}
});
{% endhighlight %}
**IndexedDB-only options:**
* `storage`: Specifies whether you want to use `persistent` or `temporary` storage. This non-standard feature has been supported in Firefox since version 26. Find out more about the available storage types, and how Firefox handles client-side data storage, at [Browser storage limits and eviction criteria](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Browser_storage_limits_and_eviction_criteria).
**WebSQL-only options:**
* `size`: Amount in MB to request for storage, which you will need if you are storing >5MB in order to [avoid storage limit errors 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:
{% 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 Ajax options:
{% highlight js %}
var db = new PouchDB('http://example.com/dbname', {
ajax: {
cache: false,
timeout: 10000,
headers: {
'X-Some-Special-Header': 'foo'
},
},
auth: {
username: 'mysecretusername',
password: 'mysecretpassword'
}
});
{% endhighlight %}
For more info, check out [adapters](/adapters.html).