Ballcap is a database schema design framework for Cloud Firestore. This repository supports the WEB and Admin.
Why Ballcap
Cloud Firestore is a great schema-less and flexible database that can handle data. However, its flexibility can create many bugs in development. Ballcap can assign schemas to Cloud Firestore to visualize data structures. This plays a very important role when developing as a team.
export class User extends Doc {
@Field name?: string
@Field thumbnailImage?: File
@SubCollection items: Collection<Item> = new Collection()
}
Web
npm add @1amageek/ballcap
Admin(node.js)
npm add @1amageek/ballcap-admin
expo init new-project
cd my-new-project
npm add firebase @1amageek/ballcap
{
"compilerOptions": {
"noEmit": true,
"target": "esnext",
"module": "commonjs",
"esModuleInterop": true,
"experimentalDecorators": true,
"lib": ["dom", "esnext"],
"jsx": "react-native",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
}
}
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import * as firebase from 'firebase'
import '@firebase/firestore'
import * as Ballcap from '@1amageek/ballcap'
const config = { ... } // apiKey, authDomain, etc. (see above)
const app = firebase.initializeApp(config)
Ballcap.initialize(app)
npx create-react-app my-new-project
cd my-new-project
npm add firebase @1amageek/ballcap ts-loader
react-scripts eject
https://gist.github.com/1amageek/184a6054f00f1a722d37f7b4cba406a0
{
"compilerOptions": {
"target": "es2015",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react"
},
"include": [
"src"
]
}
To use Ballcap, you need to initialize it.
Ballcap.initialize(app.firestore())
Considering the extensibility of DB, it is recommended to provide a method of version control.
Ballcap.initialize(app.firestore(), app.firestore().collection("version").doc("1"))
Document
// autoID
const user: User = new User()
// with ID
const user: User = new User("ID")
// with DocumentReference
const user: User = new User(firestore.doc("a/a"))
// save
await user.save()
// update
await user.upate()
// delete
await user.delete()
Get JSON
const data = user.data()
Batch
const user: User = new User()
const batch: Batch = new Batch()
batch.save(user)
await batch.commit()
// with id
const user?: User = await User.get("id")
// with DocumentReference
const user?: User = await User.get(firestore.doc("a/a"))
const user: User = User.fromSnapshot(documentSnapshot)
Use Field to represent a field in a document.
A Field can have another Document. In that case, use the @Codable
decorator.
export class Address extends Model {
@Field postCode?: string
@Field country?: string
}
export class Shipping extends Doc {
@Codable(Address)
@Field address?: Address
@Field phone?: string
}
export class User extends Doc {
@Field name?: string
@Field thumbnailImage?: File
@Codable(Address)
@Field address: Address[] = []
@Codable(Shipping, true)
@Field shipping?: Shipping
}
A Document in Ballcap can have nested Documents and Models. Use Cadable to provide type
to Ballcap
@Codable(Address)
@Field address: Address[] = []
If the nested object is a Document, it can have the following structure because the Document contains an ID.
{
id: "shipping id",
path: "shipping path",
data: {
// shipping data
}
}
If you want to keep the structure, set the second argument to Codable to true. By default, it is false. If false, only the data is retained.
@Codable(Shipping, true)
@Field shipping?: Shipping
Use SubCollection and Collection to represent SubCollection.
class Charge extends Doc {
@Field amount: number = 0
@Field userID!: string
}
class User extends Doc {
@SubCollection charges: Collection<Charge> = new Collection()
}
cd web
jest
firebase setup:emulators:firestore
firebase serve --only firestore
cd web
jest