@kubun/db 0.5.0 → 0.6.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/db.d.ts +18 -38
- package/lib/db.js +1 -1
- package/lib/migrations/0-init.js +1 -1
- package/lib/migrations/migrations.js +1 -1
- package/lib/query-builder.d.ts +3 -2
- package/lib/query-builder.js +1 -1
- package/lib/types.d.ts +12 -17
- package/package.json +7 -7
- package/lib/migrations/1-add-access-control.d.ts +0 -3
- package/lib/migrations/1-add-access-control.js +0 -1
- package/lib/migrations/2-add-sync.d.ts +0 -3
- package/lib/migrations/2-add-sync.js +0 -1
- package/lib/migrations/3-add-search.d.ts +0 -3
- package/lib/migrations/3-add-search.js +0 -1
package/lib/db.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { EventEmitter } from '@enkaku/event';
|
|
2
2
|
import type { Adapter, AdapterTypes } from '@kubun/db-adapter';
|
|
3
|
-
import {
|
|
3
|
+
import { DocumentID, DocumentModelID } from '@kubun/id';
|
|
4
4
|
import { type Logger } from '@kubun/logger';
|
|
5
5
|
import type { DocumentModel, DocumentModelsRecord, DocumentNode } from '@kubun/protocol';
|
|
6
6
|
import { Kysely, type Migration, type Transaction } from 'kysely';
|
|
7
|
-
import type { Database, DocumentData,
|
|
7
|
+
import type { Database, DocumentData, GraphModel, InsertDocumentAttachment, InsertMutationLogEntry, ListDocumentsParams, MutationLogEntry, QueryDocumentsParams, QueryDocumentsResult, SearchConfig, SearchDocumentResult, SearchDocumentsParams } from './types.js';
|
|
8
8
|
export type Migrations = Record<string, Migration>;
|
|
9
9
|
export type DocumentCreatedEvent = {
|
|
10
10
|
type: 'create';
|
|
@@ -31,7 +31,6 @@ export type GraphModelWithRecord = GraphModel & {
|
|
|
31
31
|
export type DocumentParams = {
|
|
32
32
|
id: DocumentID;
|
|
33
33
|
data: DocumentData | null;
|
|
34
|
-
state: Uint8Array | null;
|
|
35
34
|
};
|
|
36
35
|
export type CreateDocumentParams = DocumentParams & {
|
|
37
36
|
owner: string;
|
|
@@ -67,12 +66,26 @@ export declare class KubunDB<T extends AdapterTypes = AdapterTypes> {
|
|
|
67
66
|
getGraph(id: string): Promise<GraphModelWithRecord | null>;
|
|
68
67
|
getDocumentModel(id: DocumentModelID | string): Promise<DocumentModel>;
|
|
69
68
|
getDocument(id: DocumentID): Promise<DocumentNode | null>;
|
|
70
|
-
getDocumentState(id: string): Promise<Uint8Array | null>;
|
|
71
|
-
getDocumentStates(ids: Array<string>): Promise<Record<string, Uint8Array | null>>;
|
|
72
69
|
listDocuments(params: ListDocumentsParams): Promise<Array<DocumentNode>>;
|
|
73
70
|
queryDocuments(params: QueryDocumentsParams): Promise<QueryDocumentsResult>;
|
|
74
71
|
createDocument(params: CreateDocumentParams): Promise<DocumentNode>;
|
|
75
72
|
saveDocument(params: SaveDocumentParams): Promise<DocumentNode>;
|
|
73
|
+
insertMutationLogEntry(entry: InsertMutationLogEntry): Promise<void>;
|
|
74
|
+
hasMutationHash(hash: string): Promise<boolean>;
|
|
75
|
+
getMutationLogForDocuments(documentIDs: Array<string>): Promise<Array<MutationLogEntry>>;
|
|
76
|
+
getPendingMutations(documentID: string): Promise<Array<MutationLogEntry>>;
|
|
77
|
+
updateMutationStatus(mutationHash: string, status: string): Promise<void>;
|
|
78
|
+
getFieldHLCs(documentID: string): Promise<Record<string, string> | null>;
|
|
79
|
+
updateFieldHLCs(docID: DocumentID, fieldHLCs: Record<string, string>): Promise<void>;
|
|
80
|
+
getDocumentIDsForScope(scopes: Array<{
|
|
81
|
+
modelID: string;
|
|
82
|
+
ownerDID: string;
|
|
83
|
+
}>, excludedDocumentIDs?: Array<string>): Promise<Array<string>>;
|
|
84
|
+
getDocumentMetadataForSync(modelID: string, documentIDs: Array<string>): Promise<Array<{
|
|
85
|
+
id: string;
|
|
86
|
+
model: string;
|
|
87
|
+
owner: string;
|
|
88
|
+
}>>;
|
|
76
89
|
addAttachments(attachments: Array<InsertDocumentAttachment>): Promise<void>;
|
|
77
90
|
getUserModelAccessDefault(ownerDID: string, modelID: string, permissionType: 'read' | 'write'): Promise<{
|
|
78
91
|
level: string;
|
|
@@ -86,41 +99,8 @@ export declare class KubunDB<T extends AdapterTypes = AdapterTypes> {
|
|
|
86
99
|
allowedDIDs: Array<string> | null;
|
|
87
100
|
}): Promise<void>;
|
|
88
101
|
removeUserModelAccessDefaults(ownerDID: string, modelID: string, permissionTypes: Array<string>): Promise<void>;
|
|
89
|
-
storeMutationLog(entry: {
|
|
90
|
-
documentID: string;
|
|
91
|
-
sequenceNumber: string;
|
|
92
|
-
mutationJWT: string;
|
|
93
|
-
authorDID: string;
|
|
94
|
-
}): Promise<void>;
|
|
95
|
-
getMutationLog(documentID: string, fromSequence: number): Promise<Array<DocumentMutationLog>>;
|
|
96
102
|
listDocumentModelIDs(): Promise<Array<string>>;
|
|
97
103
|
queryDocumentsByOwner(modelID: string, ownerDID: string): Promise<Array<DocumentNode>>;
|
|
98
|
-
storeSyncCheckpoint(peerServerID: string, checkpointData: {
|
|
99
|
-
userID: string;
|
|
100
|
-
lastSyncTimestamp: number;
|
|
101
|
-
documentCheckpoints: Record<string, {
|
|
102
|
-
lastKnownHash: string;
|
|
103
|
-
lastSyncedSequence: number;
|
|
104
|
-
}>;
|
|
105
|
-
vectorClock: Record<string, number>;
|
|
106
|
-
}): Promise<boolean>;
|
|
107
|
-
getSyncCheckpoint(peerServerID: string, userID: string): Promise<{
|
|
108
|
-
lastSyncTimestamp: number;
|
|
109
|
-
documentCheckpoints: Record<string, {
|
|
110
|
-
lastKnownHash: string;
|
|
111
|
-
lastSyncedSequence: number;
|
|
112
|
-
}>;
|
|
113
|
-
vectorClock: Record<string, number>;
|
|
114
|
-
} | null>;
|
|
115
|
-
listSyncCheckpoints(peerServerID: string): Promise<Array<{
|
|
116
|
-
userID: string;
|
|
117
|
-
lastSyncTimestamp: number;
|
|
118
|
-
documentCheckpoints: Record<string, {
|
|
119
|
-
lastKnownHash: string;
|
|
120
|
-
lastSyncedSequence: number;
|
|
121
|
-
}>;
|
|
122
|
-
vectorClock: Record<string, number>;
|
|
123
|
-
}>>;
|
|
124
104
|
createSearchIndex(modelID: string, fields: Array<string>): Promise<void>;
|
|
125
105
|
dropSearchIndex(modelID: string): Promise<void>;
|
|
126
106
|
updateSearchEntry(modelID: string, documentID: string, data: Record<string, unknown>, fields: Array<string>): Promise<void>;
|
package/lib/db.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{EventEmitter as e}from"@enkaku/event";import{DocumentModelID as t}from"@kubun/id";import{getKubunLogger as a}from"@kubun/logger";import{Kysely as i,Migrator as s,ParseJSONResultsPlugin as r,sql as n}from"kysely";import{serializeCursor as d}from"./cursor.js";import{getMigrations as o}from"./migrations/migrations.js";import{applyDocumentFilter as l,applyDocumentOrderBy as c,applyPagination as u}from"./query-builder.js";function m(e){let t={version:e.version,name:e.name,behavior:e.behavior,interfaces:e.interfaces,schema:e.schema,fieldsMeta:e.fields_meta};return"unique"===t.behavior&&(t.uniqueFields=e.unique_fields),t}function h(e,t){return{id:t.id,model:t.model,owner:t.owner,data:t.data,createdAt:e.decodeTimestamp(t.created_at),updatedAt:t.updated_at?e.decodeTimestamp(t.updated_at):null}}function _(e,a){let i=t.fromString(a);return i.isLocal?i.toGlobal(t.fromString(e)).toString():a}export class KubunDB{#e;#t;#a=new e;#i;#s;constructor(e){this.#e=e.adapter,this.#t=new i({dialect:this.#e.dialect,plugins:[new r]}),this.#i=e.logger??a("db"),this.#s=this.#r()}get adapter(){return this.#e}get events(){return this.#a}getDB(){return this.#s.then(()=>this.#t)}async #r(){let e=new s({db:this.#t,provider:{getMigrations:()=>Promise.resolve(this.getMigrations())}}),t=await e.migrateToLatest();if(null!=t.error)throw t.error;this.#i.info("ready")}getMigrations(){return o(this.#e)}async close(){await this.#t.destroy(),this.#i.info("closed")}async listGraphs(){return await this.#s,await this.#t.selectFrom("kubun_graph_models").selectAll().execute()}async createGraph(e){await this.#s;let t=e.id??globalThis.crypto.randomUUID(),a=e.name??t;return await this.#t.transaction().setIsolationLevel("read committed").execute(async i=>{await i.insertInto("kubun_graph_models").values({aliases:JSON.stringify(e.aliases??{}),id:t,name:a,search:e.search?JSON.stringify(e.search):null}).onConflict(e=>e.doNothing()).execute();let s=Object.entries(e.record),r=new Set;for(;r.size<s.length;){let e=s.filter(([e,t])=>!r.has(e)&&t.interfaces.every(t=>r.has(_(e,t))));if(0===e.length)throw Error("Could not create graph: no valid model to add");await Promise.all(e.map(async([e,a])=>{await this.#n({id:e,graphModelID:t,model:a,transaction:i}),r.add(e)}))}}),this.#i.debug("created graph {id}",{id:t}),t}async #n(e){let{id:t,graphModelID:a,model:i,transaction:s}=e,r=this.#e.types;for(let e of(await this.#s,await s.schema.createTable(`k_${t}`).ifNotExists().addColumn("id",r.text,e=>e.notNull().primaryKey()).addColumn("owner",r.text,e=>e.notNull()).addColumn("model",r.text,e=>e.notNull()).addColumn("data",r.json).addColumn("unique",r.binary,e=>e.notNull()).addColumn("created_at",r.timestamp,e=>e.defaultTo(this.#e.functions.now).notNull()).addColumn("updated_at",r.timestamp).execute(),await s.insertInto("kubun_document_models").values({id:t,version:i.version,name:i.name,behavior:i.behavior,unique_fields:"unique"===i.behavior?JSON.stringify(i.uniqueFields):null,interfaces:JSON.stringify(i.interfaces),schema:JSON.stringify(i.schema),fields_meta:JSON.stringify(i.fieldsMeta)}).onConflict(e=>e.doNothing()).execute(),i.interfaces))await s.insertInto("kubun_document_model_interfaces").values({interface_id:_(t,e),implementation_id:t}).onConflict(e=>e.doNothing()).execute();await s.insertInto("kubun_graph_document_models").values({graph_model_id:a,document_model_id:t}).onConflict(e=>e.doNothing()).execute()}async getGraph(e){await this.#s;let t=await this.#t.selectFrom("kubun_graph_models").selectAll().where("id","=",e).executeTakeFirst();if(null==t)return null;let a={};for(let t of(await this.#t.selectFrom("kubun_graph_document_models").innerJoin("kubun_document_models","kubun_graph_document_models.document_model_id","kubun_document_models.id").selectAll("kubun_document_models").where("kubun_graph_document_models.graph_model_id","=",e).execute()))a[t.id]=m(t);return{...t,record:a}}async getDocumentModel(e){return await this.#s,m(await this.#t.selectFrom("kubun_document_models").selectAll().where("id","=",e.toString()).executeTakeFirstOrThrow())}async getDocument(e){await this.#s;let t=await this.#t.selectFrom(`k_${e.model.toString()}`).selectAll().where("id","=",e.toString()).executeTakeFirst();return t?h(this.#e,t):null}async getDocumentState(e){await this.#s;let t=await this.#t.selectFrom("kubun_document_states").selectAll().where("id","=",e).executeTakeFirst();return t?.data??null}async getDocumentStates(e){await this.#s;let t=await this.#t.selectFrom("kubun_document_states").selectAll().where("id","in",e).execute(),a={};for(let e of t)a[e.id]=e.data;return e.reduce((e,t)=>(e[t]=a[t]??null,e),{})}async listDocuments(e){await this.#s;let[t,...a]=e.modelIDs,i=this.#t.selectFrom(`k_${t}`).selectAll().where("id","in",e.docIDs);for(let t of a)i=i.unionAll(a=>a.selectFrom(`k_${t}`).selectAll().where("id","in",e.docIDs));return(await i.execute()).map(e=>h(this.#e,e))}async queryDocuments(e){let{filter:t,modelIDs:a,orderBy:i,owner:s,...r}=e,n=null!=e.last;await this.#s;let[o,...m]=a,_=this.#t.selectFrom(`k_${o}`).selectAll();for(let e of(null!=s&&(_=_.where("owner","=",s)),null!=t&&(_=_.where(e=>l(e,t))),m))_=_.unionAll(a=>{let i=a.selectFrom(`k_${e}`).selectAll();return null!=s&&(i=i.where("owner","=",s)),null!=t&&(i=i.where(e=>l(e,t))),i});let[w,p]=c(_,i,n),[y,b]=u(w,r,i),g=await y.execute(),f=n?g.slice(0,b).reverse():g.slice(0,b);return{entries:p?f.map(e=>{let t={};for(let a of p){let i=e.data;for(let e of a)null!=i&&(i=i[e]);null!=i&&(t[a.join(".")]=i)}return{cursor:d({id:e.id,values:t}),document:h(this.#e,e)}}):f.map(e=>({cursor:d({id:e.id,ts:+e.created_at}),document:h(this.#e,e)})),hasMore:f.length>b}}async createDocument(e){await this.#s;let t=e.id.toString(),a=e.id.model.toString(),i=await this.#t.transaction().setIsolationLevel("read committed").execute(async i=>{let s=await i.insertInto(`k_${a}`).values({id:t,owner:e.owner,model:a,data:this.#e.encodeJSON(e.data),unique:this.#e.encodeBinary(e.unique)}).returningAll().executeTakeFirst();return await i.insertInto("kubun_document_states").values({id:t,data:e.state?this.#e.encodeBinary(e.state):null}).execute(),s}),s=h(this.#e,i);return this.#a.emit("document:saved",{type:"create",document:s,getCursor:()=>d({id:i.id,ts:+i.created_at})}),this.#i.debug("created document {id} with model {model}",{id:t,model:a}),s}async saveDocument(e){await this.#s;let t=e.id.toString(),a=await this.#t.transaction().setIsolationLevel("read committed").execute(async a=>{let i=await a.updateTable(`k_${e.id.model.toString()}`).set({data:e.data?this.#e.encodeJSON(e.data):null,updated_at:this.#e.encodeTimestamp(new Date)}).where("id","=",t).returningAll().executeTakeFirst();return await a.updateTable("kubun_document_states").set({data:e.state?this.#e.encodeBinary(e.state):null}).where("id","=",t).execute(),i}),i=h(this.#e,a);return this.#a.emit("document:saved",{type:"update",document:i,previous:e.existing,getCursor:()=>d({id:a.id,ts:+a.created_at})}),this.#i.debug("updated document {id}",{id:t}),i}async addAttachments(e){await this.#s,await this.#t.insertInto("kubun_document_attachments").values(e).onConflict(e=>e.doNothing()).execute()}async getUserModelAccessDefault(e,t,a){await this.#s;let i=await this.#t.selectFrom("kubun_user_model_access_defaults").select(["access_level","allowed_dids"]).where("owner_did","=",e).where("model_id","=",t).where("permission_type","=",a).executeTakeFirst();return i?{level:i.access_level,allowedDIDs:i.allowed_dids}:null}async setUserModelAccessDefault(e){await this.#s,await this.#t.insertInto("kubun_user_model_access_defaults").values({owner_did:e.ownerDID,model_id:e.modelID,permission_type:e.permissionType,access_level:e.accessLevel,allowed_dids:e.allowedDIDs?this.#e.encodeJSON(e.allowedDIDs):null}).onConflict(t=>t.columns(["owner_did","model_id","permission_type"]).doUpdateSet({access_level:e.accessLevel,allowed_dids:e.allowedDIDs?this.#e.encodeJSON(e.allowedDIDs):null,updated_at:this.#e.encodeTimestamp(new Date)})).execute()}async removeUserModelAccessDefaults(e,t,a){await this.#s,await this.#t.deleteFrom("kubun_user_model_access_defaults").where("owner_did","=",e).where("model_id","=",t).where("permission_type","in",a).execute()}async storeMutationLog(e){await this.#s;let t=globalThis.crypto.randomUUID();await this.#t.insertInto("kubun_document_mutation_log").values({id:t,document_id:e.documentID,sequence_number:e.sequenceNumber,mutation_jwt:e.mutationJWT,author_did:e.authorDID,timestamp:Date.now()}).execute(),await this.#t.deleteFrom("kubun_document_mutation_log").where("document_id","=",e.documentID).where("id","not in",this.#t.selectFrom("kubun_document_mutation_log").select("id").where("document_id","=",e.documentID).orderBy(n`CAST(sequence_number AS INTEGER)`,"desc").limit(10)).execute()}async getMutationLog(e,t){return await this.#s,await this.#t.selectFrom("kubun_document_mutation_log").selectAll().where("document_id","=",e).where(n`CAST(sequence_number AS INTEGER)`,">=",t).orderBy(n`CAST(sequence_number AS INTEGER)`,"asc").execute()}async listDocumentModelIDs(){return await this.#s,(await this.#t.selectFrom("kubun_document_models").select("id").execute()).map(e=>e.id)}async queryDocumentsByOwner(e,t){return await this.#s,(await this.#t.selectFrom(`k_${e}`).selectAll().where("owner","=",t).execute()).map(e=>h(this.#e,e))}async storeSyncCheckpoint(e,t){await this.#s;let a=`${e}:${t.userID}`,i=JSON.stringify(t.documentCheckpoints),s=JSON.stringify(t.vectorClock);try{return await this.#t.insertInto("kubun_sync_checkpoints").values({id:a,peer_server_id:e,user_id:t.userID,last_sync_timestamp:t.lastSyncTimestamp,document_checkpoints:i,vector_clock:s}).onConflict(e=>e.column("id").doUpdateSet({last_sync_timestamp:t.lastSyncTimestamp,document_checkpoints:i,vector_clock:s})).execute(),!0}catch{return!1}}async getSyncCheckpoint(e,t){await this.#s;let a=`${e}:${t}`,i=await this.#t.selectFrom("kubun_sync_checkpoints").selectAll().where("id","=",a).executeTakeFirst();return i?{lastSyncTimestamp:i.last_sync_timestamp,documentCheckpoints:i.document_checkpoints,vectorClock:i.vector_clock}:null}async listSyncCheckpoints(e){return await this.#s,(await this.#t.selectFrom("kubun_sync_checkpoints").selectAll().where("peer_server_id","=",e).execute()).map(e=>({userID:e.user_id,lastSyncTimestamp:e.last_sync_timestamp,documentCheckpoints:e.document_checkpoints,vectorClock:e.vector_clock}))}async createSearchIndex(e,t){await this.#s,await this.#e.createSearchIndex(this.#t,{modelID:e,fields:t})}async dropSearchIndex(e){await this.#s,await this.#e.dropSearchIndex(this.#t,e)}async updateSearchEntry(e,t,a,i){await this.#s;let s={};for(let e of i){let t=this.#d(a,e);null!=t&&(s[e]=String(t))}await this.#e.updateSearchEntry(this.#t,e,t,s,i)}async removeSearchEntry(e,t){await this.#s,await this.#e.removeSearchEntry(this.#t,e,t)}async searchDocuments(e){await this.#s;let t=[];for(let a of e.modelIDs)for(let i of(await this.#e.searchIndex(this.#t,{query:e.query,modelID:a,limit:e.first??50})))t.push({documentID:i.documentID,modelID:a,rank:i.rank});return(t.sort((e,t)=>Math.abs(t.rank)-Math.abs(e.rank)),e.first)?t.slice(0,e.first):t}#d(e,t){let a=t.split("."),i=e;for(let e of a){if(null==i||"object"!=typeof i)return null;i=i[e]}return i}}
|
|
1
|
+
import{EventEmitter as e}from"@enkaku/event";import{DocumentID as t,DocumentModelID as a}from"@kubun/id";import{getKubunLogger as i}from"@kubun/logger";import{Kysely as r,Migrator as s,ParseJSONResultsPlugin as n}from"kysely";import{serializeCursor as d}from"./cursor.js";import{getMigrations as o}from"./migrations/migrations.js";import{applyDocumentFilter as l,applyDocumentOrderBy as u,applyPagination as c}from"./query-builder.js";function h(e){let t={version:e.version,name:e.name,behavior:e.behavior,interfaces:e.interfaces,schema:e.schema,fieldsMeta:e.fields_meta};return"unique"===t.behavior&&(t.uniqueFields=e.unique_fields),t}function m(e,t){return{id:t.id,model:t.model,owner:t.owner,data:t.data,createdAt:e.decodeTimestamp(t.created_at),updatedAt:t.updated_at?e.decodeTimestamp(t.updated_at):null}}function w(e,t){let i=a.fromString(t);return i.isLocal?i.toGlobal(a.fromString(e)).toString():t}export class KubunDB{#e;#t;#a=new e;#i;#r;constructor(e){this.#e=e.adapter,this.#t=new r({dialect:this.#e.dialect,plugins:[new n]}),this.#i=e.logger??i("db"),this.#r=this.#s()}get adapter(){return this.#e}get events(){return this.#a}getDB(){return this.#r.then(()=>this.#t)}async #s(){let e=new s({db:this.#t,provider:{getMigrations:()=>Promise.resolve(this.getMigrations())}}),t=await e.migrateToLatest();if(null!=t.error)throw t.error;this.#i.info("ready")}getMigrations(){return o(this.#e)}async close(){await this.#r,await this.#t.destroy(),this.#i.info("closed")}async listGraphs(){return await this.#r,await this.#t.selectFrom("kubun_graph_models").selectAll().execute()}async createGraph(e){await this.#r;let t=e.id??globalThis.crypto.randomUUID(),a=e.name??t;return await this.#t.transaction().setIsolationLevel("read committed").execute(async i=>{await i.insertInto("kubun_graph_models").values({aliases:JSON.stringify(e.aliases??{}),id:t,name:a,search:e.search?JSON.stringify(e.search):null}).onConflict(e=>e.doNothing()).execute();let r=Object.entries(e.record),s=new Set;for(;s.size<r.length;){let e=r.filter(([e,t])=>!s.has(e)&&t.interfaces.every(t=>s.has(w(e,t))));if(0===e.length)throw Error("Could not create graph: no valid model to add");await Promise.all(e.map(async([e,a])=>{await this.#n({id:e,graphModelID:t,model:a,transaction:i}),s.add(e)}))}}),this.#i.debug("created graph {id}",{id:t}),t}async #n(e){let{id:t,graphModelID:a,model:i,transaction:r}=e,s=this.#e.types;for(let e of(await this.#r,await r.schema.createTable(`k_${t}`).ifNotExists().addColumn("id",s.text,e=>e.notNull().primaryKey()).addColumn("owner",s.text,e=>e.notNull()).addColumn("model",s.text,e=>e.notNull()).addColumn("data",s.json).addColumn("field_hlcs",s.json).addColumn("unique",s.binary,e=>e.notNull()).addColumn("created_at",s.timestamp,e=>e.defaultTo(this.#e.functions.now).notNull()).addColumn("updated_at",s.timestamp).execute(),await r.insertInto("kubun_document_models").values({id:t,version:i.version,name:i.name,behavior:i.behavior,unique_fields:"unique"===i.behavior?JSON.stringify(i.uniqueFields):null,interfaces:JSON.stringify(i.interfaces),schema:JSON.stringify(i.schema),fields_meta:JSON.stringify(i.fieldsMeta)}).onConflict(e=>e.doNothing()).execute(),i.interfaces))await r.insertInto("kubun_document_model_interfaces").values({interface_id:w(t,e),implementation_id:t}).onConflict(e=>e.doNothing()).execute();await r.insertInto("kubun_graph_document_models").values({graph_model_id:a,document_model_id:t}).onConflict(e=>e.doNothing()).execute()}async getGraph(e){await this.#r;let t=await this.#t.selectFrom("kubun_graph_models").selectAll().where("id","=",e).executeTakeFirst();if(null==t)return null;let a={};for(let t of(await this.#t.selectFrom("kubun_graph_document_models").innerJoin("kubun_document_models","kubun_graph_document_models.document_model_id","kubun_document_models.id").selectAll("kubun_document_models").where("kubun_graph_document_models.graph_model_id","=",e).execute()))a[t.id]=h(t);return{...t,record:a}}async getDocumentModel(e){return await this.#r,h(await this.#t.selectFrom("kubun_document_models").selectAll().where("id","=",e.toString()).executeTakeFirstOrThrow())}async getDocument(e){await this.#r;let t=await this.#t.selectFrom(`k_${e.model.toString()}`).selectAll().where("id","=",e.toString()).executeTakeFirst();return t?m(this.#e,t):null}async listDocuments(e){await this.#r;let[t,...a]=e.modelIDs,i=this.#t.selectFrom(`k_${t}`).selectAll().where("id","in",e.docIDs);for(let t of a)i=i.unionAll(a=>a.selectFrom(`k_${t}`).selectAll().where("id","in",e.docIDs));return(await i.execute()).map(e=>m(this.#e,e))}async queryDocuments(e){let{filter:t,modelIDs:a,orderBy:i,owner:r,...s}=e,n=null!=e.last;await this.#r;let o=this.#e.coerceFilterValue.bind(this.#e),[h,...w]=a,_=this.#t.selectFrom(`k_${h}`).selectAll();for(let e of(null!=r&&(_=_.where("owner","=",r)),null!=t&&(_=_.where(e=>l(e,t,[],o))),w))_=_.unionAll(a=>{let i=a.selectFrom(`k_${e}`).selectAll();return null!=r&&(i=i.where("owner","=",r)),null!=t&&(i=i.where(e=>l(e,t,[],o))),i});let[p,y]=u(_,i,n),[g,f]=c(p,s,i),b=await g.execute(),k=n?b.slice(0,f).reverse():b.slice(0,f);return{entries:y?k.map(e=>{let t={};for(let a of y){let i=e.data;for(let e of a)null!=i&&(i=i[e]);null!=i&&(t[a.join(".")]=i)}return{cursor:d({id:e.id,values:t}),document:m(this.#e,e)}}):k.map(e=>({cursor:d({id:e.id,ts:+e.created_at}),document:m(this.#e,e)})),hasMore:k.length>f}}async createDocument(e){await this.#r;let t=e.id.toString(),a=e.id.model.toString(),i=await this.#t.transaction().setIsolationLevel("read committed").execute(async i=>await i.insertInto(`k_${a}`).values({id:t,owner:e.owner,model:a,data:this.#e.encodeJSON(e.data),unique:this.#e.encodeBinary(e.unique)}).returningAll().executeTakeFirst()),r=m(this.#e,i);return this.#a.emit("document:saved",{type:"create",document:r,getCursor:()=>d({id:i.id,ts:+i.created_at})}),this.#i.debug("created document {id} with model {model}",{id:t,model:a}),r}async saveDocument(e){await this.#r;let t=e.id.toString(),a=await this.#t.transaction().setIsolationLevel("read committed").execute(async a=>await a.updateTable(`k_${e.id.model.toString()}`).set({data:e.data?this.#e.encodeJSON(e.data):null,updated_at:this.#e.encodeTimestamp(new Date)}).where("id","=",t).returningAll().executeTakeFirst()),i=m(this.#e,a);return this.#a.emit("document:saved",{type:"update",document:i,previous:e.existing,getCursor:()=>d({id:a.id,ts:+a.created_at})}),this.#i.debug("updated document {id}",{id:t}),i}async insertMutationLogEntry(e){await this.#r,await this.#t.insertInto("kubun_mutation_log").values(e).onConflict(e=>e.doNothing()).execute()}async hasMutationHash(e){return await this.#r,null!=await this.#t.selectFrom("kubun_mutation_log").select("mutation_hash").where("mutation_hash","=",e).executeTakeFirst()}async getMutationLogForDocuments(e){return 0===e.length?[]:(await this.#r,await this.#t.selectFrom("kubun_mutation_log").selectAll().where("document_id","in",e).orderBy("hlc","asc").execute())}async getPendingMutations(e){return await this.#r,await this.#t.selectFrom("kubun_mutation_log").selectAll().where("document_id","=",e).where("status","=","pending").orderBy("hlc","asc").execute()}async updateMutationStatus(e,t){await this.#r,await this.#t.updateTable("kubun_mutation_log").set({status:t}).where("mutation_hash","=",e).execute()}async getFieldHLCs(e){await this.#r;let a=t.fromString(e).model.toString(),i=await this.#t.selectFrom(`k_${a}`).select("field_hlcs").where("id","=",e).executeTakeFirst();if(i?.field_hlcs==null)return null;let r=i.field_hlcs;return"string"==typeof r?JSON.parse(r):r}async updateFieldHLCs(e,t){await this.#r;let a=e.model.toString();await this.#t.updateTable(`k_${a}`).set({field_hlcs:this.#e.encodeJSON(t)}).where("id","=",e.toString()).execute()}async getDocumentIDsForScope(e,t=[]){await this.#r;let a=[];for(let t of e)try{let e=await this.#t.selectFrom(`k_${t.modelID}`).select("id").where("owner","=",t.ownerDID).execute();a.push(...e.map(e=>e.id))}catch{}if(t.length>0){let e=new Set(t);return a.filter(t=>!e.has(t))}return a}async getDocumentMetadataForSync(e,t){return 0===t.length?[]:(await this.#r,(await this.#t.selectFrom(`k_${e}`).select(["id","model","owner"]).where("id","in",t).execute()).map(e=>({id:e.id,model:e.model,owner:e.owner})))}async addAttachments(e){await this.#r,await this.#t.insertInto("kubun_document_attachments").values(e).onConflict(e=>e.doNothing()).execute()}async getUserModelAccessDefault(e,t,a){await this.#r;let i=await this.#t.selectFrom("kubun_user_model_access_defaults").select(["access_level","allowed_dids"]).where("owner_did","=",e).where("model_id","=",t).where("permission_type","=",a).executeTakeFirst();return i?{level:i.access_level,allowedDIDs:i.allowed_dids}:null}async setUserModelAccessDefault(e){await this.#r,await this.#t.insertInto("kubun_user_model_access_defaults").values({owner_did:e.ownerDID,model_id:e.modelID,permission_type:e.permissionType,access_level:e.accessLevel,allowed_dids:e.allowedDIDs?this.#e.encodeJSON(e.allowedDIDs):null}).onConflict(t=>t.columns(["owner_did","model_id","permission_type"]).doUpdateSet({access_level:e.accessLevel,allowed_dids:e.allowedDIDs?this.#e.encodeJSON(e.allowedDIDs):null,updated_at:this.#e.encodeTimestamp(new Date)})).execute()}async removeUserModelAccessDefaults(e,t,a){await this.#r,await this.#t.deleteFrom("kubun_user_model_access_defaults").where("owner_did","=",e).where("model_id","=",t).where("permission_type","in",a).execute()}async listDocumentModelIDs(){return await this.#r,(await this.#t.selectFrom("kubun_document_models").select("id").execute()).map(e=>e.id)}async queryDocumentsByOwner(e,t){return await this.#r,(await this.#t.selectFrom(`k_${e}`).selectAll().where("owner","=",t).execute()).map(e=>m(this.#e,e))}async createSearchIndex(e,t){await this.#r,await this.#e.createSearchIndex(this.#t,{modelID:e,fields:t})}async dropSearchIndex(e){await this.#r,await this.#e.dropSearchIndex(this.#t,e)}async updateSearchEntry(e,t,a,i){await this.#r;let r={};for(let e of i){let t=this.#d(a,e);null!=t&&(r[e]=String(t))}await this.#e.updateSearchEntry(this.#t,e,t,r,i)}async removeSearchEntry(e,t){await this.#r,await this.#e.removeSearchEntry(this.#t,e,t)}async searchDocuments(e){await this.#r;let t=[];for(let a of e.modelIDs)for(let i of(await this.#e.searchIndex(this.#t,{query:e.query,modelID:a,limit:e.first??50})))t.push({documentID:i.documentID,modelID:a,rank:i.rank});return(t.sort((e,t)=>Math.abs(t.rank)-Math.abs(e.rank)),e.first)?t.slice(0,e.first):t}#d(e,t){let a=t.split("."),i=e;for(let e of a){if(null==i||"object"!=typeof i)return null;i=i[e]}return i}}
|
package/lib/migrations/0-init.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export function getMigration(e){let t=e.types;return{up:async function(a){await a.schema.createTable("kubun_document_attachments").ifNotExists().addColumn("id",t.text,e=>e.notNull().primaryKey()).addColumn("data",t.binary,e=>e.notNull()).addColumn("created_at",t.timestamp,t=>t.defaultTo(e.functions.now).notNull()).execute(),await a.schema.createTable("kubun_document_models").ifNotExists().addColumn("id",t.text,e=>e.notNull().primaryKey()).addColumn("version",t.text,e=>e.notNull()).addColumn("name",t.text,e=>e.notNull()).addColumn("behavior",t.text,e=>e.notNull()).addColumn("unique_fields",t.json).addColumn("interfaces",t.json,e=>e.notNull()).addColumn("schema",t.json,e=>e.notNull()).addColumn("fields_meta",t.json,e=>e.notNull()).addColumn("created_at",t.timestamp,t=>t.defaultTo(e.functions.now).notNull()).execute(),await a.schema.createTable("kubun_document_model_interfaces").ifNotExists().addColumn("interface_id",t.text,e=>e.notNull().references("kubun_document_models.id")).addColumn("implementation_id",t.text,e=>e.notNull().references("kubun_document_models.id")).addUniqueConstraint("kubun_document_model_interfaces_pkey",["interface_id","implementation_id"]).execute(),await a.schema.createTable("
|
|
1
|
+
export function getMigration(e){let t=e.types;return{up:async function(a){await a.schema.createTable("kubun_document_attachments").ifNotExists().addColumn("id",t.text,e=>e.notNull().primaryKey()).addColumn("data",t.binary,e=>e.notNull()).addColumn("created_at",t.timestamp,t=>t.defaultTo(e.functions.now).notNull()).execute(),await a.schema.createTable("kubun_document_models").ifNotExists().addColumn("id",t.text,e=>e.notNull().primaryKey()).addColumn("version",t.text,e=>e.notNull()).addColumn("name",t.text,e=>e.notNull()).addColumn("behavior",t.text,e=>e.notNull()).addColumn("unique_fields",t.json).addColumn("interfaces",t.json,e=>e.notNull()).addColumn("schema",t.json,e=>e.notNull()).addColumn("fields_meta",t.json,e=>e.notNull()).addColumn("created_at",t.timestamp,t=>t.defaultTo(e.functions.now).notNull()).execute(),await a.schema.createTable("kubun_document_model_interfaces").ifNotExists().addColumn("interface_id",t.text,e=>e.notNull().references("kubun_document_models.id")).addColumn("implementation_id",t.text,e=>e.notNull().references("kubun_document_models.id")).addUniqueConstraint("kubun_document_model_interfaces_pkey",["interface_id","implementation_id"]).execute(),await a.schema.createTable("kubun_graph_models").ifNotExists().addColumn("id",t.text,e=>e.notNull().primaryKey()).addColumn("name",t.text,e=>e.notNull()).addColumn("aliases",t.json,e=>e.defaultTo("{}").notNull()).addColumn("search",t.json).addColumn("created_at",t.timestamp,t=>t.defaultTo(e.functions.now).notNull()).addColumn("updated_at",t.timestamp).execute(),await a.schema.createTable("kubun_graph_document_models").ifNotExists().addColumn("graph_model_id",t.text,e=>e.notNull().references("kubun_graph_models.id")).addColumn("document_model_id",t.text,e=>e.notNull().references("kubun_document_models.id")).addUniqueConstraint("kubun_graph_document_models_pkey",["graph_model_id","document_model_id"]).execute(),await a.schema.createTable("kubun_user_model_access_defaults").ifNotExists().addColumn("owner_did",t.text,e=>e.notNull()).addColumn("model_id",t.text,e=>e.notNull()).addColumn("permission_type",t.text,e=>e.notNull()).addColumn("access_level",t.text,e=>e.notNull()).addColumn("allowed_dids",t.json).addColumn("created_at",t.timestamp,t=>t.defaultTo(e.functions.now).notNull()).addColumn("updated_at",t.timestamp).addPrimaryKeyConstraint("kubun_user_model_access_defaults_pkey",["owner_did","model_id","permission_type"]).execute(),await a.schema.createIndex("idx_user_model_access_owner_model").on("kubun_user_model_access_defaults").columns(["owner_did","model_id"]).execute(),await a.schema.createTable("kubun_sync_checkpoints").ifNotExists().addColumn("id",t.text,e=>e.notNull().primaryKey()).addColumn("peer_server_id",t.text,e=>e.notNull()).addColumn("user_id",t.text,e=>e.notNull()).addColumn("last_sync_timestamp",t.timestamp,e=>e.notNull()).addColumn("document_checkpoints",t.json,e=>e.notNull()).addColumn("vector_clock",t.json,e=>e.notNull()).execute(),await a.schema.createIndex("idx_sync_checkpoints_peer_user").on("kubun_sync_checkpoints").columns(["peer_server_id","user_id"]).execute(),await a.schema.createTable("kubun_sync_peers").ifNotExists().addColumn("id",t.text,e=>e.notNull().primaryKey()).addColumn("peer_did",t.text,e=>e.notNull().unique()).addColumn("endpoint",t.text,e=>e.notNull()).addColumn("mode",t.text,e=>e.notNull()).addColumn("allowed_users",t.json,e=>e.notNull()).addColumn("priority",t.text,e=>e.notNull().defaultTo("0")).addColumn("trust_level",t.text,e=>e.notNull()).addColumn("config",t.json,e=>e.notNull()).addColumn("created_at",t.timestamp,t=>t.defaultTo(e.functions.now).notNull()).addColumn("updated_at",t.timestamp,t=>t.defaultTo(e.functions.now).notNull()).execute(),await a.schema.createTable("kubun_mutation_log").ifNotExists().addColumn("mutation_hash",t.text,e=>e.primaryKey()).addColumn("model_id",t.text,e=>e.notNull()).addColumn("document_id",t.text,e=>e.notNull()).addColumn("author_did",t.text,e=>e.notNull()).addColumn("hlc",t.text,e=>e.notNull()).addColumn("mutation_jwt",t.text,e=>e.notNull()).addColumn("status",t.text,e=>e.notNull().defaultTo("applied")).execute(),await a.schema.createIndex("idx_mutation_log_hlc").on("kubun_mutation_log").column("hlc").execute(),await a.schema.createIndex("idx_mutation_log_document").on("kubun_mutation_log").column("document_id").execute(),await a.schema.createIndex("idx_mutation_log_status").on("kubun_mutation_log").column("status").execute()},down:async function(e){await e.schema.dropTable("kubun_mutation_log").ifExists().execute(),await e.schema.dropTable("kubun_sync_peers").ifExists().execute(),await e.schema.dropTable("kubun_sync_checkpoints").ifExists().execute(),await e.schema.dropTable("kubun_user_model_access_defaults").ifExists().execute(),await e.schema.dropTable("kubun_graph_document_models").ifExists().execute(),await e.schema.dropTable("kubun_graph_models").ifExists().execute(),await e.schema.dropTable("kubun_document_model_interfaces").ifExists().execute(),await e.schema.dropTable("kubun_document_models").ifExists().execute(),await e.schema.dropTable("kubun_document_attachments").ifExists().execute()}}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{getMigration as i}from"./0-init.js";
|
|
1
|
+
import{getMigration as i}from"./0-init.js";export function getMigrations(t){return{kubunMigration0:i(t)}}
|
package/lib/query-builder.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { AnyValueFilter, DocumentFilter, DocumentOrderBy } from '@kubun/protocol';
|
|
2
2
|
import type { ExpressionBuilder, ExpressionWrapper, SelectQueryBuilder } from 'kysely';
|
|
3
3
|
import type { ConnectionArguments, Database, Document } from './types.js';
|
|
4
|
+
export type CoerceFilterValue = (value: unknown) => unknown;
|
|
4
5
|
type DocumentExpressionBuilder = ExpressionBuilder<Database, 'k_document'>;
|
|
5
6
|
type DocumentExpressionWrapper = ExpressionWrapper<Database, 'k_document', any>;
|
|
6
7
|
export type DocumentQueryBuilder = SelectQueryBuilder<Database, 'k_document', Document>;
|
|
7
8
|
export declare function applyPagination(query: DocumentQueryBuilder, args: ConnectionArguments, orderBy?: DocumentOrderBy): [DocumentQueryBuilder, number];
|
|
8
|
-
export declare function applyDocumentFilter(eb: DocumentExpressionBuilder, filter: DocumentFilter, path?: Array<string
|
|
9
|
-
export declare function applyValueFilter(eb: DocumentExpressionBuilder, keys: Array<string>, filter: AnyValueFilter): DocumentExpressionWrapper;
|
|
9
|
+
export declare function applyDocumentFilter(eb: DocumentExpressionBuilder, filter: DocumentFilter, path?: Array<string>, coerce?: CoerceFilterValue): DocumentExpressionWrapper;
|
|
10
|
+
export declare function applyValueFilter(eb: DocumentExpressionBuilder, keys: Array<string>, filter: AnyValueFilter, coerce?: CoerceFilterValue): DocumentExpressionWrapper;
|
|
10
11
|
export declare function applyDocumentOrderBy(queryBuilder: DocumentQueryBuilder, orderBy?: DocumentOrderBy, isReverse?: boolean): [DocumentQueryBuilder, Array<Array<string>> | null];
|
|
11
12
|
export {};
|
package/lib/query-builder.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{parseCursor as e}from"./cursor.js";function t(e=50){return Math.min(e,100)}function r(e,t){let r=e.ref("data","->>");for(let e of t)r=r.key(e);return r}function n(e){let t=[],r=e;do{let[e,n]=Object.entries(r)[0];if(t.push(e),"string"==typeof n)return{keys:t,direction:n};r=n}while(null!=r)throw Error("Could not extract field entry")}function l(e,t=!1){return t?"asc"===e?"desc":"asc":e}export function applyPagination(l,a,o=[]){let{first:i,last:u,before:c,after:d}=a;if(null!=i){let a=t(i);return[function(t,l,a,o){let i=t;if(null!=a){let{id:t,ts:l,values:u}=e(a);null!=l?i=i.where(e=>e.or([e("created_at",">",l),e("created_at","=",l).and("id",">",t)])):null!=u&&(i=i.where(e=>{let l=[];for(let a of o){let o=n(a),i=u[o.keys.join(".")];if(null==i)continue;let c=r(e,o.keys);l.push(e.or([e(c,"asc"===o.direction?">":"<",i),e(c,"=",i).and("id",">",t)]))}return e.and(l)}))}return i.orderBy("created_at","asc").limit(l)}(l,a+1,d,o),a]}if(null!=u){let a=t(u);return[function(t,l,a,o){let i=t;if(null!=a){let{id:t,ts:l,values:u}=e(a);null!=l?i=i.where(e=>e.or([e("created_at","<",l),e("created_at","=",l).and("id","<",t)])):null!=u&&(i=i.where(e=>{let l=[];for(let a of o){let o=n(a),i=u[o.keys.join(".")];if(null==i)continue;let c=r(e,o.keys);l.push(e.or([e(c,"asc"===o.direction?"<":">",i),e(c,"=",i).and("id",">",t)]))}return e.and(l)}))}return i.orderBy("created_at","desc").limit(l)}(l,a+1,c,o),a]}let s=t();return[l.orderBy("created_at","asc").limit(s+1),s]}export function applyDocumentFilter(e,t,r=[]){let
|
|
1
|
+
import{parseCursor as e}from"./cursor.js";function t(e=50){return Math.min(e,100)}function r(e,t){let r=e.ref("data","->>");for(let e of t)r=r.key(e);return r}function n(e){let t=[],r=e;do{let[e,n]=Object.entries(r)[0];if(t.push(e),"string"==typeof n)return{keys:t,direction:n};r=n}while(null!=r)throw Error("Could not extract field entry")}function l(e,t=!1){return t?"asc"===e?"desc":"asc":e}export function applyPagination(l,a,o=[]){let{first:i,last:u,before:c,after:d}=a;if(null!=i){let a=t(i);return[function(t,l,a,o){let i=t;if(null!=a){let{id:t,ts:l,values:u}=e(a);null!=l?i=i.where(e=>e.or([e("created_at",">",l),e("created_at","=",l).and("id",">",t)])):null!=u&&(i=i.where(e=>{let l=[];for(let a of o){let o=n(a),i=u[o.keys.join(".")];if(null==i)continue;let c=r(e,o.keys);l.push(e.or([e(c,"asc"===o.direction?">":"<",i),e(c,"=",i).and("id",">",t)]))}return e.and(l)}))}return i.orderBy("created_at","asc").limit(l)}(l,a+1,d,o),a]}if(null!=u){let a=t(u);return[function(t,l,a,o){let i=t;if(null!=a){let{id:t,ts:l,values:u}=e(a);null!=l?i=i.where(e=>e.or([e("created_at","<",l),e("created_at","=",l).and("id","<",t)])):null!=u&&(i=i.where(e=>{let l=[];for(let a of o){let o=n(a),i=u[o.keys.join(".")];if(null==i)continue;let c=r(e,o.keys);l.push(e.or([e(c,"asc"===o.direction?"<":">",i),e(c,"=",i).and("id",">",t)]))}return e.and(l)}))}return i.orderBy("created_at","desc").limit(l)}(l,a+1,c,o),a]}let s=t();return[l.orderBy("created_at","asc").limit(s+1),s]}export function applyDocumentFilter(e,t,r=[],n){let l=Object.entries(t);if(1!==l.length)throw Error("Invalid document filter");let[a,o]=l[0];switch(a){case"where":var i,u,c,d;let s;return i=e,u=o,c=r,d=n,s=Object.entries(u).map(([e,t])=>applyValueFilter(i,[...c,e],t,d)),i.and(s);case"and":return e.and(o.map(t=>applyDocumentFilter(e,t,r,n)));case"or":return e.or(o.map(t=>applyDocumentFilter(e,t,r,n)));case"not":return e.not(applyDocumentFilter(e,o,r,n));default:throw Error(`Invalid document filter type: ${a}`)}}export function applyValueFilter(e,t,n,l){let a=Object.entries(n);if(1!==a.length)throw Error("Invalid value filter");let o=r(e,t),[i,u]=a[0],c=l??(e=>e);switch(i){case"isNull":return e(o,!0===u?"is":"is not",null);case"equalTo":return e(o,"=",c(u));case"notEqualTo":return e(o,"!=",c(u));case"in":return e(o,"in",u.map(c));case"notIn":return e(o,"not in",u.map(c));case"lessThan":return e(o,"<",c(u));case"lessThanOrEqualTo":return e(o,"<=",c(u));case"greaterThan":return e(o,">",c(u));case"greaterThanOrEqualTo":return e(o,">=",c(u));default:throw Error(`Invalid value filter type: ${i}`)}}export function applyDocumentOrderBy(e,t=[],n=!1){if(0===t.length)return[e.orderBy("created_at",l("asc",n)),null];let o=e,i=[];for(let e of t){let[t,u]=function e(t,n,o,i=[]){let u=Object.entries(n);if(1!==u.length)throw Error("Invalid order by field");let[c,d]=u[0],s=[...i,c],f=a[c];return null!=f?[t.orderBy(f,l(d,o)),s]:"string"==typeof d?[t.orderBy(e=>r(e,s),l(d,o)),s]:e(t,d,o,s)}(o,e,n);o=t,i.push(u)}return[o,i]}let a={_createdAt:"created_at",_owner:"owner"};
|
package/lib/types.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ export type DocumentTable<Data extends DocumentData = DocumentData> = {
|
|
|
14
14
|
owner: string;
|
|
15
15
|
model: string;
|
|
16
16
|
data: JSONColumnType<Data> | null;
|
|
17
|
+
field_hlcs: JSONColumnType<Record<string, string>> | null;
|
|
17
18
|
unique: Uint8Array;
|
|
18
19
|
created_at: CreatedAtColumn;
|
|
19
20
|
updated_at: UpdatedAtColumn;
|
|
@@ -49,12 +50,17 @@ export type DocumentModelInterfaceTable = {
|
|
|
49
50
|
};
|
|
50
51
|
export type DocumentModelInterface = Selectable<DocumentModelInterfaceTable>;
|
|
51
52
|
export type InsertDocumentModelInterface = Insertable<DocumentModelInterfaceTable>;
|
|
52
|
-
export type
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
export type MutationLogTable = {
|
|
54
|
+
mutation_hash: string;
|
|
55
|
+
model_id: string;
|
|
56
|
+
document_id: string;
|
|
57
|
+
author_did: string;
|
|
58
|
+
hlc: string;
|
|
59
|
+
mutation_jwt: string;
|
|
60
|
+
status: string;
|
|
55
61
|
};
|
|
56
|
-
export type
|
|
57
|
-
export type
|
|
62
|
+
export type MutationLogEntry = Selectable<MutationLogTable>;
|
|
63
|
+
export type InsertMutationLogEntry = Insertable<MutationLogTable>;
|
|
58
64
|
export type SearchConfig = Record<string, {
|
|
59
65
|
fields?: Array<string>;
|
|
60
66
|
}>;
|
|
@@ -98,16 +104,6 @@ export type UserModelAccessDefaultTable = {
|
|
|
98
104
|
export type UserModelAccessDefault = Selectable<UserModelAccessDefaultTable>;
|
|
99
105
|
export type InsertUserModelAccessDefault = Insertable<UserModelAccessDefaultTable>;
|
|
100
106
|
export type UpdateUserModelAccessDefault = Updateable<UserModelAccessDefaultTable>;
|
|
101
|
-
export type DocumentMutationLogTable = {
|
|
102
|
-
id: string;
|
|
103
|
-
document_id: string;
|
|
104
|
-
sequence_number: string;
|
|
105
|
-
mutation_jwt: string;
|
|
106
|
-
timestamp: CreatedAtColumn;
|
|
107
|
-
author_did: string;
|
|
108
|
-
};
|
|
109
|
-
export type DocumentMutationLog = Selectable<DocumentMutationLogTable>;
|
|
110
|
-
export type InsertDocumentMutationLog = Insertable<DocumentMutationLogTable>;
|
|
111
107
|
export type SyncCheckpointTable = {
|
|
112
108
|
id: string;
|
|
113
109
|
peer_server_id: string;
|
|
@@ -154,11 +150,10 @@ export type Database = {
|
|
|
154
150
|
kubun_document_attachments: DocumentAttachmentTable;
|
|
155
151
|
kubun_document_models: DocumentModelTable;
|
|
156
152
|
kubun_document_model_interfaces: DocumentModelInterfaceTable;
|
|
157
|
-
|
|
153
|
+
kubun_mutation_log: MutationLogTable;
|
|
158
154
|
kubun_graph_document_models: GraphDocumentModelTable;
|
|
159
155
|
kubun_graph_models: GraphModelTable;
|
|
160
156
|
kubun_user_model_access_defaults: UserModelAccessDefaultTable;
|
|
161
|
-
kubun_document_mutation_log: DocumentMutationLogTable;
|
|
162
157
|
kubun_sync_checkpoints: SyncCheckpointTable;
|
|
163
158
|
kubun_sync_peers: SyncPeerTable;
|
|
164
159
|
[key: `k_${string}`]: DocumentTable;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubun/db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"license": "see LICENSE.md",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"type": "module",
|
|
@@ -18,15 +18,15 @@
|
|
|
18
18
|
"@enkaku/codec": "^0.13.0",
|
|
19
19
|
"@enkaku/event": "^0.13.0",
|
|
20
20
|
"kysely": "^0.28.11",
|
|
21
|
-
"@kubun/logger": "^0.
|
|
21
|
+
"@kubun/logger": "^0.6.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@databases/pg-test": "^3.1.2",
|
|
25
|
-
"@kubun/
|
|
26
|
-
"@kubun/
|
|
27
|
-
"@kubun/
|
|
28
|
-
"@kubun/db-sqlite": "^0.
|
|
29
|
-
"@kubun/
|
|
25
|
+
"@kubun/db-adapter": "^0.6.0",
|
|
26
|
+
"@kubun/db-postgres": "^0.6.0",
|
|
27
|
+
"@kubun/id": "^0.6.0",
|
|
28
|
+
"@kubun/db-better-sqlite": "^0.6.0",
|
|
29
|
+
"@kubun/protocol": "^0.6.0"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build:clean": "del lib",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export function getMigration(e){let d=e.types;return{up:async function(t){await t.schema.createTable("kubun_user_model_access_defaults").ifNotExists().addColumn("owner_did",d.text,e=>e.notNull()).addColumn("model_id",d.text,e=>e.notNull()).addColumn("permission_type",d.text,e=>e.notNull()).addColumn("access_level",d.text,e=>e.notNull()).addColumn("allowed_dids",d.json).addColumn("created_at",d.timestamp,d=>d.defaultTo(e.functions.now).notNull()).addColumn("updated_at",d.timestamp).addPrimaryKeyConstraint("kubun_user_model_access_defaults_pkey",["owner_did","model_id","permission_type"]).execute(),await t.schema.createIndex("idx_user_model_access_owner_model").on("kubun_user_model_access_defaults").columns(["owner_did","model_id"]).execute()},down:async function(e){await e.schema.dropIndex("idx_user_model_access_owner_model").execute(),await e.schema.dropTable("kubun_user_model_access_defaults").execute()}}}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export function getMigration(t){let e=t.types;return{up:async function(n){await n.schema.createTable("kubun_document_mutation_log").ifNotExists().addColumn("id",e.text,t=>t.notNull().primaryKey()).addColumn("document_id",e.text,t=>t.notNull()).addColumn("sequence_number",e.text,t=>t.notNull()).addColumn("mutation_jwt",e.text,t=>t.notNull()).addColumn("timestamp",e.timestamp,e=>e.defaultTo(t.functions.now).notNull()).addColumn("author_did",e.text,t=>t.notNull()).execute(),await n.schema.createIndex("idx_mutation_log_doc_seq").on("kubun_document_mutation_log").columns(["document_id","sequence_number"]).execute(),await n.schema.createTable("kubun_sync_checkpoints").ifNotExists().addColumn("id",e.text,t=>t.notNull().primaryKey()).addColumn("peer_server_id",e.text,t=>t.notNull()).addColumn("user_id",e.text,t=>t.notNull()).addColumn("last_sync_timestamp",e.timestamp,t=>t.notNull()).addColumn("document_checkpoints",e.json,t=>t.notNull()).addColumn("vector_clock",e.json,t=>t.notNull()).execute(),await n.schema.createIndex("idx_sync_checkpoints_peer_user").on("kubun_sync_checkpoints").columns(["peer_server_id","user_id"]).execute(),await n.schema.createTable("kubun_sync_peers").ifNotExists().addColumn("id",e.text,t=>t.notNull().primaryKey()).addColumn("peer_did",e.text,t=>t.notNull().unique()).addColumn("endpoint",e.text,t=>t.notNull()).addColumn("mode",e.text,t=>t.notNull()).addColumn("allowed_users",e.json,t=>t.notNull()).addColumn("priority",e.text,t=>t.notNull().defaultTo("0")).addColumn("trust_level",e.text,t=>t.notNull()).addColumn("config",e.json,t=>t.notNull()).addColumn("created_at",e.timestamp,e=>e.defaultTo(t.functions.now).notNull()).addColumn("updated_at",e.timestamp,e=>e.defaultTo(t.functions.now).notNull()).execute()},down:async function(t){await t.schema.dropTable("kubun_sync_peers").ifExists().execute(),await t.schema.dropTable("kubun_sync_checkpoints").ifExists().execute(),await t.schema.dropIndex("idx_sync_checkpoints_peer_user").ifExists().execute(),await t.schema.dropIndex("idx_mutation_log_doc_seq").ifExists().execute(),await t.schema.dropTable("kubun_document_mutation_log").ifExists().execute()}}}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export function getMigration(e){return{up:async function(a){await a.schema.alterTable("kubun_graph_models").addColumn("search",e.types.json).execute()},down:async function(e){await e.schema.alterTable("kubun_graph_models").dropColumn("search").execute()}}}
|