Skip to content

Commit

Permalink
feat: adds solid-js support
Browse files Browse the repository at this point in the history
  • Loading branch information
thedanchez committed Feb 5, 2024
1 parent 33ac293 commit 3859e8d
Show file tree
Hide file tree
Showing 17 changed files with 1,096 additions and 5 deletions.
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,40 @@
"build:clean:blockstore": "pnpm --filter @fireproof/encrypted-blockstore build:clean",
"build:clean:core": "pnpm --filter @fireproof/core build:clean",
"build:clean:react": "pnpm --filter use-fireproof build:clean",
"build:clean:solid": "pnpm --filter @fireproof/solid-js build:clean",
"build:core": "pnpm --filter @fireproof/core build",
"build:react": "pnpm --filter use-fireproof build",
"build:scripts": "pnpm -r build:scripts",
"build:scripts:blockstore": "pnpm --filter @fireproof/encrypted-blockstore build:scripts",
"build:watch:react": "pnpm --parallel build:watch:react",
"build:solid": "pnpm --filter @fireproof/solid-js build",
"build:watch:solid": "pnpm --parallel build:watch:solid",
"clean": "rm -rf node_modules && pnpm -r clean",
"clean:all": "pnpm build:clean && pnpm clean",
"clean:blockstore": "pnpm --filter @fireproof/encrypted-blockstore clean",
"clean:core": "pnpm --filter @fireproof/core clean",
"clean:react": "pnpm --filter use-fireproof clean",
"clean:solid": "pnpm --filter @fireproof/solid-js clean",
"format:check": "pnpm -r format:check",
"format:check:react": "pnpm --filter use-fireproof format:check",
"format:fix": "pnpm -r format:fix",
"format:fix:react": "pnpm --filter use-fireproof format:fix",
"format:fix:solid": "pnpm --filter @fireproof/solid-js format:fix",
"lint:check": "pnpm -r lint:check",
"lint:check:react": "pnpm --filter use-fireproof lint:check",
"lint:check:solid": "pnpm --filter @fireproof/solid-js lint:check",
"lint:fix": "pnpm -r lint:fix",
"lint:fix:react": "pnpm --filter use-fireproof lint:fix",
"lint:fix:solid": "pnpm --filter @fireproof/solid-js lint:fix",
"start:react": "pnpm --filter @fireproof-example/react start",
"test": "pnpm -r test",
"test:blockstore": "pnpm --filter @fireproof/encrypted-blockstore test",
"test:core": "pnpm --filter @fireproof/core test",
"test:cov": "pnpm -r test:cov",
"test:react": "pnpm --filter use-fireproof test"
"test:cov:react": "pnpm --filter use-fireproof test:cov",
"test:cov:solid": "pnpm --filter @fireproof/solid-js test:cov",
"test:react": "pnpm --filter use-fireproof test",
"test:solid": "pnpm --filter @fireproof/solid-js test"
},
"keywords": [
"database",
Expand Down
3 changes: 2 additions & 1 deletion packages/encrypted-blockstore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"build:types": "tsc --declaration --outDir dist/types && node ./scripts/types.js",
"build:version": "node -p \"'export const PACKAGE_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts",
"build:watch": "tsup --watch",
"build:watch:nodemon": "nodemon -w src -w test -e ts,js --exec \"npm run build\"",
"build:watch:node": "nodemon -w src -w test -e ts,js --exec \"npm run build\"",
"build:watch:solid": "pnpm build:watch",
"build:watch:react": "pnpm build:watch",
"clean": "rm -rf node_modules",
"cp:artifacts": "cp dist/lib/index.global.js ../fireproof/test/www/encrypted-blockstore.iife.js",
Expand Down
5 changes: 4 additions & 1 deletion packages/encrypted-blockstore/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,7 @@ const TEST_BUNDLES: readonly Options[] = [
},
]

