@kubun/standalone 0.4.3 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/graphql.d.ts CHANGED
@@ -4,4 +4,3 @@ export type ExecutionContext = MutationContext & {
4
4
  did: string;
5
5
  };
6
6
  export declare function createContext(ctx: ExecutionContext): Context;
7
- //# sourceMappingURL=graphql.d.ts.map
package/lib/graphql.js CHANGED
@@ -1,66 +1 @@
1
- import { createReadContext } from '@kubun/graphql';
2
- import { applyChangeMutation, applySetMutation, createChangeMutation, createRemoveMutation, createSetMutation } from '@kubun/mutation';
3
- export function createContext(ctx) {
4
- return {
5
- ...createReadContext({
6
- db: ctx.db,
7
- viewerDID: ctx.did
8
- }),
9
- async executeCreateMutation (modelID, data) {
10
- const unique = globalThis.crypto.getRandomValues(new Uint8Array(12));
11
- const mutation = await createSetMutation({
12
- issuer: ctx.did,
13
- modelID,
14
- data,
15
- unique
16
- });
17
- return await applySetMutation(ctx, mutation);
18
- },
19
- async executeSetMutation (modelID, unique, data) {
20
- const mutation = await createSetMutation({
21
- issuer: ctx.did,
22
- modelID,
23
- data,
24
- unique
25
- });
26
- return await applySetMutation(ctx, mutation);
27
- },
28
- async executeUpdateMutation (input) {
29
- const mutation = await createChangeMutation({
30
- issuer: ctx.did,
31
- loadState: async (id)=>await ctx.db.getDocumentState(id),
32
- docID: input.id,
33
- patch: input.patch.map((patch)=>{
34
- // convert GraphQL patch input types to PatchOperation
35
- const entries = Object.entries(patch);
36
- const [op, rest] = entries[0];
37
- return {
38
- ...rest,
39
- op
40
- };
41
- }),
42
- from: input.from
43
- });
44
- return await applyChangeMutation(ctx, mutation);
45
- },
46
- async executeRemoveMutation (id) {
47
- const mutation = createRemoveMutation({
48
- docID: id,
49
- issuer: ctx.did
50
- });
51
- await applyChangeMutation(ctx, mutation);
52
- },
53
- async executeSetModelAccessDefaults () {
54
- throw new Error('Access control mutations are not supported in standalone mode');
55
- },
56
- async executeRemoveModelAccessDefaults () {
57
- throw new Error('Access control mutations are not supported in standalone mode');
58
- },
59
- async executeSetDocumentAccessOverride () {
60
- throw new Error('Access control mutations are not supported in standalone mode');
61
- },
62
- async executeRemoveDocumentAccessOverride () {
63
- throw new Error('Access control mutations are not supported in standalone mode');
64
- }
65
- };
66
- }
1
+ import{createReadContext as e}from"@kubun/graphql";import{applyChangeMutation as t,applySetMutation as o,createChangeMutation as a,createRemoveMutation as r,createSetMutation as n}from"@kubun/mutation";export function createContext(s){return{...e({db:s.db,viewerDID:s.did}),async executeCreateMutation(e,t){let a=globalThis.crypto.getRandomValues(new Uint8Array(12)),r=await n({issuer:s.did,modelID:e,data:t,unique:a});return await o(s,r)},async executeSetMutation(e,t,a){let r=await n({issuer:s.did,modelID:e,data:a,unique:t});return await o(s,r)},async executeUpdateMutation(e){let o=await a({issuer:s.did,loadState:async e=>await s.db.getDocumentState(e),docID:e.id,patch:e.patch.map(e=>{let[t,o]=Object.entries(e)[0];return{...o,op:t}}),from:e.from});return await t(s,o)},async executeRemoveMutation(e){let o=r({docID:e,issuer:s.did});await t(s,o)},async executeSetModelAccessDefaults(){throw Error("Access control mutations are not supported in standalone mode")},async executeRemoveModelAccessDefaults(){throw Error("Access control mutations are not supported in standalone mode")},async executeSetDocumentAccessOverride(){throw Error("Access control mutations are not supported in standalone mode")},async executeRemoveDocumentAccessOverride(){throw Error("Access control mutations are not supported in standalone mode")}}}
package/lib/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
1
  export { createContext, type ExecutionContext } from './graphql.js';
2
2
  export * from './standalone.js';
3
- //# sourceMappingURL=index.d.ts.map
package/lib/index.js CHANGED
@@ -1,2 +1 @@
1
- export { createContext } from './graphql.js';
2
- export * from './standalone.js';
1
+ export{createContext}from"./graphql.js";export*from"./standalone.js";
@@ -14,7 +14,8 @@ export type DeployParams = {
14
14
  };
