tsondb 0.11.4 → 0.12.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.
@@ -7,6 +7,7 @@ import { groupDeclarationsBySourceUrl } from "../../schema/declarations/Declarat
7
7
  import { render } from "./render.js";
8
8
  const debug = Debug("tsondb:renderer:ts");
9
9
  const extension = ".d.ts";
10
+ const pragma = "// Generated by TSONDB -- do not edit manually.\n\n";
10
11
  export const TypeScriptOutput = (options) => ({
11
12
  run: async (schema) => {
12
13
  if (options.rendererOptions?.preserveFiles === true) {
@@ -23,7 +24,7 @@ export const TypeScriptOutput = (options) => ({
23
24
  const newDir = join(options.targetPath, relativePath);
24
25
  const newPath = join(newDir, basename(sourcePath, extname(sourcePath)) + extension);
25
26
  await mkdir(newDir, { recursive: true });
26
- await writeFile(newPath, render(options.rendererOptions, decls ?? []), {
27
+ await writeFile(newPath, pragma + render(options.rendererOptions, decls ?? []), {
27
28
  encoding: "utf-8",
28
29
  });
29
30
  }
@@ -33,7 +34,7 @@ export const TypeScriptOutput = (options) => ({
33
34
  else {
34
35
  debug("emitting declarations to single file...");
35
36
  await mkdir(dirname(options.targetPath), { recursive: true });
36
- await writeFile(options.targetPath, render(options.rendererOptions, schema.declarations), {
37
+ await writeFile(options.targetPath, pragma + render(options.rendererOptions, schema.declarations), {
37
38
  encoding: "utf-8",
38
39
  });
39
40
  debug("emitted declarations to %s", options.targetPath);
@@ -3,5 +3,7 @@ export type TypeScriptRendererOptions = {
3
3
  indentation: number;
4
4
  objectTypeKeyword: "interface" | "type";
5
5
  preserveFiles: boolean;
6
+ generateEntityMapType: boolean;
7
+ addIdentifierToEntities: boolean;
6
8
  };
7
9
  export declare const render: (options: Partial<TypeScriptRendererOptions> | undefined, declarations: readonly Decl[]) => string;
@@ -13,11 +13,13 @@ import { isObjectType } from "../../schema/types/generic/ObjectType.js";
13
13
  import { isNestedEntityMapType } from "../../schema/types/references/NestedEntityMapType.js";
14
14
  import { ReferenceIdentifierType } from "../../schema/types/references/ReferenceIdentifierType.js";
15
15
  import { ensureSpecialDirStart } from "../../utils/path.js";
16
- import { combineSyntaxes, indent, prefixLines, syntax } from "../../utils/render.js";
16
+ import { combineSyntaxes, emptyRenderResult, indent, prefixLines, syntax, } from "../../utils/render.js";
17
17
  const defaultOptions = {
18
18
  indentation: 2,
19
19
  objectTypeKeyword: "interface",
20
20
  preserveFiles: false,
21
+ generateEntityMapType: false,
22
+ addIdentifierToEntities: false,
21
23
  };
22
24
  const renderDocumentation = (comment, isDeprecated) => syntax `${comment === undefined
23
25
  ? ""
@@ -91,7 +93,7 @@ const renderType = (options, type) => {
91
93
  return assertExhaustive(type, "Unknown type");
92
94
  }
93
95
  };
94
- const renderEntityDecl = (options, decl) => syntax `${renderDocumentation(decl.comment, decl.isDeprecated)}export ${options.objectTypeKeyword} ${decl.name} ${options.objectTypeKeyword === "type" ? "= " : ""}${renderType(options, addEphemeralUUIDToType(decl))}`;
96
+ const renderEntityDecl = (options, decl) => syntax `${renderDocumentation(decl.comment, decl.isDeprecated)}export ${options.objectTypeKeyword} ${decl.name} ${options.objectTypeKeyword === "type" ? "= " : ""}${renderType(options, options.addIdentifierToEntities ? addEphemeralUUIDToType(decl) : decl.type.value)}`;
95
97
  const renderEnumDecl = (options, decl) => syntax `${renderDocumentation(decl.comment, decl.isDeprecated)}export type ${decl.name}${renderTypeParameters(options, decl.parameters)} =${renderEnumType(options, decl.type.value)}`;
96
98
  const renderTypeAliasDecl = (options, decl) => isObjectType(decl.type.value)
97
99
  ? syntax `${renderDocumentation(decl.comment, decl.isDeprecated)}export ${options.objectTypeKeyword} ${decl.name}${renderTypeParameters(options, decl.parameters)} ${options.objectTypeKeyword === "type" ? "= " : ""}${renderType(options, decl.type.value)}`
@@ -123,8 +125,15 @@ const renderImports = (currentUrl, imports) => {
123
125
  .join(EOL);
124
126
  return importsSyntax.length > 0 ? importsSyntax + EOL + EOL : "";
125
127
  };
128
+ const renderEntityMapType = (options, declarations) => syntax `export type EntityMap = {${EOL}${indent(options.indentation, 1, combineSyntaxes(declarations
129
+ .filter(isEntityDecl)
130
+ .sort((a, b) => a.name.localeCompare(b.name))
131
+ .map(decl => syntax `${decl.name}: ${decl.name}`), EOL))}${EOL}}${EOL + EOL}`;
126
132
  export const render = (options = defaultOptions, declarations) => {
127
133
  const finalOptions = { ...defaultOptions, ...options };
134
+ const [_, entityMap] = finalOptions.generateEntityMapType
135
+ ? renderEntityMapType(finalOptions, declarations)
136
+ : emptyRenderResult;
128
137
  const [imports, content] = renderDeclarations(finalOptions, flatMapAuxiliaryDecls((parentNodes, node) => {
129
138
  if (isNestedEntityMapType(node)) {
130
139
  return TypeAliasDecl(asDecl(parentNodes[0])?.sourceUrl ?? "", {
@@ -138,8 +147,9 @@ export const render = (options = defaultOptions, declarations) => {
138
147
  }
139
148
  return undefined;
140
149
  }, declarations));
141
- return finalOptions.preserveFiles
142
- ? (declarations[0] === undefined ? "" : renderImports(declarations[0].sourceUrl, imports)) +
143
- content
144
- : content;
150
+ return (entityMap +
151
+ (finalOptions.preserveFiles
152
+ ? (declarations[0] === undefined ? "" : renderImports(declarations[0].sourceUrl, imports)) +
153
+ content
154
+ : content));
145
155
  };
@@ -4,6 +4,7 @@ export declare const joinSyntax: (...syntaxes: (string | undefined)[]) => string
4
4
  export type RenderResult = [imports: {
5
5
  [sourcePath: string]: string[];
6
6
  }, syntax: string];
7
+ export declare const emptyRenderResult: RenderResult;
7
8
  export declare const combineSyntaxes: (syntaxes: (RenderResult | string | undefined)[], separator?: string) => RenderResult;
8
9
  export declare const syntax: (strings: TemplateStringsArray, ...values: (RenderResult | string | undefined)[]) => RenderResult;
9
10
  export declare const indent: (spaces: number, indentLevel: number, text: RenderResult) => RenderResult;
@@ -6,7 +6,7 @@ export const prefixLines = (prefix, text, includeEmptyLines = false) => text
6
6
  .join(EOL);
7
7
  export const applyIndentation = (indentLevel, text, spaces) => prefixLines(" ".repeat(spaces * indentLevel), text);
8
8
  export const joinSyntax = (...syntaxes) => syntaxes.filter(syntax => syntax !== undefined).join("");
9
- const emptyRenderResult = [{}, ""];
9
+ export const emptyRenderResult = [{}, ""];
10
10
  export const combineSyntaxes = (syntaxes, separator = "") => syntaxes
11
11
  .filter(syntax => syntax !== undefined)
12
12
  .reduce((acc, value, i) => typeof value === "string"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsondb",
3
- "version": "0.11.4",
3
+ "version": "0.12.0",
4
4
  "description": "",
5
5
  "license": "ISC",
6
6
  "author": "Lukas Obermann",