@perspective-dev/client 5.6.1 → 5.8.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.
Files changed (142) hide show
  1. package/README.md +5 -1
  2. package/dist/cdn/perspective.js +3 -2
  3. package/dist/cdn/perspective.js.map +4 -4
  4. package/dist/esm/perspective.browser.d.ts +71 -17
  5. package/dist/esm/perspective.js +2 -2
  6. package/dist/esm/perspective.js.map +4 -4
  7. package/dist/esm/perspective.node.d.ts +20 -3
  8. package/dist/esm/perspective.node.js +734 -39
  9. package/dist/esm/perspective.node.js.map +4 -4
  10. package/dist/esm/public_types.d.ts +31 -0
  11. package/dist/esm/virtual_servers/clickhouse.d.ts +41 -0
  12. package/dist/esm/virtual_servers/clickhouse.js +6 -1
  13. package/dist/esm/virtual_servers/clickhouse.js.map +3 -3
  14. package/dist/esm/virtual_servers/duckdb.d.ts +19 -9
  15. package/dist/esm/virtual_servers/duckdb.js +1 -1
  16. package/dist/esm/virtual_servers/duckdb.js.map +3 -3
  17. package/dist/esm/wasm/cdn.d.ts +12 -0
  18. package/dist/esm/wasm/migrate.d.ts +40 -0
  19. package/dist/esm/wasm/registry.d.ts +123 -0
  20. package/dist/esm/wasm/registry.js +3 -0
  21. package/dist/esm/wasm/registry.js.map +7 -0
  22. package/{src/ts/ts-rs/AggSpec.ts → dist/ts-rs/AggSpec.d.ts} +1 -1
  23. package/{src/ts/ts-rs/Features.ts → dist/ts-rs/Features.d.ts} +16 -6
  24. package/{src/ts/ts-rs/Filter.ts → dist/ts-rs/Filter.d.ts} +1 -1
  25. package/{src/ts/ts-rs/FilterTerm.ts → dist/ts-rs/FilterTerm.d.ts} +1 -1
  26. package/{src/ts/ts-rs/JoinOptions.ts → dist/ts-rs/JoinOptions.d.ts} +1 -1
  27. package/dist/ts-rs/ListFlatten.d.ts +3 -0
  28. package/dist/ts-rs/MapFlatten.d.ts +3 -0
  29. package/{src/ts/ts-rs/OnRemoveData.ts → dist/ts-rs/OnRemoveData.d.ts} +1 -1
  30. package/{src/ts/ts-rs/OnUpdateData.ts → dist/ts-rs/OnUpdateData.d.ts} +1 -1
  31. package/{src/ts/ts-rs/OnUpdateOptions.ts → dist/ts-rs/OnUpdateOptions.d.ts} +1 -1
  32. package/{src/ts/ts-rs/Sort.ts → dist/ts-rs/Sort.d.ts} +1 -1
  33. package/dist/ts-rs/StructFlatten.d.ts +3 -0
  34. package/dist/ts-rs/TableIndex.d.ts +9 -0
  35. package/dist/ts-rs/TableInitOptions.d.ts +65 -0
  36. package/{src/ts/ts-rs/UpdateOptions.ts → dist/ts-rs/UpdateOptions.d.ts} +1 -1
  37. package/{src/ts/ts-rs/ViewConfig.ts → dist/ts-rs/ViewConfig.d.ts} +8 -8
  38. package/{src/ts/ts-rs/ViewConfigUpdate.ts → dist/ts-rs/ViewConfigUpdate.d.ts} +8 -8
  39. package/{src/ts/ts-rs/ViewOnUpdateResp.ts → dist/ts-rs/ViewOnUpdateResp.d.ts} +1 -1
  40. package/{src/ts/ts-rs/WindowAggSpec.ts → dist/ts-rs/WindowAggSpec.d.ts} +1 -1
  41. package/{src/ts/ts-rs/WindowSort.ts → dist/ts-rs/WindowSort.d.ts} +1 -1
  42. package/{src/ts/ts-rs/WindowSpec.ts → dist/ts-rs/WindowSpec.d.ts} +1 -1
  43. package/{src/ts/ts-rs/Windows.ts → dist/ts-rs/Windows.d.ts} +1 -1
  44. package/dist/wasm/perspective-js.d.ts +136 -58
  45. package/dist/wasm/perspective-js.js +190 -24
  46. package/dist/wasm/perspective-js.wasm +0 -0
  47. package/dist/wasm/perspective-js.wasm.d.ts +14 -8
  48. package/dist/wasm/perspective-migrate.d.ts +41 -0
  49. package/dist/wasm/perspective-migrate.js +524 -0
  50. package/dist/wasm/perspective-migrate.wasm +0 -0
  51. package/dist/wasm/perspective-migrate.wasm.d.ts +8 -0
  52. package/package.json +30 -15
  53. package/dist/esm/ts-rs/AggSpec.d.ts +0 -8
  54. package/dist/esm/ts-rs/Aggregate.d.ts +0 -1
  55. package/dist/esm/ts-rs/ColumnType.d.ts +0 -4
  56. package/dist/esm/ts-rs/ColumnWindow.d.ts +0 -4
  57. package/dist/esm/ts-rs/DeleteOptions.d.ts +0 -6
  58. package/dist/esm/ts-rs/Expressions.d.ts +0 -3
  59. package/dist/esm/ts-rs/Features.d.ts +0 -71
  60. package/dist/esm/ts-rs/Filter.d.ts +0 -2
  61. package/dist/esm/ts-rs/FilterReducer.d.ts +0 -1
  62. package/dist/esm/ts-rs/FilterTerm.d.ts +0 -2
  63. package/dist/esm/ts-rs/GroupRollupMode.d.ts +0 -1
  64. package/dist/esm/ts-rs/JoinOptions.d.ts +0 -9
  65. package/dist/esm/ts-rs/JoinType.d.ts +0 -1
  66. package/dist/esm/ts-rs/ListFlatten.d.ts +0 -1
  67. package/dist/esm/ts-rs/OnRemoveData.d.ts +0 -6
  68. package/dist/esm/ts-rs/OnUpdateData.d.ts +0 -6
  69. package/dist/esm/ts-rs/OnUpdateMode.d.ts +0 -9
  70. package/dist/esm/ts-rs/OnUpdateOptions.d.ts +0 -7
  71. package/dist/esm/ts-rs/Scalar.d.ts +0 -5
  72. package/dist/esm/ts-rs/Sort.d.ts +0 -2
  73. package/dist/esm/ts-rs/SortDir.d.ts +0 -1
  74. package/dist/esm/ts-rs/SplitRollupMode.d.ts +0 -9
  75. package/dist/esm/ts-rs/SystemInfo.d.ts +0 -40
  76. package/dist/esm/ts-rs/TableInitOptions.d.ts +0 -45
  77. package/dist/esm/ts-rs/TableReadFormat.d.ts +0 -7
  78. package/dist/esm/ts-rs/TypedArrayWindow.d.ts +0 -33
  79. package/dist/esm/ts-rs/UpdateOptions.d.ts +0 -8
  80. package/dist/esm/ts-rs/ViewConfig.d.ts +0 -24
  81. package/dist/esm/ts-rs/ViewConfigUpdate.d.ts +0 -116
  82. package/dist/esm/ts-rs/ViewOnRemoveResp.d.ts +0 -4
  83. package/dist/esm/ts-rs/ViewOnUpdateResp.d.ts +0 -4
  84. package/dist/esm/ts-rs/ViewWindow.d.ts +0 -30
  85. package/dist/esm/ts-rs/WindowAggSpec.d.ts +0 -25
  86. package/dist/esm/ts-rs/WindowSort.d.ts +0 -7
  87. package/dist/esm/ts-rs/WindowSortDir.d.ts +0 -6
  88. package/dist/esm/ts-rs/WindowSpec.d.ts +0 -47
  89. package/dist/esm/ts-rs/Windows.d.ts +0 -10
  90. package/src/rust/client.rs +0 -590
  91. package/src/rust/generic_sql_model.rs +0 -276
  92. package/src/rust/lib.rs +0 -99
  93. package/src/rust/table.rs +0 -397
  94. package/src/rust/table_data.rs +0 -164
  95. package/src/rust/typed_array.rs +0 -386
  96. package/src/rust/utils/browser.rs +0 -35
  97. package/src/rust/utils/console_logger.rs +0 -237
  98. package/src/rust/utils/errors.rs +0 -274
  99. package/src/rust/utils/futures.rs +0 -274
  100. package/src/rust/utils/json.rs +0 -252
  101. package/src/rust/utils/local_poll_loop.rs +0 -63
  102. package/src/rust/utils/mod.rs +0 -32
  103. package/src/rust/utils/serde.rs +0 -46
  104. package/src/rust/utils/trace_allocator.rs +0 -98
  105. package/src/rust/view.rs +0 -463
  106. package/src/rust/virtual_server.rs +0 -991
  107. package/src/ts/perspective-server.worker.ts +0 -74
  108. package/src/ts/perspective.browser.ts +0 -380
  109. package/src/ts/perspective.cdn.ts +0 -28
  110. package/src/ts/perspective.node.ts +0 -556
  111. package/src/ts/ts-rs/ListFlatten.ts +0 -3
  112. package/src/ts/ts-rs/TableInitOptions.ts +0 -42
  113. package/src/ts/virtual_server.ts +0 -83
  114. package/src/ts/virtual_servers/clickhouse.ts +0 -533
  115. package/src/ts/virtual_servers/duckdb.ts +0 -549
  116. package/src/ts/wasm/browser.ts +0 -134
  117. package/src/ts/wasm/cdn.ts +0 -32
  118. package/src/ts/wasm/decompress.ts +0 -83
  119. package/src/ts/wasm/emscripten_api.ts +0 -91
  120. package/src/ts/wasm/engine.ts +0 -309
  121. package/src/ts/wasm/features.ts +0 -31
  122. package/src/ts/wasm/perspective-server.poly.ts +0 -460
  123. package/src/ts/websocket.ts +0 -95
  124. package/tsconfig.json +0 -22
  125. /package/{src/ts/ts-rs/Aggregate.ts → dist/ts-rs/Aggregate.d.ts} +0 -0
  126. /package/{src/ts/ts-rs/ColumnType.ts → dist/ts-rs/ColumnType.d.ts} +0 -0
  127. /package/{src/ts/ts-rs/ColumnWindow.ts → dist/ts-rs/ColumnWindow.d.ts} +0 -0
  128. /package/{src/ts/ts-rs/DeleteOptions.ts → dist/ts-rs/DeleteOptions.d.ts} +0 -0
  129. /package/{src/ts/ts-rs/Expressions.ts → dist/ts-rs/Expressions.d.ts} +0 -0
  130. /package/{src/ts/ts-rs/FilterReducer.ts → dist/ts-rs/FilterReducer.d.ts} +0 -0
  131. /package/{src/ts/ts-rs/GroupRollupMode.ts → dist/ts-rs/GroupRollupMode.d.ts} +0 -0
  132. /package/{src/ts/ts-rs/JoinType.ts → dist/ts-rs/JoinType.d.ts} +0 -0
  133. /package/{src/ts/ts-rs/OnUpdateMode.ts → dist/ts-rs/OnUpdateMode.d.ts} +0 -0
  134. /package/{src/ts/ts-rs/Scalar.ts → dist/ts-rs/Scalar.d.ts} +0 -0
  135. /package/{src/ts/ts-rs/SortDir.ts → dist/ts-rs/SortDir.d.ts} +0 -0
  136. /package/{src/ts/ts-rs/SplitRollupMode.ts → dist/ts-rs/SplitRollupMode.d.ts} +0 -0
  137. /package/{src/ts/ts-rs/SystemInfo.ts → dist/ts-rs/SystemInfo.d.ts} +0 -0
  138. /package/{src/ts/ts-rs/TableReadFormat.ts → dist/ts-rs/TableReadFormat.d.ts} +0 -0
  139. /package/{src/ts/ts-rs/TypedArrayWindow.ts → dist/ts-rs/TypedArrayWindow.d.ts} +0 -0
  140. /package/{src/ts/ts-rs/ViewOnRemoveResp.ts → dist/ts-rs/ViewOnRemoveResp.d.ts} +0 -0
  141. /package/{src/ts/ts-rs/ViewWindow.ts → dist/ts-rs/ViewWindow.d.ts} +0 -0
  142. /package/{src/ts/ts-rs/WindowSortDir.ts → dist/ts-rs/WindowSortDir.d.ts} +0 -0
