TypeScript Persistent API (TSPA)
TypeScript Persistence API (TSPA) is a simple, lightweight, and easy-to-use library for creating and managing persistent data in a TypeScript application.
It provides a simple API for creating, reading, updating, and deleting data in a various data store such as LocalStorage, Firestore, and MongoDB (more to come).
Install
yarn install tspa
Usage
Note that you only need to set the environment variables for the data store you want to use. For example, if you want to use MongoDB, you only need to set the TSPA_MONGO_URI
and TSPA_MONGO_APP_NAME
environment variables.
.env
TSPA_MONGO_URI=your-mongo-uri
TSPA_MONGO_APP_NAME=your-app-name
TSPA_FIREBASE_API_KEY=your-api-key
TSPA_FIREBASE_AUTH_DOMAIN=your-auth-domain
TSPA_FIREBASE_PROJECT_ID=your-project-id
TSPA_FIREBASE_APP_NAME=your-app-name
TSPA_APP_NAME=your-app-name
TSPA_STORAGE_PATH=your-app-name
MongoDB
import { Entity, MongoCrudRepository } from 'tspa';
interface User extends Entity {
name: string;
email: string;
}
const user: User = {
id: '1',
name: 'John Doe',
email: 'johndoe@email.com'
}
const userRepository = MongoCrudRepository.initFor<User>('users', { entities: [{ user }], uri: TSPA_MONGO_URI, appName: TSPA_MONGO_APP_NAME });
MongoCrudRepository.init<User>({ entities: [{ user }], uri: TSPA_MONGO_URI, appName: TSPA_MONGO_APP_NAME });
const userRepository = MongoCrudRepository.for<User>('users');
const createdUser: User = userRepository.create(user);
Firestore
import { Entity, FirestoreCrudRepository } from 'tspa';
interface User extends Entity {
name: string;
email: string;
}
const user: User = {
id: '1',
name: 'John Doe',
email: 'johndoe@email.com'
}
const userRepository = FirestoreCrudRepository.initFor<User>('users', {
apiKey: 'apiKey',
authDomain: 'authDomain',
projectId: 'your-project-id',
appName: 'your-app-name'
});
FirestoreCrudRepository.init<User>({
apiKey: 'apiKey',
authDomain: 'authDomain',
projectId: 'your-project-id',
appName: 'your-app-name'
});
const userRepository = FirestoreCrudRepository.for<User>('users');
const createdUser: User = userRepository.create(user);
LocalStorage
import { Entity, LocalStorageCrudRepository } from 'tspa';
interface User extends Entity {
name: string;
email: string;
}
const user: User = {
id: '1',
name: 'John Doe',
email: 'johndoe@email.com'
};
const userRepository = LocalStorageCrudRepository.initFor<User>('user', {
appName: 'your-app-name',
storagePath: './db'
});
LocalStorageCrudRepository.init<User>({
appName: 'your-app-name',
storagePath: './db'
});
const userRepository = LocalStorageCrudRepository.for<User>('users');
const createdUser: User = userRepository.create(user);
Author
👤 Anietie Asuquo hello@anietieasuquo.com
🤝 Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page. You can also take a look at the contributing guide.
📝 License
Copyright © 2024 Anietie Asuquo hello@anietieasuquo.com.
This project is MIT licensed.