mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
Merge pull request #1922 from daniel-lang/sqljs-startup
Sql.js: Fixed startup error when file does not yet exist
This commit is contained in:
commit
926ce07444
@ -79,7 +79,7 @@ export class SqljsDriver extends AbstractSqliteDriver {
|
||||
* Loads a database from a given file (Node.js), local storage key (browser) or array.
|
||||
* This will delete the current database!
|
||||
*/
|
||||
load(fileNameOrLocalStorageOrData: string | Uint8Array): Promise<any> {
|
||||
load(fileNameOrLocalStorageOrData: string | Uint8Array, checkIfFileOrLocalStorageExists: boolean = true): Promise<any> {
|
||||
if (typeof fileNameOrLocalStorageOrData === "string") {
|
||||
// content has to be loaded
|
||||
if (PlatformTools.type === "node") {
|
||||
@ -89,15 +89,34 @@ export class SqljsDriver extends AbstractSqliteDriver {
|
||||
const database = PlatformTools.readFileSync(fileNameOrLocalStorageOrData);
|
||||
return this.createDatabaseConnectionWithImport(database);
|
||||
}
|
||||
else {
|
||||
else if (checkIfFileOrLocalStorageExists) {
|
||||
throw new Error(`File ${fileNameOrLocalStorageOrData} does not exist`);
|
||||
}
|
||||
else {
|
||||
// File doesn't exist and checkIfFileOrLocalStorageExists is set to false.
|
||||
// Therefore open a database without importing an existing file.
|
||||
// File will be written on first write operation.
|
||||
return this.createDatabaseConnectionWithImport();
|
||||
}
|
||||
}
|
||||
else {
|
||||
// browser
|
||||
// fileNameOrLocalStorageOrData should be a local storage key
|
||||
const localStorageContent = PlatformTools.getGlobalVariable().localStorage.getItem(fileNameOrLocalStorageOrData);
|
||||
return this.createDatabaseConnectionWithImport(JSON.parse(localStorageContent));
|
||||
|
||||
if (localStorageContent != null) {
|
||||
// localStorage value exists.
|
||||
return this.createDatabaseConnectionWithImport(JSON.parse(localStorageContent));
|
||||
}
|
||||
else if (checkIfFileOrLocalStorageExists) {
|
||||
throw new Error(`File ${fileNameOrLocalStorageOrData} does not exist`);
|
||||
}
|
||||
else {
|
||||
// localStorage value doesn't exist and checkIfFileOrLocalStorageExists is set to false.
|
||||
// Therefore open a database without importing anything.
|
||||
// localStorage value will be written on first write operation.
|
||||
return this.createDatabaseConnectionWithImport();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
@ -196,7 +215,7 @@ export class SqljsDriver extends AbstractSqliteDriver {
|
||||
*/
|
||||
protected createDatabaseConnection(): Promise<any> {
|
||||
if (this.options.location) {
|
||||
return this.load(this.options.location);
|
||||
return this.load(this.options.location, false);
|
||||
}
|
||||
|
||||
return this.createDatabaseConnectionWithImport(this.options.database);
|
||||
|
||||
40
test/functional/sqljs/startup.ts
Normal file
40
test/functional/sqljs/startup.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import "reflect-metadata";
|
||||
import * as path from "path";
|
||||
import {expect} from "chai";
|
||||
import {Post} from "./entity/Post";
|
||||
import {Connection} from "../../../src/connection/Connection";
|
||||
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
|
||||
import {PlatformTools} from "../../../src/platform/PlatformTools";
|
||||
|
||||
describe("sqljs driver > startup", () => {
|
||||
let connections: Connection[];
|
||||
const pathToSqlite = path.resolve(__dirname, "startup.sqlite");
|
||||
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [Post],
|
||||
schemaCreate: true,
|
||||
dropSchema: true,
|
||||
enabledDrivers: ["sqljs"],
|
||||
driverSpecific: {
|
||||
autoSave: true,
|
||||
location: pathToSqlite,
|
||||
}
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
it("should startup even if the file doesn't exist", () => Promise.all(connections.map(async connection => {
|
||||
// if we come this far, test was successful as a connection was established
|
||||
expect(connection).to.not.be.null;
|
||||
})));
|
||||
|
||||
it("should write a new file after first write operation", () => Promise.all(connections.map(async connection => {
|
||||
let post = new Post();
|
||||
post.title = "The title";
|
||||
|
||||
const repository = connection.getRepository(Post);
|
||||
await repository.save(post);
|
||||
|
||||
expect(PlatformTools.fileExist(pathToSqlite)).to.be.true;
|
||||
})));
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user