15
15
  export type DeployResult = {
16
16
  id: string;
17
- models: DocumentModelsRecord;
17
+ record: DocumentModelsRecord;
18
+ aliases: Record<string, string>;
18
19
  };
19
20
  export type ExecuteParams = {
20
21
  graphID: string;
@@ -31,4 +32,3 @@ export declare class KubunStandalone {
31
32
  subscribe<Data extends Record<string, unknown> = Record<string, unknown>>(params: ExecuteParams): Promise<AsyncGenerator<ExecutionResult<Data>> | ExecutionResult<Data>>;
32
33
  }
33
34
  export declare function standalone(params: StandaloneParams): KubunStandalone;
34
- //# sourceMappingURL=standalone.d.ts.map
package/lib/standalone.js CHANGED
@@ -1,101 +1 @@
1
- import { createSchema } from '@kubun/graphql';
2
- import { getKubunLogger } from '@kubun/logger';
3
- import { GraphModel } from '@kubun/protocol';
4
- import { execute, parse, subscribe } from 'graphql';
5
- import { createContext } from './graphql.js';
6
- export class KubunStandalone {
7
- #context;
8
- #db;
9
- #graphModels = {};
10
- #logger;
11
- #schemas = {};
12
- constructor(params){
13
- this.#context = createContext({
14
- db: params.db,
15
- did: params.did,
16
- validators: {}
17
- });
18
- this.#db = params.db;
19
- this.#logger = params.logger ?? getKubunLogger('standalone');
20
- }
21
- async getGraphModel(id) {
22
- if (this.#graphModels[id] == null) {
23
- this.#graphModels[id] = this.#db.getGraph(id).then((graph)=>{
24
- if (graph == null) {
25
- this.#logger.warn('graph {id} not found', {
26
- id
27
- });
28
- delete this.#graphModels[id];
29
- throw new Error(`Graph not found: ${id}`);
30
- }
31
- this.#logger.debug('cached model for graph {id}', {
32
- id
33
- });
34
- return {
35
- aliases: graph.aliases,
36
- record: graph.record
37
- };
38
- });
39
- }
40
- return await this.#graphModels[id];
41
- }
42
- async getGraphQLSchema(id) {
43
- if (this.#schemas[id] == null) {
44
- this.#schemas[id] = this.getGraphModel(id).then((model)=>{
45
- const schema = createSchema(model);
46
- this.#logger.debug('cached schema for graph {id}', {
47
- id
48
- });
49
- // console.log(printSchema(schema))
50
- return schema;
51
- }).catch((err)=>{
52
- delete this.#schemas[id];
53
- throw err;
54
- });
55
- }
56
- return await this.#schemas[id];
57
- }
58
- async #getExecutionArgs(params) {
59
- const document = parse(params.text);
60
- const definition = document.definitions[0];
61
- if (definition == null) {
62
- throw new Error('Missing GraphQL document definition');
63
- }
64
- return {
65
- schema: await this.getGraphQLSchema(params.graphID),
66
- document,
67
- contextValue: this.#context,
68
- variableValues: params.variables
69
- };
70
- }
71
- async deploy(params) {
72
- const model = GraphModel.fromClusters({
73
- clusters: params.clusters
74
- });
75
- const id = await this.#db.createGraph({
76
- id: params.id,
77
- name: params.name,
78
- record: model.record
79
- });
80
- this.#graphModels[id] = Promise.resolve(model.toJSON());
81
- delete this.#schemas[id];
82
- this.#logger.info('deployed graph {id}', {
83
- id
84
- });
85
- return {
86
- id,
87
- models: model.record
88
- };
89
- }
90
- async execute(params) {
91
- const args = await this.#getExecutionArgs(params);
92
- return await execute(args);
93
- }
94
- async subscribe(params) {
95
- const args = await this.#getExecutionArgs(params);
96
- return await subscribe(args);
97
- }
98
- }
99
- export function standalone(params) {
100
- return new KubunStandalone(params);
101
- }
1
+ import{createSchema as e}from"@kubun/graphql";import{getKubunLogger as t}from"@kubun/logger";import{GraphModel as r}from"@kubun/protocol";import{execute as a,parse as s,subscribe as o}from"graphql";import{createContext as i}from"./graphql.js";export class KubunStandalone{#e;#t;#r={};#a;#s={};constructor(e){this.#e=i({db:e.db,did:e.did,validators:{}}),this.#t=e.db,this.#a=e.logger??t("standalone")}async getGraphModel(e){return null==this.#r[e]&&(this.#r[e]=this.#t.getGraph(e).then(t=>{if(null==t)throw this.#a.warn("graph {id} not found",{id:e}),delete this.#r[e],Error(`Graph not found: ${e}`);return this.#a.debug("cached model for graph {id}",{id:e}),{aliases:t.aliases,record:t.record}})),await this.#r[e]}async getGraphQLSchema(t){return null==this.#s[t]&&(this.#s[t]=this.getGraphModel(t).then(r=>{let a=e(r);return this.#a.debug("cached schema for graph {id}",{id:t}),a}).catch(e=>{throw delete this.#s[t],e})),await this.#s[t]}async #o(e){let t=s(e.text);if(null==t.definitions[0])throw Error("Missing GraphQL document definition");return{schema:await this.getGraphQLSchema(e.graphID),document:t,contextValue:this.#e,variableValues:e.variables}}async deploy(e){let t=r.fromClusters({clusters:e.clusters}),a=await this.#t.createGraph({id:e.id,name:e.name,record:t.record}),s=t.toJSON();return this.#r[a]=Promise.resolve(s),delete this.#s[a],this.#a.info("deployed graph {id}",{id:a}),{id:a,...s}}async execute(e){let t=await this.#o(e);return await a(t)}async subscribe(e){let t=await this.#o(e);return await o(t)}}export function standalone(e){return new KubunStandalone(e)}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/standalone",
3
- "version": "0.4.3",
3
+ "version": "0.5.0",
4
4
  "license": "see LICENSE.md",
5
5
  "keywords": [],
6
6
  "type": "module",
@@ -16,14 +16,14 @@
16
16
  "sideEffects": false,
17
17
  "dependencies": {
18
18
  "graphql": "^16.12.0",
19
- "@kubun/mutation": "^0.4.0",
20
- "@kubun/protocol": "^0.4.1",
21
- "@kubun/logger": "^0.4.0",
22
- "@kubun/graphql": "^0.4.5"
19
+ "@kubun/graphql": "^0.5.0",
20
+ "@kubun/logger": "^0.5.0",
21
+ "@kubun/protocol": "^0.5.0",
22
+ "@kubun/mutation": "^0.5.0"
23
23
  },
24
24
  "devDependencies": {
25
- "@kubun/db": "^0.4.0",
26
- "@kubun/test-utils": "^0.4.0"
25
+ "@kubun/db": "^0.5.0",
26
+ "@kubun/test-utils": "^0.5.0"
27
27
  },
28
28
  "scripts": {
29
29
  "build:clean": "del lib",
@@ -1 +0,0 @@
1
- {"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../src/graphql.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAuC,MAAM,gBAAgB,CAAA;AAElF,OAAO,EAML,KAAK,eAAe,EACrB,MAAM,iBAAiB,CAAA;AAGxB,MAAM,MAAM,gBAAgB,GAAG,eAAe,GAAG;IAC/C,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,gBAAgB,GAAG,OAAO,CAgD5D"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAA;AACnE,cAAc,iBAAiB,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"standalone.d.ts","sourceRoot":"","sources":["../src/standalone.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAExC,OAAO,EAAkB,KAAK,MAAM,EAAE,MAAM,eAAe,CAAA;AAC3D,OAAO,KAAK,EAAE,YAAY,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AAEzF,OAAO,KAAK,EAAiB,eAAe,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAK5E,MAAM,MAAM,gBAAgB,GAAG;IAC7B,EAAE,EAAE,OAAO,CAAA;IACX,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,EAAE,KAAK,CAAC,YAAY,CAAC,CAAA;IAC7B,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED,MAAM,MAAM,YAAY,GAAG;IACzB,EAAE,EAAE,MAAM,CAAA;IACV,MAAM,EAAE,oBAAoB,CAAA;CAC7B,CAAA;AAED,MAAM,MAAM,aAAa,GAAG;IAC1B,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACpC,CAAA;AAED,qBAAa,eAAe;;gBAOd,MAAM,EAAE,gBAAgB;IAU9B,aAAa,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC;IAelD,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IAgCpD,MAAM,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;IAanD,OAAO,CAAC,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC1E,MAAM,EAAE,aAAa,GACpB,OAAO,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;IAK3B,SAAS,CAAC,IAAI,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5E,MAAM,EAAE,aAAa,GACpB,OAAO,CAAC,cAAc,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,GAAG,eAAe,CAAC,IAAI,CAAC,CAAC;CAI1E;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,gBAAgB,GAAG,eAAe,CAEpE"}