@stonyx/orm 0.2.1-beta.89 → 0.2.1-beta.90
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/dist/db.js +1 -1
- package/dist/hooks.d.ts +16 -1
- package/dist/types/orm-types.d.ts +5 -5
- package/package.json +1 -1
- package/src/db.ts +1 -1
- package/src/hooks.ts +17 -1
- package/src/orm-request.ts +1 -16
- package/src/postgres/postgres-db.ts +1 -1
- package/src/serializer.ts +1 -1
- package/src/transforms.ts +1 -1
- package/src/types/mysql2.d.ts +24 -5
- package/src/types/orm-types.ts +5 -5
- package/src/types/pg.d.ts +7 -3
- package/src/types/stonyx-rest-server.d.ts +6 -1
- package/src/types/stonyx-utils.d.ts +6 -6
package/dist/db.js
CHANGED
|
@@ -128,7 +128,7 @@ export default class DB {
|
|
|
128
128
|
await Promise.all(collectionKeys.map(async (key) => {
|
|
129
129
|
const filePath = path.join(dirPath, `${key}.json`);
|
|
130
130
|
const exists = await fileExists(filePath);
|
|
131
|
-
const data = jsonData[key] || [];
|
|
131
|
+
const data = (jsonData[key] || []);
|
|
132
132
|
if (exists)
|
|
133
133
|
await updateFile(filePath, data, { json: true });
|
|
134
134
|
else
|
package/dist/hooks.d.ts
CHANGED
|
@@ -2,7 +2,22 @@
|
|
|
2
2
|
* Middleware-based hooks registry for ORM operations.
|
|
3
3
|
* Unlike event-based hooks, middleware hooks run sequentially and can halt operations.
|
|
4
4
|
*/
|
|
5
|
-
|
|
5
|
+
export interface HookContext {
|
|
6
|
+
model: string;
|
|
7
|
+
operation: string;
|
|
8
|
+
request?: unknown;
|
|
9
|
+
params?: Record<string, string>;
|
|
10
|
+
body?: Record<string, unknown>;
|
|
11
|
+
query?: Record<string, string>;
|
|
12
|
+
state?: Record<string, unknown>;
|
|
13
|
+
oldState?: unknown;
|
|
14
|
+
recordId?: string | number;
|
|
15
|
+
response?: unknown;
|
|
16
|
+
record?: unknown;
|
|
17
|
+
records?: unknown[];
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
type HookHandler = (context: HookContext) => unknown | Promise<unknown>;
|
|
6
21
|
/**
|
|
7
22
|
* Register a before hook middleware that runs before the operation executes.
|
|
8
23
|
*
|
|
@@ -5,7 +5,7 @@ export interface OrmDbConfig {
|
|
|
5
5
|
mode: string;
|
|
6
6
|
directory: string;
|
|
7
7
|
autosave: string;
|
|
8
|
-
saveInterval:
|
|
8
|
+
saveInterval: string | number;
|
|
9
9
|
}
|
|
10
10
|
export interface OrmMysqlConfig {
|
|
11
11
|
host: string;
|
|
@@ -63,23 +63,23 @@ export interface SourceRecord {
|
|
|
63
63
|
};
|
|
64
64
|
__data?: Record<string, unknown>;
|
|
65
65
|
__relationships?: Record<string, unknown>;
|
|
66
|
-
id:
|
|
66
|
+
id: string | number;
|
|
67
67
|
[key: string]: unknown;
|
|
68
68
|
}
|
|
69
69
|
export interface OrmRecord {
|
|
70
|
-
id: string | number
|
|
70
|
+
id: string | number;
|
|
71
71
|
__model?: {
|
|
72
72
|
__name: string;
|
|
73
73
|
};
|
|
74
74
|
__data: Record<string, unknown> & {
|
|
75
|
-
id?:
|
|
75
|
+
id?: string | number;
|
|
76
76
|
__pendingSqlId?: boolean;
|
|
77
77
|
};
|
|
78
78
|
__relationships: Record<string, unknown>;
|
|
79
79
|
toJSON?(options?: {
|
|
80
80
|
fields?: Set<string>;
|
|
81
81
|
baseUrl?: string;
|
|
82
|
-
}): unknown
|
|
82
|
+
}): Record<string, unknown>;
|
|
83
83
|
[key: string]: unknown;
|
|
84
84
|
}
|
|
85
85
|
export interface ForeignKeyDef {
|
package/package.json
CHANGED
package/src/db.ts
CHANGED
|
@@ -167,7 +167,7 @@ export default class DB {
|
|
|
167
167
|
await Promise.all(collectionKeys.map(async key => {
|
|
168
168
|
const filePath = path.join(dirPath, `${key}.json`);
|
|
169
169
|
const exists = await fileExists(filePath);
|
|
170
|
-
const data = jsonData[key] || [];
|
|
170
|
+
const data = (jsonData[key] || []) as Record<string, unknown> | unknown[];
|
|
171
171
|
|
|
172
172
|
if (exists) await updateFile(filePath, data, { json: true });
|
|
173
173
|
else await createFile(filePath, data, { json: true });
|
package/src/hooks.ts
CHANGED
|
@@ -19,7 +19,23 @@
|
|
|
19
19
|
* Unlike event-based hooks, middleware hooks run sequentially and can halt operations.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
export interface HookContext {
|
|
23
|
+
model: string;
|
|
24
|
+
operation: string;
|
|
25
|
+
request?: unknown;
|
|
26
|
+
params?: Record<string, string>;
|
|
27
|
+
body?: Record<string, unknown>;
|
|
28
|
+
query?: Record<string, string>;
|
|
29
|
+
state?: Record<string, unknown>;
|
|
30
|
+
oldState?: unknown;
|
|
31
|
+
recordId?: string | number;
|
|
32
|
+
response?: unknown;
|
|
33
|
+
record?: unknown;
|
|
34
|
+
records?: unknown[];
|
|
35
|
+
[key: string]: unknown;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type HookHandler = (context: HookContext) => unknown | Promise<unknown>;
|
|
23
39
|
|
|
24
40
|
// Map of "operation:model" -> handler[]
|
|
25
41
|
const beforeHooks: Map<string, HookHandler[]> = new Map();
|
package/src/orm-request.ts
CHANGED
|
@@ -3,6 +3,7 @@ import Orm, { store, createRecord, updateRecord } from '@stonyx/orm';
|
|
|
3
3
|
import { camelCaseToKebabCase } from '@stonyx/utils/string';
|
|
4
4
|
import { getPluralName } from './plural-registry.js';
|
|
5
5
|
import { getBeforeHooks, getAfterHooks } from './hooks.js';
|
|
6
|
+
import type { HookContext } from './hooks.js';
|
|
6
7
|
import config from 'stonyx/config';
|
|
7
8
|
import type { OrmRecord } from './types/orm-types.js';
|
|
8
9
|
import { isOrmRecord } from './utils.js';
|
|
@@ -26,22 +27,6 @@ interface Filter {
|
|
|
26
27
|
value: string;
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
interface HookContext {
|
|
30
|
-
model: string;
|
|
31
|
-
operation: string;
|
|
32
|
-
request: OrmRequest$;
|
|
33
|
-
params: { [key: string]: string };
|
|
34
|
-
body?: { [key: string]: unknown };
|
|
35
|
-
query?: { [key: string]: string };
|
|
36
|
-
state: { [key: string]: unknown };
|
|
37
|
-
oldState?: unknown;
|
|
38
|
-
recordId?: string | number;
|
|
39
|
-
response?: unknown;
|
|
40
|
-
record?: unknown;
|
|
41
|
-
records?: unknown;
|
|
42
|
-
[key: string]: unknown;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
30
|
interface JsonApiResponse {
|
|
46
31
|
data: unknown;
|
|
47
32
|
links?: { [key: string]: string };
|
|
@@ -507,7 +507,7 @@ export default class PostgresDB {
|
|
|
507
507
|
// Re-key the record in the store if PostgreSQL generated the ID (via RETURNING)
|
|
508
508
|
if (isPendingId && result.rows.length > 0) {
|
|
509
509
|
const pendingId = record.id;
|
|
510
|
-
const realId = result.rows[0].id;
|
|
510
|
+
const realId = result.rows[0].id as string | number;
|
|
511
511
|
const modelStore = (this.deps.store as unknown as { get(name: string): Map<unknown, unknown> }).get(modelName);
|
|
512
512
|
|
|
513
513
|
modelStore.delete(pendingId);
|
package/src/serializer.ts
CHANGED
|
@@ -33,7 +33,7 @@ function query(rawData: unknown, pathPrefix: string, subPath: unknown): unknown
|
|
|
33
33
|
|
|
34
34
|
const [path, getter, pointer] = makeArray(subPath) as [string, unknown, string | undefined];
|
|
35
35
|
const fullPath = `${pathPrefix}${path}`;
|
|
36
|
-
const value = get(rawData, fullPath);
|
|
36
|
+
const value = get(rawData as Record<string, unknown>, fullPath);
|
|
37
37
|
|
|
38
38
|
if (getter === undefined || getter === null) return value;
|
|
39
39
|
|
package/src/transforms.ts
CHANGED
|
@@ -7,7 +7,7 @@ const transforms: Record<string, (value: unknown) => unknown> = {
|
|
|
7
7
|
number: (value: unknown) => parseInt(value as string),
|
|
8
8
|
passthrough: (value: unknown) => value,
|
|
9
9
|
string: (value: unknown) => String(value),
|
|
10
|
-
timestamp: (value: unknown) => getTimestamp(value),
|
|
10
|
+
timestamp: (value: unknown) => getTimestamp(value as string | number | Date | undefined),
|
|
11
11
|
trim: (value: unknown) => (value as string)?.trim(),
|
|
12
12
|
uppercase: (value: unknown) => (value as string)?.toUpperCase(),
|
|
13
13
|
};
|
package/src/types/mysql2.d.ts
CHANGED
|
@@ -5,16 +5,35 @@ declare module 'mysql2/promise' {
|
|
|
5
5
|
password: string;
|
|
6
6
|
database: string;
|
|
7
7
|
port?: number;
|
|
8
|
-
|
|
8
|
+
waitForConnections?: boolean;
|
|
9
|
+
connectionLimit?: number;
|
|
10
|
+
queueLimit?: number;
|
|
11
|
+
enableKeepAlive?: boolean;
|
|
12
|
+
keepAliveInitialDelay?: number;
|
|
13
|
+
[key: string]: string | number | boolean | undefined;
|
|
9
14
|
}
|
|
10
15
|
|
|
11
|
-
interface
|
|
12
|
-
|
|
16
|
+
interface ExecuteResult {
|
|
17
|
+
insertId: number;
|
|
18
|
+
affectedRows: number;
|
|
19
|
+
changedRows: number;
|
|
20
|
+
fieldCount: number;
|
|
21
|
+
info: string;
|
|
22
|
+
serverStatus: number;
|
|
23
|
+
warningStatus: number;
|
|
13
24
|
}
|
|
14
25
|
|
|
26
|
+
interface FieldPacket {
|
|
27
|
+
name: string;
|
|
28
|
+
type: number;
|
|
29
|
+
length: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type RowDataPacket = Record<string, string | number | boolean | null>;
|
|
33
|
+
|
|
15
34
|
interface Pool {
|
|
16
|
-
execute(sql: string, params?: unknown[]): Promise<[
|
|
17
|
-
query(sql: string, params?: unknown[]): Promise<[
|
|
35
|
+
execute(sql: string, params?: unknown[]): Promise<[RowDataPacket[] | ExecuteResult, FieldPacket[]]>;
|
|
36
|
+
query(sql: string, params?: unknown[]): Promise<[RowDataPacket[] | ExecuteResult, FieldPacket[]]>;
|
|
18
37
|
end(): Promise<void>;
|
|
19
38
|
getConnection(): Promise<PoolConnection>;
|
|
20
39
|
}
|
package/src/types/orm-types.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface OrmDbConfig {
|
|
|
6
6
|
mode: string;
|
|
7
7
|
directory: string;
|
|
8
8
|
autosave: string;
|
|
9
|
-
saveInterval:
|
|
9
|
+
saveInterval: string | number;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export interface OrmMysqlConfig {
|
|
@@ -68,16 +68,16 @@ export interface SourceRecord {
|
|
|
68
68
|
__model: { __name: string; [key: string]: unknown };
|
|
69
69
|
__data?: Record<string, unknown>;
|
|
70
70
|
__relationships?: Record<string, unknown>;
|
|
71
|
-
id:
|
|
71
|
+
id: string | number;
|
|
72
72
|
[key: string]: unknown;
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
export interface OrmRecord {
|
|
76
|
-
id: string | number
|
|
76
|
+
id: string | number;
|
|
77
77
|
__model?: { __name: string };
|
|
78
|
-
__data: Record<string, unknown> & { id?:
|
|
78
|
+
__data: Record<string, unknown> & { id?: string | number; __pendingSqlId?: boolean };
|
|
79
79
|
__relationships: Record<string, unknown>;
|
|
80
|
-
toJSON?(options?: { fields?: Set<string>; baseUrl?: string }): unknown
|
|
80
|
+
toJSON?(options?: { fields?: Set<string>; baseUrl?: string }): Record<string, unknown>;
|
|
81
81
|
[key: string]: unknown;
|
|
82
82
|
}
|
|
83
83
|
|
package/src/types/pg.d.ts
CHANGED
|
@@ -5,13 +5,17 @@ declare module 'pg' {
|
|
|
5
5
|
password?: string;
|
|
6
6
|
database?: string;
|
|
7
7
|
port?: number;
|
|
8
|
-
|
|
8
|
+
max?: number;
|
|
9
|
+
idleTimeoutMillis?: number;
|
|
10
|
+
connectionTimeoutMillis?: number;
|
|
9
11
|
}
|
|
10
12
|
|
|
13
|
+
type RowData = Record<string, string | number | boolean | null>;
|
|
14
|
+
|
|
11
15
|
interface QueryResult {
|
|
12
|
-
rows:
|
|
16
|
+
rows: RowData[];
|
|
13
17
|
rowCount: number;
|
|
14
|
-
fields?: { name: string }[];
|
|
18
|
+
fields?: { name: string; dataTypeID: number }[];
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
export class Pool {
|
|
@@ -3,9 +3,14 @@ declare module '@stonyx/rest-server' {
|
|
|
3
3
|
constructor(...args: unknown[]);
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
+
interface RouteOptions {
|
|
7
|
+
name: string;
|
|
8
|
+
options?: { model: string; access: (request: unknown) => unknown } | Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
|
|
6
11
|
export default class RestServer {
|
|
7
12
|
static instance: RestServer;
|
|
8
13
|
static close(): void;
|
|
9
|
-
mountRoute(RequestClass:
|
|
14
|
+
mountRoute(RequestClass: typeof Request, options: RouteOptions): void;
|
|
10
15
|
}
|
|
11
16
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
declare module '@stonyx/utils/file' {
|
|
2
|
-
export function createFile(path: string, data: unknown, options?: { json?: boolean }): Promise<void>;
|
|
2
|
+
export function createFile(path: string, data: string | Record<string, unknown> | unknown[], options?: { json?: boolean }): Promise<void>;
|
|
3
3
|
export function createDirectory(path: string): Promise<void>;
|
|
4
|
-
export function updateFile(path: string, data: unknown, options?: { json?: boolean }): Promise<void>;
|
|
5
|
-
export function readFile(path: string, options?: { json?: boolean; missingFileCallback?: () => Promise<unknown
|
|
4
|
+
export function updateFile(path: string, data: string | Record<string, unknown> | unknown[], options?: { json?: boolean }): Promise<void>;
|
|
5
|
+
export function readFile(path: string, options?: { json?: boolean; missingFileCallback?: () => Promise<Record<string, unknown>> }): Promise<string | Record<string, unknown> | unknown[]>;
|
|
6
6
|
export function fileExists(path: string): Promise<boolean>;
|
|
7
7
|
export function deleteDirectory(path: string): Promise<void>;
|
|
8
8
|
export function forEachFileImport(
|
|
9
9
|
path: string,
|
|
10
|
-
callback: (exported:
|
|
10
|
+
callback: (exported: Function | Record<string, unknown>, meta: { name: string }) => void | unknown,
|
|
11
11
|
options?: { ignoreAccessFailure?: boolean; rawName?: boolean; recursive?: boolean; recursiveNaming?: boolean }
|
|
12
12
|
): Promise<void>;
|
|
13
13
|
}
|
|
@@ -19,13 +19,13 @@ declare module '@stonyx/utils/string' {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
declare module '@stonyx/utils/object' {
|
|
22
|
-
export function get(obj:
|
|
22
|
+
export function get(obj: Record<string, unknown>, path: string): unknown;
|
|
23
23
|
export function getOrSet<T>(map: Map<unknown, T>, key: unknown, defaultValue: T): T;
|
|
24
24
|
export function makeArray<T>(value: T | T[]): T[];
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
declare module '@stonyx/utils/date' {
|
|
28
|
-
export function getTimestamp(value?:
|
|
28
|
+
export function getTimestamp(value?: string | number | Date): number;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
declare module '@stonyx/utils/prompt' {
|