Skip to content

Commit d8ccf5d

Browse files
committed
feat(docs): create-script guide
1 parent 97368ab commit d8ccf5d

File tree

20 files changed

+305
-50
lines changed

20 files changed

+305
-50
lines changed

packages/docs/content/en-US/1.guide/1.create-collections.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ Each column type have different options to be customized, but for now let's just
4949

5050
With this we already have the basic structure of our collection and we can create our todos
5151

52-
![views-table](/views.table.jpeg)
52+
![views-table](/views.table.jpeg)
53+
54+
- [Check this example here](https://github.com/sidekick-coder/index-san/tree/main/packages/examples/todo-list)
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Create scripts
2+
3+
Scripts are javascript code that run simples tasks using(or not) workspace files & collections.
4+
5+
They are very powerful but they can also be dangerous if you do not take care, so pay extra attention to the code you will execute in the app.
6+
7+
::alert{type=danger}
8+
Never run code from untrusted sources
9+
::
10+
11+
## Creating & Running script
12+
13+
Go to entries page and click in the plus button to create a new file.
14+
15+
Select the file and press **F2** to rename him to **my-script.ts**.
16+
17+
::alert{type=info}
18+
Any file in the workspace with the extname *.ts* or *.js* will be treated as a script by the app!
19+
::
20+
21+
![entires-script-file](/entires.script-file.jpeg)
22+
23+
Open the file to access the editor and we can already write our script.
24+
25+
To execute the script just press the play button on top right of the page.
26+
27+
![script-editor](/scripts.editor.jpeg)
28+
29+
30+
31+
Scrips have some **global variables** like ```Workspace``` and ```Facades``` to help you make the operations needed in the workspace.
32+
33+
Check the [script global scope](/scripts/global-scope) to know more about
34+
35+
## Item examples
36+
37+
::code-group
38+
39+
```ts [List]
40+
// list items of a collection
41+
42+
const repository = await Facades.item
43+
.createRepositoryFromWorkspace(Workspace, 'todos')
44+
45+
const items = await repository.list()
46+
47+
setResult(items)
48+
49+
```
50+
51+
```ts [Create]
52+
const repository = await Facades.item
53+
.createRepositoryFromWorkspace(Workspace, 'todos')
54+
55+
const item = await repository.create({
56+
name: 'New todo'
57+
})
58+
59+
setResult(item)
60+
```
61+
62+
```ts [Create multiple]
63+
const repository = await Facades.item
64+
.createRepositoryFromWorkspace(Workspace, 'todos')
65+
66+
const items = []
67+
68+
for await (const i of Array.from(Array(10).keys())) {
69+
items.push(await repository.create({
70+
name: 'New todo ' + i
71+
}))
72+
}
73+
74+
setResult(items)
75+
```
76+
77+
```ts [Update]
78+
// update items of a collection
79+
80+
const repository = await Facades.item
81+
.createRepositoryFromWorkspace(Workspace, 'todos')
82+
83+
const items = await repository.list()
84+
85+
for await (const item of items) {
86+
87+
await repository.update(item.id, {
88+
done: !item.done
89+
})
90+
91+
console.log('updated: ', item.name)
92+
}
93+
94+
```
95+
96+
::
97+
98+
99+
## Drive examples
100+
101+
::code-group
102+
103+
```ts [List]
104+
105+
// read a file/folders in root workspace
106+
107+
const entries = await Drive.list('/')
108+
109+
setResult(entries)
110+
```
111+
112+
```ts [Read]
113+
// read a file in root workspace
114+
115+
const bytes = await Drive.read('hello.md')
116+
117+
const text = bytes ? Entry.decode(bytes) : ''
118+
119+
setResult(text)
120+
```
121+
122+
```ts [Write]
123+
// write a file in root workspace
124+
125+
await Drive.write('hello.md', Entry.encode('# Hello word'))
126+
127+
setResult("Done")
128+
```
129+
::
130+
7.71 KB
Loading
13.2 KB
Loading

packages/examples/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
gtd/tasks/.is/metas.json
2-
gtd/projects/.is/metas.json
2+
gtd/projects/.is/metas.json
3+
4+
scripts/collections/todos/.is/metas.json
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[
2+
{
3+
"columns": [
4+
{
5+
"label": "Done",
6+
"id": "47dcb1b3-4895-4642-a9f9-c23b0d9b3331",
7+
"field": "done",
8+
"type": "checkbox",
9+
"options": []
10+
},
11+
{
12+
"label": "Name",
13+
"id": "11ce8ee8-52b6-4dcb-9c49-d5c56a9dbe92",
14+
"field": "name",
15+
"type": "text",
16+
"options": []
17+
}
18+
],
19+
"id": "todos",
20+
"crudName": "fsFolder",
21+
"name": "Todos",
22+
"path": "/collections/todos"
23+
}
24+
]
Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
[
22
{
3-
"label": "execute.ts",
4-
"to": "/entries/execute.ts",
5-
"children": [],
6-
"id": "0c4b4eb8-c14f-4272-bd9a-9cce18dd9f79",
7-
"icon": "fas fa-code"
3+
"id": "6421.837380922849",
4+
"label": "Collections",
5+
"icon": "code",
6+
"isSection": true,
7+
"children": []
88
},
99
{
10-
"id": "5930.060426198447",
11-
"label": "Operations",
10+
"id": "289.65503229275765",
11+
"label": "Scripts",
1212
"icon": "code",
1313
"isSection": true,
14-
"children": [
15-
{
16-
"label": "subtraction.ts",
17-
"to": "/entries/subtraction.ts",
18-
"children": [],
19-
"id": "1d3cb4d7-f861-4f2c-8766-530f7621f811",
20-
"icon": "fas fa-code"
21-
},
22-
{
23-
"label": "list-operations.ts",
24-
"to": "/entries///list-operations.ts",
25-
"children": [],
26-
"id": "48fd5d64-cff1-40f8-ba90-6f5db40452e3",
27-
"icon": "fas fa-code"
28-
},
29-
{
30-
"label": "sum.ts",
31-
"to": "/entries/sum.ts",
32-
"children": [],
33-
"id": "0a92dd20-140f-4f28-98e1-e49f4ff6c312",
34-
"icon": "fas fa-code"
35-
}
36-
]
14+
"children": []
15+
},
16+
{
17+
"label": "create-item.ts",
18+
"to": "/entries/items/create-item.ts",
19+
"children": [],
20+
"id": "04372ef9-90e8-46f4-ae6e-1913436d2d16",
21+
"icon": "bookmark"
22+
},
23+
{
24+
"label": "Todos",
25+
"to": "/collections/todos/items",
26+
"children": [],
27+
"id": "4ef035fd-89a1-4509-aa55-2b575b25a141",
28+
"icon": "bookmark"
29+
},
30+
{
31+
"label": "create-multiple.ts",
32+
"to": "/entries/items/create-multiple.ts",
33+
"children": [],
34+
"id": "e6262730-0af1-42b8-abcc-fb0f05efdccb",
35+
"icon": "bookmark"
3736
}
3837
]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[
2+
{
3+
"component": "group",
4+
"id": "default",
5+
"label": "",
6+
"viewIds": [
7+
"9753fc8e-fac2-4995-92a9-b47d77bb7e4e"
8+
]
9+
},
10+
{
11+
"component": "table",
12+
"id": "9753fc8e-fac2-4995-92a9-b47d77bb7e4e",
13+
"label": "Table",
14+
"search": "",
15+
"filters": [],
16+
"columns": [
17+
{
18+
"id": "_actions_left",
19+
"label": "#",
20+
"width": 43
21+
},
22+
{
23+
"label": "Done",
24+
"id": "47dcb1b3-4895-4642-a9f9-c23b0d9b3331",
25+
"options": [],
26+
"width": 92
27+
},
28+
{
29+
"label": "Name",
30+
"id": "11ce8ee8-52b6-4dcb-9c49-d5c56a9dbe92",
31+
"options": []
32+
},
33+
{
34+
"id": "_actions_right",
35+
"width": "100%"
36+
}
37+
],
38+
"orderBy": []
39+
}
40+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* This scrit list all files in root of workspace
3+
*/
4+
5+
const entries = await Drive.list('/')
6+
7+
8+
setResult(entries)
9+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// read a file in root worksapce
2+
3+
const bytes = await Drive.read('hello.md')
4+
5+
const text = bytes ? Entry.decode(bytes) : ''
6+
7+
setResult(text)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// write a file in root worksapce
2+
3+
await Drive.write('hello.md', Entry.encode('# Hello word'))
4+
5+
setResult("Done")

packages/examples/scripts/execute.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/examples/scripts/hello.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Hello word
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* This scrit create an item in the todos collection
3+
*/
4+
5+
const repository = await Facades.item.createRepositoryFromWorkspace(Workspace, 'todos')
6+
7+
8+
const item = await repository.create({
9+
name: 'New todo'
10+
})
11+
12+
13+
setResult(item)
14+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* This scrit create multiple an item in the todos collection
3+
*/
4+
5+
const repository = await Facades.item.createRepositoryFromWorkspace(Workspace, 'todos')
6+
7+
const items = []
8+
9+
for await (const i of Array.from(Array(10).keys())) {
10+
// items.push(await repository.create({
11+
// name: 'New todo ' + i
12+
// }))
13+
}
14+
15+
16+
setResult(items)
17+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// list items of a collection
2+
3+
const repository = await Facades.item.createRepositoryFromWorkspace(Workspace, 'todos')
4+
5+
const items = await repository.list()
6+
7+
setResult(items)
8+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// update items of a collection
2+
3+
const repository = await Facades.item.createRepositoryFromWorkspace(Workspace, 'todos')
4+
5+
const items = await repository.list()
6+
7+
for await (const item of items) {
8+
9+
await repository.update(item.id, {
10+
done: !item.done
11+
})
12+
13+
console.log('updated: ', item.name)
14+
}
15+

packages/examples/scripts/list-operations.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)