rev-dal 0.0.0-stage → 0.2.2
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/AGENTS.md +242 -0
- package/CONTRIBUTING.md +83 -0
- package/LICENSE +121 -0
- package/README.md +67 -2
- package/dist/index.d.ts +48 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/clone.d.ts +24 -0
- package/dist/lib/clone.d.ts.map +1 -0
- package/dist/lib/clone.js +83 -0
- package/dist/lib/clone.js.map +1 -0
- package/dist/lib/create-model.d.ts +207 -0
- package/dist/lib/create-model.d.ts.map +1 -0
- package/dist/lib/create-model.js +230 -0
- package/dist/lib/create-model.js.map +1 -0
- package/dist/lib/data-access-layer.d.ts +140 -0
- package/dist/lib/data-access-layer.d.ts.map +1 -0
- package/dist/lib/data-access-layer.js +389 -0
- package/dist/lib/data-access-layer.js.map +1 -0
- package/dist/lib/errors.d.ts +139 -0
- package/dist/lib/errors.d.ts.map +1 -0
- package/dist/lib/errors.js +206 -0
- package/dist/lib/errors.js.map +1 -0
- package/dist/lib/filter-where.d.ts +199 -0
- package/dist/lib/filter-where.d.ts.map +1 -0
- package/dist/lib/filter-where.js +563 -0
- package/dist/lib/filter-where.js.map +1 -0
- package/dist/lib/ml-string.d.ts +63 -0
- package/dist/lib/ml-string.d.ts.map +1 -0
- package/dist/lib/ml-string.js +331 -0
- package/dist/lib/ml-string.js.map +1 -0
- package/dist/lib/model-factory.d.ts +25 -0
- package/dist/lib/model-factory.d.ts.map +1 -0
- package/dist/lib/model-factory.js +57 -0
- package/dist/lib/model-factory.js.map +1 -0
- package/dist/lib/model-handle.d.ts +67 -0
- package/dist/lib/model-handle.d.ts.map +1 -0
- package/dist/lib/model-handle.js +182 -0
- package/dist/lib/model-handle.js.map +1 -0
- package/dist/lib/model-initializer.d.ts +96 -0
- package/dist/lib/model-initializer.d.ts.map +1 -0
- package/dist/lib/model-initializer.js +182 -0
- package/dist/lib/model-initializer.js.map +1 -0
- package/dist/lib/model-manifest.d.ts +112 -0
- package/dist/lib/model-manifest.d.ts.map +1 -0
- package/dist/lib/model-manifest.js +2 -0
- package/dist/lib/model-manifest.js.map +1 -0
- package/dist/lib/model-registry.d.ts +81 -0
- package/dist/lib/model-registry.d.ts.map +1 -0
- package/dist/lib/model-registry.js +139 -0
- package/dist/lib/model-registry.js.map +1 -0
- package/dist/lib/model-types.d.ts +380 -0
- package/dist/lib/model-types.d.ts.map +1 -0
- package/dist/lib/model-types.js +2 -0
- package/dist/lib/model-types.js.map +1 -0
- package/dist/lib/model.d.ts +503 -0
- package/dist/lib/model.d.ts.map +1 -0
- package/dist/lib/model.js +1423 -0
- package/dist/lib/model.js.map +1 -0
- package/dist/lib/postgres-config.d.ts +3 -0
- package/dist/lib/postgres-config.d.ts.map +1 -0
- package/dist/lib/postgres-config.js +2 -0
- package/dist/lib/postgres-config.js.map +1 -0
- package/dist/lib/query-builder.d.ts +607 -0
- package/dist/lib/query-builder.d.ts.map +1 -0
- package/dist/lib/query-builder.js +1734 -0
- package/dist/lib/query-builder.js.map +1 -0
- package/dist/lib/revision.d.ts +55 -0
- package/dist/lib/revision.d.ts.map +1 -0
- package/dist/lib/revision.js +307 -0
- package/dist/lib/revision.js.map +1 -0
- package/dist/lib/runtime.d.ts +18 -0
- package/dist/lib/runtime.d.ts.map +1 -0
- package/dist/lib/runtime.js +134 -0
- package/dist/lib/runtime.js.map +1 -0
- package/dist/lib/transaction.d.ts +17 -0
- package/dist/lib/transaction.d.ts.map +1 -0
- package/dist/lib/transaction.js +30 -0
- package/dist/lib/transaction.js.map +1 -0
- package/dist/lib/type.d.ts +130 -0
- package/dist/lib/type.d.ts.map +1 -0
- package/dist/lib/type.js +376 -0
- package/dist/lib/type.js.map +1 -0
- package/docs/models.md +406 -0
- package/docs/multilingual-text.md +142 -0
- package/docs/queries.md +374 -0
- package/docs/revisions.md +61 -0
- package/package.json +60 -4
- package/releases/0.2.0.md +18 -0
- package/releases/0.2.1.md +22 -0
- package/releases/0.2.2.md +11 -0
- package/releases/README.md +7 -0
- package/setup-db-grants.sql +51 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep-copy a row value so the copy shares no mutable state with the
|
|
3
|
+
* original, while keeping every type `pg` serializes differently:
|
|
4
|
+
*
|
|
5
|
+
* - primitives, `null`, `undefined` and functions are returned as-is
|
|
6
|
+
* - `Date` → new `Date`; `Buffer` → new `Buffer`; other typed arrays and
|
|
7
|
+
* `DataView`s → copies of the same class
|
|
8
|
+
* - arrays, `Map` and `Set` → new containers with copied contents
|
|
9
|
+
* - other objects → a copy with the same prototype and copied own
|
|
10
|
+
* properties, so plain JSON objects stay plain and class instances keep
|
|
11
|
+
* methods such as `toJSON()` / `toPostgres()` that `pg` relies on
|
|
12
|
+
*
|
|
13
|
+
* `structuredClone` is not used because it turns a `Buffer` into a plain
|
|
14
|
+
* `Uint8Array`, drops prototypes, and throws on functions.
|
|
15
|
+
*
|
|
16
|
+
* Cycles are preserved. Objects whose state lives in internal slots or
|
|
17
|
+
* `#private` fields (other than the built-ins handled above) cannot be copied
|
|
18
|
+
* faithfully by any generic routine; they don't come out of PostgreSQL rows.
|
|
19
|
+
*
|
|
20
|
+
* @param value Value to copy
|
|
21
|
+
* @returns An independent copy of `value`
|
|
22
|
+
*/
|
|
23
|
+
export function cloneRowValue(value) {
|
|
24
|
+
return cloneInner(value, new WeakMap());
|
|
25
|
+
}
|
|
26
|
+
function cloneInner(value, seen) {
|
|
27
|
+
if (value === null || typeof value !== 'object') {
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
if (seen.has(value)) {
|
|
31
|
+
return seen.get(value);
|
|
32
|
+
}
|
|
33
|
+
if (value instanceof Date) {
|
|
34
|
+
return new Date(value.getTime());
|
|
35
|
+
}
|
|
36
|
+
if (Buffer.isBuffer(value)) {
|
|
37
|
+
return Buffer.from(value);
|
|
38
|
+
}
|
|
39
|
+
if (ArrayBuffer.isView(value)) {
|
|
40
|
+
// Typed arrays and DataViews keep their concrete class
|
|
41
|
+
return structuredClone(value);
|
|
42
|
+
}
|
|
43
|
+
if (value instanceof RegExp) {
|
|
44
|
+
return new RegExp(value.source, value.flags);
|
|
45
|
+
}
|
|
46
|
+
if (Array.isArray(value)) {
|
|
47
|
+
const copy = [];
|
|
48
|
+
seen.set(value, copy);
|
|
49
|
+
for (const item of value) {
|
|
50
|
+
copy.push(cloneInner(item, seen));
|
|
51
|
+
}
|
|
52
|
+
return copy;
|
|
53
|
+
}
|
|
54
|
+
if (value instanceof Map) {
|
|
55
|
+
const copy = new Map();
|
|
56
|
+
seen.set(value, copy);
|
|
57
|
+
for (const [key, item] of value) {
|
|
58
|
+
copy.set(cloneInner(key, seen), cloneInner(item, seen));
|
|
59
|
+
}
|
|
60
|
+
return copy;
|
|
61
|
+
}
|
|
62
|
+
if (value instanceof Set) {
|
|
63
|
+
const copy = new Set();
|
|
64
|
+
seen.set(value, copy);
|
|
65
|
+
for (const item of value) {
|
|
66
|
+
copy.add(cloneInner(item, seen));
|
|
67
|
+
}
|
|
68
|
+
return copy;
|
|
69
|
+
}
|
|
70
|
+
const copy = Object.create(Object.getPrototypeOf(value));
|
|
71
|
+
seen.set(value, copy);
|
|
72
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
73
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
74
|
+
if (!descriptor)
|
|
75
|
+
continue;
|
|
76
|
+
if ('value' in descriptor) {
|
|
77
|
+
descriptor.value = cloneInner(descriptor.value, seen);
|
|
78
|
+
}
|
|
79
|
+
Object.defineProperty(copy, key, descriptor);
|
|
80
|
+
}
|
|
81
|
+
return copy;
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=clone.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clone.js","sourceRoot":"","sources":["../../src/lib/clone.ts"],"names":[],"mappings":"AAA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xD,CAAC;QACD,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;IAC/C,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import type { EmptyStaticMethods, InferConstructor, InferData, InferInstance, ManifestVirtualFields, ModelManifest } from './model-manifest.js';
|
|
2
|
+
import type { DataAccessLayer, InstanceMethod } from './model-types.js';
|
|
3
|
+
interface CreateModelOptions {
|
|
4
|
+
staticProperties?: Record<string, unknown>;
|
|
5
|
+
}
|
|
6
|
+
type EmptyRecord = Record<never, never>;
|
|
7
|
+
type EmptyInstanceMethods = Record<never, InstanceMethod>;
|
|
8
|
+
type MethodRecord = Record<string, (...args: unknown[]) => unknown>;
|
|
9
|
+
type ModelConstructorWithStatics<Manifest extends ModelManifest, ExtraStatics extends Record<string, unknown> = EmptyRecord> = InferConstructor<Manifest> & ExtraStatics;
|
|
10
|
+
/**
|
|
11
|
+
* Merge additional static/instance methods into a manifest type.
|
|
12
|
+
*
|
|
13
|
+
* This is used when you define methods separately from the manifest
|
|
14
|
+
* (e.g., in models/*.ts files) and need to combine them.
|
|
15
|
+
*
|
|
16
|
+
* The conditional checks handle the case where no methods are provided
|
|
17
|
+
* (empty objects) by using `unknown` to avoid adding empty properties.
|
|
18
|
+
*
|
|
19
|
+
* Uses intersection to preserve the original manifest's relations type.
|
|
20
|
+
*/
|
|
21
|
+
type MergeManifestMethods<Manifest extends ModelManifest, StaticMethods extends object, InstanceMethods extends object> = Manifest & (keyof StaticMethods extends never ? unknown : {
|
|
22
|
+
staticMethods: StaticMethods;
|
|
23
|
+
}) & (keyof InstanceMethods extends never ? unknown : {
|
|
24
|
+
instanceMethods: InstanceMethods;
|
|
25
|
+
});
|
|
26
|
+
/**
|
|
27
|
+
* Create a lazy proxy for a model manifest. This is the internal implementation
|
|
28
|
+
* used by defineModel(). The proxy forwards all property access to the initialized
|
|
29
|
+
* model from the DAL registry.
|
|
30
|
+
*
|
|
31
|
+
* @param manifest - Model manifest definition
|
|
32
|
+
* @param options - Optional static properties to attach
|
|
33
|
+
* @returns Typed model proxy constructor
|
|
34
|
+
*
|
|
35
|
+
* @internal Most code should use defineModel() instead
|
|
36
|
+
*/
|
|
37
|
+
export declare function createModelProxy<Manifest extends ModelManifest>(manifest: Manifest, options?: CreateModelOptions): InferConstructor<Manifest>;
|
|
38
|
+
/**
|
|
39
|
+
* Initialize all registered manifest-based models
|
|
40
|
+
* Called by bootstrap after DAL is connected
|
|
41
|
+
*
|
|
42
|
+
* @param dal - Connected DAL instance
|
|
43
|
+
*/
|
|
44
|
+
export declare function initializeManifestModels(dal: DataAccessLayer): void;
|
|
45
|
+
/**
|
|
46
|
+
* Helper to define static methods with properly typed `this` context.
|
|
47
|
+
*
|
|
48
|
+
* This is a zero-cost abstraction - at runtime it just returns the methods object.
|
|
49
|
+
* At compile-time, it provides TypeScript with ThisType<InferConstructor<Manifest> & Methods>
|
|
50
|
+
* so that `this` inside your methods is correctly typed as the model constructor.
|
|
51
|
+
*
|
|
52
|
+
* @param manifest - Model manifest to derive types from
|
|
53
|
+
* @param methods - Static methods with `this` bound to the model constructor
|
|
54
|
+
* @returns The methods object with preserved typing
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* const reviewStaticMethods = defineStaticMethods(reviewManifest, {
|
|
58
|
+
* async create(this: ReviewModel, data: ReviewInputObject) {
|
|
59
|
+
* // `this` is fully typed as ReviewModel
|
|
60
|
+
* const thing = await this.findOrCreateThing(data);
|
|
61
|
+
* return this.createFirstRevision(...);
|
|
62
|
+
* }
|
|
63
|
+
* });
|
|
64
|
+
*/
|
|
65
|
+
export declare function defineStaticMethods<Manifest extends ModelManifest, Methods extends object>(_manifest: Manifest, methods: Methods & ThisType<InferConstructor<Manifest> & Methods>): Methods;
|
|
66
|
+
/**
|
|
67
|
+
* Helper to define instance methods with properly typed `this` context.
|
|
68
|
+
*
|
|
69
|
+
* This is a zero-cost abstraction - at runtime it just returns the methods object.
|
|
70
|
+
* At compile-time, it provides TypeScript with ThisType<InferInstance<Manifest> & Methods>
|
|
71
|
+
* so that `this` inside your methods is correctly typed as the model instance.
|
|
72
|
+
*
|
|
73
|
+
* @param manifest - Model manifest to derive types from
|
|
74
|
+
* @param methods - Instance methods with `this` bound to model instances
|
|
75
|
+
* @returns The methods object with preserved typing
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* const reviewInstanceMethods = defineInstanceMethods(reviewManifest, {
|
|
79
|
+
* populateUserInfo(this: ReviewInstance, user: UserAccessContext) {
|
|
80
|
+
* // `this` is fully typed as ReviewInstance
|
|
81
|
+
* if (user.id === this.createdBy) {
|
|
82
|
+
* this.userIsAuthor = true;
|
|
83
|
+
* }
|
|
84
|
+
* }
|
|
85
|
+
* });
|
|
86
|
+
*/
|
|
87
|
+
export declare function defineInstanceMethods<Manifest extends ModelManifest, Methods extends Record<string, InstanceMethod>>(_manifest: Manifest, methods: Methods & ThisType<InferInstance<Manifest> & Methods>): Methods;
|
|
88
|
+
/**
|
|
89
|
+
* Convenience wrapper around {@link createModelProxy} that preserves manifest-derived
|
|
90
|
+
* typing while layering on additional statics in a type-safe manner.
|
|
91
|
+
*
|
|
92
|
+
* This is purely a TypeScript ergonomic helper. At runtime it forwards directly
|
|
93
|
+
* to {@link createModelProxy}, ensuring the emitted JavaScript stays unchanged.
|
|
94
|
+
*
|
|
95
|
+
* @param manifest - Model manifest definition
|
|
96
|
+
* @returns Typed model constructor
|
|
97
|
+
*/
|
|
98
|
+
export declare function defineModel<Manifest extends ModelManifest>(manifest: Manifest): ModelConstructorWithStatics<Manifest>;
|
|
99
|
+
export declare function defineModel<Manifest extends ModelManifest, ExtraStatics extends Record<string, unknown> = EmptyRecord, StaticMethods extends object = EmptyStaticMethods, InstanceMethods extends Record<string, InstanceMethod> = EmptyInstanceMethods>(manifest: Manifest, options?: {
|
|
100
|
+
statics?: ExtraStatics;
|
|
101
|
+
staticMethods?: StaticMethods;
|
|
102
|
+
instanceMethods?: InstanceMethods;
|
|
103
|
+
}): ModelConstructorWithStatics<MergeManifestMethods<Manifest, StaticMethods, InstanceMethods>, ExtraStatics>;
|
|
104
|
+
/**
|
|
105
|
+
* Convenience helper to infer instance type from a manifest with additional instance methods.
|
|
106
|
+
*
|
|
107
|
+
* This is much cleaner than manually calling InferInstance<MergeManifestMethods<...>>.
|
|
108
|
+
* Use this in your manifest file to define the instance type.
|
|
109
|
+
*
|
|
110
|
+
* @template Manifest - The model manifest
|
|
111
|
+
* @template InstanceMethods - Additional instance methods (optional, defaults to no methods)
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* // In models/manifests/review.ts
|
|
115
|
+
* export type ReviewInstance = ManifestInstance<typeof reviewManifest, ReviewInstanceMethods>;
|
|
116
|
+
*/
|
|
117
|
+
export type ManifestInstance<Manifest extends ModelManifest, InstanceMethods extends object = Record<never, InstanceMethod>> = InferInstance<MergeManifestMethods<Manifest, EmptyStaticMethods, InstanceMethods>>;
|
|
118
|
+
/**
|
|
119
|
+
* Convenience helper to infer model constructor type from a manifest with additional methods.
|
|
120
|
+
*
|
|
121
|
+
* This is much cleaner than manually calling InferConstructor<MergeManifestMethods<...>>.
|
|
122
|
+
* Use this in your manifest file to define the model constructor type.
|
|
123
|
+
*
|
|
124
|
+
* @template Manifest - The model manifest
|
|
125
|
+
* @template StaticMethods - Additional static methods (optional, defaults to no methods)
|
|
126
|
+
* @template InstanceMethods - Additional instance methods (optional, defaults to no methods)
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* // In models/manifests/review.ts
|
|
130
|
+
* export type ReviewModel = ManifestModel<
|
|
131
|
+
* typeof reviewManifest,
|
|
132
|
+
* ReviewStaticMethods,
|
|
133
|
+
* ReviewInstanceMethods
|
|
134
|
+
* >;
|
|
135
|
+
*/
|
|
136
|
+
export type ManifestModel<Manifest extends ModelManifest, StaticMethods extends object = EmptyStaticMethods, InstanceMethods extends object = Record<never, InstanceMethod>> = InferConstructor<MergeManifestMethods<Manifest, StaticMethods, InstanceMethods>>;
|
|
137
|
+
/**
|
|
138
|
+
* Bundle of commonly used manifest-derived types.
|
|
139
|
+
*
|
|
140
|
+
* This keeps manifest files terse by collecting the different inferred types
|
|
141
|
+
* in one place, so authors don't have to declare separate aliases for the
|
|
142
|
+
* base model, base instance, data shape, and virtual fields.
|
|
143
|
+
*/
|
|
144
|
+
export type ManifestTypes<Manifest extends ModelManifest, StaticMethods extends object = EmptyStaticMethods, InstanceMethods extends object = Record<never, InstanceMethod>, RelationFields extends object = Record<never, never>> = {
|
|
145
|
+
/**
|
|
146
|
+
* Instance type including any relation fields added via intersection.
|
|
147
|
+
*/
|
|
148
|
+
Instance: ManifestInstance<Manifest, InstanceMethods> & RelationFields;
|
|
149
|
+
/**
|
|
150
|
+
* Model constructor type including additional static/instance methods.
|
|
151
|
+
*/
|
|
152
|
+
Model: ManifestModel<Manifest, StaticMethods, InstanceMethods>;
|
|
153
|
+
/**
|
|
154
|
+
* Base model constructor type without custom statics/instance methods.
|
|
155
|
+
*/
|
|
156
|
+
BaseModel: InferConstructor<Manifest>;
|
|
157
|
+
/**
|
|
158
|
+
* Base instance type without custom instance methods or relations.
|
|
159
|
+
*/
|
|
160
|
+
BaseInstance: InferInstance<Manifest>;
|
|
161
|
+
/**
|
|
162
|
+
* Persisted data shape inferred from the schema.
|
|
163
|
+
*/
|
|
164
|
+
Data: InferData<Manifest['schema']>;
|
|
165
|
+
/**
|
|
166
|
+
* Virtual fields inferred from the schema.
|
|
167
|
+
*/
|
|
168
|
+
Virtual: ManifestVirtualFields<Manifest>;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* Bundle convenience that keeps manifest files terse without circular aliases.
|
|
172
|
+
*
|
|
173
|
+
* Provide the relation fields and the raw static/instance method maps, and
|
|
174
|
+
* this helper derives the typed instance/model along with data/virtual shapes.
|
|
175
|
+
*/
|
|
176
|
+
export type ManifestBundle<Manifest extends ModelManifest, RelationFields extends object = Record<never, never>, StaticMethods extends MethodRecord = EmptyRecord, InstanceMethods extends Record<string, InstanceMethod> = EmptyInstanceMethods> = ManifestTypes<Manifest, StaticMethods, InstanceMethods, RelationFields> & {
|
|
177
|
+
/**
|
|
178
|
+
* Instance methods with correctly typed `this` context.
|
|
179
|
+
*/
|
|
180
|
+
InstanceMethods: InstanceMethodsFrom<Manifest, InstanceMethods, RelationFields>;
|
|
181
|
+
/**
|
|
182
|
+
* Static methods with correctly typed `this` context.
|
|
183
|
+
*/
|
|
184
|
+
StaticMethods: StaticMethodsFrom<Manifest, StaticMethods, InstanceMethods, RelationFields>;
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Map a methods object to include the correct `this` type for model statics.
|
|
188
|
+
*
|
|
189
|
+
* This lets manifest authors describe their method signatures without
|
|
190
|
+
* manually repeating `this: ModelType` on every function. The returned type
|
|
191
|
+
* still exposes the fully-typed `this` context to callers.
|
|
192
|
+
*/
|
|
193
|
+
export type StaticMethodsFrom<Manifest extends ModelManifest, Methods extends MethodRecord, InstanceMethods extends Record<string, InstanceMethod> = EmptyInstanceMethods, RelationFields extends object = Record<never, never>> = {
|
|
194
|
+
[K in keyof Methods]: (this: ManifestTypes<Manifest, Methods, InstanceMethods, RelationFields>['Model'] & Methods, ...args: Parameters<Methods[K]>) => ReturnType<Methods[K]>;
|
|
195
|
+
};
|
|
196
|
+
/**
|
|
197
|
+
* Map a methods object to include the correct `this` type for model instances.
|
|
198
|
+
*
|
|
199
|
+
* Similar to {@link StaticMethodsFrom}, but targets instance methods so they
|
|
200
|
+
* receive the fully-typed instance (including relation fields) as their
|
|
201
|
+
* `this` context without extra boilerplate.
|
|
202
|
+
*/
|
|
203
|
+
export type InstanceMethodsFrom<Manifest extends ModelManifest, Methods extends Record<string, InstanceMethod>, RelationFields extends object = Record<never, never>> = {
|
|
204
|
+
[K in keyof Methods]: (this: ManifestTypes<Manifest, EmptyStaticMethods, Methods, RelationFields>['Instance'] & Methods, ...args: Parameters<Methods[K]>) => ReturnType<Methods[K]>;
|
|
205
|
+
};
|
|
206
|
+
export type { ModelConstructorWithStatics, MergeManifestMethods };
|
|
207
|
+
//# sourceMappingURL=create-model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-model.d.ts","sourceRoot":"","sources":["../../src/lib/create-model.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,kBAAkB,EAClB,gBAAgB,EAChB,SAAS,EACT,aAAa,EACb,qBAAqB,EACrB,aAAa,EACd,MAAM,qBAAqB,CAAC;AAE7B,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAc,MAAM,kBAAkB,CAAC;AAIpF,UAAU,kBAAkB;IAC1B,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC5C;AAED,KAAK,WAAW,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AACxC,KAAK,oBAAoB,GAAG,MAAM,CAAC,KAAK,EAAE,cAAc,CAAC,CAAC;AAC1D,KAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,CAAC;AAEpE,KAAK,2BAA2B,CAC9B,QAAQ,SAAS,aAAa,EAC9B,YAAY,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,WAAW,IACxD,gBAAgB,CAAC,QAAQ,CAAC,GAAG,YAAY,CAAC;AAE9C;;;;;;;;;;GAUG;AACH,KAAK,oBAAoB,CACvB,QAAQ,SAAS,aAAa,EAC9B,aAAa,SAAS,MAAM,EAC5B,eAAe,SAAS,MAAM,IAC5B,QAAQ,GACV,CAAC,MAAM,aAAa,SAAS,KAAK,GAAG,OAAO,GAAG;IAAE,aAAa,EAAE,aAAa,CAAA;CAAE,CAAC,GAChF,CAAC,MAAM,eAAe,SAAS,KAAK,GAAG,OAAO,GAAG;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7B,QAAQ,SAAS,aAAa,EAC9B,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,EAC9C,cAAc,SAAS,MAAM,GAAG,MAAM,CAAC,KAAK,EAAE,KAAK,CAAC,IAClD;KACD,CAAC,IAAI,MAAM,OAAO,GAAG,CACpB,IAAI,EAAE,aAAa,CAAC,QAAQ,EAAE,kBAAkB,EAAE,OAAO,EAAE,cAAc,CAAC,CAAC,UAAU,CAAC,GACpF,OAAO,EACT,GAAG,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAC5B,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;CAC5B,CAAC;AAEF,YAAY,EAAE,2BAA2B,EAAE,oBAAoB,EAAE,CAAC"}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { createFilterWhereStatics } from './filter-where.js';
|
|
2
|
+
import { getRegisteredModel, registerModelHandle } from './model-handle.js';
|
|
3
|
+
import { initializeModel } from './model-initializer.js';
|
|
4
|
+
import { getAllManifests, registerManifest } from './model-registry.js';
|
|
5
|
+
const filterWhereStaticsByTable = new Map();
|
|
6
|
+
/**
|
|
7
|
+
* Initialize a model from its manifest
|
|
8
|
+
* Called by bootstrap after DAL is ready
|
|
9
|
+
*
|
|
10
|
+
* @param manifest - Model manifest definition
|
|
11
|
+
* @param dal - DAL instance to use
|
|
12
|
+
* @returns Initialized model constructor
|
|
13
|
+
*/
|
|
14
|
+
function initializeFromManifest(manifest, dal) {
|
|
15
|
+
// Extract relation definitions, resolving target functions to targetTable strings
|
|
16
|
+
const relationDefinitions = manifest.relations
|
|
17
|
+
? manifest.relations.map(relation => {
|
|
18
|
+
const { target, ...rest } = relation;
|
|
19
|
+
// If target is a function, call it to get the model reference and extract tableName
|
|
20
|
+
if (typeof target === 'function') {
|
|
21
|
+
try {
|
|
22
|
+
const targetModel = target();
|
|
23
|
+
if (targetModel && typeof targetModel.tableName === 'string') {
|
|
24
|
+
// Use resolved tableName, but don't override explicit targetTable
|
|
25
|
+
return {
|
|
26
|
+
...rest,
|
|
27
|
+
targetTable: rest.targetTable ?? targetModel.tableName,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// Target resolution failed - model may not be registered yet
|
|
33
|
+
// Fall back to explicit targetTable if provided
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return rest;
|
|
37
|
+
})
|
|
38
|
+
: null;
|
|
39
|
+
// Convert manifest to initializeModel options format
|
|
40
|
+
const options = {
|
|
41
|
+
dal,
|
|
42
|
+
baseTable: manifest.tableName,
|
|
43
|
+
schema: manifest.schema,
|
|
44
|
+
withRevision: manifest.hasRevisions
|
|
45
|
+
? {
|
|
46
|
+
static: ['createFirstRevision', 'getNotStaleOrDeleted', 'getMultipleNotStaleOrDeleted'],
|
|
47
|
+
instance: ['deleteAllRevisions'],
|
|
48
|
+
}
|
|
49
|
+
: false,
|
|
50
|
+
relations: relationDefinitions,
|
|
51
|
+
views: manifest.views || undefined,
|
|
52
|
+
};
|
|
53
|
+
if (manifest.camelToSnake) {
|
|
54
|
+
options.camelToSnake = manifest.camelToSnake;
|
|
55
|
+
}
|
|
56
|
+
if (manifest.staticMethods) {
|
|
57
|
+
options.staticMethods = manifest.staticMethods;
|
|
58
|
+
}
|
|
59
|
+
if (manifest.instanceMethods) {
|
|
60
|
+
options.instanceMethods = manifest.instanceMethods;
|
|
61
|
+
}
|
|
62
|
+
const { model } = initializeModel(options);
|
|
63
|
+
const filterStatics = filterWhereStaticsByTable.get(manifest.tableName);
|
|
64
|
+
if (filterStatics) {
|
|
65
|
+
Object.assign(model, filterStatics);
|
|
66
|
+
}
|
|
67
|
+
if (!Object.hasOwn(model, 'createFromRow')) {
|
|
68
|
+
Object.defineProperty(model, 'createFromRow', {
|
|
69
|
+
value(row) {
|
|
70
|
+
const runtime = model;
|
|
71
|
+
if (typeof runtime._createInstance !== 'function') {
|
|
72
|
+
throw new Error(`Model "${manifest.tableName}" does not expose _createInstance; bootstrap may be incomplete.`);
|
|
73
|
+
}
|
|
74
|
+
return runtime._createInstance(row);
|
|
75
|
+
},
|
|
76
|
+
enumerable: false,
|
|
77
|
+
writable: false,
|
|
78
|
+
configurable: false,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return model;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Create a lazy proxy for a model manifest. This is the internal implementation
|
|
85
|
+
* used by defineModel(). The proxy forwards all property access to the initialized
|
|
86
|
+
* model from the DAL registry.
|
|
87
|
+
*
|
|
88
|
+
* @param manifest - Model manifest definition
|
|
89
|
+
* @param options - Optional static properties to attach
|
|
90
|
+
* @returns Typed model proxy constructor
|
|
91
|
+
*
|
|
92
|
+
* @internal Most code should use defineModel() instead
|
|
93
|
+
*/
|
|
94
|
+
export function createModelProxy(manifest, options = {}) {
|
|
95
|
+
// Register the manifest in the global registry
|
|
96
|
+
registerManifest(manifest);
|
|
97
|
+
const filterWhereStatics = createFilterWhereStatics(manifest);
|
|
98
|
+
filterWhereStaticsByTable.set(manifest.tableName, filterWhereStatics);
|
|
99
|
+
const providedStatic = options.staticProperties ?? {};
|
|
100
|
+
const getModel = () => {
|
|
101
|
+
return getRegisteredModel(manifest.tableName);
|
|
102
|
+
};
|
|
103
|
+
const createFromRow = (row) => {
|
|
104
|
+
const model = getModel();
|
|
105
|
+
if (typeof model._createInstance !== 'function') {
|
|
106
|
+
throw new Error(`Model "${manifest.tableName}" does not expose _createInstance; bootstrap may be incomplete.`);
|
|
107
|
+
}
|
|
108
|
+
return model._createInstance(row);
|
|
109
|
+
};
|
|
110
|
+
const staticProperties = { ...filterWhereStatics, ...providedStatic, createFromRow };
|
|
111
|
+
const staticPropertyKeys = new Set(Object.keys(staticProperties));
|
|
112
|
+
// Create a function target so the proxy can be used as a constructor
|
|
113
|
+
function TargetConstructor() {
|
|
114
|
+
// Intentionally empty: runtime constructor is provided by initialized model.
|
|
115
|
+
}
|
|
116
|
+
const target = TargetConstructor;
|
|
117
|
+
for (const [prop, value] of Object.entries(staticProperties)) {
|
|
118
|
+
Object.defineProperty(target, prop, {
|
|
119
|
+
value,
|
|
120
|
+
enumerable: true,
|
|
121
|
+
writable: false,
|
|
122
|
+
configurable: false,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
// Return a proxy that forwards to the initialized model
|
|
126
|
+
// Model will be initialized by bootstrap before any app code runs
|
|
127
|
+
const proxy = new Proxy(target, {
|
|
128
|
+
get(_target, prop) {
|
|
129
|
+
if (staticPropertyKeys.has(String(prop))) {
|
|
130
|
+
return Reflect.get(target, prop);
|
|
131
|
+
}
|
|
132
|
+
const model = getModel();
|
|
133
|
+
// Access the property on the model
|
|
134
|
+
const value = model[prop];
|
|
135
|
+
// Bind methods to the model instance
|
|
136
|
+
if (typeof value === 'function') {
|
|
137
|
+
return value.bind(model);
|
|
138
|
+
}
|
|
139
|
+
return value;
|
|
140
|
+
},
|
|
141
|
+
// Support for 'new' operator
|
|
142
|
+
construct(_target, args) {
|
|
143
|
+
const model = getModel();
|
|
144
|
+
return new model(...args);
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
registerModelHandle(proxy, manifest.tableName);
|
|
148
|
+
return proxy;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Initialize all registered manifest-based models
|
|
152
|
+
* Called by bootstrap after DAL is connected
|
|
153
|
+
*
|
|
154
|
+
* @param dal - Connected DAL instance
|
|
155
|
+
*/
|
|
156
|
+
export function initializeManifestModels(dal) {
|
|
157
|
+
const manifests = getAllManifests();
|
|
158
|
+
for (const [tableName, manifest] of manifests) {
|
|
159
|
+
// Check if already initialized by checking DAL registry
|
|
160
|
+
try {
|
|
161
|
+
dal.getModel(tableName);
|
|
162
|
+
// Already initialized, skip
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
// Not initialized yet, proceed
|
|
167
|
+
}
|
|
168
|
+
initializeFromManifest(manifest, dal);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Helper to define static methods with properly typed `this` context.
|
|
173
|
+
*
|
|
174
|
+
* This is a zero-cost abstraction - at runtime it just returns the methods object.
|
|
175
|
+
* At compile-time, it provides TypeScript with ThisType<InferConstructor<Manifest> & Methods>
|
|
176
|
+
* so that `this` inside your methods is correctly typed as the model constructor.
|
|
177
|
+
*
|
|
178
|
+
* @param manifest - Model manifest to derive types from
|
|
179
|
+
* @param methods - Static methods with `this` bound to the model constructor
|
|
180
|
+
* @returns The methods object with preserved typing
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* const reviewStaticMethods = defineStaticMethods(reviewManifest, {
|
|
184
|
+
* async create(this: ReviewModel, data: ReviewInputObject) {
|
|
185
|
+
* // `this` is fully typed as ReviewModel
|
|
186
|
+
* const thing = await this.findOrCreateThing(data);
|
|
187
|
+
* return this.createFirstRevision(...);
|
|
188
|
+
* }
|
|
189
|
+
* });
|
|
190
|
+
*/
|
|
191
|
+
export function defineStaticMethods(_manifest, methods) {
|
|
192
|
+
return methods;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Helper to define instance methods with properly typed `this` context.
|
|
196
|
+
*
|
|
197
|
+
* This is a zero-cost abstraction - at runtime it just returns the methods object.
|
|
198
|
+
* At compile-time, it provides TypeScript with ThisType<InferInstance<Manifest> & Methods>
|
|
199
|
+
* so that `this` inside your methods is correctly typed as the model instance.
|
|
200
|
+
*
|
|
201
|
+
* @param manifest - Model manifest to derive types from
|
|
202
|
+
* @param methods - Instance methods with `this` bound to model instances
|
|
203
|
+
* @returns The methods object with preserved typing
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* const reviewInstanceMethods = defineInstanceMethods(reviewManifest, {
|
|
207
|
+
* populateUserInfo(this: ReviewInstance, user: UserAccessContext) {
|
|
208
|
+
* // `this` is fully typed as ReviewInstance
|
|
209
|
+
* if (user.id === this.createdBy) {
|
|
210
|
+
* this.userIsAuthor = true;
|
|
211
|
+
* }
|
|
212
|
+
* }
|
|
213
|
+
* });
|
|
214
|
+
*/
|
|
215
|
+
export function defineInstanceMethods(_manifest, methods) {
|
|
216
|
+
return methods;
|
|
217
|
+
}
|
|
218
|
+
export function defineModel(manifest, options) {
|
|
219
|
+
const extraStatics = options?.statics;
|
|
220
|
+
const manifestWithMethods = {
|
|
221
|
+
...manifest,
|
|
222
|
+
...(options?.staticMethods ? { staticMethods: options.staticMethods } : {}),
|
|
223
|
+
...(options?.instanceMethods ? { instanceMethods: options.instanceMethods } : {}),
|
|
224
|
+
};
|
|
225
|
+
const model = createModelProxy(manifestWithMethods, {
|
|
226
|
+
staticProperties: extraStatics,
|
|
227
|
+
});
|
|
228
|
+
return model;
|
|
229
|
+
}
|
|
230
|
+
//# sourceMappingURL=create-model.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-model.js","sourceRoot":"","sources":["../../src/lib/create-model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAE5E,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AASzD,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAGxE,MAAM,yBAAyB,GAAG,IAAI,GAAG,EAAuD,CAAC;AAkCjG;;;;;;;GAOG;AACH,SAAS,sBAAsB,CAC7B,QAAkB,EAClB,GAAoB;IAMpB,kFAAkF;IAClF,MAAM,mBAAmB,GAAG,QAAQ,CAAC,SAAS;QAC5C,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE;YAChC,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,QAAQ,CAAC;YAErC,oFAAoF;YACpF,IA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