create-wirejs-app 2.0.33 → 2.0.35-table-resource

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-wirejs-app",
3
- "version": "2.0.33",
3
+ "version": "2.0.35-table-resource",
4
4
  "description": "Initializes a wirejs package.",
5
5
  "author": "Jon Wire",
6
6
  "license": "MIT",
@@ -1,14 +1,27 @@
1
- import { AuthenticationService, FileService, withContext } from 'wirejs-resources';
1
+ import {
2
+ AuthenticationService,
3
+ DistributedTable,
4
+ FileService,
5
+ PassThruParser,
6
+ withContext
7
+ } from 'wirejs-resources';
8
+
9
+ const userTodos = new DistributedTable('app', 'userTodos', {
10
+ parse: PassThruParser<Todo & { userId: string }>,
11
+ key: {
12
+ partition: 'userId',
13
+ sort: ['id']
14
+ }
15
+ });
2
16
 
3
- const userTodos = new FileService('app', 'userTodoApp');
4
17
  const wikiPages = new FileService('app', 'wikiPages');
5
18
  const authService = new AuthenticationService('app', 'core-users');
6
19
 
7
20
  export const auth = authService.buildApi();
8
-
9
21
  export type Todo = {
10
22
  id: string;
11
23
  text: string;
24
+ order: number;
12
25
  };
13
26
 
14
27
  export const todos = withContext(context => ({
@@ -16,27 +29,49 @@ export const todos = withContext(context => ({
16
29
  const user = await auth.requireCurrentUser(context);
17
30
 
18
31
  try {
19
- const todos = await userTodos.read(`${user.id}/todos.json`);
20
- return todos ? JSON.parse(todos) : [];
32
+ const todos = await userTodos.query({ userId: user.id });
33
+ const todosArray = await Array.fromAsync(todos);
34
+ return todosArray
35
+ .sort((a, b) => a.order - b.order)
36
+ .map(todo => ({
37
+ id: todo.id,
38
+ text: todo.text,
39
+ order: todo.order,
40
+ }));
21
41
  } catch (error) {
22
42
  return [];
23
43
  }
24
44
  },
25
- async write(todos: Todo[]) {
45
+
46
+ async save(todo: Todo) {
26
47
  const user = await auth.requireCurrentUser(context);
27
48
 
28
- if (!Array.isArray(todos) || !todos.every(todo =>
29
- typeof todo.id === 'string'
30
- && typeof todo.text === 'string')
31
- ) {
32
- throw new Error("Invalid todos!");
49
+ if (typeof todo.id !== 'string' || typeof todo.text !== 'string') {
50
+ throw new Error("Invalid todo!");
33
51
  }
34
52
 
35
- const finalTodos = todos.map(todo => ({ id: todo.id, text: todo.text }));
36
- await userTodos.write(`${user.id}/todos.json`, JSON.stringify(finalTodos));
53
+ const finalTodo = {
54
+ userId: user.id,
55
+ id: todo.id,
56
+ text: todo.text,
57
+ order: todo.order,
58
+ };
59
+ await userTodos.save(finalTodo);
37
60
 
38
61
  return true;
39
- }
62
+ },
63
+
64
+ async remove(todoId: string) {
65
+ const user = await auth.requireCurrentUser(context);
66
+
67
+ if (typeof todoId !== 'string') {
68
+ throw new Error("Invalid todo ID!");
69
+ }
70
+
71
+ await userTodos.delete({ userId: user.id, id: todoId });
72
+
73
+ return true;
74
+ },
40
75
  }));
41
76
 
42
77
  function normalizeWikiPageFilename(page: string) {
@@ -11,10 +11,10 @@
11
11
  "dompurify": "^3.2.3",
12
12
  "marked": "^15.0.6",
13
13
  "wirejs-dom": "^1.0.38",
14
- "wirejs-resources": "^0.1.28"
14
+ "wirejs-resources": "^0.1.30-table-resource"
15
15
  },
16
16
  "devDependencies": {
17
- "wirejs-scripts": "^3.0.26",
17
+ "wirejs-scripts": "^3.0.28-table-resource",
18
18
  "typescript": "^5.7.3"
19
19
  },
20
20
  "scripts": {
@@ -3,9 +3,9 @@ import { accountMenu } from '../components/account-menu.js';
3
3
  import { auth, todos, Todo } from 'my-api';
4
4
 
5
5
  function Todos() {
6
- const save = async () => {
6
+ const add = async (todo: Todo) => {
7
7
  try {
8
- await todos.write(null, self.data.todos);
8
+ await todos.save(null, todo);
9
9
  } catch (error) {
10
10
  alert(error);
11
11
  }
@@ -13,7 +13,7 @@ function Todos() {
13
13
 
14
14
  const remove = (todo: Todo) => {
15
15
  self.data.todos = self.data.todos.filter(t => t.id !== todo.id);
16
- save();
16
+ todos.remove(null, todo.id);
17
17
  }
18
18
 
19
19
  const newid = () => crypto.randomUUID();
@@ -29,9 +29,16 @@ function Todos() {
29
29
  <div>
30
30
  <form onsubmit=${(event: Event) => {
31
31
  event.preventDefault();
32
- self.data.todos.push({ id: newid(), text: self.data.newTodo });
32
+ const todo = {
33
+ id: newid(),
34
+ text: self.data.newTodo,
35
+ order: (self.data.todos[
36
+ self.data.todos.length - 1
37
+ ]?.order ?? 0) + 1,
38
+ };
39
+ self.data.todos.push(todo);
33
40
  self.data.newTodo = '';
34
- save();
41
+ add(todo);
35
42
  }}>
36
43
  <input type='text' value=${attribute('newTodo', '' as string)} />
37
44
  <input type='submit' value='Add' />
@@ -2,14 +2,15 @@
2
2
  "compilerOptions": {
3
3
  "module": "ESNext",
4
4
  "target": "ESNext",
5
+ "lib": ["ESNext", "DOM"],
5
6
  "moduleResolution": "bundler",
6
7
  "outDir": "./dist",
7
8
  "esModuleInterop": true,
8
9
  "strict": true,
9
10
  "noImplicitAny": true,
10
11
  "skipLibCheck": true,
11
- "declaration": true,
12
+ "declaration": false,
12
13
  },
13
- "include": ["src/**/*"],
14
+ "include": ["src/**/*", "api/**/*"],
14
15
  "exclude": ["node_modules"]
15
16
  }