@bg-dev/nuxt-zenstack 0.0.4 → 0.0.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/module.d.mts +2 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +42 -6
- package/dist/runtime/composables/common.d.ts +3 -5
- package/dist/runtime/composables/useZenstackRealtime/index.d.ts +6 -0
- package/dist/runtime/composables/useZenstackRealtime/index.js +53 -0
- package/dist/runtime/server/api/models/[model]/[id].delete.d.ts +1 -1
- package/dist/runtime/server/api/models/[model]/[id].delete.js +5 -12
- package/dist/runtime/server/api/models/[model]/[id].get.d.ts +1 -1
- package/dist/runtime/server/api/models/[model]/[id].get.js +5 -13
- package/dist/runtime/server/api/models/[model]/[id].patch.d.ts +1 -1
- package/dist/runtime/server/api/models/[model]/[id].patch.js +5 -15
- package/dist/runtime/server/api/models/[model]/index.get.d.ts +2 -2
- package/dist/runtime/server/api/models/[model]/index.get.js +9 -10
- package/dist/runtime/server/api/models/[model]/index.post.d.ts +1 -1
- package/dist/runtime/server/api/models/[model]/index.post.js +5 -12
- package/dist/runtime/server/routes/realtime.d.ts +2 -0
- package/dist/runtime/server/routes/realtime.js +18 -0
- package/dist/runtime/server/utils/helpers.d.ts +7 -0
- package/dist/runtime/server/utils/helpers.js +34 -0
- package/dist/runtime/server/utils/index.d.ts +14 -7
- package/dist/runtime/server/utils/index.js +12 -42
- package/dist/runtime/server/utils/{error.d.ts → operations/common.d.ts} +2 -1
- package/dist/runtime/server/utils/{error.js → operations/common.js} +3 -0
- package/dist/runtime/server/utils/operations/create.d.ts +10 -0
- package/dist/runtime/server/utils/operations/create.js +15 -0
- package/dist/runtime/server/utils/operations/delete.d.ts +10 -0
- package/dist/runtime/server/utils/operations/delete.js +16 -0
- package/dist/runtime/server/utils/operations/findMany.d.ts +14 -0
- package/dist/runtime/server/utils/operations/findMany.js +14 -0
- package/dist/runtime/server/utils/operations/findUnique.d.ts +11 -0
- package/dist/runtime/server/utils/operations/findUnique.js +14 -0
- package/dist/runtime/server/utils/operations/update.d.ts +11 -0
- package/dist/runtime/server/utils/operations/update.js +19 -0
- package/dist/runtime/server/utils/parsers.d.ts +1 -0
- package/dist/runtime/server/utils/parsers.js +7 -1
- package/package.json +1 -1
package/dist/module.d.mts
CHANGED
|
@@ -4,6 +4,8 @@ import { FetchPolicy } from '../dist/runtime/composables/common.js';
|
|
|
4
4
|
interface ModuleOptions {
|
|
5
5
|
apiPath: string;
|
|
6
6
|
fetchPolicy: FetchPolicy;
|
|
7
|
+
realtime: boolean;
|
|
8
|
+
baseUrl: string;
|
|
7
9
|
}
|
|
8
10
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
9
11
|
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { defineNuxtModule, createResolver, addTemplate, addServerTemplate, addTypeTemplate, addServerHandler, addImportsDir } from '@nuxt/kit';
|
|
1
|
+
import { defineNuxtModule, createResolver, addTemplate, addServerTemplate, addTypeTemplate, addServerHandler, addImportsDir, addServerImports, addImports } from '@nuxt/kit';
|
|
2
2
|
import { joinURL } from 'ufo';
|
|
3
3
|
import { defu } from 'defu';
|
|
4
4
|
|
|
5
5
|
const name = "@bg-dev/nuxt-zenstack";
|
|
6
|
-
const version = "0.0.
|
|
6
|
+
const version = "0.0.5";
|
|
7
7
|
|
|
8
8
|
const module$1 = defineNuxtModule({
|
|
9
9
|
meta: {
|
|
@@ -14,13 +14,17 @@ const module$1 = defineNuxtModule({
|
|
|
14
14
|
// Default configuration options of the Nuxt module
|
|
15
15
|
defaults: {
|
|
16
16
|
apiPath: "/api/_zenstack",
|
|
17
|
-
fetchPolicy: "cache-first"
|
|
17
|
+
fetchPolicy: "cache-first",
|
|
18
|
+
realtime: false,
|
|
19
|
+
baseUrl: "http://localhost:3000"
|
|
18
20
|
},
|
|
19
21
|
setup(_options, _nuxt) {
|
|
20
22
|
_nuxt.options.runtimeConfig.public = defu(_nuxt.options.runtimeConfig.public, {
|
|
21
23
|
zenstack: {
|
|
22
24
|
apiPath: _options.apiPath,
|
|
23
|
-
fetchPolicy: _options.fetchPolicy
|
|
25
|
+
fetchPolicy: _options.fetchPolicy,
|
|
26
|
+
realtime: _options.realtime,
|
|
27
|
+
baseUrl: _options.baseUrl
|
|
24
28
|
}
|
|
25
29
|
});
|
|
26
30
|
const resolver = createResolver(import.meta.url);
|
|
@@ -49,6 +53,7 @@ import { ORMError, RejectedByPolicyReason, ORMErrorReason } from '@zenstackhq/or
|
|
|
49
53
|
type ItemGetPayload<Zmodel extends $Zmodel, Args extends SelectIncludeOmit<SchemaType, Zmodel, true>, Options extends QueryOptions<SchemaType> = QueryOptions<SchemaType>> = SimplifiedPlainResult<SchemaType, Zmodel, Args, Options>
|
|
50
54
|
|
|
51
55
|
export type $Zschema = SchemaType
|
|
56
|
+
export type $Zclient = ClientContract<SchemaType>
|
|
52
57
|
export type $Zmodel = keyof SchemaType['models']
|
|
53
58
|
export type $Zdef = ModelDef
|
|
54
59
|
export type $Zoperations<Zmodel extends $Zmodel> = ModelOperations<SchemaType, Zmodel>
|
|
@@ -57,8 +62,13 @@ export type $Zinclude<Zmodel extends $Zmodel> = IncludeInput<SchemaType, Zmodel>
|
|
|
57
62
|
export type $Zitem<Zmodel extends $Zmodel, Zinclude extends $Zinclude = undefined> = ItemGetPayload<Zmodel, { include: Zinclude }>
|
|
58
63
|
export type $Zwhere<Zmodel extends $Zmodel> = WhereInput<SchemaType, Zmodel> | undefined
|
|
59
64
|
export type $ZorderBy<Zmodel extends $Zmodel> = FindManyArgs<SchemaType, Zmodel>['orderBy'] | undefined
|
|
60
|
-
|
|
61
|
-
|
|
65
|
+
|
|
66
|
+
type NonRelationalFields<Zmodel extends $Zmodel> = keyof $Zitem<Zmodel>
|
|
67
|
+
type CreateData<Zmodel extends $Zmodel> = CreateArgs<SchemaType, Zmodel>['data']
|
|
68
|
+
type UpdateData<Zmodel extends $Zmodel> = UpdateArgs<SchemaType, Zmodel>['data']
|
|
69
|
+
|
|
70
|
+
export type $ZcreateData<Zmodel extends $Zmodel> = Pick<CreateData<Zmodel>, NonRelationalFields<Zmodel>>
|
|
71
|
+
export type $ZupdateData<Zmodel extends $Zmodel> = Pick<UpdateData<Zmodel>, NonRelationalFields<Zmodel>>
|
|
62
72
|
export type $ZormError = ORMError
|
|
63
73
|
export type $Zerror = H3Error<{
|
|
64
74
|
ormErrorReason: ORMErrorReason
|
|
@@ -75,6 +85,18 @@ declare module 'h3' {
|
|
|
75
85
|
}
|
|
76
86
|
}
|
|
77
87
|
}
|
|
88
|
+
|
|
89
|
+
type RealtimeData = {
|
|
90
|
+
action: 'create' | 'update' | 'delete'
|
|
91
|
+
model: $Zmodel
|
|
92
|
+
ids: Array<number | string>
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
declare module 'nitropack' {
|
|
96
|
+
interface NitroRuntimeHooks {
|
|
97
|
+
'zenstack:after_mutation': (action: 'create' | 'update' | 'delete', model: $Zmodel, items: Record<string, unknown>[]) => Promise<void> | void
|
|
98
|
+
}
|
|
99
|
+
}
|
|
78
100
|
`
|
|
79
101
|
});
|
|
80
102
|
addServerHandler({
|
|
@@ -98,6 +120,20 @@ declare module 'h3' {
|
|
|
98
120
|
handler: resolver.resolve("./runtime/server/api/models/[model]/[id].get")
|
|
99
121
|
});
|
|
100
122
|
addImportsDir(resolver.resolve("./runtime/composables"));
|
|
123
|
+
addServerImports({
|
|
124
|
+
from: resolver.resolve("./runtime/server/utils"),
|
|
125
|
+
name: "useZenstack"
|
|
126
|
+
});
|
|
127
|
+
if (_options.realtime) {
|
|
128
|
+
addServerHandler({
|
|
129
|
+
route: joinURL(_options.apiPath, "realtime"),
|
|
130
|
+
handler: resolver.resolve("./runtime/server/routes/realtime")
|
|
131
|
+
});
|
|
132
|
+
addImports({
|
|
133
|
+
from: resolver.resolve("./runtime/composables/useZenstackRealtime"),
|
|
134
|
+
name: "useZenstackRealtime"
|
|
135
|
+
});
|
|
136
|
+
}
|
|
101
137
|
}
|
|
102
138
|
});
|
|
103
139
|
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
+
import type { ModuleOptions } from '../../module.js';
|
|
1
2
|
export type Status = 'idle' | 'pending' | 'success' | 'error';
|
|
2
3
|
export type FetchPolicy = 'cache-first' | 'fetch-only' | 'cache-only' | 'cache-and-fetch';
|
|
3
|
-
export declare function getConfig():
|
|
4
|
-
apiPath: string;
|
|
5
|
-
fetchPolicy: FetchPolicy;
|
|
6
|
-
};
|
|
4
|
+
export declare function getConfig(): ModuleOptions;
|
|
7
5
|
/**
|
|
8
6
|
* @param instance a custom fetch method created by `$fetch.create`
|
|
9
7
|
*/
|
|
10
8
|
export declare function provideFetch(instance: typeof $fetch): void;
|
|
11
|
-
export declare function getFetch(): import("nitropack
|
|
9
|
+
export declare function getFetch(): import("nitropack").$Fetch<unknown, import("nitropack").NitroFetchRequest>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { useZenstackStore } from "../useZenstackStore/index.js";
|
|
2
|
+
import { useZenstackRead } from "../useZenstackRead/index.js";
|
|
3
|
+
import destr from "destr";
|
|
4
|
+
import { joinURL } from "ufo";
|
|
5
|
+
import { getConfig } from "../common.js";
|
|
6
|
+
export function useZenstackRealtime() {
|
|
7
|
+
const config = getConfig();
|
|
8
|
+
const url = joinURL(config.baseUrl, config.apiPath, "realtime");
|
|
9
|
+
const subscriptions = /* @__PURE__ */ new Set();
|
|
10
|
+
let eventSource = null;
|
|
11
|
+
function connect() {
|
|
12
|
+
if (eventSource)
|
|
13
|
+
return;
|
|
14
|
+
eventSource = new EventSource(url);
|
|
15
|
+
eventSource.onmessage = (event) => {
|
|
16
|
+
const data = destr(event.data);
|
|
17
|
+
if (data)
|
|
18
|
+
onMessage(data);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
async function onMessage(data) {
|
|
22
|
+
if (!subscriptions.has(data.model))
|
|
23
|
+
return;
|
|
24
|
+
for (const id of data.ids) {
|
|
25
|
+
if (data.action === "delete") {
|
|
26
|
+
useZenstackStore().deleteOne(data.model, id);
|
|
27
|
+
} else {
|
|
28
|
+
await useZenstackRead(data.model, id, { fetchPolicy: "fetch-only" });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function disconnect() {
|
|
33
|
+
if (eventSource)
|
|
34
|
+
eventSource.close();
|
|
35
|
+
eventSource = null;
|
|
36
|
+
}
|
|
37
|
+
function subscribe(models) {
|
|
38
|
+
for (const model of models)
|
|
39
|
+
subscriptions.add(model);
|
|
40
|
+
if (subscriptions.size > 0)
|
|
41
|
+
connect();
|
|
42
|
+
}
|
|
43
|
+
function unsubscribe(models) {
|
|
44
|
+
for (const model of models)
|
|
45
|
+
subscriptions.delete(model);
|
|
46
|
+
if (subscriptions.size === 0)
|
|
47
|
+
disconnect();
|
|
48
|
+
}
|
|
49
|
+
function getSubscriptions() {
|
|
50
|
+
return Array.from(subscriptions);
|
|
51
|
+
}
|
|
52
|
+
return { subscribe, unsubscribe, getSubscriptions };
|
|
53
|
+
}
|
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
import { defineEventHandler } from "h3";
|
|
2
|
-
import { parseModel, parseId } from "../../../utils/parsers.js";
|
|
3
|
-
import {
|
|
4
|
-
import { createError } from "../../../utils/error.js";
|
|
2
|
+
import { parseModel, parseId, parseClient } from "../../../utils/parsers.js";
|
|
3
|
+
import { useZenstack } from "../../../utils/index.js";
|
|
5
4
|
export default defineEventHandler(async (event) => {
|
|
5
|
+
const client = parseClient(event);
|
|
6
6
|
const model = parseModel(event);
|
|
7
7
|
const id = parseId(event, model);
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
where: {
|
|
11
|
-
id
|
|
12
|
-
}
|
|
13
|
-
}).catch((err) => {
|
|
14
|
-
throw createError(err);
|
|
15
|
-
});
|
|
16
|
-
return { data };
|
|
8
|
+
const zenstack = useZenstack();
|
|
9
|
+
return zenstack.delete({ client, model, id });
|
|
17
10
|
});
|
|
@@ -1,19 +1,11 @@
|
|
|
1
|
-
import { parseModel, parseId, parseReadArgs } from "../../../utils/parsers.js";
|
|
1
|
+
import { parseModel, parseId, parseReadArgs, parseClient } from "../../../utils/parsers.js";
|
|
2
2
|
import { defineEventHandler } from "h3";
|
|
3
|
-
import {
|
|
4
|
-
import { createError } from "../../../utils/error.js";
|
|
3
|
+
import { useZenstack } from "../../../utils/index.js";
|
|
5
4
|
export default defineEventHandler(async (event) => {
|
|
5
|
+
const client = parseClient(event);
|
|
6
6
|
const model = parseModel(event);
|
|
7
7
|
const id = parseId(event, model);
|
|
8
8
|
const readArgs = parseReadArgs(event, model);
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
include: readArgs.include,
|
|
12
|
-
where: {
|
|
13
|
-
id
|
|
14
|
-
}
|
|
15
|
-
}).catch((err) => {
|
|
16
|
-
throw createError(err);
|
|
17
|
-
});
|
|
18
|
-
return { data };
|
|
9
|
+
const zenstack = useZenstack();
|
|
10
|
+
return zenstack.findUnique({ client, model, id, include: readArgs.include });
|
|
19
11
|
});
|
|
@@ -1,21 +1,11 @@
|
|
|
1
|
-
import { parseModel, parseId, parseUpdateArgs } from "../../../utils/parsers.js";
|
|
1
|
+
import { parseModel, parseId, parseUpdateArgs, parseClient } from "../../../utils/parsers.js";
|
|
2
2
|
import { defineEventHandler } from "h3";
|
|
3
|
-
import {
|
|
4
|
-
import { createError } from "../../../utils/error.js";
|
|
3
|
+
import { useZenstack } from "../../../utils/index.js";
|
|
5
4
|
export default defineEventHandler(async (event) => {
|
|
5
|
+
const client = parseClient(event);
|
|
6
6
|
const model = parseModel(event);
|
|
7
7
|
const id = parseId(event, model);
|
|
8
8
|
const updateArgs = await parseUpdateArgs(event, model);
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const data = await operations.update({
|
|
12
|
-
data: updateArgs.data,
|
|
13
|
-
include,
|
|
14
|
-
where: {
|
|
15
|
-
id
|
|
16
|
-
}
|
|
17
|
-
}).catch((err) => {
|
|
18
|
-
throw createError(err);
|
|
19
|
-
});
|
|
20
|
-
return { data };
|
|
9
|
+
const zenstack = useZenstack();
|
|
10
|
+
return zenstack.update({ client, model, id, data: updateArgs.data });
|
|
21
11
|
});
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import { parseModel, parseReadArgs } from "../../../utils/parsers.js";
|
|
1
|
+
import { parseClient, parseModel, parseReadArgs } from "../../../utils/parsers.js";
|
|
2
2
|
import { defineEventHandler } from "h3";
|
|
3
|
-
import {
|
|
4
|
-
import { createError } from "../../../utils/error.js";
|
|
3
|
+
import { useZenstack } from "../../../utils/index.js";
|
|
5
4
|
export default defineEventHandler(async (event) => {
|
|
5
|
+
const client = parseClient(event);
|
|
6
6
|
const model = parseModel(event);
|
|
7
7
|
const readArgs = parseReadArgs(event, model);
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const zenstack = useZenstack();
|
|
9
|
+
return zenstack.findMany({
|
|
10
|
+
client,
|
|
11
|
+
model,
|
|
12
12
|
skip: readArgs.skip,
|
|
13
13
|
take: readArgs.take,
|
|
14
|
+
include: readArgs.include,
|
|
15
|
+
orderBy: readArgs.orderBy,
|
|
14
16
|
where: readArgs.where
|
|
15
|
-
}).catch((err) => {
|
|
16
|
-
throw createError(err);
|
|
17
17
|
});
|
|
18
|
-
return { data };
|
|
19
18
|
});
|
|
@@ -1,17 +1,10 @@
|
|
|
1
|
-
import { parseModel, parseCreateArgs } from "../../../utils/parsers.js";
|
|
1
|
+
import { parseModel, parseCreateArgs, parseClient } from "../../../utils/parsers.js";
|
|
2
2
|
import { defineEventHandler } from "h3";
|
|
3
|
-
import {
|
|
4
|
-
import { createError } from "../../../utils/error.js";
|
|
3
|
+
import { useZenstack } from "../../../utils/index.js";
|
|
5
4
|
export default defineEventHandler(async (event) => {
|
|
5
|
+
const client = parseClient(event);
|
|
6
6
|
const model = parseModel(event);
|
|
7
7
|
const createArgs = await parseCreateArgs(event, model);
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
const data = await operations.create({
|
|
11
|
-
data: createArgs.data,
|
|
12
|
-
include
|
|
13
|
-
}).catch((err) => {
|
|
14
|
-
throw createError(err);
|
|
15
|
-
});
|
|
16
|
-
return { data };
|
|
8
|
+
const zenstack = useZenstack();
|
|
9
|
+
return zenstack.create({ client, model, data: createArgs.data });
|
|
17
10
|
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineEventHandler, createEventStream } from "h3";
|
|
2
|
+
import { useNitroApp } from "nitropack/runtime";
|
|
3
|
+
export default defineEventHandler(async (event) => {
|
|
4
|
+
const eventStream = createEventStream(event);
|
|
5
|
+
const nitroApp = useNitroApp();
|
|
6
|
+
nitroApp.hooks.hook("zenstack:after_mutation", (action, model, items) => {
|
|
7
|
+
const message = {
|
|
8
|
+
model,
|
|
9
|
+
action,
|
|
10
|
+
ids: items.map((item) => item.id)
|
|
11
|
+
};
|
|
12
|
+
eventStream.push(JSON.stringify(message));
|
|
13
|
+
});
|
|
14
|
+
eventStream.onClosed(async () => {
|
|
15
|
+
await eventStream.close();
|
|
16
|
+
});
|
|
17
|
+
return eventStream.send();
|
|
18
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { $Zmodel, $Zdef, $ZcreateData, $ZupdateData } from '#build/types/nuxt-zenstack';
|
|
2
|
+
export declare function getModelDef(model: $Zmodel): $Zdef;
|
|
3
|
+
export declare function isIdString(model: $Zmodel): boolean;
|
|
4
|
+
export declare function getModels(): $Zmodel[];
|
|
5
|
+
export declare function isValidModel(model: $Zmodel): boolean;
|
|
6
|
+
export declare function sanitizeCreateData<Zmodel extends $Zmodel>(model: Zmodel, data: $ZcreateData<Zmodel>): $ZcreateData<Zmodel>;
|
|
7
|
+
export declare function sanitizeUpdateData<Zmodel extends $Zmodel>(model: Zmodel, data: $ZupdateData<Zmodel>): $ZupdateData<Zmodel>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { zenstackSchema } from "nuxt-zenstack-server.mjs";
|
|
2
|
+
export function getModelDef(model) {
|
|
3
|
+
return zenstackSchema.models[model];
|
|
4
|
+
}
|
|
5
|
+
export function isIdString(model) {
|
|
6
|
+
const modelDef = getModelDef(model);
|
|
7
|
+
if (!modelDef.uniqueFields.id)
|
|
8
|
+
throw new Error(`Model ${model.toString()} does not have an id as a unique field`);
|
|
9
|
+
return modelDef.uniqueFields.id.type === "String";
|
|
10
|
+
}
|
|
11
|
+
export function getModels() {
|
|
12
|
+
return Object.keys(zenstackSchema.models);
|
|
13
|
+
}
|
|
14
|
+
export function isValidModel(model) {
|
|
15
|
+
return getModels().includes(model);
|
|
16
|
+
}
|
|
17
|
+
function getRelationalFields(model) {
|
|
18
|
+
const modelDef = getModelDef(model);
|
|
19
|
+
return Object.entries(modelDef.fields).filter(([_field, fieldDef]) => isValidModel(fieldDef.type)).map(([field]) => field);
|
|
20
|
+
}
|
|
21
|
+
export function sanitizeCreateData(model, data) {
|
|
22
|
+
const relationalFields = getRelationalFields(model);
|
|
23
|
+
relationalFields.forEach((field) => {
|
|
24
|
+
data[field] = void 0;
|
|
25
|
+
});
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
export function sanitizeUpdateData(model, data) {
|
|
29
|
+
const relationalFields = getRelationalFields(model);
|
|
30
|
+
relationalFields.forEach((field) => {
|
|
31
|
+
data[field] = void 0;
|
|
32
|
+
});
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { _delete } from './operations/delete.js';
|
|
2
|
+
import { update } from './operations/update.js';
|
|
3
|
+
import { findUnique } from './operations/findUnique.js';
|
|
4
|
+
import { findMany } from './operations/findMany.js';
|
|
5
|
+
import { create } from './operations/create.js';
|
|
6
|
+
import type { $Zclient } from '#build/types/nuxt-zenstack';
|
|
2
7
|
import type { H3Event } from 'h3';
|
|
3
|
-
export declare function
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
export declare function useZenstack(): {
|
|
9
|
+
delete: typeof _delete;
|
|
10
|
+
update: typeof update;
|
|
11
|
+
findUnique: typeof findUnique;
|
|
12
|
+
findMany: typeof findMany;
|
|
13
|
+
create: typeof create;
|
|
14
|
+
setSessionClient: (event: H3Event, client: $Zclient) => void;
|
|
15
|
+
};
|
|
@@ -1,44 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { _delete } from "./operations/delete.js";
|
|
2
|
+
import { update } from "./operations/update.js";
|
|
3
|
+
import { findUnique } from "./operations/findUnique.js";
|
|
4
|
+
import { findMany } from "./operations/findMany.js";
|
|
5
|
+
import { create } from "./operations/create.js";
|
|
6
|
+
export function useZenstack() {
|
|
7
|
+
function setSessionClient(event, client) {
|
|
8
|
+
if (event.context.zenstack)
|
|
9
|
+
event.context.zenstack.client = client;
|
|
10
|
+
else
|
|
11
|
+
event.context.zenstack = { client };
|
|
5
12
|
}
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
export function getModelOperations(event, model) {
|
|
9
|
-
const db = getClient(event);
|
|
10
|
-
return db[model.toString().toLocaleLowerCase()];
|
|
11
|
-
}
|
|
12
|
-
export function getModelDef(model) {
|
|
13
|
-
return zenstackSchema.models[model];
|
|
14
|
-
}
|
|
15
|
-
export function isIdString(model) {
|
|
16
|
-
const modelDef = getModelDef(model);
|
|
17
|
-
if (!modelDef.uniqueFields.id)
|
|
18
|
-
throw new Error(`Model ${model.toString()} does not have an id as a unique field`);
|
|
19
|
-
return modelDef.uniqueFields.id.type === "String";
|
|
20
|
-
}
|
|
21
|
-
export function getModels() {
|
|
22
|
-
return Object.keys(zenstackSchema.models);
|
|
23
|
-
}
|
|
24
|
-
export function isValidModel(model) {
|
|
25
|
-
return getModels().includes(model);
|
|
26
|
-
}
|
|
27
|
-
export function includeAll(model) {
|
|
28
|
-
const include = {};
|
|
29
|
-
const modelSet = /* @__PURE__ */ new Set();
|
|
30
|
-
function iterate(_model, acc) {
|
|
31
|
-
const modelDef = getModelDef(_model);
|
|
32
|
-
for (const [field, fieldDef] of Object.entries(modelDef.fields)) {
|
|
33
|
-
if (isValidModel(fieldDef.type) && !modelSet.has(fieldDef.type)) {
|
|
34
|
-
acc["include"] ||= {};
|
|
35
|
-
acc["include"][field] = {};
|
|
36
|
-
modelSet.add(fieldDef.type);
|
|
37
|
-
iterate(fieldDef.type, acc["include"][field]);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
modelSet.add(model);
|
|
42
|
-
iterate(model, include);
|
|
43
|
-
return include["include"] ?? {};
|
|
13
|
+
return { delete: _delete, update, findUnique, findMany, create, setSessionClient };
|
|
44
14
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { $ZormError } from '#build/types/nuxt-zenstack';
|
|
1
|
+
import type { $Zmodel, $Zoperations, $Zclient, $ZormError } from '#build/types/nuxt-zenstack';
|
|
2
2
|
import { ORMErrorReason } from '@zenstackhq/orm';
|
|
3
3
|
export declare function createError(ormError: $ZormError): import("h3").H3Error<{
|
|
4
4
|
ormErrorReason: ORMErrorReason;
|
|
@@ -7,3 +7,4 @@ export declare function createError(ormError: $ZormError): import("h3").H3Error<
|
|
|
7
7
|
model?: string;
|
|
8
8
|
rejectedByPolicyReason?: import("@zenstackhq/orm").RejectedByPolicyReason;
|
|
9
9
|
}>;
|
|
10
|
+
export declare function getModelOperations<Zmodel extends $Zmodel>(client: $Zclient, model: Zmodel): $Zoperations<Zmodel>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { $Zclient, $Zmodel, $ZcreateData, $Zitem } from '#build/types/nuxt-zenstack';
|
|
2
|
+
type CreateArgs<Zmodel extends $Zmodel> = {
|
|
3
|
+
client: $Zclient;
|
|
4
|
+
model: Zmodel;
|
|
5
|
+
data: $ZcreateData<Zmodel>;
|
|
6
|
+
};
|
|
7
|
+
export declare function create<Zmodel extends $Zmodel>(args: CreateArgs<Zmodel>): Promise<{
|
|
8
|
+
data: $Zitem<Zmodel>;
|
|
9
|
+
}>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getModelOperations, createError } from "./common.js";
|
|
2
|
+
import { sanitizeCreateData } from "../helpers.js";
|
|
3
|
+
import { useNitroApp } from "nitropack/runtime";
|
|
4
|
+
export async function create(args) {
|
|
5
|
+
const operations = getModelOperations(args.client, args.model);
|
|
6
|
+
const sanitizedData = sanitizeCreateData(args.model, args.data);
|
|
7
|
+
const data = await operations.create({
|
|
8
|
+
data: sanitizedData
|
|
9
|
+
}).catch((err) => {
|
|
10
|
+
throw createError(err);
|
|
11
|
+
});
|
|
12
|
+
const nitroApp = useNitroApp();
|
|
13
|
+
await nitroApp.hooks.callHook("zenstack:after_mutation", "create", args.model, [data]);
|
|
14
|
+
return { data };
|
|
15
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { $Zclient, $Zmodel, $Zid, $Zitem } from '#build/types/nuxt-zenstack';
|
|
2
|
+
type DeleteArgs<Zmodel extends $Zmodel> = {
|
|
3
|
+
client: $Zclient;
|
|
4
|
+
model: Zmodel;
|
|
5
|
+
id: $Zid<Zmodel>;
|
|
6
|
+
};
|
|
7
|
+
export declare function _delete<Zmodel extends $Zmodel>(args: DeleteArgs<Zmodel>): Promise<{
|
|
8
|
+
data: $Zitem<Zmodel>;
|
|
9
|
+
}>;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { getModelOperations, createError } from "./common.js";
|
|
2
|
+
import { useNitroApp } from "nitropack/runtime";
|
|
3
|
+
export async function _delete(args) {
|
|
4
|
+
const operations = getModelOperations(args.client, args.model);
|
|
5
|
+
const data = await operations.delete({
|
|
6
|
+
// @ts-expect-error id not known here
|
|
7
|
+
where: {
|
|
8
|
+
id: args.id
|
|
9
|
+
}
|
|
10
|
+
}).catch((err) => {
|
|
11
|
+
throw createError(err);
|
|
12
|
+
});
|
|
13
|
+
const nitroApp = useNitroApp();
|
|
14
|
+
await nitroApp.hooks.callHook("zenstack:after_mutation", "delete", args.model, [data]);
|
|
15
|
+
return { data };
|
|
16
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { $Zclient, $Zmodel, $Zinclude, $ZorderBy, $Zwhere, $Zitem } from '#build/types/nuxt-zenstack';
|
|
2
|
+
type FindManyArgs<Zmodel extends $Zmodel> = {
|
|
3
|
+
client: $Zclient;
|
|
4
|
+
model: Zmodel;
|
|
5
|
+
include?: $Zinclude<Zmodel>;
|
|
6
|
+
orderBy?: $ZorderBy<Zmodel>;
|
|
7
|
+
where?: $Zwhere<Zmodel>;
|
|
8
|
+
skip?: number;
|
|
9
|
+
take?: number;
|
|
10
|
+
};
|
|
11
|
+
export declare function findMany<Zmodel extends $Zmodel>(args: FindManyArgs<Zmodel>): Promise<{
|
|
12
|
+
data: $Zitem<Zmodel, $Zinclude<Zmodel>>[];
|
|
13
|
+
}>;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { getModelOperations, createError } from "./common.js";
|
|
2
|
+
export async function findMany(args) {
|
|
3
|
+
const operations = getModelOperations(args.client, args.model);
|
|
4
|
+
const data = await operations.findMany({
|
|
5
|
+
orderBy: args.orderBy,
|
|
6
|
+
include: args.include,
|
|
7
|
+
skip: args.skip,
|
|
8
|
+
take: args.take,
|
|
9
|
+
where: args.where
|
|
10
|
+
}).catch((err) => {
|
|
11
|
+
throw createError(err);
|
|
12
|
+
});
|
|
13
|
+
return { data };
|
|
14
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { $Zclient, $Zmodel, $Zinclude, $Zid, $Zitem } from '#build/types/nuxt-zenstack';
|
|
2
|
+
type FindUniqueArgs<Zmodel extends $Zmodel> = {
|
|
3
|
+
client: $Zclient;
|
|
4
|
+
model: Zmodel;
|
|
5
|
+
id: $Zid<Zmodel>;
|
|
6
|
+
include?: $Zinclude<Zmodel>;
|
|
7
|
+
};
|
|
8
|
+
export declare function findUnique<Zmodel extends $Zmodel>(args: FindUniqueArgs<Zmodel>): Promise<{
|
|
9
|
+
data: $Zitem<Zmodel, $Zinclude<Zmodel>>;
|
|
10
|
+
}>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { getModelOperations, createError } from "./common.js";
|
|
2
|
+
export async function findUnique(args) {
|
|
3
|
+
const operations = getModelOperations(args.client, args.model);
|
|
4
|
+
const data = await operations.findUnique({
|
|
5
|
+
include: args.include,
|
|
6
|
+
// @ts-expect-error id not known here
|
|
7
|
+
where: {
|
|
8
|
+
id: args.id
|
|
9
|
+
}
|
|
10
|
+
}).catch((err) => {
|
|
11
|
+
throw createError(err);
|
|
12
|
+
});
|
|
13
|
+
return { data };
|
|
14
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { $Zclient, $Zmodel, $Zid, $ZupdateData, $Zitem } from '#build/types/nuxt-zenstack';
|
|
2
|
+
type UpdateArgs<Zmodel extends $Zmodel> = {
|
|
3
|
+
client: $Zclient;
|
|
4
|
+
model: Zmodel;
|
|
5
|
+
id: $Zid<Zmodel>;
|
|
6
|
+
data: $ZupdateData<Zmodel>;
|
|
7
|
+
};
|
|
8
|
+
export declare function update<Zmodel extends $Zmodel>(args: UpdateArgs<Zmodel>): Promise<{
|
|
9
|
+
data: $Zitem<Zmodel>;
|
|
10
|
+
}>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { getModelOperations, createError } from "./common.js";
|
|
2
|
+
import { sanitizeUpdateData } from "../helpers.js";
|
|
3
|
+
import { useNitroApp } from "nitropack/runtime";
|
|
4
|
+
export async function update(args) {
|
|
5
|
+
const operations = getModelOperations(args.client, args.model);
|
|
6
|
+
const sanitizedData = sanitizeUpdateData(args.model, args.data);
|
|
7
|
+
const data = await operations.update({
|
|
8
|
+
data: sanitizedData,
|
|
9
|
+
// @ts-expect-error id not known here
|
|
10
|
+
where: {
|
|
11
|
+
id: args.id
|
|
12
|
+
}
|
|
13
|
+
}).catch((err) => {
|
|
14
|
+
throw createError(err);
|
|
15
|
+
});
|
|
16
|
+
const nitroApp = useNitroApp();
|
|
17
|
+
await nitroApp.hooks.callHook("zenstack:after_mutation", "update", args.model, [data]);
|
|
18
|
+
return { data };
|
|
19
|
+
}
|
|
@@ -7,6 +7,7 @@ type ReadArgs<Zmodel extends $Zmodel> = {
|
|
|
7
7
|
skip?: number;
|
|
8
8
|
take?: number;
|
|
9
9
|
};
|
|
10
|
+
export declare function parseClient(event: H3Event): import("@zenstackhq/orm").ClientContract<SchemaType>;
|
|
10
11
|
export declare function parseModel(event: H3Event): string | number | symbol;
|
|
11
12
|
export declare function parseId(event: H3Event, model: $Zmodel): string | number;
|
|
12
13
|
export declare function parseReadArgs<Zmodel extends $Zmodel>(event: H3Event, _model: Zmodel): ReadArgs<Zmodel>;
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { getRouterParam, createError, getQuery, readBody } from "h3";
|
|
2
|
-
import { isValidModel, isIdString } from "./
|
|
2
|
+
import { isValidModel, isIdString } from "./helpers.js";
|
|
3
3
|
import { parse } from "superjson";
|
|
4
|
+
export function parseClient(event) {
|
|
5
|
+
if (event.context.zenstack?.client) {
|
|
6
|
+
return event.context.zenstack.client;
|
|
7
|
+
}
|
|
8
|
+
throw new Error("ZenStack client not provided");
|
|
9
|
+
}
|
|
4
10
|
export function parseModel(event) {
|
|
5
11
|
const model = getRouterParam(event, "model");
|
|
6
12
|
if (isValidModel(model) === false) {
|