@smuzi/database 0.0.13 → 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.
@@ -1,17 +1,14 @@
1
- import { TInsertRow, TInsertRowResult, TQueryResult } from "./types.js";
1
+ import { TInsertRowResult, TQueryResult } from "./types.js";
2
2
  import { Option } from "@smuzi/std";
3
- import { SchemaObject } from "@smuzi/schema";
4
- export declare const migrationLogRowSchema: SchemaObject<{
5
- name: import("@smuzi/schema").SchemaString;
6
- branch: import("@smuzi/schema").SchemaNumber;
7
- action: import("@smuzi/schema").SchemaString;
8
- sql_source: import("@smuzi/schema").SchemaString;
9
- created_at: import("@smuzi/schema").SchemaNativeDate<{
10
- msg: string;
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<never>>;
33
- listRuned(): Promise<TQueryResult<TMigrationLogRowSchema>>;
34
- listRunedByBranch(branch: number): Promise<TQueryResult<TMigrationLogRowSchema>>;
29
+ createTableIfNotExists(): Promise<TQueryResult>;
30
+ listRuned(): Promise<TQueryResult<TMigrationLogRow>>;
31
+ listRunedByBranch(branch: number): Promise<TQueryResult<TMigrationLogRow>>;
35
32
  getLastBranch(): Promise<Option<number>>;
36
- create<const RC extends (keyof TMigrationLogSave)[]>(row: TInsertRow<TMigrationLogRowSchema>, returningColumns?: RC): Promise<TInsertRowResult<TMigrationLogRowSchema, RC>>;
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<SchemaObject<any>>>;
36
+ freshSchema(): Promise<TQueryResult>;
40
37
  };
41
38
  export declare const Migrations: (groupName?: string) => TMigrations;
42
39
  export declare function Migration(migration: TMigration): TMigration;
@@ -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, RecordFromKeys, Result, Simplify, StdError, StdRecord } from "@smuzi/std";
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<S extends SchemaObject> = {
22
- rows: TableRows<S>;
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<S extends SchemaObject> = Result<TQueryRawResult<S>, DBQueryError>;
26
- export type TInsertRowResult<S extends SchemaObject, Columns extends readonly (keyof S["__infer"])[]> = Result<Option<Simplify<RecordFromKeys<S["__infer"], Columns>>>, DBQueryError>;
27
- 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>;
28
- export declare class TableRows<Schema extends SchemaObject, 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"]>, Rows extends Array<Record<string, unknown>> = Array<Record<string, unknown>>> {
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(schema: Option<Schema>, rows: Rows);
31
- get(key: number): Option<TableRow>;
29
+ constructor(rows: Array<Record<string, unknown>>);
30
+ get(key: number): Option<StdRecord<Row>>;
32
31
  has(key: number): boolean;
33
- entries(): IterableIterator<[number, TableRow]>;
34
- [Symbol.iterator](): IterableIterator<[number, TableRow]>;
35
- unsafeSource(): Rows;
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<S extends SchemaObject<any>>(sql: string, params?: TQueryParams, schema?: Option<S>): Promise<TQueryResult<S>>;
39
- insertRow<S extends SchemaObject<any>, const RC extends string[]>(table: string, schema: S, row: TInsertRow<S>, returningColumns?: RC): Promise<TInsertRowResult<S, RC>>;
40
- insertManyRows<S extends SchemaObject<any>, const RC extends string[]>(table: string, schema: S, rows: TInsertRow<S>[], returningColumns?: RC): Promise<TInsertManyRowResult<S, RC>>;
41
- updateRowById<S extends SchemaObject<any>>(table: string, schema: S, id: number | string, row: Partial<TInsertRow<S>>, idColumn?: string): Promise<TQueryResult<S>>;
42
- updateManyRows<S extends SchemaObject<any>>(table: string, values: Partial<TInsertRow<S>>, where: any): Promise<TQueryResult<S>>;
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, OptionFromNullable, Some, StdError, StdRecord } from "@smuzi/std";
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
- #schema;
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(this.#prepareRow(this.#rows[key]));
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.13",
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",
@@ -34,7 +34,6 @@
34
34
  },
35
35
  "peerDependencies": {
36
36
  "@smuzi/std": "^0.2.14",
37
- "@smuzi/schema": "^0.0.19",
38
37
  "@smuzi/console": "^0.0.11"
39
38
  },
40
39
  "devDependencies": {