-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79eb6bd
commit 5bc6f5c
Showing
33 changed files
with
1,264 additions
and
1,874 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
"typescript.tsdk": "node_modules/typescript/lib", | ||
"editor.codeActionsOnSave": ["source.organizeImports"] | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,63 +1,24 @@ | ||
import { ExtractTable } from './Query'; | ||
import { ISchemaAny } from './schema'; | ||
import { ITable, Table } from './Table'; | ||
import { mapObject } from './Utils'; | ||
import { SchemaAny } from './schema'; | ||
import { DatabaseTable } from './DatabaseTable'; | ||
import { ExtractTable } from './DatabaseTableQuery'; | ||
import { IDriverDatabase, IDriverStatement } from './Driver'; | ||
import { schemaToCreateTableQueries } from './CreateTableUtils'; | ||
|
||
export type VariableArgFunction = (...params: any[]) => any; | ||
export type ArgumentTypes<F extends VariableArgFunction> = F extends (...args: infer A) => any ? A : never; | ||
|
||
export interface Transaction<F extends VariableArgFunction> { | ||
(...params: ArgumentTypes<F>): ReturnType<F>; | ||
default(...params: ArgumentTypes<F>): ReturnType<F>; | ||
deferred(...params: ArgumentTypes<F>): ReturnType<F>; | ||
immediate(...params: ArgumentTypes<F>): ReturnType<F>; | ||
exclusive(...params: ArgumentTypes<F>): ReturnType<F>; | ||
} | ||
|
||
export class Database< | ||
DriverStatement extends IDriverStatement, | ||
DriverDatabase extends IDriverDatabase<DriverStatement>, | ||
Schema extends SchemaAny | ||
> { | ||
readonly driverDatabase: DriverDatabase; | ||
readonly schema: Schema; | ||
export interface IDatabase<Schema extends ISchemaAny> { | ||
readonly tables: { | ||
[K in keyof Schema['tables']]: DatabaseTable<DriverStatement, DriverDatabase, Schema, K, ExtractTable<Schema, K>>; | ||
[K in keyof Schema['tables']]: ITable<Schema, K, ExtractTable<Schema, K>>; | ||
}; | ||
} | ||
|
||
private readonly createTableQueries: ReadonlyArray<string>; | ||
|
||
readonly fingerpring: number; | ||
|
||
constructor(driverDatabase: DriverDatabase, schema: Schema, fingerprint: number) { | ||
this.driverDatabase = driverDatabase; | ||
this.schema = schema; | ||
this.fingerpring = fingerprint; | ||
this.createTableQueries = schemaToCreateTableQueries(schema); | ||
this.tables = mapObject(schema.tables, (tableName) => { | ||
return new DatabaseTable(driverDatabase, schema, tableName); | ||
}); | ||
} | ||
|
||
writeFingerprint() { | ||
this.driverDatabase.setUserVersion(this.fingerpring); | ||
} | ||
|
||
initSchema() { | ||
const selectTablesQuery = this.driverDatabase.prepare(`SELECT name FROM sqlite_master WHERE type = 'table'`); | ||
const currentTables = selectTablesQuery.all(); | ||
if (currentTables.length) { | ||
throw new Error(`Cannot init schema on non-empty database`); | ||
} | ||
this.createTableQueries.forEach((query) => { | ||
// console.info('-> ' + query); | ||
this.driverDatabase.exec(query); | ||
}); | ||
} | ||
export const Database = (() => { | ||
return { | ||
create, | ||
}; | ||
|
||
close() { | ||
this.driverDatabase.close(); | ||
function create<ISchema extends ISchemaAny>(schema: ISchema): IDatabase<ISchema> { | ||
return { | ||
tables: mapObject(schema.tables, (tableName) => { | ||
return Table.create(schema, tableName); | ||
}), | ||
}; | ||
} | ||
} | ||
})(); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.