GithubHelp home page GithubHelp logo

azarattum / crstore Goto Github PK

View Code? Open in Web Editor NEW
91.0 91.0 3.0 1.52 MB

Conflict-free replicated store.

Home Page: https://npmjs.com/package/crstore

License: MIT License

HTML 0.29% Svelte 20.91% TypeScript 75.78% JavaScript 3.02%
crdt kysely local-first react reactivity solid sqlite store superstruct svelte trpc

crstore's People

Contributors

azarattum avatar mjadobson avatar silviot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

crstore's Issues

Difficulties starting the project locally

Hey @Azarattum, I wanted to play with CRStore, but could not get it started with vite locally.

This is the message:

$  yarn dev
yarn run v1.22.19
$ vite dev
failed to load config from CRStore/vite.config.js
error when starting dev server:
ReferenceError: BroadcastChannel is not defined
    at database (file:///CRStore/vite.config.js.timestamp-1684107342218-0f51a313eeb19.mjs:470:19)
    at file:///CRStore/vite.config.js.timestamp-1684107342218-0f51a313eeb19.mjs:697:28
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Same message with pnpm. Do I need to configure something else to have BroadcastChannel available for vite to compile properly? Is this expected behaviour?

Cheers and have a good Monday,
Roman

Better Bun Support (bun:sqlite)

Hello! ๐Ÿ‘‹

I'm currently using your awesome package to build an internal local-first app at my company. I'm using Bun & SvelteKit to build my project, and so I noticed that this package has experimental support for Bun. Now I'm still a beginner in this whole local-first cr-sqlite CRDT world. But as I understand it, any SQLite instance that works with cr-sqlite should be able to work with CRStore, bun:sqlite included.

Problem

When I try to run this file in Bun:

import { crr, primary } from "crstore";
import { boolean, object, string } from "superstruct";
import { database } from "crstore";
import Os from 'os';
import { Database } from 'bun:sqlite';

const macOS = Os.platform() === "darwin";

if (macOS) {
    /**
     * By default, macOS ships with Apple's proprietary build of SQLite
     * which doesn't support extensions. (therefore does not support cr-sqlite)
     * To use extensions, you'll need to install a vanilla build of SQLite.
     * Reference: https://bun.sh/docs/api/sqlite#loadextension
     */
    Database.setCustomSQLite("/opt/homebrew/opt/sqlite/lib/libsqlite3.dylib");
}

// Struct that represents the table
const todos = object({
  id: string(),
  title: string(),
  text: string(),
  completed: boolean(),
});
crr(todos); // Register table with conflict-free replicated relations
primary(todos, "id"); // Define a primary key (can be multi-column)

const schema = object({ todos });

const { close } = database(schema);

close();

I get this error:

53 |             return Promise.resolve({
54 |                 rows: stmt.all(parameters),
55 |             });
56 |         }
57 |         else {
58 |             const { changes, lastInsertRowid } = stmt.run(parameters);
                                                      ^
TypeError: Right side of assignment cannot be destructured
      at executeQuery (/Users/fawaz/Projects/crstore-bun-issue/node_modules/kysely/dist/esm/dialect/sqlite/sqlite-driver.js:58:50)
      at /Users/fawaz/Projects/crstore-bun-issue/node_modules/kysely/dist/esm/dialect/sqlite/sqlite-driver.js:35:15
      at rollbackTransaction (/Users/fawaz/Projects/crstore-bun-issue/node_modules/kysely/dist/esm/dialect/sqlite/sqlite-driver.js:34:31)
      at /Users/fawaz/Projects/crstore-bun-issue/node_modules/kysely/dist/esm/kysely.js:417:23

Minimal Reproduction: https://github.com/fawaz-alesayi/crstore-bun-issue

Why I think this happens

After a couple of minutes of investigation I noticed that you're using Kysely when creating the new tables defined by superstruct.

As I understand it, Kysely only supports bun:sqlite if you're using a custom adapter like these ones
https://github.com/dylanblokhuis/kysely-bun-sqlite
https://github.com/subframe7536/kysely-sqlite-tools/tree/master/packages/dialect-bun-worker

Solutions

Here are some ways I thought up of making Bun support better:

Solution 1: Allow users to provide their own Kysely adapters

This is the solution I'm using in my fork and it works great. The new API for the database function would be

import { object, string, type Infer } from "superstruct";

    const db = database(schema, {
      name: "test.db",
      kysely: ({ database }) => {
        return new Kysely<Schema<Infer<typeof schema>>>({
          dialect: new SqliteDialect({ database }), // Or BunDialect, LibSQL, whatever works with cr-sqlite
          plugins: [new JSONPlugin()],
        });
      },
    });

Solution 2: Conditional check to load appropriate Kysely Dialect

Would look something like this:

// https://github.com/Azarattum/CRStore/blob/main/src/lib/database/index.ts
async function init<T extends CRSchema>(
  file: string,
  schema: T,
  paths = defaultPaths,
) {
  type DB = Schema<T>;
  if (connections.has(file)) return connections.get(file) as Connection<DB>;

  const { database, browser } = await load(file, paths);
  const Dialect = browser ? CRDialect : process.isBun ? SqliteBunDialect : SqliteDialect;
  const kysely = new Kysely<DB>({
    dialect: new Dialect({ database }),
    plugins: [new JSONPlugin()],
  });

// ... rest of the code

I haven't had much time to dig into the actual error that is produced by Kysely and why it happens with Bun. but my guess is that there's some subtle API differences between bun:sqlite and better-sqlite3 that causes this error.

Question about migrations

I'm using cr-sqlite in a static SvelteKit project and stumbled upon this library and it looks nicer than running raw sql and handling async stores myself.

If my primary (only) data lives in the browser with a named sqlite db, what happens when I update the schema? My original plan with my method is to write migrations manually and apply them on each first page load, wondering if this library can handle this case.

Stores initialise as empty array rather than undefined

$posts // --> []
$posts // --> [{},{},{},{}]

Because an empty array is a valid output, this can't be relied upon to assess subscription status (initialising, refreshing, loading...). It would be helpful to either initialise as undefined or have array status methods (eg: $posts.loading)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.