Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/1amageek/pring.ts
Cloud Firestore model framework for TypeScript - Google
https://github.com/1amageek/pring.ts
cloud-firestore cloudfunctions firebase firestore pring typescript
Last synced: 3 days ago
JSON representation
Cloud Firestore model framework for TypeScript - Google
- Host: GitHub
- URL: https://github.com/1amageek/pring.ts
- Owner: 1amageek
- License: mit
- Created: 2017-11-17T08:46:37.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-02-06T09:46:27.000Z (almost 6 years ago)
- Last Synced: 2024-10-30T16:13:21.269Z (15 days ago)
- Topics: cloud-firestore, cloudfunctions, firebase, firestore, pring, typescript
- Language: TypeScript
- Homepage: https://firebase.google.com/docs/firestore/
- Size: 1.11 MB
- Stars: 109
- Watchers: 6
- Forks: 7
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pring.ts
Firebase Cloud Firestore model framework for TypeScript.
- [Pring for iOS](https://github.com/1amageek/Pring)
- [Pring for Web](https://github.com/1amageek/pring.ts)
- [Pring for CloudFunctions](https://github.com/1amageek/pring-admin.ts)## Installation ⚙
`npm install pring --save `
## Usage
### TypeScript
required
```JSON
"devDependencies": {
"@types/node": "^10.9.2",
"typescript": "^3.0.3"
},
```### tsconfig.json
```JSON
{
"compilerOptions": {
"target": "es6",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"node"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
```### webpack.config.js
```js
const alias = require('pring/webpack-alias')module.exports = {
// ...
resolve: {
alias
},
}
```### Initialize
When using Pring in Vue please include it in `main.ts`.
#### For Web
``` typescript
import * as Pring from "pring"
import { config } from "./config"
import firebase from "firebase"
import "firebase/firestore"const app = firebase.initializeApp(config)
Pring.initialize(app.firestore())
```### Scheme
- Please add `@property` for property declaration.
- SubCollection can not be optional. Please initialize here.``` typescript
import * as Pring from "pring"
const property = Pring.propertyclass Group extends Pring.Base {
@property name: string
@property users: NestedCollection = NestedCollection(this)
}class User extends Pring.Base {
@property name: string
@property groups: ReferenceCollection = ReferenceCollection(this)
}
```### Manage data
#### Initialize
``` typescript// auto generate ID
let user = new User()// any ID
let user = new User("YOUR_ID")// any ID, Handle already saved users
let user = new User("YOUR_ID", {})
```
__memo__The developer is responsible for managing the Document being saved.
In Pring it is prohibited to save the already saved Document again.Please use explicitly by the initialization method.
`new User("YOUR_ID", {})` `let user = new User("YOUR_ID")`#### Save
``` typescript
let user = new User()
user.name = "hoge"
await user.save()
```#### Get
``` typescript
let user: User = await User.get("USER_ID", User)
```#### Update
``` typescript
let user: User = await User.get("USER_ID", User)
user.name = "UPDATE NAME"
await user.update()
`````` typescript
let user: User = new User("USER_ID", {})
user.name = "UPDATE NAME"
await user.update()
```#### Delete
``` typescript
let user: User = await User.get("USER_ID", User)
await user.delete()
```### SubCollection
You can use ReferenceCollection and NestedCollection.
The inserted Object is saved simultaneously with the save of the parent.``` typescript
let user = new User()
let group = new Group()
user.groups.insert(group)
await user.save()
```If you insert the parent after it is saved, you need to use `await` to guarantee the count of SubCollection.
``` typescript
let user = new User()
await user.save()let group0 = new Group()
let group1 = new Group()
try {
await user.groups.insert(group0)
await user.groups.insert(group1)
} catch(error) {
console.log(error)
}
```### DataSource
DataSource is a class that controls Collection of Firestore.``` typescript
export default class Home extends Vue {public async addUser() {
const user: User = new User()
user.name = "@1amageek"
await user.save()
}public created() {
const dataSource = User.query().dataSource(User)
dataSource.on((snapshot, changes) => {switch (changes.type) {
case "initial": {
console.log(dataSource.documents)
break
}
case "update": {
console.log("insert", changes.insertions)
console.log("change", changes.modifications)
console.log("delete", changes.deletions)
break
}
case "error": {
break
}
}
}).listen()
}
}
```## Test
https://facebook.github.io/jest/
```shell
npm install -g jest
``````shell
jest
```