Skip to content

Commit

Permalink
Merge pull request #145 from tompng/number_or_string_pkey
Browse files Browse the repository at this point in the history
primary_key is number|string
  • Loading branch information
tompng authored Jul 26, 2024
2 parents 02007a1 + 92c05e8 commit 16b9415
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 29 deletions.
26 changes: 14 additions & 12 deletions src/core/ArSyncStore.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import ArSyncApi from './ArSyncApi'
export type Request = { api: string; query: any; params?: any; id?: any }
export type Request = { api: string; query: any; params?: any; id?: IDType }

type IDType = number | string

class ModelBatchRequest {
timer: number | null = null
apiRequests = new Map<string,
Map<string,
{
query,
requests: Map<number, {
id: number
requests: Map<IDType, {
id: IDType
model?
callbacks: {
resolve: (model: any) => void
Expand All @@ -18,7 +20,7 @@ class ModelBatchRequest {
}
>
>()
fetch(api: string, query, id: number) {
fetch(api: string, query, id: IDType) {
this.setTimer()
return new Promise((resolve, reject) => {
const queryJSON = JSON.stringify(query)
Expand Down Expand Up @@ -68,7 +70,7 @@ type ParsedQuery = {
params: any
} | {}

type SyncField = { id: number; keys: string[] }
type SyncField = { id: IDType; keys: string[] }
type Unsubscribable = { unsubscribe: () => void }

class ArSyncContainerBase {
Expand Down Expand Up @@ -227,12 +229,12 @@ class ArSyncContainerBase {
type NotifyData = {
action: 'add' | 'remove' | 'update'
class: string
id: number
id: IDType
field?: string
}

class ArSyncRecord extends ArSyncContainerBase {
id: number
id: IDType
root: ArSyncStore
query
queryAttributes
Expand Down Expand Up @@ -426,7 +428,7 @@ class ArSyncCollection extends ArSyncContainerBase {
data: any[]
children: ArSyncRecord[]
aliasOrderKey = 'id'
fetching = new Set<number>()
fetching = new Set<IDType>()
constructor(parentSyncKeys: string[], path: string, query, data: any[], request, root: ArSyncStore, parentModel: ArSyncRecord | null, parentKey: string | null){
super()
this.root = root
Expand Down Expand Up @@ -466,7 +468,7 @@ class ArSyncCollection extends ArSyncContainerBase {
}
replaceData(data: any[] | { collection: any[]; ordering: Ordering }, parentSyncKeys: string[]) {
this.setSyncKeys(parentSyncKeys)
const existings = new Map<number, ArSyncRecord>()
const existings = new Map<IDType, ArSyncRecord>()
for (const child of this.children) existings.set(child.id, child)
let collection: any[]
if (Array.isArray(data)) {
Expand Down Expand Up @@ -502,7 +504,7 @@ class ArSyncCollection extends ArSyncContainerBase {
for (const el of newData) this.data.push(el)
this.subscribeAll()
}
consumeAdd(className: string, id: number) {
consumeAdd(className: string, id: IDType) {
const { first, last, direction } = this.ordering
const limit = first || last
if (this.children.find(a => a.id === id)) return
Expand Down Expand Up @@ -585,7 +587,7 @@ class ArSyncCollection extends ArSyncContainerBase {
this.data.sort((a, b) => a[orderKey] > b[orderKey] ? -1 : +1)
}
}
consumeRemove(id: number) {
consumeRemove(id: IDType) {
const idx = this.children.findIndex(a => a.id === id)
this.fetching.delete(id) // To cancel consumeAdd
if (idx < 0) return
Expand All @@ -610,7 +612,7 @@ class ArSyncCollection extends ArSyncContainerBase {
super.onChange(path, data)
if (path[1] === this.aliasOrderKey) this.markAndSort()
}
markAndSet(id: number, data) {
markAndSet(id: IDType, data) {
this.mark()
const idx = this.children.findIndex(a => a.id === id)
if (idx >= 0) this.data[idx] = data
Expand Down
3 changes: 3 additions & 0 deletions test/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class User < BaseRecord
end

class Post < BaseRecord
before_validation do
self.id ||= '%08d' % (Post.maximum(:id).to_i + 1)
end
self.table_name = :posts
belongs_to :user
has_many :comments, dependent: :destroy
Expand Down
6 changes: 3 additions & 3 deletions test/seed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
create_table :users do |t|
t.string :name
end
create_table :posts do |t|
create_table :posts, id: :string do |t|
t.references :user
t.string :title
t.text :body
t.timestamps
end

create_table :comments do |t|
t.references :post
t.references :post, type: :string
t.references :user
t.text :body
t.timestamps
Expand All @@ -36,7 +36,7 @@
user_ids = User.ids

posts = 16.times.map do |i|
{ user_id: user_ids.sample, title: "Post#{i}", body: "post #{i}" }
{ id: '%08d' % (i + 1), user_id: user_ids.sample, title: "Post#{i}", body: "post #{i}" }
end
Post.import posts
post_ids = Post.ids
Expand Down
18 changes: 9 additions & 9 deletions test/sync_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class Schema
runner.eval_script <<-JAVASCRIPT
global.postModel = new ArSyncModel({
api: 'post',
params: { id: #{post.id} },
params: { id: #{post.id.to_json} },
query: {
comments: {
params: { orderBy: 'body', direction: '#{direction}' },
Expand Down Expand Up @@ -214,7 +214,7 @@ class Schema
runner.eval_script <<-JAVASCRIPT
global.postModel = new ArSyncModel({
api: 'post',
params: { id: #{post.id} },
params: { id: #{post.id.to_json} },
query: {
comments: {
params: { #{first_last}: 3, direction: '#{direction}' },
Expand Down Expand Up @@ -295,7 +295,7 @@ class Schema
runner.eval_script <<~JAVASCRIPT
global.postModel = new ArSyncModel({
api: 'post',
params: { id: #{post.id} },
params: { id: #{post.id.to_json} },
query: ['id','titleChars']
})
JAVASCRIPT
Expand All @@ -315,7 +315,7 @@ class Schema
runner.eval_script <<~JAVASCRIPT
global.postModel = new ArSyncModel({
api: 'post',
params: { id: #{post.id} },
params: { id: #{post.id.to_json} },
query: { comments: 'editedStarCount' }
})
JAVASCRIPT
Expand All @@ -333,7 +333,7 @@ class Schema
runner.eval_script <<~JAVASCRIPT
global.postsModel = new ArSyncModel({
api: 'Post',
params: { ids: [#{post1.id}, #{post2.id}] },
params: { ids: [#{post1.id.to_json}, #{post2.id.to_json}] },
query: ['id', 'title']
})
JAVASCRIPT
Expand All @@ -348,8 +348,8 @@ class Schema
post1 = Post.first
post2 = Post.second
runner.eval_script <<~JAVASCRIPT
global.p1 = new ArSyncModel({ api: 'Post', id: #{post1.id}, query: 'title' })
global.p2 = new ArSyncModel({ api: 'Post', id: #{post2.id}, query: 'title' })
global.p1 = new ArSyncModel({ api: 'Post', id: #{post1.id.to_json}, query: 'title' })
global.p2 = new ArSyncModel({ api: 'Post', id: #{post2.id.to_json}, query: 'title' })
JAVASCRIPT
runner.assert_script 'p1.data && p2.data'
runner.assert_script '[p1.data.title, p2.data.title]', to_be: [post1.title, post2.title]
Expand Down Expand Up @@ -401,8 +401,8 @@ class Schema
runner.eval_script <<~JAVASCRIPT
global.data1 = {}
global.data2 = {}
ArSyncApi.syncFetch({ api: 'Post', id: #{post.id}, query: 'title' }).then(data => { global.data1 = data })
ArSyncApi.fetch({ api: 'Post', id: #{post.id}, query: 'title' }).then(data => { global.data2 = data })
ArSyncApi.syncFetch({ api: 'Post', id: #{post.id.to_json}, query: 'title' }).then(data => { global.data1 = data })
ArSyncApi.fetch({ api: 'Post', id: #{post.id.to_json}, query: 'title' }).then(data => { global.data2 = data })
JAVASCRIPT
runner.assert_script 'data1.title', to_be: post.title
runner.assert_script 'data2.title', to_be: post.title
Expand Down
10 changes: 5 additions & 5 deletions test/type_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ isOK<IsEqual<typeof data2, { id: number; name: string | null }>>()
const data3 = new ArSyncModel({ api: 'currentUser', query: '*' }).data!
isOK<IsEqual<typeof data3, { id: number; name: string | null; posts: {}[]; postOrNull: {} | null; itemWithId: any; itemsWithId: any }>>()
const data4 = new ArSyncModel({ api: 'currentUser', query: { posts: 'id' } }).data!
isOK<IsEqual<typeof data4, { posts: { id: number }[] }>>()
isOK<IsEqual<typeof data4, { posts: { id: string }[] }>>()
const data5 = new ArSyncModel({ api: 'currentUser', query: { posts: '*' } }).data!
data5.posts[0].id; data5.posts[0].user; data5.posts[0].body
isOK<TypeIncludes<typeof data5.posts[0], { body: string | null, comments: {}[] }>>()
Expand All @@ -46,11 +46,11 @@ isOK<HasExtraField<typeof data11, 'commmments'>>()
const data12 = new ArSyncModel({ api: 'currentUser', query: { posts: { params: { first: 4 }, attributes: 'title' } } }).data!
isOK<IsEqual<(typeof data12.posts)[0], { title: string | null }>>()
const data13 = new ArSyncModel({ api: 'currentUser', query: { posts: { params: { first: 4 }, attributes: ['id', 'title'] } } }).data!
isOK<IsEqual<(typeof data13.posts)[0], { id: number; title: string | null }>>()
isOK<IsEqual<(typeof data13.posts)[0], { id: string; title: string | null }>>()
const data14 = new ArSyncModel({ api: 'currentUser', query: { posts: { params: { first: 4 }, attributes: { id: true, title: true } } } }).data!
isOK<IsEqual<(typeof data14.posts)[0], { id: number; title: string | null }>>()
isOK<IsEqual<(typeof data14.posts)[0], { id: string; title: string | null }>>()
const data15 = new ArSyncModel({ api: 'currentUser', query: { posts: ['id', 'title'] } } as const).data!
isOK<IsEqual<(typeof data15.posts)[0], { id: number; title: string | null }>>()
isOK<IsEqual<(typeof data15.posts)[0], { id: string; title: string | null }>>()
const data16 = new ArSyncModel({ api: 'User', id: 1, query: 'name' }).data!
isOK<IsEqual<typeof data16, { name: string | null }>>()
const data17 = new ArSyncModel({ api: 'currentUser', query: 'postOrNull' }).data!
Expand All @@ -62,6 +62,6 @@ isOK<TypeIncludes<typeof data19, { postOrNull: { title: string | null } | null }

const model = new ArSyncModel({ api: 'currentUser', query: { posts: ['id', 'title'] } } as const)
let digId = model.dig(['posts', 0, 'id'] as const)
isOK<IsEqual<typeof digId, number | null | undefined>>()
isOK<IsEqual<typeof digId, string | null | undefined>>()
let digTitle = model.dig(['posts', 0, 'title'] as const)
isOK<IsEqual<typeof digTitle, string | null | undefined>>()

0 comments on commit 16b9415

Please sign in to comment.