不要怂,就是干,撸起袖子干!

You need to sign in or sign up before continuing.
Commit 567c3e5a by Azeem Bande-Ali Committed by Sushant

Add relative and full path support for sqlite (#7700)

* Add relative and full path support for sqlite

If the dialect is 'sqlite', then we parse host+pathname as a file path.
sqlite documentation suggests that this is the right way to parse them:
https://sqlite.org/c3ref/open.html#urifilenameexamples

If the "storage" option is provided, then that wil take precedence.

Examples:
sqlite:subfolder/dbname.db => subfolder/dbname.db
sqlite:/home/abs/dbname.db => /home/abs/dbname.db

* Add dialect check for the unit tests

* Update docs and changelog
1 parent 6a84ba89
...@@ -73,6 +73,7 @@ ...@@ -73,6 +73,7 @@
- [ADDED] Support for JSON attributes in orders and groups. [#7564](https://github.com/sequelize/sequelize/issues/7564) - [ADDED] Support for JSON attributes in orders and groups. [#7564](https://github.com/sequelize/sequelize/issues/7564)
- [REMOVED] Removes support for interpretation of raw properties and values in the where object. [#7568](https://github.com/sequelize/sequelize/issues/7568) - [REMOVED] Removes support for interpretation of raw properties and values in the where object. [#7568](https://github.com/sequelize/sequelize/issues/7568)
- [FIXED] Upsert now updates all changed fields by default - [FIXED] Upsert now updates all changed fields by default
- [ADDED] Support for paths for sqlite databases via connection strings [#4721](https://github.com/sequelize/sequelize/issues/4721)
## BC breaks: ## BC breaks:
- Model.validate instance method now runs validation hooks by default. Previously you needed to pass { hooks: true }. You can override this behavior by passing { hooks: false } - Model.validate instance method now runs validation hooks by default. Previously you needed to pass { hooks: true }. You can override this behavior by passing { hooks: false }
......
...@@ -192,6 +192,13 @@ const sequelize = new Sequelize('database', 'username', 'password', { ...@@ -192,6 +192,13 @@ const sequelize = new Sequelize('database', 'username', 'password', {
}) })
``` ```
Or you can use a connection string as well with a path:
```js
const sequelize = new Sequelize('sqlite:/home/abs/path/dbname.db')
const sequelize = new Sequelize('sqlite:relativePath/dbname.db')
```
### PostgreSQL ### PostgreSQL
The library for PostgreSQL is`pg@~3.6.0` You'll just need to define the dialect: The library for PostgreSQL is`pg@~3.6.0` You'll just need to define the dialect:
......
...@@ -108,14 +108,18 @@ class Sequelize { ...@@ -108,14 +108,18 @@ class Sequelize {
const urlParts = url.parse(arguments[0]); const urlParts = url.parse(arguments[0]);
// SQLite don't have DB in connection url options.dialect = urlParts.protocol.replace(/:$/, '');
options.host = urlParts.hostname;
if (options.dialect === 'sqlite' && urlParts.pathname) {
const path = Path.join(options.host, urlParts.pathname);
options.storage = options.storage || path;
}
if (urlParts.pathname) { if (urlParts.pathname) {
config.database = urlParts.pathname.replace(/^\//, ''); config.database = urlParts.pathname.replace(/^\//, '');
} }
options.dialect = urlParts.protocol.replace(/:$/, '');
options.host = urlParts.hostname;
if (urlParts.port) { if (urlParts.port) {
options.port = urlParts.port; options.port = urlParts.port;
} }
......
...@@ -69,6 +69,32 @@ describe('Sequelize', () => { ...@@ -69,6 +69,32 @@ describe('Sequelize', () => {
expect(config.port).to.equal('9821'); expect(config.port).to.equal('9821');
}); });
describe('sqllite path inititalization', () =>{
const current = Support.sequelize;
if (current.dialect.name === 'sqlite') {
it('should accept relative paths for sqlite', () => {
const sequelize = new Sequelize('sqlite:subfolder/dbname.db');
const options = sequelize.options;
expect(options.dialect).to.equal('sqlite');
expect(options.storage).to.equal('subfolder/dbname.db');
});
it('should accept absolute paths for sqlite', () => {
const sequelize = new Sequelize('sqlite:/home/abs/dbname.db');
const options = sequelize.options;
expect(options.dialect).to.equal('sqlite');
expect(options.storage).to.equal('/home/abs/dbname.db');
});
it('should prefer storage in options object', () => {
const sequelize = new Sequelize('sqlite:/home/abs/dbname.db', {storage: '/completely/different/path.db'});
const options = sequelize.options;
expect(options.dialect).to.equal('sqlite');
expect(options.storage).to.equal('/completely/different/path.db');
});
}
});
it('should work with no authentication options', () => { it('should work with no authentication options', () => {
const sequelize = new Sequelize('mysql://example.com:9821/dbname'); const sequelize = new Sequelize('mysql://example.com:9821/dbname');
const config = sequelize.config; const config = sequelize.config;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!