@firtoz/db-helpers 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/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # @firtoz/db-helpers
2
+
3
+ TanStack DB helpers and utilities. TypeScript-only, no build step—consume source directly.
4
+
5
+ This package is a small collection of helpers for [@tanstack/db](https://tanstack.com/db). More utilities will be added over time.
6
+
7
+ ## Current helpers
8
+
9
+ - **Memory collection** – In-memory TanStack DB collection with sync adapter and `truncate` utility. Useful for tests and ephemeral state.
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ bun add @firtoz/db-helpers
15
+ # or npm/pnpm/yarn
16
+ ```
17
+
18
+ Peer dependencies: `@tanstack/db` and `@standard-schema/spec`.
19
+
20
+ ## Usage
21
+
22
+ ```ts
23
+ import {
24
+ createMemoryCollection,
25
+ memoryCollectionOptions,
26
+ type MemoryCollection,
27
+ } from "@firtoz/db-helpers";
28
+ ```
29
+
30
+ See [@tanstack/db](https://tanstack.com/db) docs for collection usage.
31
+
32
+ ## License
33
+
34
+ MIT
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@firtoz/db-helpers",
3
+ "version": "0.1.0",
4
+ "description": "TanStack DB helpers and utilities",
5
+ "main": "./src/index.ts",
6
+ "module": "./src/index.ts",
7
+ "types": "./src/index.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./src/index.ts",
11
+ "import": "./src/index.ts",
12
+ "require": "./src/index.ts"
13
+ }
14
+ },
15
+ "files": [
16
+ "src/**/*.ts",
17
+ "!src/**/*.test.ts",
18
+ "README.md"
19
+ ],
20
+ "scripts": {
21
+ "typecheck": "tsc --noEmit -p ./tsconfig.json",
22
+ "lint": "biome check --write src",
23
+ "lint:ci": "biome ci src",
24
+ "format": "biome format src --write"
25
+ },
26
+ "keywords": [
27
+ "typescript",
28
+ "tanstack",
29
+ "tanstack-db",
30
+ "db-helpers"
31
+ ],
32
+ "author": "Firtina Ozbalikchi <firtoz@github.com>",
33
+ "license": "MIT",
34
+ "homepage": "https://github.com/firtoz/fullstack-toolkit#readme",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "https://github.com/firtoz/fullstack-toolkit.git",
38
+ "directory": "packages/db-helpers"
39
+ },
40
+ "bugs": {
41
+ "url": "https://github.com/firtoz/fullstack-toolkit/issues"
42
+ },
43
+ "engines": {
44
+ "node": ">=18.0.0"
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "peerDependencies": {
50
+ "@standard-schema/spec": ">=1.1.0",
51
+ "@tanstack/db": ">=0.5.25"
52
+ },
53
+ "devDependencies": {
54
+ "@standard-schema/spec": "^1.1.0",
55
+ "@tanstack/db": "^0.5.25"
56
+ }
57
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export {
2
+ createMemoryCollection,
3
+ memoryCollectionOptions,
4
+ type MemoryCollection,
5
+ } from "./memoryCollection";
@@ -0,0 +1,124 @@
1
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
2
+ import {
3
+ type Collection,
4
+ type CollectionConfig,
5
+ createCollection,
6
+ type DeleteMutationFnParams,
7
+ type InferSchemaInput,
8
+ type InferSchemaOutput,
9
+ type InsertMutationFnParams,
10
+ type SyncConfig,
11
+ type UpdateMutationFnParams,
12
+ } from "@tanstack/db";
13
+
14
+ type MemoryCollectionConfig<TSchema extends StandardSchemaV1> = Omit<
15
+ CollectionConfig<InferSchemaOutput<TSchema>, string | number, TSchema>,
16
+ "onInsert" | "onUpdate" | "onDelete" | "sync" | "schema"
17
+ > & {
18
+ schema: TSchema;
19
+ };
20
+
21
+ type MemoryUtils = {
22
+ truncate: () => Promise<void>;
23
+ };
24
+
25
+ export function memoryCollectionOptions<TSchema extends StandardSchemaV1>(
26
+ config: MemoryCollectionConfig<TSchema>,
27
+ ): CollectionConfig<InferSchemaOutput<TSchema>, string | number, TSchema> & {
28
+ utils: MemoryUtils;
29
+ schema: TSchema;
30
+ } {
31
+ type TItem = InferSchemaOutput<TSchema>;
32
+ let syncParams: Parameters<SyncConfig<TItem>["sync"]>[0] | null = null;
33
+
34
+ const sync: SyncConfig<TItem>["sync"] = (params) => {
35
+ syncParams = params;
36
+
37
+ params.markReady();
38
+
39
+ // Return cleanup function
40
+ return () => {};
41
+ };
42
+
43
+ // All mutation handlers use the same transaction sender
44
+ const onInsert = async (params: InsertMutationFnParams<TItem>) => {
45
+ if (!syncParams) {
46
+ throw new Error("Sync parameters not initialized");
47
+ }
48
+ syncParams.begin();
49
+ for (const mutation of params.transaction.mutations) {
50
+ syncParams.write({
51
+ type: "insert",
52
+ value: mutation.modified,
53
+ });
54
+ }
55
+ syncParams.commit();
56
+ };
57
+
58
+ const onUpdate = async (params: UpdateMutationFnParams<TItem>) => {
59
+ if (!syncParams) {
60
+ throw new Error("Sync parameters not initialized");
61
+ }
62
+ syncParams.begin();
63
+ for (const mutation of params.transaction.mutations) {
64
+ syncParams.write({
65
+ type: "update",
66
+ value: mutation.modified,
67
+ previousValue: mutation.original,
68
+ });
69
+ }
70
+ syncParams.commit();
71
+ };
72
+
73
+ const onDelete = async (params: DeleteMutationFnParams<TItem>) => {
74
+ if (!syncParams) {
75
+ throw new Error("Sync parameters not initialized");
76
+ }
77
+ syncParams.begin();
78
+ for (const mutation of params.transaction.mutations) {
79
+ syncParams.write({
80
+ type: "delete",
81
+ key: mutation.key,
82
+ });
83
+ }
84
+ syncParams?.commit();
85
+ };
86
+
87
+ const truncate = async () => {
88
+ if (!syncParams) {
89
+ throw new Error("Sync parameters not initialized");
90
+ }
91
+ syncParams.begin();
92
+ syncParams.truncate();
93
+ syncParams.commit();
94
+ };
95
+
96
+ return {
97
+ id: config.id,
98
+ schema: config.schema,
99
+ getKey: config.getKey,
100
+ sync: { sync },
101
+ onInsert,
102
+ onUpdate,
103
+ onDelete,
104
+ utils: {
105
+ truncate,
106
+ },
107
+ };
108
+ }
109
+
110
+ export type MemoryCollection<TSchema extends StandardSchemaV1> = Collection<
111
+ InferSchemaOutput<TSchema>,
112
+ string | number,
113
+ MemoryUtils,
114
+ TSchema,
115
+ InferSchemaInput<TSchema>
116
+ >;
117
+
118
+ export function createMemoryCollection<TSchema extends StandardSchemaV1>(
119
+ config: MemoryCollectionConfig<TSchema>,
120
+ ): MemoryCollection<TSchema> {
121
+ return createCollection(
122
+ memoryCollectionOptions(config),
123
+ ) as MemoryCollection<TSchema>;
124
+ }