@basictech/react 0.2.0-beta.8 → 0.2.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.
@@ -1,6 +1,6 @@
1
1
 
2
2
  
3
- > @basictech/react@0.2.0-beta.7 build
3
+ > @basictech/react@0.2.0-beta.11 build
4
4
  > tsup
5
5
 
6
6
  CLI Building entry: src/index.ts
@@ -11,13 +11,13 @@
11
11
  CLI Cleaning output folder
12
12
  CJS Build start
13
13
  ESM Build start
14
- CJS dist/index.js 22.22 KB
15
- CJS dist/index.js.map 45.33 KB
16
- CJS ⚡️ Build success in 14ms
17
- ESM dist/index.mjs 19.99 KB
18
- ESM dist/index.mjs.map 45.28 KB
19
- ESM ⚡️ Build success in 14ms
14
+ CJS dist/index.js 24.60 KB
15
+ CJS dist/index.js.map 51.73 KB
16
+ CJS ⚡️ Build success in 13ms
17
+ ESM dist/index.mjs 22.39 KB
18
+ ESM dist/index.mjs.map 51.69 KB
19
+ ESM ⚡️ Build success in 13ms
20
20
  DTS Build start
21
- DTS ⚡️ Build success in 934ms
22
- DTS dist/index.d.ts 1003.00 B
23
- DTS dist/index.d.mts 1003.00 B
21
+ DTS ⚡️ Build success in 1177ms
22
+ DTS dist/index.d.ts 1006.00 B
23
+ DTS dist/index.d.mts 1006.00 B
package/changelog.md CHANGED
@@ -1,5 +1,65 @@
1
1
  # 1.3.4
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 5890f4c: changed isLoaded to isAuthReady and fixed dbStatus hooks for useBasic
8
+ - ba294c5: dependency updates
9
+ - cd827af: add schema validation
10
+ - 1a3fe80: schema validation bugfix
11
+ - 13594a9: added basic-sync
12
+ - ba294c5: dependency update
13
+ - f035281: remove sync lib
14
+ - 4841960: rebuild tsup
15
+ - 96e0156: update schema validation
16
+ - cda3d16: add schema validation
17
+ - 2c898ae: update useQuery hook
18
+
19
+ - no need to pass param as a function, can now enter query directly
20
+ - defaults to empty array when loading
21
+
22
+ - 90ccdd7: add debug option
23
+ - 4841960: test build
24
+ - f035281: remove sync library
25
+ - ef528e9: added schema package - test
26
+
27
+ ### Patch Changes
28
+
29
+ - 3c7b34e: version changes mostly
30
+ - c085672: reset versions
31
+ - Updated dependencies [96e0156]
32
+ - Updated dependencies [ef528e9]
33
+ - @basictech/schema@0.1.0
34
+
35
+ ## 0.2.0-beta.11
36
+
37
+ ### Minor Changes
38
+
39
+ - update schema validation
40
+
41
+ ### Patch Changes
42
+
43
+ - Updated dependencies
44
+ - @basictech/schema@0.1.0-beta.1
45
+
46
+ ## 0.2.0-beta.10
47
+
48
+ ### Minor Changes
49
+
50
+ - added schema package - test
51
+
52
+ ### Patch Changes
53
+
54
+ - Updated dependencies
55
+ - @basictech/schema@0.1.0-beta.0
56
+
57
+ ## 0.2.0-beta.9
58
+
59
+ ### Minor Changes
60
+
61
+ - add schema validation
62
+
3
63
  ## 0.2.0-beta.8
4
64
 
5
65
  ### Minor Changes
package/dist/index.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import React from 'react';
3
+ export { useLiveQuery as useQuery } from 'dexie-react-hooks';
3
4
 
