To create a model class, first import the `Model` class from radiks. Then, create a class that extends this model, and provide a schema.
To create a model class, first import the `Model` class from radiks. Then, create a class that extends this model, and provide a schema.
**Important**: Make sure you add a static `className` property to your class. This is used when storing and querying information. If you don't add this, radiks will default to the actual model's class name. However, in production, your code will likely be minified, and the actual class name will be different. For this reason, it's highly recommended that you define the `className` manually.
**Important**: Make sure you add a static `className` property to your class. This is used when storing and querying information. If you don't add this, radiks will default to the actual model's class name. However, in production, your code will likely be minified, and the actual class name will be different. For this reason, it's highly recommended that you define the `className` manually.
We provide a `Model` class that you can extend to easily create, save, and fetch models.
```javascript
import { Model, User } from 'radiks';
class Todo extends Model {
static className = 'Todo';
static schema = { // all fields are encrypted by default
title: String,
completed: Boolean,
}
};
// after authentication:
const todo = new Todo({ title: 'Use Radiks in an app' });
await todo.save();
todo.update({
completed: true,
});
await todo.save();
const incompleteTodos = await Todo.fetchOwnList({ // fetch todos that this user created
completed: false
});
console.log(incompleteTodos.length); // 0
```
#### Schema
#### Schema
The first static property you'll need to define is a schema. Create a static `schema` property on your class to define it. Each `key` in this object is the name of the field. The value is whatever type you want the field to be, or you can pass some options.
The first static property you'll need to define is a schema. Create a static `schema` property on your class to define it. Each `key` in this object is the name of the field. The value is whatever type you want the field to be, or you can pass some options.
3. Configure your application to use your `radiks-server`.
To configure your applciation as a `radiks` client, use code that looks like this when starting up your application:
```js
import { UserSession, AppConfig } from 'blockstack';
import { configure } from 'radiks';
const userSession = new UserSession({
appConfig: new AppConfig(['store_write', 'publish_data'])
})
configure({
apiServer: 'http://localhost:1260',
userSession
});
```
For more information on configuring and writing a Radiks a client application, see [the Radiks client](https://github.com/blockstack-radiks/radiks) repository.
4. Create an `MONGODB_URI` environment variable on the same machine where you are running the `radiks-server`.
4. Create an `MONGODB_URI` environment variable on the same machine where you are running the `radiks-server`.
Use the `mongodb://username:password@host:port/db_name` format for your variable. For example, to set this variable in a `bash` shell:
Use the `mongodb://username:password@host:port/db_name` format for your variable. For example, to set this variable in a `bash` shell:
@ -163,20 +138,20 @@ If you are using `blockstack.js` version 18 or earlier, you must use the Radiks
## Task 3. Add startup code and build your application
### Configuration
To set up radiks.js, you only need to configure the URL that your Radiks-server instance is running on. If you're using the pre-built Radiks server, this will be `http://localhost:1260`. If you're in production or are using a custom Radiks server, you'll need to specify exactly which URL it's available at.
To set up radiks.js, you only need to configure the URL that your Radiks-server instance is running on. If you're using the pre-built Radiks server, this will be `http://localhost:1260`. If you're in production or are using a custom Radiks server, you'll need to specify exactly which URL it's available at.
Radiks also is compatible with version 19 of blockstack.js, which requires you to configure a `UserSession` object to handle all user-data-related methods. You'll need to define this and pass it to your Radiks configuration, so that Radiks can know how to fetch information about the current logged in user.
Radiks also is compatible with version 19 of blockstack.js, which requires you to configure a `UserSession` object to handle all user-data-related methods. You'll need to define this and pass it to your Radiks configuration, so that Radiks can know how to fetch information about the current logged in user.
To configure radiks, use code that looks like this when starting up your application:
### Configure your application to use your `radiks-server`.
To configure your application as a `radiks` client, do the following:
~~~javascript
1. Start your application so that a `UserSession` allows the app to both write and publish data:
```js
import { UserSession, AppConfig } from 'blockstack';
import { UserSession, AppConfig } from 'blockstack';
import { configure } from 'radiks';
import { configure } from 'radiks';
@ -185,18 +160,16 @@ const userSession = new UserSession({
})
})
configure({
configure({
apiServer: 'http://my-radiks-server.com',
apiServer: 'http://localhost:1260',
userSession
userSession
});
});
~~~
```
### Authentication
Most of your code will be informed by following [Blockstack's authentication documentation](https://github.com/blockstack/blockstack.js/blob/master/src/auth/README.md).
2. Add authentication to your application
After your user logs in with Blockstack, you'll have some code to save the user's data in localStorage. You'll want to use the same `UserSession` you configured with Radiks, which can be fetched from the `getConfig` method.
After your user logs in with Blockstack, you'll have some code to save the user's data in your applications `localStorage`. You'll want to use the same `UserSession` you configured with Radiks, which can be fetched from the `getConfig` method.
~~~javascript
```js
import { User, getConfig } from 'radiks';
import { User, getConfig } from 'radiks';
const handleSignIn = () => {
const handleSignIn = () => {
@ -206,42 +179,19 @@ const handleSignIn = () => {
await User.createWithCurrentUser();
await User.createWithCurrentUser();
}
}
}
}
~~~
```
Calling `User.createWithCurrentUser` will do a few things:
1. Fetch user data that Blockstack.js stores in `localStorage`
2. Save the user's public data (including their public key) in Radiks-server
3. Find or create a signing key that is used to authorize writes on behalf of this user
4. Cache the user's signing key (and any group-related signing keys) to make signatures and decryption happen quickly later on
## Models
Calling `User.createWithCurrentUser` does the following:
Creating models for your application's data is where radiks truly becomes helpful. We provide a `Model` class that you can extend to easily create, save, and fetch models.
* Fetch user data that Blockstack.js stores in `localStorage`
* Save the user's public data (including their public key) in Radiks-server
* Find or create a signing key that is used to authorize writes on behalf of this user
* Cache the user's signing key (and any group-related signing keys) to make signatures and decryption happen quickly later on
### Quick start
### Build and run your application
```javascript
After you have added Radiks to your application, build and run the application. Test the application by logging in with your Blockstack ID. Create some data using the application. If you inspect the MongoDB database, you should see the encrypted data stored in the database.
import { Model, User } from 'radiks';
class Todo extends Model {
## Where to go next
static className = 'Todo';
static schema = { // all fields are encrypted by default
title: String,
completed: Boolean,
}
};
// after authentication:
Creating models for your application's data is where radiks truly becomes helpful. To learn how to use models, see the [Create and use models](radiks-models.html) section.
const todo = new Todo({ title: 'Use Radiks in an app' });
await todo.save();
todo.update({
completed: true,
});
await todo.save();
const incompleteTodos = await Todo.fetchOwnList({ // fetch todos that this user created