Skip to content

Commit 9bb5efb

Browse files
committed
update readme, fix imports
1 parent 5c79f2a commit 9bb5efb

7 files changed

Lines changed: 64 additions & 52 deletions

File tree

README.md

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# @dnvgl-electricgrid/sqljs-documentstore
1+
# sqljs-documentstore
22

3-
`@dnvgl-electricgrid/sqljs-documentstore` Minimal, encrypted, sql friendly typed document store, with support for indexed columns. Protects against transactional conflicts.
3+
`sqljs-documentstore` Minimal, encrypted, sql friendly typed document store, with support for indexed columns. Protects against transactional conflicts.
44
The schema for each table is [id, json, ...indexedColumns]
55

66

77
## Install
8-
`npm install --save @dnvgl-electricgrid/sqljs-documentstore`
8+
`npm install --save sqljs-documentstore`
99

1010
## Usage
1111

@@ -18,12 +18,16 @@ exported pieces:
1818
-----
1919
### Getting started
2020

21-
first, load your database
22-
recommend creating a wrapping "DocumentStores" class as shown below:
21+
first, load your database
2322
```ts
24-
const database = await sqljsHelpers.load('mydb', await sqlite());
25-
const lockedDatabase = new LockedDatabase(database, () => sqljsHelpers.save('mydb', database));
23+
const sqlJsStatic = await sqlite({ locateFile: (file: string) => `${import.meta.env.BASE_URL}assets/${file}` }); //ensure sql-wasm.wasm is placed in dist/assets/ folder
24+
const {key, database} = await sqljsPersistence.load('mydb', 'mySecretDbKey', sqlJsStatic);
2625

26+
const flush = flushHelpers.createAsyncFlushQueue(() => sqljsPersistence.save('mydb', key, database));
27+
const lockedDatabase = new LockedDatabase(database, flush);
28+
```
29+
recommend creating a wrapping "DocumentStores" class as shown below:
30+
```ts
2731
public class DocumentStores extends Db {
2832
public CustomerData = new TypedDocumentStore(this.db, 'CustomerData', <CustomerData>{}, {
2933
name: x => x.name,
@@ -40,4 +44,48 @@ public class DocumentStores extends Db {
4044
}
4145
```
4246

43-
see [examples.md] (https://github.com/dnvgl-opensource/sqljs-documentstore/docs/examples.md) for usage examples.
47+
usage example
48+
```ts
49+
interface CustomerData {
50+
id: string;
51+
name: string;
52+
address: string;
53+
orders: any[];//OrderData[];
54+
}
55+
56+
57+
function exampleUsage() {
58+
const db = DocumentStores.Instance;
59+
const allCustomers = ds.CustomerData.getAll(); // returns CustomerData[]
60+
61+
const dave_id = 'abc123';
62+
63+
db.txn('create Dave', txnId => {
64+
db.CustomerData.set(txnId, { id: dave_id, name: 'Dave', address: '1 infinity lp', orders: [{ id: 1, description: 'first order' }]});
65+
});
66+
67+
const dave = db.CustomerData.get(dave_id); // returns <CustomerData>
68+
const maybe_dave = db.CustomerData.tryGet(dave_id); //returns <CustomerData?> assert it's not null before using
69+
70+
db.txn('update Dave', txnId => {
71+
db.CustomerData.update(txnId, dave_id, x => {
72+
x.name += ' the great';
73+
x.address = 'cloud 9';
74+
});
75+
});
76+
77+
const itemsWithOrders = db.CustomerData.query(x => `where ${x.orderCount} >= ?`, [1]);
78+
const indexValuesOnly = db.CustomerData.queryIndexes(x => `where ${x.orderCount} > 0`); //fetch just index columns (strongly typed as index type - {id, name, orderCount} in this case) useful if table has large quantities of data and you only need, e.g. the ids that match a query
79+
const customerIds = new Set(indexValuesOnly.map(x => x.id));
80+
}
81+
82+
//use txnAsync when you want to have a transactional scope around actions that are async
83+
async function exampleUsageAsync() {
84+
const db = DocumentStores.Instance;
85+
await db.txnAsync('fetch in the middle of a txn', txnId => {
86+
const newData = await fetch(.../* actual params go here */);
87+
db.CustomerData.insertMany(newData); //use insertMany if data is not expected to exist already
88+
db.CustomerData.setMany(newData); //insert new rows, update if already exist
89+
});
90+
}
91+
```

docs/examples.md

Lines changed: 0 additions & 37 deletions
This file was deleted.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "@dnvgl-electricgrid/sqljs-documentstore",
3-
"version": "1.0.0",
2+
"name": "sqljs-documentstore",
3+
"version": "1.1.0",
44
"description": "Minimal, encrypted, sql friendly typed document store, with support for indexed columns. Protects against transactional conflicts",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -22,13 +22,15 @@
2222
"Generic",
2323
"ES7"
2424
],
25+
"repository": "https://github.com/dnv-opensource/sqljs-documentstore",
26+
"author": "Michael Goeke",
2527
"license": "MIT",
28+
"private": false,
2629
"dependencies": {
2730
"async-lock": "^1.4.1",
2831
"idb-keyval": "^6.2.2",
2932
"lodash": "^4.17.21",
3033
"sql.js": "^1.13.0"
31-
3234
},
3335
"devDependencies": {
3436
"@types/async-lock": "^1.4.2",

src/LockedDatabase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {BindParams, QueryExecResult, type Database} from 'sql.js';
2-
import AsyncLock from "async-lock";
2+
import * as AsyncLock from "async-lock";
33

44
export class LockedDatabase implements ILockedDatabase {
55
lock = new AsyncLock();

src/TypedDocumentStore.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-use-before-define */
22
/* eslint-disable lines-between-class-members */
3-
import _ from 'lodash';
3+
import * as _ from 'lodash';
44
import { ILockedDatabase } from './LockedDatabase';
55
import { sqljsHelpers } from './sqljsHelpers';
66
import { SqlValue } from 'sql.js';

src/sqljsHelpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import sqlite from 'sql.js';
1+
import * as sqlite from 'sql.js';
22
import { createStore, get, getMany, setMany } from 'idb-keyval';
33

44
export interface EncryptedDataItem {

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"strict": true,
77
"experimentalDecorators": true,
88
"emitDecoratorMetadata": true,
9-
"allowSyntheticDefaultImports": true,
109
"lib": ["es6", "dom", "esnext.asynciterable"],
1110
"sourceMap": true,
1211
"declaration": true,

0 commit comments

Comments
 (0)