Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/roj1512/pls
Preserve `localStorage` in databases.
https://github.com/roj1512/pls
deno localstorage mongo postgres redis
Last synced: 3 months ago
JSON representation
Preserve `localStorage` in databases.
- Host: GitHub
- URL: https://github.com/roj1512/pls
- Owner: UnmaintainedProjects
- License: wtfpl
- Created: 2022-08-09T07:59:15.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-08-13T07:49:37.000Z (about 2 years ago)
- Last Synced: 2024-07-07T10:25:49.593Z (4 months ago)
- Topics: deno, localstorage, mongo, postgres, redis
- Language: TypeScript
- Homepage:
- Size: 8.79 KB
- Stars: 9
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-deno - PLS - Use 2 lines to persist localStorage in any database, including, but not limited to, MongoDB, PostgreSQL and Redis. (Modules / Utils)
README
# PLS [![](https://shield.deno.dev/x/pls)](https://deno.land/x/pls)
> Preserve `localStorage` in databases.
## Features
- No adapter dependency is loaded by default.
- Possible to write/contribute your own adapters.
- Currently providing ready-to-use adapters for MongoDB, PostgreSQL and Redis.## Get Started
```ts
import { setup } from "https://deno.land/x/pls/mod.ts";
```### Automatic
You can set the `PLS_CONNECTION_URI` environment variable to a MongoDB,
PostgreSQL or Redis connnection URI, and simply call `setup()` without any
arguments:```ts
await setup();
```### MongoDB
```ts
import { MongoClient } from "https://deno.land/x/mongo/mod.ts";
import { MongoAdapter } from "https://deno.land/x/pls/adapters/mongo.ts";const client = new MongoClient();
...
await client.connect(...)
...const collection = client.database("deno").collection("localStorage");
await setup({
adapter: new MongoAdapter(collection),
});
```### PostgreSQL
```ts
import { Client } from "https://deno.land/x/postgres/mod.ts";
import { PostgresAdapter } from "https://deno.land/x/pls/adapters/postgres.ts";const client = new Client(...);
...
await client.connect()
...const table = "localStorage";
await setup({
adapter: await new PostgresAdapter(client, table).initialize(),
});
```### Redis
```ts
import { connect } from "https://deno.land/x/redis/mod.ts";
import { RedisAdapter } from "https://deno.land/x/pls/adapters/redis.ts";const redis = await connect(...);
await setup({
adapter: new RedisAdapter(redis),
});
```After that, you’ll just use `localStorage` as you normally would, and...
Everything will be synchronized with your database!
## TTL
By default, `localStorage` will be checked every 20 seconds to see if there were
any changes were made to it, and if there were, they will get pushed to the
database adapter you provided when calling `setup()`.You can configure the duration, or just disable it and call the flush method
whenever you like.### Configuring
```ts
await setup({
ttl: 30000, // in milliseconds
});
```> Note that you should not set it too low, otherwise you may face unexpected
> problems.### Disabling
```ts
await setup({ ttl: null });
```You can then call `flush()` whenever you’d liked to synchronize:
```ts
import { flush } from "https://deno.land/x/pls/mod.ts";flush();
```## Including/excluding
You can decide on which keys of pattern of keys should be included or excluded
when persisting them:```ts
await setup({
include: ["someSpecificKeyToInclude", /^someRegExpToInclude/],
exclude: ["someSpecificKeyToExclude", /someRegExpToExclude$/],
});
```---
Written by [@roj1512](https://github.com/roj1512). Under [WTFPL](./LICENSE).