Giter Club home page Giter Club logo

pls's Introduction

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

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:

await setup();

MongoDB

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

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

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

await setup({
  ttl: 30000, // in milliseconds
});

Note that you should not set it too low, otherwise you may face unexpected problems.

Disabling

await setup({ ttl: null });

You can then call flush() whenever you’d liked to synchronize:

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:

await setup({
  include: ["someSpecificKeyToInclude", /^someRegExpToInclude/],
  exclude: ["someSpecificKeyToExclude", /someRegExpToExclude$/],
});

Written by @roj1512. Under WTFPL.

pls's People

Contributors

roj1512 avatar

Stargazers

Michael Scott Hertzberg avatar Md. Abu Taher avatar Andrejs Agejevs avatar Roman Hossain Shaon avatar Charlike Mike Reagent avatar Tom Connors avatar Santiago Aguilar Hernández avatar Ahmed avatar Dunkan avatar

Forkers

coding-to-music

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.