package/src/rust/table.rs DELETED
@@ -1,397 +0,0 @@
1
- // ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2
- // ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3
- // ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4
- // ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5
- // ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6
- // ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7
- // ┃ Copyright (c) 2017, the Perspective Authors. ┃
8
- // ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9
- // ┃ This file is part of the Perspective library, distributed under the terms ┃
10
- // ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11
- // ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
-
13
- use js_sys::Function;
14
- use perspective_client::config::*;
15
- use perspective_client::{
16
- DeleteOptions, DescribeVerdict, UpdateData, UpdateOptions, assert_table_api,
17
- };
18
- use wasm_bindgen::prelude::*;
19
- use wasm_bindgen_derive::TryFromJsValue;
20
- use wasm_bindgen_futures::spawn_local;
21
-
22
- use crate::Client;
23
- use crate::table_data::UpdateDataExt;
24
- use crate::utils::{ApiFuture, ApiResult, JsValueSerdeExt, LocalPollLoop};
25
- pub use crate::view::*;
26
-
27
- #[derive(TryFromJsValue, Clone, PartialEq)]
28
- #[wasm_bindgen]
29
- pub struct Table(pub(crate) perspective_client::Table);
30
-
31
- assert_table_api!(Table);
32
-
33
- impl From<perspective_client::Table> for Table {
34
- fn from(value: perspective_client::Table) -> Self {
35
- Table(value)
36
- }
37
- }
38
-
39
- /// [`Table`] is Perspective's columnar data frame, analogous to a Pandas/Polars
40
- /// `DataFrame` or Apache Arrow, supporting append & in-place updates, removal
41
- /// by index, and update notifications.
42
- ///
43
- /// A [`Table`] contains columns, each of which have a unique name, are strongly
44
- /// and consistently typed, and contains rows of data conforming to the column's
45
- /// type. Each column in a [`Table`] must have the same number of rows, though
46
- /// not every row must contain data; null-values are used to indicate missing
47
- /// values in the dataset. The schema of a [`Table`] is _immutable after
48
- /// creation_, which means the column names and data types cannot be changed
49
- /// after the [`Table`] has been created. Columns cannot be added or deleted
50
- /// after creation either, but a [`View`] can be used to select an arbitrary set
51
- /// of columns from the [`Table`].
52
- impl Table {
53
- pub fn get_table(&self) -> &'_ perspective_client::Table {
54
- &self.0
55
- }
56
- }
57
-
58
- #[wasm_bindgen]
59
- extern "C" {
60
- // TODO Fix me
61
- #[wasm_bindgen(typescript_type = "\
62
- string | ArrayBuffer | Record<string, unknown[]> | Record<string, unknown>[] | \
63
- Record<string, ColumnType>")]
64
- pub type JsTableInitData;
65
-
66
- #[wasm_bindgen(typescript_type = "ViewConfigUpdate")]
67
- pub type JsViewConfig;
68
-
69
- #[wasm_bindgen(typescript_type = "UpdateOptions")]
70
- pub type JsUpdateOptions;
71
-
72
- #[wasm_bindgen(typescript_type = "DeleteOptions")]
73
- pub type JsDeleteOptions;
74
- }
75
-
76
- #[wasm_bindgen]
77
- impl Table {
78
- /// Returns the name of the index column for the table.
79
- ///
80
- /// # JavaScript Examples
81
- ///
82
- /// ```javascript
83
- /// const table = await client.table("x,y\n1,2\n3,4", { index: "x" });
84
- /// const index = table.get_index(); // "x"
85
- /// ```
86
- #[wasm_bindgen]
87
- pub async fn get_index(&self) -> Option<String> {
88
- self.0.get_index()
89
- }
90
-
91
- /// Get a copy of the [`Client`] this [`Table`] came from.
92
- #[wasm_bindgen]
93
- pub async fn get_client(&self) -> Client {
94
- Client {
95
- close: None,
96
- client: self.0.get_client(),
97
- }
98
- }
99
-
100
- /// Returns the user-specified name for this table, or the auto-generated
101
- /// name if a name was not specified when the table was created.
102
- #[wasm_bindgen]
103
- pub async fn get_name(&self) -> String {
104
- self.0.get_name().to_owned()
105
- }
106
-
107
- /// Returns the user-specified row limit for this table.
108
- #[wasm_bindgen]
109
- pub async fn get_limit(&self) -> Option<u32> {
110
- self.0.get_limit()
111
- }
112
-
113
- /// Removes all the rows in the [`Table`], but preserves everything else
114
- /// including the schema, index, and any callbacks or registered
115
- /// [`View`] instances.
116
- ///
117
- /// Calling [`Table::clear`], like [`Table::update`] and [`Table::remove`],
118
- /// will trigger an update event to any registered listeners via
119
- /// [`View::on_update`].
120
- #[wasm_bindgen]
121
- pub async fn clear(&self) -> ApiResult<()> {
122
- self.0.clear().await?;
123
- Ok(())
124
- }
125
-
126
- /// Delete this [`Table`] and cleans up associated resources.
127
- ///
128
- /// [`Table`]s do not stop consuming resources or processing updates when
129
- /// they are garbage collected in their host language - you must call
130
- /// this method to reclaim these.
131
- ///
132
- /// # Arguments
133
- ///
134
- /// - `options` An options dictionary.
135
- /// - `lazy` Whether to delete this [`Table`] _lazily_. When false (the
136
- /// default), the delete will occur immediately, assuming it has no
137
- /// [`View`] instances registered to it (which must be deleted first,
138
- /// otherwise this method will throw an error). When true, the
139
- /// [`Table`] will only be marked for deltion once its [`View`]
140
- /// dependency count reaches 0.
141
- ///
142
- /// # JavaScript Examples
143
- ///
144
- /// ```javascript
145
- /// const table = await client.table("x,y\n1,2\n3,4");
146
- ///
147
- /// // ...
148
- ///
149
- /// await table.delete({ lazy: true });
150
- /// ```
151
- #[wasm_bindgen]
152
- pub async fn delete(self, options: Option<JsDeleteOptions>) -> ApiResult<()> {
153
- let options = options
154
- .into_serde_ext::<Option<DeleteOptions>>()?
155
- .unwrap_or_default();
156
-
157
- self.0.delete(options).await?;
158
- Ok(())
159
- }
160
-
161
- /// Returns the number of rows in a [`Table`].
162
- #[wasm_bindgen]
163
- pub async fn size(&self) -> ApiResult<f64> {
164
- Ok(self.0.size().await? as f64)
165
- }
166
-
167
- /// Returns a table's [`Schema`], a mapping of column names to column types.
168
- ///
169
- /// The mapping of a [`Table`]'s column names to data types is referred to
170
- /// as a [`Schema`]. Each column has a unique name and a data type, one
171
- /// of:
172
- ///
173
- /// - `"boolean"` - A boolean type
174
- /// - `"date"` - A timesonze-agnostic date type (month/day/year)
175
- /// - `"datetime"` - A millisecond-precision datetime type in the UTC
176
- /// timezone
177
- /// - `"float"` - A 64 bit float
178
- /// - `"integer"` - A signed 32 bit integer (the integer type supported by
179
- /// JavaScript)
180
- /// - `"string"` - A [`String`] data type (encoded internally as a
181
- /// _dictionary_)
182
- ///
183
- /// Note that all [`Table`] columns are _nullable_, regardless of the data
184
- /// type.
185
- #[wasm_bindgen(unchecked_return_type = "Record<string, ColumnType>")]
186
- pub async fn schema(&self) -> ApiResult<JsValue> {
187
- let schema = self.0.schema().await?;
188
- Ok(JsValue::from_serde_ext(&schema)?)
189
- }
190
-
191
- /// Returns the column names of this [`Table`] in "natural" order (the
192
- /// ordering implied by the input format).
193
- ///
194
- /// # JavaScript Examples
195
- ///
196
- /// ```javascript
197
- /// const columns = await table.columns();
198
- /// ```
199
- #[wasm_bindgen]
200
- pub async fn columns(&self) -> ApiResult<JsValue> {
201
- let columns = self.0.columns().await?;
202
- Ok(JsValue::from_serde_ext(&columns)?)
203
- }
204
-
205
- /// Create a unique channel ID on this [`Table`], which allows
206
- /// `View::on_update` callback calls to be associated with the
207
- /// `Table::update` which caused them.
208
- #[wasm_bindgen]
209
- pub async fn make_port(&self) -> ApiResult<i32> {
210
- Ok(self.0.make_port().await?)
211
- }
212
-
213
- /// Register a callback which is called exactly once, when this [`Table`] is
214
- /// deleted with the [`Table::delete`] method.
215
- ///
216
- /// [`Table::on_delete`] resolves when the subscription message is sent, not
217
- /// when the _delete_ event occurs.
218
- #[wasm_bindgen]
219
- pub fn on_delete(&self, on_delete: Function) -> ApiFuture<u32> {
220
- let table = self.clone();
221
- ApiFuture::new(async move {
222
- let emit = LocalPollLoop::new(move |()| on_delete.call0(&JsValue::UNDEFINED));
223
- let on_delete = Box::new(move || spawn_local(emit.poll(())));
224
- Ok(table.0.on_delete(on_delete).await?)
225
- })
226
- }
227
-
228
- /// Removes a listener with a given ID, as returned by a previous call to
229
- /// [`Table::on_delete`].
230
- #[wasm_bindgen]
231
- pub fn remove_delete(&self, callback_id: u32) -> ApiFuture<()> {
232
- let client = self.0.clone();
233
- ApiFuture::new(async move {
234
- client.remove_delete(callback_id).await?;
235
- Ok(())
236
- })
237
- }
238
-
239
- /// Removes rows from this [`Table`] with the `index` column values
240
- /// supplied.
241
- ///
242
- /// # Arguments
243
- ///
244
- /// - `indices` - A list of `index` column values for rows that should be
245
- /// removed.
246
- ///
247
- /// # JavaScript Examples
248
- ///
249
- /// ```javascript
250
- /// await table.remove([1, 2, 3]);
251
- /// ```
252
- #[wasm_bindgen]
253
- pub async fn remove(&self, value: &JsValue, options: Option<JsUpdateOptions>) -> ApiResult<()> {
254
- let options = options
255
- .into_serde_ext::<Option<UpdateOptions>>()?
256
- .unwrap_or_default();
257
-
258
- let input = UpdateData::from_js_value(value, options.format)?;
259
- self.0.remove(input).await?;
260
- Ok(())
261
- }
262
-
263
- /// Replace all rows in this [`Table`] with the input data, coerced to this
264
- /// [`Table`]'s existing [`perspective_client::Schema`], notifying any
265
- /// derived [`View`] and [`View::on_update`] callbacks.
266
- ///
267
- /// Calling [`Table::replace`] is an easy way to replace _all_ the data in a
268
- /// [`Table`] without losing any derived [`View`] instances or
269
- /// [`View::on_update`] callbacks. [`Table::replace`] does _not_ infer
270
- /// data types like [`Client::table`] does, rather it _coerces_ input
271
- /// data to the `Schema` like [`Table::update`]. If you need a [`Table`]
272
- /// with a different `Schema`, you must create a new one.
273
- ///
274
- /// # JavaScript Examples
275
- ///
276
- /// ```javascript
277
- /// await table.replace("x,y\n1,2");
278
- /// ```
279
- #[wasm_bindgen]
280
- pub async fn replace(
281
- &self,
282
- input: &JsValue,
283
- options: Option<JsUpdateOptions>,
284
- ) -> ApiResult<()> {
285
- let options = options
286
- .into_serde_ext::<Option<UpdateOptions>>()?
287
- .unwrap_or_default();
288
-
289
- let input = UpdateData::from_js_value(input, options.format)?;
290
- self.0.replace(input).await?;
291
- Ok(())
292
- }
293
-
294
- /// Updates the rows of this table and any derived [`View`] instances.
295
- ///
296
- /// Calling [`Table::update`] will trigger the [`View::on_update`] callbacks
297
- /// register to derived [`View`], and the call itself will not resolve until
298
- /// _all_ derived [`View`]'s are notified.
299
- ///
300
- /// When updating a [`Table`] with an `index`, [`Table::update`] supports
301
- /// partial updates, by omitting columns from the update data.
302
- ///
303
- /// # Arguments
304
- ///
305
- /// - `input` - The input data for this [`Table`]. The schema of a [`Table`]
306
- /// is immutable after creation, so this method cannot be called with a
307
- /// schema.
308
- /// - `options` - Options for this update step - see [`UpdateOptions`].
309
- ///
310
- /// # JavaScript Examples
311
- ///
312
- /// ```javascript
313
- /// await table.update("x,y\n1,2");
314
- /// ```
315
- #[wasm_bindgen]
316
- pub fn update(
317
- &self,
318
- input: JsTableInitData,
319
- options: Option<JsUpdateOptions>,
320
- ) -> ApiFuture<()> {
321
- let table = self.clone();
322
- ApiFuture::new(async move {
323
- let options = options
324
- .into_serde_ext::<Option<UpdateOptions>>()?
325
- .unwrap_or_default();
326
-
327
- let input = UpdateData::from_js_value(&input, options.format)?;
328
- Ok(table.0.update(input, options).await?)
329
- })
330
- }
331
-
332
- /// Create a new [`View`] from this table with a specified
333
- /// [`ViewConfigUpdate`].
334
- ///
335
- /// See [`View`] struct.
336
- ///
337
- /// # JavaScript Examples
338
- ///
339
- /// ```javascript
340
- /// const view = await table.view({
341
- /// columns: ["Sales"],
342
- /// aggregates: { Sales: "sum" },
343
- /// group_by: ["Region", "Country"],
344
- /// filter: [["Category", "in", ["Furniture", "Technology"]]],
345
- /// });
346
- /// ```
347
- #[wasm_bindgen]
348
- pub async fn view(&self, config: Option<JsViewConfig>) -> ApiResult<View> {
349
- let config = config
350
- .map(|config| js_sys::JSON::stringify(&config))
351
- .transpose()?
352
- .and_then(|x| x.as_string())
353
- .map(|x| serde_json::from_str(x.as_str()))
354
- .transpose()?;
355
-
356
- let view = self.0.view(config).await?;
357
- Ok(View(view))
358
- }
359
-
360
- /// Validate a complete view config against this table and report the
361
- /// schema a [`View`] built from it would have, WITHOUT creating one.
362
- ///
363
- /// # JavaScript Examples
364
- ///
365
- /// ```javascript
366
- /// const verdict = await table.describe({
367
- /// columns: ["Sales", "margin"],
368
- /// group_by: ["Region"],
369
- /// expressions: { margin: '"Profit" / "Sales"' },
370
- /// });
371
- ///
372
- /// if ("view_schema" in verdict) {
373
- /// const view = await table.view(config);
374
- /// }
375
- /// ```
376
- #[wasm_bindgen]
377
- pub async fn describe(&self, config: Option<JsViewConfig>) -> ApiResult<JsValue> {
378
- let config = config
379
- .map(|config| js_sys::JSON::stringify(&config))
380
- .transpose()?
381
- .and_then(|x| x.as_string())
382
- .map(|x| serde_json::from_str(x.as_str()))
383
- .transpose()?
384
- .unwrap_or_default();
385
-
386
- let verdict: DescribeVerdict = self.0.describe(config).await?.into();
387
- Ok(JsValue::from_serde_ext(&verdict)?)
388
- }
389
-
390
- /// Validates the given expressions.
391
- #[wasm_bindgen]
392
- pub async fn validate_expressions(&self, exprs: &JsValue) -> ApiResult<JsValue> {
393
- let exprs = JsValue::into_serde_ext::<Expressions>(exprs.clone())?;
394
- let columns = self.0.validate_expressions(exprs).await?;
395
- Ok(JsValue::from_serde_ext(&columns)?)
396
- }
397
- }
@@ -1,164 +0,0 @@
1
- // ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2
- // ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3
- // ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4
- // ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5
- // ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6
- // ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7
- // ┃ Copyright (c) 2017, the Perspective Authors. ┃
8
- // ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9
- // ┃ This file is part of the Perspective library, distributed under the terms ┃
10
- // ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11
- // ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
-
13
- use extend::ext;
14
- use js_sys::{Array, ArrayBuffer, Function, JSON, Object, Reflect, Uint8Array};
15
- use perspective_client::config::ColumnType;
16
- use perspective_client::{TableData, TableReadFormat, UpdateData};
17
- use wasm_bindgen::convert::TryFromJsValue;
18
- use wasm_bindgen::intern;
19
- use wasm_bindgen::prelude::*;
20
-
21
- use crate::apierror;
22
- use crate::utils::{ApiError, ApiResult, JsValueSerdeExt};
23
- pub use crate::view::*;
24
-
25
- #[ext]
26
- impl Vec<(String, ColumnType)> {
27
- fn from_js_value(value: &JsValue) -> ApiResult<Vec<(String, ColumnType)>> {
28
- Ok(Object::keys(value.unchecked_ref())
29
- .iter()
30
- .map(|x| -> Result<_, JsValue> {
31
- let key = x.as_string().expect("Not string??");
32
- let val = Reflect::get(value, &x)?
33
- .as_string()
34
- .expect("Y no string?")
35
- .into_serde_ext()?;
36
-
37
- Ok((key, val))
38
- })
39
- .collect::<Result<Vec<_>, _>>()?)
40
- }
41
- }
42
-
43
- #[ext]
44
- pub(crate) impl TableData {
45
- fn from_js_value(value: &JsValue, format: Option<TableReadFormat>) -> ApiResult<TableData> {
46
- if let Some(result) = UpdateData::from_js_value_partial(value, format)? {
47
- Ok(result.into())
48
- } else if value.is_instance_of::<Object>()
49
- && Reflect::has(value, &intern("__get_model").into())?
50
- {
51
- let val = Reflect::get(value, &intern("__get_model").into())?
52
- .dyn_into::<Function>()?
53
- .call0(value)?;
54
-
55
- let view = View::try_from_js_value(val)?;
56
- Ok(TableData::View(view.0))
57
- } else if value.is_instance_of::<Object>() {
58
- let all_strings = || {
59
- Object::values(value.unchecked_ref())
60
- .to_vec()
61
- .iter()
62
- .all(|x| x.is_string())
63
- };
64
-
65
- let all_arrays = || {
66
- Object::values(value.unchecked_ref())
67
- .to_vec()
68
- .iter()
69
- .all(|x| x.is_instance_of::<Array>())
70
- };
71
-
72
- if all_strings() {
73
- Ok(TableData::Schema(Vec::from_js_value(value)?))
74
- } else if all_arrays() {
75
- let json = JSON::stringify(value)?
76
- .as_string()
77
- .expect("STRINGIFY_ARRAY??");
78
- Ok(UpdateData::JsonColumns(json).into())
79
- } else {
80
- Err(apierror!(TableError(value.clone())))
81
- }
82
- } else {
83
- Err(apierror!(TableError(value.clone())))
84
- }
85
- }
86
- }
87
-
88
- #[ext]
89
- pub(crate) impl UpdateData {
90
- fn from_js_value_partial(
91
- value: &JsValue,
92
- format: Option<TableReadFormat>,
93
- ) -> ApiResult<Option<UpdateData>> {
94
- if value.is_undefined() {
95
- Err(apierror!(TableError(value.clone())))
96
- } else if value.is_string() {
97
- match format {
98
- None | Some(TableReadFormat::Csv) => {
99
- Ok(Some(UpdateData::Csv(value.as_string().expect("Csv????"))))
100
- },
101
- Some(TableReadFormat::JsonString) => Ok(Some(UpdateData::JsonRows(
102
- value.as_string().expect("JSON???"),
103
- ))),
104
- Some(TableReadFormat::ColumnsString) => Ok(Some(UpdateData::JsonColumns(
105
- value.as_string().expect("ColumnString???"),
106
- ))),
107
- Some(TableReadFormat::Arrow) => Ok(Some(UpdateData::Arrow(
108
- value.as_string().expect("Arrow???").into_bytes().into(),
109
- ))),
110
- Some(TableReadFormat::Ndjson) => Ok(Some(UpdateData::Ndjson(
111
- value.as_string().expect("Ndjson???"),
112
- ))),
113
- }
114
- } else if value.is_instance_of::<ArrayBuffer>() {
115
- let uint8array = Uint8Array::new(value);
116
- let slice = uint8array.to_vec();
117
- match format {
118
- Some(TableReadFormat::Csv) => Ok(Some(UpdateData::Csv(String::from_utf8(slice)?))),
119
- Some(TableReadFormat::JsonString) => {
120
- Ok(Some(UpdateData::JsonRows(String::from_utf8(slice)?)))
121
- },
122
- Some(TableReadFormat::ColumnsString) => {
123
- Ok(Some(UpdateData::JsonColumns(String::from_utf8(slice)?)))
124
- },
125
- Some(TableReadFormat::Ndjson) => {
126
- Ok(Some(UpdateData::Ndjson(String::from_utf8(slice)?)))
127
- },
128
- None | Some(TableReadFormat::Arrow) => Ok(Some(UpdateData::Arrow(slice.into()))),
129
- }
130
- } else if let Some(uint8array) = value.dyn_ref::<Uint8Array>() {
131
- let slice = uint8array.to_vec();
132
- match format {
133
- Some(TableReadFormat::Csv) => Ok(Some(UpdateData::Csv(String::from_utf8(slice)?))),
134
- Some(TableReadFormat::JsonString) => {
135
- Ok(Some(UpdateData::JsonRows(String::from_utf8(slice)?)))
136
- },
137
- Some(TableReadFormat::ColumnsString) => {
138
- Ok(Some(UpdateData::JsonColumns(String::from_utf8(slice)?)))
139
- },
140
- Some(TableReadFormat::Ndjson) => {
141
- Ok(Some(UpdateData::Ndjson(String::from_utf8(slice)?)))
142
- },
143
- None | Some(TableReadFormat::Arrow) => Ok(Some(UpdateData::Arrow(slice.into()))),
144
- }
145
- } else if value.is_instance_of::<Array>() {
146
- let rows = JSON::stringify(value)?.as_string().expect("STRINGIFY??");
147
- Ok(Some(UpdateData::JsonRows(rows)))
148
- } else {
149
- Ok(None)
150
- }
151
- }
152
-
153
- fn from_js_value(value: &JsValue, format: Option<TableReadFormat>) -> ApiResult<UpdateData> {
154
- match TableData::from_js_value(value, format)? {
155
- TableData::Schema(_) => Err(ApiError::new(
156
- "Method cannot be called with `Schema` argument",
157
- )),
158
- TableData::Update(x) => Ok(x),
159
- TableData::View(_) => Err(ApiError::new(
160
- "Method cannot be called with `Schema` argument",
161
- )),
162
- }
163
- }
164
- }