-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: modify the SqliteDriver API to support autosave (#41)
* Modified the SqliteDriver API to behave more like the original TypeORM's SqljsDriver to support the browser in the future.
- Loading branch information
Showing
20 changed files
with
536 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {Driver} from "../Driver.ts"; | ||
|
||
export interface AutoSavableOptions { | ||
/** | ||
* Enables the autoSave mechanism which either saves to location | ||
* or calls autoSaveCallback every time a change to the database is made. | ||
*/ | ||
readonly autoSave?: boolean; | ||
|
||
/** | ||
* A function that gets called on every change instead of the internal autoSave function. | ||
* autoSave has to be enabled for this to work. | ||
*/ | ||
readonly autoSaveCallback?: Function; | ||
} | ||
|
||
export interface AutoSavableDriver extends Driver { | ||
options: Driver["options"] & AutoSavableOptions; | ||
autoSave(): Promise<void>; | ||
save(): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// This code is simply copied from src/entity-manager/SqljsEntityManager.ts | ||
import {Connection} from "../connection/Connection.ts"; | ||
import {QueryRunner} from "../query-runner/QueryRunner.ts"; | ||
import {EntityManager} from "./EntityManager.ts"; | ||
import {SqliteDriver} from "../driver/sqlite/SqliteDriver.ts"; | ||
|
||
/** | ||
* A special EntityManager that includes import/export and load/save function | ||
* that are unique to deno-sqlite. | ||
* | ||
* This class provides the same behavior as the original TypeORM's SqljsEntityManager. | ||
*/ | ||
export class SqliteEntityManager extends EntityManager { | ||
private driver: SqliteDriver; | ||
|
||
// ------------------------------------------------------------------------- | ||
// Constructor | ||
// ------------------------------------------------------------------------- | ||
|
||
constructor(connection: Connection, queryRunner?: QueryRunner) { | ||
super(connection, queryRunner); | ||
this.driver = connection.driver as SqliteDriver; | ||
} | ||
|
||
// ------------------------------------------------------------------------- | ||
// Public Methods | ||
// ------------------------------------------------------------------------- | ||
|
||
/** | ||
* Loads either the definition from a file (Deno) or localstorage (browser) | ||
* or uses the given definition to open a new database. | ||
*/ | ||
async loadDatabase(fileNameOrLocalStorageOrData: string | Uint8Array): Promise<void> { | ||
await this.driver.load(fileNameOrLocalStorageOrData); | ||
} | ||
|
||
/** | ||
* Saves the current database to a file (Deno) or localstorage (browser) | ||
* if fileNameOrLocalStorage is not set options.location is used. | ||
*/ | ||
async saveDatabase(fileNameOrLocalStorage: string): Promise<void> { | ||
await this.driver.save(fileNameOrLocalStorage); | ||
} | ||
|
||
/** | ||
* Returns the current database definition. | ||
*/ | ||
exportDatabase(): Uint8Array { | ||
return this.driver.export(); | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.