4
5
  declare enum DBStatus {
5
6
  LOADING = "LOADING",
@@ -37,6 +38,4 @@ declare function useBasic(): {
37
38
  dbStatus: DBStatus;
38
39
  };
39
40
 
40
- declare function useQuery(queryable: any): any;
41
-
42
- export { BasicProvider, useBasic, useQuery };
41
+ export { BasicProvider, useBasic };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import React from 'react';
3
+ export { useLiveQuery as useQuery } from 'dexie-react-hooks';
3
4
 
4
5
  declare enum DBStatus {
5
6
  LOADING = "LOADING",
@@ -37,6 +38,4 @@ declare function useBasic(): {
37
38
  dbStatus: DBStatus;
38
39
  };
39
40
 
40
- declare function useQuery(queryable: any): any;
41
-
42
- export { BasicProvider, useBasic, useQuery };
41
+ export { BasicProvider, useBasic };
package/dist/index.js CHANGED
@@ -32,7 +32,7 @@ var src_exports = {};
32
32
  __export(src_exports, {
33
33
  BasicProvider: () => BasicProvider,
34
34
  useBasic: () => useBasic,
35
- useQuery: () => useQuery
35
+ useQuery: () => import_dexie_react_hooks.useLiveQuery
36
36
  });
37
37
  module.exports = __toCommonJS(src_exports);
38
38
 
@@ -50,8 +50,7 @@ var import_dexie_observable = require("dexie-observable");
50
50
  var import_dexie = require("dexie");
51
51
 
52
52
  // src/config.ts
53
- var import_ajv = __toESM(require("ajv"));
54
- var SERVER_URL = "http://localhost:3000";
53
+ var SERVER_URL = "https://api.basic.tech";
55
54
  var log = (...args) => {
56
55
  try {
57
56
  if (localStorage.getItem("basic_debug") === "true") {
@@ -60,65 +59,6 @@ var log = (...args) => {
60
59
  } catch (e) {
61
60
  }
62
61
  };
63
- var basicJsonSchema = {
64
- "$schema": "http://json-schema.org/draft-07/schema#",
65
- "type": "object",
66
- "properties": {
67
- "project_id": {
68
- "type": "string"
69
- },
70
- "namespace": {
71
- "type": "string"
72
- },
73
- "version": {
74
- "type": "integer",
75
- "minimum": 0
76
- },
77
- "tables": {
78
- "type": "object",
79
- "patternProperties": {
80
- "^[a-zA-Z0-9_]+$": {
81
- "type": "object",
82
- "properties": {
83
- "name": {
84
- "type": "string"
85
- },
86
- "type": {
87
- "type": "string",
88
- "enum": ["collection"]
89
- },
90
- "fields": {
91
- "type": "object",
92
- "patternProperties": {
93
- "^[a-zA-Z0-9_]+$": {
94
- "type": "object",
95
- "properties": {
96
- "type": {
97
- "type": "string"
98
- },
99
- "primary": {
100
- "type": "boolean"
101
- },
102
- "indexed": {
103
- "type": "boolean"
104
- }
105
- },
106
- "required": ["type"]
107
- }
108
- },
109
- "additionalProperties": true
110
- }
111
- },
112
- "required": ["fields"]
113
- }
114
- },
115
- "additionalProperties": true
116
- }
117
- },
118
- "required": ["project_id", "version", "tables"]
119
- };
120
- var ajv = new import_ajv.default();
121
- var validator = ajv.compile(basicJsonSchema);
122
62
 
123
63
  // src/sync/syncProtocol.js
124
64
  var syncProtocol = function() {
@@ -223,6 +163,115 @@ var syncProtocol = function() {
223
163
  });
224
164
  };
225
165
 
166
+ // src/schema.ts
167
+ var import_ajv = __toESM(require("ajv"));
168
+ var basicJsonSchema = {
169
+ "$schema": "http://json-schema.org/draft-07/schema#",
170
+ "type": "object",
171
+ "properties": {
172
+ "project_id": {
173
+ "type": "string"
174
+ },
175
+ "namespace": {
176
+ "type": "string"
177
+ },
178
+ "version": {
179
+ "type": "integer",
180
+ "minimum": 0
181
+ },
182
+ "tables": {
183
+ "type": "object",
184
+ "patternProperties": {
185
+ "^[a-zA-Z0-9_]+$": {
186
+ "type": "object",
187
+ "properties": {
188
+ "name": {
189
+ "type": "string"
190
+ },
191
+ "type": {
192
+ "type": "string",
193
+ "enum": ["collection"]
194
+ },
195
+ "fields": {
196
+ "type": "object",
197
+ "patternProperties": {
198
+ "^[a-zA-Z0-9_]+$": {
199
+ "type": "object",
200
+ "properties": {
201
+ "type": {
202
+ "type": "string",
203
+ "enum": ["string", "boolean", "number", "json"]
204
+ },
205
+ "indexed": {
206
+ "type": "boolean"
207
+ },
208
+ "required": {
209
+ "type": "boolean"
210
+ }
211
+ },
212
+ "required": ["type"]
213
+ }
214
+ },
215
+ "additionalProperties": true
216
+ }
217
+ },
218
+ "required": ["fields"]
219
+ }
220
+ },
221
+ "additionalProperties": true
222
+ }
223
+ },
224
+ "required": ["project_id", "version", "tables"]
225
+ };
226
+ var ajv = new import_ajv.default();
227
+ var validator = ajv.compile(basicJsonSchema);
228
+ function validateSchema(schema) {
229
+ const v = validator(schema);
230
+ return {
231
+ valid: v,
232
+ errors: validator.errors || []
233
+ };
234
+ }
235
+ function validateData(schema, table, data, checkRequired = true) {
236
+ const valid = validateSchema(schema);
237
+ if (!valid.valid) {
238
+ return { valid: false, errors: valid.errors, message: "Schema is invalid" };
239
+ }
240
+ const tableSchema = schema.tables[table];
241
+ if (!tableSchema) {
242
+ return { valid: false, errors: [{ message: `Table ${table} not found in schema` }], message: "Table not found" };
243
+ }
244
+ for (const [fieldName, fieldValue] of Object.entries(data)) {
245
+ const fieldSchema = tableSchema.fields[fieldName];
246
+ if (!fieldSchema) {
247
+ return {
248
+ valid: false,
249
+ errors: [{ message: `Field ${fieldName} not found in schema` }],
250
+ message: "Invalid field"
251
+ };
252
+ }
253
+ const schemaType = fieldSchema.type;
254
+ const valueType = typeof fieldValue;
255
+ if (schemaType === "string" && valueType !== "string" || schemaType === "number" && valueType !== "number" || schemaType === "boolean" && valueType !== "boolean" || schemaType === "json" && valueType !== "object") {
256
+ return {
257
+ valid: false,
258
+ errors: [{
259
+ message: `Field ${fieldName} should be type ${schemaType}, got ${valueType}`
260
+ }],
261
+ message: "invalid type"
262
+ };
263
+ }
264
+ }
265
+ if (checkRequired) {
266
+ for (const [fieldName, fieldSchema] of Object.entries(tableSchema.fields)) {
267
+ if (fieldSchema.required && !data[fieldName]) {
268
+ return { valid: false, errors: [{ message: `Field ${fieldName} is required` }], message: "Required field missing" };
269
+ }
270
+ }
271
+ }
272
+ return { valid: true, errors: [] };
273
+ }
274
+
226
275
  // src/sync/index.ts
227
276
  syncProtocol();
228
277
  var BasicSync = class extends import_dexie2.Dexie {
@@ -240,6 +289,10 @@ var BasicSync = class extends import_dexie2.Dexie {
240
289
  log("Starting connection...");
241
290
  return this.syncable.connect("websocket", WS_URL, { authToken: access_token });
242
291
  }
292
+ async disconnect() {
293
+ const WS_URL = `${SERVER_URL}/ws`;
294
+ return this.syncable.disconnect(WS_URL);
295
+ }
243
296
  async updateSyncNodes() {
244
297
  try {
245
298
  const syncNodes = await this.table("_syncNodes").toArray();
@@ -290,19 +343,33 @@ var BasicSync = class extends import_dexie2.Dexie {
290
343
  ref: this.table(name),
291
344
  // --- WRITE ---- //
292
345
  add: (data) => {
293
- log("Adding data to", name, data);
346
+ const valid = validateData(this.basic_schema, name, data);
347
+ if (!valid.valid) {
348
+ log("Invalid data", valid);
349
+ return Promise.reject({ ...valid });
350
+ }
294
351
  return this.table(name).add({
295
352
  id: (0, import_uuid.v7)(),
296
353
  ...data
297
354
  });
298
355
  },
299
356
  put: (data) => {
357
+ const valid = validateData(this.basic_schema, name, data);
358
+ if (!valid.valid) {
359
+ log("Invalid data", valid);
360
+ return Promise.reject({ ...valid });
361
+ }
300
362
  return this.table(name).put({
301
363
  id: (0, import_uuid.v7)(),
302
364
  ...data
303
365
  });
304
366
  },
305
367
  update: (id, data) => {
368
+ const valid = validateData(this.basic_schema, name, data, false);
369
+ if (!valid.valid) {
370
+ log("Invalid data", valid);
371
+ return Promise.reject({ ...valid });
372
+ }
306
373
  return this.table(name).update(id, data);
307
374
  },
308
375
  delete: (id) => {
@@ -370,6 +437,7 @@ async function deleteRecord({ projectId, accountId, tableName, id, token }) {
370
437
  }
371
438
 
372
439
  // src/AuthContext.tsx
440
+ var import_schema2 = require("@basictech/schema");
373
441
  var import_jsx_runtime = require("react/jsx-runtime");
374
442
  var BasicContext = (0, import_react.createContext)({
375
443
  unicorn: "\u{1F984}",
@@ -414,11 +482,12 @@ function BasicProvider({ children, project_id, schema, debug = false }) {
414
482
  const syncRef = (0, import_react.useRef)(null);
415
483
  (0, import_react.useEffect)(() => {
416
484
  function initDb() {
417
- if (!validator(schema)) {
418
- log("Basic Schema is invalid!", validator.errors);
485
+ const valid = (0, import_schema2.validateSchema)(schema);
486
+ if (!valid.valid) {
487
+ log("Basic Schema is invalid!", valid.errors);
419
488
  console.group("Schema Errors");
420
489
  let errorMessage = "";
421
- validator.errors.forEach((error2, index) => {
490
+ valid.errors.forEach((error2, index) => {
422
491
  log(`${index + 1}:`, error2.message, ` - at ${error2.instancePath}`);
423
492
  errorMessage += `${index + 1}: ${error2.message} - at ${error2.instancePath}
424
493
  `;
@@ -461,10 +530,10 @@ function BasicProvider({ children, project_id, schema, debug = false }) {
461
530
  });
462
531
  };
463
532
  (0, import_react.useEffect)(() => {
464
- if (token && syncRef.current) {
533
+ if (token && syncRef.current && isSignedIn && isSignedIn) {
465
534
  connectToDb();
466
535
  }
467
- }, [token]);
536
+ }, [isSignedIn]);
468
537
  const getSignInLink = () => {
469
538
  log("getting sign in link...");
470
539
  const randomState = Math.random().toString(36).substring(6);
@@ -489,6 +558,9 @@ function BasicProvider({ children, project_id, schema, debug = false }) {
489
558
  setToken(null);
490
559
  document.cookie = `basic_token=; Secure; SameSite=Strict`;
491
560
  localStorage.removeItem("basic_auth_state");
561
+ if (syncRef.current) {
562
+ syncRef.current.disconnect();
563
+ }
492
564
  };
493
565
  const getToken = async () => {
494
566
  log("getting token...");
@@ -678,14 +750,6 @@ function useBasic() {
678
750
 
679
751
  // src/index.ts
680
752
  var import_dexie_react_hooks = require("dexie-react-hooks");
681
- function useQuery(queryable) {
682
- return (0, import_dexie_react_hooks.useLiveQuery)(() => {
683
- if (typeof queryable === "function") {
684
- return queryable();
685
- }
686
- return queryable;
687
- }, [queryable], []);
688
- }
689
753
  // Annotate the CommonJS export names for ESM import in node:
690
754
  0 && (module.exports = {
691
755
  BasicProvider,