@evolu/svelte 2.4.0 → 3.0.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Evolu for Svelte
2
2
 
3
- [Evolu](https://github.com/evoluhq/evolu) for [Svelte](https://svelte.dev/).
3
+ This package provides Evolu for [Svelte](https://svelte.dev).
4
4
 
5
5
  ## Documentation
6
6
 
@@ -12,4 +12,4 @@ The Evolu community is on [GitHub Discussions](https://github.com/evoluhq/evolu/
12
12
 
13
13
  To chat with other community members, you can join the [Evolu Discord](https://discord.gg/2J8yyyyxtZ).
14
14
 
15
- [![Twitter URL](https://img.shields.io/twitter/url/https/twitter.com/evoluhq.svg?style=social&label=Follow%20%40evoluhq)](https://twitter.com/evoluhq)
15
+ [![X](https://img.shields.io/twitter/url/https/x.com/evoluhq.svg?style=social&label=Follow%20%40evoluhq)](https://x.com/evoluhq)
@@ -2,8 +2,8 @@
2
2
  * This file needs to be named .svelte.ts to be recognized by the Svelte
3
3
  * compiler in the app itself.
4
4
  */
5
- import type { AppOwner, Evolu, EvoluDeps, EvoluSchema, InferRow, Query, Row } from "@evolu/common/local-first";
6
- export declare const evoluSvelteDeps: EvoluDeps;
5
+ import type { AppOwner, Evolu, EvoluSchema, InferRow, Query, Row } from "@evolu/common/local-first";
6
+ export declare const evoluSvelteDeps: any;
7
7
  /**
8
8
  * Load and subscribe to the Query, and return an object with `rows` property
9
9
  * that are automatically updated when data changes.
@@ -12,7 +12,9 @@ export declare const evoluSvelteDeps: EvoluDeps;
12
12
  *
13
13
  * ```ts
14
14
  * // Create your query
15
- * const allTodos = evolu.createQuery((db) => ...);
15
+ * const allTodos = evolu.createQuery((db) =>
16
+ * db.selectFrom("todo").selectAll(),
17
+ * );
16
18
  *
17
19
  * // Get all rows.
18
20
  * const allTodosState = queryState(evolu, () => allTodos);
@@ -22,10 +24,14 @@ export declare const evoluSvelteDeps: EvoluDeps;
22
24
  *
23
25
  * ```ts
24
26
  * // some kind of state
25
- * let someKindOfState = $state('someId');
27
+ * let someKindOfState = $state("someId");
26
28
  *
27
29
  * // derive your query based other props
28
- * const allTodos = $derived(evolu.createQuery((db) => use someKindOfState here ));
30
+ * const allTodos = $derived(
31
+ * evolu.createQuery((db) =>
32
+ * db.selectFrom("todo").where("id", "=", someKindOfState).selectAll(),
33
+ * ),
34
+ * );
29
35
  *
30
36
  * // Get all rows, once someKindOfState changes, this allTodosState will be updated with the evolu query result
31
37
  * const allTodosState = queryState(evolu, () => allTodos);
@@ -33,14 +39,14 @@ export declare const evoluSvelteDeps: EvoluDeps;
33
39
  * // use allTodosState.rows in further calculations / UI
34
40
  * ```
35
41
  */
36
- export declare function queryState<R extends Row, Schema extends EvoluSchema, MappedRow = InferRow<Query<R>>>(evolu: Evolu<Schema>,
42
+ export declare const queryState: <R extends Row, Schema extends EvoluSchema, MappedRow = InferRow<Query<Schema, R>>>(evolu: Evolu<Schema>,
37
43
  /**
38
44
  * Can be a normal query or a derived query.
39
45
  *
40
46
  * Svelte reactivity: it needs to be a callback, if the query is $derived this
41
47
  * will re-trigger the load/subscription based on the new query.
42
48
  */
43
- observedQuery: () => Query<R> | undefined, options?: {
49
+ observedQuery: () => Query<Schema, R> | undefined, options?: {
44
50
  /**
45
51
  * This is a little helper so that you can map the results instead of using
46
52
  * a $derive operation.
@@ -48,7 +54,7 @@ observedQuery: () => Query<R> | undefined, options?: {
48
54
  * @param row
49
55
  */
50
56
  mapping?: (row: R) => MappedRow;
51
- }): {
57
+ }) => {
52
58
  readonly rows: Array<MappedRow>;
53
59
  };
54
60
  /**
@@ -65,6 +71,7 @@ observedQuery: () => Query<R> | undefined, options?: {
65
71
  * // it will be undefined initially and set once the promise resolves
66
72
  * ```
67
73
  */
68
- export declare function appOwnerState<Schema extends EvoluSchema>(evolu: Evolu<Schema>): {
74
+ export declare const appOwnerState: <Schema extends EvoluSchema>(evolu: Evolu<Schema>) => {
69
75
  readonly current: AppOwner | undefined;
70
76
  };
77
+ //# sourceMappingURL=index.svelte.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.svelte.d.ts","sourceRoot":"","sources":["../src/lib/index.svelte.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,QAAQ,EACR,KAAK,EACL,WAAW,EACX,QAAQ,EACR,KAAK,EAEL,GAAG,EACJ,MAAM,2BAA2B,CAAC;AAKnC,eAAO,MAAM,eAAe,KAAkC,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,eAAO,MAAM,UAAU,GACrB,CAAC,SAAS,GAAG,EACb,MAAM,SAAS,WAAW,EAC1B,SAAS,GAAG,QAAQ,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAEtC,OAAO,KAAK,CAAC,MAAM,CAAC;AACpB;;;;;GAKG;AACH,eAAe,MAAM,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,SAAS,EACjD,UAAU;IACR;;;;;OAKG;IACH,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,SAAS,CAAC;CACjC,KACA;IAAE,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,SAAS,CAAC,CAAA;CA2CnC,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,SAAS,WAAW,EACtD,OAAO,KAAK,CAAC,MAAM,CAAC,KACnB;IACD,QAAQ,CAAC,OAAO,EAAE,QAAQ,GAAG,SAAS,CAAC;CAkBxC,CAAC"}
@@ -2,9 +2,10 @@
2
2
  * This file needs to be named .svelte.ts to be recognized by the Svelte
3
3
  * compiler in the app itself.
4
4
  */
5
- import { evoluWebDeps } from "@evolu/web";
5
+ import { createEvoluDeps } from "@evolu/web";
6
6
  // just in case we need to add some svelte specific deps
7
- export const evoluSvelteDeps = Object.assign({}, evoluWebDeps);
7
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
8
+ export const evoluSvelteDeps = /*#__PURE__*/ createEvoluDeps();
8
9
  /**
9
10
  * Load and subscribe to the Query, and return an object with `rows` property
10
11
  * that are automatically updated when data changes.
@@ -13,7 +14,9 @@ export const evoluSvelteDeps = Object.assign({}, evoluWebDeps);
13
14
  *
14
15
  * ```ts
15
16
  * // Create your query
16
- * const allTodos = evolu.createQuery((db) => ...);
17
+ * const allTodos = evolu.createQuery((db) =>
18
+ * db.selectFrom("todo").selectAll(),
19
+ * );
17
20
  *
18
21
  * // Get all rows.
19
22
  * const allTodosState = queryState(evolu, () => allTodos);
@@ -23,10 +26,14 @@ export const evoluSvelteDeps = Object.assign({}, evoluWebDeps);
23
26
  *
24
27
  * ```ts
25
28
  * // some kind of state
26
- * let someKindOfState = $state('someId');
29
+ * let someKindOfState = $state("someId");
27
30
  *
28
31
  * // derive your query based other props
29
- * const allTodos = $derived(evolu.createQuery((db) => use someKindOfState here ));
32
+ * const allTodos = $derived(
33
+ * evolu.createQuery((db) =>
34
+ * db.selectFrom("todo").where("id", "=", someKindOfState).selectAll(),
35
+ * ),
36
+ * );
30
37
  *
31
38
  * // Get all rows, once someKindOfState changes, this allTodosState will be updated with the evolu query result
32
39
  * const allTodosState = queryState(evolu, () => allTodos);
@@ -34,18 +41,18 @@ export const evoluSvelteDeps = Object.assign({}, evoluWebDeps);
34
41
  * // use allTodosState.rows in further calculations / UI
35
42
  * ```
36
43
  */
37
- export function queryState(evolu,
44
+ export const queryState = (evolu,
38
45
  /**
39
46
  * Can be a normal query or a derived query.
40
47
  *
41
48
  * Svelte reactivity: it needs to be a callback, if the query is $derived this
42
49
  * will re-trigger the load/subscription based on the new query.
43
50
  */
44
- observedQuery, options) {
51
+ observedQuery, options) => {
45
52
  {
46
53
  // writing to this variable - svelte's compiler will track it
47
54
  let writableState = $state([]);
48
- function updateState(rows) {
55
+ const updateState = (rows) => {
49
56
  if (options === null || options === void 0 ? void 0 : options.mapping) {
50
57
  // re-assigning because somehow typescript thinks its still nullable here
51
58
  // remove again once no issue anymore
@@ -53,7 +60,7 @@ observedQuery, options) {
53
60
  writableState = rows.map((row) => mapper(row));
54
61
  }
55
62
  writableState = rows;
56
- }
63
+ };
57
64
  $effect(() => {
58
65
  const query = observedQuery();
59
66
  if (!query) {
@@ -76,7 +83,7 @@ observedQuery, options) {
76
83
  },
77
84
  };
78
85
  }
79
- }
86
+ };
80
87
  /**
81
88
  * Get the {@link AppOwner} promise that resolves when available.
82
89
  *
@@ -91,14 +98,13 @@ observedQuery, options) {
91
98
  * // it will be undefined initially and set once the promise resolves
92
99
  * ```
93
100
  */
94
- export function appOwnerState(evolu) {
101
+ export const appOwnerState = (evolu) => {
95
102
  {
96
103
  // writing to this variable - svelte's compiler will track it
97
104
  let writableState = $state(undefined);
98
105
  $effect(() => {
99
- void evolu.appOwner.then((appOwner) => {
100
- writableState = appOwner;
101
- });
106
+ // TODO: Review, it was promise, it isn't anymore.
107
+ writableState = evolu.appOwner;
102
108
  });
103
109
  return {
104
110
  // Svelte reactivity: it needs to be a getter
@@ -107,4 +113,4 @@ export function appOwnerState(evolu) {
107
113
  },
108
114
  };
109
115
  }
110
- }
116
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evolu/svelte",
3
- "version": "2.4.0",
3
+ "version": "3.0.0",
4
4
  "description": "Evolu for Svelte",
5
5
  "keywords": [
6
6
  "evolu",
@@ -10,9 +10,12 @@
10
10
  "bugs": {
11
11
  "url": "https://github.com/evoluhq/evolu/issues"
12
12
  },
13
- "repository": "evoluhq/evolu",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/evoluhq/evolu.git"
16
+ },
14
17
  "license": "MIT",
15
- "sideEffects": [],
18
+ "sideEffects": false,
16
19
  "type": "module",
17
20
  "exports": {
18
21
  ".": {
@@ -20,36 +23,35 @@
20
23
  "svelte": "./dist/index.svelte.js"
21
24
  }
22
25
  },
23
- "svelte": "./dist/index.svelte.js",
24
26
  "types": "./dist/index.svelte.d.ts",
25
27
  "files": [
26
28
  "dist",
29
+ "src/**",
27
30
  "!dist/**/*.test.*",
28
31
  "!dist/**/*.spec.*"
29
32
  ],
30
33
  "devDependencies": {
31
- "@sveltejs/package": "^2.5.0",
32
- "@tsconfig/svelte": "^5.0.5",
33
- "svelte": "^5.38.2",
34
- "svelte-check": "^4.3.1",
35
- "typescript": "^5.9.2",
36
- "@evolu/common": "7.4.0",
37
- "@evolu/tsconfig": "0.0.2",
38
- "@evolu/web": "2.4.0"
34
+ "@sveltejs/package": "^2.5.7",
35
+ "@tsconfig/svelte": "^5.0.6",
36
+ "@typescript/native": "npm:typescript@^7.0.2",
37
+ "svelte": "^5.56.8",
38
+ "svelte-check": "^4.7.4",
39
+ "typescript": "npm:@typescript/typescript6@^6.0.2",
40
+ "@evolu/typescript-config": "0.0.2",
41
+ "@evolu/web": "3.0.0",
42
+ "@evolu/common": "8.0.0"
39
43
  },
40
44
  "peerDependencies": {
41
- "@evolu/common": "^7.4.0",
42
- "@evolu/web": "^2.4.0",
45
+ "@evolu/common": "^8.0.0-next.0",
46
+ "@evolu/web": "^3.0.0-next.0",
43
47
  "svelte": ">=5"
44
48
  },
45
49
  "publishConfig": {
46
50
  "access": "public"
47
51
  },
48
52
  "scripts": {
49
- "build": "rimraf dist && tsc && pnpm run package",
53
+ "build": "tsc --build tsconfig.build.json && pnpm package",
50
54
  "check": "svelte-check --tsconfig ./tsconfig.json",
51
- "clean": "rimraf .turbo .svelte-kit node_modules dist",
52
- "dev": "tsc --watch",
53
55
  "package": "svelte-package",
54
56
  "preview": "vite preview"
55
57
  }
@@ -0,0 +1,158 @@
1
+ /**
2
+ * This file needs to be named .svelte.ts to be recognized by the Svelte
3
+ * compiler in the app itself.
4
+ */
5
+
6
+ import type {
7
+ AppOwner,
8
+ Evolu,
9
+ EvoluSchema,
10
+ InferRow,
11
+ Query,
12
+ QueryRows,
13
+ Row,
14
+ } from "@evolu/common/local-first";
15
+ import { createEvoluDeps } from "@evolu/web";
16
+
17
+ // just in case we need to add some svelte specific deps
18
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
19
+ export const evoluSvelteDeps = /*#__PURE__*/ createEvoluDeps();
20
+
21
+ /**
22
+ * Load and subscribe to the Query, and return an object with `rows` property
23
+ * that are automatically updated when data changes.
24
+ *
25
+ * ### Example
26
+ *
27
+ * ```ts
28
+ * // Create your query
29
+ * const allTodos = evolu.createQuery((db) =>
30
+ * db.selectFrom("todo").selectAll(),
31
+ * );
32
+ *
33
+ * // Get all rows.
34
+ * const allTodosState = queryState(evolu, () => allTodos);
35
+ * ```
36
+ *
37
+ * ### Example
38
+ *
39
+ * ```ts
40
+ * // some kind of state
41
+ * let someKindOfState = $state("someId");
42
+ *
43
+ * // derive your query based other props
44
+ * const allTodos = $derived(
45
+ * evolu.createQuery((db) =>
46
+ * db.selectFrom("todo").where("id", "=", someKindOfState).selectAll(),
47
+ * ),
48
+ * );
49
+ *
50
+ * // Get all rows, once someKindOfState changes, this allTodosState will be updated with the evolu query result
51
+ * const allTodosState = queryState(evolu, () => allTodos);
52
+ *
53
+ * // use allTodosState.rows in further calculations / UI
54
+ * ```
55
+ */
56
+ export const queryState = <
57
+ R extends Row,
58
+ Schema extends EvoluSchema,
59
+ MappedRow = InferRow<Query<Schema, R>>,
60
+ >(
61
+ evolu: Evolu<Schema>,
62
+ /**
63
+ * Can be a normal query or a derived query.
64
+ *
65
+ * Svelte reactivity: it needs to be a callback, if the query is $derived this
66
+ * will re-trigger the load/subscription based on the new query.
67
+ */
68
+ observedQuery: () => Query<Schema, R> | undefined,
69
+ options?: {
70
+ /**
71
+ * This is a little helper so that you can map the results instead of using
72
+ * a $derive operation.
73
+ *
74
+ * @param row
75
+ */
76
+ mapping?: (row: R) => MappedRow;
77
+ },
78
+ ): { readonly rows: Array<MappedRow> } => {
79
+ {
80
+ // writing to this variable - svelte's compiler will track it
81
+ let writableState: Array<MappedRow> = $state([]);
82
+
83
+ const updateState = (rows: QueryRows<R>): void => {
84
+ if (options?.mapping) {
85
+ // re-assigning because somehow typescript thinks its still nullable here
86
+ // remove again once no issue anymore
87
+ const mapper = options.mapping;
88
+ writableState = rows.map((row) => mapper(row));
89
+ }
90
+
91
+ writableState = rows as Array<MappedRow>;
92
+ };
93
+
94
+ $effect(() => {
95
+ const query = observedQuery();
96
+
97
+ if (!query) {
98
+ return;
99
+ }
100
+
101
+ // always setting the state on first load
102
+ // for a) if the query changes we definitely need current content immediately
103
+ // for b) if you sub/unsub in a very short time can cause the subscription not to trigger a callback
104
+ // => this is also for HMR
105
+ void evolu.loadQuery(query).then(updateState);
106
+
107
+ return evolu.subscribeQuery(query)(() => {
108
+ const rows = evolu.getQueryRows(query);
109
+
110
+ updateState(rows);
111
+ });
112
+ });
113
+
114
+ return {
115
+ // Svelte reactivity: it needs to be a getter
116
+ get rows() {
117
+ return writableState;
118
+ },
119
+ };
120
+ }
121
+ };
122
+
123
+ /**
124
+ * Get the {@link AppOwner} promise that resolves when available.
125
+ *
126
+ * ### Example
127
+ *
128
+ * ```ts
129
+ * import { appOwnerState } from "@evolu/svelte";
130
+ *
131
+ * const owner = appOwnerState(evolu);
132
+ *
133
+ * // use owner.current in your Svelte templates
134
+ * // it will be undefined initially and set once the promise resolves
135
+ * ```
136
+ */
137
+ export const appOwnerState = <Schema extends EvoluSchema>(
138
+ evolu: Evolu<Schema>,
139
+ ): {
140
+ readonly current: AppOwner | undefined;
141
+ } => {
142
+ {
143
+ // writing to this variable - svelte's compiler will track it
144
+ let writableState = $state<AppOwner | undefined>(undefined);
145
+
146
+ $effect(() => {
147
+ // TODO: Review, it was promise, it isn't anymore.
148
+ writableState = evolu.appOwner;
149
+ });
150
+
151
+ return {
152
+ // Svelte reactivity: it needs to be a getter
153
+ get current() {
154
+ return writableState;
155
+ },
156
+ };
157
+ }
158
+ };