@kollors/deep-json-server 0.6.0 → 0.7.0
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/README.md +41 -12
- package/README.ru.md +41 -12
- package/index.js +5 -0
- package/package.json +9 -2
- package/src/cli.js +3 -3
- package/src/config.js +14 -5
- package/src/constants.js +1 -1
- package/src/database.js +19 -28
- package/src/files.js +394 -129
- package/src/openapi/document.js +109 -32
- package/src/query/filter.js +11 -10
- package/src/query/pagination.js +2 -2
- package/src/relation-metadata.js +1 -0
- package/src/server.js +47 -45
- package/src/utils.js +30 -0
- package/types/index.d.ts +10 -0
- package/types/src/config.d.ts +28 -41
- package/types/src/constants.d.ts +1 -1
- package/types/src/database.d.ts +25 -3
- package/types/src/files.d.ts +61 -21
- package/types/src/openapi/document.d.ts +2 -2
- package/types/src/server.d.ts +9 -8
- package/types/src/utils.d.ts +8 -0
package/types/src/config.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export type DatabaseConfig = {
|
|
|
11
11
|
};
|
|
12
12
|
export type MemoryFile = {
|
|
13
13
|
content: Uint8Array;
|
|
14
|
-
|
|
14
|
+
directory?: string;
|
|
15
15
|
mimeType: string;
|
|
16
16
|
name: string;
|
|
17
17
|
};
|
|
@@ -24,6 +24,16 @@ export type FilesConfig = {
|
|
|
24
24
|
directory: string;
|
|
25
25
|
metadata: string;
|
|
26
26
|
};
|
|
27
|
+
export type OpenapiConfig = {
|
|
28
|
+
path?: string;
|
|
29
|
+
};
|
|
30
|
+
export type ServerConfig = {
|
|
31
|
+
host?: string;
|
|
32
|
+
logger?: boolean | Record<string, unknown>;
|
|
33
|
+
maxFileSize?: number;
|
|
34
|
+
maxPageSize?: number;
|
|
35
|
+
port?: number;
|
|
36
|
+
};
|
|
27
37
|
export type DeepJsonServerConfig = {
|
|
28
38
|
/**
|
|
29
39
|
* Database source and optional schema.
|
|
@@ -36,51 +46,28 @@ export type DeepJsonServerConfig = {
|
|
|
36
46
|
/**
|
|
37
47
|
* Generated OpenAPI file.
|
|
38
48
|
*/
|
|
39
|
-
openapi?:
|
|
40
|
-
path?: string;
|
|
41
|
-
};
|
|
49
|
+
openapi?: OpenapiConfig;
|
|
42
50
|
/**
|
|
43
51
|
* Runtime settings.
|
|
44
52
|
*/
|
|
45
|
-
server?:
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
53
|
+
server?: ServerConfig;
|
|
54
|
+
};
|
|
55
|
+
export type NormalizedServerConfig = {
|
|
56
|
+
database: DatabaseConfig;
|
|
57
|
+
files?: FilesConfig;
|
|
58
|
+
openapi: OpenapiConfig;
|
|
59
|
+
server: ServerConfig;
|
|
52
60
|
};
|
|
53
61
|
/**
|
|
62
|
+
* Validates configuration and resolves relative paths.
|
|
54
63
|
* @param {DeepJsonServerConfig} config Server configuration.
|
|
55
64
|
* @param {string} [directoryPath] Base directory for relative paths.
|
|
56
|
-
* @returns {
|
|
65
|
+
* @returns {NormalizedServerConfig} Normalized configuration.
|
|
57
66
|
*/
|
|
58
|
-
export declare const normalizeServerConfig: (config: DeepJsonServerConfig, directoryPath?: string) =>
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
host?: string;
|
|
66
|
-
logger?: boolean | Record<string, unknown>;
|
|
67
|
-
maxFileSize?: number;
|
|
68
|
-
maxPageSize?: number;
|
|
69
|
-
port?: number;
|
|
70
|
-
};
|
|
71
|
-
};
|
|
72
|
-
/** @param {string} configPath Configuration module path. */
|
|
73
|
-
export declare function readServerConfig(configPath: string): Promise<{
|
|
74
|
-
database: DatabaseConfig;
|
|
75
|
-
files?: FilesConfig;
|
|
76
|
-
openapi: {
|
|
77
|
-
path?: string;
|
|
78
|
-
};
|
|
79
|
-
server: {
|
|
80
|
-
host?: string;
|
|
81
|
-
logger?: boolean | Record<string, unknown>;
|
|
82
|
-
maxFileSize?: number;
|
|
83
|
-
maxPageSize?: number;
|
|
84
|
-
port?: number;
|
|
85
|
-
};
|
|
86
|
-
}>;
|
|
67
|
+
export declare const normalizeServerConfig: (config: DeepJsonServerConfig, directoryPath?: string) => NormalizedServerConfig;
|
|
68
|
+
/**
|
|
69
|
+
* Loads an ES module config and resolves paths from its directory.
|
|
70
|
+
* @param {string} configPath Configuration module path.
|
|
71
|
+
* @returns {Promise<NormalizedServerConfig>} Normalized configuration.
|
|
72
|
+
*/
|
|
73
|
+
export declare function readServerConfig(configPath: string): Promise<NormalizedServerConfig>;
|
package/types/src/constants.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare const DEFAULT_HOST = "127.0.0.1";
|
|
2
2
|
export declare const DEFAULT_MAX_FILE_SIZE: number;
|
|
3
|
+
export declare const DEFAULT_MAX_PAGE_SIZE = 1000;
|
|
3
4
|
export declare const DEFAULT_PAGE_SIZE = 10;
|
|
4
5
|
export declare const DEFAULT_PORT = 4001;
|
|
5
|
-
export declare const MAX_PAGE_SIZE = 1000;
|
package/types/src/database.d.ts
CHANGED
|
@@ -1,11 +1,33 @@
|
|
|
1
|
+
export type DatabaseContainer = {
|
|
2
|
+
data: import('./config.js').DatabaseData;
|
|
3
|
+
};
|
|
4
|
+
export type DatabaseStore = {
|
|
5
|
+
/**
|
|
6
|
+
* Current database container.
|
|
7
|
+
*/
|
|
8
|
+
database: DatabaseContainer;
|
|
9
|
+
/**
|
|
10
|
+
* Resolved database file path.
|
|
11
|
+
*/
|
|
12
|
+
path?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Returns current data and reloads disk-backed sources.
|
|
15
|
+
*/
|
|
16
|
+
read: () => Promise<import('./config.js').DatabaseData>;
|
|
17
|
+
/**
|
|
18
|
+
* Runs a serialized update.
|
|
19
|
+
*/
|
|
20
|
+
update: <T>(operation: (database: DatabaseContainer) => T) => Promise<T>;
|
|
21
|
+
};
|
|
1
22
|
export declare const validateDatabase: (data: any) => any;
|
|
2
23
|
export declare const readJsonObject: (path: any, label: any) => Promise<any>;
|
|
3
24
|
export declare const readDatabaseFile: (databasePath: any) => Promise<any>;
|
|
4
25
|
/**
|
|
26
|
+
* Creates a disk- or memory-backed database with serialized updates.
|
|
5
27
|
* @param {import('./config.js').DatabaseConfig} config Database source.
|
|
6
|
-
* @returns {Promise<
|
|
28
|
+
* @returns {Promise<DatabaseStore>} Database store.
|
|
7
29
|
*/
|
|
8
|
-
export declare const createDatabaseStore: (config: import('./config.js').DatabaseConfig) => Promise<
|
|
30
|
+
export declare const createDatabaseStore: (config: import('./config.js').DatabaseConfig) => Promise<DatabaseStore>;
|
|
9
31
|
export declare const getCollection: (database: any, resource: any) => any[];
|
|
10
32
|
export declare const findItem: (collection: any, id: any) => any;
|
|
11
|
-
export declare const createId: (collection: any) =>
|
|
33
|
+
export declare const createId: (collection: any) => string;
|
package/types/src/files.d.ts
CHANGED
|
@@ -1,24 +1,64 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
export type StoredFileMetadata = {
|
|
3
|
+
directory: string;
|
|
4
|
+
mimeType: string;
|
|
5
|
+
name: string;
|
|
6
|
+
};
|
|
7
|
+
export type FileRecord = StoredFileMetadata & {
|
|
8
|
+
size: number;
|
|
9
|
+
};
|
|
10
|
+
export type FileMetadata = FileRecord & {
|
|
11
|
+
downloadUrl: string;
|
|
12
|
+
metadataUrl: string;
|
|
13
|
+
url: string;
|
|
14
|
+
};
|
|
15
|
+
export type FileUpload = {
|
|
16
|
+
directory: string;
|
|
17
|
+
maxFileSize: number;
|
|
18
|
+
mimeType: string;
|
|
19
|
+
name: string;
|
|
20
|
+
override: boolean;
|
|
21
|
+
stream: Readable;
|
|
22
|
+
};
|
|
23
|
+
export type FileUpdate = {
|
|
24
|
+
directory?: string;
|
|
25
|
+
name?: string;
|
|
26
|
+
};
|
|
27
|
+
export type FileStore = {
|
|
28
|
+
/**
|
|
29
|
+
* Returns file metadata and contents.
|
|
30
|
+
*/
|
|
31
|
+
get: (path: string) => Promise<{
|
|
32
|
+
file: FileRecord;
|
|
33
|
+
stream: Readable;
|
|
34
|
+
}>;
|
|
35
|
+
/**
|
|
36
|
+
* Returns file metadata.
|
|
37
|
+
*/
|
|
38
|
+
metadata: (path: string) => Promise<FileRecord>;
|
|
39
|
+
/**
|
|
40
|
+
* Deletes a file.
|
|
41
|
+
*/
|
|
42
|
+
remove: (path: string) => Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Renames or moves a file.
|
|
45
|
+
*/
|
|
46
|
+
update: (path: string, update: FileUpdate) => Promise<FileRecord>;
|
|
47
|
+
/**
|
|
48
|
+
* Stores a file.
|
|
49
|
+
*/
|
|
50
|
+
upload: (upload: FileUpload) => Promise<{
|
|
51
|
+
created: boolean;
|
|
52
|
+
file: FileRecord;
|
|
53
|
+
}>;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Creates a disk- or memory-backed file store with serialized operations.
|
|
57
|
+
* @param {import('./config.js').FilesConfig} config File storage configuration.
|
|
58
|
+
* @returns {Promise<FileStore>} File store.
|
|
59
|
+
*/
|
|
60
|
+
export declare const createFileStore: (config: import('./config.js').FilesConfig) => Promise<FileStore>;
|
|
61
|
+
export declare const registerFileRoutes: (fastify: any, { maxFileSize, store }: {
|
|
22
62
|
maxFileSize: any;
|
|
23
63
|
store: any;
|
|
24
64
|
}) => void;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Builds an OpenAPI
|
|
3
|
-
* @param {{ database: Record<string, Array<Record<string, unknown>>>, files?: boolean, maxPageSize?: number, schema?: Record<string, unknown> }} options
|
|
2
|
+
* Builds an OpenAPI document without runtime server addresses.
|
|
3
|
+
* @param {{ database: Record<string, Array<Record<string, unknown>>>, files?: boolean, maxPageSize?: number, schema?: Record<string, unknown> }} options Source data and schema settings.
|
|
4
4
|
* @returns {Record<string, unknown>} OpenAPI document.
|
|
5
5
|
*/
|
|
6
6
|
export declare function buildOpenapiDocument(options: {
|
package/types/src/server.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
export type OpenapiDocument = Record<string, unknown>;
|
|
2
|
+
export type ServerFacade = {
|
|
3
|
+
fastify: () => import('fastify').FastifyInstance;
|
|
4
|
+
openapi: () => Promise<OpenapiDocument>;
|
|
5
|
+
};
|
|
2
6
|
/**
|
|
3
|
-
* Creates
|
|
4
|
-
* @param {import('./config.js').DeepJsonServerConfig}
|
|
7
|
+
* Creates lazy Fastify and OpenAPI accessors from one configuration.
|
|
8
|
+
* @param {import('./config.js').DeepJsonServerConfig} config Server configuration.
|
|
5
9
|
* @param {{ files?: boolean }} [features] Optional feature switches.
|
|
6
|
-
* @returns {Promise<
|
|
10
|
+
* @returns {Promise<ServerFacade>} Server facade.
|
|
7
11
|
*/
|
|
8
|
-
export declare function createServer(
|
|
12
|
+
export declare function createServer(config: import('./config.js').DeepJsonServerConfig, features?: {
|
|
9
13
|
files?: boolean;
|
|
10
|
-
}): Promise<
|
|
11
|
-
fastify: () => import('fastify').FastifyInstance;
|
|
12
|
-
openapi: () => Promise<OpenapiDocument>;
|
|
13
|
-
}>;
|
|
14
|
+
}): Promise<ServerFacade>;
|
package/types/src/utils.d.ts
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
export declare const createHttpError: (statusCode: any, message: any) => Error;
|
|
2
|
+
/** @returns {<T>(operation: () => T | Promise<T>) => Promise<T>} Serialized operation scheduler. */
|
|
3
|
+
export declare const createSerialQueue: () => <T>(operation: () => T | Promise<T>) => Promise<T>;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a random ID that is not currently in use.
|
|
6
|
+
* @param {(id: string) => boolean} isUsed Checks whether an ID already exists.
|
|
7
|
+
* @returns {string} Unique ID.
|
|
8
|
+
*/
|
|
9
|
+
export declare const createUniqueId: (isUsed: (id: string) => boolean) => string;
|
|
2
10
|
export declare const getResourceNames: (data: any) => string[];
|
|
3
11
|
export declare const isObject: (value: any) => boolean;
|
|
4
12
|
export declare const isSafeKey: (key: any) => boolean;
|