Introduction
db-cookie-store - this is database based store for cookie management library tough cookie. The library uses ORM Sequelize and allow store cookies in databases which are supported by sequelize: MySQL, MariaDB, SQLite, PostgreSQL.
Synopsis
var DBCookieStore = ;var CookieJar = CookieJar;var jar = db_name db_user db_password db_options;
Installation
If you have npm installed, you can simply type:
npm install db-cookie-store
Or you can clone this repository using the git command:
git clone git://github.com/JSBizon/db-cookie-store.git
Usage
Class DbCookieStore has options:
- cookies_model - pre-defined sequalize model for store cookies ( Default : null)
- cookies_model_create - create table for store cookies if table doesn't exist ( Default : true)
- cookies_table - name of table for store cookies ( Default : cookies )
- cookies_store_schema - name of pre-defined schema or object with fields for custom schema. There are several pre-defined schemes: default, mozilla, chrome-win. ( Default : default).
- cookies_fields_map - map fields from cookie to the table. ( Default : null, it's dependent from store schema)
- cookies_model_options - options parameter for creating cookies sequalize model. ( Default : null, it's dependent from store schema)
- cookies_schema_init - this function will be called before create new cookies sequalize model. It must return promise.( Default : null, it's dependent from store schema)
- transactions_queue - use transaction queue for insert and update sql queries. If value is true, requests will executed step by step. ( Default : true)
Store schema
Store schema describes how cookies data will be stored to the database. Store schema defines columns name and type. For example there are columns for default schema:
fields_map : 'id' : type: SequelizeINTEGER primaryKey: true autoIncrement: true allowNull: false field: "id" 'key' : type: SequelizeTEXT allowNull: false field : "key" 'value' : type: SequelizeTEXT allowNull: false field : "value" 'expires' : type: SequelizeINTEGER field : "expires" 'maxAge' : type: SequelizeINTEGER field : "max_age" 'domain' : type: SequelizeTEXT allowNull: false field : "domain" 'path' : type: SequelizeTEXT allowNull: false field : "path" 'secure' : type: SequelizeBOOLEAN allowNull: false defaultValue: true field : "secure" 'httpOnly' : type: SequelizeBOOLEAN allowNull: false defaultValue: true field : "http_only" 'creation' : type: SequelizeINTEGER field : "creation_time" 'lastAccessed' : type: SequelizeINTEGER field : "last_accessed"
this means cookies attribute key will be writed(or readed) to the column key with type TEXT, cookies attribute expires will be writed to the column expires with type INTEGER, etc. Supported all options which supported by sequalize define method.
There are exists next pre-defined schemas: default, mozilla, chrome-win.
You can define your own schema. Usually schema is a hash with fields: fields_map, options, init:
var my_schema = //sequalize fields description fields_map : ... //sequalize options options : timestamps: false indexes : fields : attribute : "domain" length : 25 fields : attribute: "key" length : 25 attribute : "domain" length: 25 attribute : "path" length: 25 //should return Promise, will be called before model created { return { ...... }; } }
Export cookies
For receive all cookies from the store might be used method export:
cookie_store;cookie_store;
Examples
Using this module with 'cookies_store_schema' parameter allow to read/write cookies from/to different cookies storages
Read/Write cookies from firefox cookies storage
Firefox uses Sqlite database for store cookies. So cookies could be readed and writed via CookieJar:
var DBCookieStore = ;var CookieJar = CookieJar;var cookie_jar = nullnullnullcookies_store_schema : 'mozilla' //use pre-defined fields setdialect : 'sqlite'storage: './cookies.sqlite';
Read/Write cookies from chrome cookies storage(Linux)
Chrome uses uses Sqlite database for store cookies. Chrome for linux decode values of cookies. Encoding/decoding is implemented in chrome store schema:
var DBCookieStore = ;var CookieJar = CookieJar;var chrome_schema = ; //load chrome schemavar cookie_jar = nullnullnullcookies_store_schema : chrome_schema //use loaded fieldsetdialect : 'sqlite'storage: './Cookies';