0.0.280 • Published 6 months ago

ashpack-soil v0.0.280

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

SOIL Documentation

SOIL is a strongly-typed opinionated wrapper around Firebase's Real-Time Database meant to supercharge Firebase and allow for better relational management and out-of-the-box security rules. It comes with a lot of other features as well, such as type-safety, easy authentication, and solutions for pagination, infinite scroll, and more as built-in results to using database keys effectively.

Lastly, the package includes a nearly exhaustive list of helper functions, hooks, context generators, and components that make working with soil and building a serverless architecture nearly effortless.

This project was the brainchild of @dmurawsky and was given lots of love by him and a co-developer to flesh out the ecosystem that makes working with soil effective and enjoyable.

Initial Setup

Pull in the SoilContextProviderComponent and use it like so:

  <SoilContextProviderComponent firebaseOptions={FIREBASE_OPTIONS}>
    {yourAppHere}
  </SoilContextProviderComponent>

This will initialize the database connection.

Authentication

We provide authentication helper functions which will work once nested in the SoilContextProviderComponent. A good place to start is to simply use the signUp and signIn functions, but check here for more:

  • ashpack-soil/services/auth.ts

An even faster approach would be to use the given components:

  • ashpack-soil/components/SignUp/index.tsx
  • ashpack-soil/components/SignUp/index.tsx

Data Types

The first step to building a project with soil is to think about your data types and begin building them out. To do this, navigate from the root to @types/ashpack-soil/index.d.ts and paste the following:

import * as soilTypes from "ashpack-soil";

declare module "ashpack-soil" {
  export interface SoilDatabase extends soilTypes.SoilDatabase {
    /** Example JS Doc explaining the `manager` dataType */
    manager: { firstName: string; lastName: string };
  }
}

This will set soil up to receive the proper types. Now begin building out your types, likely importing them from another file and assigning them to the relevant keys here. For example, you might have a data type for each of the following: manager, user, profile, project, and message.

The structure that will be saved is as follows: data/user/{uid}/userData where userData will include the information provided in your type plus a few soil-things (such as permissions and timestamps).

Data Keys

A data key is simply the key for accessing a particular instance of a data type. For example, the uid of a user is the data key for the user data type. You can use Firebase Push Keys for your data keys, but when possible, Soil really shines when you get creative with your keys. For example, the data key to a message could be {userId}__{projectId}__{pushKey}. In this way, without even fetching the message data, with the key alone you would be able to fetch the user who created the message and the project that the message is under.

To generate keys like this, use the generateDbKey function, and the parse the same, use parseDbKey:

const key = generateDbKey("{userId}", "{projectId}", "{pushKey}");
const [userId, projectId, pushKey] = parseDbKey();

This is part of why the JS Docs are so important. Make sure that each data type has a doc explaining how it is keyed and any other useful information, such as what it will be connected to and its role in the database architecture.

Connection Data Lists (CDL)

Connecting data together is how we make Soil relational. Any time data is created or updated (using createData or updateData) you can pass in a list of connections. You can also directly call createConnection. This creates a two-way connection between two data types, for example, between a user and all of the projects that user is managing.

As such, you can use soil functions to then get all of the connected data of a particular thing. For example, when you render a user's project list, you simply fetch all the connected projects' data for that user. Inversely, for a project, you could fetch all connected users' data.

If you want to create a unique list of existing data of any kind, you can do so by adding that list as an empty object in the types file like so:

import * as soilTypes from "ashpack-soil";

declare module "ashpack-soil" {
  export interface SoilDatabase extends soilTypes.SoilDatabase {
    manager: { firstName: string; lastName: string };
    user: { email: string; };
    favorite: {};
  }
}

For example, if you have all of your users under the user data type but want to save a specific sublist of users, you would make a favorite data type that does not directly have any data because it is simply a list referencing the user data type. You would create a connection between a manager and a favorite using the {uid} of the favorited user. You would then be able to save, fetch, and remove favorited users of the manager this way, simply by creating and removing connections between manager and favorite via user {uid}.

User Data Lists (UDL)

User data lists are similar to connection data lists, but they are less flexible and specifically connect a user to a piece of data (rather than a piece of data to another piece of data). This exists to tie into the security system. Just like you can set connections, you can also set owners, and the owner of a piece of data has priviledges to modify that data.

