@smuzi/database 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -7
- package/src/commands/migrations/fresh.ts +0 -15
- package/src/commands/migrations/refresh.ts +0 -57
- package/src/commands/migrations/rollback.ts +0 -45
- package/src/commands/migrations/run.ts +0 -40
- package/src/commands/router.ts +0 -21
- package/src/config.ts +0 -6
- package/src/helpers.ts +0 -30
- package/src/index.ts +0 -5
- package/src/migration.ts +0 -80
- package/src/types.ts +0 -189
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smuzi/database",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Database access layer and migrations toolkit for JavaScript and TypeScript",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
"access": "public"
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
18
|
-
"
|
|
19
|
-
"
|
|
18
|
+
"build/**/*.js",
|
|
19
|
+
"build/**/*.d.ts"
|
|
20
20
|
],
|
|
21
21
|
"exports": {
|
|
22
22
|
"./package.json": "./package.json",
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
"#lib/*": "./src/*"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@smuzi/std": "0.2.
|
|
37
|
-
"@smuzi/schema": "0.0.
|
|
38
|
-
"@smuzi/console": "0.0.
|
|
36
|
+
"@smuzi/std": "0.2.8",
|
|
37
|
+
"@smuzi/schema": "0.0.7",
|
|
38
|
+
"@smuzi/console": "0.0.6"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/node": "^22.15.21",
|
|
@@ -43,5 +43,6 @@
|
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
45
|
"build": "tsc --project tsconfig.build.json"
|
|
46
|
-
}
|
|
46
|
+
},
|
|
47
|
+
"main": "./build/index.js"
|
|
47
48
|
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import {TDatabaseService} from "../../types.js";
|
|
2
|
-
import {TOutputConsole} from "@smuzi/console";
|
|
3
|
-
import run from "./run.js";
|
|
4
|
-
import {Ok, OkOrNullableAsError, OptionFromNullable} from "@smuzi/std";
|
|
5
|
-
|
|
6
|
-
export default function (service: TDatabaseService) {
|
|
7
|
-
return async (output: TOutputConsole, params) => {
|
|
8
|
-
|
|
9
|
-
output.success('Drop all tables...');
|
|
10
|
-
|
|
11
|
-
(await service.buildMigrationLogRepository(service.client).freshSchema()).unwrap()
|
|
12
|
-
|
|
13
|
-
await run(service)(output, params);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import {TDatabaseService } from "../../types.js";
|
|
2
|
-
import {TOutputConsole} from "@smuzi/console";
|
|
3
|
-
import {Ok, OkOrNullableAsError, OptionFromNullable} from "@smuzi/std";
|
|
4
|
-
import {clearSQL} from "../../helpers.js";
|
|
5
|
-
import {TMigrationLogAction} from "../../migration.js";
|
|
6
|
-
|
|
7
|
-
export default function (service: TDatabaseService) {
|
|
8
|
-
return async (output: TOutputConsole, params) => {
|
|
9
|
-
const migrationsLogRepository = service.buildMigrationLogRepository(service.client);
|
|
10
|
-
|
|
11
|
-
const sortedLogMigrations = (await migrationsLogRepository.listRuned()).unwrap();
|
|
12
|
-
const migrations = service.buildMigrations();
|
|
13
|
-
|
|
14
|
-
if (sortedLogMigrations.rowCount.isZero()) {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
for (const [key, rowLog] of sortedLogMigrations.rows) {
|
|
19
|
-
const name = rowLog.get("name").unwrap();
|
|
20
|
-
const migration = migrations.getByName(name)
|
|
21
|
-
|
|
22
|
-
output.success('Down migration - ' + name)
|
|
23
|
-
|
|
24
|
-
const sql_source = clearSQL(migration.down());
|
|
25
|
-
(await service.client.query(sql_source)).unwrap();
|
|
26
|
-
|
|
27
|
-
(await migrationsLogRepository.create({
|
|
28
|
-
name,
|
|
29
|
-
branch: rowLog.get("branch").unwrap(),
|
|
30
|
-
action: TMigrationLogAction.down,
|
|
31
|
-
sql_source,
|
|
32
|
-
created_at: new Date()
|
|
33
|
-
})).unwrap()
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const branch = (await migrationsLogRepository.getLastBranch())
|
|
37
|
-
.match({
|
|
38
|
-
Some: value => ++value,
|
|
39
|
-
None: () => 1
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
for (const [name, migration] of migrations.getList()) {
|
|
43
|
-
output.success('Run migration - ' + name)
|
|
44
|
-
|
|
45
|
-
const sql_source = clearSQL(migration.up());
|
|
46
|
-
await service.client.query(sql_source);
|
|
47
|
-
|
|
48
|
-
await migrationsLogRepository.create({
|
|
49
|
-
name,
|
|
50
|
-
branch,
|
|
51
|
-
action: TMigrationLogAction.up,
|
|
52
|
-
sql_source,
|
|
53
|
-
created_at: new Date()
|
|
54
|
-
})
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import {TDatabaseService} from "../../types.js";
|
|
2
|
-
import {Ok, OkOrNullableAsError, OptionFromNullable} from "@smuzi/std";
|
|
3
|
-
import {TOutputConsole} from "@smuzi/console";
|
|
4
|
-
import {clearSQL} from "../../helpers.js";
|
|
5
|
-
import {TMigrationLogAction} from "../../migration.js";
|
|
6
|
-
|
|
7
|
-
export default function (service: TDatabaseService) {
|
|
8
|
-
return async (output: TOutputConsole, params) => {
|
|
9
|
-
|
|
10
|
-
const migrationsLogRepository = service.buildMigrationLogRepository(service.client);
|
|
11
|
-
|
|
12
|
-
const branch = await params.get("branch").match({
|
|
13
|
-
Some: async (val) => val as number,
|
|
14
|
-
None: async () => (await migrationsLogRepository.getLastBranch()).unwrap(`Last branch not founded in ${migrationsLogRepository.getTable()} table`)
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
output.info("Branch for rollback - " + branch)
|
|
18
|
-
|
|
19
|
-
const logMigrations = (await migrationsLogRepository.listRunedByBranch(branch)).unwrap();
|
|
20
|
-
const migrations = service.buildMigrations();
|
|
21
|
-
|
|
22
|
-
if (logMigrations.rowCount.isZero()) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
for (const [key, rowLog] of logMigrations.rows) {
|
|
27
|
-
const name = rowLog.get("name").unwrap();
|
|
28
|
-
|
|
29
|
-
const migration = migrations.getByName(name)
|
|
30
|
-
|
|
31
|
-
output.success('Down migration - ' + name)
|
|
32
|
-
|
|
33
|
-
const sql_source = clearSQL(migration.down());
|
|
34
|
-
(await service.client.query(sql_source)).unwrap();
|
|
35
|
-
|
|
36
|
-
(await migrationsLogRepository.create({
|
|
37
|
-
name,
|
|
38
|
-
branch,
|
|
39
|
-
action: TMigrationLogAction.down,
|
|
40
|
-
sql_source,
|
|
41
|
-
created_at: new Date(),
|
|
42
|
-
})).unwrap()
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import {TDatabaseService} from "../../types.js";
|
|
2
|
-
import {Ok, OkOrNullableAsError, OptionFromNullable} from "@smuzi/std";
|
|
3
|
-
import {TOutputConsole} from "@smuzi/console";
|
|
4
|
-
import {clearSQL} from "../../helpers.js";
|
|
5
|
-
import {TMigrationLogAction} from "../../migration.js";
|
|
6
|
-
|
|
7
|
-
export default function (service: TDatabaseService) {
|
|
8
|
-
return async (output: TOutputConsole, params) => {
|
|
9
|
-
|
|
10
|
-
const migrationsLogRepository = service.buildMigrationLogRepository(service.client);
|
|
11
|
-
|
|
12
|
-
(await migrationsLogRepository.createTableIfNotExists()).unwrap();
|
|
13
|
-
|
|
14
|
-
const branch = (await migrationsLogRepository.getLastBranch())
|
|
15
|
-
.match({
|
|
16
|
-
Some: value => ++value,
|
|
17
|
-
None: () => 1
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
for (const [name, migration] of service.buildMigrations().getList()) {
|
|
21
|
-
if (await migrationsLogRepository.migrationWillBeRuned(name)) {
|
|
22
|
-
continue;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
output.success('Run migration - ' + name)
|
|
26
|
-
|
|
27
|
-
const sql_source = clearSQL(migration.up());
|
|
28
|
-
|
|
29
|
-
(await service.client.query(sql_source)).unwrap();
|
|
30
|
-
|
|
31
|
-
(await migrationsLogRepository.create({
|
|
32
|
-
name,
|
|
33
|
-
branch,
|
|
34
|
-
action: TMigrationLogAction.up,
|
|
35
|
-
sql_source,
|
|
36
|
-
created_at: new Date(),
|
|
37
|
-
})).unwrap();
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
package/src/commands/router.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import {CreateConsoleRouter} from "@smuzi/console";
|
|
2
|
-
import migrationsRun from "./migrations/run.js";
|
|
3
|
-
import migrationsRollback from "./migrations/rollback.js";
|
|
4
|
-
import migrationsRefresh from "./migrations/refresh.js";
|
|
5
|
-
import migrationsFresh from "./migrations/fresh.js";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export const databaseConsole = (config) => {
|
|
9
|
-
const router = CreateConsoleRouter('db:');
|
|
10
|
-
const routerMigrations = CreateConsoleRouter('migrations:');
|
|
11
|
-
|
|
12
|
-
routerMigrations.add('run', migrationsRun(config))
|
|
13
|
-
routerMigrations.add('rollback', migrationsRollback(config))
|
|
14
|
-
routerMigrations.add('refresh', migrationsRefresh(config))
|
|
15
|
-
routerMigrations.add('fresh', migrationsFresh(config))
|
|
16
|
-
|
|
17
|
-
router.group(routerMigrations);
|
|
18
|
-
|
|
19
|
-
return router;
|
|
20
|
-
}
|
|
21
|
-
|
package/src/config.ts
DELETED
package/src/helpers.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import {dump, Err, isEmpty, isOption, Ok, Result} from "@smuzi/std";
|
|
2
|
-
import {TQueryParams} from "./types.js";
|
|
3
|
-
|
|
4
|
-
export function clearSQL(sql: string): string
|
|
5
|
-
{
|
|
6
|
-
return sql.trim();
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function preparedSqlFromObjectToArrayParams(sql: string, params: TQueryParams): Result<{sql: string, params: any[]}, string>
|
|
10
|
-
{
|
|
11
|
-
let paramArray: string[] = [];
|
|
12
|
-
let index = 1;
|
|
13
|
-
let error = '';
|
|
14
|
-
|
|
15
|
-
const processedSql = sql.replace(/:(\w+)/g, (input, key) => {
|
|
16
|
-
if (params.hasOwnProperty(key)) {
|
|
17
|
-
paramArray.push(isOption(params[key]) ? params[key].someOr(null) : params[key]);
|
|
18
|
-
return `$${index++}`;
|
|
19
|
-
}
|
|
20
|
-
error += `Missing parameter: ${key}. `;
|
|
21
|
-
|
|
22
|
-
return input;
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
if (isEmpty(error)) {
|
|
26
|
-
return Ok({sql: processedSql, params: paramArray});
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return Err(error);
|
|
30
|
-
}
|
package/src/index.ts
DELETED
package/src/migration.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import {TInsertRow, TInsertRowResult, TQueryResult,} from "./types.js";
|
|
2
|
-
import {Option, panic} from "@smuzi/std";
|
|
3
|
-
import {schema, SchemaObject} from "@smuzi/schema";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export const migrationLogRowSchema = schema.obj({
|
|
7
|
-
name: schema.string(),
|
|
8
|
-
branch: schema.number(),
|
|
9
|
-
action: schema.string(),
|
|
10
|
-
sql_source: schema.string(),
|
|
11
|
-
created_at: schema.datetime.native(),
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
export type TMigrationLogRowSchema = typeof migrationLogRowSchema;
|
|
15
|
-
export type TMigrationLogSave = TMigrationLogRowSchema["__infer"]
|
|
16
|
-
|
|
17
|
-
export type TMigration = {
|
|
18
|
-
up: () => string,
|
|
19
|
-
down: () => string,
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export type TMigrations = {
|
|
23
|
-
add: (name: string, migration: TMigration) => void,
|
|
24
|
-
group: (migrations: TMigrations) => void,
|
|
25
|
-
getList: () => Map<string, TMigration>,
|
|
26
|
-
getByName: (name: string) => TMigration,
|
|
27
|
-
getGroupName:() => string
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export enum TMigrationLogAction {
|
|
31
|
-
up = 'up',
|
|
32
|
-
down = 'down',
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
export type TMigrationsLogRepository = {
|
|
37
|
-
getTable(): string,
|
|
38
|
-
createTableIfNotExists(): Promise<TQueryResult<never>>,
|
|
39
|
-
listRuned(): Promise<TQueryResult<TMigrationLogRowSchema>>,
|
|
40
|
-
listRunedByBranch(branch: number): Promise<TQueryResult<TMigrationLogRowSchema>>,
|
|
41
|
-
getLastBranch(): Promise<Option<number>>,
|
|
42
|
-
create<const RC extends (keyof TMigrationLogSave)[]>(row: TInsertRow<TMigrationLogRowSchema>, returningColumns?: RC): Promise<TInsertRowResult<TMigrationLogRowSchema, RC>>,
|
|
43
|
-
migrationLastAction(name: string): Promise<Option<string>>,
|
|
44
|
-
migrationWillBeRuned(name: string): Promise<boolean>,
|
|
45
|
-
freshSchema(): Promise<TQueryResult<SchemaObject<any>>>
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
export const Migrations = (groupName: string = ''): TMigrations => {
|
|
50
|
-
const migrations = new Map();
|
|
51
|
-
|
|
52
|
-
return {
|
|
53
|
-
add(name: string, migration) {
|
|
54
|
-
if (migrations.has(name)) {
|
|
55
|
-
panic(`Migration with name = '${name}' is not unique`)
|
|
56
|
-
}
|
|
57
|
-
migrations.set(name, migration)
|
|
58
|
-
},
|
|
59
|
-
group(migrationsGroup) {
|
|
60
|
-
const groupName = migrationsGroup.getGroupName();
|
|
61
|
-
|
|
62
|
-
for (const [name, migration] of migrationsGroup.getList()) {
|
|
63
|
-
migrations.set(groupName + name, migration)
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
getList() {
|
|
67
|
-
return migrations;
|
|
68
|
-
},
|
|
69
|
-
getGroupName() {
|
|
70
|
-
return groupName;
|
|
71
|
-
},
|
|
72
|
-
getByName(name) {
|
|
73
|
-
return migrations.get(name);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function Migration(migration: TMigration){
|
|
79
|
-
return migration;
|
|
80
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
dump,
|
|
3
|
-
None,
|
|
4
|
-
Option,
|
|
5
|
-
OptionFromNullable,
|
|
6
|
-
RecordFromKeys,
|
|
7
|
-
Result,
|
|
8
|
-
Simplify,
|
|
9
|
-
Some, StdError,
|
|
10
|
-
StdList,
|
|
11
|
-
StdRecord
|
|
12
|
-
} from "@smuzi/std"
|
|
13
|
-
import {
|
|
14
|
-
schema,
|
|
15
|
-
SchemaObject,
|
|
16
|
-
SchemaOption,
|
|
17
|
-
SchemaRule,
|
|
18
|
-
SchemaStorageAutoNumber,
|
|
19
|
-
} from "@smuzi/schema"
|
|
20
|
-
import {TMigrations, TMigrationsLogRepository} from "./migration.js";
|
|
21
|
-
|
|
22
|
-
export type TQueryParams = unknown[] | Record<string, unknown>
|
|
23
|
-
|
|
24
|
-
export class DBQueryError {
|
|
25
|
-
readonly sql: string
|
|
26
|
-
readonly message: string
|
|
27
|
-
readonly code: Option<string>
|
|
28
|
-
readonly detail: Option<string>
|
|
29
|
-
readonly table: Option<string>
|
|
30
|
-
readonly trace: string
|
|
31
|
-
|
|
32
|
-
constructor(config: {sql: string, message: string, code: Option<string>, detail: Option<string>, table: Option<string>} ) {
|
|
33
|
-
this.sql = config.sql;
|
|
34
|
-
this.message = config.message;
|
|
35
|
-
this.code = config.code;
|
|
36
|
-
this.detail = config.detail;
|
|
37
|
-
this.table = config.table;
|
|
38
|
-
this.trace = new Error().stack as string;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
toError(): StdError {
|
|
42
|
-
return new StdError(
|
|
43
|
-
this.message,
|
|
44
|
-
Some(this.trace),
|
|
45
|
-
Some({
|
|
46
|
-
sql: this.sql,
|
|
47
|
-
code: this.code,
|
|
48
|
-
table: this.table,
|
|
49
|
-
detail: this.detail,
|
|
50
|
-
})
|
|
51
|
-
)
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export type TQueryRawResult<S extends SchemaObject> = {
|
|
56
|
-
rows: TableRows<S>,
|
|
57
|
-
rowCount: Option<number>
|
|
58
|
-
}
|
|
59
|
-
export type TQueryResult<S extends SchemaObject> = Result<TQueryRawResult<S>, DBQueryError>
|
|
60
|
-
export type TInsertRowResult<S extends SchemaObject, Columns extends readonly (keyof S["__infer"])[]> = Result<Option<Simplify<RecordFromKeys<S["__infer"], Columns>>>, DBQueryError>
|
|
61
|
-
export type TInsertManyRowResult<S extends SchemaObject, Columns extends readonly (keyof S["__infer"])[], Prepared extends SchemaObject = SchemaObject<RecordFromKeys<ReturnType<S["getConfig"]>, Columns>>> = Result<TableRows<Prepared>, DBQueryError>
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
export class TableRows<
|
|
65
|
-
Schema extends SchemaObject,
|
|
66
|
-
TableRow extends Option<Schema> extends Option<never> ? StdRecord<Record<string, unknown>> : StdRecord<Schema["__infer"]> = Option<Schema> extends Option<never> ? StdRecord<Record<string, unknown>> : StdRecord<Schema["__infer"]>,
|
|
67
|
-
Rows extends Array<Record<string, unknown>> = Array<Record<string, unknown>>
|
|
68
|
-
> {
|
|
69
|
-
#rows: Rows
|
|
70
|
-
#schema: Option<Schema>
|
|
71
|
-
|
|
72
|
-
constructor(schema: Option<Schema>, rows: Rows) {
|
|
73
|
-
this.#rows = rows;
|
|
74
|
-
this.#schema = schema;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
#prepareRow(row: Record<string, unknown>): TableRow {
|
|
78
|
-
return this.#schema.match({
|
|
79
|
-
None: () => new StdRecord(row) as TableRow,
|
|
80
|
-
Some: (schema) => {
|
|
81
|
-
|
|
82
|
-
const config = schema.getConfig();
|
|
83
|
-
const prepare = {} as any;
|
|
84
|
-
for (const field in config) {
|
|
85
|
-
/**
|
|
86
|
-
* TODO:
|
|
87
|
-
* A database is a sufficiently reliable data source
|
|
88
|
-
* to skip validating the retrieved data.
|
|
89
|
-
* However, it is important to understand that type inference in this case
|
|
90
|
-
* does not guarantee a 100% match with the actual database types.
|
|
91
|
-
* This especially affects working with nullable fields.
|
|
92
|
-
* For example, a field was required but later became nullable.
|
|
93
|
-
* In this case, without changing the schema, `null` values will start flowing into your code.
|
|
94
|
-
* This issue can be solved by `StdRecord`, but then all fields have to be handled as `Option`,
|
|
95
|
-
* which is very inconvenient for developers.
|
|
96
|
-
**/
|
|
97
|
-
if (field in row) {
|
|
98
|
-
if (config[field] instanceof SchemaOption) {
|
|
99
|
-
prepare[field] = OptionFromNullable(row[field]);
|
|
100
|
-
} else {
|
|
101
|
-
prepare[field] = row[field];
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
return new StdRecord(prepare) as TableRow;
|
|
107
|
-
}
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
get(key: number): Option<TableRow> {
|
|
113
|
-
if (this.has(key)) {
|
|
114
|
-
return Some(this.#prepareRow(this.#rows[key]));
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return None();
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
has(key: number) {
|
|
121
|
-
return key in this.#rows;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
*entries(): IterableIterator<[number, TableRow]> {
|
|
125
|
-
for (let k = 0; k < this.#rows.length; k++) {
|
|
126
|
-
yield [k, this.get(k).unwrap()];
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
[Symbol.iterator](): IterableIterator<[number, TableRow]> {
|
|
131
|
-
return this.entries();
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
unsafeSource() {
|
|
135
|
-
return this.#rows;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
export interface TDatabaseClient {
|
|
141
|
-
query<S extends SchemaObject<any>>(
|
|
142
|
-
sql: string,
|
|
143
|
-
params?: TQueryParams,
|
|
144
|
-
schema?: Option<S>
|
|
145
|
-
): Promise<TQueryResult<S>>;
|
|
146
|
-
|
|
147
|
-
insertRow<S extends SchemaObject<any>, const RC extends string[]>(
|
|
148
|
-
table: string,
|
|
149
|
-
schema: S,
|
|
150
|
-
row: TInsertRow<S>,
|
|
151
|
-
returningColumns?: RC
|
|
152
|
-
): Promise<TInsertRowResult<S, RC>>;
|
|
153
|
-
|
|
154
|
-
insertManyRows<S extends SchemaObject<any>, const RC extends string[]>(
|
|
155
|
-
table: string,
|
|
156
|
-
schema: S,
|
|
157
|
-
rows: TInsertRow<S>[],
|
|
158
|
-
returningColumns?: RC
|
|
159
|
-
): Promise<TInsertManyRowResult<S, RC>>;
|
|
160
|
-
|
|
161
|
-
updateRowById<S extends SchemaObject<any>>(
|
|
162
|
-
table: string,
|
|
163
|
-
schema: S,
|
|
164
|
-
id: number | string,
|
|
165
|
-
row: Partial<TInsertRow<S>>,
|
|
166
|
-
idColumn?: string
|
|
167
|
-
): Promise<TQueryResult<S>>;
|
|
168
|
-
|
|
169
|
-
updateManyRows<S extends SchemaObject<any>>(table: string, values: Partial<TInsertRow<S>>, where): Promise<TQueryResult<S>>
|
|
170
|
-
|
|
171
|
-
// updateManyRows: <Entity = TRow>(table: string, values: TInsertRow<Entity>, where: string) => Promise<TQueryResult>,
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export type TDatabaseService = {
|
|
175
|
-
client: TDatabaseClient,
|
|
176
|
-
buildMigrations: () => TMigrations,
|
|
177
|
-
buildMigrationLogRepository: (client: TDatabaseClient) => TMigrationsLogRepository,
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
export type IsExcludeSaving<T> = T extends SchemaStorageAutoNumber ? true : false;
|
|
181
|
-
|
|
182
|
-
export type ExcludeExcludeSaveKeys<T> = {
|
|
183
|
-
[K in keyof T]: IsExcludeSaving<T[K]> extends true ? never : K
|
|
184
|
-
}[keyof T];
|
|
185
|
-
|
|
186
|
-
export type TInsertRow<S extends SchemaObject> = S extends SchemaObject<infer U> ? {
|
|
187
|
-
[K in ExcludeExcludeSaveKeys<U>]: S["__infer"][K]
|
|
188
|
-
} : {};
|
|
189
|
-
|