tsondb 0.21.11 → 0.23.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.
@@ -15,9 +15,6 @@ export type Config<T extends DefaultTSONDBTypes = DefaultTSONDBTypes> = {
15
15
  */
16
16
  locales?: string[];
17
17
  dataRootPath?: string;
18
- homeLayoutSections?: HomeLayoutSection[];
19
- customStylesheetPath?: string;
20
- customAssetsPath?: string;
21
18
  validationOptions?: Partial<ValidationOptions>;
22
19
  formattingOptions?: Partial<FormatterOptions>;
23
20
  };
@@ -29,6 +26,9 @@ export type FormatterOptions = {
29
26
  };
30
27
  export type ServerOptions = {
31
28
  port: number;
29
+ homeLayoutSections?: HomeLayoutSection[];
30
+ customStylesheetPath?: string;
31
+ customAssetsPath?: string;
32
32
  };
33
33
  export type HomeLayoutSection = {
34
34
  title: string;
@@ -58,10 +58,7 @@ export declare const validateConfigForData: (config: Config) => asserts config i
58
58
  export type ServerConfig<T extends DefaultTSONDBTypes = DefaultTSONDBTypes> = DataConfig<T> & {
59
59
  serverOptions?: ServerOptions;
60
60
  locales: string[];
61
- homeLayoutSections?: HomeLayoutSection[];
62
61
  validationOptions?: Partial<ValidationOptions>;
63
- customStylesheetPath?: string;
64
- customAssetsPath?: string;
65
62
  formattingOptions?: Partial<FormatterOptions>;
66
63
  };
67
64
  /**
@@ -1,9 +1,9 @@
1
- import type { InstanceContainer, InstanceContainerOverview, InstanceContent, SingletonInstanceContainer } from "../shared/utils/instances.ts";
2
- import type { FormatterOptions } from "./config.ts";
1
+ import type { InstanceContainer, InstanceContent, SingletonInstanceContainer } from "../shared/utils/instances.ts";
2
+ import type { FormatterOptions, ServerOptions } from "./config.ts";
3
3
  import { Git } from "./git.js";
4
- import type { Output } from "./output.ts";
4
+ import { ReadonlyTSONDB } from "./readonly.ts";
5
5
  import type { EntityDecl } from "./schema/dsl/index.ts";
6
- import type { AnyChildEntityMap, AnyEntityMap, AnyEnumMap, AnySingletonEntityMap, AnyTypeAliasMap, IdArgsVariant } from "./schema/generatedTypeHelpers.ts";
6
+ import type { AnyChildEntityMap, AnyEntityMap, AnyEnumMap, AnySingletonEntityMap, AnyTypeAliasMap } from "./schema/generatedTypeHelpers.ts";
7
7
  import type { Schema } from "./schema/index.ts";
8
8
  import { Transaction } from "./transaction.ts";
9
9
  export interface DefaultTSONDBTypes {
@@ -116,7 +116,7 @@ export type HookTrigger = "transaction:before" | "transaction:after";
116
116
  /**
117
117
  * The main class for managing a typed JSON database.
118
118
  */
119
- export declare class TSONDB<T extends DefaultTSONDBTypes = DefaultTSONDBTypes> {
119
+ export declare class TSONDB<T extends DefaultTSONDBTypes = DefaultTSONDBTypes> extends ReadonlyTSONDB<T> {
120
120
  #private;
121
121
  private constructor();
122
122
  /**
@@ -126,35 +126,10 @@ export declare class TSONDB<T extends DefaultTSONDBTypes = DefaultTSONDBTypes> {
126
126
  * @param skipReferenceCache If set to `true`, the reference cache will not be created during initialization. This can speed up the initialization process, but referential integrity checks will not work correctly until the cache is created. This should only be set to `true` if you plan to call `sync()` immediately after initialization.
127
127
  */
128
128
  static create<Types extends DefaultTSONDBTypes = DefaultTSONDBTypes>(options: TSONDBOptions<Types>, skipReferenceCache?: boolean): Promise<TSONDB<Types>>;
129
- /**
130
- * Generates outputs based on the provided schema and output configurations.
131
- */
132
- static generateOutputs(options: {
133
- schema: Schema;
134
- outputs: Output[];
135
- }): Promise<void>;
136
- /**
137
- * Generates outputs based on the current schema and the provided output configurations.
138
- */
139
- generateOutputs(outputs: Output[]): Promise<void>;
140
- /**
141
- * Validates the data in the database.
142
- *
143
- * Returns `true` if the data is valid, `false` otherwise.
144
- */
145
- validate(): boolean;
146
129
  /**
147
130
  * Formats the data on disk according to the current in-memory representation.
148
131
  */
149
132
  format(): Promise<void>;
150
- /**
151
- * Checks if the data on disk is formatted according to the current in-memory representation.
152
- */
153
- checkFormat(): Promise<boolean>;
154
- /**
155
- * Reloads the data from disk into memory.
156
- */
157
- sync(): Promise<void>;
158
133
  /**
159
134
  * Runs a transaction on the database.
160
135
  *
@@ -216,82 +191,6 @@ export declare class TSONDB<T extends DefaultTSONDBTypes = DefaultTSONDBTypes> {
216
191
  * @throws {Error}
217
192
  */
218
193
  reorderInstanceOfEntity(entityName: EntityName<T> | EntityDecl<EntityName<T>>, instanceId: string, newIndex: number): Promise<[id: string, oldContent: InstanceContent, newContent: InstanceContent][]>;
219
- /**
220
- * Retrieves an instance of the specified entity by its identifier.
221
- */
222
- getInstanceOfEntityById<E extends EntityName<T>>(...args: IdArgsVariant<T["entityMap"], E>): T["entityMap"][E] | undefined;
223
- /**
224
- * Retrieves the instance container of the specified entity by its identifier.
225
- */
226
- getInstanceContainerOfEntityById<E extends EntityName<T>>(...args: IdArgsVariant<T["entityMap"], E>): InstanceContainer<T["entityMap"][E]> | undefined;
227
- /**
228
- * Retrieves the overview of an instance of the specified entity by its identifier.
229
- */
230
- getInstanceOverviewOfEntityById(...args: IdArgsVariant<T["entityMap"]>): InstanceContainerOverview | undefined;
231
- /**
232
- * Retrieves all instances of the specified entity.
233
- */
234
- getAllInstancesOfEntity<E extends EntityName<T>>(entityName: E): Entity<T, E>[];
235
- /**
236
- * Retrieves all instance containers of the specified entity.
237
- */
238
- getAllInstanceContainersOfEntity<E extends EntityName<T>>(entityName: E): InstanceContainer<Entity<T, E>>[];
239
- /**
240
- * Retrieves all instance overviews of the specified entity.
241
- */
242
- getAllInstanceOverviewsOfEntity(entity: EntityDecl<EntityName<T>> | EntityName<T>): InstanceContainerOverview[];
243
- /**
244
- * Retrieves an instance of the specified entity by its identifier.
245
- */
246
- getSingletonInstanceOfEntity<E extends SingletonEntityName<T>>(entityName: E): T["singletonEntityMap"][E] | undefined;
247
- /**
248
- * Retrieves the instance container of the specified entity by its identifier.
249
- */
250
- getSingletonInstanceContainerOfEntity<E extends SingletonEntityName<T>>(entityName: E): SingletonInstanceContainer<T["singletonEntityMap"][E]> | undefined;
251
- /**
252
- * Counts the number of instances registered for the specified entity.
253
- */
254
- countInstancesOfEntity(entityName: EntityName<T>): number;
255
- /**
256
- * Returns whether the singleton entity has an instance.
257
- */
258
- hasInstanceOfSingletonEntity(entityName: SingletonEntityName<T>): boolean;
259
- /**
260
- * Retrieves overviews for all instances in the database, grouped by entity name.
261
- */
262
- getAllInstanceOverviews(): Record<EntityName<T>, InstanceContainerOverview[]>;
263
- /**
264
- * Retrieves all instance containers of the specified child entity for a given parent instance.
265
- */
266
- getAllChildInstanceContainersForParent<U extends ChildEntityName<T>>(childEntityName: U, parentId: ChildEntityConfig<T, U>[2]): InstanceContainer<ChildEntity<T, U>>[];
267
- /**
268
- * Displays the name of an entity instance including its ID. If no display name is found, `undefined` is returned.
269
- */
270
- getDisplayName(...args: IdArgsVariant<T["entityMap"]>): string | undefined;
271
- /**
272
- * Displays the name of an entity instance including its ID. If no display name is found, only the ID is returned.
273
- */
274
- getDisplayNameWithId(...args: IdArgsVariant<T["entityMap"]>): string;
275
- /**
276
- * Checks if a given instance is referenced by other instances in the database.
277
- *
278
- * If `otherInstancesToDelete` is provided, those instances will be excluded from the check. This can be useful when planning to delete multiple instances at once and wanting to ignore references from those instances.
279
- */
280
- isReferencedByOtherInstances(instance: string, otherInstancesToDelete?: string[]): boolean;
281
- /**
282
- * The configured locales for this TSONDB instance.
283
- */
284
- get locales(): string[];
285
- /**
286
- * Sets the locales for this TSONDB instance.
287
- *
288
- * The provided locales must exist in the database if a locale entity is defined in the schema.
289
- */
290
- setLocales(locales: string[]): void;
291
- /**
292
- * The loaded schema for this TSONDB instance.
293
- */
294
- get schema(): Schema<T>;
295
194
  /**
296
195
  * The Git wrapper for this TSONDB instance, if a git repository is connected.
297
196
  */
@@ -301,43 +200,24 @@ export declare class TSONDB<T extends DefaultTSONDBTypes = DefaultTSONDBTypes> {
301
200
  */
302
201
  isGitRepo(): this is TSONDBWithGit<T>;
303
202
  /**
304
- * Searches for instances matching the provided query in their ID or display name.
305
- *
306
- * The results are sorted by relevance. A custom relevance score function can be provided.
203
+ * Registers a new hook for the specified trigger. The callback will be invoked whenever the trigger is activated.
307
204
  */
308
- search(query: string, options?: Partial<{
309
- getRelevanceScore: <E extends EntityName<T>>(args: {
310
- entityName: E;
311
- id: string;
312
- content: Entity<T, E>;
313
- displayName: string;
314
- displayNameLocaleId: string | undefined;
315
- query: string;
316
- queryLowerCase: string;
317
- }) => number;
318
- }>): [string, InstanceContainerOverview][];
205
+ registerHook(trigger: HookTrigger, callback: (db: TSONDB<T>) => void): void;
319
206
  /**
320
- * Retrieves the name of the entity for a given instance ID. If no instance with the provided ID is found, `undefined` is returned.
207
+ * Unregisters a hook for the specified trigger.
321
208
  */
322
- getEntityNameOfInstanceId(id: string): string | undefined;
209
+ unregisterHook(trigger: HookTrigger, callback: (db: TSONDB<T>) => void): void;
323
210
  /**
324
211
  * Compresses the entire database into a single JSON file at the specified path.
325
212
  *
326
213
  * Instead of storing each instance in its own file, this method serializes the entire database into a single JSON file. This can be useful for backup, transfer or archiving purposes. The resulting file contains all instances organized by entity names. No spaces or indentation are used in the JSON output to minimize file size.
327
214
  * @param path The file path where the compressed database will be saved.
215
+ * @param indentation Optional. If provided, the JSON output will be formatted with the specified number of spaces for indentation. If not provided, the output will be minified without any extra spaces.
328
216
  */
329
- compressToSingleFile(path: string): Promise<void>;
330
- /**
331
- * Registers a new hook for the specified trigger. The callback will be invoked whenever the trigger is activated.
332
- */
333
- registerHook(trigger: HookTrigger, callback: (db: TSONDB<T>) => void): void;
334
- /**
335
- * Unregisters a hook for the specified trigger.
336
- */
337
- unregisterHook(trigger: HookTrigger, callback: (db: TSONDB<T>) => void): void;
217
+ compressToSingleFile(path: string, indentation?: number): Promise<void>;
218
+ serve(config: ServerOptions, callback?: (error?: NodeJS.ErrnoException) => void): void;
338
219
  }
339
220
  export type TSONDBWithGit<T extends DefaultTSONDBTypes = DefaultTSONDBTypes> = TSONDB<T> & {
340
221
  git: Git<T>;
341
222
  };
342
223
  export type { Config } from "./config.ts";
343
- export { createServer } from "./server/index.ts";