@peerbit/indexer-sqlite3 1.0.0 → 1.0.1-5e0d4ec

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,80 +1,80 @@
1
1
  {
2
- "name": "@peerbit/indexer-sqlite3",
3
- "version": "1.0.0",
4
- "description": "SQLite index for document store",
5
- "type": "module",
6
- "sideEffects": false,
7
- "types": "./dist/src/index.d.ts",
8
- "typesVersions": {
9
- "*": {
10
- "*": [
11
- "*",
12
- "dist/*",
13
- "dist/src/*",
14
- "dist/src/*/index"
15
- ],
16
- "src/*": [
17
- "*",
18
- "dist/*",
19
- "dist/src/*",
20
- "dist/src/*/index"
21
- ]
22
- }
23
- },
24
- "files": [
25
- "src",
26
- "dist",
27
- "!dist/e2e",
28
- "!dist/test",
29
- "!**/*.tsbuildinfo"
30
- ],
31
- "exports": {
32
- ".": {
33
- "types": "./dist/src/index.d.ts",
34
- "import": "./dist/src/index.js"
35
- },
36
- "./sqlite.org": {
37
- "types": "./dist/peerbit/sqlite.org/sqlite3.d.ts",
38
- "import": "./dist/peerbit/sqlite.org/sqlite3.js"
39
- }
40
- },
41
- "browser": {
42
- "./dist/src/sqlite3.js": "./dist/src/sqlite3.browser.js"
43
- },
44
- "eslintConfig": {
45
- "extends": "peerbit",
46
- "parserOptions": {
47
- "project": true,
48
- "sourceType": "module"
49
- },
50
- "ignorePatterns": [
51
- "!.aegir.js",
52
- "test/ts-use",
53
- "*.d.ts"
54
- ]
55
- },
56
- "publishConfig": {
57
- "access": "public"
58
- },
59
- "scripts": {
60
- "clean": "aegir clean",
61
- "copy-sqlite-wasm": "mkdir -p ./dist/peerbit/ && cp -r ../../../../node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/. ./dist/peerbit/",
62
- "build-worker": "node ../../../../node_modules/esbuild/bin/esbuild --bundle ./src/sqlite3.worker.ts --format=esm --outfile=./dist/peerbit/sqlite3.worker.min.js",
63
- "build": "aegir build && npm run build-worker && npm run copy-sqlite-wasm",
64
- "test": "aegir test",
65
- "test:browser": "aegir clean && aegir test -t browser",
66
- "test:node": "aegir clean && aegir test -t node",
67
- "lint": "aegir lint"
68
- },
69
- "author": "dao.xyz",
70
- "license": "MIT",
71
- "dependencies": {
72
- "better-sqlite3": "^11.1.2",
73
- "@peerbit/indexer-interface": "^1.0.0",
74
- "@sqlite.org/sqlite-wasm": "^3.46.0-build2"
75
- },
76
- "devDependencies": {
77
- "@types/better-sqlite3": "^7.6.11",
78
- "@peerbit/indexer-tests": "^1.0.0"
79
- }
2
+ "name": "@peerbit/indexer-sqlite3",
3
+ "version": "1.0.1-5e0d4ec",
4
+ "description": "SQLite index for document store",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "types": "./dist/src/index.d.ts",
8
+ "typesVersions": {
9
+ "*": {
10
+ "*": [
11
+ "*",
12
+ "dist/*",
13
+ "dist/src/*",
14
+ "dist/src/*/index"
15
+ ],
16
+ "src/*": [
17
+ "*",
18
+ "dist/*",
19
+ "dist/src/*",
20
+ "dist/src/*/index"
21
+ ]
22
+ }
23
+ },
24
+ "files": [
25
+ "src",
26
+ "dist",
27
+ "!dist/e2e",
28
+ "!dist/test",
29
+ "!**/*.tsbuildinfo"
30
+ ],
31
+ "exports": {
32
+ ".": {
33
+ "types": "./dist/src/index.d.ts",
34
+ "import": "./dist/src/index.js"
35
+ },
36
+ "./sqlite.org": {
37
+ "types": "./dist/peerbit/sqlite.org/sqlite3.d.ts",
38
+ "import": "./dist/peerbit/sqlite.org/sqlite3.js"
39
+ }
40
+ },
41
+ "browser": {
42
+ "./dist/src/sqlite3.js": "./dist/src/sqlite3.browser.js"
43
+ },
44
+ "eslintConfig": {
45
+ "extends": "peerbit",
46
+ "parserOptions": {
47
+ "project": true,
48
+ "sourceType": "module"
49
+ },
50
+ "ignorePatterns": [
51
+ "!.aegir.js",
52
+ "test/ts-use",
53
+ "*.d.ts"
54
+ ]
55
+ },
56
+ "publishConfig": {
57
+ "access": "public"
58
+ },
59
+ "scripts": {
60
+ "clean": "aegir clean",
61
+ "copy-sqlite-wasm": "mkdir -p ./dist/peerbit/ && cp -r ../../../../node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/. ./dist/peerbit/",
62
+ "build-worker": "node ../../../../node_modules/esbuild/bin/esbuild --bundle ./src/sqlite3.worker.ts --format=esm --outfile=./dist/peerbit/sqlite3.worker.min.js",
63
+ "build": "aegir build && npm run build-worker && npm run copy-sqlite-wasm",
64
+ "test": "aegir test",
65
+ "test:browser": "aegir clean && aegir test -t browser",
66
+ "test:node": "aegir clean && aegir test -t node",
67
+ "lint": "aegir lint"
68
+ },
69
+ "author": "dao.xyz",
70
+ "license": "MIT",
71
+ "dependencies": {
72
+ "better-sqlite3": "^11.1.2",
73
+ "@peerbit/indexer-interface": "1.0.0-5e0d4ec",
74
+ "@sqlite.org/sqlite-wasm": "^3.46.0-build2"
75
+ },
76
+ "devDependencies": {
77
+ "@types/better-sqlite3": "^7.6.11",
78
+ "@peerbit/indexer-tests": "1.0.0-5e0d4ec"
79
+ }
80
80
  }