export default defineConfig([ ...LIBRARY_BUNDLES, ...TEST_BUNDLES ]);
export default defineConfig((options) => [
...LIBRARY_BUNDLES,
...(options.watch ? [] : TEST_BUNDLES)
]);
1 change: 1 addition & 0 deletions packages/fireproof/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"build:types": "tsc --declaration --outDir dist/types && node ./scripts/types.js",
"build:version": "node -p \"'export const PACKAGE_VERSION = ' + JSON.stringify(require('./package.json').version) + ';'\" > src/version.ts",
"build:watch": "tsup --watch",
"build:watch:solid": "pnpm build:watch",
"build:watch:react": "pnpm build:watch",
"clean": "rm -rf node_modules",
"cp:artifacts": "cp dist/browser/fireproof.global.js test/www/fireproof.iife.js",
Expand Down
5 changes: 4 additions & 1 deletion packages/fireproof/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,7 @@ const TEST_BUNDLES: readonly Options[] = [
},
]

export default defineConfig([ ...LIBRARY_BUNDLES, ...TEST_BUNDLES ]);
export default defineConfig((options) => [
...LIBRARY_BUNDLES,
...(options.watch ? [] : TEST_BUNDLES)
]);
26 changes: 26 additions & 0 deletions packages/solid-js/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"parser": "@typescript-eslint/parser",
"plugins": ["simple-import-sort", "solid"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:solid/typescript"
],
"env": {
"browser": true,
"node": true,
"es6": true,
"jest": true
},
"parserOptions": {
"sourceType": "module"
},
"rules": {
"@typescript-eslint/no-unused-vars": ["error", { "varsIgnorePattern": "^_" }],
"no-unused-vars": "off",
"simple-import-sort/imports": "error",
"simple-import-sort/exports": "error"
},
"ignorePatterns": ["dist", "node_modules"]
}
3 changes: 3 additions & 0 deletions packages/solid-js/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage
dist
node_modules
106 changes: 106 additions & 0 deletions packages/solid-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"name": "@fireproof/solid-js",
"version": "0.17.0",
"description": "The SolidJS adapter for Fireproof. Light up your data with an embedded live database for your SolidJS web app.",
"type": "module",
"module": "./dist/server.js",
"main": "./dist/server.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"browser": {
"./dist/server.js": "./dist/index.js"
},
"exports": {
"worker": {
"solid": "./dist/server.jsx",
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/server.js"
}
},
"browser": {
"solid": "./dist/index.jsx",
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"deno": {
"solid": "./dist/server.jsx",
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/server.js"
}
},
"node": {
"solid": "./dist/server.jsx",
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/server.js"
}
},
"solid": "./dist/index.jsx",
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"typesVersions": {},
"author": "Daniel Sanchez",
"license": "Apache-2.0 OR MIT",
"homepage": "https://use-fireproof.com",
"repository": {
"type": "git",
"url": "git+https://github.com/fireproof-storage/fireproof.git"
},
"bugs": {
"url": "https://github.com/fireproof-storage/fireproof/issues"
},
"scripts": {
"prepublishOnly": "cp ../../README.md . && pnpm build",
"postpublish": "rm README.md",
"build": "tsup",
"build:clean": "rm -rf dist",
"build:watch:solid": "tsup --watch",
"clean": "rm -rf node_modules",
"format:check": "prettier . --check",
"format:fix": "prettier . --write",
"lint:check": "eslint . --ext .ts,.tsx",
"lint:fix": "eslint . --ext .ts,.tsx --fix",
"test": "vitest run",
"test:cov": "vitest run --coverage"
},
"dependencies": {
"@fireproof/core": "workspace:^",
"deepmerge-ts": "^5.1.0"
},
"peerDependencies": {
"solid-js": ">=1.8.0"
},
"devDependencies": {
"@solidjs/router": "^0.10.5",
"@vitest/coverage-istanbul": "^1.1.0",
"buffer": "^6.0.3",
"esbuild": "^0.19.10",
"eslint": "^8.56.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-solid": "^0.13.0",
"fake-indexeddb": "^5.0.1",
"prettier": "^3.1.1",
"solid-js": "^1.8.7",
"tsup-preset-solid": "^2.2.0",
"typescript": "^5.3.3",
"vite": "^5.0.12",
"vite-plugin-solid": "^2.9.1",
"vitest": "^1.2.2"
},
"keywords": [
"solid",
"database",
"json",
"live",
"sync"
]
}
14 changes: 14 additions & 0 deletions packages/solid-js/prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @type {import("prettier").Config} */
const config = {
arrowParens: "always",
bracketSpacing: true,
endOfLine: "lf",
printWidth: 120,
semi: true,
singleQuote: false,
tabWidth: 2,
trailingComma: "es5",
useTabs: false,
};

