@crombolo/data 0.48.1 → 0.50.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/data",
3
- "version": "0.48.1",
3
+ "version": "0.50.0",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. SQLite and Postgres storage for crombolo: schema, migrations and repositories.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -28,7 +28,7 @@
28
28
  "access": "public"
29
29
  },
30
30
  "dependencies": {
31
- "@crombolo/core": "0.48.1",
31
+ "@crombolo/core": "0.50.0",
32
32
  "kysely": "0.29.6"
33
33
  }
34
34
  }
@@ -77,6 +77,57 @@ export class FolderRepository {
77
77
  return 'deleted'
78
78
  }
79
79
 
80
+ /** Whether anything outside the recycle bin is still inside. */
81
+ async holdsAnything(key: string): Promise<boolean> {
82
+ const node = await this.#nodes.byKey(key)
83
+ if (!node) return false
84
+ const inside = await this.#nodes.childrenOf(node.id, [this.#objectType, this.#itemType], 0, 1)
85
+ return inside.total > 0
86
+ }
87
+
88
+ /** Moves a folder to `bin`: its branch is trashed, and what it holds stays unpublished. */
89
+ async toRecycleBin(key: string, bin: number): Promise<boolean> {
90
+ const node = await this.#nodes.byKey(key)
91
+ if (!node || node.objectType !== this.#objectType) return false
92
+ await this.#nodes.move(node.id, bin)
93
+ const moved = await this.#nodes.byKey(key)
94
+ const branch = `${moved?.path ?? node.path},%`
95
+ const db = this.#db
96
+ await db.q
97
+ .updateTable('node')
98
+ .set({ trashed: db.dialect.boolValue(true) as boolean })
99
+ .where((eb) => eb.or([eb('id', '=', node.id), eb('path', 'like', branch)]))
100
+ .execute()
101
+ await db.q
102
+ .updateTable('document')
103
+ .set({
104
+ published: db.dialect.boolValue(false) as boolean,
105
+ edited: db.dialect.boolValue(true) as boolean,
106
+ })
107
+ .where('node_id', 'in', (eb) =>
108
+ eb
109
+ .selectFrom('node')
110
+ .select('id')
111
+ .where((w) => w.or([w('id', '=', node.id), w('path', 'like', branch)])),
112
+ )
113
+ .execute()
114
+ return true
115
+ }
116
+
117
+ /** Takes a folder out of the recycle bin to `parentKey`, or the top level. */
118
+ async restore(key: string, parentKey: string | null): Promise<boolean> {
119
+ const node = await this.#nodes.byKey(key)
120
+ if (!node || node.objectType !== this.#objectType || !node.trashed) return false
121
+ await this.#db.q
122
+ .updateTable('node')
123
+ .set({ trashed: this.#db.dialect.boolValue(false) as boolean })
124
+ .where((eb) => eb.or([eb('id', '=', node.id), eb('path', 'like', `${node.path},%`)]))
125
+ .execute()
126
+ const parent = parentKey ? await this.#nodes.byKey(parentKey) : undefined
127
+ await this.#nodes.move(node.id, parent?.id ?? SystemNodes.Root)
128
+ return true
129
+ }
130
+
80
131
  /** Makes `A/B/C` exist, creating what is missing; returns the leaf's key, or null for an empty path. */
81
132
  async ensurePath(names: readonly string[]): Promise<string | null> {
82
133
  let parentId: number = SystemNodes.Root