@kubun/server 0.1.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.
Files changed (51) hide show
  1. package/LICENSE.md +57 -0
  2. package/README.md +11 -0
  3. package/lib/data/cursor.d.ts +8 -0
  4. package/lib/data/cursor.d.ts.map +1 -0
  5. package/lib/data/cursor.js +7 -0
  6. package/lib/data/db.d.ts +31 -0
  7. package/lib/data/db.d.ts.map +1 -0
  8. package/lib/data/db.js +192 -0
  9. package/lib/data/engines/engine.d.ts +6 -0
  10. package/lib/data/engines/engine.d.ts.map +1 -0
  11. package/lib/data/engines/engine.js +12 -0
  12. package/lib/data/engines/postgres.d.ts +25 -0
  13. package/lib/data/engines/postgres.d.ts.map +1 -0
  14. package/lib/data/engines/postgres.js +30 -0
  15. package/lib/data/engines/sqlite.d.ts +24 -0
  16. package/lib/data/engines/sqlite.d.ts.map +1 -0
  17. package/lib/data/engines/sqlite.js +29 -0
  18. package/lib/data/engines/types.d.ts +19 -0
  19. package/lib/data/engines/types.d.ts.map +1 -0
  20. package/lib/data/engines/types.js +1 -0
  21. package/lib/data/graphql.d.ts +24 -0
  22. package/lib/data/graphql.d.ts.map +1 -0
  23. package/lib/data/graphql.js +106 -0
  24. package/lib/data/migrations/0-init.d.ts +4 -0
  25. package/lib/data/migrations/0-init.d.ts.map +1 -0
  26. package/lib/data/migrations/0-init.js +22 -0
  27. package/lib/data/migrations/migrations.d.ts +4 -0
  28. package/lib/data/migrations/migrations.d.ts.map +1 -0
  29. package/lib/data/migrations/migrations.js +6 -0
  30. package/lib/data/mutations.d.ts +4 -0
  31. package/lib/data/mutations.d.ts.map +1 -0
  32. package/lib/data/mutations.js +66 -0
  33. package/lib/data/query-builder.d.ts +12 -0
  34. package/lib/data/query-builder.d.ts.map +1 -0
  35. package/lib/data/query-builder.js +218 -0
  36. package/lib/data/types.d.ts +134 -0
  37. package/lib/data/types.d.ts.map +1 -0
  38. package/lib/data/types.js +1 -0
  39. package/lib/handlers/graph.d.ts +8 -0
  40. package/lib/handlers/graph.d.ts.map +1 -0
  41. package/lib/handlers/graph.js +174 -0
  42. package/lib/handlers/index.d.ts +9 -0
  43. package/lib/handlers/index.d.ts.map +1 -0
  44. package/lib/handlers/index.js +4 -0
  45. package/lib/index.d.ts +3 -0
  46. package/lib/index.d.ts.map +1 -0
  47. package/lib/index.js +2 -0
  48. package/lib/server.d.ts +18 -0
  49. package/lib/server.d.ts.map +1 -0
  50. package/lib/server.js +45 -0
  51. package/package.json +49 -0
