Skip to content

Commit 9215b47

Browse files
committed
Update readme
1 parent 0640b7a commit 9215b47

File tree

2 files changed

+36
-122
lines changed

2 files changed

+36
-122
lines changed

README.md

Lines changed: 35 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,14 @@
5959

6060
## Protocols
6161

62-
Clients are strongly encouraged to read through the **[protocols document](https://github.com/FreeLeh/docs/blob/main/freedb/protocols.md)** to see how things work
63-
under the hood and **the limitations**.
62+
Clients are strongly encouraged to read through the **[protocols document](https://github.com/FreeLeh/docs/blob/main/freedb/protocols.md)** to see how things work under the hood and **the limitations**.
6463

6564
## Getting Started
6665

6766
### Installation
6867

6968
```
70-
npm install jsfreedb
69+
npm install @freeleh/jsfreedb
7170
```
7271

7372
### Pre-requisites
@@ -79,6 +78,9 @@ npm install jsfreedb
7978

8079
Let's assume each row in the table is represented by the `Person` interface.
8180

81+
> Note that you can also represent the row using a normal JavaScript object.
82+
> The object must follow the columns registered in the `store`.
83+
8284
```typescript
8385
interface Person {
8486
name: string;
@@ -87,37 +89,41 @@ interface Person {
8789
```
8890

8991
```typescript
90-
import { ServiceAccountGoogleAuthClient } from 'jsfreedb/google/auth/service_account';
91-
import { GOOGLE_SHEETS_READ_WRITE } from 'jsfreedb/google/auth/models';
92-
import { GoogleSheetRowStore, GoogleSheetRowStoreConfig } from 'jsfreedb/google/store/row';
92+
import {
93+
Oauth2GoogleAuthClient,
94+
ServiceAccountGoogleAuthClient,
95+
GoogleSheetRowStore,
96+
GoogleSheetRowStoreConfig,
97+
GOOGLE_SHEETS_READ_WRITE,
98+
} from '@freeleh/jsfreedb';
9399

94100
// If using Google Service Account.
95101
const auth = ServiceAccountGoogleAuthClient.fromServiceAccountFile(
96102
"<path_to_service_account_json>",
97-
GOOGLE_SHEETS_READ_WRITE
103+
GOOGLE_SHEETS_READ_WRITE,
98104
);
99105

100106
// If using Google OAuth2 Flow.
101-
// const auth = OAuth2GoogleAuthClient.fromClientSecretFile(
102-
// "<path_to_client_secret_json>",
103-
// "<path_to_cached_credentials_json>",
104-
// GOOGLE_SHEETS_READ_WRITE
105-
// );
107+
const auth = OAuth2GoogleAuthClient.fromFile(
108+
"<path_to_client_secret_json>",
109+
"<path_to_cached_credentials_json>",
110+
GOOGLE_SHEETS_READ_WRITE,
111+
);
106112

113+
// Create a new row store.
107114
const store = await GoogleSheetRowStore.create(
108115
auth,
109116
"<spreadsheet_id>",
110117
"<sheet_name>",
111-
new GoogleSheetRowStoreConfig(["name", "age"])
118+
new GoogleSheetRowStoreConfig(["name", "age"]),
112119
);
113120
```
114121

115122
### Querying Rows
116123

117-
```typescript
118-
// Output variable
119-
let output: Person[];
124+
The returned value is an array of JavaScript objects (or TypeScript `Record<string, any>`) that represents the rows in the table.
120125

126+
```typescript
121127
// Select all columns for all rows
122128
output = await store.select().exec();
123129

@@ -171,7 +177,7 @@ await store.insert(
171177
```typescript
172178
const colToUpdate: Record<string, any> = {
173179
name: "new_name",
174-
age: 12
180+
age: 12,
175181
};
176182

177183
// Update all rows
@@ -220,10 +226,13 @@ class PersonClass {
220226
> Please use `KV Store V2` as much as possible, especially if you are creating a new storage.
221227
222228
```typescript
223-
import { ServiceAccountGoogleAuthClient } from 'jsfreedb/google/auth/service_account';
224-
import { GOOGLE_SHEETS_READ_WRITE } from 'jsfreedb/google/auth/models';
225-
import { GoogleSheetKVStore } from 'jsfreedb/google/store/kv';
226-
import { KVMode } from 'jsfreedb/google/utils/kv';
229+
import {
230+
OAuth2GoogleAuthClient,
231+
ServiceAccountGoogleAuthClient,
232+
GOOGLE_SHEETS_READ_WRITE,
233+
GoogleSheetKVStore,
234+
KVMode,
235+
} from '@freeleh/jsfreedb';
227236

228237
// If using Google Service Account.
229238
const auth = ServiceAccountGoogleAuthClient.fromServiceAccountFile(
@@ -232,11 +241,11 @@ const auth = ServiceAccountGoogleAuthClient.fromServiceAccountFile(
232241
);
233242

234243
// If using Google OAuth2 Flow.
235-
// const auth = OAuth2GoogleAuthClient.fromClientSecretFile(
236-
// "<path_to_client_secret_json>",
237-
// "<path_to_cached_credentials_json>",
238-
// GOOGLE_SHEETS_READ_WRITE
239-
// );
244+
const auth = OAuth2GoogleAuthClient.fromFile(
245+
"<path_to_client_secret_json>",
246+
"<path_to_cached_credentials_json>",
247+
GOOGLE_SHEETS_READ_WRITE
248+
);
240249

241250
const kv = await GoogleSheetKVStore.create(
242251
auth,
@@ -302,101 +311,6 @@ const kv = await GoogleSheetKVStore.create(
302311
);
303312
```
304313

305-
## KV Store V2
306-
307-
The KV Store V2 is implemented internally using the row store.
308-
309-
> The original `KV Store` was created using more complicated formulas, making it less maintainable.
310-
> You can still use the original `KV Store` implementation, but we strongly suggest using this new `KV Store V2`.
311-
312-
You cannot use an existing sheet based on `KV Store` with `KV Store V2` as the sheet structure is different.
313-
- If you want to convert an existing sheet, just add an `_rid` column and insert the first key-value row with `1`
314-
and increase it by 1 until the last row.
315-
- Remove the timestamp column as `KV Store V2` does not depend on it anymore.
316-
317-
```typescript
318-
import { ServiceAccountGoogleAuthClient } from 'jsfreedb/google/auth/service_account';
319-
import { GOOGLE_SHEETS_READ_WRITE } from 'jsfreedb/google/auth/models';
320-
import { GoogleSheetKVStoreV2 } from 'jsfreedb/google/store/kv_v2';
321-
import { KVMode } from 'jsfreedb/google/utils/kv';
322-
323-
// If using Google Service Account.
324-
const auth = ServiceAccountGoogleAuthClient.fromServiceAccountFile(
325-
"<path_to_service_account_json>",
326-
GOOGLE_SHEETS_READ_WRITE
327-
);
328-
329-
// If using Google OAuth2 Flow.
330-
// const auth = OAuth2GoogleAuthClient.fromClientSecretFile(
331-
// "<path_to_client_secret_json>",
332-
// "<path_to_cached_credentials_json>",
333-
// GOOGLE_SHEETS_READ_WRITE
334-
// );
335-
336-
const kv = await GoogleSheetKVStoreV2.create(
337-
auth,
338-
"<spreadsheet_id>",
339-
"<sheet_name>",
340-
{ mode: KVMode.AppendOnly }
341-
);
342-
```
343-
344-
### Get Value V2
345-
346-
If the key is not found, a `KeyNotFoundError` will be thrown.
347-
348-
```typescript
349-
try {
350-
const value = await kv.get("k1");
351-
console.log(value);
352-
} catch (error) {
353-
if (error instanceof KeyNotFoundError) {
354-
console.log("Key not found");
355-
} else {
356-
throw error;
357-
}
358-
}
359-
```
360-
361-
### Set Key V2
362-
363-
```typescript
364-
await kv.set("k1", "some_value");
365-
```
366-
367-
### Delete Key V2
368-
369-
```typescript
370-
await kv.delete("k1");
371-
```
372-
373-
### Supported Modes V2
374-
375-
> For more details on how the two modes are different, please read the [protocol document](https://github.com/FreeLeh/docs/blob/main/freedb/protocols.md).
376-
377-
There are 2 different modes supported:
378-
379-
1. Default mode.
380-
2. Append only mode.
381-
382-
```typescript
383-
// Default mode
384-
const kv = await GoogleSheetKVStoreV2.create(
385-
auth,
386-
"<spreadsheet_id>",
387-
"<sheet_name>",
388-
{ mode: KVMode.Default }
389-
);
390-
391-
// Append only mode
392-
const kv = await GoogleSheetKVStoreV2.create(
393-
auth,
394-
"<spreadsheet_id>",
395-
"<sheet_name>",
396-
{ mode: KVMode.AppendOnly }
397-
);
398-
```
399-
400314
## License
401315

402316
This project is [MIT licensed](https://github.com/FreeLeh/JSFreeDB/blob/main/LICENSE).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@freeleh/jsfreedb",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "JSFreeDB is a JavaScript library that provides common and simple database abstractions on top of Google Sheets.",
55
"main": "dist/index.cjs",
66
"module": "dist/index.mjs",

0 commit comments

Comments
 (0)