1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-01-24 14:26:58 +02:00

replaced DynamicList with a more generic (model) helper to allow creating pointer slice of any type

This commit is contained in:
Gani Georgiev 2023-06-25 20:19:01 +03:00
parent 39accdba58
commit 051b3702b0
4 changed files with 5315 additions and 5248 deletions

View File

@ -16,6 +16,39 @@ const heading = `
declare var $app: pocketbase.PocketBase
/**
* $arrayOf creates a placeholder array of the specified models.
* Usually used to populate DB result into an array of models.
*
* Example:
*
* ` + "```" + `js
* const records = $arrayOf(new Record)
*
* $app.dao().recordQuery(collection).limit(10).all(records)
* ` + "```" + `
*/
declare function $arrayOf<T>(model: T): Array<T>;
/**
* DynamicModel creates a new dynamic model with fields from the provided data shape.
*
* Example:
*
* ` + "```" + `js
* const model = new DynamicModel({
* name: ""
* age: 0,
* active: false,
* roles: [],
* meta: {}
* })
* ` + "```" + `
*/
declare class DynamicModel {
constructor(shape?: { [key:string]: any })
}
interface Record extends models.Record{} // merge
declare class Record implements models.Record {
constructor(collection?: models.Collection, data?: { [key:string]: any })
@ -244,23 +277,23 @@ declare class ApiError implements apis.ApiError {
constructor(status?: number, message?: string, data?: any)
}
interface NotFoundError extends apis.NotFoundError{} // merge
declare class NotFoundError implements apis.NotFoundError {
interface NotFoundError extends apis.ApiError{} // merge
declare class NotFoundError implements apis.ApiError {
constructor(message?: string, data?: any)
}
interface BadRequestError extends apis.BadRequestError{} // merge
declare class BadRequestError implements apis.BadRequestError {
interface BadRequestError extends apis.ApiError{} // merge
declare class BadRequestError implements apis.ApiError {
constructor(message?: string, data?: any)
}
interface ForbiddenError extends apis.ForbiddenError{} // merge
declare class ForbiddenError implements apis.ForbiddenError {
interface ForbiddenError extends apis.ApiError{} // merge
declare class ForbiddenError implements apis.ApiError {
constructor(message?: string, data?: any)
}
interface UnauthorizedError extends apis.UnauthorizedError{} // merge
declare class UnauthorizedError implements apis.UnauthorizedError {
interface UnauthorizedError extends apis.ApiError{} // merge
declare class UnauthorizedError implements apis.ApiError {
constructor(message?: string, data?: any)
}

File diff suppressed because it is too large Load Diff

View File

@ -67,6 +67,14 @@ func baseBinds(vm *goja.Runtime) {
}
`)
vm.Set("$arrayOf", func(model any) any {
mt := reflect.TypeOf(model)
st := reflect.SliceOf(mt)
elem := reflect.New(st).Elem()
return elem.Addr().Interface()
})
vm.Set("DynamicModel", func(call goja.ConstructorCall) *goja.Object {
shape, ok := call.Argument(0).Export().(map[string]any)
if !ok || len(shape) == 0 {
@ -80,19 +88,6 @@ func baseBinds(vm *goja.Runtime) {
return instanceValue
})
vm.Set("DynamicList", func(call goja.ConstructorCall) *goja.Object {
shape, ok := call.Argument(0).Export().(map[string]any)
if !ok || len(shape) == 0 {
panic("missing shape data")
}
instance := newDynamicList(shape)
instanceValue := vm.ToValue(instance).(*goja.Object)
instanceValue.SetPrototype(call.This.Prototype())
return instanceValue
})
vm.Set("Record", func(call goja.ConstructorCall) *goja.Object {
var instance *models.Record
@ -406,24 +401,6 @@ func filesContent(dirPath string, pattern string) (map[string][]byte, error) {
return result, nil
}
// newDynamicList creates a new dynamic slice of structs with fields based
// on the specified "shape".
//
// Example:
//
// m := newDynamicList(map[string]any{
// "title": "",
// "total": 0,
// })
func newDynamicList(shape map[string]any) any {
m := newDynamicModel(shape)
mt := reflect.TypeOf(m)
st := reflect.SliceOf(mt)
elem := reflect.New(st).Elem()
return elem.Addr().Interface()
}
// newDynamicModel creates a new dynamic struct with fields based
// on the specified "shape".
//

View File

@ -91,7 +91,7 @@ func TestBaseBindsCollection(t *testing.T) {
vm := goja.New()
baseBinds(vm)
v, err := vm.RunString(`new Collection({ name: "test", schema: [{name: "title", "type": "text"}] })`)
v, err := vm.RunString(`new Collection({ name: "test", createRule: "@request.auth.id != ''", schema: [{name: "title", "type": "text"}] })`)
if err != nil {
t.Fatal(err)
}
@ -105,6 +105,11 @@ func TestBaseBindsCollection(t *testing.T) {
t.Fatalf("Expected collection with name %q, got %q", "test", m.Name)
}
expectedRule := "@request.auth.id != ''"
if m.CreateRule == nil || *m.CreateRule != expectedRule {
t.Fatalf("Expected create rule %q, got %v", "@request.auth.id != ''", m.CreateRule)
}
if f := m.Schema.GetFieldByName("title"); f == nil {
t.Fatalf("Expected schema to be set, got %v", m.Schema)
}
@ -796,7 +801,7 @@ func TestLoadingDynamicModel(t *testing.T) {
}
}
func TestLoadingDynamicList(t *testing.T) {
func TestLoadingArrayOf(t *testing.T) {
app, _ := tests.NewTestApp()
defer app.Cleanup()
@ -806,10 +811,10 @@ func TestLoadingDynamicList(t *testing.T) {
vm.Set("$app", app)
_, err := vm.RunString(`
let result = new DynamicList({
let result = $arrayOf(new DynamicModel({
id: "",
text: "",
})
}))
$app.dao().db()
.select("id", "text")