@bg-dev/nuxt-zenstack 0.0.3 → 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 +61 -12
- package/dist/runtime/composables/common.d.ts +3 -13
- package/dist/runtime/composables/useZenstackCreate/index.d.ts +3 -3
- package/dist/runtime/composables/useZenstackDelete/index.d.ts +3 -3
- package/dist/runtime/composables/useZenstackRead/index.d.ts +4 -4
- package/dist/runtime/composables/useZenstackRead/index.js +8 -7
- package/dist/runtime/composables/useZenstackReadMany/index.d.ts +4 -3
- package/dist/runtime/composables/useZenstackReadMany/index.js +21 -8
- package/dist/runtime/composables/useZenstackReadMany/orderBy.d.ts +5 -0
- package/dist/runtime/composables/useZenstackReadMany/orderBy.js +25 -0
- package/dist/runtime/composables/useZenstackReadMany/where.d.ts +2 -0
- package/dist/runtime/composables/useZenstackReadMany/where.js +235 -0
- package/dist/runtime/composables/useZenstackRealtime/index.d.ts +6 -0
- package/dist/runtime/composables/useZenstackRealtime/index.js +53 -0
- package/dist/runtime/composables/useZenstackStore/index.d.ts +6 -2
- package/dist/runtime/composables/useZenstackStore/index.js +3 -4
- package/dist/runtime/composables/useZenstackStore/normalization.d.ts +16 -3
- package/dist/runtime/composables/useZenstackStore/normalization.js +16 -3
- package/dist/runtime/composables/useZenstackUpdate/index.d.ts +2 -2
- package/dist/runtime/server/api/models/[model]/[id].delete.d.ts +1 -1
- package/dist/runtime/server/api/models/[model]/[id].delete.js +5 -9
- package/dist/runtime/server/api/models/[model]/[id].get.d.ts +1 -1
- package/dist/runtime/server/api/models/[model]/[id].get.js +5 -10
- package/dist/runtime/server/api/models/[model]/[id].patch.d.ts +1 -1
- package/dist/runtime/server/api/models/[model]/[id].patch.js +6 -13
- package/dist/runtime/server/api/models/[model]/index.get.d.ts +2 -2
- package/dist/runtime/server/api/models/[model]/index.get.js +9 -7
- package/dist/runtime/server/api/models/[model]/index.post.d.ts +1 -1
- package/dist/runtime/server/api/models/[model]/index.post.js +6 -10
- 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 +15 -12
- package/dist/runtime/server/utils/index.js +12 -46
- package/dist/runtime/server/utils/operations/common.d.ts +10 -0
- package/dist/runtime/server/utils/operations/common.js +52 -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 +3 -2
- package/dist/runtime/server/utils/parsers.js +9 -3
- package/package.json +3 -1
|
@@ -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
|
+
}
|
|
@@ -11,11 +11,15 @@ type FetchEntry = {
|
|
|
11
11
|
export declare function useZenstackStore(): {
|
|
12
12
|
state: Readonly<import("vue").Ref<{
|
|
13
13
|
readonly [x: string]: {
|
|
14
|
-
readonly [x: string]:
|
|
14
|
+
readonly [x: string]: {
|
|
15
|
+
readonly [x: string]: string | number | boolean | readonly string[] | null;
|
|
16
|
+
};
|
|
15
17
|
};
|
|
16
18
|
}, {
|
|
17
19
|
readonly [x: string]: {
|
|
18
|
-
readonly [x: string]:
|
|
20
|
+
readonly [x: string]: {
|
|
21
|
+
readonly [x: string]: string | number | boolean | readonly string[] | null;
|
|
22
|
+
};
|
|
19
23
|
};
|
|
20
24
|
}>>;
|
|
21
25
|
fetchHistory: Readonly<import("vue").Ref<readonly {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { generateNormalizrSchema, getNormalizrSchema } from "./normalization.js";
|
|
1
|
+
import { generateNormalizrSchema, getNormalizrSchema, mergeNormalizedData } from "./normalization.js";
|
|
2
2
|
import { normalize, denormalize } from "normalizr";
|
|
3
|
-
import { defu } from "defu";
|
|
4
3
|
import { readonly, useState } from "#imports";
|
|
5
4
|
generateNormalizrSchema();
|
|
6
5
|
export function useZenstackStore() {
|
|
@@ -9,12 +8,12 @@ export function useZenstackStore() {
|
|
|
9
8
|
function setOne(model, input) {
|
|
10
9
|
const schema = getNormalizrSchema(model);
|
|
11
10
|
const res = normalize(input, schema);
|
|
12
|
-
state.value =
|
|
11
|
+
state.value = mergeNormalizedData(res.entities, state.value);
|
|
13
12
|
}
|
|
14
13
|
function setMany(model, input) {
|
|
15
14
|
const schema = [getNormalizrSchema(model)];
|
|
16
15
|
const res = normalize(input, schema);
|
|
17
|
-
state.value =
|
|
16
|
+
state.value = mergeNormalizedData(res.entities, state.value);
|
|
18
17
|
}
|
|
19
18
|
function getOne(model, input) {
|
|
20
19
|
const schema = getNormalizrSchema(model);
|
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import { schema as normalizrSchema } from 'normalizr';
|
|
2
2
|
import type { $Zmodel } from '#build/types/nuxt-zenstack';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
type NormalizedDataField = number | string | boolean | null | string[];
|
|
4
|
+
/**
|
|
5
|
+
* Normalized data structure
|
|
6
|
+
* {
|
|
7
|
+
* model: {
|
|
8
|
+
* id: {
|
|
9
|
+
* field: NormalizedDataField
|
|
10
|
+
* }
|
|
11
|
+
* }
|
|
12
|
+
* }
|
|
13
|
+
*/
|
|
14
|
+
export type NormalizedData = Record<string, Record<string, Record<string, NormalizedDataField>>>;
|
|
15
|
+
export declare function getNormalizrSchema(model: $Zmodel): normalizrSchema.Entity<any>;
|
|
16
|
+
export declare function generateNormalizrSchema(): void;
|
|
17
|
+
export declare function mergeNormalizedData(newData: NormalizedData, currentData: NormalizedData): NormalizedData;
|
|
18
|
+
export {};
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { schema as normalizrSchema } from "normalizr";
|
|
2
2
|
import { getModelDef, getModels, isValidModel } from "./helpers.js";
|
|
3
|
+
import { defu } from "defu";
|
|
3
4
|
const NORMALIZR_SCHEMA = /* @__PURE__ */ new Map();
|
|
4
|
-
function getNormalizrSchema(model) {
|
|
5
|
+
export function getNormalizrSchema(model) {
|
|
5
6
|
const schema = NORMALIZR_SCHEMA.get(model);
|
|
6
7
|
if (!schema)
|
|
7
8
|
throw new Error(`Model ${model.toString()} not defined`);
|
|
8
9
|
return schema;
|
|
9
10
|
}
|
|
10
|
-
function generateNormalizrSchema() {
|
|
11
|
+
export function generateNormalizrSchema() {
|
|
11
12
|
const models = getModels();
|
|
12
13
|
for (const model of models) {
|
|
13
14
|
NORMALIZR_SCHEMA.set(model, new normalizrSchema.Entity(model.toString()));
|
|
@@ -28,4 +29,16 @@ function generateNormalizrSchema() {
|
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
}
|
|
31
|
-
export
|
|
32
|
+
export function mergeNormalizedData(newData, currentData) {
|
|
33
|
+
const merged = defu(newData, currentData);
|
|
34
|
+
for (const model in merged) {
|
|
35
|
+
for (const id in merged[model]) {
|
|
36
|
+
for (const field in merged[model][id]) {
|
|
37
|
+
if (Array.isArray(merged[model][id][field])) {
|
|
38
|
+
merged[model][id][field] = Array.from(new Set(merged[model][id][field]));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return merged;
|
|
44
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { $Zmodel, $Zitem, $ZupdateData, $Zid } from '#build/types/nuxt-zenstack';
|
|
1
|
+
import type { $Zmodel, $Zitem, $ZupdateData, $Zid, $Zerror } from '#build/types/nuxt-zenstack';
|
|
2
2
|
import type { Status } from '../common.js';
|
|
3
3
|
import type { Ref } from '#imports';
|
|
4
4
|
export declare function useZenstackUpdate<Zmodel extends $Zmodel, Zitem extends $Zitem<Zmodel>>(model: Zmodel): {
|
|
5
5
|
data: Ref<Zitem | null>;
|
|
6
|
-
error: Ref
|
|
6
|
+
error: Ref<$Zerror | null>;
|
|
7
7
|
status: Ref<Status>;
|
|
8
8
|
mutate: (id: $Zid<Zmodel>, input: $ZupdateData<Zmodel>) => Promise<void>;
|
|
9
9
|
reset: () => void;
|
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
import { defineEventHandler } from "h3";
|
|
2
|
-
import { parseModel, parseId } from "../../../utils/parsers.js";
|
|
3
|
-
import {
|
|
2
|
+
import { parseModel, parseId, parseClient } from "../../../utils/parsers.js";
|
|
3
|
+
import { useZenstack } from "../../../utils/index.js";
|
|
4
4
|
export default defineEventHandler(async (event) => {
|
|
5
|
+
const client = parseClient(event);
|
|
5
6
|
const model = parseModel(event);
|
|
6
7
|
const id = parseId(event, model);
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
where: {
|
|
10
|
-
id
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
return { data };
|
|
8
|
+
const zenstack = useZenstack();
|
|
9
|
+
return zenstack.delete({ client, model, id });
|
|
14
10
|
});
|
|
@@ -1,16 +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 {
|
|
3
|
+
import { useZenstack } from "../../../utils/index.js";
|
|
4
4
|
export default defineEventHandler(async (event) => {
|
|
5
|
+
const client = parseClient(event);
|
|
5
6
|
const model = parseModel(event);
|
|
6
7
|
const id = parseId(event, model);
|
|
7
8
|
const readArgs = parseReadArgs(event, model);
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
include: readArgs.include,
|
|
11
|
-
where: {
|
|
12
|
-
id
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
return { data };
|
|
9
|
+
const zenstack = useZenstack();
|
|
10
|
+
return zenstack.findUnique({ client, model, id, include: readArgs.include });
|
|
16
11
|
});
|
|
@@ -1,18 +1,11 @@
|
|
|
1
|
-
import { parseModel, parseId,
|
|
1
|
+
import { parseModel, parseId, parseUpdateArgs, parseClient } from "../../../utils/parsers.js";
|
|
2
2
|
import { defineEventHandler } from "h3";
|
|
3
|
-
import {
|
|
3
|
+
import { useZenstack } from "../../../utils/index.js";
|
|
4
4
|
export default defineEventHandler(async (event) => {
|
|
5
|
+
const client = parseClient(event);
|
|
5
6
|
const model = parseModel(event);
|
|
6
7
|
const id = parseId(event, model);
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
const data = await operations.update({
|
|
11
|
-
data: updateData.data,
|
|
12
|
-
include,
|
|
13
|
-
where: {
|
|
14
|
-
id
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
return { data };
|
|
8
|
+
const updateArgs = await parseUpdateArgs(event, model);
|
|
9
|
+
const zenstack = useZenstack();
|
|
10
|
+
return zenstack.update({ client, model, id, data: updateArgs.data });
|
|
18
11
|
});
|
|
@@ -1,16 +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 {
|
|
3
|
+
import { useZenstack } from "../../../utils/index.js";
|
|
4
4
|
export default defineEventHandler(async (event) => {
|
|
5
|
+
const client = parseClient(event);
|
|
5
6
|
const model = parseModel(event);
|
|
6
7
|
const readArgs = parseReadArgs(event, model);
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const zenstack = useZenstack();
|
|
9
|
+
return zenstack.findMany({
|
|
10
|
+
client,
|
|
11
|
+
model,
|
|
11
12
|
skip: readArgs.skip,
|
|
12
13
|
take: readArgs.take,
|
|
14
|
+
include: readArgs.include,
|
|
15
|
+
orderBy: readArgs.orderBy,
|
|
13
16
|
where: readArgs.where
|
|
14
17
|
});
|
|
15
|
-
return { data };
|
|
16
18
|
});
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
import { parseModel,
|
|
1
|
+
import { parseModel, parseCreateArgs, parseClient } from "../../../utils/parsers.js";
|
|
2
2
|
import { defineEventHandler } from "h3";
|
|
3
|
-
import {
|
|
3
|
+
import { useZenstack } from "../../../utils/index.js";
|
|
4
4
|
export default defineEventHandler(async (event) => {
|
|
5
|
+
const client = parseClient(event);
|
|
5
6
|
const model = parseModel(event);
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
const data = await operations.create({
|
|
10
|
-
data: createData.data,
|
|
11
|
-
include
|
|
12
|
-
});
|
|
13
|
-
return { data };
|
|
7
|
+
const createArgs = await parseCreateArgs(event, model);
|
|
8
|
+
const zenstack = useZenstack();
|
|
9
|
+
return zenstack.create({ client, model, data: createArgs.data });
|
|
14
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,12 +1,15 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export declare function
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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';
|
|
7
|
+
import type { H3Event } from 'h3';
|
|
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,48 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
export function
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
export function getModelOperations(model) {
|
|
13
|
-
const db = getClient();
|
|
14
|
-
return db[model.toString().toLocaleLowerCase()];
|
|
15
|
-
}
|
|
16
|
-
export function getModelDef(model) {
|
|
17
|
-
return zenstackSchema.models[model];
|
|
18
|
-
}
|
|
19
|
-
export function isIdString(model) {
|
|
20
|
-
const modelDef = getModelDef(model);
|
|
21
|
-
if (!modelDef.uniqueFields.id)
|
|
22
|
-
throw new Error(`Model ${model.toString()} does not have an id as a unique field`);
|
|
23
|
-
return modelDef.uniqueFields.id.type === "String";
|
|
24
|
-
}
|
|
25
|
-
export function getModels() {
|
|
26
|
-
return Object.keys(zenstackSchema.models);
|
|
27
|
-
}
|
|
28
|
-
export function isValidModel(model) {
|
|
29
|
-
return getModels().includes(model);
|
|
30
|
-
}
|
|
31
|
-
export function includeAll(model) {
|
|
32
|
-
const include = {};
|
|
33
|
-
const modelSet = /* @__PURE__ */ new Set();
|
|
34
|
-
function iterate(_model, acc) {
|
|
35
|
-
const modelDef = getModelDef(_model);
|
|
36
|
-
for (const [field, fieldDef] of Object.entries(modelDef.fields)) {
|
|
37
|
-
if (isValidModel(fieldDef.type) && !modelSet.has(fieldDef.type)) {
|
|
38
|
-
acc["include"] ||= {};
|
|
39
|
-
acc["include"][field] = {};
|
|
40
|
-
modelSet.add(fieldDef.type);
|
|
41
|
-
iterate(fieldDef.type, acc["include"][field]);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
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 };
|
|
44
12
|
}
|
|
45
|
-
|
|
46
|
-
iterate(model, include);
|
|
47
|
-
return include["include"] ?? {};
|
|
13
|
+
return { delete: _delete, update, findUnique, findMany, create, setSessionClient };
|
|
48
14
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { $Zmodel, $Zoperations, $Zclient, $ZormError } from '#build/types/nuxt-zenstack';
|
|
2
|
+
import { ORMErrorReason } from '@zenstackhq/orm';
|
|
3
|
+
export declare function createError(ormError: $ZormError): import("h3").H3Error<{
|
|
4
|
+
ormErrorReason: ORMErrorReason;
|
|
5
|
+
dbErrorCode?: unknown;
|
|
6
|
+
dbErrorMessage?: string;
|
|
7
|
+
model?: string;
|
|
8
|
+
rejectedByPolicyReason?: import("@zenstackhq/orm").RejectedByPolicyReason;
|
|
9
|
+
}>;
|
|
10
|
+
export declare function getModelOperations<Zmodel extends $Zmodel>(client: $Zclient, model: Zmodel): $Zoperations<Zmodel>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ORMErrorReason } from "@zenstackhq/orm";
|
|
2
|
+
import { createError as createH3Error } from "h3";
|
|
3
|
+
export function createError(ormError) {
|
|
4
|
+
let statusCode = 500;
|
|
5
|
+
switch (ormError.reason) {
|
|
6
|
+
case ORMErrorReason.CONFIG_ERROR:
|
|
7
|
+
statusCode = 500;
|
|
8
|
+
break;
|
|
9
|
+
case ORMErrorReason.DB_QUERY_ERROR:
|
|
10
|
+
statusCode = 409;
|
|
11
|
+
break;
|
|
12
|
+
case ORMErrorReason.INTERNAL_ERROR:
|
|
13
|
+
statusCode = 500;
|
|
14
|
+
break;
|
|
15
|
+
case ORMErrorReason.NOT_FOUND:
|
|
16
|
+
statusCode = 404;
|
|
17
|
+
break;
|
|
18
|
+
case ORMErrorReason.INVALID_INPUT:
|
|
19
|
+
statusCode = 400;
|
|
20
|
+
break;
|
|
21
|
+
case ORMErrorReason.NOT_SUPPORTED:
|
|
22
|
+
statusCode = 501;
|
|
23
|
+
break;
|
|
24
|
+
case ORMErrorReason.REJECTED_BY_POLICY:
|
|
25
|
+
statusCode = 401;
|
|
26
|
+
break;
|
|
27
|
+
default:
|
|
28
|
+
statusCode = 500;
|
|
29
|
+
}
|
|
30
|
+
const error = {
|
|
31
|
+
name: "Zenstack Error",
|
|
32
|
+
cause: ormError.cause,
|
|
33
|
+
message: ormError.message,
|
|
34
|
+
statusCode,
|
|
35
|
+
statusMessage: ormError.reason,
|
|
36
|
+
stack: ormError.stack,
|
|
37
|
+
fatal: false,
|
|
38
|
+
unhandled: false,
|
|
39
|
+
toJSON: () => error,
|
|
40
|
+
data: {
|
|
41
|
+
ormErrorReason: ormError.reason,
|
|
42
|
+
dbErrorCode: ormError.dbErrorCode,
|
|
43
|
+
dbErrorMessage: ormError.dbErrorMessage,
|
|
44
|
+
model: ormError.model,
|
|
45
|
+
rejectedByPolicyReason: ormError.rejectedByPolicyReason
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
return createH3Error(error);
|
|
49
|
+
}
|
|
50
|
+
export function getModelOperations(client, model) {
|
|
51
|
+
return client[model.toString().toLocaleLowerCase()];
|
|
52
|
+
}
|
|
@@ -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
|
+
}
|