package/src/schema.ts CHANGED
@@ -253,17 +253,8 @@ export const ARRAY_INDEX_COLUMN = "__index";
253
253
  export const PARENT_TABLE_ID = "__parent_id";
254
254
  const FOREIGN_VALUE_PROPERTY = "value";
255
255
 
256
- /* const clazzHasVariants = (clazz: Constructor<any>) => {
257
- const schema = getSchema(clazz);
258
- return schema?.variant !== undefined;
259
-
260
- }
261
- */
262
256
  const clazzCanBeInlined = (clazz: Constructor<any>) => {
263
- return (
264
- /* clazzHasVariants(clazz) === false && */ (getDependencies(clazz, 0)
265
- ?.length ?? 0) === 0
266
- );
257
+ return (getDependencies(clazz, 0)?.length ?? 0) === 0;
267
258
  };
268
259
 
269
260
  interface SimpleNested {
@@ -10,6 +10,7 @@ import {
10
10
  } from "./types.js";
11
11
 
12
12
  class ProxyStatement implements IStatement {
13
+ id: string;
13
14
  resolvers: {
14
15
  [hash in string]: {
15
16
  resolve: (...args: any) => void;
@@ -23,7 +24,9 @@ class ProxyStatement implements IStatement {
23
24
  ) => Promise<T>,
24
25
  readonly databaseId: string,
25
26
  readonly statementId: string,
26
- ) {}
27
+ ) {
28
+ this.id = statementId;
29
+ }
27
30
 
28
31
  async bind(values: any[]) {
29
32
  await this.send({
@@ -7,14 +7,13 @@ import {
7
7
  type PreparedStatement as SQLStatement,
8
8
  default as sqlite3InitModule,
9
9
  } from "@sqlite.org/sqlite-wasm";
10
+ import { v4 as uuid } from "uuid";
10
11
  import type { BindableValue } from "./schema.js";
11
12
  import {
12
13
  type Statement as IStatement,
13
14
  type StatementGetResult,
14
15
  } from "./types.js";
15
16
 
16
- /* import { v4 as uuid } from 'uuid';
17
- */
18
17
  export const encodeName = (name: string): string => {
19
18
  // since "/" and perhaps other characters might not be allowed we do encode
20
19
  const writer = new BinaryWriter();
@@ -29,7 +28,10 @@ export const decodeName = (name: string): string => {
29
28
  };
30
29
 
31
30
  class Statement implements IStatement {
32
- constructor(private statement: SQLStatement) {}
31
+ constructor(
32
+ readonly statement: SQLStatement,
33
+ readonly id: string,
34
+ ) {}
33
35
 
34
36
  async bind(values: any[]) {
35
37
  await this.statement.bind(values);
@@ -85,61 +87,11 @@ class Statement implements IStatement {
85
87
  }
86
88
  }
87
89
 
88
- /* export class Database implements IDatabase {
89
-
90
- statements: Map<string, Statement> = new Map();
91
- private db: SQLDatabase
92
- constructor(private readonly _close?: () => Promise<any> | any) { }
93
-
94
- async exec(sql: string) {
95
- return this.db.exec(sql);
96
- }
97
-
98
- async prepare(sql: string) {
99
- const statement = this.db.prepare(sql);
100
- const wrappedStatement = new Statement(statement);
101
- this.statements.set(sql, wrappedStatement)
102
- return wrappedStatement
103
- }
104
-
105
- async close() {
106
- await Promise.all([...this.statements.values()].map(x => x.finalize?.()))
107
- await this.db.close();
108
- await this._close?.()
109
- }
110
-
111
- async get(sql: string) {
112
- return this.db.exec({ sql, rowMode: 'array' });
113
- }
114
-
115
- async run(sql: string, bind: any[]) {
116
- return this.db.exec(sql, { bind, rowMode: 'array' });
117
- }
118
- }
119
-
120
- */
121
-
122
90
  // eslint-disable-next-line no-console
123
91
  const log = (...args: any) => console.log(...args);
124
92
  // eslint-disable-next-line no-console
125
93
  const error = (...args: any) => console.error(...args);
126
94
 
127
- /* let initOpfsResult: Promise<{ sqlite3: Awaited<ReturnType<typeof sqlite3InitModule>>,poolUtil: }> | undefined = undefined;
128
- const initOpfs = async () => {
129
-
130
- let sqlite3: Awaited<ReturnType<typeof sqlite3InitModule>> = await sqlite3InitModule({
131
- locateFile: (path, prefix) => {
132
-
133
- return path;
134
- }, print: log, printErr: error
135
- });
136
- let poolUtil = await sqlite3.installOpfsSAHPoolVfs({
137
- directory: encodeName("helloworld")
138
- });
139
-
140
- return initOpfsResult || (initOpfsResult = { sqlite3, poolUtil })
141
- } */
142
-
143
95
  let poolUtil: SAHPoolUtil = undefined;
144
96
  let sqlite3: Awaited<ReturnType<typeof sqlite3InitModule>> | undefined =
145
97
  undefined;
@@ -190,10 +142,13 @@ const create = async (directory?: string) => {
190
142
  return sqliteDb.exec(sql);
191
143
  },
192
144
  open,
193
- prepare: (sql: string) => {
145
+ prepare: (sql: string, id?: string) => {
146
+ if (id == null) {
147
+ id = uuid();
148
+ }
194
149
  const statement = sqliteDb.prepare(sql);
195
- const wrappedStatement = new Statement(statement);
196
- statements.set(sql, wrappedStatement);
150
+ const wrappedStatement = new Statement(statement, id);
151
+ statements.set(id, wrappedStatement);
197
152
  return wrappedStatement;
198
153
  },
199
154
  get(sql: string) {
@@ -38,8 +38,8 @@ class SqliteWorkerHandler {
38
38
  } else if (message.type === "status") {
39
39
  return db.status();
40
40
  } else if (message.type === "prepare") {
41
- const statementId = message.sql;
42
- await db.prepare(message.sql);
41
+ const statementId = message.id;
42
+ await db.prepare(message.sql, message.id);
43
43
  // db.statements.get(statementId) -> statement, because sqlite3.wasm stores the statement in a map like this
44
44
  return statementId;
45
45
  } else if (message.type === "close") {
package/src/types.ts CHANGED
@@ -6,10 +6,7 @@ export type SQLite = {
6
6
 
7
7
  export type Database = {
8
8
  exec: (sql: string) => Promise<any> | any;
9
- prepare: (
10
- sql: string,
11
- err?: (err: any) => any,
12
- ) => Promise<Statement> | Statement;
9
+ prepare: (sql: string, id?: string) => Promise<Statement> | Statement;
13
10
  close: (err?: (err: any) => any) => Promise<any> | any;
14
11
  open(): Promise<any> | any;
15
12
  status: () => Promise<"open" | "closed"> | "open" | "closed";
@@ -18,6 +15,7 @@ export type Database = {
18
15
  export type StatementGetResult = { [key: string]: SQLLiteValue };
19
16
 
20
17
  export type Statement = {
18
+ id: string;
21
19
  bind: (
22
20
  values: BindableValue[],
23
21
  err?: (err: any) => any,
package/README.md DELETED
@@ -1,111 +0,0 @@
1
- # Documents
2
-
3
- Distributed document store.
4
-
5
- This store is built on top of the base store. This store allows for type-safe document storage and retrieval accross peers.
6
-
7
- As of now, go through the [tests](./src//__tests__/index.integration.test.ts) for documentation on how to use the module.
8
-
9
-
10
-
11
- Example
12
- ```typescript
13
- import { field, option, serialize, variant } from "@dao-xyz/borsh";
14
- import { Program } from "@peerbit/program";
15
- import { Peerbit } from "peerbit";
16
- import {
17
- Documents,
18
- DocumentIndex,
19
- SearchRequest,
20
- StringMatch,
21
- StringMatchMethod,
22
- Results,
23
- } from "@peerbit/document";
24
-
25
-
26
- @variant("document")
27
- class Document {
28
- @field({ type: "string" })
29
- id: string;
30
-
31
- @field({ type: option("string") })
32
- name?: string;
33
-
34
- @field({ type: option("u64") })
35
- number?: bigint;
36
-
37
- constructor(opts?: Document) {
38
- if (opts) {
39
- Object.assign(this, opts);
40
- }
41
- }
42
- }
43
-
44
- @variant("test_documents")
45
- class TestStore extends Program {
46
- @field({ type: Documents })
47
- docs: Documents<Document>;
48
-
49
- constructor(properties?: { docs: Documents<Document> }) {
50
- super();
51
- if (properties) {
52
- this.docs = properties.docs;
53
- }
54
- }
55
- async open(): Promise<void> {
56
- await this.docs.open({
57
- type: Document,
58
- index: {
59
- fields: (obj) => obj // here you can filter and transform what fields you want to index
60
- }})
61
- }
62
- }
63
-
64
- // later
65
-
66
- const peer = await Peerbit.create ({libp2p: your_libp2p_instance})
67
- const store = peer.open(new TestStore());
68
- console.log(store.address) /// this address can be opened by another peer
69
-
70
-
71
- // insert
72
- let doc = new Document({
73
- id: "1",
74
- name: "hello world",
75
- number: 1n,
76
- });
77
- let doc2 = new Document({
78
- id: "2",
79
- name: "hello world",
80
- number: 2n,
81
- });
82
-
83
- let doc3 = new Document({
84
- id: "3",
85
- name: "foo",
86
- number: 3n,
87
- });
88
-
89
- await store.docs.put(doc);
90
- await store.docs.put(doc2);
91
- await store.docs.put(doc3);
92
-
93
-
94
- // search for documents from another peer
95
- const peer2 = await Peerbit.create ({libp2: another_libp2p_instance})
96
- const store2 = peer2.open(store.address);
97
-
98
- let responses: Document[] = await store2.docs.index.search(
99
- new SearchRequest({
100
- query: [
101
- new StringMatch({
102
- key: "name",
103
- value: "ello",
104
- method: StringMatchMethod.contains
105
- }),
106
- ],
107
- })
108
- );
109
- expect(responses]).to.have.length(2);
110
- expect(responses.map((x) => x.value.id)).to.deep.equal(["1", "2"]);
111
- ```