package/LICENSE.md ADDED
@@ -0,0 +1,57 @@
1
+ # The Prosperity Public License 3.0.0
2
+
3
+ Contributor: Paul Le Cam
4
+
5
+ Source Code: https://github.com/PaulLeCam/kubun
6
+
7
+ ## Purpose
8
+
9
+ This license allows you to use and share this software for noncommercial purposes for free and to try this software for commercial purposes for thirty days.
10
+
11
+ ## Agreement
12
+
13
+ In order to receive this license, you have to agree to its rules. Those rules are both obligations under that agreement and conditions to your license. Don't do anything with this software that triggers a rule you can't or won't follow.
14
+
15
+ ## Notices
16
+
17
+ Make sure everyone who gets a copy of any part of this software from you, with or without changes, also gets the text of this license and the contributor and source code lines above.
18
+
19
+ ## Commercial Trial
20
+
21
+ Limit your use of this software for commercial purposes to a thirty-day trial period. If you use this software for work, your company gets one trial period for all personnel, not one trial per person.
22
+
23
+ ## Contributions Back
24
+
25
+ Developing feedback, changes, or additions that you contribute back to the contributor on the terms of a standardized public software license such as [the Blue Oak Model License 1.0.0](https://blueoakcouncil.org/license/1.0.0), [the Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0.html), [the MIT license](https://spdx.org/licenses/MIT.html), or [the two-clause BSD license](https://spdx.org/licenses/BSD-2-Clause.html) doesn't count as use for a commercial purpose.
26
+
27
+ ## Personal Uses
28
+
29
+ Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, doesn't count as use for a commercial purpose.
30
+
31
+ ## Noncommercial Organizations
32
+
33
+ Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution doesn't count as use for a commercial purpose regardless of the source of funding or obligations resulting from the funding.
34
+
35
+ ## Defense
36
+
37
+ Don't make any legal claim against anyone accusing this software, with or without changes, alone or with other technology, of infringing any patent.
38
+
39
+ ## Copyright
40
+
41
+ The contributor licenses you to do everything with this software that would otherwise infringe their copyright in it.
42
+
43
+ ## Patent
44
+
45
+ The contributor licenses you to do everything with this software that would otherwise infringe any patents they can license or become able to license.
46
+
47
+ ## Reliability
48
+
49
+ The contributor can't revoke this license.
50
+
51
+ ## Excuse
52
+
53
+ You're excused for unknowingly breaking [Notices](#notices) if you take all practical steps to comply within thirty days of learning you broke the rule.
54
+
55
+ ## No Liability
56
+
57
+ ***As far as the law allows, this software comes as is, without any warranty or condition, and the contributor won't be liable to anyone for any damages related to this software or this license, under any kind of legal claim.***
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # Kubun server
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ npm install @kubun/server
7
+ ```
8
+
9
+ ## License
10
+
11
+ [Prosperity Public License 3.0.0](LICENSE.md)
@@ -0,0 +1,8 @@
1
+ export type CursorData = {
2
+ id: string;
3
+ ts?: number;
4
+ values?: Record<string, unknown>;
5
+ };
6
+ export declare function parseCursor(cursor: string): CursorData;
7
+ export declare function serializeCursor(data: CursorData): string;
8
+ //# sourceMappingURL=cursor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cursor.d.ts","sourceRoot":"","sources":["../../src/data/cursor.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,UAAU,GAAG;IACvB,EAAE,EAAE,MAAM,CAAA;IACV,EAAE,CAAC,EAAE,MAAM,CAAA;IACX,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACjC,CAAA;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAEtD;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,CAExD"}
@@ -0,0 +1,7 @@
1
+ import { b64uFromJSON, b64uToJSON } from '@enkaku/codec';
2
+ export function parseCursor(cursor) {
3
+ return b64uToJSON(cursor);
4
+ }
5
+ export function serializeCursor(data) {
6
+ return b64uFromJSON(data);
7
+ }
@@ -0,0 +1,31 @@
1
+ import type { DocumentID } from '@kubun/id';
2
+ import type { DocumentModel, DocumentModelsRecord } from '@kubun/protocol';
3
+ import { type Transaction } from 'kysely';
4
+ import { type SupportedEngineParams } from './engines/engine.js';
5
+ import type { Database, Document, DocumentData, InsertDocumentAttachment, ListDocumentsParams, QueryDocumentsParams, QueryDocumentsResult } from './types.js';
6
+ export type KubunDBParams = SupportedEngineParams;
7
+ export type CreateGraphParams = {
8
+ name: string;
9
+ record: DocumentModelsRecord;
10
+ };
11
+ export type AddDocumentModelParams = {
12
+ transaction: Transaction<Database>;
13
+ id: string;
14
+ graphModelID: string;
15
+ model: DocumentModel;
16
+ };
17
+ export declare class KubunDB {
18
+ #private;
19
+ constructor(params: KubunDBParams);
20
+ close(): Promise<void>;
21
+ createGraph(params: CreateGraphParams): Promise<string>;
22
+ _addDocumentModel(params: AddDocumentModelParams): Promise<void>;
23
+ getGraph(id: string): Promise<DocumentModelsRecord>;
24
+ getDocument(id: DocumentID): Promise<Document | null>;
25
+ listDocuments(params: ListDocumentsParams): Promise<Array<Document>>;
26
+ queryDocuments(params: QueryDocumentsParams): Promise<QueryDocumentsResult>;
27
+ createDocument(id: DocumentID, owner: string, data: DocumentData | null, unique: Uint8Array): Promise<Document>;
28
+ saveDocument(id: DocumentID, data: DocumentData | null): Promise<Document>;
29
+ addAttachments(attachments: Array<InsertDocumentAttachment>): Promise<void>;
30
+ }
31
+ //# sourceMappingURL=db.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"db.d.ts","sourceRoot":"","sources":["../../src/data/db.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAC3C,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAA;AAC1E,OAAO,EAA4C,KAAK,WAAW,EAAE,MAAM,QAAQ,CAAA;AAGnF,OAAO,EAAE,KAAK,qBAAqB,EAAa,MAAM,qBAAqB,CAAA;AAS3E,OAAO,KAAK,EACV,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,wBAAwB,EACxB,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,EACrB,MAAM,YAAY,CAAA;AAEnB,MAAM,MAAM,aAAa,GAAG,qBAAqB,CAAA;AAEjD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,oBAAoB,CAAA;CAC7B,CAAA;AAED,MAAM,MAAM,sBAAsB,GAAG;IACnC,WAAW,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAA;IAClC,EAAE,EAAE,MAAM,CAAA;IACV,YAAY,EAAE,MAAM,CAAA;IACpB,KAAK,EAAE,aAAa,CAAA;CACrB,CAAA;AAED,qBAAa,OAAO;;gBAKN,MAAM,EAAE,aAAa;IAsB3B,KAAK;IAIL,WAAW,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAAC,MAAM,CAAC;IA6BvD,iBAAiB,CAAC,MAAM,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC;IAyChE,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,CAAC;IA4BnD,WAAW,CAAC,EAAE,EAAE,UAAU,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;IAWrD,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAiBpE,cAAc,CAAC,MAAM,EAAE,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC;IA6C3E,cAAc,CAClB,EAAE,EAAE,UAAU,EACd,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,YAAY,GAAG,IAAI,EACzB,MAAM,EAAE,UAAU,GACjB,OAAO,CAAC,QAAQ,CAAC;IAiBd,YAAY,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,YAAY,GAAG,IAAI,GAAG,OAAO,CAAC,QAAQ,CAAC;IAY1E,cAAc,CAAC,WAAW,EAAE,KAAK,CAAC,wBAAwB,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAQlF"}
package/lib/data/db.js ADDED
@@ -0,0 +1,192 @@
1
+ import { Kysely, Migrator, ParseJSONResultsPlugin } from 'kysely';
2
+ import { serializeCursor } from './cursor.js';
3
+ import { getEngine } from './engines/engine.js';
4
+ import { getMigrations } from './migrations/migrations.js';
5
+ import { applyDocumentFilter, applyDocumentOrderBy, applyPagination } from './query-builder.js';
6
+ export class KubunDB {
7
+ #db;
8
+ #engine;
9
+ #ready;
10
+ constructor(params){
11
+ this.#engine = getEngine(params);
12
+ this.#db = new Kysely({
13
+ dialect: this.#engine.getDialect(),
14
+ plugins: [
15
+ new ParseJSONResultsPlugin()
16
+ ]
17
+ });
18
+ this.#ready = this.#runMigrations();
19
+ }
20
+ async #runMigrations() {
21
+ const migrator = new Migrator({
22
+ db: this.#db,
23
+ provider: {
24
+ getMigrations: ()=>Promise.resolve(getMigrations(this.#engine))
25
+ }
26
+ });
27
+ const result = await migrator.migrateToLatest();
28
+ if (result.error != null) {
29
+ throw result.error;
30
+ }
31
+ }
32
+ async close() {
33
+ await this.#db.destroy();
34
+ }
35
+ async createGraph(params) {
36
+ await this.#ready;
37
+ const graphModelID = globalThis.crypto.randomUUID();
38
+ return await this.#db.transaction().setIsolationLevel('read committed').execute(async (trx)=>{
39
+ await trx.insertInto('graph_models').values({
40
+ id: graphModelID,
41
+ name: params.name
42
+ }).execute();
43
+ await Promise.all(Object.entries(params.record).map(async ([id, model])=>{
44
+ await this._addDocumentModel({
45
+ id,
46
+ graphModelID,
47
+ model,
48
+ transaction: trx
49
+ });
50
+ }));
51
+ return graphModelID;
52
+ });
53
+ }
54
+ async _addDocumentModel(params) {
55
+ const { id, graphModelID, model, transaction } = params;
56
+ const t = this.#engine.types;
57
+ await this.#ready;
58
+ await transaction.schema.createTable(`doc_${id}`).ifNotExists().addColumn('id', t.text, (col)=>col.notNull().primaryKey()).addColumn('owner', t.text, (col)=>col.notNull()).addColumn('model', t.text, (col)=>col.notNull()).addColumn('data', t.json).addColumn('unique', t.binary, (col)=>col.notNull()).addColumn('created_at', t.timestamp, (col)=>{
59
+ return col.defaultTo(this.#engine.functions.now).notNull();
60
+ }).addColumn('updated_at', t.timestamp).execute();
61
+ await transaction.insertInto('document_models').values({
62
+ id,
63
+ version: model.version,
64
+ name: model.name,
65
+ behavior: model.behavior,
66
+ set_fields: model.behavior === 'set' ? JSON.stringify(model.setFields) : null,
67
+ interfaces: JSON.stringify(model.interfaces),
68
+ schema: JSON.stringify(model.schema),
69
+ fields_meta: JSON.stringify(model.fieldsMeta)
70
+ }).onConflict((oc)=>oc.doNothing()).execute();
71
+ await transaction.insertInto('graph_document_models').values({
72
+ graph_model_id: graphModelID,
73
+ document_model_id: id
74
+ }).onConflict((oc)=>oc.doNothing()).execute();
75
+ }
76
+ async getGraph(id) {
77
+ await this.#ready;
78
+ const record = {};
79
+ const rows = await this.#db.selectFrom('graph_document_models').innerJoin('document_models', 'graph_document_models.document_model_id', 'document_models.id').selectAll('document_models').where('graph_document_models.graph_model_id', '=', id).execute();
80
+ for (const row of rows){
81
+ const model = {
82
+ version: row.version,
83
+ name: row.name,
84
+ behavior: row.behavior,
85
+ interfaces: row.interfaces,
86
+ schema: row.schema,
87
+ fieldsMeta: row.fields_meta
88
+ };
89
+ if (model.behavior === 'set') {
90
+ model.setFields = row.set_fields;
91
+ }
92
+ record[row.id] = model;
93
+ }
94
+ return record;
95
+ }
96
+ async getDocument(id) {
97
+ await this.#ready;
98
+ const doc = await this.#db// @ts-ignore unknown table by Kysely
99
+ .selectFrom(`doc_${id.model.toString()}`).selectAll().where('id', '=', id.toString()).executeTakeFirst();
100
+ return doc ?? null;
101
+ }
102
+ async listDocuments(params) {
103
+ await this.#ready;
104
+ const [first, ...rest] = params.modelIDs;
105
+ let query = this.#db// @ts-ignore unknown table by Kysely
106
+ .selectFrom(`doc_${first}`).selectAll().where('id', 'in', params.docIDS);
107
+ for (const model of rest){
108
+ query = query.unionAll((eb)=>{
109
+ // @ts-ignore unknown table by Kysely
110
+ return eb.selectFrom(`doc_${model}`).selectAll().where('id', 'in', params.docIDS);
111
+ });
112
+ }
113
+ return await query.execute();
114
+ }
115
+ async queryDocuments(params) {
116
+ const { filter, orderBy, ...pagination } = params;
117
+ const isReverse = params.last != null;
118
+ await this.#ready;
119
+ let query = this.#db// @ts-ignore unknown table by Kysely
120
+ .selectFrom(`doc_${params.model}`).selectAll();
121
+ if (filter != null) {
122
+ query = query.where((eb)=>applyDocumentFilter(eb, filter));
123
+ }
124
+ const [orderedQuery, orderedFieldsPaths] = applyDocumentOrderBy(query, orderBy, isReverse);
125
+ const [paginatedQuery, limit] = applyPagination(orderedQuery, pagination, orderBy);
126
+ const results = await paginatedQuery.execute();
127
+ const docs = isReverse ? results.slice(0, limit).reverse() : results.slice(0, limit);
128
+ const entries = orderedFieldsPaths ? docs.map((document)=>{
129
+ const values = {};
130
+ // Iterate through all the fields that we order by
131
+ for (const path of orderedFieldsPaths){
132
+ let value = document.data;
133
+ // Iterate through all the keys in the path
134
+ for (const key of path){
135
+ if (value != null) {
136
+ value = value[key];
137
+ }
138
+ }
139
+ // Add the value if set
140
+ if (value != null) {
141
+ values[path.join('.')] = value;
142
+ }
143
+ }
144
+ const cursor = serializeCursor({
145
+ id: document.id,
146
+ values
147
+ });
148
+ return {
149
+ cursor,
150
+ document
151
+ };
152
+ }) : docs.map((document)=>{
153
+ const cursor = serializeCursor({
154
+ id: document.id,
155
+ ts: document.created_at.getTime()
156
+ });
157
+ return {
158
+ cursor,
159
+ document
160
+ };
161
+ });
162
+ return {
163
+ entries,
164
+ hasMore: docs.length > limit
165
+ };
166
+ }
167
+ async createDocument(id, owner, data, unique) {
168
+ await this.#ready;
169
+ const doc = await this.#db// @ts-ignore unknown table by Kysely
170
+ .insertInto(`doc_${id.model.toString()}`).values({
171
+ id: id.toString(),
172
+ owner,
173
+ model: id.model.toString(),
174
+ data: this.#engine.encodeJSON(data),
175
+ unique: this.#engine.encodeBinary(unique)
176
+ }).returningAll().executeTakeFirst();
177
+ return doc;
178
+ }
179
+ async saveDocument(id, data) {
180
+ await this.#ready;
181
+ const doc = await this.#db// @ts-ignore unknown table by Kysely
182
+ .updateTable(`doc_${id.model.toString()}`).set({
183
+ data: this.#engine.encodeJSON(data),
184
+ updated_at: new Date().toISOString()
185
+ }).where('id', '=', id.toString()).returningAll().executeTakeFirst();
186
+ return doc;
187
+ }
188
+ async addAttachments(attachments) {
189
+ await this.#ready;
190
+ await this.#db.insertInto('document_attachments').values(attachments).onConflict((oc)=>oc.doNothing()).execute();
191
+ }
192
+ }
@@ -0,0 +1,6 @@
1
+ import { type PostgresEngineParams } from './postgres.js';
2
+ import { type SQLiteEngineParams } from './sqlite.js';
3
+ import type { Engine } from './types.js';
4
+ export type SupportedEngineParams = PostgresEngineParams | SQLiteEngineParams;
5
+ export declare function getEngine(params: SupportedEngineParams): Engine;
6
+ //# sourceMappingURL=engine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../../../src/data/engines/engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,oBAAoB,EAAE,MAAM,eAAe,CAAA;AACzE,OAAO,EAAgB,KAAK,kBAAkB,EAAE,MAAM,aAAa,CAAA;AACnE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAExC,MAAM,MAAM,qBAAqB,GAAG,oBAAoB,GAAG,kBAAkB,CAAA;AAE7E,wBAAgB,SAAS,CAAC,MAAM,EAAE,qBAAqB,GAAG,MAAM,CAS/D"}
@@ -0,0 +1,12 @@
1
+ import { PostgresEngine } from './postgres.js';
2
+ import { SQLiteEngine } from './sqlite.js';
3
+ export function getEngine(params) {
4
+ switch(params.type){
5
+ case 'postgres':
6
+ return new PostgresEngine(params);
7
+ case 'sqlite':
8
+ return new SQLiteEngine(params);
9
+ default:
10
+ throw new Error(`Unsupported engine type ${params.type}`);
11
+ }
12
+ }
@@ -0,0 +1,25 @@
1
+ import { PostgresDialect } from 'kysely';
2
+ import type { PoolConfig } from 'pg';
3
+ import type { Engine } from './types.js';
4
+ export type PostgresEngineParams = {
5
+ type: 'postgres';
6
+ config: PoolConfig;
7
+ };
8
+ export declare class PostgresEngine implements Engine {
9
+ #private;
10
+ functions: {
11
+ readonly now: "now()";
12
+ };
13
+ types: {
14
+ readonly binary: "bytea";
15
+ readonly json: "jsonb";
16
+ readonly text: "text";
17
+ readonly timestamp: "timestamptz(3)";
18
+ readonly uuid: "uuid";
19
+ };
20
+ constructor(params: PostgresEngineParams);
21
+ getDialect(): PostgresDialect;
22
+ encodeBinary(value: Uint8Array): Uint8Array;
23
+ encodeJSON(value: unknown): unknown;
24
+ }
25
+ //# sourceMappingURL=postgres.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../../src/data/engines/postgres.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,QAAQ,CAAA;AAExC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,IAAI,CAAA;AAEpC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAExC,MAAM,MAAM,oBAAoB,GAAG;IACjC,IAAI,EAAE,UAAU,CAAA;IAChB,MAAM,EAAE,UAAU,CAAA;CACnB,CAAA;AAED,qBAAa,cAAe,YAAW,MAAM;;IAG3C,SAAS;;MAEC;IAEV,KAAK;;;;;;MAOK;gBAEE,MAAM,EAAE,oBAAoB;IAIxC,UAAU;IAIV,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU;IAI3C,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO;CAGpC"}
@@ -0,0 +1,30 @@
1
+ import { PostgresDialect } from 'kysely';
2
+ import pg from 'pg';
3
+ export class PostgresEngine {
4
+ #params;
5
+ functions = {
6
+ now: 'now()'
7
+ };
8
+ types = {
9
+ binary: 'bytea',
10
+ json: 'jsonb',
11
+ text: 'text',
12
+ // Use 3 digits for timestamps to have millisecond precision, same as JS dates
13
+ timestamp: 'timestamptz(3)',
14
+ uuid: 'uuid'
15
+ };
16
+ constructor(params){
17
+ this.#params = params;
18
+ }
19
+ getDialect() {
20
+ return new PostgresDialect({
21
+ pool: new pg.Pool(this.#params.config)
22
+ });
23
+ }
24
+ encodeBinary(value) {
25
+ return value;
26
+ }
27
+ encodeJSON(value) {
28
+ return value;
29
+ }
30
+ }
@@ -0,0 +1,24 @@
1
+ import { SqliteDialect } from 'kysely';
2
+ import type { Engine } from './types.js';
3
+ export type SQLiteEngineParams = {
4
+ type: 'sqlite';
5
+ database: string;
6
+ };
7
+ export declare class SQLiteEngine implements Engine {
8
+ #private;
9
+ functions: {
10
+ readonly now: import("kysely").RawBuilder<string>;
11
+ };
12
+ types: {
13
+ readonly binary: "blob";
14
+ readonly json: "jsonb";
15
+ readonly text: "text";
16
+ readonly timestamp: "integer";
17
+ readonly uuid: "text";
18
+ };
19
+ constructor(params: SQLiteEngineParams);
20
+ getDialect(): SqliteDialect;
21
+ encodeBinary(value: Uint8Array): Buffer;
22
+ encodeJSON(value: unknown): string;
23
+ }
24
+ //# sourceMappingURL=sqlite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../../../src/data/engines/sqlite.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAO,MAAM,QAAQ,CAAA;AAE3C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAExC,MAAM,MAAM,kBAAkB,GAAG;IAC/B,IAAI,EAAE,QAAQ,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,qBAAa,YAAa,YAAW,MAAM;;IAGzC,SAAS;;MAEC;IAEV,KAAK;;;;;;MAMK;gBAEE,MAAM,EAAE,kBAAkB;IAItC,UAAU;IAIV,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM;IAIvC,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM;CAGnC"}
@@ -0,0 +1,29 @@
1
+ import SQLite from 'better-sqlite3';
2
+ import { SqliteDialect, sql } from 'kysely';
3
+ export class SQLiteEngine {
4
+ #params;
5
+ functions = {
6
+ now: sql`(unixepoch())`
7
+ };
8
+ types = {
9
+ binary: 'blob',
10
+ json: 'jsonb',
11
+ text: 'text',
12
+ timestamp: 'integer',
13
+ uuid: 'text'
14
+ };
15
+ constructor(params){
16
+ this.#params = params;
17
+ }
18
+ getDialect() {
19
+ return new SqliteDialect({
20
+ database: new SQLite(this.#params.database)
21
+ });
22
+ }
23
+ encodeBinary(value) {
24
+ return Buffer.from(value);
25
+ }
26
+ encodeJSON(value) {
27
+ return JSON.stringify(value);
28
+ }
29
+ }
@@ -0,0 +1,19 @@
1
+ import type { ColumnDataType, Dialect, RawBuilder } from 'kysely';
2
+ export type ColumnTypes = {
3
+ binary: ColumnDataType;
4
+ json: ColumnDataType;
5
+ text: ColumnDataType;
6
+ timestamp: ColumnDataType;
7
+ uuid: ColumnDataType;
8
+ };
9
+ export type Functions = {
10
+ now: string | RawBuilder<string>;
11
+ };
12
+ export type Engine = {
13
+ functions: Functions;
14
+ types: ColumnTypes;
15
+ getDialect(): Dialect;
16
+ encodeBinary(value: Uint8Array): unknown;
17
+ encodeJSON(value: unknown): unknown;
18
+ };
19
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/data/engines/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAA;AAEjE,MAAM,MAAM,WAAW,GAAG;IACxB,MAAM,EAAE,cAAc,CAAA;IACtB,IAAI,EAAE,cAAc,CAAA;IACpB,IAAI,EAAE,cAAc,CAAA;IACpB,SAAS,EAAE,cAAc,CAAA;IACzB,IAAI,EAAE,cAAc,CAAA;CACrB,CAAA;AAED,MAAM,MAAM,SAAS,GAAG;IACtB,GAAG,EAAE,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,CAAA;CACjC,CAAA;AAED,MAAM,MAAM,MAAM,GAAG;IACnB,SAAS,EAAE,SAAS,CAAA;IACpB,KAAK,EAAE,WAAW,CAAA;IAClB,UAAU,IAAI,OAAO,CAAA;IACrB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAA;IACxC,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAAA;CACpC,CAAA"}
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,24 @@
1
+ import type { DocumentID } from '@kubun/id';
2
+ import { type Connection, type ConnectionArguments } from 'graphql-relay';
3
+ import { SchemaBuilder, type SharedDefinitions } from '../../../graphql/lib/schema.js';
4
+ import type { Document, DocumentData, QueryDocumentsParams } from './types.js';
5
+ export type ContextListDocumentsParams = {
6
+ model: string | null;
7
+ ids: Array<string>;
8
+ };
9
+ export type Context = {
10
+ createDocument: (modelID: string, data: DocumentData) => Promise<Document>;
11
+ listDocuments: (params: ContextListDocumentsParams) => Promise<Array<Document>>;
12
+ loadDocument: (id: DocumentID | string) => Promise<Document | null>;
13
+ queryDocuments: (params: QueryDocumentsParams) => Promise<Connection<Document>>;
14
+ viewerDID: string | null;
15
+ mutatedDocuments?: Record<string, Document>;
16
+ };
17
+ export declare class ServerSchemaBuilder extends SchemaBuilder<Document, Context> {
18
+ getViewer(ctx: Context): string | null;
19
+ loadDocument(id: string, ctx: Context): Promise<Document | null>;
20
+ resolveConnection(model: string | null, args: ConnectionArguments, ctx: Context): Promise<Connection<Document>>;
21
+ resolveList(model: string | null, ids: Array<string>, ctx: Context): Promise<Array<Document>>;
22
+ buildMutations(id: string, definitions: SharedDefinitions<Document, Context>): void;
23
+ }
24
+ //# sourceMappingURL=graphql.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../../src/data/graphql.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AAE3C,OAAO,EACL,KAAK,UAAU,EACf,KAAK,mBAAmB,EAEzB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,aAAa,EAAE,KAAK,iBAAiB,EAAE,MAAM,gCAAgC,CAAA;AAEtF,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAA;AAE9E,MAAM,MAAM,0BAA0B,GAAG;IACvC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;IACpB,GAAG,EAAE,KAAK,CAAC,MAAM,CAAC,CAAA;CACnB,CAAA;AAED,MAAM,MAAM,OAAO,GAAG;IACpB,cAAc,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;IAC1E,aAAa,EAAE,CAAC,MAAM,EAAE,0BAA0B,KAAK,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAA;IAC/E,YAAY,EAAE,CAAC,EAAE,EAAE,UAAU,GAAG,MAAM,KAAK,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAAA;IACnE,cAAc,EAAE,CAAC,MAAM,EAAE,oBAAoB,KAAK,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAA;IAC/E,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;IACxB,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;CAC5C,CAAA;AAED,qBAAa,mBAAoB,SAAQ,aAAa,CAAC,QAAQ,EAAE,OAAO,CAAC;IACvE,SAAS,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI;IAIhC,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;IAIhE,iBAAiB,CACrB,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,IAAI,EAAE,mBAAmB,EACzB,GAAG,EAAE,OAAO,GACX,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;IAI1B,WAAW,CACf,KAAK,EAAE,MAAM,GAAG,IAAI,EACpB,GAAG,EAAE,KAAK,CAAC,MAAM,CAAC,EAClB,GAAG,EAAE,OAAO,GACX,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAI3B,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,iBAAiB,CAAC,QAAQ,EAAE,OAAO,CAAC,GAAG,IAAI;CAgEpF"}
@@ -0,0 +1,106 @@
1
+ import { GraphQLID, GraphQLNonNull } from 'graphql';
2
+ import { mutationWithClientMutationId } from 'graphql-relay';
3
+ import { SchemaBuilder } from '../../../graphql/lib/schema.js';
4
+ export class ServerSchemaBuilder extends SchemaBuilder {
5
+ getViewer(ctx) {
6
+ return ctx.viewerDID;
7
+ }
8
+ async loadDocument(id, ctx) {
9
+ return await ctx.loadDocument(id);
10
+ }
11
+ async resolveConnection(model, args, ctx) {
12
+ return await ctx.queryDocuments({
13
+ ...args,
14
+ model
15
+ });
16
+ }
17
+ async resolveList(model, ids, ctx) {
18
+ return await ctx.listDocuments({
19
+ model,
20
+ ids
21
+ });
22
+ }
23
+ buildMutations(id, definitions) {
24
+ const model = this._record[id];
25
+ if (model == null) {
26
+ throw new Error(`Could not find model id: ${id}`);
27
+ }
28
+ const name = this._aliases[id];
29
+ switch(model.behavior){
30
+ case 'interface':
31
+ // Interfaces don't have mutations
32
+ return;
33
+ case 'list':
34
+ {
35
+ this._mutations[`create${name}`] = mutationWithClientMutationId({
36
+ name: `Create${name}`,
37
+ inputFields: ()=>({
38
+ data: {
39
+ type: new GraphQLNonNull(this._inputObjects[id])
40
+ }
41
+ }),
42
+ outputFields: ()=>({
43
+ ...definitions.queryFields,
44
+ document: {
45
+ type: new GraphQLNonNull(this._types[id])
46
+ }
47
+ }),
48
+ mutateAndGetPayload: async (_input, ctx, info)=>{
49
+ const document = ctx.mutatedDocuments?.[info.path.key];
50
+ return {
51
+ document
52
+ };
53
+ }
54
+ });
55
+ break;
56
+ }
57
+ case 'set':
58
+ {
59
+ this._mutations[`set${name}`] = mutationWithClientMutationId({
60
+ name: `Set${name}`,
61
+ inputFields: ()=>({
62
+ data: {
63
+ type: new GraphQLNonNull(this._inputObjects[id])
64
+ }
65
+ }),
66
+ outputFields: ()=>({
67
+ ...definitions.queryFields,
68
+ document: {
69
+ type: new GraphQLNonNull(this._types[id])
70
+ }
71
+ }),
72
+ mutateAndGetPayload: async (_input, ctx, info)=>{
73
+ const document = ctx.mutatedDocuments?.[info.path.key];
74
+ return {
75
+ document
76
+ };
77
+ }
78
+ });
79
+ break;
80
+ }
81
+ }
82
+ this._mutations[`update${name}`] = mutationWithClientMutationId({
83
+ name: `Update${name}`,
84
+ inputFields: ()=>({
85
+ id: {
86
+ type: new GraphQLNonNull(GraphQLID)
87
+ },
88
+ data: {
89
+ type: this._inputObjects[`${id}-update`]
90
+ }
91
+ }),
92
+ outputFields: ()=>({
93
+ ...definitions.queryFields,
94
+ document: {
95
+ type: this._types[id]
96
+ }
97
+ }),
98
+ mutateAndGetPayload: async (_input, ctx, info)=>{
99
+ const document = ctx.mutatedDocuments?.[info.path.key];
100
+ return {
101
+ document
102
+ };
103
+ }
104
+ });
105
+ }
106
+ }
@@ -0,0 +1,4 @@
1
+ import type { Migration } from 'kysely';
2
+ import type { Engine } from '../engines/types.js';
3
+ export declare function getMigration(engine: Engine): Migration;
4
+ //# sourceMappingURL=0-init.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"0-init.d.ts","sourceRoot":"","sources":["../../../src/data/migrations/0-init.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAU,SAAS,EAAE,MAAM,QAAQ,CAAA;AAE/C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAGjD,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAmDtD"}