cloesce 0.0.3-fix.3 → 0.0.3-fix.5

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/cli.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
@@ -5,8 +5,8 @@ import { readFile } from "fs/promises";
5
5
  import path from "node:path";
6
6
  import { command, run, subcommands, flag, option, optional, string, } from "cmd-ts";
7
7
  import { Project } from "ts-morph";
8
- import { CidlExtractor } from "./extract.js";
9
- import { ExtractorError, ExtractorErrorCode, getErrorInfo } from "../common.js";
8
+ import { CidlExtractor } from "./extractor/extract.js";
9
+ import { ExtractorError, ExtractorErrorCode, getErrorInfo } from "./common.js";
10
10
  const cmds = subcommands({
11
11
  name: "cloesce",
12
12
  cmds: {
@@ -28,7 +28,6 @@ const cmds = subcommands({
28
28
  }
29
29
  // Creates a `cidl.json` file. Exits the process on failure.
30
30
  await extract({ debug: args.debug });
31
- const root = process.cwd();
32
31
  const outputDir = config.outputDir ?? ".generated";
33
32
  const allConfig = {
34
33
  name: "all",
@@ -37,8 +36,8 @@ const cmds = subcommands({
37
36
  "generate",
38
37
  "all",
39
38
  path.join(outputDir, "cidl.json"),
40
- path.join(root, "wrangler.toml"),
41
- path.join(root, "migrations/migrations.sql"),
39
+ "wrangler.toml",
40
+ "migrations/migrations.sql",
42
41
  path.join(outputDir, "workers.ts"),
43
42
  path.join(outputDir, "client.ts"),
44
43
  config.clientUrl,
@@ -54,11 +53,10 @@ const cmds = subcommands({
54
53
  description: "Generate wrangler.toml configuration",
55
54
  args: {},
56
55
  handler: async () => {
57
- const root = process.cwd();
58
56
  await generate({
59
57
  name: "wrangler",
60
58
  wasmFile: "generator.wasm",
61
- args: ["generate", "wrangler", path.join(root, "wrangler.toml")],
59
+ args: ["generate", "wrangler", "wrangler.toml"],
62
60
  });
63
61
  },
64
62
  }),
@@ -68,7 +66,6 @@ const cmds = subcommands({
68
66
  args: {},
69
67
  handler: async () => {
70
68
  const config = loadCloesceConfig(process.cwd());
71
- const root = process.cwd();
72
69
  const outputDir = config.outputDir ?? ".generated";
73
70
  await generate({
74
71
  name: "d1",
@@ -77,7 +74,7 @@ const cmds = subcommands({
77
74
  "generate",
78
75
  "d1",
79
76
  path.join(outputDir, "cidl.json"),
80
- path.join(root, "migrations/migrations.sql"),
77
+ "migrations/migrations.sql",
81
78
  ],
82
79
  });
83
80
  },
@@ -88,10 +85,9 @@ const cmds = subcommands({
88
85
  args: {},
89
86
  handler: async () => {
90
87
  const config = loadCloesceConfig(process.cwd());
91
- const root = process.cwd();
92
88
  const outputDir = config.outputDir ?? ".generated";
93
89
  if (!config.workersUrl) {
94
- console.error("Error: workersUrl must be defined in cloesce-config.json");
90
+ console.error("Error: workersUrl must be defined in cloesce.config.json");
95
91
  process.exit(1);
96
92
  }
97
93
  await generate({
@@ -102,7 +98,7 @@ const cmds = subcommands({
102
98
  "workers",
103
99
  path.join(outputDir, "cidl.json"),
104
100
  path.join(outputDir, "workers.ts"),
105
- path.join(root, "wrangler.toml"),
101
+ "wrangler.toml",
106
102
  config.workersUrl,
107
103
  ],
108
104
  });
@@ -238,32 +234,13 @@ async function generate(config) {
238
234
  if (!fs.existsSync(wranglerPath)) {
239
235
  fs.writeFileSync(wranglerPath, "");
240
236
  }
241
- const wasiArgs = config.args.map((arg) => {
242
- // Skip URLs
243
- if (/^[a-zA-Z]+:\/\//.test(arg)) {
244
- return arg;
245
- }
246
- // Convert file path it to Unix style
247
- if (arg.includes(path.sep) || arg.includes("/")) {
248
- // Convert to relative path from root
249
- const relativePath = path.isAbsolute(arg)
250
- ? path.relative(root, arg)
251
- : arg;
252
- // Convert Windows separators to Unix and ensure leading slash
253
- const unixPath = relativePath.replace(/\\/g, "/");
254
- return unixPath.startsWith("/") ? unixPath : "/" + unixPath;
255
- }
256
- return arg;
257
- });
258
237
  const wasi = new WASI({
259
238
  version: "preview1",
260
- args: ["generate", ...wasiArgs],
239
+ args: ["generate", ...config.args],
261
240
  env: { ...process.env, ...config.env },
262
- preopens: { "/": root },
241
+ preopens: { ".": root },
263
242
  });
264
- // Since `generator.wasm` is a binary and not a library, it must be
265
- // manually read
266
- const wasm = await readFile(new URL("../generator.wasm", import.meta.url));
243
+ const wasm = await readFile(new URL("./generator.wasm", import.meta.url));
267
244
  const mod = await WebAssembly.compile(new Uint8Array(wasm));
268
245
  let instance = await WebAssembly.instantiate(mod, {
269
246
  wasi_snapshot_preview1: wasi.wasiImport,
@@ -0,0 +1,126 @@
1
+ export type Either<L, R> = {
2
+ ok: false;
3
+ value: L;
4
+ } | {
5
+ ok: true;
6
+ value: R;
7
+ };
8
+ export declare function left<L>(value: L): Either<L, never>;
9
+ export declare function right<R>(value: R): Either<never, R>;
10
+ export declare enum ExtractorErrorCode {
11
+ UnknownType = 0,
12
+ MultipleGenericType = 1,
13
+ InvalidIncludeTree = 2,
14
+ UnknownNavigationPropertyReference = 3,
15
+ InvalidNavigationPropertyReference = 4,
16
+ MissingNavigationPropertyReference = 5,
17
+ MissingManyToManyUniqueId = 6,
18
+ MissingPrimaryKey = 7,
19
+ MissingWranglerEnv = 8,
20
+ TooManyWranglerEnvs = 9,
21
+ MissingFile = 10
22
+ }
23
+ export declare function getErrorInfo(code: ExtractorErrorCode): {
24
+ description: string;
25
+ suggestion: string;
26
+ };
27
+ export declare class ExtractorError {
28
+ code: ExtractorErrorCode;
29
+ context?: string;
30
+ snippet?: string;
31
+ constructor(code: ExtractorErrorCode);
32
+ addContext(fn: (val: string | undefined) => string | undefined): void;
33
+ }
34
+ export type HttpResult<T = unknown> = {
35
+ ok: boolean;
36
+ status: number;
37
+ data?: T;
38
+ message?: string;
39
+ };
40
+ export type CidlType = "Void" | "Integer" | "Real" | "Text" | "Blob" | {
41
+ Inject: string;
42
+ } | {
43
+ Object: string;
44
+ } | {
45
+ Nullable: CidlType;
46
+ } | {
47
+ Array: CidlType;
48
+ } | {
49
+ HttpResult: CidlType;
50
+ };
51
+ export declare function isNullableType(ty: CidlType): boolean;
52
+ export declare enum HttpVerb {
53
+ GET = "GET",
54
+ POST = "POST",
55
+ PUT = "PUT",
56
+ PATCH = "PATCH",
57
+ DELETE = "DELETE"
58
+ }
59
+ export interface NamedTypedValue {
60
+ name: string;
61
+ cidl_type: CidlType;
62
+ }
63
+ export interface ModelAttribute {
64
+ value: NamedTypedValue;
65
+ foreign_key_reference: string | null;
66
+ }
67
+ export interface ModelMethod {
68
+ name: string;
69
+ is_static: boolean;
70
+ http_verb: HttpVerb;
71
+ return_type: CidlType | null;
72
+ parameters: NamedTypedValue[];
73
+ }
74
+ export type NavigationPropertyKind = {
75
+ OneToOne: {
76
+ reference: string;
77
+ };
78
+ } | {
79
+ OneToMany: {
80
+ reference: string;
81
+ };
82
+ } | {
83
+ ManyToMany: {
84
+ unique_id: string;
85
+ };
86
+ };
87
+ export interface NavigationProperty {
88
+ var_name: string;
89
+ model_name: string;
90
+ kind: NavigationPropertyKind;
91
+ }
92
+ export declare function getNavigationPropertyCidlType(nav: NavigationProperty): CidlType;
93
+ export interface Model {
94
+ name: string;
95
+ primary_key: NamedTypedValue;
96
+ attributes: ModelAttribute[];
97
+ navigation_properties: NavigationProperty[];
98
+ methods: Record<string, ModelMethod>;
99
+ data_sources: Record<string, DataSource>;
100
+ source_path: string;
101
+ }
102
+ export interface PlainOldObject {
103
+ name: string;
104
+ attributes: NamedTypedValue[];
105
+ source_path: string;
106
+ }
107
+ export interface CidlIncludeTree {
108
+ [key: string]: CidlIncludeTree;
109
+ }
110
+ export interface DataSource {
111
+ name: string;
112
+ tree: CidlIncludeTree;
113
+ }
114
+ export interface WranglerEnv {
115
+ name: string;
116
+ source_path: string;
117
+ }
118
+ export interface CloesceAst {
119
+ version: string;
120
+ project_name: string;
121
+ language: "TypeScript";
122
+ wrangler_env: WranglerEnv;
123
+ models: Record<string, Model>;
124
+ poos: Record<string, PlainOldObject>;
125
+ }
126
+ //# sourceMappingURL=common.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../src/common.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAE,CAAC;AAC5E,wBAAgB,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAElD;AACD,wBAAgB,KAAK,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAEnD;AAED,oBAAY,kBAAkB;IAC5B,WAAW,IAAA;IACX,mBAAmB,IAAA;IACnB,kBAAkB,IAAA;IAClB,kCAAkC,IAAA;IAClC,kCAAkC,IAAA;IAClC,kCAAkC,IAAA;IAClC,yBAAyB,IAAA;IACzB,iBAAiB,IAAA;IACjB,kBAAkB,IAAA;IAClB,mBAAmB,IAAA;IACnB,WAAW,KAAA;CACZ;AAyDD,wBAAgB,YAAY,CAAC,IAAI,EAAE,kBAAkB;iBArDpC,MAAM;gBAAc,MAAM;EAuD1C;AAED,qBAAa,cAAc;IAIN,IAAI,EAAE,kBAAkB;IAH3C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;gBAEE,IAAI,EAAE,kBAAkB;IAE3C,UAAU,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,KAAK,MAAM,GAAG,SAAS;CAG/D;AAED,MAAM,MAAM,UAAU,CAAC,CAAC,GAAG,OAAO,IAAI;IACpC,EAAE,EAA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ph";
2
+ import { CloesceAst, Either, ExtractorError } from "../common.js";
3
+ export declare class CidlExtractor {
4
+ projectName: string;
5
+ version: string;
6
+ constructor(projectName: string, version: string);
7
+ extract(project: Project): Either<ExtractorError, CloesceAst>;
8
+ private static model;
9
+ private static poo;
10
+ private static readonly primTypeMap;
11
+ private static cidlType;
12
+ private static includeTree;
13
+ private static method;
14
+ }
15
+ //# sourceMappingURL=extract.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extract.d.ts","sourceRoot":"","sources":["../../src/extractor/extract.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EASR,MAAM,UAAU,CAAC;AAElB,OAAO,EAEL,UAAU,EAGV,MAAM,EAUN,cAAc,EAGf,MAAM,cAAc,CAAC;AAsBtB,qBAAa,aAAa;IAEf,WAAW,EAAE,MAAM;IACnB,OAAO,EAAE,MAAM;gBADf,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,MAAM;IAGxB,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAAC,cAAc,EAAE,UAAU,CAAC;IAuE7D,OAAO,CAAC,MAAM,CAAC,KAAK;IA6MpB,OAAO,CAAC,MAAM,CAAC,GAAG;IAgClB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,WAAW,CAQjC;IAEF,OAAO,CAAC,MAAM,CAAC,QAAQ;IAoGvB,OAAO,CAAC,MAAM,CAAC,WAAW;IAqF1B,OAAO,CAAC,MAAM,CAAC,MAAM;CA+DtB"}
Binary file
@@ -0,0 +1,22 @@
1
+ export { cloesce, modelsFromSql } from "../runtime/runtime.js";
2
+ export type { HttpResult, Either } from "../common.js";
3
+ export declare const D1: ClassDecorator;
4
+ export declare const PlainOldObject: ClassDecorator;
5
+ export declare const WranglerEnv: ClassDecorator;
6
+ export declare const PrimaryKey: PropertyDecorator;
7
+ export declare const GET: MethodDecorator;
8
+ export declare const POST: MethodDecorator;
9
+ export declare const PUT: MethodDecorator;
10
+ export declare const PATCH: MethodDecorator;
11
+ export declare const DELETE: MethodDecorator;
12
+ export declare const DataSource: PropertyDecorator;
13
+ export declare const OneToMany: (_: string) => PropertyDecorator;
14
+ export declare const OneToOne: (_: string) => PropertyDecorator;
15
+ export declare const ManyToMany: (_: string) => PropertyDecorator;
16
+ export declare const ForeignKey: <T>(_: T) => PropertyDecorator;
17
+ export declare const Inject: ParameterDecorator;
18
+ type Primitive = string | number | boolean | bigint | symbol | null | undefined;
19
+ export type IncludeTree<T> = T extends Primitive ? never : {
20
+ [K in keyof T]?: T[K] extends (infer U)[] ? IncludeTree<NonNullable<U>> : IncludeTree<NonNullable<T[K]>>;
21
+ };
22
+ //# sourceMappingURL=backend.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"backend.d.ts","sourceRoot":"","sources":["../../src/index/backend.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC/D,YAAY,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAGvD,eAAO,MAAM,EAAE,EAAE,cAAyB,CAAC;AAC3C,eAAO,MAAM,cAAc,EAAE,cAAyB,CAAC;AACvD,eAAO,MAAM,WAAW,EAAE,cAAyB,CAAC;AACpD,eAAO,MAAM,UAAU,EAAE,iBAA4B,CAAC;AACtD,eAAO,MAAM,GAAG,EAAE,eAA0B,CAAC;AAC7C,eAAO,MAAM,IAAI,EAAE,eAA0B,CAAC;AAC9C,eAAO,MAAM,GAAG,EAAE,eAA0B,CAAC;AAC7C,eAAO,MAAM,KAAK,EAAE,eAA0B,CAAC;AAC/C,eAAO,MAAM,MAAM,EAAE,eAA0B,CAAC;AAChD,eAAO,MAAM,UAAU,EAAE,iBAA4B,CAAC;AACtD,eAAO,MAAM,SAAS,GACnB,GAAG,MAAM,KAAG,iBACL,CAAC;AACX,eAAO,MAAM,QAAQ,GAClB,GAAG,MAAM,KAAG,iBACL,CAAC;AACX,eAAO,MAAM,UAAU,GACpB,GAAG,MAAM,KAAG,iBACL,CAAC;AACX,eAAO,MAAM,UAAU,GACpB,CAAC,EAAE,GAAG,CAAC,KAAG,iBACH,CAAC;AACX,eAAO,MAAM,MAAM,EAAE,kBAA6B,CAAC;AAGnD,KAAK,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;AAChF,MAAM,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,SAAS,SAAS,GAC5C,KAAK,GACL;KACG,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,GACrC,WAAW,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAC3B,WAAW,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CACnC,CAAC"}
@@ -1,4 +1,4 @@
1
- export { cloesce, modelsFromSql } from "./runtime/runtime.js";
1
+ export { cloesce, modelsFromSql } from "../runtime/runtime.js";
2
2
  // Compiler hints
3
3
  export const D1 = () => { };
4
4
  export const PlainOldObject = () => { };
@@ -15,10 +15,3 @@ export const OneToOne = (_) => () => { };
15
15
  export const ManyToMany = (_) => () => { };
16
16
  export const ForeignKey = (_) => () => { };
17
17
  export const Inject = () => { };
18
- // Helpers
19
- export function instantiateObjectArray(data, ctor) {
20
- if (Array.isArray(data)) {
21
- return data.map((x) => instantiateObjectArray(x, ctor)).flat();
22
- }
23
- return [Object.assign(new ctor(), data)];
24
- }
@@ -0,0 +1,5 @@
1
+ export type { HttpResult, Either } from "../common.js";
2
+ export declare function instantiateObjectArray<T extends object>(data: any, ctor: {
3
+ new (): T;
4
+ }): T[];
5
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/index/client.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAGvD,wBAAgB,sBAAsB,CAAC,CAAC,SAAS,MAAM,EACrD,IAAI,EAAE,GAAG,EACT,IAAI,EAAE;IAAE,QAAQ,CAAC,CAAA;CAAE,GAClB,CAAC,EAAE,CAKL"}
@@ -0,0 +1,7 @@
1
+ // Helpers
2
+ export function instantiateObjectArray(data, ctor) {
3
+ if (Array.isArray(data)) {
4
+ return data.map((x) => instantiateObjectArray(x, ctor)).flat();
5
+ }
6
+ return [Object.assign(new ctor(), data)];
7
+ }
@@ -0,0 +1,112 @@
1
+ import { HttpResult, Either, ModelMethod, CloesceAst, Model } from "../common.js";
2
+ import { IncludeTree } from "../index/backend.js";
3
+ /**
4
+ * Map of model names to their respective constructor.
5
+ *
6
+ * The value accepted into the `cloesce` function is generated by the Cloesce compiler, and
7
+ * is guaranteed to contain all model definitions.
8
+ */
9
+ type ModelConstructorRegistry = Record<string, new () => UserDefinedModel>;
10
+ /**
11
+ * Dependency injection container, mapping an object type name to an instance of that object.
12
+ *
13
+ * The value accepted into the `cloesce` function is generated by the Cloesce compiler, and is
14
+ * guaranteed to contain all injected model method parameters.
15
+ */
16
+ type InstanceRegistry = Map<string, any>;
17
+ /**
18
+ * Users will create Cloesce models, which have metadata for them in the ast.
19
+ * For TypeScript's purposes, these models can be anything. We can assume any
20
+ * `UserDefinedModel` has been verified by the compiler.
21
+ */
22
+ type UserDefinedModel = any;
23
+ /**
24
+ * Given a request, this represents a map of each body / url param name to
25
+ * its actual value. Unknown, as the a request can be anything.
26
+ */
27
+ type RequestParamMap = Record<string, unknown>;
28
+ /**
29
+ * Meta information on the wrangler env and db bindings
30
+ */
31
+ interface MetaWranglerEnv {
32
+ envName: string;
33
+ dbName: string;
34
+ }
35
+ /**
36
+ * WASM ABI
37
+ */
38
+ interface RuntimeWasmExports {
39
+ memory: WebAssembly.Memory;
40
+ get_return_len(): number;
41
+ set_meta_ptr(ptr: number, len: number): number;
42
+ alloc(len: number): number;
43
+ dealloc(ptr: number, len: number): void;
44
+ object_relational_mapping(model_name_ptr: number, model_name_len: number, sql_rows_ptr: number, sql_rows_len: number, include_tree_ptr: number, include_tree_len: number): number;
45
+ }
46
+ /**
47
+ * Singleton instances of the cidl, constructor registry, and wasm binary.
48
+ * These values are guaranteed to never change throughout a program lifetime.
49
+ */
50
+ declare class RuntimeContainer {
51
+ readonly ast: CloesceAst;
52
+ readonly constructorRegistry: ModelConstructorRegistry;
53
+ readonly wasm: RuntimeWasmExports;
54
+ private static instance;
55
+ private constructor();
56
+ static init(ast: CloesceAst, constructorRegistry: ModelConstructorRegistry, wasm?: WebAssembly.Instance): Promise<void>;
57
+ static get(): RuntimeContainer;
58
+ }
59
+ /**
60
+ * Runtime entry point. Given a request, undergoes: routing, validating,
61
+ * hydrating, and method dispatch.
62
+ *
63
+ * @returns A Response with an `HttpResult` JSON body.
64
+ */
65
+ export declare function cloesce(request: Request, ast: CloesceAst, constructorRegistry: ModelConstructorRegistry, instanceRegistry: InstanceRegistry, envMeta: MetaWranglerEnv, api_route: string): Promise<Response>;
66
+ /**
67
+ * Matches a request to a method on a model.
68
+ * @param api_route The route from the domain to the actual API, ie https://foo.com/route/to/api => route/to/api/
69
+ * @returns 404 or a `MatchedRoute`
70
+ */
71
+ declare function matchRoute(request: Request, ast: CloesceAst, api_route: string): Either<HttpResult, {
72
+ model: Model;
73
+ method: ModelMethod;
74
+ id: string | null;
75
+ }>;
76
+ /**
77
+ * Validates the request's body/search params against a ModelMethod
78
+ * @returns 400 or a `RequestParamMap` consisting of each parameters name mapped to its value, and
79
+ * a data source
80
+ */
81
+ declare function validateRequest(request: Request, ast: CloesceAst, model: Model, method: ModelMethod, id: string | null): Promise<Either<HttpResult, {
82
+ params: RequestParamMap;
83
+ dataSource: string | null;
84
+ }>>;
85
+ /**
86
+ * Calls a method on a model given a list of parameters.
87
+ * @returns 500 on an uncaught client error, 200 with a result body on success
88
+ */
89
+ declare function methodDispatch(instance: object, instanceRegistry: InstanceRegistry, envMeta: MetaWranglerEnv, method: ModelMethod, params: Record<string, unknown>): Promise<HttpResult<unknown>>;
90
+ /**
91
+ * Creates model instances given a properly formatted SQL record, being either:
92
+ *
93
+ * 1. Flat, relationship-less (ex: id, name, location, ...)
94
+ * 2. `DataSource` formatted (ex: Horse.id, Horse.name, Horse.rider, ...)
95
+ *
96
+ * @param ctor The type of the model
97
+ * @param records SQL records
98
+ * @param includeTree The include tree to use when parsing the records
99
+ * @returns An instantiated array of `T`, containing one or more objects.
100
+ */
101
+ export declare function modelsFromSql<T extends object>(ctor: new () => T, records: Record<string, any>[], includeTree: IncludeTree<T> | null): T[];
102
+ /**
103
+ * For testing purposes
104
+ */
105
+ export declare const _cloesceInternal: {
106
+ matchRoute: typeof matchRoute;
107
+ validateRequest: typeof validateRequest;
108
+ methodDispatch: typeof methodDispatch;
109
+ RuntimeContainer: typeof RuntimeContainer;
110
+ };
111
+ export {};
112
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/runtime/runtime.ts"],"names":[],"mappings":"AACA,OAAO,EACL,UAAU,EACV,MAAM,EACN,WAAW,EAIX,UAAU,EAEV,KAAK,EAGN,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAKlD;;;;;GAKG;AACH,KAAK,wBAAwB,GAAG,MAAM,CAAC,MAAM,EAAE,UAAU,gBAAgB,CAAC,CAAC;AAE3E;;;;;GAKG;AACH,KAAK,gBAAgB,GAAG,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAEzC;;;;GAIG;AACH,KAAK,gBAAgB,GAAG,GAAG,CAAC;AAE5B;;;GAGG;AACH,KAAK,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE/C;;GAEG;AACH,UAAU,eAAe;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MA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,CAAC;IACb,MAAM,EAAE,WAAW,CAAC;IACpB,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;CACnB,CACF,CAyCA;AAED;;;;GAIG;AACH,iBAAe,eAAe,CAC5B,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,UAAU,EACf,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,WAAW,EACnB,EAAE,EAAE,MAAM,GAAG,IAAI,GAChB,OAAO,CACR,MAAM,CAAC,UAAU,EAAE;IAAE,MAAM,EAAE,eAAe,CAAC;IAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC,CAC3E,CAuDA;AA+DD;;;GAGG;AACH,iBAAe,cAAc,CAC3B,QAAQ,EAAE,MAAM,EAChB,gBAAgB,EAAE,gBAAgB,EAClC,OAAO,EAAE,eAAe,EACxB,MAAM,EAAE,WAAW,EACnB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC9B,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAoC9B;AA4FD;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,CAAC,SAAS,MAAM,EAC5C,IAAI,EAAE,UAAU,CAAC,EACjB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,EAC9B,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,IAAI,GACjC,CAAC,EAAE,CAoFL;AAaD;;GAEG;AACH,eAAO,MAAM,gBAAgB;;;;;CAK5B,CAAC"}
package/package.json CHANGED
@@ -1,10 +1,8 @@
1
1
  {
2
2
  "name": "cloesce",
3
- "version": "0.0.3-fix.3",
3
+ "version": "0.0.3-fix.5",
4
4
  "description": "A tool to extract and compile TypeScript code into something wrangler can consume and deploy for D1 Databases and Cloudflare Workers",
5
5
  "type": "module",
6
- "main": "./dist/index.js",
7
- "types": "./dist/index.d.ts",
8
6
  "license": "Apache-2.0",
9
7
  "scripts": {
10
8
  "test": "vitest",
@@ -29,14 +27,17 @@
29
27
  "vite-plugin-wasm": "^3.5.0"
30
28
  },
31
29
  "exports": {
32
- ".": {
33
- "types": "./dist/index.d.ts",
34
- "import": "./dist/index.js"
30
+ "./client": {
31
+ "types": "./dist/index/client.d.ts",
32
+ "import": "./dist/index/client.js"
35
33
  },
36
- "./cli": "./dist/extractor/cli.js"
34
+ "./backend": {
35
+ "types": "./dist/index/backend.d.ts",
36
+ "import": "./dist/index/backend.js"
37
+ }
37
38
  },
38
39
  "bin": {
39
- "cloesce": "dist/extractor/cli.js"
40
+ "cloesce": "dist/cli.js"
40
41
  },
41
42
  "files": [
42
43
  "dist/**/*",