NOTE: User data lists may end up being deprecated. If so, you would rely on connections.

Security Access

In order to read data:

  • The the user must either own the data or be connected to the data or...
  • The data must be set as publicAccess: true or have the appropriate connectionAccess.

We should explain the connectionAccess, so an example is provided below:

await createData({
  data,
  dataKey,
  dataType: "project",
  owners: [managerKey],
  connectionAccess: {
    connectionKey: managerKey,
    connectionType: "manager",
    uidDataType: "user",
    read: true,
    write: true,
  },
});

Here, we are saying that any user that is connected to the manager that owns this project also has access to read and write this project.

Once/Get/On/Use

Firebase has a terminology which we have tried to extend: onValue and onceValue. The on represents an open data connection using Firebase's built-in websockets. Use this when you want a live subscription to the data. This is often likely the default. Sometimes, though, that is unnecessary. If you only need to fetch the data as in a normal CRUD API, you use once.

We have mostly extended this terminlogy, but we sometimes use (1) get instead of once and (2) use instead of on for reasons explained in the following section.

CRUD Operations

There are too many helper functions to mention here. It suffices to say that if there is a createData and an updateData, there is also a removeData. If there is a createConnection, there is a removeConnection. IDE auto-complete and common sense are your allies in this regard.

When you want to fetch a piece of data, you can call getDataKeyValue. If you want to subscribe to a piece of data, you can call useDataKeyValue. The reason we use the terminology use here instead of once is because this is a custom hook. In order for the subscription to work, it needs a useEffect and a useState.

Nonetheless, all of this is obsfucated away from the developer who can simply reach for the data as they need it. Just be sure to check what is happening under-the-hood to make sure you aren't doing something otherwise silly, like calling a custom hook within a useEffect.

Helper Functions & Hooks

Take a moment to skim these two files in order to understand the underlying functions for working with a Soil Database:

  • ashpack-soil/services/client-data.ts
  • ashpack-soil/services/server-data.ts

Feel free to look at ashpack-soil/hooks, but you will probably mainly use the following to start:

  • useDataKeyValue
  • useGetDataKeyValue
  • useConnectionsTypeData

Context Generators

One of the neatest features that Soil provides is a way to quickly build out your global state using context generaters. See:

  • ashpack-soil/context

There are two likely to be used the most:

  • createConnectionsTypeDataContext
  • createConnectionsTypeConnectionsContext

Below is the example usage of createConnectionsTypeDataContext:

import { createConnectionsTypeDataContext } from "ashpack-soil/context/createConnectionsTypeDataContext";

const {
  useConnectionsTypeDataContext: useManagerUsersContext,
  ConnectionsTypeDataContextProviderComponent: ManagerUsersContextProviderComponent,
} = createConnectionsTypeDataContext("manager", "user");

export { useManagerUsersContext, ManagerUsersContextProviderComponent };
  <ManagerUsersContextProviderComponent>
    {globalContextWrappedAppHere}
  </ManagerUsersContextProviderComponent>
const { dataArray: usersConnectedToTheManager } = useManagerUsersContext("{managerUid}")

The optional argument initialChildEqualTo (unused in the example) in the useManagerUsersContext call is meant to speed up the initial fetch with a Firebase Query. See the relevant createConnectionsTypeDataContext code and Firebase query documentation for more details.

Components

There are a few components that we have provided, they can be found in:

  • ashpack-soil/components

The one which helps with infinite scroll is the DataInViewItem. There is another component, pending entry into Soil, which makes this truly powerful and easy to use. It is called the ConnectionsObserverHOC and is used like so:

<ConnectionsObserverHOC
  listItemMinHeight="80px"
  listItemMinWidth="80px"
  className={styles.productList}
  version="connectionDataList"
  parentDataType="manager"
  parentDataKey={managerKey}
  dataType="user"
  sort="created newest"
  ItemComponent={UserInView}
  EmptyComponent={NoUsers}
/>

It has not yet been brought into the package, but look for it soon!

Impactful Conventions

There are a few conventions we employ that have real consequences in using Soil.

Initialization and Falsey Keys in Helpers

