你需要先花3分钟配置Supabase免费云数据库,才能开启多设备同步功能,全程免费不需要信用卡。
步骤1:注册Supabase账号
打开 supabase.com,用GitHub账号或者邮箱注册,点击「New Project」新建一个免费项目,地区选新加坡(离国内近速度快),自己设置一个数据库密码记好。
步骤2:创建数据表
等项目创建完成(约1分钟),进入左侧菜单「SQL Editor」→「New query」,把下面这段SQL复制进去,点击「Run」执行就会自动创建好数据表:
create table records (
id uuid default gen_random_uuid() primary key,
user_id uuid references auth.users not null,
type text not null,
amount numeric not null,
category text not null,
note text,
date date not null,
created_at timestamp with time zone default timezone('utc'::text, now()) not null
);
alter table records enable row level security;
create policy "Users can view their own records" on records
for select using (auth.uid() = user_id);
create policy "Users can insert their own records" on records
for insert with check (auth.uid() = user_id);
create policy "Users can update their own records" on records
for update using (auth.uid() = user_id);
create policy "Users can delete their own records" on records
for delete using (auth.uid() = user_id);
如果执行后提示 relation "records" already exists,说明数据表之前已经建好了,跳过这一步即可。
步骤3:关闭邮箱验证(必做)
左侧菜单「Authentication」→「Providers」→ 找到 Email,把 Confirm email 开关关掉并保存。不关的话注册需要收邮件验证,而我们是账号密码直接登录,会提示登录失败。
步骤4:把这两个值填到最下面的输入框里
在 Supabase 左侧菜单「Project Settings」里找到下面两个值,复制后粘贴到最下面的两个输入框,点「保存配置」就完成了:
- Project URL:在「Data API」页面里,或点项目页右上角的「Connect」按钮复制,形如
https://xxxx.supabase.co
- Publishable key:在「API Keys」页面里,形如
sb_publishable_xxxx;老项目也可用「Legacy API Keys」里的 anon public 密钥
- 千万别填 Secret key(sb_secret_ 开头)