tsondb 0.2.0 → 0.4.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/LICENSE +385 -0
- package/README.md +173 -1
- package/lib/bin/tsondb.d.ts +8 -0
- package/lib/bin/tsondb.js +83 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +1 -1
- package/lib/{Schema.d.ts → node/Schema.d.ts} +2 -2
- package/lib/{Schema.js → node/Schema.js} +1 -1
- package/lib/node/index.d.ts +8 -0
- package/lib/node/index.js +62 -0
- package/lib/{renderers → node/renderers}/Output.d.ts +1 -1
- package/lib/node/renderers/jsonschema/index.d.ts +6 -0
- package/lib/node/renderers/jsonschema/index.js +35 -0
- package/lib/node/renderers/jsonschema/render.d.ts +9 -0
- package/lib/{renderers → node/renderers}/jsonschema/render.js +42 -23
- package/lib/node/renderers/ts/index.d.ts +6 -0
- package/lib/node/renderers/ts/index.js +42 -0
- package/lib/{renderers → node/renderers}/ts/render.d.ts +3 -1
- package/lib/node/renderers/ts/render.js +136 -0
- package/lib/{schema → node/schema}/Node.d.ts +5 -4
- package/lib/{schema → node/schema}/Node.js +11 -6
- package/lib/{schema/parameters → node/schema}/TypeParameter.d.ts +3 -2
- package/lib/{schema/parameters → node/schema}/TypeParameter.js +2 -2
- package/lib/{schema → node/schema}/declarations/Declaration.d.ts +11 -10
- package/lib/{schema → node/schema}/declarations/Declaration.js +10 -4
- package/lib/{schema → node/schema}/declarations/EntityDecl.d.ts +13 -9
- package/lib/{schema → node/schema}/declarations/EntityDecl.js +2 -2
- package/lib/{schema → node/schema}/declarations/EnumDecl.d.ts +11 -26
- package/lib/node/schema/declarations/EnumDecl.js +52 -0
- package/lib/{schema → node/schema}/declarations/TypeAliasDecl.d.ts +8 -7
- package/lib/{schema → node/schema}/declarations/TypeAliasDecl.js +3 -3
- package/lib/{schema → node/schema}/index.d.ts +4 -3
- package/lib/{schema → node/schema}/index.js +3 -3
- package/lib/node/schema/types/Type.d.ts +48 -0
- package/lib/{schema → node/schema}/types/Type.js +90 -28
- package/lib/{schema → node/schema}/types/generic/ArrayType.d.ts +6 -4
- package/lib/{schema → node/schema}/types/generic/ArrayType.js +5 -4
- package/lib/node/schema/types/generic/EnumType.d.ts +39 -0
- package/lib/node/schema/types/generic/EnumType.js +98 -0
- package/lib/{schema → node/schema}/types/generic/ObjectType.d.ts +9 -7
- package/lib/{schema → node/schema}/types/generic/ObjectType.js +11 -3
- package/lib/{schema → node/schema}/types/primitives/BooleanType.d.ts +5 -3
- package/lib/{schema → node/schema}/types/primitives/BooleanType.js +1 -0
- package/lib/{schema → node/schema}/types/primitives/DateType.d.ts +6 -4
- package/lib/{schema → node/schema}/types/primitives/DateType.js +2 -1
- package/lib/{schema → node/schema}/types/primitives/FloatType.d.ts +6 -4
- package/lib/{schema → node/schema}/types/primitives/FloatType.js +2 -1
- package/lib/{schema → node/schema}/types/primitives/IntegerType.d.ts +6 -4
- package/lib/{schema → node/schema}/types/primitives/IntegerType.js +2 -1
- package/lib/{schema → node/schema}/types/primitives/NumericType.d.ts +2 -2
- package/lib/node/schema/types/primitives/PrimitiveType.d.ts +6 -0
- package/lib/{schema → node/schema}/types/primitives/StringType.d.ts +6 -4
- package/lib/{schema → node/schema}/types/primitives/StringType.js +2 -1
- package/lib/{schema → node/schema}/types/references/IncludeIdentifierType.d.ts +11 -8
- package/lib/{schema → node/schema}/types/references/IncludeIdentifierType.js +15 -2
- package/lib/{schema → node/schema}/types/references/NestedEntityMapType.d.ts +11 -9
- package/lib/{schema → node/schema}/types/references/NestedEntityMapType.js +6 -2
- package/lib/{schema → node/schema}/types/references/ReferenceIdentifierType.d.ts +8 -6
- package/lib/{schema → node/schema}/types/references/ReferenceIdentifierType.js +1 -0
- package/lib/node/schema/types/references/TypeArgumentType.d.ts +23 -0
- package/lib/node/schema/types/references/TypeArgumentType.js +19 -0
- package/lib/node/schema/validation/type.d.ts +4 -0
- package/lib/node/server/api/declarations.d.ts +1 -0
- package/lib/node/server/api/declarations.js +154 -0
- package/lib/node/server/api/git.d.ts +1 -0
- package/lib/node/server/api/git.js +178 -0
- package/lib/node/server/api/index.d.ts +1 -0
- package/lib/node/server/api/index.js +8 -0
- package/lib/node/server/api/instanceOperations.d.ts +6 -0
- package/lib/node/server/api/instanceOperations.js +91 -0
- package/lib/node/server/api/instances.d.ts +1 -0
- package/lib/node/server/api/instances.js +27 -0
- package/lib/node/server/index.d.ts +28 -0
- package/lib/node/server/index.js +53 -0
- package/lib/node/server/init.d.ts +5 -0
- package/lib/node/server/init.js +56 -0
- package/lib/{utils → node/utils}/error.js +4 -1
- package/lib/node/utils/git.d.ts +3 -0
- package/lib/node/utils/git.js +12 -0
- package/lib/node/utils/instances.d.ts +5 -0
- package/lib/node/utils/instances.js +21 -0
- package/lib/node/utils/path.d.ts +1 -0
- package/lib/node/utils/path.js +2 -0
- package/lib/node/utils/references.d.ts +7 -0
- package/lib/node/utils/references.js +40 -0
- package/lib/node/utils/render.d.ts +9 -0
- package/lib/node/utils/render.js +34 -0
- package/lib/shared/api.d.ts +19 -2
- package/lib/shared/utils/array.d.ts +19 -0
- package/lib/shared/utils/array.js +30 -0
- package/lib/shared/utils/displayName.d.ts +1 -1
- package/lib/shared/utils/displayName.js +4 -2
- package/lib/shared/utils/git.d.ts +12 -0
- package/lib/shared/utils/git.js +98 -0
- package/lib/shared/utils/instances.d.ts +10 -0
- package/lib/shared/utils/instances.js +8 -1
- package/lib/{utils → shared/utils}/lazy.js +1 -1
- package/lib/shared/utils/markdown.d.ts +14 -0
- package/lib/shared/utils/markdown.js +42 -0
- package/lib/shared/utils/object.d.ts +4 -0
- package/lib/shared/utils/object.js +5 -0
- package/lib/{utils → shared/utils}/result.js +2 -2
- package/lib/shared/utils/string.d.ts +1 -0
- package/lib/shared/utils/string.js +16 -4
- package/lib/shared/utils/validation.js +3 -2
- package/lib/shared/validation/number.js +3 -3
- package/lib/shared/validation/object.js +4 -3
- package/lib/shared/validation/string.js +12 -7
- package/lib/web/api.d.ts +24 -0
- package/lib/web/api.js +204 -0
- package/lib/web/components/Git.d.ts +2 -0
- package/lib/web/components/Git.js +160 -0
- package/lib/{client → web}/components/Layout.d.ts +1 -1
- package/lib/{client → web}/components/Layout.js +2 -1
- package/lib/web/components/Select.d.ts +3 -0
- package/lib/web/components/Select.js +5 -0
- package/lib/web/components/typeInputs/ArrayTypeInput.d.ts +13 -0
- package/lib/{client → web}/components/typeInputs/ArrayTypeInput.js +8 -1
- package/lib/{client → web}/components/typeInputs/BooleanTypeInput.d.ts +2 -2
- package/lib/{client → web}/components/typeInputs/DateTypeInput.d.ts +2 -2
- package/lib/web/components/typeInputs/EnumTypeInput.d.ts +13 -0
- package/lib/{client/components/typeInputs/utils/EnumDeclField.js → web/components/typeInputs/EnumTypeInput.js} +11 -11
- package/lib/{client → web}/components/typeInputs/FloatTypeInput.d.ts +2 -2
- package/lib/web/components/typeInputs/GenericTypeArgumentIdentifierTypeInput.d.ts +7 -0
- package/lib/{client → web}/components/typeInputs/GenericTypeArgumentIdentifierTypeInput.js +1 -1
- package/lib/web/components/typeInputs/IncludeIdentifierTypeInput.d.ts +13 -0
- package/lib/web/components/typeInputs/IncludeIdentifierTypeInput.js +9 -0
- package/lib/{client → web}/components/typeInputs/IntegerTypeInput.d.ts +2 -2
- package/lib/web/components/typeInputs/NestedEntityMapTypeInput.d.ts +13 -0
- package/lib/{client → web}/components/typeInputs/NestedEntityMapTypeInput.js +7 -2
- package/lib/{client → web}/components/typeInputs/ObjectTypeInput.d.ts +4 -4
- package/lib/{client → web}/components/typeInputs/ObjectTypeInput.js +4 -1
- package/lib/{client → web}/components/typeInputs/ReferenceIdentifierTypeInput.d.ts +3 -3
- package/lib/web/components/typeInputs/ReferenceIdentifierTypeInput.js +11 -0
- package/lib/{client → web}/components/typeInputs/StringTypeInput.d.ts +2 -2
- package/lib/web/components/typeInputs/TypeInput.d.ts +13 -0
- package/lib/{client → web}/components/typeInputs/TypeInput.js +6 -3
- package/lib/{client → web}/components/typeInputs/utils/Markdown.d.ts +1 -1
- package/lib/web/components/typeInputs/utils/Markdown.js +26 -0
- package/lib/{client → web}/components/typeInputs/utils/MismatchingTypeError.d.ts +1 -1
- package/lib/{client → web}/components/typeInputs/utils/ValidationErrors.d.ts +1 -1
- package/lib/web/hooks/useAPIResource.d.ts +1 -0
- package/lib/web/hooks/useAPIResource.js +2 -0
- package/lib/{client → web}/hooks/useEntityFromRoute.d.ts +1 -1
- package/lib/{client → web}/hooks/useEntityFromRoute.js +1 -1
- package/lib/{client → web}/hooks/useInstanceNamesByEntity.d.ts +1 -1
- package/lib/{client → web}/hooks/useInstanceNamesByEntity.js +8 -6
- package/lib/web/hooks/useMappedAPIResource.d.ts +1 -0
- package/lib/web/hooks/useMappedAPIResource.js +17 -0
- package/lib/{client → web}/hooks/useSecondaryDeclarations.d.ts +1 -1
- package/lib/{client → web}/hooks/useSecondaryDeclarations.js +4 -2
- package/lib/{client → web}/routes/CreateInstance.d.ts +1 -1
- package/lib/{client → web}/routes/CreateInstance.js +4 -2
- package/lib/web/routes/Entity.d.ts +2 -0
- package/lib/web/routes/Entity.js +43 -0
- package/lib/web/routes/Home.d.ts +2 -0
- package/lib/web/routes/Home.js +9 -0
- package/lib/{client → web}/routes/Instance.d.ts +1 -1
- package/lib/{client → web}/routes/Instance.js +14 -8
- package/lib/{client → web}/routes/NotFound.d.ts +1 -1
- package/lib/web/utils/typeSkeleton.d.ts +3 -0
- package/lib/{client → web}/utils/typeSkeleton.js +14 -17
- package/package.json +35 -15
- package/public/css/styles.css +200 -1
- package/lib/ModelContainer.d.ts +0 -17
- package/lib/ModelContainer.js +0 -63
- package/lib/client/api.d.ts +0 -11
- package/lib/client/api.js +0 -83
- package/lib/client/components/Select.d.ts +0 -3
- package/lib/client/components/Select.js +0 -2
- package/lib/client/components/typeInputs/ArrayTypeInput.d.ts +0 -13
- package/lib/client/components/typeInputs/GenericTypeArgumentIdentifierTypeInput.d.ts +0 -7
- package/lib/client/components/typeInputs/IncludeIdentifierTypeInput.d.ts +0 -13
- package/lib/client/components/typeInputs/IncludeIdentifierTypeInput.js +0 -18
- package/lib/client/components/typeInputs/NestedEntityMapTypeInput.d.ts +0 -13
- package/lib/client/components/typeInputs/ReferenceIdentifierTypeInput.js +0 -9
- package/lib/client/components/typeInputs/TypeInput.d.ts +0 -13
- package/lib/client/components/typeInputs/utils/EnumDeclField.d.ts +0 -13
- package/lib/client/components/typeInputs/utils/Markdown.js +0 -57
- package/lib/client/routes/Entity.d.ts +0 -2
- package/lib/client/routes/Entity.js +0 -47
- package/lib/client/routes/Home.d.ts +0 -2
- package/lib/client/routes/Home.js +0 -18
- package/lib/client/utils/typeSkeleton.d.ts +0 -3
- package/lib/renderers/jsonschema/index.d.ts +0 -6
- package/lib/renderers/jsonschema/index.js +0 -12
- package/lib/renderers/jsonschema/render.d.ts +0 -5
- package/lib/renderers/ts/index.d.ts +0 -6
- package/lib/renderers/ts/index.js +0 -11
- package/lib/renderers/ts/render.js +0 -112
- package/lib/schema/declarations/EnumDecl.js +0 -115
- package/lib/schema/types/Type.d.ts +0 -42
- package/lib/schema/types/primitives/PrimitiveType.d.ts +0 -6
- package/lib/schema/types/references/GenericArgumentIdentifierType.d.ts +0 -21
- package/lib/schema/types/references/GenericArgumentIdentifierType.js +0 -18
- package/lib/schema/validation/type.d.ts +0 -4
- package/lib/server/index.d.ts +0 -8
- package/lib/server/index.js +0 -207
- package/lib/server/instanceOperations.d.ts +0 -7
- package/lib/server/instanceOperations.js +0 -67
- package/lib/tsconfig.tsbuildinfo +0 -1
- package/lib/utils/instances.d.ts +0 -4
- package/lib/utils/instances.js +0 -12
- package/lib/utils/object.d.ts +0 -3
- package/lib/utils/object.js +0 -1
- package/lib/utils/render.d.ts +0 -4
- package/lib/utils/render.js +0 -8
- /package/lib/{renderers → node/renderers}/Output.js +0 -0
- /package/lib/{schema → node/schema}/types/primitives/NumericType.js +0 -0
- /package/lib/{schema → node/schema}/types/primitives/PrimitiveType.js +0 -0
- /package/lib/{schema → node/schema}/validation/options.d.ts +0 -0
- /package/lib/{schema → node/schema}/validation/options.js +0 -0
- /package/lib/{schema → node/schema}/validation/type.js +0 -0
- /package/lib/{utils → node/utils}/error.d.ts +0 -0
- /package/lib/{utils → shared/utils}/enum.d.ts +0 -0
- /package/lib/{utils → shared/utils}/enum.js +0 -0
- /package/lib/{utils → shared/utils}/lazy.d.ts +0 -0
- /package/lib/{utils → shared/utils}/result.d.ts +0 -0
- /package/lib/{client → web}/components/typeInputs/BooleanTypeInput.js +0 -0
- /package/lib/{client → web}/components/typeInputs/DateTypeInput.js +0 -0
- /package/lib/{client → web}/components/typeInputs/FloatTypeInput.js +0 -0
- /package/lib/{client → web}/components/typeInputs/IntegerTypeInput.js +0 -0
- /package/lib/{client → web}/components/typeInputs/StringTypeInput.js +0 -0
- /package/lib/{client → web}/components/typeInputs/utils/MismatchingTypeError.js +0 -0
- /package/lib/{client → web}/components/typeInputs/utils/ValidationErrors.js +0 -0
- /package/lib/{client → web}/index.d.ts +0 -0
- /package/lib/{client → web}/index.js +0 -0
- /package/lib/{client → web}/routes/NotFound.js +0 -0
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { mkdir, writeFile } from "fs/promises";
|
|
2
|
-
import { dirname } from "path";
|
|
3
|
-
import { resolveTypeArgumentsInDecls } from "../../schema/index.js";
|
|
4
|
-
import { render } from "./render.js";
|
|
5
|
-
export const JsonSchemaOutput = (options) => ({
|
|
6
|
-
run: async (schema) => {
|
|
7
|
-
await mkdir(dirname(options.targetPath), { recursive: true });
|
|
8
|
-
await writeFile(options.targetPath, render(options.rendererOptions, resolveTypeArgumentsInDecls(schema.declarations)), {
|
|
9
|
-
encoding: "utf-8",
|
|
10
|
-
});
|
|
11
|
-
},
|
|
12
|
-
});
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { mkdir, writeFile } from "fs/promises";
|
|
2
|
-
import { dirname } from "path";
|
|
3
|
-
import { render } from "./render.js";
|
|
4
|
-
export const TypeScriptOutput = (options) => ({
|
|
5
|
-
run: async (schema) => {
|
|
6
|
-
await mkdir(dirname(options.targetPath), { recursive: true });
|
|
7
|
-
await writeFile(options.targetPath, render(options.rendererOptions, schema.declarations), {
|
|
8
|
-
encoding: "utf-8",
|
|
9
|
-
});
|
|
10
|
-
},
|
|
11
|
-
});
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { EOL } from "node:os";
|
|
2
|
-
import { addEphemeralUUIDToType, createEntityIdentifierTypeAsDecl, isEntityDecl, } from "../../schema/declarations/EntityDecl.js";
|
|
3
|
-
import { TypeAliasDecl } from "../../schema/declarations/TypeAliasDecl.js";
|
|
4
|
-
import { flatMapAuxiliaryDecls, NodeKind } from "../../schema/Node.js";
|
|
5
|
-
import { isObjectType } from "../../schema/types/generic/ObjectType.js";
|
|
6
|
-
import { isNestedEntityMapType, } from "../../schema/types/references/NestedEntityMapType.js";
|
|
7
|
-
import { getParentDecl } from "../../schema/types/Type.js";
|
|
8
|
-
import { discriminatorKey } from "../../shared/enum.js";
|
|
9
|
-
import { toCamelCase } from "../../shared/utils/string.js";
|
|
10
|
-
import { assertExhaustive } from "../../shared/utils/typeSafety.js";
|
|
11
|
-
import { applyIndentation, joinSyntax, prefixLines, syntax } from "../../utils/render.js";
|
|
12
|
-
const defaultOptions = {
|
|
13
|
-
indentation: 2,
|
|
14
|
-
};
|
|
15
|
-
const renderDocumentation = (comment, isDeprecated) => comment === undefined
|
|
16
|
-
? ""
|
|
17
|
-
: syntax `/**
|
|
18
|
-
${prefixLines(" * ", comment, true)}${isDeprecated
|
|
19
|
-
? syntax `
|
|
20
|
-
* @deprecated`
|
|
21
|
-
: ""}
|
|
22
|
-
*/
|
|
23
|
-
`;
|
|
24
|
-
const renderTypeParameters = (options, params) => params.length === 0
|
|
25
|
-
? ""
|
|
26
|
-
: `<${params
|
|
27
|
-
.map(param => param.constraint === undefined
|
|
28
|
-
? param.name
|
|
29
|
-
: joinSyntax(param.name, " extends ", renderType(options, param.constraint)))
|
|
30
|
-
.join(", ")}>`;
|
|
31
|
-
const renderArrayType = (options, type) => `${renderType(options, type.items)}[]`;
|
|
32
|
-
const wrapAsObject = (options, syntax) => joinSyntax("{", EOL, applyIndentation(1, syntax, options.indentation), EOL, "}");
|
|
33
|
-
const renderObjectType = (options, type) => {
|
|
34
|
-
return wrapAsObject(options, Object.entries(type.properties)
|
|
35
|
-
.map(([name, config]) => joinSyntax(renderDocumentation(config.comment, config.isDeprecated), name, config.isRequired ? "" : "?", ": ", renderType(options, config.type)))
|
|
36
|
-
.join(Object.values(type.properties).some(prop => prop.comment !== undefined) ? EOL + EOL : EOL));
|
|
37
|
-
};
|
|
38
|
-
const renderBooleanType = (_options, _type) => "boolean";
|
|
39
|
-
const renderDateType = (_options, _type) => "Date";
|
|
40
|
-
const renderNumericType = (_options, _type) => "number";
|
|
41
|
-
const renderStringType = (_options, _type) => "string";
|
|
42
|
-
const renderGenericArgumentIdentifierType = (_options, type) => type.argument.name;
|
|
43
|
-
const renderReferenceIdentifierType = (_options, type) => type.entity.name + "_ID";
|
|
44
|
-
const renderIncludeIdentifierType = (_options, type) => type.reference.name;
|
|
45
|
-
const renderNestedEntityMapType = (options, type) => wrapAsObject(options, syntax `[${toCamelCase(type.secondaryEntity.name)}Id: string]: ${type.name}`);
|
|
46
|
-
const renderType = (options, type) => {
|
|
47
|
-
switch (type.kind) {
|
|
48
|
-
case NodeKind.ArrayType:
|
|
49
|
-
return renderArrayType(options, type);
|
|
50
|
-
case NodeKind.ObjectType:
|
|
51
|
-
return renderObjectType(options, type);
|
|
52
|
-
case NodeKind.BooleanType:
|
|
53
|
-
return renderBooleanType(options, type);
|
|
54
|
-
case NodeKind.DateType:
|
|
55
|
-
return renderDateType(options, type);
|
|
56
|
-
case NodeKind.FloatType:
|
|
57
|
-
return renderNumericType(options, type);
|
|
58
|
-
case NodeKind.IntegerType:
|
|
59
|
-
return renderNumericType(options, type);
|
|
60
|
-
case NodeKind.StringType:
|
|
61
|
-
return renderStringType(options, type);
|
|
62
|
-
case NodeKind.GenericArgumentIdentifierType:
|
|
63
|
-
return renderGenericArgumentIdentifierType(options, type);
|
|
64
|
-
case NodeKind.ReferenceIdentifierType:
|
|
65
|
-
return renderReferenceIdentifierType(options, type);
|
|
66
|
-
case NodeKind.IncludeIdentifierType:
|
|
67
|
-
return renderIncludeIdentifierType(options, type);
|
|
68
|
-
case NodeKind.NestedEntityMapType:
|
|
69
|
-
return renderNestedEntityMapType(options, type);
|
|
70
|
-
default:
|
|
71
|
-
return assertExhaustive(type, "Unknown type");
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
const renderEntityDecl = (options, decl) => joinSyntax(renderDocumentation(decl.comment, decl.isDeprecated), "export interface ", decl.name, " ", renderType(options, addEphemeralUUIDToType(decl)));
|
|
75
|
-
const renderEnumDecl = (options, decl) => joinSyntax(renderDocumentation(decl.comment, decl.isDeprecated), "export type ", decl.name, renderTypeParameters(options, decl.parameters), " =", ...Object.entries(decl.values.value).map(([caseName, caseDef]) => applyIndentation(1, joinSyntax(EOL, "| {", EOL, applyIndentation(1, joinSyntax(`${discriminatorKey}: "${caseName}"`, caseDef.type === null
|
|
76
|
-
? ""
|
|
77
|
-
: joinSyntax(EOL, caseName + ": ", renderType(options, caseDef.type))), options.indentation), EOL, "}"), options.indentation)));
|
|
78
|
-
const renderTypeAliasDecl = (options, decl) => {
|
|
79
|
-
const type = decl.type.value;
|
|
80
|
-
return isObjectType(type)
|
|
81
|
-
? joinSyntax(renderDocumentation(decl.comment, decl.isDeprecated), "export interface ", decl.name, renderTypeParameters(options, decl.parameters), " ", renderType(options, type))
|
|
82
|
-
: joinSyntax(renderDocumentation(decl.comment), "export type ", decl.name, renderTypeParameters(options, decl.parameters), " = ", renderType(options, type));
|
|
83
|
-
};
|
|
84
|
-
const renderDecl = (options, decl) => {
|
|
85
|
-
switch (decl.kind) {
|
|
86
|
-
case NodeKind.EntityDecl:
|
|
87
|
-
return renderEntityDecl(options, decl);
|
|
88
|
-
case NodeKind.EnumDecl:
|
|
89
|
-
return renderEnumDecl(options, decl);
|
|
90
|
-
case NodeKind.TypeAliasDecl:
|
|
91
|
-
return renderTypeAliasDecl(options, decl);
|
|
92
|
-
default:
|
|
93
|
-
return assertExhaustive(decl, "Unknown declaration");
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
const renderDeclarations = (options, declarations) => declarations.map(decl => renderDecl(options, decl)).join(EOL + EOL);
|
|
97
|
-
export const render = (options = defaultOptions, declarations) => {
|
|
98
|
-
const finalOptions = { ...defaultOptions, ...options };
|
|
99
|
-
return renderDeclarations(finalOptions, flatMapAuxiliaryDecls(node => {
|
|
100
|
-
if (isNestedEntityMapType(node)) {
|
|
101
|
-
return TypeAliasDecl(getParentDecl(node)?.sourceUrl ?? "", {
|
|
102
|
-
name: node.name,
|
|
103
|
-
comment: node.comment,
|
|
104
|
-
type: () => node.type.value,
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
else if (isEntityDecl(node)) {
|
|
108
|
-
return createEntityIdentifierTypeAsDecl(node);
|
|
109
|
-
}
|
|
110
|
-
return undefined;
|
|
111
|
-
}, declarations));
|
|
112
|
-
};
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { discriminatorKey } from "../../shared/enum.js";
|
|
2
|
-
import { Lazy } from "../../utils/lazy.js";
|
|
3
|
-
import { NodeKind } from "../Node.js";
|
|
4
|
-
import { serializeTypeParameter, } from "../parameters/TypeParameter.js";
|
|
5
|
-
import { getReferencesForType, resolveTypeArgumentsInType, serializeType, validate, } from "../types/Type.js";
|
|
6
|
-
import { getNestedDeclarations, getTypeArgumentsRecord, validateDeclName, } from "./Declaration.js";
|
|
7
|
-
export const GenEnumDecl = (sourceUrl, options) => {
|
|
8
|
-
validateDeclName(options.name);
|
|
9
|
-
const decl = {
|
|
10
|
-
...options,
|
|
11
|
-
kind: NodeKind.EnumDecl,
|
|
12
|
-
sourceUrl,
|
|
13
|
-
values: Lazy.of(() => {
|
|
14
|
-
const type = options.values(...options.parameters);
|
|
15
|
-
Object.values(type).forEach(type => {
|
|
16
|
-
if (type.type) {
|
|
17
|
-
type.type.parent = decl;
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
return type;
|
|
21
|
-
}),
|
|
22
|
-
};
|
|
23
|
-
return decl;
|
|
24
|
-
};
|
|
25
|
-
export { GenEnumDecl as GenEnum };
|
|
26
|
-
export const EnumDecl = (sourceUrl, options) => {
|
|
27
|
-
validateDeclName(options.name);
|
|
28
|
-
const decl = {
|
|
29
|
-
...options,
|
|
30
|
-
kind: NodeKind.EnumDecl,
|
|
31
|
-
sourceUrl,
|
|
32
|
-
parameters: [],
|
|
33
|
-
values: Lazy.of(() => {
|
|
34
|
-
const type = options.values();
|
|
35
|
-
Object.values(type).forEach(type => {
|
|
36
|
-
if (type.type) {
|
|
37
|
-
type.type.parent = decl;
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
return type;
|
|
41
|
-
}),
|
|
42
|
-
};
|
|
43
|
-
return decl;
|
|
44
|
-
};
|
|
45
|
-
export { EnumDecl as Enum };
|
|
46
|
-
export const isEnumDecl = (node) => node.kind === NodeKind.EnumDecl;
|
|
47
|
-
export const getNestedDeclarationsInEnumDecl = (addedDecls, decl) => Object.values(decl.values.value).reduce((acc, caseMember) => caseMember.type === null ? acc : getNestedDeclarations(acc, caseMember.type), addedDecls);
|
|
48
|
-
export const validateEnumDecl = (helpers, decl, args, value) => {
|
|
49
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
50
|
-
return [TypeError(`expected an object, but got ${JSON.stringify(value)}`)];
|
|
51
|
-
}
|
|
52
|
-
const actualKeys = Object.keys(value);
|
|
53
|
-
if (!(discriminatorKey in value) || typeof value[discriminatorKey] !== "string") {
|
|
54
|
-
return [
|
|
55
|
-
TypeError(`missing required discriminator value at key "${discriminatorKey}" of type string`),
|
|
56
|
-
];
|
|
57
|
-
}
|
|
58
|
-
const caseName = value[discriminatorKey];
|
|
59
|
-
if (!(caseName in decl.values.value)) {
|
|
60
|
-
return [TypeError(`discriminator "${caseName}" is not a valid enum case`)];
|
|
61
|
-
}
|
|
62
|
-
const unknownKeyErrors = actualKeys.flatMap(actualKey => actualKey === discriminatorKey || actualKey in decl.values.value
|
|
63
|
-
? []
|
|
64
|
-
: [TypeError(`key "${actualKey}" is not the discriminator key or a valid enum case`)]);
|
|
65
|
-
if (unknownKeyErrors.length > 0) {
|
|
66
|
-
return unknownKeyErrors;
|
|
67
|
-
}
|
|
68
|
-
const associatedType = decl.values.value[caseName]?.type;
|
|
69
|
-
if (associatedType != null) {
|
|
70
|
-
if (!(caseName in value)) {
|
|
71
|
-
return [TypeError(`missing required associated value for case "${caseName}"`)];
|
|
72
|
-
}
|
|
73
|
-
return validate(helpers, resolveTypeArgumentsInType(getTypeArgumentsRecord(decl, args), associatedType), value[caseName]);
|
|
74
|
-
}
|
|
75
|
-
return [];
|
|
76
|
-
};
|
|
77
|
-
export const resolveTypeArgumentsInEnumDecl = (decl, args) => {
|
|
78
|
-
const resolvedArgs = getTypeArgumentsRecord(decl, args);
|
|
79
|
-
return EnumDecl(decl.sourceUrl, {
|
|
80
|
-
...decl,
|
|
81
|
-
values: () => Object.fromEntries(Object.entries(decl.values.value).map(([key, { type, ...caseMember }]) => [
|
|
82
|
-
key,
|
|
83
|
-
{
|
|
84
|
-
...caseMember,
|
|
85
|
-
type: type === null ? null : resolveTypeArgumentsInType(resolvedArgs, type),
|
|
86
|
-
},
|
|
87
|
-
])),
|
|
88
|
-
});
|
|
89
|
-
};
|
|
90
|
-
export const EnumCaseDecl = (options) => ({
|
|
91
|
-
...options,
|
|
92
|
-
kind: NodeKind.EnumCaseDecl,
|
|
93
|
-
});
|
|
94
|
-
export { EnumCaseDecl as EnumCase };
|
|
95
|
-
export const serializeEnumDecl = type => ({
|
|
96
|
-
...type,
|
|
97
|
-
values: Object.fromEntries(Object.entries(type.values.value).map(([key, caseMember]) => [
|
|
98
|
-
key,
|
|
99
|
-
{
|
|
100
|
-
...caseMember,
|
|
101
|
-
type: caseMember.type === null ? null : serializeType(caseMember.type),
|
|
102
|
-
},
|
|
103
|
-
])),
|
|
104
|
-
parameters: type.parameters.map(param => serializeTypeParameter(param)),
|
|
105
|
-
});
|
|
106
|
-
export const getReferencesForEnumDecl = (decl, value) => typeof value === "object" &&
|
|
107
|
-
value !== null &&
|
|
108
|
-
!Array.isArray(value) &&
|
|
109
|
-
discriminatorKey in value &&
|
|
110
|
-
typeof value[discriminatorKey] === "string" &&
|
|
111
|
-
value[discriminatorKey] in decl.values.value &&
|
|
112
|
-
decl.values.value[value[discriminatorKey]]?.type == null &&
|
|
113
|
-
value[discriminatorKey] in value
|
|
114
|
-
? getReferencesForType(decl.values.value[value[discriminatorKey]].type, value[value[discriminatorKey]])
|
|
115
|
-
: [];
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { Decl } from "../declarations/Declaration.js";
|
|
2
|
-
import { BaseNode, GetReferences, Serializer } from "../Node.js";
|
|
3
|
-
import { Validator } from "../validation/type.js";
|
|
4
|
-
import { ArrayType, SerializedArrayType } from "./generic/ArrayType.js";
|
|
5
|
-
import { MemberDecl, ObjectType, SerializedMemberDecl, SerializedObjectType } from "./generic/ObjectType.js";
|
|
6
|
-
import { BooleanType, SerializedBooleanType } from "./primitives/BooleanType.js";
|
|
7
|
-
import { DateType, SerializedDateType } from "./primitives/DateType.js";
|
|
8
|
-
import { FloatType, SerializedFloatType } from "./primitives/FloatType.js";
|
|
9
|
-
import { IntegerType, SerializedIntegerType } from "./primitives/IntegerType.js";
|
|
10
|
-
import { PrimitiveType, SerializedPrimitiveType } from "./primitives/PrimitiveType.js";
|
|
11
|
-
import { SerializedStringType, StringType } from "./primitives/StringType.js";
|
|
12
|
-
import { GenericArgumentIdentifierType, SerializedGenericArgumentIdentifierType } from "./references/GenericArgumentIdentifierType.js";
|
|
13
|
-
import { IncludeIdentifierType, SerializedIncludeIdentifierType } from "./references/IncludeIdentifierType.js";
|
|
14
|
-
import { NestedEntityMapType, SerializedNestedEntityMapType } from "./references/NestedEntityMapType.js";
|
|
15
|
-
import { ReferenceIdentifierType, SerializedReferenceIdentifierType } from "./references/ReferenceIdentifierType.js";
|
|
16
|
-
export interface BaseType extends BaseNode {
|
|
17
|
-
/**
|
|
18
|
-
* The parent node of this type will be set when the type is used in a declaration or nested in another type.
|
|
19
|
-
*/
|
|
20
|
-
parent?: Type | Decl;
|
|
21
|
-
}
|
|
22
|
-
export interface SerializedBaseType extends BaseNode {
|
|
23
|
-
}
|
|
24
|
-
export type Type = PrimitiveType | ArrayType | ObjectType | GenericArgumentIdentifierType | ReferenceIdentifierType | IncludeIdentifierType | NestedEntityMapType;
|
|
25
|
-
export type SerializedType = SerializedPrimitiveType | SerializedArrayType | SerializedObjectType | SerializedGenericArgumentIdentifierType | SerializedReferenceIdentifierType | SerializedIncludeIdentifierType | SerializedNestedEntityMapType;
|
|
26
|
-
export declare const validate: Validator<Type>;
|
|
27
|
-
export declare const resolveTypeArgumentsInType: <Args extends Record<string, Type>>(args: Args, type: Type) => Type;
|
|
28
|
-
export declare function walkTypeNodeTree(callbackFn: (type: Type) => void, type: Type): void;
|
|
29
|
-
export type AsType<T extends Type> = T extends ArrayType<infer I> ? AsType<I>[] : T extends ObjectType<infer P> ? {
|
|
30
|
-
[K in keyof P]: P[K] extends MemberDecl<Type, true> ? AsType<P[K]["type"]> : AsType<P[K]["type"]> | undefined;
|
|
31
|
-
} : T extends BooleanType ? boolean : T extends DateType ? Date : T extends FloatType ? number : T extends IntegerType ? number : T extends StringType ? string : T extends GenericArgumentIdentifierType ? unknown : T extends IncludeIdentifierType ? unknown : T extends NestedEntityMapType ? unknown : T extends ReferenceIdentifierType ? unknown : never;
|
|
32
|
-
export type SerializedAsType<T extends SerializedType> = T extends SerializedArrayType<infer I> ? SerializedAsType<I>[] : T extends SerializedObjectType<infer P> ? {
|
|
33
|
-
[K in keyof P]: P[K] extends SerializedMemberDecl<SerializedType, true> ? SerializedAsType<P[K]["type"]> : SerializedAsType<P[K]["type"]> | undefined;
|
|
34
|
-
} : T extends SerializedBooleanType ? boolean : T extends SerializedDateType ? Date : T extends SerializedFloatType ? number : T extends SerializedIntegerType ? number : T extends SerializedStringType ? string : T extends SerializedGenericArgumentIdentifierType ? unknown : T extends SerializedIncludeIdentifierType ? unknown : T extends SerializedNestedEntityMapType ? unknown : T extends SerializedReferenceIdentifierType ? unknown : never;
|
|
35
|
-
export type AsNode<T> = T extends (infer I)[] ? ArrayType<AsNode<I>> : T extends Record<string, any> ? ObjectType<{
|
|
36
|
-
[K in keyof T]: T[K] extends MemberDecl<Type, boolean> ? T[K] : T extends null | undefined ? MemberDecl<AsNode<NonNullable<T[K]>>, false> : MemberDecl<AsNode<T[K]>, true>;
|
|
37
|
-
}> : T extends string ? StringType : T extends number ? FloatType : T extends boolean ? BooleanType : never;
|
|
38
|
-
export declare const getParentDecl: (type: Type) => Decl | undefined;
|
|
39
|
-
export declare const findTypeAtPath: (type: Type, path: string[]) => Type | undefined;
|
|
40
|
-
export declare const serializeType: Serializer<Type, SerializedType>;
|
|
41
|
-
export declare const removeParentKey: <T extends BaseType>(type: T) => Omit<T, "parent">;
|
|
42
|
-
export declare const getReferencesForType: GetReferences<Type>;
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { BooleanType, SerializedBooleanType } from "./BooleanType.js";
|
|
2
|
-
import { DateType, SerializedDateType } from "./DateType.js";
|
|
3
|
-
import { NumericType, SerializedNumericType } from "./NumericType.js";
|
|
4
|
-
import { SerializedStringType, StringType } from "./StringType.js";
|
|
5
|
-
export type PrimitiveType = BooleanType | DateType | NumericType | StringType;
|
|
6
|
-
export type SerializedPrimitiveType = SerializedBooleanType | SerializedDateType | SerializedNumericType | SerializedStringType;
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { GetReferences, Node, NodeKind, Serializer } from "../../Node.js";
|
|
2
|
-
import { SerializedTypeParameter, TypeParameter } from "../../parameters/TypeParameter.js";
|
|
3
|
-
import { Validator } from "../../validation/type.js";
|
|
4
|
-
import { BaseType, SerializedBaseType, Type } from "../Type.js";
|
|
5
|
-
type TConstraint = TypeParameter;
|
|
6
|
-
export interface GenericArgumentIdentifierType<T extends TConstraint = TConstraint> extends BaseType {
|
|
7
|
-
kind: NodeKind["GenericArgumentIdentifierType"];
|
|
8
|
-
argument: T;
|
|
9
|
-
}
|
|
10
|
-
type TSerializedConstraint = SerializedTypeParameter;
|
|
11
|
-
export interface SerializedGenericArgumentIdentifierType<T extends TSerializedConstraint = TSerializedConstraint> extends SerializedBaseType {
|
|
12
|
-
kind: NodeKind["GenericArgumentIdentifierType"];
|
|
13
|
-
argument: T;
|
|
14
|
-
}
|
|
15
|
-
export declare const GenericArgumentIdentifierType: <T extends TConstraint>(argument: T) => GenericArgumentIdentifierType<T>;
|
|
16
|
-
export { GenericArgumentIdentifierType as GenericArgumentIdentifier };
|
|
17
|
-
export declare const isGenericArgumentIdentifierType: (node: Node) => node is GenericArgumentIdentifierType;
|
|
18
|
-
export declare const validateGenericArgumentIdentifierType: Validator<GenericArgumentIdentifierType>;
|
|
19
|
-
export declare const resolveTypeArgumentsInGenericArgumentIdentifierType: <T extends TConstraint, Args extends Record<string, Type>>(args: Args, type: GenericArgumentIdentifierType<T>) => Args[T["name"]];
|
|
20
|
-
export declare const serializeGenericArgumentIdentifierType: Serializer<GenericArgumentIdentifierType, SerializedGenericArgumentIdentifierType>;
|
|
21
|
-
export declare const getReferencesForGenericArgumentIdentifierType: GetReferences<GenericArgumentIdentifierType>;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { NodeKind } from "../../Node.js";
|
|
2
|
-
import { serializeTypeParameter, } from "../../parameters/TypeParameter.js";
|
|
3
|
-
import { removeParentKey } from "../Type.js";
|
|
4
|
-
export const GenericArgumentIdentifierType = (argument) => ({
|
|
5
|
-
kind: NodeKind.GenericArgumentIdentifierType,
|
|
6
|
-
argument,
|
|
7
|
-
});
|
|
8
|
-
export { GenericArgumentIdentifierType as GenericArgumentIdentifier };
|
|
9
|
-
export const isGenericArgumentIdentifierType = (node) => node.kind === NodeKind.GenericArgumentIdentifierType;
|
|
10
|
-
export const validateGenericArgumentIdentifierType = (_helpers, type, _value) => {
|
|
11
|
-
throw new TypeError(`generic argument "${type.argument.name}" has not been replaced with a concrete type`);
|
|
12
|
-
};
|
|
13
|
-
export const resolveTypeArgumentsInGenericArgumentIdentifierType = (args, type) => args[type.argument.name];
|
|
14
|
-
export const serializeGenericArgumentIdentifierType = type => ({
|
|
15
|
-
...removeParentKey(type),
|
|
16
|
-
argument: serializeTypeParameter(type.argument),
|
|
17
|
-
});
|
|
18
|
-
export const getReferencesForGenericArgumentIdentifierType = (_type, _value) => [];
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { Validators } from "../Node.js";
|
|
2
|
-
import { Type } from "../types/Type.js";
|
|
3
|
-
export type ValidatorHelpers = Validators;
|
|
4
|
-
export type Validator<T extends Type, Args extends any[] = []> = (helpers: ValidatorHelpers, type: T, value: unknown, ...args: Args) => Error[];
|
package/lib/server/index.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { ModelContainer } from "../ModelContainer.js";
|
|
2
|
-
import { InstancesByEntityName } from "../shared/utils/instances.js";
|
|
3
|
-
type ServerOptions = {
|
|
4
|
-
name: string;
|
|
5
|
-
port: number;
|
|
6
|
-
};
|
|
7
|
-
export declare const createServer: (modelContainer: ModelContainer, instancesByEntityName: InstancesByEntityName, options?: Partial<ServerOptions>) => void;
|
|
8
|
-
export {};
|
package/lib/server/index.js
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import express from "express";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { serializeDecl } from "../schema/declarations/Declaration.js";
|
|
4
|
-
import { isEntityDecl, serializeEntityDecl } from "../schema/declarations/EntityDecl.js";
|
|
5
|
-
import { isEnumDecl } from "../schema/declarations/EnumDecl.js";
|
|
6
|
-
import { isTypeAliasDecl } from "../schema/declarations/TypeAliasDecl.js";
|
|
7
|
-
import { getDisplayNameFromEntityInstance } from "../shared/utils/displayName.js";
|
|
8
|
-
import { isOk } from "../utils/result.js";
|
|
9
|
-
import { createInstance, deleteInstance, updateInstance } from "./instanceOperations.js";
|
|
10
|
-
const defaultOptions = {
|
|
11
|
-
name: "tsondb server",
|
|
12
|
-
port: 3000,
|
|
13
|
-
};
|
|
14
|
-
export const createServer = (modelContainer, instancesByEntityName, options) => {
|
|
15
|
-
const { name, port } = { ...defaultOptions, ...options };
|
|
16
|
-
const app = express();
|
|
17
|
-
app.use(express.static(join(import.meta.dirname, "../../public")));
|
|
18
|
-
app.use("/js/node_modules", express.static(join(import.meta.dirname, "../../node_modules")));
|
|
19
|
-
app.use("/js/client", express.static(join(import.meta.dirname, "../../lib/client")));
|
|
20
|
-
app.use("/js/shared", express.static(join(import.meta.dirname, "../../lib/shared")));
|
|
21
|
-
app.use(express.json());
|
|
22
|
-
const declarations = modelContainer.schema.declarations;
|
|
23
|
-
const entities = declarations.filter(isEntityDecl);
|
|
24
|
-
const entitiesByName = Object.fromEntries(entities.map(entity => [entity.name, entity]));
|
|
25
|
-
const instancesByEntityNameInMemory = { ...instancesByEntityName };
|
|
26
|
-
app.get("/api/declarations", (req, res) => {
|
|
27
|
-
let filteredEntities;
|
|
28
|
-
switch (req.query["kind"]) {
|
|
29
|
-
case "Entity":
|
|
30
|
-
filteredEntities = entities.filter(isEntityDecl);
|
|
31
|
-
break;
|
|
32
|
-
case "TypeAlias":
|
|
33
|
-
filteredEntities = entities.filter(isTypeAliasDecl);
|
|
34
|
-
break;
|
|
35
|
-
case "Enum":
|
|
36
|
-
filteredEntities = entities.filter(isEnumDecl);
|
|
37
|
-
break;
|
|
38
|
-
default:
|
|
39
|
-
filteredEntities = declarations;
|
|
40
|
-
}
|
|
41
|
-
const body = {
|
|
42
|
-
declarations: filteredEntities.map(decl => ({
|
|
43
|
-
declaration: serializeDecl(decl),
|
|
44
|
-
instanceCount: instancesByEntityNameInMemory[decl.name]?.length ?? 0,
|
|
45
|
-
})),
|
|
46
|
-
localeEntity: modelContainer.schema.localeEntity?.name,
|
|
47
|
-
};
|
|
48
|
-
res.json(body);
|
|
49
|
-
});
|
|
50
|
-
app.get("/api/declarations/:name", (req, res) => {
|
|
51
|
-
const decl = declarations.find(decl => decl.name === req.params.name);
|
|
52
|
-
if (decl === undefined) {
|
|
53
|
-
res.status(404).send(`Declaration "${req.params.name}" not found`);
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
const body = {
|
|
57
|
-
declaration: serializeDecl(decl),
|
|
58
|
-
instanceCount: instancesByEntityNameInMemory[decl.name]?.length ?? 0,
|
|
59
|
-
isLocaleEntity: decl === modelContainer.schema.localeEntity,
|
|
60
|
-
};
|
|
61
|
-
res.json(body);
|
|
62
|
-
});
|
|
63
|
-
app.get("/api/declarations/:name/instances", (req, res) => {
|
|
64
|
-
const decl = declarations.find(decl => decl.name === req.params.name);
|
|
65
|
-
if (decl === undefined) {
|
|
66
|
-
res.status(404).send(`Declaration "${req.params.name}" not found`);
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
if (!isEntityDecl(decl)) {
|
|
70
|
-
res.status(400).send(`Declaration "${decl.name}" is not an entity`);
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
const body = {
|
|
74
|
-
instances: instancesByEntityNameInMemory[req.params.name] ?? [],
|
|
75
|
-
isLocaleEntity: decl === modelContainer.schema.localeEntity,
|
|
76
|
-
};
|
|
77
|
-
res.json(body);
|
|
78
|
-
});
|
|
79
|
-
app.post("/api/declarations/:name/instances", async (req, res) => {
|
|
80
|
-
const decl = declarations.find(decl => decl.name === req.params.name);
|
|
81
|
-
if (decl === undefined) {
|
|
82
|
-
res.status(404).send(`Declaration "${req.params.name}" not found`);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
if (!isEntityDecl(decl)) {
|
|
86
|
-
res.status(400).send(`Declaration "${decl.name}" is not an entity`);
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
const result = await createInstance(modelContainer, entitiesByName, instancesByEntityNameInMemory, decl.name, req.body, req.query["id"]);
|
|
90
|
-
if (isOk(result)) {
|
|
91
|
-
const body = {
|
|
92
|
-
instance: result.value,
|
|
93
|
-
isLocaleEntity: decl === modelContainer.schema.localeEntity,
|
|
94
|
-
};
|
|
95
|
-
res.json(body);
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
res.status(result.error[0]).send(result.error[1]);
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
app.get("/api/declarations/:name/instances/:id", (req, res) => {
|
|
102
|
-
const decl = declarations.find(decl => decl.name === req.params.name);
|
|
103
|
-
if (decl === undefined) {
|
|
104
|
-
res.status(404).send(`Declaration "${req.params.name}" not found`);
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
if (!isEntityDecl(decl)) {
|
|
108
|
-
res.status(400).send(`Declaration "${decl.name}" is not an entity`);
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
const instance = instancesByEntityNameInMemory[decl.name]?.find(instance => instance.id === req.params.id);
|
|
112
|
-
if (instance === undefined) {
|
|
113
|
-
res.status(404).send(`Instance "${req.params.id}" not found`);
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
const body = {
|
|
117
|
-
instance: instance,
|
|
118
|
-
isLocaleEntity: decl === modelContainer.schema.localeEntity,
|
|
119
|
-
};
|
|
120
|
-
res.json(body);
|
|
121
|
-
});
|
|
122
|
-
app.put("/api/declarations/:name/instances/:id", async (req, res) => {
|
|
123
|
-
const decl = declarations.find(decl => decl.name === req.params.name);
|
|
124
|
-
if (decl === undefined) {
|
|
125
|
-
res.status(404).send(`Declaration "${req.params.name}" not found`);
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
if (!isEntityDecl(decl)) {
|
|
129
|
-
res.status(400).send(`Declaration "${decl.name}" is not an entity`);
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
const result = await updateInstance(modelContainer, entitiesByName, instancesByEntityNameInMemory, decl.name, req.params.id, req.body);
|
|
133
|
-
if (isOk(result)) {
|
|
134
|
-
const body = {
|
|
135
|
-
instance: result.value,
|
|
136
|
-
isLocaleEntity: decl === modelContainer.schema.localeEntity,
|
|
137
|
-
};
|
|
138
|
-
res.json(body);
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
res.status(result.error[0]).send(result.error[1]);
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
app.delete("/api/declarations/:name/instances/:id", async (req, res) => {
|
|
145
|
-
const decl = declarations.find(decl => decl.name === req.params.name);
|
|
146
|
-
if (decl === undefined) {
|
|
147
|
-
res.status(404).send(`Declaration "${req.params.name}" not found`);
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
if (!isEntityDecl(decl)) {
|
|
151
|
-
res.status(400).send(`Declaration "${decl.name}" is not an entity`);
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
const result = await deleteInstance(modelContainer, instancesByEntityNameInMemory, decl.name, req.params.id);
|
|
155
|
-
if (isOk(result)) {
|
|
156
|
-
const body = {
|
|
157
|
-
instance: result.value,
|
|
158
|
-
isLocaleEntity: decl === modelContainer.schema.localeEntity,
|
|
159
|
-
};
|
|
160
|
-
res.json(body);
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
res.status(result.error[0]).send(result.error[1]);
|
|
164
|
-
}
|
|
165
|
-
});
|
|
166
|
-
app.get("/api/instances", (req, res) => {
|
|
167
|
-
const locales = (Array.isArray(req.query["locales"]) ? req.query["locales"] : [req.query["locales"]]).filter(locale => typeof locale === "string");
|
|
168
|
-
const body = {
|
|
169
|
-
instances: Object.fromEntries(Object.entries(instancesByEntityNameInMemory).map(([entityName, instances]) => [
|
|
170
|
-
entityName,
|
|
171
|
-
instances.map(instance => ({
|
|
172
|
-
id: instance.id,
|
|
173
|
-
name: getDisplayNameFromEntityInstance(serializeEntityDecl(entitiesByName[entityName]), instance.content, instance.id, locales),
|
|
174
|
-
})),
|
|
175
|
-
])),
|
|
176
|
-
};
|
|
177
|
-
res.json(body);
|
|
178
|
-
});
|
|
179
|
-
app.get(/^\/.*/, (_req, res) => {
|
|
180
|
-
res.send(`<!DOCTYPE html>
|
|
181
|
-
<html lang="en">
|
|
182
|
-
<head>
|
|
183
|
-
<meta charset="UTF-8">
|
|
184
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
185
|
-
<title>TSONDB</title>
|
|
186
|
-
<link rel="stylesheet" href="/css/styles.css">
|
|
187
|
-
</head>
|
|
188
|
-
<body>
|
|
189
|
-
<div id="app"></div>
|
|
190
|
-
<script type="importmap">
|
|
191
|
-
{
|
|
192
|
-
"imports": {
|
|
193
|
-
"preact": "/js/node_modules/preact/dist/preact.module.js",
|
|
194
|
-
"preact/hooks": "/js/node_modules/preact/hooks/dist/hooks.module.js",
|
|
195
|
-
"preact/jsx-runtime": "/js/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js",
|
|
196
|
-
"preact-iso": "/js/node_modules/preact-iso/src/index.js"
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
</script>
|
|
200
|
-
<script type="module" src="/js/client/index.js"></script>
|
|
201
|
-
</body>
|
|
202
|
-
</html>`);
|
|
203
|
-
});
|
|
204
|
-
app.listen(port, () => {
|
|
205
|
-
console.log(`${name} listening on http://localhost:${port}`);
|
|
206
|
-
});
|
|
207
|
-
};
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { ModelContainer } from "../ModelContainer.js";
|
|
2
|
-
import { EntityDecl } from "../schema/declarations/EntityDecl.js";
|
|
3
|
-
import { InstanceContainer, InstancesByEntityName } from "../shared/utils/instances.js";
|
|
4
|
-
import { Result } from "../utils/result.js";
|
|
5
|
-
export declare const createInstance: (modelContainer: ModelContainer, entitiesByName: Record<string, EntityDecl>, instancesByEntityName: InstancesByEntityName, entityName: string, instance: unknown, idQueryParam: unknown) => Promise<Result<InstanceContainer, [code: number, message: string]>>;
|
|
6
|
-
export declare const updateInstance: (modelContainer: ModelContainer, entitiesByName: Record<string, EntityDecl>, instancesByEntityName: InstancesByEntityName, entityName: string, id: string, instance: unknown) => Promise<Result<InstanceContainer, [code: number, message: string]>>;
|
|
7
|
-
export declare const deleteInstance: (modelContainer: ModelContainer, instancesByEntityName: InstancesByEntityName, entityName: string, id: string) => Promise<Result<InstanceContainer, [code: number, message: string]>>;
|