@kubun/db 0.6.0 → 0.6.1
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 +16 -3
- package/lib/db.js +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/types.d.ts +5 -0
- package/package.json +6 -6
package/lib/db.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ import type { Adapter, AdapterTypes } from '@kubun/db-adapter';
|
|
|
3
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
|
-
import { Kysely, type Migration
|
|
7
|
-
import type { Database, DocumentData, GraphModel, InsertDocumentAttachment, InsertMutationLogEntry, ListDocumentsParams, MutationLogEntry, QueryDocumentsParams, QueryDocumentsResult, SearchConfig, SearchDocumentResult, SearchDocumentsParams } from './types.js';
|
|
6
|
+
import { Kysely, type Migration } from 'kysely';
|
|
7
|
+
import type { CountDocumentsParams, 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';
|
|
@@ -47,11 +47,22 @@ export type CreateGraphParams = {
|
|
|
47
47
|
search?: SearchConfig;
|
|
48
48
|
};
|
|
49
49
|
export type AddDocumentModelParams = {
|
|
50
|
-
transaction: Transaction<Database>;
|
|
51
50
|
id: string;
|
|
52
51
|
graphModelID: string;
|
|
53
52
|
model: DocumentModel;
|
|
54
53
|
};
|
|
54
|
+
export type WritableDB = {
|
|
55
|
+
readonly events: EventEmitter<DBEvents>;
|
|
56
|
+
getDocument(id: DocumentID): Promise<DocumentNode | null>;
|
|
57
|
+
getDocumentModel(id: DocumentModelID | string): Promise<DocumentModel>;
|
|
58
|
+
createDocument(params: CreateDocumentParams): Promise<DocumentNode>;
|
|
59
|
+
saveDocument(params: SaveDocumentParams): Promise<DocumentNode>;
|
|
60
|
+
insertMutationLogEntry(entry: InsertMutationLogEntry): Promise<void>;
|
|
61
|
+
hasMutationHash(hash: string): Promise<boolean>;
|
|
62
|
+
updateMutationStatus(mutationHash: string, status: string): Promise<void>;
|
|
63
|
+
getFieldHLCs(documentID: string): Promise<Record<string, string> | null>;
|
|
64
|
+
updateFieldHLCs(docID: DocumentID, fieldHLCs: Record<string, string>): Promise<void>;
|
|
65
|
+
};
|
|
55
66
|
export declare class KubunDB<T extends AdapterTypes = AdapterTypes> {
|
|
56
67
|
#private;
|
|
57
68
|
constructor(params: DBParams<T>);
|
|
@@ -59,6 +70,7 @@ export declare class KubunDB<T extends AdapterTypes = AdapterTypes> {
|
|
|
59
70
|
get adapter(): Adapter;
|
|
60
71
|
get events(): EventEmitter<DBEvents>;
|
|
61
72
|
getDB<T extends Database>(): Promise<Kysely<T>>;
|
|
73
|
+
withTransaction<R>(fn: (db: WritableDB) => Promise<R>): Promise<R>;
|
|
62
74
|
getMigrations(): Migrations;
|
|
63
75
|
close(): Promise<void>;
|
|
64
76
|
listGraphs(): Promise<Array<GraphModel>>;
|
|
@@ -67,6 +79,7 @@ export declare class KubunDB<T extends AdapterTypes = AdapterTypes> {
|
|
|
67
79
|
getDocumentModel(id: DocumentModelID | string): Promise<DocumentModel>;
|
|
68
80
|
getDocument(id: DocumentID): Promise<DocumentNode | null>;
|
|
69
81
|
listDocuments(params: ListDocumentsParams): Promise<Array<DocumentNode>>;
|
|
82
|
+
countDocuments(params: CountDocumentsParams): Promise<number>;
|
|
70
83
|
queryDocuments(params: QueryDocumentsParams): Promise<QueryDocumentsResult>;
|
|
71
84
|
createDocument(params: CreateDocumentParams): Promise<DocumentNode>;
|
|
72
85
|
saveDocument(params: SaveDocumentParams): Promise<DocumentNode>;
|
package/lib/db.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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}}
|
|
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;#i;#r;constructor(t){this.#e=t.adapter,this.#t=new r({dialect:this.#e.dialect,plugins:[new n]}),this.#a=new e,this.#i=t.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 withTransaction(e){await this.#r;let a=this.#e,i=this.#a,r=[],s=await this.#t.transaction().setIsolationLevel("read committed").execute(async s=>await e({events:i,getDocumentModel:async e=>h(await s.selectFrom("kubun_document_models").selectAll().where("id","=",e.toString()).executeTakeFirstOrThrow()),async getDocument(e){let t=await s.selectFrom(`k_${e.model.toString()}`).selectAll().where("id","=",e.toString()).executeTakeFirst();return t?m(a,t):null},async createDocument(e){let t=e.id.toString(),i=e.id.model.toString(),n=await s.insertInto(`k_${i}`).values({id:t,owner:e.owner,model:i,data:a.encodeJSON(e.data),unique:a.encodeBinary(e.unique)}).returningAll().executeTakeFirst(),o=m(a,n);return r.push({type:"create",document:o,getCursor:()=>d({id:n.id,ts:+n.created_at})}),o},async saveDocument(e){let t=e.id.toString(),i=await s.updateTable(`k_${e.id.model.toString()}`).set({data:e.data?a.encodeJSON(e.data):null,updated_at:a.encodeTimestamp(new Date)}).where("id","=",t).returningAll().executeTakeFirst(),n=m(a,i);return r.push({type:"update",document:n,previous:e.existing,getCursor:()=>d({id:i.id,ts:+i.created_at})}),n},async insertMutationLogEntry(e){await s.insertInto("kubun_mutation_log").values(e).onConflict(e=>e.doNothing()).execute()},hasMutationHash:async e=>null!=await s.selectFrom("kubun_mutation_log").select("mutation_hash").where("mutation_hash","=",e).executeTakeFirst(),async updateMutationStatus(e,t){await s.updateTable("kubun_mutation_log").set({status:t}).where("mutation_hash","=",e).execute()},async getFieldHLCs(e){let a=t.fromString(e).model.toString(),i=await s.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){let i=e.model.toString();await s.updateTable(`k_${i}`).set({field_hlcs:a.encodeJSON(t)}).where("id","=",e.toString()).execute()}}));for(let e of r)i.emit("document:saved",e);return s}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(i,{id:e,graphModelID:t,model:a}),s.add(e)}))}}),this.#i.debug("created graph {id}",{id:t}),t}async #n(e,t){let{id:a,graphModelID:i,model:r}=t,s=this.#e.types;for(let t of(await this.#r,await e.schema.createTable(`k_${a}`).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 e.insertInto("kubun_document_models").values({id:a,version:r.version,name:r.name,behavior:r.behavior,unique_fields:"unique"===r.behavior?JSON.stringify(r.uniqueFields):null,interfaces:JSON.stringify(r.interfaces),schema:JSON.stringify(r.schema),fields_meta:JSON.stringify(r.fieldsMeta)}).onConflict(e=>e.doNothing()).execute(),r.interfaces))await e.insertInto("kubun_document_model_interfaces").values({interface_id:w(a,t),implementation_id:a}).onConflict(e=>e.doNothing()).execute();await e.insertInto("kubun_graph_document_models").values({graph_model_id:i,document_model_id:a}).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 countDocuments(e){let{filter:t,modelIDs:a,owner:i}=e;await this.#r;let r=this.#e.coerceFilterValue.bind(this.#e);return(await Promise.all(a.map(async e=>{let a=this.#t.selectFrom(`k_${e}`).select(e=>e.fn.countAll().as("count"));return null!=i&&(a=a.where("owner","=",i)),null!=t&&(a=a.where(e=>l(e,t,[],r))),Number((await a.executeTakeFirstOrThrow()).count)}))).reduce((e,t)=>e+t,0)}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={id:t,owner:e.owner,model:a,data:this.#e.encodeJSON(e.data),unique:this.#e.encodeBinary(e.unique)},r=await this.#t.transaction().setIsolationLevel("read committed").execute(async e=>await e.insertInto(`k_${a}`).values(i).returningAll().executeTakeFirst()),s=m(this.#e,r);return this.#a.emit("document:saved",{type:"create",document:s,getCursor:()=>d({id:r.id,ts:+r.created_at})}),this.#i.debug("created document {id} with model {model}",{id:t,model:a}),s}async saveDocument(e){await this.#r;let t=e.id.toString(),a={data:e.data?this.#e.encodeJSON(e.data):null,updated_at:this.#e.encodeTimestamp(new Date)},i=await this.#t.transaction().setIsolationLevel("read committed").execute(async i=>await i.updateTable(`k_${e.id.model.toString()}`).set(a).where("id","=",t).returningAll().executeTakeFirst()),r=m(this.#e,i);return this.#a.emit("document:saved",{type:"update",document:r,previous:e.existing,getCursor:()=>d({id:i.id,ts:+i.created_at})}),this.#i.debug("updated document {id}",{id:t}),r}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/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export type { CreateGraphParams, DBEvents, DBParams, DocumentCreatedEvent, DocumentSavedEvent, GraphModelWithRecord, Migrations, } from './db.js';
|
|
1
|
+
export type { CreateGraphParams, DBEvents, DBParams, DocumentCreatedEvent, DocumentSavedEvent, GraphModelWithRecord, Migrations, WritableDB, } from './db.js';
|
|
2
2
|
export { KubunDB } from './db.js';
|
|
3
3
|
export type * from './types.js';
|
package/lib/types.d.ts
CHANGED
|
@@ -172,6 +172,11 @@ export type QueryDocumentsParams = ConnectionArguments & {
|
|
|
172
172
|
orderBy?: DocumentOrderBy;
|
|
173
173
|
owner?: string;
|
|
174
174
|
};
|
|
175
|
+
export type CountDocumentsParams = {
|
|
176
|
+
modelIDs: Array<string>;
|
|
177
|
+
filter?: DocumentFilter;
|
|
178
|
+
owner?: string;
|
|
179
|
+
};
|
|
175
180
|
export type QueryDocumentsResult = {
|
|
176
181
|
entries: Array<CursorDocument>;
|
|
177
182
|
hasMore: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubun/db",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.1",
|
|
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.6.
|
|
21
|
+
"@kubun/logger": "^0.6.1"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@
|
|
25
|
-
"@kubun/db-adapter": "^0.6.0",
|
|
26
|
-
"@kubun/db-postgres": "^0.6.0",
|
|
24
|
+
"@testcontainers/postgresql": "^11.12.0",
|
|
27
25
|
"@kubun/id": "^0.6.0",
|
|
26
|
+
"@kubun/db-adapter": "^0.6.0",
|
|
28
27
|
"@kubun/db-better-sqlite": "^0.6.0",
|
|
29
|
-
"@kubun/protocol": "^0.6.0"
|
|
28
|
+
"@kubun/protocol": "^0.6.0",
|
|
29
|
+
"@kubun/db-postgres": "^0.6.0"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build:clean": "del lib",
|