@noy-db/yjs 0.9.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/LICENSE +21 -0
- package/dist/index.cjs +157 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +116 -0
- package/dist/index.d.ts +116 -0
- package/dist/index.js +116 -0
- package/dist/index.js.map +1 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 vLannaAi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
YjsCollection: () => YjsCollection,
|
|
34
|
+
yArray: () => yArray,
|
|
35
|
+
yMap: () => yMap,
|
|
36
|
+
yText: () => yText,
|
|
37
|
+
yjsCollection: () => yjsCollection
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(index_exports);
|
|
40
|
+
|
|
41
|
+
// src/yjs-collection.ts
|
|
42
|
+
var Y = __toESM(require("yjs"), 1);
|
|
43
|
+
var YjsCollection = class {
|
|
44
|
+
/** @internal — use `yjsCollection()` factory instead. */
|
|
45
|
+
constructor(compartment, name, opts) {
|
|
46
|
+
this.compartment = compartment;
|
|
47
|
+
this.name = name;
|
|
48
|
+
this.yFields = opts.yFields;
|
|
49
|
+
this.coll = compartment.collection(name, { crdt: "yjs" });
|
|
50
|
+
}
|
|
51
|
+
compartment;
|
|
52
|
+
name;
|
|
53
|
+
coll;
|
|
54
|
+
yFields;
|
|
55
|
+
/**
|
|
56
|
+
* Get the Y.Doc for a record.
|
|
57
|
+
*
|
|
58
|
+
* If the record does not exist, returns a fresh empty Y.Doc with the declared
|
|
59
|
+
* `yFields` initialised (Y.Text / Y.Map / Y.Array as declared).
|
|
60
|
+
*
|
|
61
|
+
* If the record exists, the stored Yjs update is applied to the returned doc.
|
|
62
|
+
*/
|
|
63
|
+
async getYDoc(id) {
|
|
64
|
+
const doc = new Y.Doc();
|
|
65
|
+
this.initFields(doc);
|
|
66
|
+
const base64Update = await this.coll.get(id);
|
|
67
|
+
if (base64Update) {
|
|
68
|
+
const bytes = base64ToUint8Array(base64Update);
|
|
69
|
+
Y.applyUpdate(doc, bytes);
|
|
70
|
+
}
|
|
71
|
+
return doc;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Persist a Y.Doc by encoding its state as a Yjs update and storing it
|
|
75
|
+
* as the encrypted envelope payload.
|
|
76
|
+
*/
|
|
77
|
+
async putYDoc(id, doc) {
|
|
78
|
+
const update = Y.encodeStateAsUpdate(doc);
|
|
79
|
+
const base64 = uint8ArrayToBase64(update);
|
|
80
|
+
await this.coll.put(id, base64);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Merge a Yjs update into an existing record.
|
|
84
|
+
* Reads the current doc, applies the update, then persists the merged state.
|
|
85
|
+
*/
|
|
86
|
+
async applyUpdate(id, update) {
|
|
87
|
+
const doc = await this.getYDoc(id);
|
|
88
|
+
Y.applyUpdate(doc, update);
|
|
89
|
+
await this.putYDoc(id, doc);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Delete the record.
|
|
93
|
+
*/
|
|
94
|
+
async delete(id) {
|
|
95
|
+
await this.coll.delete(id);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Check whether a record exists.
|
|
99
|
+
*/
|
|
100
|
+
async has(id) {
|
|
101
|
+
return await this.coll.get(id) !== null;
|
|
102
|
+
}
|
|
103
|
+
// ─── Private ────────────────────────────────────────────────────────
|
|
104
|
+
initFields(doc) {
|
|
105
|
+
for (const [fieldName, descriptor] of Object.entries(this.yFields)) {
|
|
106
|
+
switch (descriptor._yjsType) {
|
|
107
|
+
case "Y.Text":
|
|
108
|
+
doc.getText(fieldName);
|
|
109
|
+
break;
|
|
110
|
+
case "Y.Map":
|
|
111
|
+
doc.getMap(fieldName);
|
|
112
|
+
break;
|
|
113
|
+
case "Y.Array":
|
|
114
|
+
doc.getArray(fieldName);
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
function uint8ArrayToBase64(bytes) {
|
|
121
|
+
let binary = "";
|
|
122
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
123
|
+
binary += String.fromCharCode(bytes[i]);
|
|
124
|
+
}
|
|
125
|
+
return btoa(binary);
|
|
126
|
+
}
|
|
127
|
+
function base64ToUint8Array(base64) {
|
|
128
|
+
const binary = atob(base64);
|
|
129
|
+
const bytes = new Uint8Array(binary.length);
|
|
130
|
+
for (let i = 0; i < binary.length; i++) {
|
|
131
|
+
bytes[i] = binary.charCodeAt(i);
|
|
132
|
+
}
|
|
133
|
+
return bytes;
|
|
134
|
+
}
|
|
135
|
+
function yjsCollection(compartment, name, opts) {
|
|
136
|
+
return new YjsCollection(compartment, name, opts);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// src/descriptors.ts
|
|
140
|
+
function yText() {
|
|
141
|
+
return { _yjsType: "Y.Text" };
|
|
142
|
+
}
|
|
143
|
+
function yMap() {
|
|
144
|
+
return { _yjsType: "Y.Map" };
|
|
145
|
+
}
|
|
146
|
+
function yArray() {
|
|
147
|
+
return { _yjsType: "Y.Array" };
|
|
148
|
+
}
|
|
149
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
150
|
+
0 && (module.exports = {
|
|
151
|
+
YjsCollection,
|
|
152
|
+
yArray,
|
|
153
|
+
yMap,
|
|
154
|
+
yText,
|
|
155
|
+
yjsCollection
|
|
156
|
+
});
|
|
157
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/yjs-collection.ts","../src/descriptors.ts"],"sourcesContent":["/**\n * @noy-db/yjs — Yjs Y.Doc interop for noy-db.\n *\n * Enables collaborative rich-text editing with encrypted-at-rest Yjs state.\n * Requires peer dependencies: `@noy-db/core` and `yjs >= 13`.\n *\n * @example\n * ```ts\n * import { yjsCollection, yText } from '@noy-db/yjs'\n *\n * const notes = yjsCollection(comp, 'notes', {\n * yFields: { body: yText() },\n * })\n *\n * const doc = await notes.getYDoc('note-1')\n * doc.getText('body').insert(0, 'Hello world')\n * await notes.putYDoc('note-1', doc)\n * ```\n */\n\nexport { yjsCollection, YjsCollection } from './yjs-collection.js'\nexport type {\n YjsCollectionOptions,\n YjsSnapshot,\n} from './yjs-collection.js'\n\nexport { yText, yMap, yArray } from './descriptors.js'\nexport type {\n YFieldDescriptor,\n YTextDescriptor,\n YMapDescriptor,\n YArrayDescriptor,\n} from './descriptors.js'\n","/**\n * YjsCollection — wraps a noy-db Collection<string> (crdt: 'yjs') with\n * Y.Doc-aware `getYDoc` and `putYDoc` methods.\n *\n * The encrypted envelope stores base64(Y.encodeStateAsUpdate(ydoc)) in `_data`.\n * `collection.get(id)` returns this raw base64 string. `getYDoc(id)` decodes\n * it into a Y.Doc and applies any `yFields` initialisation.\n *\n * v0.9 #136\n */\n\nimport * as Y from 'yjs'\nimport type { Compartment } from '@noy-db/core'\nimport type { YFieldDescriptor } from './descriptors.js'\n\n/** A resolved snapshot of a Yjs-backed record. Field values depend on yFields descriptors. */\nexport type YjsSnapshot<YF extends Record<string, YFieldDescriptor>> = {\n [K in keyof YF]: YF[K] extends { _yjsType: 'Y.Text' }\n ? Y.Text\n : YF[K] extends { _yjsType: 'Y.Map' }\n ? Y.Map<unknown>\n : Y.Array<unknown>\n}\n\n/** Options for creating a YjsCollection. */\nexport interface YjsCollectionOptions<YF extends Record<string, YFieldDescriptor>> {\n /** The field descriptors — tells YjsCollection which Y.* types to initialise. */\n yFields: YF\n}\n\n/**\n * A YjsCollection wraps a noy-db `crdt: 'yjs'` collection and exposes\n * `getYDoc(id)` and `putYDoc(id, ydoc)` instead of the raw `get`/`put` API.\n *\n * Construct via `yjsCollection(compartment, name, opts)`.\n */\nexport class YjsCollection<YF extends Record<string, YFieldDescriptor>> {\n private readonly coll: ReturnType<Compartment['collection']>\n private readonly yFields: YF\n\n /** @internal — use `yjsCollection()` factory instead. */\n constructor(\n private readonly compartment: Compartment,\n private readonly name: string,\n opts: YjsCollectionOptions<YF>,\n ) {\n this.yFields = opts.yFields\n // Collection<string> with crdt: 'yjs' — T = string (the base64 update blob)\n this.coll = compartment.collection<string>(name, { crdt: 'yjs' })\n }\n\n /**\n * Get the Y.Doc for a record.\n *\n * If the record does not exist, returns a fresh empty Y.Doc with the declared\n * `yFields` initialised (Y.Text / Y.Map / Y.Array as declared).\n *\n * If the record exists, the stored Yjs update is applied to the returned doc.\n */\n async getYDoc(id: string): Promise<Y.Doc> {\n const doc = new Y.Doc()\n this.initFields(doc)\n\n const base64Update = await this.coll.get(id) as string | null\n if (base64Update) {\n const bytes = base64ToUint8Array(base64Update)\n Y.applyUpdate(doc, bytes)\n }\n\n return doc\n }\n\n /**\n * Persist a Y.Doc by encoding its state as a Yjs update and storing it\n * as the encrypted envelope payload.\n */\n async putYDoc(id: string, doc: Y.Doc): Promise<void> {\n const update = Y.encodeStateAsUpdate(doc)\n const base64 = uint8ArrayToBase64(update)\n await this.coll.put(id, base64)\n }\n\n /**\n * Merge a Yjs update into an existing record.\n * Reads the current doc, applies the update, then persists the merged state.\n */\n async applyUpdate(id: string, update: Uint8Array): Promise<void> {\n const doc = await this.getYDoc(id)\n Y.applyUpdate(doc, update)\n await this.putYDoc(id, doc)\n }\n\n /**\n * Delete the record.\n */\n async delete(id: string): Promise<void> {\n await this.coll.delete(id)\n }\n\n /**\n * Check whether a record exists.\n */\n async has(id: string): Promise<boolean> {\n return (await this.coll.get(id)) !== null\n }\n\n // ─── Private ────────────────────────────────────────────────────────\n\n private initFields(doc: Y.Doc): void {\n for (const [fieldName, descriptor] of Object.entries(this.yFields)) {\n switch (descriptor._yjsType) {\n case 'Y.Text':\n doc.getText(fieldName)\n break\n case 'Y.Map':\n doc.getMap(fieldName)\n break\n case 'Y.Array':\n doc.getArray(fieldName)\n break\n }\n }\n }\n}\n\n// ─── Base64 helpers ───────────────────────────────────────────────────────────\n\nfunction uint8ArrayToBase64(bytes: Uint8Array): string {\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]!)\n }\n return btoa(binary)\n}\n\nfunction base64ToUint8Array(base64: string): Uint8Array {\n const binary = atob(base64)\n const bytes = new Uint8Array(binary.length)\n for (let i = 0; i < binary.length; i++) {\n bytes[i] = binary.charCodeAt(i)\n }\n return bytes\n}\n\n// ─── Factory function ─────────────────────────────────────────────────────────\n\n/**\n * Create a `YjsCollection` for a compartment.\n *\n * @param compartment An opened NOYDB compartment.\n * @param name Collection name (must not start with `_`).\n * @param opts Field descriptors (`yFields`) and optional collection settings.\n *\n * @example\n * ```ts\n * import { yjsCollection, yText } from '@noy-db/yjs'\n *\n * const notes = yjsCollection(comp, 'notes', {\n * yFields: { body: yText() },\n * })\n *\n * const doc = await notes.getYDoc('note-1')\n * doc.getText('body').insert(0, 'Hello world')\n * await notes.putYDoc('note-1', doc)\n * ```\n */\nexport function yjsCollection<YF extends Record<string, YFieldDescriptor>>(\n compartment: Compartment,\n name: string,\n opts: YjsCollectionOptions<YF>,\n): YjsCollection<YF> {\n return new YjsCollection(compartment, name, opts)\n}\n","/**\n * Field descriptors for Yjs-backed fields in @noy-db/yjs.\n * Mirrors the dictKey / i18nText descriptor pattern from @noy-db/core.\n */\n\n/** Descriptor for a Y.Text field (rich text, TipTap/ProseMirror compatible). */\nexport interface YTextDescriptor {\n readonly _yjsType: 'Y.Text'\n}\n\n/** Descriptor for a Y.Map<string> field. */\nexport interface YMapDescriptor {\n readonly _yjsType: 'Y.Map'\n}\n\n/** Descriptor for a Y.Array field. */\nexport interface YArrayDescriptor {\n readonly _yjsType: 'Y.Array'\n}\n\nexport type YFieldDescriptor = YTextDescriptor | YMapDescriptor | YArrayDescriptor\n\n/** Declare a Y.Text field (rich text). */\nexport function yText(): YTextDescriptor {\n return { _yjsType: 'Y.Text' }\n}\n\n/** Declare a Y.Map field. */\nexport function yMap(): YMapDescriptor {\n return { _yjsType: 'Y.Map' }\n}\n\n/** Declare a Y.Array field. */\nexport function yArray(): YArrayDescriptor {\n return { _yjsType: 'Y.Array' }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACWA,QAAmB;AAyBZ,IAAM,gBAAN,MAAiE;AAAA;AAAA,EAKtE,YACmB,aACA,MACjB,MACA;AAHiB;AACA;AAGjB,SAAK,UAAU,KAAK;AAEpB,SAAK,OAAO,YAAY,WAAmB,MAAM,EAAE,MAAM,MAAM,CAAC;AAAA,EAClE;AAAA,EAPmB;AAAA,EACA;AAAA,EANF;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBjB,MAAM,QAAQ,IAA4B;AACxC,UAAM,MAAM,IAAM,MAAI;AACtB,SAAK,WAAW,GAAG;AAEnB,UAAM,eAAe,MAAM,KAAK,KAAK,IAAI,EAAE;AAC3C,QAAI,cAAc;AAChB,YAAM,QAAQ,mBAAmB,YAAY;AAC7C,MAAE,cAAY,KAAK,KAAK;AAAA,IAC1B;AAEA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,QAAQ,IAAY,KAA2B;AACnD,UAAM,SAAW,sBAAoB,GAAG;AACxC,UAAM,SAAS,mBAAmB,MAAM;AACxC,UAAM,KAAK,KAAK,IAAI,IAAI,MAAM;AAAA,EAChC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,YAAY,IAAY,QAAmC;AAC/D,UAAM,MAAM,MAAM,KAAK,QAAQ,EAAE;AACjC,IAAE,cAAY,KAAK,MAAM;AACzB,UAAM,KAAK,QAAQ,IAAI,GAAG;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,OAAO,IAA2B;AACtC,UAAM,KAAK,KAAK,OAAO,EAAE;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,IAAI,IAA8B;AACtC,WAAQ,MAAM,KAAK,KAAK,IAAI,EAAE,MAAO;AAAA,EACvC;AAAA;AAAA,EAIQ,WAAW,KAAkB;AACnC,eAAW,CAAC,WAAW,UAAU,KAAK,OAAO,QAAQ,KAAK,OAAO,GAAG;AAClE,cAAQ,WAAW,UAAU;AAAA,QAC3B,KAAK;AACH,cAAI,QAAQ,SAAS;AACrB;AAAA,QACF,KAAK;AACH,cAAI,OAAO,SAAS;AACpB;AAAA,QACF,KAAK;AACH,cAAI,SAAS,SAAS;AACtB;AAAA,MACJ;AAAA,IACF;AAAA,EACF;AACF;AAIA,SAAS,mBAAmB,OAA2B;AACrD,MAAI,SAAS;AACb,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,cAAU,OAAO,aAAa,MAAM,CAAC,CAAE;AAAA,EACzC;AACA,SAAO,KAAK,MAAM;AACpB;AAEA,SAAS,mBAAmB,QAA4B;AACtD,QAAM,SAAS,KAAK,MAAM;AAC1B,QAAM,QAAQ,IAAI,WAAW,OAAO,MAAM;AAC1C,WAAS,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK;AACtC,UAAM,CAAC,IAAI,OAAO,WAAW,CAAC;AAAA,EAChC;AACA,SAAO;AACT;AAwBO,SAAS,cACd,aACA,MACA,MACmB;AACnB,SAAO,IAAI,cAAc,aAAa,MAAM,IAAI;AAClD;;;ACrJO,SAAS,QAAyB;AACvC,SAAO,EAAE,UAAU,SAAS;AAC9B;AAGO,SAAS,OAAuB;AACrC,SAAO,EAAE,UAAU,QAAQ;AAC7B;AAGO,SAAS,SAA2B;AACzC,SAAO,EAAE,UAAU,UAAU;AAC/B;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import * as Y from 'yjs';
|
|
2
|
+
import { Compartment } from '@noy-db/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Field descriptors for Yjs-backed fields in @noy-db/yjs.
|
|
6
|
+
* Mirrors the dictKey / i18nText descriptor pattern from @noy-db/core.
|
|
7
|
+
*/
|
|
8
|
+
/** Descriptor for a Y.Text field (rich text, TipTap/ProseMirror compatible). */
|
|
9
|
+
interface YTextDescriptor {
|
|
10
|
+
readonly _yjsType: 'Y.Text';
|
|
11
|
+
}
|
|
12
|
+
/** Descriptor for a Y.Map<string> field. */
|
|
13
|
+
interface YMapDescriptor {
|
|
14
|
+
readonly _yjsType: 'Y.Map';
|
|
15
|
+
}
|
|
16
|
+
/** Descriptor for a Y.Array field. */
|
|
17
|
+
interface YArrayDescriptor {
|
|
18
|
+
readonly _yjsType: 'Y.Array';
|
|
19
|
+
}
|
|
20
|
+
type YFieldDescriptor = YTextDescriptor | YMapDescriptor | YArrayDescriptor;
|
|
21
|
+
/** Declare a Y.Text field (rich text). */
|
|
22
|
+
declare function yText(): YTextDescriptor;
|
|
23
|
+
/** Declare a Y.Map field. */
|
|
24
|
+
declare function yMap(): YMapDescriptor;
|
|
25
|
+
/** Declare a Y.Array field. */
|
|
26
|
+
declare function yArray(): YArrayDescriptor;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* YjsCollection — wraps a noy-db Collection<string> (crdt: 'yjs') with
|
|
30
|
+
* Y.Doc-aware `getYDoc` and `putYDoc` methods.
|
|
31
|
+
*
|
|
32
|
+
* The encrypted envelope stores base64(Y.encodeStateAsUpdate(ydoc)) in `_data`.
|
|
33
|
+
* `collection.get(id)` returns this raw base64 string. `getYDoc(id)` decodes
|
|
34
|
+
* it into a Y.Doc and applies any `yFields` initialisation.
|
|
35
|
+
*
|
|
36
|
+
* v0.9 #136
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/** A resolved snapshot of a Yjs-backed record. Field values depend on yFields descriptors. */
|
|
40
|
+
type YjsSnapshot<YF extends Record<string, YFieldDescriptor>> = {
|
|
41
|
+
[K in keyof YF]: YF[K] extends {
|
|
42
|
+
_yjsType: 'Y.Text';
|
|
43
|
+
} ? Y.Text : YF[K] extends {
|
|
44
|
+
_yjsType: 'Y.Map';
|
|
45
|
+
} ? Y.Map<unknown> : Y.Array<unknown>;
|
|
46
|
+
};
|
|
47
|
+
/** Options for creating a YjsCollection. */
|
|
48
|
+
interface YjsCollectionOptions<YF extends Record<string, YFieldDescriptor>> {
|
|
49
|
+
/** The field descriptors — tells YjsCollection which Y.* types to initialise. */
|
|
50
|
+
yFields: YF;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A YjsCollection wraps a noy-db `crdt: 'yjs'` collection and exposes
|
|
54
|
+
* `getYDoc(id)` and `putYDoc(id, ydoc)` instead of the raw `get`/`put` API.
|
|
55
|
+
*
|
|
56
|
+
* Construct via `yjsCollection(compartment, name, opts)`.
|
|
57
|
+
*/
|
|
58
|
+
declare class YjsCollection<YF extends Record<string, YFieldDescriptor>> {
|
|
59
|
+
private readonly compartment;
|
|
60
|
+
private readonly name;
|
|
61
|
+
private readonly coll;
|
|
62
|
+
private readonly yFields;
|
|
63
|
+
/** @internal — use `yjsCollection()` factory instead. */
|
|
64
|
+
constructor(compartment: Compartment, name: string, opts: YjsCollectionOptions<YF>);
|
|
65
|
+
/**
|
|
66
|
+
* Get the Y.Doc for a record.
|
|
67
|
+
*
|
|
68
|
+
* If the record does not exist, returns a fresh empty Y.Doc with the declared
|
|
69
|
+
* `yFields` initialised (Y.Text / Y.Map / Y.Array as declared).
|
|
70
|
+
*
|
|
71
|
+
* If the record exists, the stored Yjs update is applied to the returned doc.
|
|
72
|
+
*/
|
|
73
|
+
getYDoc(id: string): Promise<Y.Doc>;
|
|
74
|
+
/**
|
|
75
|
+
* Persist a Y.Doc by encoding its state as a Yjs update and storing it
|
|
76
|
+
* as the encrypted envelope payload.
|
|
77
|
+
*/
|
|
78
|
+
putYDoc(id: string, doc: Y.Doc): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Merge a Yjs update into an existing record.
|
|
81
|
+
* Reads the current doc, applies the update, then persists the merged state.
|
|
82
|
+
*/
|
|
83
|
+
applyUpdate(id: string, update: Uint8Array): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Delete the record.
|
|
86
|
+
*/
|
|
87
|
+
delete(id: string): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Check whether a record exists.
|
|
90
|
+
*/
|
|
91
|
+
has(id: string): Promise<boolean>;
|
|
92
|
+
private initFields;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Create a `YjsCollection` for a compartment.
|
|
96
|
+
*
|
|
97
|
+
* @param compartment An opened NOYDB compartment.
|
|
98
|
+
* @param name Collection name (must not start with `_`).
|
|
99
|
+
* @param opts Field descriptors (`yFields`) and optional collection settings.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* import { yjsCollection, yText } from '@noy-db/yjs'
|
|
104
|
+
*
|
|
105
|
+
* const notes = yjsCollection(comp, 'notes', {
|
|
106
|
+
* yFields: { body: yText() },
|
|
107
|
+
* })
|
|
108
|
+
*
|
|
109
|
+
* const doc = await notes.getYDoc('note-1')
|
|
110
|
+
* doc.getText('body').insert(0, 'Hello world')
|
|
111
|
+
* await notes.putYDoc('note-1', doc)
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
declare function yjsCollection<YF extends Record<string, YFieldDescriptor>>(compartment: Compartment, name: string, opts: YjsCollectionOptions<YF>): YjsCollection<YF>;
|
|
115
|
+
|
|
116
|
+
export { type YArrayDescriptor, type YFieldDescriptor, type YMapDescriptor, type YTextDescriptor, YjsCollection, type YjsCollectionOptions, type YjsSnapshot, yArray, yMap, yText, yjsCollection };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import * as Y from 'yjs';
|
|
2
|
+
import { Compartment } from '@noy-db/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Field descriptors for Yjs-backed fields in @noy-db/yjs.
|
|
6
|
+
* Mirrors the dictKey / i18nText descriptor pattern from @noy-db/core.
|
|
7
|
+
*/
|
|
8
|
+
/** Descriptor for a Y.Text field (rich text, TipTap/ProseMirror compatible). */
|
|
9
|
+
interface YTextDescriptor {
|
|
10
|
+
readonly _yjsType: 'Y.Text';
|
|
11
|
+
}
|
|
12
|
+
/** Descriptor for a Y.Map<string> field. */
|
|
13
|
+
interface YMapDescriptor {
|
|
14
|
+
readonly _yjsType: 'Y.Map';
|
|
15
|
+
}
|
|
16
|
+
/** Descriptor for a Y.Array field. */
|
|
17
|
+
interface YArrayDescriptor {
|
|
18
|
+
readonly _yjsType: 'Y.Array';
|
|
19
|
+
}
|
|
20
|
+
type YFieldDescriptor = YTextDescriptor | YMapDescriptor | YArrayDescriptor;
|
|
21
|
+
/** Declare a Y.Text field (rich text). */
|
|
22
|
+
declare function yText(): YTextDescriptor;
|
|
23
|
+
/** Declare a Y.Map field. */
|
|
24
|
+
declare function yMap(): YMapDescriptor;
|
|
25
|
+
/** Declare a Y.Array field. */
|
|
26
|
+
declare function yArray(): YArrayDescriptor;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* YjsCollection — wraps a noy-db Collection<string> (crdt: 'yjs') with
|
|
30
|
+
* Y.Doc-aware `getYDoc` and `putYDoc` methods.
|
|
31
|
+
*
|
|
32
|
+
* The encrypted envelope stores base64(Y.encodeStateAsUpdate(ydoc)) in `_data`.
|
|
33
|
+
* `collection.get(id)` returns this raw base64 string. `getYDoc(id)` decodes
|
|
34
|
+
* it into a Y.Doc and applies any `yFields` initialisation.
|
|
35
|
+
*
|
|
36
|
+
* v0.9 #136
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/** A resolved snapshot of a Yjs-backed record. Field values depend on yFields descriptors. */
|
|
40
|
+
type YjsSnapshot<YF extends Record<string, YFieldDescriptor>> = {
|
|
41
|
+
[K in keyof YF]: YF[K] extends {
|
|
42
|
+
_yjsType: 'Y.Text';
|
|
43
|
+
} ? Y.Text : YF[K] extends {
|
|
44
|
+
_yjsType: 'Y.Map';
|
|
45
|
+
} ? Y.Map<unknown> : Y.Array<unknown>;
|
|
46
|
+
};
|
|
47
|
+
/** Options for creating a YjsCollection. */
|
|
48
|
+
interface YjsCollectionOptions<YF extends Record<string, YFieldDescriptor>> {
|
|
49
|
+
/** The field descriptors — tells YjsCollection which Y.* types to initialise. */
|
|
50
|
+
yFields: YF;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A YjsCollection wraps a noy-db `crdt: 'yjs'` collection and exposes
|
|
54
|
+
* `getYDoc(id)` and `putYDoc(id, ydoc)` instead of the raw `get`/`put` API.
|
|
55
|
+
*
|
|
56
|
+
* Construct via `yjsCollection(compartment, name, opts)`.
|
|
57
|
+
*/
|
|
58
|
+
declare class YjsCollection<YF extends Record<string, YFieldDescriptor>> {
|
|
59
|
+
private readonly compartment;
|
|
60
|
+
private readonly name;
|
|
61
|
+
private readonly coll;
|
|
62
|
+
private readonly yFields;
|
|
63
|
+
/** @internal — use `yjsCollection()` factory instead. */
|
|
64
|
+
constructor(compartment: Compartment, name: string, opts: YjsCollectionOptions<YF>);
|
|
65
|
+
/**
|
|
66
|
+
* Get the Y.Doc for a record.
|
|
67
|
+
*
|
|
68
|
+
* If the record does not exist, returns a fresh empty Y.Doc with the declared
|
|
69
|
+
* `yFields` initialised (Y.Text / Y.Map / Y.Array as declared).
|
|
70
|
+
*
|
|
71
|
+
* If the record exists, the stored Yjs update is applied to the returned doc.
|
|
72
|
+
*/
|
|
73
|
+
getYDoc(id: string): Promise<Y.Doc>;
|
|
74
|
+
/**
|
|
75
|
+
* Persist a Y.Doc by encoding its state as a Yjs update and storing it
|
|
76
|
+
* as the encrypted envelope payload.
|
|
77
|
+
*/
|
|
78
|
+
putYDoc(id: string, doc: Y.Doc): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Merge a Yjs update into an existing record.
|
|
81
|
+
* Reads the current doc, applies the update, then persists the merged state.
|
|
82
|
+
*/
|
|
83
|
+
applyUpdate(id: string, update: Uint8Array): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Delete the record.
|
|
86
|
+
*/
|
|
87
|
+
delete(id: string): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Check whether a record exists.
|
|
90
|
+
*/
|
|
91
|
+
has(id: string): Promise<boolean>;
|
|
92
|
+
private initFields;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Create a `YjsCollection` for a compartment.
|
|
96
|
+
*
|
|
97
|
+
* @param compartment An opened NOYDB compartment.
|
|
98
|
+
* @param name Collection name (must not start with `_`).
|
|
99
|
+
* @param opts Field descriptors (`yFields`) and optional collection settings.
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* import { yjsCollection, yText } from '@noy-db/yjs'
|
|
104
|
+
*
|
|
105
|
+
* const notes = yjsCollection(comp, 'notes', {
|
|
106
|
+
* yFields: { body: yText() },
|
|
107
|
+
* })
|
|
108
|
+
*
|
|
109
|
+
* const doc = await notes.getYDoc('note-1')
|
|
110
|
+
* doc.getText('body').insert(0, 'Hello world')
|
|
111
|
+
* await notes.putYDoc('note-1', doc)
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
declare function yjsCollection<YF extends Record<string, YFieldDescriptor>>(compartment: Compartment, name: string, opts: YjsCollectionOptions<YF>): YjsCollection<YF>;
|
|
115
|
+
|
|
116
|
+
export { type YArrayDescriptor, type YFieldDescriptor, type YMapDescriptor, type YTextDescriptor, YjsCollection, type YjsCollectionOptions, type YjsSnapshot, yArray, yMap, yText, yjsCollection };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// src/yjs-collection.ts
|
|
2
|
+
import * as Y from "yjs";
|
|
3
|
+
var YjsCollection = class {
|
|
4
|
+
/** @internal — use `yjsCollection()` factory instead. */
|
|
5
|
+
constructor(compartment, name, opts) {
|
|
6
|
+
this.compartment = compartment;
|
|
7
|
+
this.name = name;
|
|
8
|
+
this.yFields = opts.yFields;
|
|
9
|
+
this.coll = compartment.collection(name, { crdt: "yjs" });
|
|
10
|
+
}
|
|
11
|
+
compartment;
|
|
12
|
+
name;
|
|
13
|
+
coll;
|
|
14
|
+
yFields;
|
|
15
|
+
/**
|
|
16
|
+
* Get the Y.Doc for a record.
|
|
17
|
+
*
|
|
18
|
+
* If the record does not exist, returns a fresh empty Y.Doc with the declared
|
|
19
|
+
* `yFields` initialised (Y.Text / Y.Map / Y.Array as declared).
|
|
20
|
+
*
|
|
21
|
+
* If the record exists, the stored Yjs update is applied to the returned doc.
|
|
22
|
+
*/
|
|
23
|
+
async getYDoc(id) {
|
|
24
|
+
const doc = new Y.Doc();
|
|
25
|
+
this.initFields(doc);
|
|
26
|
+
const base64Update = await this.coll.get(id);
|
|
27
|
+
if (base64Update) {
|
|
28
|
+
const bytes = base64ToUint8Array(base64Update);
|
|
29
|
+
Y.applyUpdate(doc, bytes);
|
|
30
|
+
}
|
|
31
|
+
return doc;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Persist a Y.Doc by encoding its state as a Yjs update and storing it
|
|
35
|
+
* as the encrypted envelope payload.
|
|
36
|
+
*/
|
|
37
|
+
async putYDoc(id, doc) {
|
|
38
|
+
const update = Y.encodeStateAsUpdate(doc);
|
|
39
|
+
const base64 = uint8ArrayToBase64(update);
|
|
40
|
+
await this.coll.put(id, base64);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Merge a Yjs update into an existing record.
|
|
44
|
+
* Reads the current doc, applies the update, then persists the merged state.
|
|
45
|
+
*/
|
|
46
|
+
async applyUpdate(id, update) {
|
|
47
|
+
const doc = await this.getYDoc(id);
|
|
48
|
+
Y.applyUpdate(doc, update);
|
|
49
|
+
await this.putYDoc(id, doc);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Delete the record.
|
|
53
|
+
*/
|
|
54
|
+
async delete(id) {
|
|
55
|
+
await this.coll.delete(id);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Check whether a record exists.
|
|
59
|
+
*/
|
|
60
|
+
async has(id) {
|
|
61
|
+
return await this.coll.get(id) !== null;
|
|
62
|
+
}
|
|
63
|
+
// ─── Private ────────────────────────────────────────────────────────
|
|
64
|
+
initFields(doc) {
|
|
65
|
+
for (const [fieldName, descriptor] of Object.entries(this.yFields)) {
|
|
66
|
+
switch (descriptor._yjsType) {
|
|
67
|
+
case "Y.Text":
|
|
68
|
+
doc.getText(fieldName);
|
|
69
|
+
break;
|
|
70
|
+
case "Y.Map":
|
|
71
|
+
doc.getMap(fieldName);
|
|
72
|
+
break;
|
|
73
|
+
case "Y.Array":
|
|
74
|
+
doc.getArray(fieldName);
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
function uint8ArrayToBase64(bytes) {
|
|
81
|
+
let binary = "";
|
|
82
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
83
|
+
binary += String.fromCharCode(bytes[i]);
|
|
84
|
+
}
|
|
85
|
+
return btoa(binary);
|
|
86
|
+
}
|
|
87
|
+
function base64ToUint8Array(base64) {
|
|
88
|
+
const binary = atob(base64);
|
|
89
|
+
const bytes = new Uint8Array(binary.length);
|
|
90
|
+
for (let i = 0; i < binary.length; i++) {
|
|
91
|
+
bytes[i] = binary.charCodeAt(i);
|
|
92
|
+
}
|
|
93
|
+
return bytes;
|
|
94
|
+
}
|
|
95
|
+
function yjsCollection(compartment, name, opts) {
|
|
96
|
+
return new YjsCollection(compartment, name, opts);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// src/descriptors.ts
|
|
100
|
+
function yText() {
|
|
101
|
+
return { _yjsType: "Y.Text" };
|
|
102
|
+
}
|
|
103
|
+
function yMap() {
|
|
104
|
+
return { _yjsType: "Y.Map" };
|
|
105
|
+
}
|
|
106
|
+
function yArray() {
|
|
107
|
+
return { _yjsType: "Y.Array" };
|
|
108
|
+
}
|
|
109
|
+
export {
|
|
110
|
+
YjsCollection,
|
|
111
|
+
yArray,
|
|
112
|
+
yMap,
|
|
113
|
+
yText,
|
|
114
|
+
yjsCollection
|
|
115
|
+
};
|
|
116
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/yjs-collection.ts","../src/descriptors.ts"],"sourcesContent":["/**\n * YjsCollection — wraps a noy-db Collection<string> (crdt: 'yjs') with\n * Y.Doc-aware `getYDoc` and `putYDoc` methods.\n *\n * The encrypted envelope stores base64(Y.encodeStateAsUpdate(ydoc)) in `_data`.\n * `collection.get(id)` returns this raw base64 string. `getYDoc(id)` decodes\n * it into a Y.Doc and applies any `yFields` initialisation.\n *\n * v0.9 #136\n */\n\nimport * as Y from 'yjs'\nimport type { Compartment } from '@noy-db/core'\nimport type { YFieldDescriptor } from './descriptors.js'\n\n/** A resolved snapshot of a Yjs-backed record. Field values depend on yFields descriptors. */\nexport type YjsSnapshot<YF extends Record<string, YFieldDescriptor>> = {\n [K in keyof YF]: YF[K] extends { _yjsType: 'Y.Text' }\n ? Y.Text\n : YF[K] extends { _yjsType: 'Y.Map' }\n ? Y.Map<unknown>\n : Y.Array<unknown>\n}\n\n/** Options for creating a YjsCollection. */\nexport interface YjsCollectionOptions<YF extends Record<string, YFieldDescriptor>> {\n /** The field descriptors — tells YjsCollection which Y.* types to initialise. */\n yFields: YF\n}\n\n/**\n * A YjsCollection wraps a noy-db `crdt: 'yjs'` collection and exposes\n * `getYDoc(id)` and `putYDoc(id, ydoc)` instead of the raw `get`/`put` API.\n *\n * Construct via `yjsCollection(compartment, name, opts)`.\n */\nexport class YjsCollection<YF extends Record<string, YFieldDescriptor>> {\n private readonly coll: ReturnType<Compartment['collection']>\n private readonly yFields: YF\n\n /** @internal — use `yjsCollection()` factory instead. */\n constructor(\n private readonly compartment: Compartment,\n private readonly name: string,\n opts: YjsCollectionOptions<YF>,\n ) {\n this.yFields = opts.yFields\n // Collection<string> with crdt: 'yjs' — T = string (the base64 update blob)\n this.coll = compartment.collection<string>(name, { crdt: 'yjs' })\n }\n\n /**\n * Get the Y.Doc for a record.\n *\n * If the record does not exist, returns a fresh empty Y.Doc with the declared\n * `yFields` initialised (Y.Text / Y.Map / Y.Array as declared).\n *\n * If the record exists, the stored Yjs update is applied to the returned doc.\n */\n async getYDoc(id: string): Promise<Y.Doc> {\n const doc = new Y.Doc()\n this.initFields(doc)\n\n const base64Update = await this.coll.get(id) as string | null\n if (base64Update) {\n const bytes = base64ToUint8Array(base64Update)\n Y.applyUpdate(doc, bytes)\n }\n\n return doc\n }\n\n /**\n * Persist a Y.Doc by encoding its state as a Yjs update and storing it\n * as the encrypted envelope payload.\n */\n async putYDoc(id: string, doc: Y.Doc): Promise<void> {\n const update = Y.encodeStateAsUpdate(doc)\n const base64 = uint8ArrayToBase64(update)\n await this.coll.put(id, base64)\n }\n\n /**\n * Merge a Yjs update into an existing record.\n * Reads the current doc, applies the update, then persists the merged state.\n */\n async applyUpdate(id: string, update: Uint8Array): Promise<void> {\n const doc = await this.getYDoc(id)\n Y.applyUpdate(doc, update)\n await this.putYDoc(id, doc)\n }\n\n /**\n * Delete the record.\n */\n async delete(id: string): Promise<void> {\n await this.coll.delete(id)\n }\n\n /**\n * Check whether a record exists.\n */\n async has(id: string): Promise<boolean> {\n return (await this.coll.get(id)) !== null\n }\n\n // ─── Private ────────────────────────────────────────────────────────\n\n private initFields(doc: Y.Doc): void {\n for (const [fieldName, descriptor] of Object.entries(this.yFields)) {\n switch (descriptor._yjsType) {\n case 'Y.Text':\n doc.getText(fieldName)\n break\n case 'Y.Map':\n doc.getMap(fieldName)\n break\n case 'Y.Array':\n doc.getArray(fieldName)\n break\n }\n }\n }\n}\n\n// ─── Base64 helpers ───────────────────────────────────────────────────────────\n\nfunction uint8ArrayToBase64(bytes: Uint8Array): string {\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]!)\n }\n return btoa(binary)\n}\n\nfunction base64ToUint8Array(base64: string): Uint8Array {\n const binary = atob(base64)\n const bytes = new Uint8Array(binary.length)\n for (let i = 0; i < binary.length; i++) {\n bytes[i] = binary.charCodeAt(i)\n }\n return bytes\n}\n\n// ─── Factory function ─────────────────────────────────────────────────────────\n\n/**\n * Create a `YjsCollection` for a compartment.\n *\n * @param compartment An opened NOYDB compartment.\n * @param name Collection name (must not start with `_`).\n * @param opts Field descriptors (`yFields`) and optional collection settings.\n *\n * @example\n * ```ts\n * import { yjsCollection, yText } from '@noy-db/yjs'\n *\n * const notes = yjsCollection(comp, 'notes', {\n * yFields: { body: yText() },\n * })\n *\n * const doc = await notes.getYDoc('note-1')\n * doc.getText('body').insert(0, 'Hello world')\n * await notes.putYDoc('note-1', doc)\n * ```\n */\nexport function yjsCollection<YF extends Record<string, YFieldDescriptor>>(\n compartment: Compartment,\n name: string,\n opts: YjsCollectionOptions<YF>,\n): YjsCollection<YF> {\n return new YjsCollection(compartment, name, opts)\n}\n","/**\n * Field descriptors for Yjs-backed fields in @noy-db/yjs.\n * Mirrors the dictKey / i18nText descriptor pattern from @noy-db/core.\n */\n\n/** Descriptor for a Y.Text field (rich text, TipTap/ProseMirror compatible). */\nexport interface YTextDescriptor {\n readonly _yjsType: 'Y.Text'\n}\n\n/** Descriptor for a Y.Map<string> field. */\nexport interface YMapDescriptor {\n readonly _yjsType: 'Y.Map'\n}\n\n/** Descriptor for a Y.Array field. */\nexport interface YArrayDescriptor {\n readonly _yjsType: 'Y.Array'\n}\n\nexport type YFieldDescriptor = YTextDescriptor | YMapDescriptor | YArrayDescriptor\n\n/** Declare a Y.Text field (rich text). */\nexport function yText(): YTextDescriptor {\n return { _yjsType: 'Y.Text' }\n}\n\n/** Declare a Y.Map field. */\nexport function yMap(): YMapDescriptor {\n return { _yjsType: 'Y.Map' }\n}\n\n/** Declare a Y.Array field. */\nexport function yArray(): YArrayDescriptor {\n return { _yjsType: 'Y.Array' }\n}\n"],"mappings":";AAWA,YAAY,OAAO;AAyBZ,IAAM,gBAAN,MAAiE;AAAA;AAAA,EAKtE,YACmB,aACA,MACjB,MACA;AAHiB;AACA;AAGjB,SAAK,UAAU,KAAK;AAEpB,SAAK,OAAO,YAAY,WAAmB,MAAM,EAAE,MAAM,MAAM,CAAC;AAAA,EAClE;AAAA,EAPmB;AAAA,EACA;AAAA,EANF;AAAA,EACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAqBjB,MAAM,QAAQ,IAA4B;AACxC,UAAM,MAAM,IAAM,MAAI;AACtB,SAAK,WAAW,GAAG;AAEnB,UAAM,eAAe,MAAM,KAAK,KAAK,IAAI,EAAE;AAC3C,QAAI,cAAc;AAChB,YAAM,QAAQ,mBAAmB,YAAY;AAC7C,MAAE,cAAY,KAAK,KAAK;AAAA,IAC1B;AAEA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,QAAQ,IAAY,KAA2B;AACnD,UAAM,SAAW,sBAAoB,GAAG;AACxC,UAAM,SAAS,mBAAmB,MAAM;AACxC,UAAM,KAAK,KAAK,IAAI,IAAI,MAAM;AAAA,EAChC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,YAAY,IAAY,QAAmC;AAC/D,UAAM,MAAM,MAAM,KAAK,QAAQ,EAAE;AACjC,IAAE,cAAY,KAAK,MAAM;AACzB,UAAM,KAAK,QAAQ,IAAI,GAAG;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,OAAO,IAA2B;AACtC,UAAM,KAAK,KAAK,OAAO,EAAE;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,IAAI,IAA8B;AACtC,WAAQ,MAAM,KAAK,KAAK,IAAI,EAAE,MAAO;AAAA,EACvC;AAAA;AAAA,EAIQ,WAAW,KAAkB;AACnC,eAAW,CAAC,WAAW,UAAU,KAAK,OAAO,QAAQ,KAAK,OAAO,GAAG;AAClE,cAAQ,WAAW,UAAU;AAAA,QAC3B,KAAK;AACH,cAAI,QAAQ,SAAS;AACrB;AAAA,QACF,KAAK;AACH,cAAI,OAAO,SAAS;AACpB;AAAA,QACF,KAAK;AACH,cAAI,SAAS,SAAS;AACtB;AAAA,MACJ;AAAA,IACF;AAAA,EACF;AACF;AAIA,SAAS,mBAAmB,OAA2B;AACrD,MAAI,SAAS;AACb,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,cAAU,OAAO,aAAa,MAAM,CAAC,CAAE;AAAA,EACzC;AACA,SAAO,KAAK,MAAM;AACpB;AAEA,SAAS,mBAAmB,QAA4B;AACtD,QAAM,SAAS,KAAK,MAAM;AAC1B,QAAM,QAAQ,IAAI,WAAW,OAAO,MAAM;AAC1C,WAAS,IAAI,GAAG,IAAI,OAAO,QAAQ,KAAK;AACtC,UAAM,CAAC,IAAI,OAAO,WAAW,CAAC;AAAA,EAChC;AACA,SAAO;AACT;AAwBO,SAAS,cACd,aACA,MACA,MACmB;AACnB,SAAO,IAAI,cAAc,aAAa,MAAM,IAAI;AAClD;;;ACrJO,SAAS,QAAyB;AACvC,SAAO,EAAE,UAAU,SAAS;AAC9B;AAGO,SAAS,OAAuB;AACrC,SAAO,EAAE,UAAU,QAAQ;AAC7B;AAGO,SAAS,SAA2B;AACzC,SAAO,EAAE,UAAU,UAAU;AAC/B;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@noy-db/yjs",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "Yjs Y.Doc interop for noy-db — collaborative rich-text fields with encrypted-at-rest Yjs state",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "vLannaAi <vicio@lanna.ai>",
|
|
7
|
+
"homepage": "https://github.com/vLannaAi/noy-db/tree/main/packages/yjs#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/vLannaAi/noy-db.git",
|
|
11
|
+
"directory": "packages/yjs"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/vLannaAi/noy-db/issues"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"require": {
|
|
25
|
+
"types": "./dist/index.d.cts",
|
|
26
|
+
"default": "./dist/index.cjs"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"main": "./dist/index.cjs",
|
|
31
|
+
"module": "./dist/index.js",
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"files": [
|
|
34
|
+
"dist",
|
|
35
|
+
"README.md",
|
|
36
|
+
"LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=18.0.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"yjs": ">=13.0.0",
|
|
43
|
+
"@noy-db/core": "0.9.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"yjs": "^13.6.27",
|
|
47
|
+
"@noy-db/core": "0.9.0"
|
|
48
|
+
},
|
|
49
|
+
"keywords": [
|
|
50
|
+
"noy-db",
|
|
51
|
+
"yjs",
|
|
52
|
+
"crdt",
|
|
53
|
+
"collaborative",
|
|
54
|
+
"rich-text",
|
|
55
|
+
"encryption",
|
|
56
|
+
"offline-first"
|
|
57
|
+
],
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsup",
|
|
60
|
+
"test": "vitest run",
|
|
61
|
+
"lint": "eslint src/",
|
|
62
|
+
"typecheck": "tsc --noEmit"
|
|
63
|
+
}
|
|
64
|
+
}
|