mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
#799 sqlite: The 'database' path will now be created
This commit is contained in:
parent
82a0be782b
commit
dd920f23a0
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,3 +6,4 @@ node_modules/
|
||||
ormconfig.json
|
||||
ormlogs.log
|
||||
npm-debug.log
|
||||
/test/github-issues/799/tmp/*
|
||||
|
||||
@ -40,8 +40,10 @@
|
||||
"devDependencies": {
|
||||
"@types/chai": "^4.0.4",
|
||||
"@types/chai-as-promised": "0.0.31",
|
||||
"@types/mkdirp": "^0.5.1",
|
||||
"@types/mocha": "^2.2.42",
|
||||
"@types/node": "^8.0.26",
|
||||
"@types/rimraf": "^2.0.2",
|
||||
"@types/sinon": "^2.3.3",
|
||||
"chai": "^3.5.0",
|
||||
"chai-as-promised": "^7.1.1",
|
||||
@ -66,6 +68,7 @@
|
||||
"pg": "^7.3.0",
|
||||
"redis": "^2.8.0",
|
||||
"remap-istanbul": "^0.9.5",
|
||||
"rimraf": "^2.6.2",
|
||||
"sinon": "^2.4.1",
|
||||
"sinon-chai": "^2.13.0",
|
||||
"sqlite3": "^3.1.10",
|
||||
|
||||
@ -7,6 +7,9 @@ import {SqliteConnectionOptions} from "./SqliteConnectionOptions";
|
||||
import {ColumnType} from "../types/ColumnTypes";
|
||||
import {QueryRunner} from "../../query-runner/QueryRunner";
|
||||
import {AbstractSqliteDriver} from "../sqlite-abstract/AbstractSqliteDriver";
|
||||
import {close, open, stat} from "fs";
|
||||
import {dirname} from "path";
|
||||
import * as mkdirp from "mkdirp";
|
||||
|
||||
/**
|
||||
* Organizes communication with sqlite DBMS.
|
||||
@ -54,7 +57,7 @@ export class SqliteDriver extends AbstractSqliteDriver {
|
||||
this.databaseConnection.close((err: any) => err ? fail(err) : ok());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a query runner used to execute database queries.
|
||||
*/
|
||||
@ -77,11 +80,55 @@ export class SqliteDriver extends AbstractSqliteDriver {
|
||||
// Protected Methods
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
protected createDatabaseFile(): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
mkdirp(dirname(this.options.database), (err: NodeJS.ErrnoException) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
open(this.options.database, "w", (err, fd) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
close(fd, (err) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
});
|
||||
|
||||
return resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
protected doesFileExist(): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
stat(this.options.database, (err, stats) => {
|
||||
if (err) {
|
||||
if (err.code === "ENOENT") {
|
||||
return resolve(false);
|
||||
}
|
||||
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
return resolve(stats.isFile());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates connection with the database.
|
||||
*/
|
||||
protected createDatabaseConnection() {
|
||||
return new Promise<void>((ok, fail) => {
|
||||
return new Promise<void>(async (ok, fail) => {
|
||||
if (!await this.doesFileExist()) {
|
||||
await this.createDatabaseFile();
|
||||
}
|
||||
|
||||
const databaseConnection = new this.sqlite.Database(this.options.database, (err: any) => {
|
||||
if (err) return fail(err);
|
||||
|
||||
|
||||
29
test/github-issues/799/issue-799.ts
Normal file
29
test/github-issues/799/issue-799.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import "reflect-metadata";
|
||||
import * as assert from "assert";
|
||||
import {createConnection} from "../../../src/index";
|
||||
import * as rimraf from "rimraf";
|
||||
import {dirname} from "path";
|
||||
|
||||
describe("github issues > #799 sqlite: 'database' path should be created", () => {
|
||||
|
||||
const path = `${__dirname}/tmp/sqlitedb.db`;
|
||||
const cleanup = (done: () => void) => {
|
||||
rimraf(dirname(path), () => {
|
||||
return done();
|
||||
});
|
||||
};
|
||||
|
||||
before(cleanup);
|
||||
after(cleanup);
|
||||
|
||||
it("should create the whole path to database file", async function () {
|
||||
const connection = await createConnection({
|
||||
"name": "sqlite",
|
||||
"type": "sqlite",
|
||||
"database": path
|
||||
});
|
||||
|
||||
assert.strictEqual(connection.isConnected, true);
|
||||
});
|
||||
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user