Laravel DynamoDB Eloquent 模型和查询构建器
Laravel DynamoDB 是一个基于 DynamoDB-based Eloquent 模型和查询构造器。 使用提供的 Dynamo 驱动程序,模型扩展了 Eloquent 基础模型:
use Kitar\Dynamodb\Model\Model;
class ProductCatalog extends Model
{
// 需要
protected $table = 'ProductCatalog';
// 主键的命名 (需要)
protected $primaryKey = 'Id';
// 排序键值的命名 (可选)
protected $sortKey = 'Subject';
// 当我们使用 find 不使用排序键时,默认的排序键值。
protected $sortKeyDefault = 'profile';
protected $fillable = ['Id', 'Price', 'Title'];
}
以下是 readme 中的几个示例,您可以使用此程序包的查询和操作:
// 获取所有模型
$products = ProductCatalog::scan();
// 或者
$products = ProductCatalog::all();
// 分页
$products = ProductCatalog::limit(5)->scan();
// 创建一个用户
$user = User::create([
'email' => 'foo@bar.com',
// Sort key. If we don't specify this,
// sortKeyDefault will be used.
'type' => 'profile',
]);
// 实例化一个模型然后保存。
$user = new User([
'email' => 'foo@bar.com',
'type' => 'profile'
]);
$user->save();
// 更新一个存在的模型
$user->update([
'name' => 'foobar'
]);
这个软件包包含了更高级的 Dynamo 用法,包括您可以在没有模型的情况下使用的查询构造器 (也可以在 Laravel 之外使用)。此外,查询构造器还支持 条件表达式。例如:
DB::table('ProductCatalog')
->condition('Id', 'attribute_not_exists')
->orCondition('Price', 'attribute_not_exists')
->putItem([/*...*/]);
以下是一个 过滤表达式 的实例,它可以在数据库返回结果之前使用表达式筛选结果:
$response = DB::table('Thread')
->filter('LastPostedBy', '=', 'User A')
->scan();
// orFilter()
$response = DB::table('Thread')
->filter('LastPostedBy', '=', 'User A')
->orFilter('LastPostedBy', '=', 'User B')
->scan();
// filterIn
$response = DB::table('Thread')
->filterIn('LastPostedBy', ['User A', 'User B'])
->scan();
// filterBetween
$response = DB::table('ProductCatalog')
->filterBetween('Price', [0, 100])
->scan();
DynamoDB 包还包含有关使用 DynamoDB 模型进行 用户身份验证 的详细说明。 你可以了解有关 DynamoDB 包的更多信息、获取完整的安装说明,并在 GitHub 上查看 源代码。
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。