export default config;
7 changes: 7 additions & 0 deletions packages/solid-js/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import "fake-indexeddb/auto";

import { Buffer } from "buffer";
import { TextEncoder } from "util";

global.TextEncoder = TextEncoder;
global.Buffer = Buffer;
114 changes: 114 additions & 0 deletions packages/solid-js/src/__tests__/createFireproof.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { createRoot } from "solid-js";
import { describe, expect, test } from "vitest";

import { createFireproof } from "../createFireproof";

type TestDoc = { text: string; completed: boolean };

const sleepHalfSecond = (ms: number = 500) => new Promise((resolve) => setTimeout(resolve, ms));

describe("HOOK: createFireproof", () => {
test("can perform all expected actions", async () => {
await createRoot(async (dispose) => {
const { database, createDocument, createLiveQuery } = createFireproof("TestDB");
const [doc, setDoc, saveDoc] = createDocument<TestDoc>(() => ({ text: "", completed: false }));
const query = createLiveQuery("_id");
await sleepHalfSecond(); // wait for the initial createDocument effect to finish

// 1. Can initialize a document
expect(doc()).toEqual({ text: "", completed: false });

// 2. Can update the document
setDoc({ text: "hello", completed: true });
expect(doc()).toEqual({ text: "hello", completed: true });
expect((await database().allDocs()).rows).toEqual([]);
expect(query().docs).toEqual([]);

// 3. Can save the document to the database
const { id } = await saveDoc();
expect(await database().get<TestDoc>(id)).toEqual({ _id: id, text: "hello", completed: true });
expect(doc()).toEqual({ _id: id, text: "hello", completed: true });

await sleepHalfSecond();
expect(query().docs).toEqual([
{
_id: id,
text: "hello",
completed: true,
},
]);

// 4. Can locally update the same document (retaining _id info post first save)
setDoc({ text: "world", completed: false });
expect(doc()).toEqual({ _id: id, text: "world", completed: false });
expect(await database().get<TestDoc>(id)).toEqual({ _id: id, text: "hello", completed: true });
expect(query().docs).toEqual([
{
_id: id,
text: "hello",
completed: true,
},
]);

// 5. Can update the stored document
await saveDoc();
expect(await database().get<TestDoc>(id)).toEqual({ _id: id, text: "world", completed: false });
expect(doc()).toEqual({ _id: id, text: "world", completed: false });

await sleepHalfSecond();
expect(query().docs).toEqual([
{
_id: id,
text: "world",
completed: false,
},
]);

// 6. Can start anew with another document
setDoc();
expect(doc()).toEqual({ text: "", completed: false });

// 7. Can update the new document
setDoc({ text: "foo", completed: true });
expect(doc()).toEqual({ text: "foo", completed: true });

// 8. Can save the new document
const { id: id2 } = await saveDoc();
expect(doc()).toEqual({ _id: id2, text: "foo", completed: true });
expect(await database().get<TestDoc>(id2)).toEqual({ _id: id2, text: "foo", completed: true });

await sleepHalfSecond();
expect(query().docs).toEqual([
{
_id: id,
text: "world",
completed: false,
},
{
_id: id2,
text: "foo",
completed: true,
},
]);

// Test cleanup to not keep data in the database across tests
await database().del(id);
await database().del(id2);

expect(
await database()
.get(id)
.catch(() => null)
).toBeNull();

expect(
await database()
.get(id2)
.catch(() => null)
).toBeNull();

expect(query().docs).toEqual([]);
dispose();
});
});
});
Loading

0 comments on commit 3859e8d

Please sign in to comment.