@smuzi/database 0.0.12 → 0.0.14
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/build/migration.d.ts +15 -18
- package/build/migration.js +0 -8
- package/build/types.d.ts +17 -25
- package/build/types.js +3 -38
- package/package.json +3 -4
package/build/migration.d.ts
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TInsertRowResult, TQueryResult } from "./types.js";
|
|
2
2
|
import { Option } from "@smuzi/std";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
name:
|
|
6
|
-
branch:
|
|
7
|
-
action:
|
|
8
|
-
sql_source:
|
|
9
|
-
created_at:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}>;
|
|
13
|
-
export type TMigrationLogRowSchema = typeof migrationLogRowSchema;
|
|
14
|
-
export type TMigrationLogSave = TMigrationLogRowSchema["__infer"];
|
|
3
|
+
export type TMigrationLogRow = {
|
|
4
|
+
id: number;
|
|
5
|
+
name: string;
|
|
6
|
+
branch: number;
|
|
7
|
+
action: string;
|
|
8
|
+
sql_source: string;
|
|
9
|
+
created_at: Date;
|
|
10
|
+
};
|
|
11
|
+
export type TMigrationLogInsert = Omit<TMigrationLogRow, "id">;
|
|
15
12
|
export type TMigration = {
|
|
16
13
|
up: () => string;
|
|
17
14
|
down: () => string;
|
|
@@ -29,14 +26,14 @@ export declare enum TMigrationLogAction {
|
|
|
29
26
|
}
|
|
30
27
|
export type TMigrationsLogRepository = {
|
|
31
28
|
getTable(): string;
|
|
32
|
-
createTableIfNotExists(): Promise<TQueryResult
|
|
33
|
-
listRuned(): Promise<TQueryResult<
|
|
34
|
-
listRunedByBranch(branch: number): Promise<TQueryResult<
|
|
29
|
+
createTableIfNotExists(): Promise<TQueryResult>;
|
|
30
|
+
listRuned(): Promise<TQueryResult<TMigrationLogRow>>;
|
|
31
|
+
listRunedByBranch(branch: number): Promise<TQueryResult<TMigrationLogRow>>;
|
|
35
32
|
getLastBranch(): Promise<Option<number>>;
|
|
36
|
-
create
|
|
33
|
+
create(row: TMigrationLogInsert, returningColumns?: readonly (keyof TMigrationLogRow)[]): Promise<TInsertRowResult<TMigrationLogRow>>;
|
|
37
34
|
migrationLastAction(name: string): Promise<Option<string>>;
|
|
38
35
|
migrationWillBeRuned(name: string): Promise<boolean>;
|
|
39
|
-
freshSchema(): Promise<TQueryResult
|
|
36
|
+
freshSchema(): Promise<TQueryResult>;
|
|
40
37
|
};
|
|
41
38
|
export declare const Migrations: (groupName?: string) => TMigrations;
|
|
42
39
|
export declare function Migration(migration: TMigration): TMigration;
|
package/build/migration.js
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
1
|
import { panic } from "@smuzi/std";
|
|
2
|
-
import { schema } from "@smuzi/schema";
|
|
3
|
-
export const migrationLogRowSchema = schema.obj({
|
|
4
|
-
name: schema.string(),
|
|
5
|
-
branch: schema.number(),
|
|
6
|
-
action: schema.string(),
|
|
7
|
-
sql_source: schema.string(),
|
|
8
|
-
created_at: schema.datetime.native(),
|
|
9
|
-
});
|
|
10
2
|
export var TMigrationLogAction;
|
|
11
3
|
(function (TMigrationLogAction) {
|
|
12
4
|
TMigrationLogAction["up"] = "up";
|
package/build/types.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Option,
|
|
2
|
-
import { SchemaObject, SchemaStorageAutoNumber } from "@smuzi/schema";
|
|
1
|
+
import { Option, Result, StdError, StdRecord } from "@smuzi/std";
|
|
3
2
|
import { TMigrations, TMigrationsLogRepository } from "./migration.js";
|
|
4
3
|
export type TQueryParams = unknown[] | Record<string, unknown>;
|
|
5
4
|
export declare class DBQueryError {
|
|
@@ -18,38 +17,31 @@ export declare class DBQueryError {
|
|
|
18
17
|
});
|
|
19
18
|
toError(): StdError;
|
|
20
19
|
}
|
|
21
|
-
export type TQueryRawResult<
|
|
22
|
-
rows: TableRows<
|
|
20
|
+
export type TQueryRawResult<Row extends Record<string, unknown> = Record<string, unknown>> = {
|
|
21
|
+
rows: TableRows<Row>;
|
|
23
22
|
rowCount: Option<number>;
|
|
24
23
|
};
|
|
25
|
-
export type TQueryResult<
|
|
26
|
-
export type TInsertRowResult<
|
|
27
|
-
export type TInsertManyRowResult<
|
|
28
|
-
export declare class TableRows<
|
|
24
|
+
export type TQueryResult<Row extends Record<string, unknown> = Record<string, unknown>> = Result<TQueryRawResult<Row>, DBQueryError>;
|
|
25
|
+
export type TInsertRowResult<Row extends Record<string, unknown>> = Result<Option<StdRecord<Row>>, DBQueryError>;
|
|
26
|
+
export type TInsertManyRowResult<Row extends Record<string, unknown>> = Result<TableRows<Row>, DBQueryError>;
|
|
27
|
+
export declare class TableRows<Row extends Record<string, unknown> = Record<string, unknown>> {
|
|
29
28
|
#private;
|
|
30
|
-
constructor(
|
|
31
|
-
get(key: number): Option<
|
|
29
|
+
constructor(rows: Array<Record<string, unknown>>);
|
|
30
|
+
get(key: number): Option<StdRecord<Row>>;
|
|
32
31
|
has(key: number): boolean;
|
|
33
|
-
entries(): IterableIterator<[number,
|
|
34
|
-
[Symbol.iterator](): IterableIterator<[number,
|
|
35
|
-
unsafeSource():
|
|
32
|
+
entries(): IterableIterator<[number, StdRecord<Row>]>;
|
|
33
|
+
[Symbol.iterator](): IterableIterator<[number, StdRecord<Row>]>;
|
|
34
|
+
unsafeSource(): Record<string, unknown>[];
|
|
36
35
|
}
|
|
37
36
|
export interface TDatabaseClient {
|
|
38
|
-
query<
|
|
39
|
-
insertRow<
|
|
40
|
-
insertManyRows<
|
|
41
|
-
updateRowById<
|
|
42
|
-
updateManyRows<
|
|
37
|
+
query<Row extends Record<string, unknown> = Record<string, unknown>>(sql: string, params?: TQueryParams): Promise<TQueryResult<Row>>;
|
|
38
|
+
insertRow<Insert extends Record<string, unknown>, Row extends Record<string, unknown> = Insert>(table: string, row: Insert, returningColumns?: readonly (keyof Row)[]): Promise<TInsertRowResult<Row>>;
|
|
39
|
+
insertManyRows<Insert extends Record<string, unknown>, Row extends Record<string, unknown> = Insert>(table: string, rows: Insert[], returningColumns?: readonly (keyof Row)[]): Promise<TInsertManyRowResult<Row>>;
|
|
40
|
+
updateRowById<Row extends Record<string, unknown>>(table: string, id: number | string, row: Partial<Row>, idColumn?: string): Promise<TQueryResult<Row>>;
|
|
41
|
+
updateManyRows<Row extends Record<string, unknown>>(table: string, values: Partial<Row>, where: string): Promise<TQueryResult<Row>>;
|
|
43
42
|
}
|
|
44
43
|
export type TDatabaseService = {
|
|
45
44
|
client: TDatabaseClient;
|
|
46
45
|
buildMigrations: () => TMigrations;
|
|
47
46
|
buildMigrationLogRepository: (client: TDatabaseClient) => TMigrationsLogRepository;
|
|
48
47
|
};
|
|
49
|
-
export type IsExcludeSaving<T> = T extends SchemaStorageAutoNumber ? true : false;
|
|
50
|
-
export type ExcludeExcludeSaveKeys<T> = {
|
|
51
|
-
[K in keyof T]: IsExcludeSaving<T[K]> extends true ? never : K;
|
|
52
|
-
}[keyof T];
|
|
53
|
-
export type TInsertRow<S extends SchemaObject> = S extends SchemaObject<infer U> ? {
|
|
54
|
-
[K in ExcludeExcludeSaveKeys<U>]: S["__infer"][K];
|
|
55
|
-
} : {};
|
package/build/types.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { None,
|
|
2
|
-
import { SchemaOption, } from "@smuzi/schema";
|
|
1
|
+
import { None, Some, StdError, StdRecord } from "@smuzi/std";
|
|
3
2
|
export class DBQueryError {
|
|
4
3
|
sql;
|
|
5
4
|
message;
|
|
@@ -26,46 +25,12 @@ export class DBQueryError {
|
|
|
26
25
|
}
|
|
27
26
|
export class TableRows {
|
|
28
27
|
#rows;
|
|
29
|
-
|
|
30
|
-
constructor(schema, rows) {
|
|
28
|
+
constructor(rows) {
|
|
31
29
|
this.#rows = rows;
|
|
32
|
-
this.#schema = schema;
|
|
33
|
-
}
|
|
34
|
-
#prepareRow(row) {
|
|
35
|
-
return this.#schema.match({
|
|
36
|
-
None: () => new StdRecord(row),
|
|
37
|
-
Some: (schema) => {
|
|
38
|
-
const config = schema.getConfig();
|
|
39
|
-
const prepare = {};
|
|
40
|
-
for (const field in config) {
|
|
41
|
-
/**
|
|
42
|
-
* TODO:
|
|
43
|
-
* A database is a sufficiently reliable data source
|
|
44
|
-
* to skip validating the retrieved data.
|
|
45
|
-
* However, it is important to understand that type inference in this case
|
|
46
|
-
* does not guarantee a 100% match with the actual database types.
|
|
47
|
-
* This especially affects working with nullable fields.
|
|
48
|
-
* For example, a field was required but later became nullable.
|
|
49
|
-
* In this case, without changing the schema, `null` values will start flowing into your code.
|
|
50
|
-
* This issue can be solved by `StdRecord`, but then all fields have to be handled as `Option`,
|
|
51
|
-
* which is very inconvenient for developers.
|
|
52
|
-
**/
|
|
53
|
-
if (field in row) {
|
|
54
|
-
if (config[field] instanceof SchemaOption) {
|
|
55
|
-
prepare[field] = OptionFromNullable(row[field]);
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
prepare[field] = row[field];
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
return new StdRecord(prepare);
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
30
|
}
|
|
66
31
|
get(key) {
|
|
67
32
|
if (this.has(key)) {
|
|
68
|
-
return Some(
|
|
33
|
+
return Some(new StdRecord(this.#rows[key]));
|
|
69
34
|
}
|
|
70
35
|
return None();
|
|
71
36
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smuzi/database",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.14",
|
|
4
4
|
"description": "Database access layer and migrations toolkit for JavaScript and TypeScript",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -33,9 +33,8 @@
|
|
|
33
33
|
"#lib/*": "./src/*"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@smuzi/std": "^0.2.
|
|
37
|
-
"@smuzi/
|
|
38
|
-
"@smuzi/console": "^0.0.10"
|
|
36
|
+
"@smuzi/std": "^0.2.14",
|
|
37
|
+
"@smuzi/console": "^0.0.11"
|
|
39
38
|
},
|
|
40
39
|
"devDependencies": {
|
|
41
40
|
"@types/node": "^22.15.21",
|