When using a helper function like useDataKeyValue, if the dataKey is falsey (an empty string), the fetch will not be made at all. This is very useful. For example, if you are hydrating data on load, it will not bother to fetch the manager's users until the manager has been logged in and we have retrieved the manager's data key. Furthermore, you can control this directly with a third boolean argument: initialized.

Null vs Undefined

Note, passing undefined to Firebase at any time will break. Firebase only allows null, which will delete the data at that location. Therefore, your types won't perfectly reflect the reality that: Firebase may return undefined to you when nothing is at a target database location but will break if you pass undefined to a database location. Similarly, you may pass null to a database location to delete that data, but if you fetch that location, you will get undefined rather than null. This is a Firebase quirk to be aware of. Also note, when deleting data key values (an instance of a data type, ie. a user), do not use null, use the given Soil function: removeData.

However, even though Firebase returns undefined in such cases, most Soil helper functions override this with null. We consider undefined values to mean that the fetching has not yet been done and null values to mean that the fetching was done but nothing was returned. In this way, not only do we harmonize Firebase's undefined/null descrepancy, but also we enable the developer to know if it is loading or loaded regardless of whether or not there was a valid resource at the target location just by checking the value of undefined vs null.

0.0.279

7 months ago

0.0.274

10 months ago

0.0.273

10 months ago

0.0.272

10 months ago

0.0.271

10 months ago

0.0.278

7 months ago

0.0.277

7 months ago

0.0.276

9 months ago

0.0.275

9 months ago

0.0.270

10 months ago

0.0.280

6 months ago

0.0.269

10 months ago

0.0.268

10 months ago

0.0.263

10 months ago

0.0.262

10 months ago

0.0.261

10 months ago

0.0.260

11 months ago

0.0.267

10 months ago

0.0.266

10 months ago

0.0.265

10 months ago

0.0.264

10 months ago

0.0.259

11 months ago

0.0.258

11 months ago

0.0.257

11 months ago

0.0.238

11 months ago

0.0.239

11 months ago

0.0.249

11 months ago

0.0.248

11 months ago

0.0.247

11 months ago

0.0.246

11 months ago

0.0.241

11 months ago

0.0.240

11 months ago

0.0.245

11 months ago

0.0.244

11 months ago

0.0.243

11 months ago

0.0.242

11 months ago

0.0.252

11 months ago

0.0.251

11 months ago

0.0.250

11 months ago

0.0.256

11 months ago

0.0.255

11 months ago

0.0.254

11 months ago

0.0.253

11 months ago

0.0.227

12 months ago

0.0.226

12 months ago

0.0.225

12 months ago

0.0.229

12 months ago

0.0.228

12 months ago

0.0.237

11 months ago

0.0.236

12 months ago

0.0.235

12 months ago

0.0.230

12 months ago

0.0.234

12 months ago

0.0.233

12 months ago

0.0.232

12 months ago

0.0.231

12 months ago

0.0.224

1 year ago

0.0.223

1 year ago

0.0.222

1 year ago

0.0.216

1 year ago

0.0.215

1 year ago

0.0.214

1 year ago

0.0.213

1 year ago

0.0.219

1 year ago

0.0.218

1 year ago

0.0.217

1 year ago

0.0.221

1 year ago

0.0.220

1 year ago

0.0.205

1 year ago

0.0.204

1 year ago

0.0.203

1 year ago

0.0.202

1 year ago

0.0.209

1 year ago

0.0.208

1 year ago

0.0.207

1 year ago

0.0.206

1 year ago

0.0.212

1 year ago

0.0.211

1 year ago

0.0.210

1 year ago

0.0.201

1 year ago

0.0.200

1 year ago

0.0.189

1 year ago

0.0.188

1 year ago

0.0.197

1 year ago

0.0.196

1 year ago

0.0.195

1 year ago

0.0.194

1 year ago

0.0.199

1 year ago

0.0.198

1 year ago

0.0.193

1 year ago

0.0.192

1 year ago

0.0.191

1 year ago

0.0.190

1 year ago

0.0.159

2 years ago

0.0.153

2 years ago

0.0.152

2 years ago

0.0.151

2 years ago

0.0.150

2 years ago

0.0.157

