@super-line/tanstack-db 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.cjs +79 -0
- package/dist/index.d.cts +32 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +54 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mert
|
|
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,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
superLineCollectionOptions: () => superLineCollectionOptions
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
function superLineCollectionOptions(client, contract, name, opts = {}) {
|
|
27
|
+
const key = contract.collections?.[name]?.key;
|
|
28
|
+
if (!key) throw new Error(`superLineCollectionOptions: collection '${String(name)}' is not declared on the contract`);
|
|
29
|
+
const handle = client.collection(name);
|
|
30
|
+
return {
|
|
31
|
+
id: `superline:${String(name)}`,
|
|
32
|
+
getKey: (row) => row[key],
|
|
33
|
+
sync: {
|
|
34
|
+
rowUpdateMode: "full",
|
|
35
|
+
// super-line updates carry the whole row
|
|
36
|
+
sync: ({ begin, write, commit, markReady }) => {
|
|
37
|
+
const rowSet = handle.subscribe(opts.query ?? {});
|
|
38
|
+
let ready = false;
|
|
39
|
+
let cancelled = false;
|
|
40
|
+
const off = rowSet.subscribe((ev) => {
|
|
41
|
+
if (!ready || cancelled) return;
|
|
42
|
+
begin();
|
|
43
|
+
if (ev.type === "delete") write({ type: "delete", key: ev.id });
|
|
44
|
+
else write({ type: ev.type, value: ev.row });
|
|
45
|
+
commit();
|
|
46
|
+
});
|
|
47
|
+
void rowSet.ready.then(() => {
|
|
48
|
+
if (cancelled) return;
|
|
49
|
+
begin();
|
|
50
|
+
for (const row of rowSet.rows()) write({ type: "insert", value: row });
|
|
51
|
+
commit();
|
|
52
|
+
ready = true;
|
|
53
|
+
markReady();
|
|
54
|
+
}).catch(() => {
|
|
55
|
+
if (!cancelled) markReady();
|
|
56
|
+
});
|
|
57
|
+
return () => {
|
|
58
|
+
cancelled = true;
|
|
59
|
+
off();
|
|
60
|
+
rowSet.close();
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
// Each optimistic transaction → ONE atomic super-line batch. Ack resolves the optimism; error rolls it back.
|
|
65
|
+
onInsert: async ({ transaction }) => {
|
|
66
|
+
await handle.batch(transaction.mutations.map((m) => ({ type: "insert", row: m.modified })));
|
|
67
|
+
},
|
|
68
|
+
onUpdate: async ({ transaction }) => {
|
|
69
|
+
await handle.batch(transaction.mutations.map((m) => ({ type: "update", row: m.modified })));
|
|
70
|
+
},
|
|
71
|
+
onDelete: async ({ transaction }) => {
|
|
72
|
+
await handle.batch(transaction.mutations.map((m) => ({ type: "delete", id: String(m.key) })));
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
77
|
+
0 && (module.exports = {
|
|
78
|
+
superLineCollectionOptions
|
|
79
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { CollectionQuery, Contract, RoleOf, CollectionName, RowOf } from '@super-line/core';
|
|
2
|
+
import { SuperLineClient } from '@super-line/client';
|
|
3
|
+
import { CollectionConfig } from '@tanstack/db';
|
|
4
|
+
|
|
5
|
+
/** Options for {@link superLineCollectionOptions}. */
|
|
6
|
+
interface SuperLineCollectionOptions {
|
|
7
|
+
/** The subset to sync (filter / orderBy / limit). Omit for the whole collection, subject to the server's row policy. */
|
|
8
|
+
query?: CollectionQuery;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Build TanStack DB collection options backed by a super-line collection. Feed the result to
|
|
12
|
+
* `createCollection(...)`; TanStack then owns client-side live queries, joins, and optimistic mutations while
|
|
13
|
+
* super-line is the server-authoritative sync source.
|
|
14
|
+
*
|
|
15
|
+
* - The initial snapshot + live changes flow from `client.collection(name).subscribe(query)`.
|
|
16
|
+
* - Optimistic mutations map to atomic super-line batches (`onInsert`/`onUpdate`/`onDelete`); the ack resolves
|
|
17
|
+
* the optimistic transaction, an error rolls it back.
|
|
18
|
+
* - `getKey` comes from the contract. No client-side schema validation is wired: super-line validates every
|
|
19
|
+
* write on the server (ADR-0006). // ponytail: pass the contract schema through here for optimistic pre-validation if wanted.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const messages = createCollection(superLineCollectionOptions(client, contract, 'messages', { query: { filter: eq('channelId', id) } }))
|
|
24
|
+
* const users = createCollection(superLineCollectionOptions(client, contract, 'users'))
|
|
25
|
+
* const { data } = useLiveQuery((q) =>
|
|
26
|
+
* q.from({ m: messages }).join({ u: users }, ({ m, u }) => eq(u.id, m.authorId), 'inner'),
|
|
27
|
+
* )
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
declare function superLineCollectionOptions<C extends Contract, R extends RoleOf<C>, N extends CollectionName<C>>(client: SuperLineClient<C, R>, contract: C, name: N, opts?: SuperLineCollectionOptions): CollectionConfig<RowOf<C, N> & object, string>;
|
|
31
|
+
|
|
32
|
+
export { type SuperLineCollectionOptions, superLineCollectionOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { CollectionQuery, Contract, RoleOf, CollectionName, RowOf } from '@super-line/core';
|
|
2
|
+
import { SuperLineClient } from '@super-line/client';
|
|
3
|
+
import { CollectionConfig } from '@tanstack/db';
|
|
4
|
+
|
|
5
|
+
/** Options for {@link superLineCollectionOptions}. */
|
|
6
|
+
interface SuperLineCollectionOptions {
|
|
7
|
+
/** The subset to sync (filter / orderBy / limit). Omit for the whole collection, subject to the server's row policy. */
|
|
8
|
+
query?: CollectionQuery;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Build TanStack DB collection options backed by a super-line collection. Feed the result to
|
|
12
|
+
* `createCollection(...)`; TanStack then owns client-side live queries, joins, and optimistic mutations while
|
|
13
|
+
* super-line is the server-authoritative sync source.
|
|
14
|
+
*
|
|
15
|
+
* - The initial snapshot + live changes flow from `client.collection(name).subscribe(query)`.
|
|
16
|
+
* - Optimistic mutations map to atomic super-line batches (`onInsert`/`onUpdate`/`onDelete`); the ack resolves
|
|
17
|
+
* the optimistic transaction, an error rolls it back.
|
|
18
|
+
* - `getKey` comes from the contract. No client-side schema validation is wired: super-line validates every
|
|
19
|
+
* write on the server (ADR-0006). // ponytail: pass the contract schema through here for optimistic pre-validation if wanted.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const messages = createCollection(superLineCollectionOptions(client, contract, 'messages', { query: { filter: eq('channelId', id) } }))
|
|
24
|
+
* const users = createCollection(superLineCollectionOptions(client, contract, 'users'))
|
|
25
|
+
* const { data } = useLiveQuery((q) =>
|
|
26
|
+
* q.from({ m: messages }).join({ u: users }, ({ m, u }) => eq(u.id, m.authorId), 'inner'),
|
|
27
|
+
* )
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
declare function superLineCollectionOptions<C extends Contract, R extends RoleOf<C>, N extends CollectionName<C>>(client: SuperLineClient<C, R>, contract: C, name: N, opts?: SuperLineCollectionOptions): CollectionConfig<RowOf<C, N> & object, string>;
|
|
31
|
+
|
|
32
|
+
export { type SuperLineCollectionOptions, superLineCollectionOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function superLineCollectionOptions(client, contract, name, opts = {}) {
|
|
3
|
+
const key = contract.collections?.[name]?.key;
|
|
4
|
+
if (!key) throw new Error(`superLineCollectionOptions: collection '${String(name)}' is not declared on the contract`);
|
|
5
|
+
const handle = client.collection(name);
|
|
6
|
+
return {
|
|
7
|
+
id: `superline:${String(name)}`,
|
|
8
|
+
getKey: (row) => row[key],
|
|
9
|
+
sync: {
|
|
10
|
+
rowUpdateMode: "full",
|
|
11
|
+
// super-line updates carry the whole row
|
|
12
|
+
sync: ({ begin, write, commit, markReady }) => {
|
|
13
|
+
const rowSet = handle.subscribe(opts.query ?? {});
|
|
14
|
+
let ready = false;
|
|
15
|
+
let cancelled = false;
|
|
16
|
+
const off = rowSet.subscribe((ev) => {
|
|
17
|
+
if (!ready || cancelled) return;
|
|
18
|
+
begin();
|
|
19
|
+
if (ev.type === "delete") write({ type: "delete", key: ev.id });
|
|
20
|
+
else write({ type: ev.type, value: ev.row });
|
|
21
|
+
commit();
|
|
22
|
+
});
|
|
23
|
+
void rowSet.ready.then(() => {
|
|
24
|
+
if (cancelled) return;
|
|
25
|
+
begin();
|
|
26
|
+
for (const row of rowSet.rows()) write({ type: "insert", value: row });
|
|
27
|
+
commit();
|
|
28
|
+
ready = true;
|
|
29
|
+
markReady();
|
|
30
|
+
}).catch(() => {
|
|
31
|
+
if (!cancelled) markReady();
|
|
32
|
+
});
|
|
33
|
+
return () => {
|
|
34
|
+
cancelled = true;
|
|
35
|
+
off();
|
|
36
|
+
rowSet.close();
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
// Each optimistic transaction → ONE atomic super-line batch. Ack resolves the optimism; error rolls it back.
|
|
41
|
+
onInsert: async ({ transaction }) => {
|
|
42
|
+
await handle.batch(transaction.mutations.map((m) => ({ type: "insert", row: m.modified })));
|
|
43
|
+
},
|
|
44
|
+
onUpdate: async ({ transaction }) => {
|
|
45
|
+
await handle.batch(transaction.mutations.map((m) => ({ type: "update", row: m.modified })));
|
|
46
|
+
},
|
|
47
|
+
onDelete: async ({ transaction }) => {
|
|
48
|
+
await handle.batch(transaction.mutations.map((m) => ({ type: "delete", id: String(m.key) })));
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export {
|
|
53
|
+
superLineCollectionOptions
|
|
54
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@super-line/tanstack-db",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "TanStack DB collection adapter for super-line — use super-line collections as a TanStack DB sync source with client-side live queries and joins.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Mert",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"super-line",
|
|
10
|
+
"tanstack",
|
|
11
|
+
"tanstack-db",
|
|
12
|
+
"collections",
|
|
13
|
+
"sync",
|
|
14
|
+
"live-query"
|
|
15
|
+
],
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/mertdogar/super-line.git",
|
|
19
|
+
"directory": "packages/tanstack-db"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://mertdogar.github.io/super-line/",
|
|
22
|
+
"bugs": "https://github.com/mertdogar/super-line/issues",
|
|
23
|
+
"main": "./dist/index.cjs",
|
|
24
|
+
"module": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"import": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./dist/index.d.cts",
|
|
34
|
+
"default": "./dist/index.cjs"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist"
|
|
40
|
+
],
|
|
41
|
+
"sideEffects": false,
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=18"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@super-line/core": "^0.10.1",
|
|
50
|
+
"@super-line/client": "^0.8.0"
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"@tanstack/db": ">=0.1"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@tanstack/db": "^0.5.0",
|
|
57
|
+
"ws": "^8.18.0",
|
|
58
|
+
"zod": "^3.24.1",
|
|
59
|
+
"@super-line/server": "0.10.2",
|
|
60
|
+
"@super-line/collections-memory": "0.1.0",
|
|
61
|
+
"@super-line/transport-websocket": "0.6.0"
|
|
62
|
+
},
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "tsup"
|
|
65
|
+
}
|
|
66
|
+
}
|