2 years ago

0.0.156

2 years ago

0.0.155

2 years ago

0.0.154

2 years ago

0.0.169

2 years ago

0.0.164

2 years ago

0.0.163

2 years ago

0.0.162

2 years ago

0.0.161

2 years ago

0.0.168

2 years ago

0.0.167

2 years ago

0.0.166

2 years ago

0.0.165

2 years ago

0.0.160

2 years ago

0.0.175

2 years ago

0.0.174

2 years ago

0.0.173

2 years ago

0.0.172

2 years ago

0.0.179

2 years ago

0.0.178

2 years ago

0.0.177

2 years ago

0.0.176

2 years ago

0.0.171

2 years ago

0.0.170

2 years ago

0.0.186

1 year ago

0.0.185

1 year ago

0.0.184

1 year ago

0.0.183

1 year ago

0.0.187

1 year ago

0.0.182

2 years ago

0.0.181

2 years ago

0.0.180

2 years ago

0.0.119

2 years ago

0.0.118

2 years ago

0.0.128

2 years ago

0.0.127

2 years ago

0.0.126

2 years ago

0.0.125

2 years ago

0.0.129

2 years ago

0.0.120

2 years ago

0.0.124

2 years ago

0.0.123

2 years ago

0.0.122

2 years ago

0.0.121

2 years ago

0.0.139

2 years ago

0.0.138

2 years ago

0.0.137

2 years ago

0.0.136

2 years ago

0.0.131

2 years ago

0.0.130

2 years ago

0.0.135

2 years ago

0.0.134

2 years ago

0.0.133

2 years ago

0.0.132

2 years ago

0.0.149

2 years ago

0.0.148

2 years ago

0.0.147

2 years ago

0.0.146

2 years ago

0.0.145

2 years ago

0.0.144

2 years ago

0.0.143

2 years ago

0.0.84

2 years ago

0.0.85

2 years ago

0.0.86

2 years ago

0.0.87

2 years ago

0.0.88

2 years ago

0.0.89

2 years ago

0.0.80

2 years ago

0.0.81

2 years ago

0.0.82

2 years ago

0.0.83

2 years ago

0.0.74

2 years ago

0.0.75

2 years ago

0.0.76

2 years ago

0.0.77

2 years ago

0.0.78

2 years ago

0.0.79

2 years ago

0.0.106

2 years ago

0.0.105

2 years ago

0.0.104

2 years ago

0.0.103

2 years ago

0.0.109

2 years ago

0.0.108

2 years ago

0.0.107

2 years ago

0.0.102

2 years ago

0.0.101

2 years ago

0.0.100

2 years ago

0.0.117

2 years ago

0.0.116

2 years ago

0.0.115

2 years ago

0.0.114

2 years ago

0.0.113

2 years ago

0.0.112

2 years ago

0.0.111

2 years ago

0.0.110

2 years ago

0.0.95

2 years ago

0.0.96

2 years ago

0.0.97

2 years ago

0.0.98

2 years ago

0.0.99

2 years ago

0.0.90

2 years ago

0.0.91

2 years ago

0.0.92

2 years ago

0.0.93

2 years ago

0.0.94

2 years ago

0.0.73

2 years ago

0.0.72

2 years ago

0.0.71

2 years ago

0.0.70

2 years ago

0.0.68

2 years ago

0.0.67

2 years ago

0.0.66

2 years ago

0.0.65

2 years ago

0.0.64

2 years ago

0.0.63

2 years ago

0.0.62

2 years ago

0.0.61

2 years ago

0.0.60

2 years ago

0.0.59

2 years ago

0.0.58

2 years ago

0.0.57

2 years ago

0.0.56

2 years ago

0.0.55

2 years ago

0.0.54

2 years ago

0.0.53

2 years ago

0.0.52

2 years ago

0.0.51

2 years ago

0.0.50

2 years ago

0.0.49

2 years ago

0.0.44

2 years ago

0.0.39

2 years ago

0.0.34

2 years ago

0.0.33

2 years ago

0.0.32

2 years ago

0.0.31

2 years ago

0.0.30

2 years ago

0.0.29

2 years ago

0.0.28

2 years ago

0.0.27

2 years ago

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.8

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago